FreeDesktop Web CVS Repositories

Please note, many of these repositories are now out of date, instead you really should try the git repositories at: http://cgit.freedesktop.org/

[cairo] / pycairo / cairo / pycairo-surface.c Repository:
fd.o logo

Diff of /pycairo/cairo/pycairo-surface.c

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

revision 1.85, Tue Jan 16 14:59:34 2007 UTC revision 1.86, Wed Jan 17 05:40:01 2007 UTC
# Line 38  Line 38 
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
# Line 380  image_surface_new (PyTypeObject *type, P Line 375  image_surface_new (PyTypeObject *type, P
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  {  {
# Line 633  static PyBufferProcs image_surface_as_bu Line 568  static PyBufferProcs image_surface_as_bu
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
# Line 1177  PyTypeObject PycairoXlibSurface_Type = { Line 1108  PyTypeObject PycairoXlibSurface_Type = {
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 */  

Legend:
Removed from v.1.85  
changed lines
  Added in v.1.86

CVS Admin
ViewVC Help
Powered by ViewVC 1.1-dev