Gentoo Websites Logo
Go to: Gentoo Home Documentation Forums Lists Bugs Planet Store Wiki Get Gentoo!
View | Details | Raw Unified | Return to bug 399381 | Differences between
and this patch

Collapse All | Expand All

(-)a/INSTALL (+2 lines)
Lines 9-15 $ ./waf install Link Here
9
Use
9
Use
10
$ python3 ./waf ...
10
$ python3 ./waf ...
11
if you have python2 and python3 installed, and the default is python 2.
11
if you have python2 and python3 installed, and the default is python 2.
12
However, you specify a full path to the python binary using a command like:
12
13
14
$ PYTHON=/usr/bin/python3 ./waf configure
13
15
14
Testing
16
Testing
15
-------
17
-------
(-)a/src/surface.c (-11 / +14 lines)
Lines 418-437 static PyObject * Link Here
418
image_surface_create_for_data (PyTypeObject *type, PyObject *args) {
418
image_surface_create_for_data (PyTypeObject *type, PyObject *args) {
419
  cairo_surface_t *surface;
419
  cairo_surface_t *surface;
420
  cairo_format_t format;
420
  cairo_format_t format;
421
  unsigned char *buffer;
422
  int width, height, stride = -1, res;
421
  int width, height, stride = -1, res;
423
  Py_ssize_t buffer_len;
424
  PyObject *obj;
422
  PyObject *obj;
425
423
  Py_buffer buffer;
426
  // buffer function disabled
427
  PyErr_SetString(PyExc_NotImplementedError, "Surface.create_for_data: Not Implemented yet.");
428
  return NULL;
429
424
430
  if (!PyArg_ParseTuple(args, "Oiii|i:Surface.create_for_data",
425
  if (!PyArg_ParseTuple(args, "Oiii|i:Surface.create_for_data",
431
			&obj, &format, &width, &height, &stride))
426
			&obj, &format, &width, &height, &stride))
432
    return NULL;
427
    return NULL;
433
428
434
  res = PyObject_AsWriteBuffer (obj, (void **)&buffer, &buffer_len);
429
  res = PyObject_GetBuffer (obj, &buffer, PyBUF_WRITABLE);
435
  if (res == -1)
430
  if (res == -1)
436
    return NULL;
431
    return NULL;
437
432
Lines 452-463 image_surface_create_for_data (PyTypeObject *type, PyObject *args) { Link Here
452
      return NULL;
447
      return NULL;
453
    }
448
    }
454
  }
449
  }
455
  if (height * stride > buffer_len) {
450
  if (height * stride > buffer.len) {
456
    PyErr_SetString(PyExc_TypeError, "buffer is not long enough");
451
    PyErr_SetString(PyExc_TypeError, "buffer is not long enough");
457
    return NULL;
452
    return NULL;
458
  }
453
  }
459
  Py_BEGIN_ALLOW_THREADS;
454
  Py_BEGIN_ALLOW_THREADS;
460
  surface = cairo_image_surface_create_for_data (buffer, format, width,
455
  surface = cairo_image_surface_create_for_data (buffer.buf, format, width,
461
						 height, stride);
456
						 height, stride);
462
  Py_END_ALLOW_THREADS;
457
  Py_END_ALLOW_THREADS;
463
  return PycairoSurface_FromSurface(surface, obj);
458
  return PycairoSurface_FromSurface(surface, obj);
Lines 546-554 image_surface_format_stride_for_width (PyObject *self, PyObject *args) { Link Here
546
541
547
static PyObject *
542
static PyObject *
548
image_surface_get_data (PycairoImageSurface *o) {
543
image_surface_get_data (PycairoImageSurface *o) {
549
  PyErr_SetString(PyExc_NotImplementedError, "Surface.get_data: Not Implemented yet.");
544
  Py_buffer buffer;
545
  void *data;
546
  int height, stride;
547
548
  height = cairo_image_surface_get_height (o->surface);
549
  stride = cairo_image_surface_get_stride (o->surface);
550
  data = cairo_image_surface_get_data (o->surface);
551
552
  if(!PyBuffer_FillInfo(&buffer, NULL, data, height * stride, 0, PyBUF_CONTIG))
553
    return PyMemoryView_FromBuffer(&buffer);
550
  return NULL;
554
  return NULL;
551
  // return PyBuffer_FromReadWriteObject((PyObject *)o, 0, Py_END_OF_BUFFER);
552
}
555
}
553
556
554
static PyObject *
557
static PyObject *
(-)a/test/isurface_create_for_data2.py (-1 / +1 lines)
Lines 27-30 surface = cairo.ImageSurface.create_for_data (data, cairo.FORMAT_ARGB32, Link Here
27
                                              width, height)
27
                                              width, height)
28
ctx = cairo.Context(surface)
28
ctx = cairo.Context(surface)
29
surface.write_to_png(fileName)
29
surface.write_to_png(fileName)
30
print "see %s output file" % fileName
30
print("see %s output file" % fileName)
(-)a/test/isurface_create_from_png.py (-4 / +4 lines)
Lines 18-24 surface = cairo.ImageSurface.create_from_png(inFileName) Link Here
18
# write to filename
18
# write to filename
19
_, outFileName = tempfile.mkstemp(prefix='pycairo_', suffix='.png')
19
_, outFileName = tempfile.mkstemp(prefix='pycairo_', suffix='.png')
20
surface.write_to_png(outFileName)
20
surface.write_to_png(outFileName)
21
print "see %s output file" % outFileName
21
print("see %s output file" % outFileName)
22
22
23
# write to file object
23
# write to file object
24
h, outFileName = tempfile.mkstemp(prefix='pycairo_', suffix='.png')
24
h, outFileName = tempfile.mkstemp(prefix='pycairo_', suffix='.png')
Lines 26-32 os.close(h) Link Here
26
f=file(outFileName, "wb")
26
f=file(outFileName, "wb")
27
surface.write_to_png(f)
27
surface.write_to_png(f)
28
f.close()
28
f.close()
29
print "see %s output file" % outFileName
29
print("see %s output file" % outFileName)
30
30
31
# write to object that has a "write" method
31
# write to object that has a "write" method
32
import StringIO
32
import StringIO
Lines 38-44 buf.close() Link Here
38
f=file(outFileName, "wb")
38
f=file(outFileName, "wb")
39
f.write(png_string)
39
f.write(png_string)
40
f.close()
40
f.close()
41
print "see %s output file" % outFileName
41
print("see %s output file" % outFileName)
42
42
43
# write to object that has a "write" method
43
# write to object that has a "write" method
44
_, outFileName = tempfile.mkstemp(prefix='pycairo_', suffix='.png')
44
_, outFileName = tempfile.mkstemp(prefix='pycairo_', suffix='.png')
Lines 50-56 buf.close() Link Here
50
f=file(outFileName, "wb")
50
f=file(outFileName, "wb")
51
f.write(png_string)
51
f.write(png_string)
52
f.close()
52
f.close()
53
print "see %s output file" % outFileName
53
print("see %s output file" % outFileName)
54
54
55
# error test - to check the error message, should raise TypeError
55
# error test - to check the error message, should raise TypeError
56
#surface.write_to_png(101)
56
#surface.write_to_png(101)
(-)a/test/isurface_get_data.py (-50 lines)
Lines 1-50 Link Here
1
#!/usr/bin/env python
2
"""
3
Test ImageSurface.get_data()
4
"""
5
import tempfile
6
7
import cairo
8
import numpy
9
10
if not (cairo.HAS_IMAGE_SURFACE and cairo.HAS_PNG_FUNCTIONS):
11
  raise SystemExit ('cairo was not compiled with ImageSurface and PNG support')
12
13
w, h = 128, 128
14
15
surface = cairo.ImageSurface(cairo.FORMAT_ARGB32, w, h)
16
ctx = cairo.Context(surface)
17
18
ctx.set_source_rgb(1, 1, 1)  # white
19
ctx.set_operator(cairo.OPERATOR_SOURCE)
20
ctx.paint()
21
22
# Draw out the triangle using absolute coordinates
23
ctx.move_to(w/2, h/3)
24
ctx.line_to(2*w/3, 2*h/3)
25
ctx.rel_line_to(-1*w/3, 0)
26
ctx.close_path()
27
28
ctx.set_source_rgb(0, 0, 0)  # black
29
ctx.set_line_width(15)
30
ctx.stroke()
31
_, outFileName = tempfile.mkstemp(prefix='pycairo_', suffix='.png')
32
surface.write_to_png(outFileName)
33
print "see %s output file" % outFileName
34
35
# modify surface using numpy
36
buf = surface.get_data()
37
# alternative which should work (?) but reports
38
# TypeError: buffer is read-only
39
# - is a Python bug?
40
#buf = buffer (surface1)
41
42
a = numpy.ndarray(shape=(w,h,4), dtype=numpy.uint8, buffer=buf)
43
44
# draw a vertical line
45
a[:,40,0] = 255  # byte 0 is blue on little-endian systems
46
a[:,40,1] = 0
47
a[:,40,2] = 0
48
_, outFileName = tempfile.mkstemp(prefix='pycairo_', suffix='.png')
49
surface.write_to_png(outFileName)
50
print "see %s output file" % outFileName
(-)a/test/isurface_get_data1.py (+50 lines)
Line 0 Link Here
1
#!/usr/bin/env python
2
"""
3
Test ImageSurface.get_data()
4
"""
5
import tempfile
6
7
import cairo
8
import numpy
9
10
if not (cairo.HAS_IMAGE_SURFACE and cairo.HAS_PNG_FUNCTIONS):
11
  raise SystemExit ('cairo was not compiled with ImageSurface and PNG support')
12
13
w, h = 128, 128
14
15
surface = cairo.ImageSurface(cairo.FORMAT_ARGB32, w, h)
16
ctx = cairo.Context(surface)
17
18
ctx.set_source_rgb(1, 1, 1)  # white
19
ctx.set_operator(cairo.OPERATOR_SOURCE)
20
ctx.paint()
21
22
# Draw out the triangle using absolute coordinates
23
ctx.move_to(w/2, h/3)
24
ctx.line_to(2*w/3, 2*h/3)
25
ctx.rel_line_to(-1*w/3, 0)
26
ctx.close_path()
27
28
ctx.set_source_rgb(0, 0, 0)  # black
29
ctx.set_line_width(15)
30
ctx.stroke()
31
_, outFileName = tempfile.mkstemp(prefix='pycairo_', suffix='.png')
32
surface.write_to_png(outFileName)
33
print("see %s output file" % outFileName)
34
35
# modify surface using numpy
36
buf = surface.get_data()
37
# alternative which should work (?) but reports
38
# TypeError: buffer is read-only
39
# - is a Python bug?
40
#buf = buffer (surface1)
41
42
a = numpy.ndarray(shape=(w,h,4), dtype=numpy.uint8, buffer=buf)
43
44
# draw a vertical line
45
a[:,40,0] = 255  # byte 0 is blue on little-endian systems
46
a[:,40,1] = 0
47
a[:,40,2] = 0
48
_, outFileName = tempfile.mkstemp(prefix='pycairo_', suffix='.png')
49
surface.write_to_png(outFileName)
50
print("see %s output file" % outFileName)
(-)a/test/isurface_get_data2.py (-1 / +47 lines)
Line 0 Link Here
0
- 
1
#!/usr/bin/env python
2
"""
3
Test ImageSurface.get_data()
4
"""
5
import tempfile
6
7
import cairo
8
9
import IPython
10
11
if not (cairo.HAS_IMAGE_SURFACE and cairo.HAS_PNG_FUNCTIONS):
12
  raise SystemExit ('cairo was not compiled with ImageSurface and PNG support')
13
14
w, h = 128, 128
15
16
surface = cairo.ImageSurface(cairo.FORMAT_ARGB32, w, h)
17
ctx = cairo.Context(surface)
18
19
ctx.set_source_rgb(1, 1, 1)  # white
20
ctx.set_operator(cairo.OPERATOR_SOURCE)
21
ctx.paint()
22
23
# Draw out the triangle using absolute coordinates
24
ctx.move_to(w/2, h/3)
25
ctx.line_to(2*w/3, 2*h/3)
26
ctx.rel_line_to(-1*w/3, 0)
27
ctx.close_path()
28
29
ctx.set_source_rgb(0, 0, 0)  # black
30
ctx.set_line_width(15)
31
ctx.stroke()
32
_, outFileName = tempfile.mkstemp(prefix='pycairo_', suffix='.png')
33
surface.write_to_png(outFileName)
34
print("see %s output file" % outFileName)
35
36
buf = surface.get_data()
37
38
for i in range(h):
39
    offset = (i * surface.get_stride()) + 120
40
    buf[offset] = b'\xFF'
41
    buf[offset + 1] = b'\x00'
42
    buf[offset + 2] = b'\x00'
43
44
_, outFileName = tempfile.mkstemp(prefix='pycairo_', suffix='.png')
45
surface.write_to_png(outFileName)
46
print("see %s output file" % outFileName)
47

Return to bug 399381