[cairo-commit] pycairo/cairo pycairo-surface.c,1.47,1.48

Steve Chaplin commit at pdx.freedesktop.org
Sun Jul 24 21:10:41 PDT 2005


Committed by: stevech1097

Update of /cvs/cairo/pycairo/cairo
In directory gabe:/tmp/cvs-serv13686/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.47
retrieving revision 1.48
diff -u -d -r1.47 -r1.48
--- pycairo-surface.c	12 Jul 2005 14:06:11 -0000	1.47
+++ pycairo-surface.c	25 Jul 2005 04:10:39 -0000	1.48
@@ -375,20 +375,46 @@
 }
 #endif
 
+
 #ifdef CAIRO_HAS_PNG_FUNCTIONS
+static cairo_status_t 
+pycairo_read_func (void *closure, unsigned char *data, unsigned int length)
+{
+    if (fread (data, 1, (size_t) length, (FILE *)closure) != length)
+	return CAIRO_STATUS_READ_ERROR;
+    return CAIRO_STATUS_SUCCESS;
+}
+
+/* METH_O | METH_CLASS */
 static PyObject *
-image_surface_create_from_png (PyTypeObject *type, PyObject *args)
+image_surface_create_from_png (PyTypeObject *type, PyObject *o)
 {
-    const char *filename;
+    FILE *fp;
     cairo_surface_t *surface;
 
-    if (!PyArg_ParseTuple(args, "s:Surface.create_from_png", &filename))
+    if (PyObject_TypeCheck (o, &PyBaseString_Type)) {
+	fp = fopen (PyString_AsString(o), "rb");
+    } else if (PyObject_TypeCheck (o, &PyFile_Type)) {
+	fp = PyFile_AsFile((PyFileObject *)o);
+    } else {
+	PyErr_SetString(PyExc_TypeError, 
+			"ImageSurface.create_from_png takes one argument "
+			"which must be a filename (str) or file object");
 	return NULL;
+    }
+
+    if (fp == NULL) {
+	PyErr_SetString(PyExc_IOError, "unable to open file");
+	return NULL;
+    }
+    surface = cairo_image_surface_create_from_png_stream (pycairo_read_func, 
+							  fp);
+    if (PyObject_TypeCheck (o, &PyBaseString_Type))
+	fclose (fp);
 
-    surface = cairo_image_surface_create_from_png (filename);
     if (!surface) {
-	PyErr_SetString(PyExc_ValueError, "invalid PNG file or memory could "
-			"not be allocated for operation");
+	PyErr_SetString(PyExc_ValueError, "invalid PNG file or memory "
+			"could not be allocated for operation");
 	return NULL;
     }
     return PycairoSurface_FromSurface (surface, &PycairoImageSurface_Type, 
@@ -420,8 +446,7 @@
 #endif
 #ifdef CAIRO_HAS_PNG_FUNCTIONS
     {"create_from_png", (PyCFunction)image_surface_create_from_png, 
-                                                   METH_VARARGS | METH_CLASS },
-    /* create_from_png_stream */
+                                                   METH_O | METH_CLASS },
 #endif
     {"get_height",    (PyCFunction)image_surface_get_height,      METH_NOARGS},
     {"get_width",     (PyCFunction)image_surface_get_width,       METH_NOARGS},




More information about the cairo-commit mailing list