[cairo-commit] pycairo/cairo pycairo-surface.c, 1.24, 1.25 Makefile.am, 1.9, 1.10

Steve Chaplin commit at pdx.freedesktop.org
Thu Apr 14 08:47:22 PDT 2005


Committed by: stevech1097

Update of /cvs/cairo/pycairo/cairo
In directory gabe:/tmp/cvs-serv7437/cairo

Modified Files:
	pycairo-surface.c Makefile.am 
Log Message:
SC 2005/04/14

Index: pycairo-surface.c
===================================================================
RCS file: /cvs/cairo/pycairo/cairo/pycairo-surface.c,v
retrieving revision 1.24
retrieving revision 1.25
diff -u -d -r1.24 -r1.25
--- pycairo-surface.c	14 Apr 2005 12:05:26 -0000	1.24
+++ pycairo-surface.c	14 Apr 2005 15:47:20 -0000	1.25
@@ -38,6 +38,11 @@
 #include "pycairo-private.h"
 #include "pycairo-misc.h"
 
+#ifdef HAVE_NUMPY
+#  include <Numeric/arrayobject.h>
+   static int load_numpy(void);
+#endif
+
 /* Class Surface ----------------------------------------------------------- */
 
 /* PyCairoSurface_FromSurface
@@ -288,6 +293,19 @@
 
 /* Class ImageSurface ------------------------------------------------------ */
 
+PyObject *
+PyCairoImageSurface_FromImageSurface(cairo_surface_t *surface, PyObject *base)
+{
+    PyCairoImageSurface *s = (PyCairoImageSurface *)PyCairoImageSurface_Type.tp_new
+	(&PyCairoImageSurface_Type, NULL, NULL);
+    if (s) {
+	s->surface = surface;
+	s->base = base;
+	Py_XINCREF(base);
+    }
+    return (PyObject *) s;
+}
+
 static int
 image_surface_init(PyCairoSurface *s, PyObject *args, PyObject *kwds)
 {
@@ -307,6 +325,72 @@
     return 0;
 }
 
+#ifdef HAVE_NUMPY
+static PyObject *
+image_surface_create_for_array(PyTypeObject *type, PyObject *args)
+{
+    PyObject *s;
+    PyArrayObject *array;
+    cairo_format_t format;
+    cairo_surface_t *surface;
+    int nd;
+
+    if (!load_numpy())
+	return NULL;
+
+    if (!PyArg_ParseTuple(args, "O!:surface_create_for_array",
+			  &PyArray_Type, &array))
+	return NULL;
+
+    if (array->descr->type_num != PyArray_UBYTE) {
+	PyErr_SetString(PyExc_TypeError, "array data must be unsigned bytes");
+	return NULL;
+    }
+
+    nd = array->nd;
+    if (nd < 2) {
+	PyErr_SetString(PyExc_TypeError,
+			"array must have at least two dimensions");
+	return NULL;
+    }
+    if (nd == 2 || (nd == 3 && array->dimensions[2] == 1)) {
+	if (array->strides[1] != 1) {
+	    PyErr_SetString(PyExc_TypeError, "second axis must be contiguous");
+	    return NULL;
+	}
+	format = CAIRO_FORMAT_A8;
+    } else if (nd == 3 && array->dimensions[2] == 3) {
+	if (array->strides[1] != 3) {
+	    PyErr_SetString(PyExc_TypeError, "second axis must be contiguous");
+	    return NULL;
+	}
+	format = CAIRO_FORMAT_RGB24;
+    } else if (nd == 3 && array->dimensions[2] == 4) {
+	if (array->strides[1] != 4) {
+	    PyErr_SetString(PyExc_TypeError, "second axis must be contiguous");
+	    return NULL;
+	}
+	format = CAIRO_FORMAT_ARGB32;
+    } else {
+	PyErr_SetString(PyExc_TypeError,
+			"array must be MxN or MxNxP where P is 1, 3 or 4");
+	return NULL;
+    }
+    surface = cairo_image_surface_create_for_data(array->data, 
+						  format,
+						  array->dimensions[1],
+						  array->dimensions[0],
+						  array->strides[0]);
+    if (!surface)
+	return PyErr_NoMemory();
+
+    s = PyCairoImageSurface_FromImageSurface(surface, (PyObject *)array);
+    if (!s)
+	cairo_surface_destroy(surface);
+    return s;
+}
+#endif /* HAVE_NUMPY */
+
 #if 0 /* disable until a reference to the buffer is added to the surface */
 /* alternative constructor */
 static PyObject *
@@ -356,7 +440,7 @@
     if (!surface)
 	return PyErr_NoMemory();
 
-    s = PyCairoSurface_FromSurface(surface, NULL);
+    s = PyCairoImageSurface_FromImageSurface(surface, NULL);
     if (!s)
 	cairo_surface_destroy(surface);
     return s;
@@ -364,6 +448,7 @@
 }
 #endif
 
+#ifdef CAIRO_HAS_PNG_FUNCTIONS
 /* alternative constructor */
 static PyObject *
 image_surface_create_for_png(PyTypeObject *type, PyObject *args)
@@ -384,7 +469,7 @@
 			"not be allocated for operation");
 	return NULL;
     }
-    s = PyCairoSurface_FromSurface(surface, NULL);
+    s = PyCairoImageSurface_FromImageSurface(surface, NULL);
     if (!s)
 	cairo_surface_destroy(surface);
 
@@ -398,8 +483,14 @@
      * since cairo_image_surface_create_for_png() calls fclose(file)
      */
 }
+#endif /* CAIRO_HAS_PNG_FUNCTIONS */
+
 
 static PyMethodDef image_surface_methods[] = {
+#ifdef HAVE_NUMPY
+    { "create_for_array",(PyCFunction)image_surface_create_for_array, 
+                                                   METH_VARARGS | METH_CLASS },
+#endif
 #if 0
     { "create_for_data",(PyCFunction)image_surface_create_for_data, 
                                                    METH_VARARGS | METH_CLASS },
@@ -455,3 +546,39 @@
     0,                                  /* tp_is_gc */
     0,                                  /* tp_bases */
 };
+
+
+/* Numeric routines -------------------------------------------------------- */
+
+#ifdef HAVE_NUMPY
+/* load the Numeric Python module
+ * Return 1 if Numeric is available,
+ *        0 and set exception if it is not. 
+ *
+ * copied from pygtk
+ */
+static int
+load_numpy(void)
+{
+    static int import_done = 0;
+    static PyObject *exc_type=NULL, *exc_value=NULL;
+    PyObject *exc_tb=NULL;
+
+    if (exc_type != NULL) {
+	PyErr_Restore(exc_type, exc_value, NULL);
+	return 0;
+    }
+    if (!import_done) {
+	import_done = 1;
+	import_array();
+	if (PyErr_Occurred()) {
+	    PyErr_Fetch(&exc_type, &exc_value, &exc_tb);
+	    Py_INCREF(exc_type);
+	    Py_XINCREF(exc_value);
+	    PyErr_Restore(exc_type, exc_value, exc_tb);
+	    return 0;
+	}
+    }
+    return 1;
+}
+#endif /* HAVE_NUMPY */

Index: Makefile.am
===================================================================
RCS file: /cvs/cairo/pycairo/cairo/Makefile.am,v
retrieving revision 1.9
retrieving revision 1.10
diff -u -d -r1.9 -r1.10
--- Makefile.am	12 Jan 2005 07:24:50 -0000	1.9
+++ Makefile.am	14 Apr 2005 15:47:20 -0000	1.10
@@ -30,14 +30,6 @@
 gtk_la_SOURCES = \
   cairogtkmodule.c
 
-if HAVE_NUMPY
-pycairoexec_LTLIBRARIES += numpy.la
-endif
-numpy_la_LDFLAGS = -module -avoid-version -export-symbols-regex initnumpy
-numpy_la_LIBADD = $(CAIRO_LIBS)
-numpy_la_SOURCES = \
-  caironumpymodule.c
-
 if HAVE_LIBSVG_CAIRO
 pycairoexec_LTLIBRARIES += svg.la
 svg_la_LDFLAGS = -module -avoid-version -export-symbols-regex initsvg




More information about the cairo-commit mailing list