[cairo-commit] cairo/src Makefile.am, 1.44, 1.45 cairo-png.c, 1.6, 1.7 cairo-png.h, 1.4, 1.5 cairo.h, 1.100, 1.101

Kristian Hogsberg commit at pdx.freedesktop.org
Fri Apr 22 17:32:14 PDT 2005


Committed by: krh

Update of /cvs/cairo/cairo/src
In directory gabe:/tmp/cvs-serv5394/src

Modified Files:
	Makefile.am cairo-png.c cairo-png.h cairo.h 
Log Message:
2005-04-22  Kristian Høgsberg  <krh at redhat.com>

        * src/cairo-png.c (cairo_image_surface_create_for_png): Only check
        PNG signature if we read all the bytes.  Don't fclose() the FILE
        argument (Steve Chaplin <stevech1097 at yahoo.com.au>).

        Rename to cairo_image_surface_create_for_png() to
        cairo_image_surface_create_from_png() and change FILE arguments
        for this function and cairo_surface_write_png() to be a filename
        argument instead.



Index: Makefile.am
===================================================================
RCS file: /cvs/cairo/cairo/src/Makefile.am,v
retrieving revision 1.44
retrieving revision 1.45
diff -u -d -r1.44 -r1.45
--- Makefile.am	28 Mar 2005 22:30:40 -0000	1.44
+++ Makefile.am	23 Apr 2005 00:32:12 -0000	1.45
@@ -10,7 +10,6 @@
 endif
 
 if CAIRO_HAS_PNG_FUNCTIONS
-libcairo_png_headers = cairo-png.h
 libcairo_png_sources = cairo-png.c
 endif
 
@@ -68,7 +67,6 @@
 	$(libcairo_ft_headers)		\
 	$(libcairo_glitz_headers)	\
 	$(libcairo_pdf_headers)		\
-	$(libcairo_png_headers)		\
 	$(libcairo_ps_headers)		\
 	$(libcairo_quartz_headers)	\
 	$(libcairo_win32_headers)	\

Index: cairo-png.c
===================================================================
RCS file: /cvs/cairo/cairo/src/cairo-png.c,v
retrieving revision 1.6
retrieving revision 1.7
diff -u -d -r1.6 -r1.7
--- cairo-png.c	7 Apr 2005 19:56:43 -0000	1.6
+++ cairo-png.c	23 Apr 2005 00:32:12 -0000	1.7
@@ -37,8 +37,6 @@
 
 #include <png.h>
 #include "cairoint.h"
-#include "cairo-png.h"
-
 
 static void
 unpremultiply_data (png_structp png, png_row_infop row_info, png_bytep data)
@@ -78,8 +76,9 @@
  * surface.
  **/
 cairo_status_t
-cairo_surface_write_png (cairo_surface_t *surface, FILE *file)
+cairo_surface_write_png (cairo_surface_t *surface, const char *filename)
 {
+    FILE *file;
     int i;
     cairo_status_t status = CAIRO_STATUS_SUCCESS;
     cairo_image_surface_t *image;
@@ -92,17 +91,21 @@
     int png_color_type;
     int depth;
 
+    file = fopen (filename, "wb");
+    if (file == NULL)
+        return CAIRO_STATUS_WRITE_ERROR;
+
     status = _cairo_surface_acquire_source_image (surface,
 						  &image,
 						  &image_extra);
 
     if (status != CAIRO_STATUS_SUCCESS)
-      return status;
+        goto BAIL0;
 
     rows = malloc (image->height * sizeof(png_byte*));
     if (rows == NULL) {
         status = CAIRO_STATUS_NO_MEMORY;
-	goto BAIL0;
+	goto BAIL1;
     }
 
     for (i = 0; i < image->height; i++)
@@ -111,18 +114,18 @@
     png = png_create_write_struct (PNG_LIBPNG_VER_STRING, NULL, NULL, NULL);
     if (png == NULL) {
 	status = CAIRO_STATUS_NO_MEMORY;
-	goto BAIL1;
+	goto BAIL2;
     }
 
     info = png_create_info_struct (png);
     if (info == NULL) {
 	status = CAIRO_STATUS_NO_MEMORY;
-	goto BAIL2;
+	goto BAIL3;
     }
 
     if (setjmp (png_jmpbuf (png))) {
 	status = CAIRO_STATUS_NO_MEMORY;
-	goto BAIL2;
+	goto BAIL3;
     }
     
     png_init_io (png, file);
@@ -146,7 +149,7 @@
 	break;
     default:
 	status = CAIRO_STATUS_NULL_POINTER;
-	goto BAIL2;
+	goto BAIL3;
     }
 
     png_set_IHDR (png, info,
@@ -176,12 +179,14 @@
     png_write_image (png, rows);
     png_write_end (png, info);
 
-BAIL2:
+BAIL3:
     png_destroy_write_struct (&png, &info);
-BAIL1:
+BAIL2:
     free (rows);
-BAIL0:
+BAIL1:
     _cairo_surface_release_source_image (surface, image, image_extra);
+BAIL0:
+    fclose (file);
 
     return status;
 }
@@ -210,7 +215,7 @@
 }
 
 /**
- * cairo_image_surface_create_for_png:
+ * cairo_image_surface_create_from_png:
  * @file: a #FILE 
  * @width: if not %NULL, the width of the image surface is written to
  *   this address
@@ -226,8 +231,9 @@
  * memory could not be allocated for the operation.
  **/
 cairo_surface_t *
-cairo_image_surface_create_for_png (FILE *file, int *width, int *height)
+cairo_image_surface_create_from_png (const char *filename)
 {
+    FILE *file;
     cairo_surface_t *surface;
     png_byte *data;
     int i;
@@ -241,9 +247,13 @@
     unsigned int pixel_size;
     png_byte **row_pointers;
 
+    file = fopen (filename, "rb");
+    if (file == NULL)
+	return NULL;
+
     sig_bytes = fread (png_sig, 1, PNG_SIG_SIZE, file);
-    if (png_check_sig (png_sig, sig_bytes) == 0)
-	goto BAIL1; /* FIXME: ERROR_NOT_PNG */
+    if (sig_bytes != PNG_SIG_SIZE || png_check_sig (png_sig, sig_bytes) == 0)
+	goto BAIL0;
 
     /* XXX: Perhaps we'll want some other error handlers? */
     png = png_create_read_struct (PNG_LIBPNG_VER_STRING,
@@ -251,11 +261,11 @@
                                   NULL,
                                   NULL);
     if (png == NULL)
-	goto BAIL1;
+	goto BAIL0;
 
     info = png_create_info_struct (png);
     if (info == NULL)
-	goto BAIL2;
+	goto BAIL1;
 
     png_init_io (png, file);
     png_set_sig_bytes (png, sig_bytes);
@@ -302,11 +312,11 @@
     pixel_size = 4;
     data = malloc (png_width * png_height * pixel_size);
     if (data == NULL)
-	goto BAIL2;
+	goto BAIL1;
 
     row_pointers = malloc (png_height * sizeof(char *));
     if (row_pointers == NULL)
-	goto BAIL3;
+	goto BAIL2;
 
     for (i = 0; i < png_height; i++)
         row_pointers[i] = &data[i * png_width * pixel_size];
@@ -318,11 +328,6 @@
     png_destroy_read_struct (&png, &info, NULL);
     fclose (file);
 
-    if (width != NULL)
-	*width = png_width;
-    if (height != NULL)
-	*height = png_height;
-
     surface = cairo_image_surface_create_for_data (data,
 						   CAIRO_FORMAT_ARGB32,
 						   png_width, png_height, stride);
@@ -330,11 +335,11 @@
 
     return surface;
 
- BAIL3:
-    free (data);
  BAIL2:
-    png_destroy_read_struct (&png, NULL, NULL);
+    free (data);
  BAIL1:
+    png_destroy_read_struct (&png, NULL, NULL);
+ BAIL0:
     fclose (file);
 
     return NULL;

Index: cairo-png.h
===================================================================
RCS file: /cvs/cairo/cairo/src/cairo-png.h,v
retrieving revision 1.4
retrieving revision 1.5
diff -u -d -r1.4 -r1.5
--- cairo-png.h	28 Mar 2005 21:58:26 -0000	1.4
+++ cairo-png.h	23 Apr 2005 00:32:12 -0000	1.5
@@ -47,12 +47,10 @@
 
 cairo_status_t
 cairo_surface_write_png (cairo_surface_t	*surface,
-			 FILE			*file);
+			 const char		*filename);
 
 cairo_surface_t *
-cairo_image_surface_create_for_png (FILE	*file,
-				    int		*width,
-				    int		*height);
+cairo_image_surface_create_from_png (const char	*filename);
 
 CAIRO_END_DECLS
 

Index: cairo.h
===================================================================
RCS file: /cvs/cairo/cairo/src/cairo.h,v
retrieving revision 1.100
retrieving revision 1.101
diff -u -d -r1.100 -r1.101
--- cairo.h	19 Apr 2005 23:29:05 -0000	1.100
+++ cairo.h	23 Apr 2005 00:32:12 -0000	1.101
@@ -894,6 +894,12 @@
 cairo_status_t
 cairo_surface_finish (cairo_surface_t *surface);
 
+#ifdef CAIRO_HAS_PNG_FUNCTIONS
+cairo_status_t
+cairo_surface_write_png (cairo_surface_t	*surface,
+			 const char		*filename);
+#endif
+
 /* XXX: Note: The current Render/Ic implementations don't do the right
    thing with repeat when the surface has a non-identity matrix. */
 /* XXX: Rework this as a cairo function with an enum: cairo_set_pattern_extend */
@@ -953,6 +959,11 @@
 				     int			height,
 				     int			stride);
 
+#ifdef CAIRO_HAS_PNG_FUNCTIONS
+cairo_surface_t *
+cairo_image_surface_create_from_png (const char	*filename);
+#endif
+
 /* Pattern creation functions */
 cairo_pattern_t *
 cairo_pattern_create_for_surface (cairo_surface_t *surface);




More information about the cairo-commit mailing list