[cairo-commit] pycairo/cairo pycairo-surface.c,1.79,1.80

Steve Chaplin commit at pdx.freedesktop.org
Mon Nov 20 03:21:03 PST 2006


Committed by: stevech1097

Update of /cvs/cairo/pycairo/cairo
In directory kemper:/tmp/cvs-serv10555/cairo

Modified Files:
	pycairo-surface.c 
Log Message:
'SC'

Index: pycairo-surface.c
===================================================================
RCS file: /cvs/cairo/pycairo/cairo/pycairo-surface.c,v
retrieving revision 1.79
retrieving revision 1.80
diff -u -d -r1.79 -r1.80
--- pycairo-surface.c	3 Jul 2006 03:25:26 -0000	1.79
+++ pycairo-surface.c	20 Nov 2006 11:20:59 -0000	1.80
@@ -542,6 +542,12 @@
 #endif /* CAIRO_HAS_PNG_FUNCTIONS */
 
 static PyObject *
+image_surface_get_data (PycairoImageSurface *o)
+{
+    return PyBuffer_FromReadWriteObject(o, 0, Py_END_OF_BUFFER);
+}
+
+static PyObject *
 image_surface_get_format (PycairoImageSurface *o)
 {
     return PyInt_FromLong (cairo_image_surface_get_format (o->surface));
@@ -624,6 +630,59 @@
     return buf;
 }
 
+static int
+image_surface_buffer_getreadbuf (PycairoImageSurface *o, int segment,
+				 const void **ptr)
+{
+    if (segment != 0) {
+	PyErr_SetString(PyExc_SystemError,
+			"accessing non-existent ImageSurface segment");
+	return -1;
+    }
+    cairo_surface_t *surface = o->surface;
+    int height = cairo_image_surface_get_height (surface);
+    int stride = cairo_image_surface_get_stride (surface);
+    *ptr = (void *) cairo_image_surface_get_data (surface);
+    return height * stride;
+}
+
+static int
+image_surface_buffer_getwritebuf (PycairoImageSurface *o, int segment,
+				  const void **ptr)
+{
+    if (segment != 0) {
+	PyErr_SetString(PyExc_SystemError,
+			"accessing non-existent ImageSurface segment");
+	return -1;
+    }
+    cairo_surface_t *surface = o->surface;
+    int height = cairo_image_surface_get_height (surface);
+    int stride = cairo_image_surface_get_stride (surface);
+    *ptr = (void *) cairo_image_surface_get_data (surface);
+    return height * stride;
+}
+
+static int
+image_surface_buffer_getsegcount (PycairoImageSurface *o, int *lenp)
+{
+    if (lenp) {
+	/* report the sum of the sizes (in bytes) of all segments */
+	cairo_surface_t *surface = o->surface;
+	int height = cairo_image_surface_get_height (surface);
+	int stride = cairo_image_surface_get_stride (surface);
+	*lenp = height * stride;
+    }
+    return 1;  /* surface data is all in one segment */
+}
+
+/* See Python C API Manual 10.7 */
+static PyBufferProcs image_surface_as_buffer = {
+    (getreadbufferproc) image_surface_buffer_getreadbuf,
+    (getwritebufferproc)image_surface_buffer_getwritebuf,
+    (getsegcountproc)   image_surface_buffer_getsegcount,
+    (getcharbufferproc) NULL,
+};
+
 static PyMethodDef image_surface_methods[] = {
 #ifdef HAVE_NUMPY
     {"create_for_array",(PyCFunction)image_surface_create_for_array,
@@ -635,6 +694,7 @@
     {"create_from_png", (PyCFunction)image_surface_create_from_png,
                                                    METH_O | METH_CLASS },
 #endif
+    {"get_data",      (PyCFunction)image_surface_get_data,       METH_NOARGS},
     {"get_format",    (PyCFunction)image_surface_get_format,     METH_NOARGS},
     {"get_height",    (PyCFunction)image_surface_get_height,     METH_NOARGS},
     {"get_width",     (PyCFunction)image_surface_get_width,      METH_NOARGS},
@@ -665,7 +725,7 @@
     0,                                  /* tp_str */
     0,                                  /* tp_getattro */
     0,                                  /* tp_setattro */
-    0,                                  /* tp_as_buffer */
+    &image_surface_as_buffer,		/* tp_as_buffer */
     Py_TPFLAGS_DEFAULT,                 /* tp_flags */
     0,                                  /* tp_doc */
     0,                                  /* tp_traverse */



More information about the cairo-commit mailing list