[cairo-commit] cairo-demo/cairo_snippets ChangeLog, 1.2, 1.3 cairo_snippets_png.c, 1.1, 1.2 png_io.c, 1.1, 1.2 png_io.h, 1.1, 1.2

OEyvind Kolaas commit at pdx.freedesktop.org
Tue May 18 05:47:44 PDT 2004


Committed by: pippin

Update of /cvs/cairo/cairo-demo/cairo_snippets
In directory pdx:/tmp/cvs-serv4880

Modified Files:
	ChangeLog cairo_snippets_png.c png_io.c png_io.h 
Log Message:
tidied up the png writing backend


Index: ChangeLog
===================================================================
RCS file: /cvs/cairo/cairo-demo/cairo_snippets/ChangeLog,v
retrieving revision 1.2
retrieving revision 1.3
diff -C2 -d -r1.2 -r1.3
*** a/ChangeLog	18 May 2004 02:34:16 -0000	1.2
--- b/ChangeLog	18 May 2004 12:47:41 -0000	1.3
***************
*** 1,5 ****
  2004-05-18 OEyvind Kolaas <pippin at freedesktop.org> 
!    * cairo_snippets_pdf.c, Makefile: added pdf backend, initially commented
!    out in Makefile
      
  2004-05-16 OEyvind Kolaas <pippin at freedesktop.org> 
--- 1,10 ----
  2004-05-18 OEyvind Kolaas <pippin at freedesktop.org> 
! 	* cairo_snippets_png.c: code cleanup, change to use the actual png backend
! 	instead of using the png writing function in png_io.
! 	* png_io.[ch]: removed png writing function
! 
! 2004-05-18 OEyvind Kolaas <pippin at freedesktop.org> 
! 	* cairo_snippets_pdf.c, Makefile: added pdf backend, initially commented
! 	  out in Makefile
      
  2004-05-16 OEyvind Kolaas <pippin at freedesktop.org> 

Index: cairo_snippets_png.c
===================================================================
RCS file: /cvs/cairo/cairo-demo/cairo_snippets/cairo_snippets_png.c,v
retrieving revision 1.1
retrieving revision 1.2
diff -C2 -d -r1.1 -r1.2
*** a/cairo_snippets_png.c	16 May 2004 19:09:50 -0000	1.1
--- b/cairo_snippets_png.c	18 May 2004 12:47:41 -0000	1.2
***************
*** 8,19 ****
  #define IMAGE_HEIGHT 256
  
- #define STRIDE (IMAGE_WIDTH * 4)
- 
  #define LINE_WIDTH 0.04
  
! char image[STRIDE*IMAGE_HEIGHT];
  
! static write_image (cairo_t *cr, int no)
  {
          char pngfile[1024];
  
--- 8,38 ----
  #define IMAGE_HEIGHT 256
  
  #define LINE_WIDTH 0.04
  
! /* process a snippet, writing it out to file */
! static void
! snippet_do_png (int no);
  
! /* write an snippets.html file, containing inline images, and source code */
! int
! write_html (void);
! 
! int
! main (void)
! {
!         int i;
! 
!         for (i=0;i<snippet_count;i++)
!              snippet_do_png (i);
!         write_html ();
! 
!         return 0;
! }
! 
! static void
! snippet_do_png (int no)
  {
+         cairo_t *cr;
+         FILE *file;
          char pngfile[1024];
  
***************
*** 22,25 ****
--- 41,50 ----
          sprintf (pngfile, "%s.png", snippet_name [no]);
  
+         file = fopen (pngfile, "w");
+ 
+         cr = cairo_create ();
+ 
+         cairo_set_target_png (cr, file, CAIRO_FORMAT_ARGB32, IMAGE_WIDTH, IMAGE_HEIGHT);
+ 
          cairo_save (cr);
            cairo_scale (cr, IMAGE_WIDTH/1.0, IMAGE_HEIGHT/1.0);
***************
*** 38,44 ****
          cairo_stroke (cr);
  
!         write_png_argb32 (image, pngfile, IMAGE_WIDTH, IMAGE_HEIGHT, STRIDE);
  
          fprintf (stdout, "\n");
  }
  
--- 63,72 ----
          cairo_stroke (cr);
  
!         cairo_show_page (cr);
  
          fprintf (stdout, "\n");
+ 
+         cairo_destroy (cr);
+         fclose (file);
  }
  
***************
*** 135,155 ****
  }
  
- int
- main (void)
- {
-         cairo_t *cr;
-         int i;
- 
-         cr = cairo_create ();
- 
-         cairo_set_target_image (cr, image, CAIRO_FORMAT_ARGB32,
-                  IMAGE_WIDTH, IMAGE_HEIGHT, STRIDE);
- 
-         for (i=0;i<snippet_count;i++)
-              write_image (cr, i);
-         write_html ();
  
-         cairo_destroy (cr);
- 
-         return 0;
- }
--- 163,165 ----

Index: png_io.c
===================================================================
RCS file: /cvs/cairo/cairo-demo/cairo_snippets/png_io.c,v
retrieving revision 1.1
retrieving revision 1.2
diff -C2 -d -r1.1 -r1.2
*** a/png_io.c	16 May 2004 19:09:50 -0000	1.1
--- b/png_io.c	18 May 2004 12:47:41 -0000	1.2
***************
*** 9,36 ****
  
  static void
- unpremultiply_data (png_structp   png,
-                     png_row_infop row_info,
-                     png_bytep     data)
- {
-     int i;
- 
-     for (i = 0; i < row_info->rowbytes; i += 4) {
-         unsigned char *b = &data[i];
-         unsigned char alpha = b[3];
-         unsigned long pixel;
-         unsigned long *p;
-         if (alpha == 0)
-             pixel = 0;
-         else
-             pixel = ((((b[0] * 255) / alpha) << 0) |
-                      (((b[1] * 255) / alpha) << 8) |
-                      (((b[2] * 255) / alpha) << 16) |
-                      (alpha << 24));
-         p = (unsigned long *) b;
-         *p = pixel;
-     }
- }
- 
- static void
  premultiply_data (png_structp   png,
                    png_row_infop row_info,
--- 9,12 ----
***************
*** 55,108 ****
  }
  
- void
- write_png_argb32 (char       *buffer,
-                   const char *filename,
- 		          int         width,
-                   int         height,
-                   int         stride)
- {
-     FILE* f;
-     png_struct* png;
-     png_info* info;
-     png_byte** rows;
-     int       i;
-     png_color_16 white;
- 
-     f = fopen (filename, "w");
-     rows = malloc (height*sizeof (png_byte*));
- 
-     for (i=0;i<height;i++) {
- 	rows[i]=buffer+i*stride;
-     }
- 
-     png = png_create_write_struct (PNG_LIBPNG_VER_STRING, NULL, NULL, NULL);
-     info = png_create_info_struct (png);
- 
-     png_init_io (png, f);
-     png_set_IHDR (png, info,
- 		  width, height, 8,
- 		  PNG_COLOR_TYPE_RGB_ALPHA, 
- 		  PNG_INTERLACE_NONE,
- 		  PNG_COMPRESSION_TYPE_DEFAULT,
- 		  PNG_FILTER_TYPE_DEFAULT);
- 
-     white.red=0xff;
-     white.blue=0xff;
-     white.green=0xff;
-     png_set_bKGD (png, info, &white);
- 
-     png_set_write_user_transform_fn (png, unpremultiply_data);
-     png_set_bgr (png);
- 
-     png_write_info (png, info);
-     png_write_image (png, rows);
-     png_write_end (png, info);
- 
-     png_destroy_write_struct (&png, &info);
- 
-     free (rows);
-     fclose (f);
- }
- 
  char *
  read_png_argb32 (const char *filename,
--- 31,34 ----

Index: png_io.h
===================================================================
RCS file: /cvs/cairo/cairo-demo/cairo_snippets/png_io.h,v
retrieving revision 1.1
retrieving revision 1.2
diff -C2 -d -r1.1 -r1.2
*** a/png_io.h	16 May 2004 19:09:50 -0000	1.1
--- b/png_io.h	18 May 2004 12:47:41 -0000	1.2
***************
*** 2,12 ****
  #define PNG_IO_H
  
- void
- write_png_argb32 (char       *buffer,
-                   const char *filename,
- 		          int         width,
-                   int         height,
-                   int         stride);
- 
  char *
  read_png_argb32 (const char *filename,
--- 2,5 ----





More information about the cairo-commit mailing list