[cairo] PNG backend

Olivier Andrieu oliv__a at users.sourceforge.net
Sun Feb 8 11:21:15 PST 2004


Hi,

I wrote a simple PNG backend for cairo. Like the PS backend, it's just
a wrapper of "image" surfaces for format CAIRO_FORMAT_ARGB32 and
CAIRO_FORMAT_RGB24.

It adds the following API to cairo.h:
,----
| void
| cairo_set_target_png (cairo_t	*cr,
|                       FILE *file,
|                       cairo_format_t format,
|                       int width,
|                       int height,
|                       double pixels_per_inch);
| 
| cairo_surface_t *
| cairo_png_surface_create (FILE           *file,
|                           cairo_format_t format,
|                           int width,
|                           int height,
|                           double pixels_per_inch);
`----

I patched the png directory of gtk-demo too (for testing).

-- 
   Olivier

-------------- next part --------------
? src/cairo-config.h
Index: cairo.pc.in
===================================================================
RCS file: /cvs/cairo/cairo/cairo.pc.in,v
retrieving revision 1.10
diff -u -r1.10 cairo.pc.in
--- cairo.pc.in	12 Dec 2003 19:44:16 -0000	1.10
+++ cairo.pc.in	8 Feb 2004 18:08:25 -0000
@@ -7,7 +7,7 @@
 Description: Multi-platform 2D graphics library
 Version: @VERSION@
 
-Requires: fontconfig libpixman @XRENDER_REQUIRES@
+Requires: fontconfig libpixman @XRENDER_REQUIRES@ libpng12
 Libs: -L${libdir} -lcairo -lm @PS_SURFACE_LIBS@ @FREETYPE_LIBS@
 Cflags: -I${includedir} @FREETYPE_CFLAGS@
 
Index: configure.in
===================================================================
RCS file: /cvs/cairo/cairo/configure.in,v
retrieving revision 1.40
diff -u -r1.40 configure.in
--- configure.in	3 Feb 2004 07:24:14 -0000	1.40
+++ configure.in	8 Feb 2004 18:08:25 -0000
@@ -3,7 +3,7 @@
 dnl ===========================================================================
 
 # Package version number, (as distinct from shared library version)
-CAIRO_VERSION=0.1.17
+CAIRO_VERSION=0.1.18
 
 # libtool shared library version
 
@@ -92,6 +92,23 @@
 
 dnl ===========================================================================
 
+AC_ARG_ENABLE(png,
+  AC_HELP_STRING([--disable-png], [Disable cairo's PNG backend]),
+  [use_png=$enableval], [use_png=no])
+
+if test "x$use_png" != "xyes"; then
+  PNG_SURFACE_FEATURE=CAIRO_HAS_NO_PNG_SURFACE
+  AM_CONDITIONAL(CAIRO_HAS_PNG_SURFACE, false)
+else
+  PNG_SURFACE_FEATURE=CAIRO_HAS_PNG_SURFACE
+  PKG_CHECK_MODULES(PNG, libpng12)
+  AM_CONDITIONAL(CAIRO_HAS_PNG_SURFACE, true)
+fi
+
+AC_SUBST(PNG_SURFACE_FEATURE)
+
+dnl ===========================================================================
+
 PKG_CHECK_MODULES(FONTCONFIG, fontconfig)
 PKG_CHECK_MODULES(CAIRO, libpixman >= 0.1.0)
 
Index: src/Makefile.am
===================================================================
RCS file: /cvs/cairo/cairo/src/Makefile.am,v
retrieving revision 1.20
diff -u -r1.20 Makefile.am
--- src/Makefile.am	3 Feb 2004 07:24:14 -0000	1.20
+++ src/Makefile.am	8 Feb 2004 18:08:25 -0000
@@ -5,6 +5,10 @@
 libcairo_ps_sources = cairo_ps_surface.c
 endif
 
+if CAIRO_HAS_PNG_SURFACE
+libcairo_png_sources = cairo_png_surface.c
+endif
+
 if CAIRO_HAS_XLIB_SURFACE
 libcairo_xlib_sources = cairo_xlib_surface.c
 endif
@@ -43,12 +47,13 @@
 	cairo_surface.c		\
 	cairo_traps.c		\
 	$(libcairo_ps_sources)  \
+	$(libcairo_png_sources) \
 	$(libcairo_xlib_sources)\
 	$(libcairo_xcb_sources) \
 	cairoint.h
 
 libcairo_la_LDFLAGS = -version-info @VERSION_INFO@ -no-undefined
 
-INCLUDES = -I$(srcdir) $(CAIRO_CFLAGS) $(FONTCONFIG_CFLAGS) $(XRENDER_CFLAGS) $(X_CFLAGS) $(XCB_CFLAGS)
+INCLUDES = -I$(srcdir) $(CAIRO_CFLAGS) $(FONTCONFIG_CFLAGS) $(XRENDER_CFLAGS) $(XCB_CFLAGS) $(PNG_CFLAGS)
 
-libcairo_la_LIBADD = $(CAIRO_LIBS) $(FONTCONFIG_LIBS) $(XRENDER_LIBS) $(X_LIBS) $(XCB_LIBS) -lm
+libcairo_la_LIBADD = $(CAIRO_LIBS) $(FONTCONFIG_LIBS) $(XRENDER_LIBS) $(XCB_LIBS) $(PNG_LIBS) -lm
Index: src/cairo-features.h.in
===================================================================
RCS file: /cvs/cairo/cairo/src/cairo-features.h.in,v
retrieving revision 1.3
diff -u -r1.3 cairo-features.h.in
--- src/cairo-features.h.in	3 Feb 2004 07:24:14 -0000	1.3
+++ src/cairo-features.h.in	8 Feb 2004 18:08:25 -0000
@@ -30,6 +30,8 @@
 
 #define @PS_SURFACE_FEATURE@
 
+#define @PNG_SURFACE_FEATURE@
+
 #define @XLIB_SURFACE_FEATURE@
 
 #define @XCB_SURFACE_FEATURE@
Index: src/cairo.h
===================================================================
RCS file: /cvs/cairo/cairo/src/cairo.h,v
retrieving revision 1.43
diff -u -r1.43 cairo.h
--- src/cairo.h	3 Feb 2004 07:24:14 -0000	1.43
+++ src/cairo.h	8 Feb 2004 18:08:25 -0000
@@ -101,6 +101,20 @@
 
 #endif /* CAIRO_HAS_PS_SURFACE */
 
+#ifdef CAIRO_HAS_PNG_SURFACE
+
+#include <stdio.h>
+
+void
+cairo_set_target_png (cairo_t	*cr,
+		      FILE	*file,
+		      cairo_format_t	format,
+		      int	       	width,
+		      int		height,
+		      double		pixels_per_inch);
+
+#endif /* CAIRO_HAS_PNG_SURFACE */
+
 #ifdef CAIRO_HAS_XLIB_SURFACE
 
 #include <X11/extensions/Xrender.h>
@@ -635,6 +649,19 @@
 
 #endif /* CAIRO_HAS_PS_SURFACE */
 
+#ifdef CAIRO_HAS_PNG_SURFACE
+
+/* PNG-surface functions */
+
+cairo_surface_t *
+cairo_png_surface_create (FILE			*file,
+			  cairo_format_t	format,
+			  int			width,
+			  int			height,
+			  double	pixels_per_inch);
+
+#endif /* CAIRO_HAS_PNG_SURFACE */
+
 #ifdef CAIRO_HAS_XLIB_SURFACE
 
 /* XXX: This is a mess from the user's POV. Should the Visual or the
Index: src/cairo_png_surface.c
===================================================================
RCS file: src/cairo_png_surface.c
diff -N src/cairo_png_surface.c
--- /dev/null	1 Jan 1970 00:00:00 -0000
+++ src/cairo_png_surface.c	8 Feb 2004 18:08:26 -0000
@@ -0,0 +1,328 @@
+#include <png.h>
+
+#include "cairoint.h"
+
+static const cairo_surface_backend_t cairo_png_surface_backend;
+
+void
+cairo_set_target_png (cairo_t	*cr,
+		      FILE	*file,
+		      cairo_format_t	format,
+		      int	       	width,
+		      int		height,
+		      double		pixels_per_inch)
+{
+    cairo_surface_t *surface;
+
+    surface = cairo_png_surface_create (file, format, 
+					width, height,
+					pixels_per_inch);
+
+    if (surface == NULL) {
+	cr->status = CAIRO_STATUS_NO_MEMORY;
+	return;
+    }
+
+    cairo_set_target_surface (cr, surface);
+
+    /* cairo_set_target_surface takes a reference, so we must destroy ours */
+    cairo_surface_destroy (surface);
+}
+
+typedef struct cairo_png_surface {
+    cairo_surface_t base;
+
+    /* PNG-specific fields */
+    FILE *file;
+
+    double ppi;
+
+    png_structp png_w;
+    png_infop png_i;
+
+    cairo_image_surface_t *image;
+} cairo_png_surface_t;
+
+
+static void
+_cairo_png_surface_erase (cairo_png_surface_t *surface);
+
+cairo_surface_t *
+cairo_png_surface_create (FILE			*file,
+			  cairo_format_t	format,
+			  int			width,
+			  int			height,
+			  double	pixels_per_inch)
+{
+    cairo_png_surface_t *surface;
+    time_t now = time (NULL);
+    png_time png_time;
+
+    if (format == CAIRO_FORMAT_A8 ||
+	format == CAIRO_FORMAT_A1 ||
+	file == NULL)
+	return NULL;
+
+    surface = malloc (sizeof (cairo_png_surface_t));
+    if (surface == NULL)
+	goto failure;
+
+    _cairo_surface_init (&surface->base, &cairo_png_surface_backend);
+    surface->png_w = NULL;
+    surface->png_i = NULL;
+
+    surface->image = (cairo_image_surface_t *)
+	cairo_image_surface_create (format, width, height);
+    if (surface->image == NULL)
+	goto failure;
+
+    _cairo_png_surface_erase (surface);
+
+    surface->file = file;
+    surface->ppi = pixels_per_inch;
+
+    surface->png_w = png_create_write_struct (PNG_LIBPNG_VER_STRING,
+					      NULL, NULL, NULL);
+    if (surface->png_w == NULL)
+	goto failure;
+    surface->png_i = png_create_info_struct (surface->png_w);
+    if (surface->png_i == NULL)
+	goto failure;
+
+    if (setjmp (png_jmpbuf (surface->png_w)))
+	goto failure;
+    
+    png_init_io (surface->png_w, surface->file);
+
+    switch (format) {
+    case CAIRO_FORMAT_ARGB32:
+    png_set_IHDR (surface->png_w, surface->png_i,
+		  width, height, 8, PNG_COLOR_TYPE_RGB_ALPHA,
+		  PNG_INTERLACE_NONE,
+		  PNG_COMPRESSION_TYPE_DEFAULT,
+		  PNG_FILTER_TYPE_DEFAULT);
+    break;
+    case CAIRO_FORMAT_RGB24:
+    png_set_IHDR (surface->png_w, surface->png_i,
+		  width, height, 8, PNG_COLOR_TYPE_RGB,
+		  PNG_INTERLACE_NONE,
+		  PNG_COMPRESSION_TYPE_DEFAULT,
+		  PNG_FILTER_TYPE_DEFAULT);
+    break;
+    }
+
+    png_convert_from_time_t (&png_time, now);
+    png_set_tIME (surface->png_w, surface->png_i, &png_time);
+
+#define INCH_BY_METER  39.370079
+    png_set_pHYs(surface->png_w, surface->png_i,
+		 surface->ppi * INCH_BY_METER,
+		 surface->ppi * INCH_BY_METER,
+		 PNG_RESOLUTION_METER);
+
+    png_write_info (surface->png_w, surface->png_i);
+
+    switch (format) {
+    case CAIRO_FORMAT_ARGB32:
+	png_set_bgr (surface->png_w);
+	break;
+    case CAIRO_FORMAT_RGB24:
+	png_set_filler (surface->png_w, 0, PNG_FILLER_AFTER);
+	png_set_bgr (surface->png_w);
+	break;
+    }
+
+    return &surface->base;
+
+
+  failure:
+    if (surface) {
+	if (surface->image)
+	    cairo_surface_destroy (&surface->image->base);
+	if (surface->png_i)
+	    png_destroy_write_struct (&surface->png_w, &surface->png_i);
+	else if (surface->png_w)
+	    png_destroy_write_struct (&surface->png_w, NULL);
+	free (surface);
+    }
+    return NULL;
+}
+
+
+static cairo_surface_t *
+_cairo_png_surface_create_similar (void		*abstract_src,
+				   cairo_format_t	format,
+				   int		width,
+				   int		height)
+{
+    return NULL;
+}
+
+static void
+_cairo_png_surface_destroy (void *abstract_surface)
+{
+    cairo_png_surface_t *surface = abstract_surface;
+    int i;
+    png_byte *row;
+
+    if (setjmp (png_jmpbuf (surface->png_w)))
+	goto failure;
+
+    row = surface->image->data;
+    for (i=0; i < surface->image->height; i++) {
+	png_write_row (surface->png_w, row);
+	row += surface->image->stride;
+    }
+
+    png_write_end (surface->png_w, surface->png_i);
+
+  failure:
+    png_destroy_write_struct (&surface->png_w, &surface->png_i);
+
+    cairo_surface_destroy (&surface->image->base);
+
+    free (surface);
+}
+
+static void
+_cairo_png_surface_erase (cairo_png_surface_t *surface)
+{
+    cairo_color_t transparent;
+
+    _cairo_color_init (&transparent);
+    _cairo_color_set_rgb (&transparent, 0., 0., 0.);
+    _cairo_color_set_alpha (&transparent, 0.);
+    _cairo_surface_fill_rectangle (&surface->image->base,
+				   CAIRO_OPERATOR_SRC,
+				   &transparent,
+				   0, 0,
+				   surface->image->width,
+				   surface->image->height);
+}
+
+static double
+_cairo_png_surface_pixels_per_inch (void *abstract_surface)
+{
+    cairo_png_surface_t *surface = abstract_surface;
+
+    return surface->ppi;
+}
+
+static cairo_image_surface_t *
+_cairo_png_surface_get_image (void *abstract_surface)
+{
+    cairo_png_surface_t *surface = abstract_surface;
+
+    cairo_surface_reference (&surface->image->base);
+
+    return surface->image;
+}
+
+static cairo_status_t
+_cairo_png_surface_set_image (void			*abstract_surface,
+			      cairo_image_surface_t	*image)
+{
+    cairo_png_surface_t *surface = abstract_surface;
+
+    if (image == surface->image)
+	return CAIRO_STATUS_SUCCESS;
+
+    /* XXX: Need to call _cairo_image_surface_set_image here, but it's
+       not implemented yet. */
+
+    return CAIRO_INT_STATUS_UNSUPPORTED;
+}
+
+static cairo_status_t
+_cairo_png_surface_set_matrix (void		*abstract_surface,
+			       cairo_matrix_t	*matrix)
+{
+    cairo_png_surface_t *surface = abstract_surface;
+
+    return _cairo_image_surface_set_matrix (surface->image, matrix);
+}
+
+static cairo_status_t
+_cairo_png_surface_set_filter (void		*abstract_surface,
+			       cairo_filter_t	filter)
+{
+    cairo_png_surface_t *surface = abstract_surface;
+
+    return _cairo_image_surface_set_filter (surface->image, filter);
+}
+
+static cairo_status_t
+_cairo_png_surface_set_repeat (void		*abstract_surface,
+			       int		repeat)
+{
+    cairo_png_surface_t *surface = abstract_surface;
+
+    return _cairo_image_surface_set_repeat (surface->image, repeat);
+}
+
+static cairo_int_status_t
+_cairo_png_surface_composite (cairo_operator_t	operator,
+			      cairo_surface_t	*generic_src,
+			      cairo_surface_t	*generic_mask,
+			      void		*abstract_dst,
+			      int		src_x,
+			      int		src_y,
+			      int		mask_x,
+			      int		mask_y,
+			      int		dst_x,
+			      int		dst_y,
+			      unsigned int	width,
+			      unsigned int	height)
+{
+    return CAIRO_INT_STATUS_UNSUPPORTED;
+}
+
+static cairo_int_status_t
+_cairo_png_surface_fill_rectangles (void			*abstract_surface,
+				    cairo_operator_t	operator,
+				    const cairo_color_t	*color,
+				    cairo_rectangle_t	*rects,
+				    int			num_rects)
+{
+    return CAIRO_INT_STATUS_UNSUPPORTED;
+}
+
+static cairo_int_status_t
+_cairo_png_surface_composite_trapezoids (cairo_operator_t	operator,
+					 cairo_surface_t		*generic_src,
+					 void			*abstract_dst,
+					 int			x_src,
+					 int			y_src,
+					 cairo_trapezoid_t	*traps,
+					 int			num_traps)
+{
+    return CAIRO_INT_STATUS_UNSUPPORTED;
+}
+
+static cairo_int_status_t
+_cairo_png_surface_copy_page (void *abstract_surface)
+{
+    return CAIRO_INT_STATUS_UNSUPPORTED;
+}
+
+static cairo_int_status_t
+_cairo_png_surface_show_page (void *abstract_surface)
+{
+    return CAIRO_INT_STATUS_UNSUPPORTED;
+}
+
+static const cairo_surface_backend_t cairo_png_surface_backend = {
+    _cairo_png_surface_create_similar,
+    _cairo_png_surface_destroy,
+    _cairo_png_surface_pixels_per_inch,
+    _cairo_png_surface_get_image,
+    _cairo_png_surface_set_image,
+    _cairo_png_surface_set_matrix,
+    _cairo_png_surface_set_filter,
+    _cairo_png_surface_set_repeat,
+    _cairo_png_surface_composite,
+    _cairo_png_surface_fill_rectangles,
+    _cairo_png_surface_composite_trapezoids,
+    _cairo_png_surface_copy_page,
+    _cairo_png_surface_show_page
+};
-------------- next part --------------
? png/text-rotate
Index: png/Makefile
===================================================================
RCS file: /cvs/cairo/cairo-demo/png/Makefile,v
retrieving revision 1.5
diff -u -r1.5 Makefile
--- png/Makefile	18 Dec 2003 03:17:26 -0000	1.5
+++ png/Makefile	8 Feb 2004 17:29:27 -0000
@@ -1,13 +1,9 @@
 EXAMPLES=outline spiral hering caps_joins stars splines_tolerance text text-rotate
 
-CFLAGS=-g -Wall -Wstrict-prototypes -Wmissing-prototypes -Wmissing-declarations -Wredundant-decls `pkg-config --cflags cairo libpng`
-LDFLAGS=`pkg-config --libs cairo libpng`
+CFLAGS=-g -Wall -Wstrict-prototypes -Wmissing-prototypes -Wmissing-declarations -Wredundant-decls `pkg-config --cflags cairo`
+LDFLAGS=`pkg-config --libs cairo`
 
 all: ${EXAMPLES}
 
-${EXAMPLES}: write_png.o
-
-write_png.o: write_png.c write_png.h
-
 clean:
 	rm -f ${EXAMPLES} *.o
Index: png/caps_joins.c
===================================================================
RCS file: /cvs/cairo/cairo-demo/png/caps_joins.c,v
retrieving revision 1.1
diff -u -r1.1 caps_joins.c
--- png/caps_joins.c	18 Aug 2003 18:11:38 -0000	1.1
+++ png/caps_joins.c	8 Feb 2004 17:29:27 -0000
@@ -27,7 +27,9 @@
 
 #include <cairo.h>
 
-#include "write_png.h"
+#ifndef CAIRO_HAS_PNG_SURFACE
+#error "cairo not compiled with PNG backend"
+#endif
 
 void
 draw_caps_joins (cairo_t *ct, int width, int height);
@@ -40,19 +42,19 @@
 
 #define WIDTH 600
 #define HEIGHT 600
-#define STRIDE (WIDTH * 4)
-
-char image[STRIDE*HEIGHT];
 
 int
 main (void)
 {
     cairo_t *ct;
+    FILE *file;
 
     ct = cairo_create ();
 
-    cairo_set_target_image (ct, image, CAIRO_FORMAT_ARGB32,
-		      WIDTH, HEIGHT, STRIDE);
+    file = fopen ("caps_joins.png", "wb");
+
+    cairo_set_target_png (ct, file, CAIRO_FORMAT_ARGB32,
+			  WIDTH, HEIGHT, 96.0);
 
     cairo_rectangle (ct, 0, 0, WIDTH, HEIGHT);
     cairo_set_rgb_color (ct, 1, 1, 1);
@@ -60,10 +62,10 @@
 
     draw_caps_joins (ct, WIDTH, HEIGHT);
 
-    write_png_argb32 (image, "caps_joins.png", WIDTH, HEIGHT, STRIDE);
-
     cairo_destroy (ct);
 
+    fclose (file);
+
     return 0;
 }
 
Index: png/hering.c
===================================================================
RCS file: /cvs/cairo/cairo-demo/png/hering.c,v
retrieving revision 1.1
diff -u -r1.1 hering.c
--- png/hering.c	18 Aug 2003 18:11:38 -0000	1.1
+++ png/hering.c	8 Feb 2004 17:29:28 -0000
@@ -28,26 +28,27 @@
 #include <cairo.h>
 #include <math.h>
 
-#include "write_png.h"
+#ifndef CAIRO_HAS_PNG_SURFACE
+#error "cairo not compiled with PNG backend"
+#endif
 
 void
 draw_hering (cairo_t *ct, int width, int height);
 
 #define WIDTH 300
 #define HEIGHT 600
-#define STRIDE (WIDTH * 4)
-
-char image[STRIDE*HEIGHT];
 
 int
 main (void)
 {
     cairo_t *ct;
+    FILE *file;
 
     ct = cairo_create ();
 
-    cairo_set_target_image (ct, image, CAIRO_FORMAT_ARGB32,
-		      WIDTH, HEIGHT, STRIDE);
+    file = fopen ("hering.png", "wb");
+    cairo_set_target_png (ct, file, CAIRO_FORMAT_RGB24,
+			  WIDTH, HEIGHT, 96.0);
 
     cairo_rectangle (ct, 0, 0, WIDTH, HEIGHT);
     cairo_set_rgb_color (ct, 1, 1, 1);
@@ -55,10 +56,10 @@
 
     draw_hering (ct, WIDTH, HEIGHT);
 
-    write_png_argb32 (image, "hering.png", WIDTH, HEIGHT, STRIDE);
-
     cairo_destroy (ct);
 
+    fclose (file);
+
     return 0;
 }
 
Index: png/outline.c
===================================================================
RCS file: /cvs/cairo/cairo-demo/png/outline.c,v
retrieving revision 1.3
diff -u -r1.3 outline.c
--- png/outline.c	11 Dec 2003 15:09:12 -0000	1.3
+++ png/outline.c	8 Feb 2004 17:29:28 -0000
@@ -27,7 +27,9 @@
 
 #include <cairo.h>
 
-#include "write_png.h"
+#ifndef CAIRO_HAS_PNG_SURFACE
+#error "cairo not compiled with PNG backend"
+#endif
 
 void
 draw_outlines (cairo_t *ct, int surface_width, int surface_height);
@@ -46,19 +48,18 @@
 
 #define WIDTH 750
 #define HEIGHT 500
-#define STRIDE (WIDTH * 4)
-
-char image[STRIDE*HEIGHT];
 
 int
 main (void)
 {
     cairo_t *ct;
+    FILE *file;
 
     ct = cairo_create ();
 
-    cairo_set_target_image (ct, image, CAIRO_FORMAT_ARGB32,
-		      WIDTH, HEIGHT, STRIDE);
+    file = fopen ("outline.png", "wb");
+    cairo_set_target_png (ct, file, CAIRO_FORMAT_ARGB32,
+			  WIDTH, HEIGHT, 96.0);
 
     cairo_rectangle (ct, 0, 0, WIDTH, HEIGHT);
     cairo_set_rgb_color (ct, 1, 1, 1);
@@ -66,10 +67,10 @@
 
     draw_outlines (ct, WIDTH, HEIGHT);
 
-    write_png_argb32 (image, "outline.png", WIDTH, HEIGHT, STRIDE);
-
     cairo_destroy (ct);
 
+    fclose (file);
+
     return 0;
 }
 
@@ -86,7 +87,7 @@
 					     3, 2);
     cairo_set_target_surface (ct, gradient);
 
-    cairo_set_rgb_color (ct, 0, 0, 0);
+    cairo_set_rgb_color (ct, 0, 0, 1);
     cairo_rectangle (ct, 0, 0, 1, 2);
     cairo_fill (ct);
 
@@ -94,7 +95,7 @@
     cairo_rectangle (ct, 1, 0, 1, 2);
     cairo_fill (ct);
 
-    cairo_set_rgb_color (ct, 0, 0, 0);
+    cairo_set_rgb_color (ct, 0, 0, 1);
     cairo_rectangle (ct, 2, 0, 1, 2);
     cairo_fill (ct);
 
Index: png/spiral.c
===================================================================
RCS file: /cvs/cairo/cairo-demo/png/spiral.c,v
retrieving revision 1.1
diff -u -r1.1 spiral.c
--- png/spiral.c	18 Aug 2003 18:11:38 -0000	1.1
+++ png/spiral.c	8 Feb 2004 17:29:28 -0000
@@ -27,26 +27,28 @@
 
 #include <cairo.h>
 
-#include "write_png.h"
+#ifndef CAIRO_HAS_PNG_SURFACE
+#error "cairo not compiled with PNG backend"
+#endif
 
 void
 draw_spiral (cairo_t *ct, int width, int height);
 
 #define WIDTH 600
 #define HEIGHT 600
-#define STRIDE (WIDTH * 4)
-
-char image[STRIDE*HEIGHT];
 
 int
 main (void)
 {
     cairo_t *ct;
+    FILE *file;
 
     ct = cairo_create ();
 
-    cairo_set_target_image (ct, image, CAIRO_FORMAT_ARGB32,
-		      WIDTH, HEIGHT, STRIDE);
+    file = fopen ("spiral.png", "wb");
+
+    cairo_set_target_png (ct, file, CAIRO_FORMAT_ARGB32,
+			  WIDTH, HEIGHT, 96.0);
 
     cairo_rectangle (ct, 0, 0, WIDTH, HEIGHT);
     cairo_set_rgb_color (ct, 1, 1, 1);
@@ -54,10 +56,10 @@
 
     draw_spiral (ct, WIDTH, HEIGHT);
 
-    write_png_argb32 (image, "spiral.png", WIDTH, HEIGHT, STRIDE);
-
     cairo_destroy (ct);
 
+    fclose (file);
+
     return 0;
 }
 
Index: png/splines_tolerance.c
===================================================================
RCS file: /cvs/cairo/cairo-demo/png/splines_tolerance.c,v
retrieving revision 1.1
diff -u -r1.1 splines_tolerance.c
--- png/splines_tolerance.c	18 Aug 2003 18:11:38 -0000	1.1
+++ png/splines_tolerance.c	8 Feb 2004 17:29:28 -0000
@@ -27,7 +27,9 @@
 
 #include <cairo.h>
 
-#include "write_png.h"
+#ifndef CAIRO_HAS_PNG_SURFACE
+#error "cairo not compiled with PNG backend"
+#endif
 
 void
 draw_spline (cairo_t *ct, double height);
@@ -37,19 +39,19 @@
 
 #define WIDTH 600
 #define HEIGHT 300
-#define STRIDE (WIDTH * 4)
-
-char image[STRIDE*HEIGHT];
 
 int
 main (void)
 {
     cairo_t *ct;
+    FILE *file;
 
     ct = cairo_create ();
 
-    cairo_set_target_image (ct, image, CAIRO_FORMAT_ARGB32,
-		      WIDTH, HEIGHT, STRIDE);
+    file = fopen ("splines_tolerance.png", "wb");
+
+    cairo_set_target_png (ct, file, CAIRO_FORMAT_ARGB32,
+			  WIDTH, HEIGHT, 96.0);
 
     cairo_rectangle (ct, 0, 0, WIDTH, HEIGHT);
     cairo_set_rgb_color (ct, 1, 1, 1);
@@ -57,10 +59,10 @@
 
     draw_splines (ct, WIDTH, HEIGHT);
 
-    write_png_argb32 (image, "splines_tolerance.png", WIDTH, HEIGHT, STRIDE);
-
     cairo_destroy (ct);
 
+    fclose (file);
+
     return 0;
 }
 
Index: png/stars.c
===================================================================
RCS file: /cvs/cairo/cairo-demo/png/stars.c,v
retrieving revision 1.1
diff -u -r1.1 stars.c
--- png/stars.c	18 Aug 2003 18:11:38 -0000	1.1
+++ png/stars.c	8 Feb 2004 17:29:29 -0000
@@ -28,7 +28,9 @@
 #include <cairo.h>
 #include <math.h>
 
-#include "write_png.h"
+#ifndef CAIRO_HAS_PNG_SURFACE
+#error "cairo not compiled with PNG backend"
+#endif
 
 void
 draw_stars (cairo_t *ct, int width, int height);
@@ -38,19 +40,20 @@
 
 #define WIDTH 600
 #define HEIGHT 275
-#define STRIDE (WIDTH * 4)
-
-char image[STRIDE*HEIGHT];
 
 int
 main (void)
 {
     cairo_t *ct;
+    FILE *file;
+
+    file = fopen ("stars.png", "wb");
 
     ct = cairo_create ();
 
-    cairo_set_target_image (ct, image, CAIRO_FORMAT_ARGB32,
-		      WIDTH, HEIGHT, STRIDE);
+    cairo_set_target_png (ct, file,
+			  CAIRO_FORMAT_ARGB32,
+			  WIDTH, HEIGHT, 96.0);
 
     cairo_rectangle (ct, 0, 0, WIDTH, HEIGHT);
     cairo_set_rgb_color (ct, 1, 1, 1);
@@ -58,10 +61,10 @@
 
     draw_stars (ct, WIDTH, HEIGHT);
 
-    write_png_argb32 (image, "stars.png", WIDTH, HEIGHT, STRIDE);
-
     cairo_destroy (ct);
 
+    fclose (file);
+
     return 0;
 }
 
Index: png/text-rotate.c
===================================================================
RCS file: /cvs/cairo/cairo-demo/png/text-rotate.c,v
retrieving revision 1.1
diff -u -r1.1 text-rotate.c
--- png/text-rotate.c	18 Dec 2003 03:17:27 -0000	1.1
+++ png/text-rotate.c	8 Feb 2004 17:29:29 -0000
@@ -28,27 +28,30 @@
 #include <cairo.h>
 #include <math.h>
 
-#include "write_png.h"
+#ifndef CAIRO_HAS_PNG_SURFACE
+#error "cairo not compiled with PNG backend"
+#endif
 
 #define TEXT "hello, world"
 
 #define WIDTH 450
 #define HEIGHT 900
-#define STRIDE (WIDTH * 4)
 
-char image[STRIDE*HEIGHT];
 int
 main (void)
 {
     int i;
     cairo_t *cr;
     cairo_matrix_t *matrix;
+    FILE * file;
     double angle;
 
     cr = cairo_create ();
 
-    cairo_set_target_image (cr, image, CAIRO_FORMAT_ARGB32,
-			    WIDTH, HEIGHT, STRIDE);
+    file = fopen ("text-rotate.png", "wb");
+
+    cairo_set_target_png (cr, file, CAIRO_FORMAT_ARGB32,
+			  WIDTH, HEIGHT, 96.0);
 
     cairo_set_rgb_color (cr, 0., 0., 0.);
 
@@ -120,10 +123,10 @@
     }
     cairo_restore (cr);
 
-    write_png_argb32 (image, "text-rotate.png", WIDTH, HEIGHT, STRIDE);
-
     cairo_destroy (cr);
 
+    fclose (file);
+
     FcFini ();
 
     return 0;
Index: png/text.c
===================================================================
RCS file: /cvs/cairo/cairo-demo/png/text.c,v
retrieving revision 1.5
diff -u -r1.5 text.c
--- png/text.c	16 Dec 2003 15:23:36 -0000	1.5
+++ png/text.c	8 Feb 2004 17:29:29 -0000
@@ -28,21 +28,20 @@
 #include <cairo.h>
 #include <math.h>
 
-#include "write_png.h"
+#ifndef CAIRO_HAS_PNG_SURFACE
+#error "cairo not compiled with PNG backend"
+#endif
 
 #define TEXT "hello, world"
 
 #define WIDTH 450
 #define HEIGHT 600
-#define STRIDE (WIDTH * 4)
 
 #define DOUBLE_TO_26_6(d) ((FT_F26Dot6)((d) * 64.0))
 #define DOUBLE_FROM_26_6(t) ((double)(t) / 64.0)
 #define DOUBLE_TO_16_16(d) ((FT_Fixed)((d) * 65536.0))
 #define DOUBLE_FROM_16_16(t) ((double)(t) / 65536.0)
 
-char image[STRIDE*HEIGHT];
-
 static void
 box_text (cairo_t *cr, const char *utf8, double x, double y)
 {
@@ -110,6 +109,7 @@
 main (void)
 {
     int i;
+    FILE *file;
     cairo_t *cr;
     cairo_text_extents_t extents;
     cairo_font_extents_t font_extents;
@@ -120,8 +120,10 @@
 
     cr = cairo_create ();
 
-    cairo_set_target_image (cr, image, CAIRO_FORMAT_ARGB32,
-			    WIDTH, HEIGHT, STRIDE);
+    file = fopen ("text.png", "wb");
+
+    cairo_set_target_png (cr, file, CAIRO_FORMAT_ARGB32,
+			  WIDTH, HEIGHT, 96.0);
 
     cairo_set_rgb_color (cr, 0., 0., 0.);
     cairo_set_line_width (cr, 2.0);
@@ -200,10 +202,10 @@
 	glyphs[i].y += i * 5;
     box_glyphs (cr, glyphs, NUM_GLYPHS, 10, height);
 
-    write_png_argb32 (image, "text.png", WIDTH, HEIGHT, STRIDE);
-
     cairo_destroy (cr);
 
+    fclose (file);
+
     FcFini ();
 
     return 0;


More information about the cairo mailing list