Re: unneeded buggy code in gdk-pixbuf PGN saving



Owen Taylor <otaylor redhat com> writes:

> (...)
> 
> Maybe you could extend testpixbuf-save to test the alpha case? Also, 
> if you are using this code, you probably want to fix the silly:
> 
>                        for (j = 0, x = 0; x < w; x++)
>                                memcpy (&(data[x*3]), &(ptr[x*3]), 3);

Done.

The patch has become a bit lenghty :) ok to commit?

ciao,
--Mitch


Index: gdk-pixbuf/io-png.c
===================================================================
RCS file: /cvs/gnome/gtk+/gdk-pixbuf/io-png.c,v
retrieving revision 1.42
diff -u -p -r1.42 io-png.c
--- gdk-pixbuf/io-png.c	2001/10/05 18:51:47	1.42
+++ gdk-pixbuf/io-png.c	2001/12/11 11:29:14
@@ -720,10 +720,9 @@ gdk_pixbuf__png_image_save (FILE        
        png_textp text_ptr = NULL;
        guchar *ptr;
        guchar *pixels;
-       int x, y;
-       int i, j;
+       int y;
+       int i;
        png_bytep row_ptr;
-       png_bytep data;
        png_color_8 sig_bit;
        int w, h, rowstride;
        int has_alpha;
@@ -787,8 +786,6 @@ gdk_pixbuf__png_image_save (FILE        
                }
        }
 
-       data = NULL;
-       
        bpc = gdk_pixbuf_get_bits_per_sample (pixbuf);
        w = gdk_pixbuf_get_width (pixbuf);
        h = gdk_pixbuf_get_height (pixbuf);
@@ -823,30 +820,10 @@ gdk_pixbuf__png_image_save (FILE        
                png_set_IHDR (png_ptr, info_ptr, w, h, bpc,
                              PNG_COLOR_TYPE_RGB_ALPHA, PNG_INTERLACE_NONE,
                              PNG_COMPRESSION_TYPE_BASE, PNG_FILTER_TYPE_BASE);
-#ifdef WORDS_BIGENDIAN
-               png_set_swap_alpha (png_ptr);
-#else
-               png_set_bgr (png_ptr);
-#endif
        } else {
                png_set_IHDR (png_ptr, info_ptr, w, h, bpc,
                              PNG_COLOR_TYPE_RGB, PNG_INTERLACE_NONE,
                              PNG_COMPRESSION_TYPE_BASE, PNG_FILTER_TYPE_BASE);
-               data = g_try_malloc (w * 3 * sizeof (char));
-
-               if (data == NULL) {
-                       /* Check error NULL, normally this would be broken,
-                        * but libpng makes me want to code defensively.
-                        */
-                       if (error && *error == NULL) {
-                               g_set_error (error,
-                                            GDK_PIXBUF_ERROR,
-                                            GDK_PIXBUF_ERROR_INSUFFICIENT_MEMORY,
-                                            _("Insufficient memory to save PNG file"));
-                       }
-                       png_destroy_write_struct (&png_ptr, (png_infopp) NULL);
-                       return FALSE;
-               }
        }
        sig_bit.red = bpc;
        sig_bit.green = bpc;
@@ -859,20 +836,10 @@ gdk_pixbuf__png_image_save (FILE        
 
        ptr = pixels;
        for (y = 0; y < h; y++) {
-               if (has_alpha)
-                       row_ptr = (png_bytep)ptr;
-               else {
-                       for (j = 0, x = 0; x < w; x++)
-                               memcpy (&(data[x*3]), &(ptr[x*3]), 3);
-
-                       row_ptr = (png_bytep)data;
-               }
+               row_ptr = (png_bytep)ptr;
                png_write_rows (png_ptr, &row_ptr, 1);
                ptr += rowstride;
        }
-
-       if (data)
-               g_free (data);
 
        png_write_end (png_ptr, info_ptr);
        png_destroy_write_struct (&png_ptr, (png_infopp) NULL);
Index: demos/testpixbuf-save.c
===================================================================
RCS file: /cvs/gnome/gtk+/demos/testpixbuf-save.c,v
retrieving revision 1.9
diff -u -p -r1.9 testpixbuf-save.c
--- demos/testpixbuf-save.c	2001/10/20 23:39:31	1.9
+++ demos/testpixbuf-save.c	2001/12/11 11:29:14
@@ -4,7 +4,69 @@
 
 #include <gtk/gtk.h>
 
-void
+static void
+compare_pixbufs (GdkPixbuf *pixbuf, GdkPixbuf *compare, const gchar *file_type)
+{
+        if ((gdk_pixbuf_get_width (pixbuf) !=
+             gdk_pixbuf_get_width (compare)) ||
+            (gdk_pixbuf_get_height (pixbuf) !=
+             gdk_pixbuf_get_height (compare)) ||
+            (gdk_pixbuf_get_n_channels (pixbuf) !=
+             gdk_pixbuf_get_n_channels (compare)) ||
+            (gdk_pixbuf_get_has_alpha (pixbuf) !=
+             gdk_pixbuf_get_has_alpha (compare)) ||
+            (gdk_pixbuf_get_bits_per_sample (pixbuf) !=
+             gdk_pixbuf_get_bits_per_sample (compare))) {
+                fprintf (stderr,
+                         "saved %s file differs from copy in memory\n",
+                         file_type);
+        } else {
+                guchar *orig_pixels;
+                guchar *compare_pixels;
+                gint    orig_rowstride;
+                gint    compare_rowstride;
+                gint    width;
+                gint    height;
+                gint    bytes_per_pixel;
+                gint    x, y;
+                guchar *p1, *p2;
+                gint    count = 0;
+
+                orig_pixels = gdk_pixbuf_get_pixels (pixbuf);
+                compare_pixels = gdk_pixbuf_get_pixels (compare);
+
+                orig_rowstride = gdk_pixbuf_get_rowstride (pixbuf);
+                compare_rowstride = gdk_pixbuf_get_rowstride (compare);
+
+                width = gdk_pixbuf_get_width (pixbuf);
+                height = gdk_pixbuf_get_height (pixbuf);
+
+                /*  well...  */
+                bytes_per_pixel = gdk_pixbuf_get_n_channels (pixbuf);
+
+                p1 = orig_pixels;
+                p2 = compare_pixels;
+
+                for (y = 0; y < height; y++) {
+                        for (x = 0; x < width * bytes_per_pixel; x++)
+                                count += (*p1++ != *p2++);
+
+                        orig_pixels += orig_rowstride;
+                        compare_pixels += compare_rowstride;
+
+                        p1 = orig_pixels;
+                        p2 = compare_pixels;
+                }
+
+                if (count > 0) {
+                        fprintf (stderr,
+                                 "saved %s file differs from copy in memory\n",
+                                 file_type);
+                }
+        }
+}
+
+static void
 keypress_check (GtkWidget *widget, GdkEventKey *evt, gpointer data)
 {
         GdkPixbuf *pixbuf;
@@ -27,8 +89,20 @@ keypress_check (GtkWidget *widget, GdkEv
                                       NULL)) {
                         fprintf (stderr, "%s", err->message);
                         g_error_free (err);
+                } else {
+                        GdkPixbuf *compare;
+
+                        compare = gdk_pixbuf_new_from_file ("foo.jpg", &err);
+
+                        if (!compare) {
+                                fprintf (stderr, "%s", err->message);
+                                g_error_free (err);
+                        } else {
+                                compare_pixbufs (pixbuf, compare, "jpeg");
+                                g_object_unref (G_OBJECT (compare));
+                        }
+                                        
                 }
-                
         } else if (evt->keyval == 'p') {
                 if (pixbuf == NULL) {
                         fprintf (stderr, "PIXBUF NULL\n");
@@ -41,19 +115,46 @@ keypress_check (GtkWidget *widget, GdkEv
                                       NULL)) {
                         fprintf (stderr, "%s", err->message);
                         g_error_free (err);
+                } else {
+                        GdkPixbuf *compare;
+
+                        compare = gdk_pixbuf_new_from_file ("foo.png", &err);
+
+                        if (!compare) {
+                                fprintf (stderr, "%s", err->message);
+                                g_error_free (err);
+                        } else {
+                                compare_pixbufs (pixbuf, compare, "png");
+                                g_object_unref (G_OBJECT (compare));
+                        }
+                                        
                 }
+        } else if (evt->keyval == 'a') {
+                if (pixbuf == NULL) {
+                        fprintf (stderr, "PIXBUF NULL\n");
+                        return;
+                } else {
+                        GdkPixbuf *alpha_buf;
+
+                        alpha_buf = gdk_pixbuf_add_alpha (pixbuf,
+                                                          FALSE, 0, 0, 0);
+
+                        g_object_set_data_full (G_OBJECT (da),
+                                                "pixbuf", alpha_buf,
+                                                (GDestroyNotify) g_object_unref);
+                }
         }
 }
 
 
-int
+static int
 close_app (GtkWidget *widget, gpointer data)
 {
         gtk_main_quit ();
         return TRUE;
 }
 
-int
+static int
 expose_cb (GtkWidget *drawing_area, GdkEventExpose *evt, gpointer data)
 {
         GdkPixbuf *pixbuf;
@@ -86,7 +187,7 @@ expose_cb (GtkWidget *drawing_area, GdkE
         return FALSE;
 }
 
-int
+static int
 configure_cb (GtkWidget *drawing_area, GdkEventConfigure *evt, gpointer data)
 {
         GdkPixbuf *pixbuf;
@@ -146,7 +247,8 @@ main (int argc, char **argv)
 			  G_CALLBACK (configure_cb), NULL);
         g_signal_connect (window, "key_press_event", 
 			  G_CALLBACK (keypress_check), drawing_area);    
-        g_object_set_data (G_OBJECT (drawing_area), "pixbuf", pixbuf);
+        g_object_set_data_full (G_OBJECT (drawing_area), "pixbuf", pixbuf,
+                                (GDestroyNotify) g_object_unref);
         gtk_box_pack_start (GTK_BOX (vbox), drawing_area, TRUE, TRUE, 0);
    
         gtk_widget_show_all (window);


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]