From a2d54c73e2d50159fef593d112e1c1dccb8b4222 Mon Sep 17 00:00:00 2001 From: Steve Chaplin <> Date: Sat, 21 Apr 2012 21:40:53 +0800 Subject: Implement ImageSurface.get_data(), using some code from Paul Colomiets. bug #44935. --- src/cairomodule.c | 2 +- src/surface.c | 30 +++++++++++++++++++++++++----- 2 files changed, 26 insertions(+), 6 deletions(-) (limited to 'src') 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 * @@ -572,6 +570,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 @@ -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 */ -- cgit v1.2.3