| 38 |
#include "pycairo-private.h" |
#include "pycairo-private.h" |
| 39 |
|
|
| 40 |
|
|
|
#ifdef HAVE_NUMPY |
|
|
# include <Numeric/arrayobject.h> |
|
|
static int load_numpy (void); |
|
|
#endif |
|
|
|
|
| 41 |
/* Class Surface ---------------------------------------------------------- */ |
/* Class Surface ---------------------------------------------------------- */ |
| 42 |
|
|
| 43 |
/* PycairoSurface_FromSurface |
/* PycairoSurface_FromSurface |
| 375 |
NULL); |
NULL); |
| 376 |
} |
} |
| 377 |
|
|
|
#ifdef HAVE_NUMPY |
|
|
static PyObject * |
|
|
image_surface_create_for_array (PyTypeObject *type, PyObject *args) |
|
|
{ |
|
|
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( |
|
|
(unsigned char *) array->data, |
|
|
format, |
|
|
array->dimensions[1], |
|
|
array->dimensions[0], |
|
|
array->strides[0]); |
|
|
return PycairoSurface_FromSurface(surface, (PyObject *)array); |
|
|
} |
|
|
#endif /* HAVE_NUMPY */ |
|
|
|
|
| 378 |
static PyObject * |
static PyObject * |
| 379 |
image_surface_create_for_data (PyTypeObject *type, PyObject *args) |
image_surface_create_for_data (PyTypeObject *type, PyObject *args) |
| 380 |
{ |
{ |
| 568 |
}; |
}; |
| 569 |
|
|
| 570 |
static PyMethodDef image_surface_methods[] = { |
static PyMethodDef image_surface_methods[] = { |
|
#ifdef HAVE_NUMPY |
|
|
{"create_for_array",(PyCFunction)image_surface_create_for_array, |
|
|
METH_VARARGS | METH_CLASS }, |
|
|
#endif |
|
| 571 |
{"create_for_data",(PyCFunction)image_surface_create_for_data, |
{"create_for_data",(PyCFunction)image_surface_create_for_data, |
| 572 |
METH_VARARGS | METH_CLASS }, |
METH_VARARGS | METH_CLASS }, |
| 573 |
#ifdef CAIRO_HAS_PNG_FUNCTIONS |
#ifdef CAIRO_HAS_PNG_FUNCTIONS |
| 1108 |
0, /* tp_bases */ |
0, /* tp_bases */ |
| 1109 |
}; |
}; |
| 1110 |
#endif /* CAIRO_HAS_XLIB_SURFACE */ |
#endif /* CAIRO_HAS_XLIB_SURFACE */ |
|
|
|
|
|
|
|
/* 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 */ |
|