diff options
author | Steve Chaplin <> | 2012-04-21 21:40:53 +0800 |
---|---|---|
committer | Steve Chaplin <> | 2012-04-21 21:40:53 +0800 |
commit | a2d54c73e2d50159fef593d112e1c1dccb8b4222 (patch) | |
tree | 6a158862c8798f6f47aef264f4e17071df25e470 /src | |
parent | 0c5c297b67381f341e089e5a26c511807fa9290f (diff) |
Implement ImageSurface.get_data(), using some code from Paul Colomiets.
bug #44935.
Diffstat (limited to 'src')
-rw-r--r-- | src/cairomodule.c | 2 | ||||
-rw-r--r-- | src/surface.c | 30 |
2 files changed, 26 insertions, 6 deletions
diff --git a/src/cairomodule.c b/src/cairomodule.c index 675676d..886c6d5 100644 --- a/src/cairomodule.c +++ b/src/cairomodule.c @@ -255,7 +255,7 @@ PyInit__cairo(void) #endif #ifdef CAIRO_HAS_RECORDING_SURFACE if (PyType_Ready(&PycairoRecordingSurface_Type) < 0) - return; + return NULL; #endif #ifdef CAIRO_HAS_SVG_SURFACE if (PyType_Ready(&PycairoSVGSurface_Type) < 0) diff --git a/src/surface.c b/src/surface.c index 35ca169..7d00141 100644 --- a/src/surface.c +++ b/src/surface.c @@ -546,9 +546,7 @@ image_surface_format_stride_for_width (PyObject *self, PyObject *args) { static PyObject * image_surface_get_data (PycairoImageSurface *o) { - PyErr_SetString(PyExc_NotImplementedError, "Surface.get_data: Not Implemented yet."); - return NULL; - // return PyBuffer_FromReadWriteObject((PyObject *)o, 0, Py_END_OF_BUFFER); + return PyMemoryView_FromObject((PyObject*)o); } static PyObject * @@ -573,6 +571,24 @@ image_surface_get_width (PycairoImageSurface *o) { /* Buffer interface functions, used by ImageSurface.get_data() */ +static int +image_surface_buffer_getbufferproc (PycairoImageSurface *o, Py_buffer *view, + int flags) { + cairo_surface_t *surface = o->surface; + int height, stride; + void *data; + + height = cairo_image_surface_get_height (surface); + stride = cairo_image_surface_get_stride (surface); + data = cairo_image_surface_get_data (surface); + + if(!PyBuffer_FillInfo(view, (PyObject *)o, data, + height * stride, 0, PyBUF_CONTIG)) + return 0; + return -1; +} + +/* Buffer interface functions, used by ImageSurface.get_data() */ /* static int image_surface_buffer_getreadbuf (PycairoImageSurface *o, int segment, @@ -630,6 +646,11 @@ static PyBufferProcs image_surface_as_buffer = { (charbufferproc) NULL, }; */ +static PyBufferProcs image_surface_as_buffer = { + (getbufferproc) image_surface_buffer_getbufferproc, + (releasebufferproc) NULL, +}; + static PyMethodDef image_surface_methods[] = { {"create_for_data",(PyCFunction)image_surface_create_for_data, @@ -669,8 +690,7 @@ PyTypeObject PycairoImageSurface_Type = { 0, /* tp_str */ 0, /* tp_getattro */ 0, /* tp_setattro */ - // &image_surface_as_buffer, /* tp_as_buffer */ - 0, /* tp_as_buffer */ + &image_surface_as_buffer, /* tp_as_buffer */ Py_TPFLAGS_DEFAULT, /* tp_flags */ 0, /* tp_doc */ 0, /* tp_traverse */ |