]> Pileus Git - ~andy/gtk/blobdiff - demos/testpixbuf-save.c
Updated
[~andy/gtk] / demos / testpixbuf-save.c
index a97fd95858bb1ca3ac480e7e7125cf4f56d9e260..b3928ede86871c7bf2e77a44342cbebc9a93adfc 100644 (file)
@@ -1,19 +1,80 @@
+/* -*- Mode: C; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 8 -*- */
 
-#include <config.h>
-/* if building outside GTK, remove /x11 part */
-#include <gdk/x11/gdkx.h>
-#include <gtk/gtk.h>
+#include "config.h"
 #include <stdio.h>
 
+#include <gtk/gtk.h>
+
+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);
+                }
+        }
+}
 
-void
+static void
 keypress_check (GtkWidget *widget, GdkEventKey *evt, gpointer data)
 {
         GdkPixbuf *pixbuf;
         GtkDrawingArea *da = (GtkDrawingArea*)data;
         GError *err = NULL;
         
-        pixbuf = (GdkPixbuf *) gtk_object_get_data (GTK_OBJECT (da), "pixbuf");
+        pixbuf = (GdkPixbuf *) g_object_get_data (G_OBJECT (da), "pixbuf");
 
         if (evt->keyval == 'q')
                 gtk_main_quit ();
@@ -29,36 +90,78 @@ keypress_check (GtkWidget *widget, GdkEventKey *evt, gpointer data)
                                       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 (compare);
+                        }
+                                        
                 }
-                
         } else if (evt->keyval == 'p') {
                 if (pixbuf == NULL) {
                         fprintf (stderr, "PIXBUF NULL\n");
                         return;
                 }
 
-                if (!gdk_pixbuf_save (pixbuf, "foo.png", "png", &err, NULL)) {
+                if (!gdk_pixbuf_save (pixbuf, "foo.png", "png", 
+                                      &err,
+                                      "tEXt::Software", "testpixbuf-save",
+                                      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 (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;
          
-        pixbuf = (GdkPixbuf *) gtk_object_get_data (GTK_OBJECT (drawing_area),
-                                                    "pixbuf");
+        pixbuf = (GdkPixbuf *) g_object_get_data (G_OBJECT (drawing_area),
+                                                 "pixbuf");
         if (gdk_pixbuf_get_has_alpha (pixbuf)) {
                 gdk_draw_rgb_32_image (drawing_area->window,
                                        drawing_area->style->black_gc,
@@ -85,24 +188,24 @@ expose_cb (GtkWidget *drawing_area, GdkEventExpose *evt, gpointer data)
         return FALSE;
 }
 
-int
+static int
 configure_cb (GtkWidget *drawing_area, GdkEventConfigure *evt, gpointer data)
 {
         GdkPixbuf *pixbuf;
                            
-        pixbuf = (GdkPixbuf *) gtk_object_get_data (GTK_OBJECT (drawing_area),   
-                                                    "pixbuf");
+        pixbuf = (GdkPixbuf *) g_object_get_data (G_OBJECT (drawing_area),   
+                                                 "pixbuf");
     
         g_print ("X:%d Y:%d\n", evt->width, evt->height);
         if (evt->width != gdk_pixbuf_get_width (pixbuf) || evt->height != gdk_pixbuf_get_height (pixbuf)) {
                 GdkWindow *root;
                 GdkPixbuf *new_pixbuf;
 
-                root = GDK_ROOT_PARENT ();
+                root = gdk_get_default_root_window ();
                 new_pixbuf = gdk_pixbuf_get_from_drawable (NULL, root, NULL,
                                                            0, 0, 0, 0, evt->width, evt->height);
-                gtk_object_set_data (GTK_OBJECT (drawing_area), "pixbuf", new_pixbuf);
-                gdk_pixbuf_unref (pixbuf);
+                g_object_set_data_full (G_OBJECT (drawing_area), "pixbuf", new_pixbuf,
+                                        (GDestroyNotify) g_object_unref);
         }
 
         return FALSE;
@@ -119,33 +222,34 @@ main (int argc, char **argv)
    
         gtk_init (&argc, &argv);   
 
-        gtk_widget_set_default_colormap (gdk_rgb_get_cmap ());
+        gtk_widget_set_default_colormap (gdk_rgb_get_colormap ());
 
-        root = GDK_ROOT_PARENT ();
+        root = gdk_get_default_root_window ();
         pixbuf = gdk_pixbuf_get_from_drawable (NULL, root, NULL,
                                                0, 0, 0, 0, 150, 160);
    
         window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
-        gtk_signal_connect (GTK_OBJECT (window), "delete_event",
-                            GTK_SIGNAL_FUNC (close_app), NULL);
-        gtk_signal_connect (GTK_OBJECT (window), "destroy",   
-                            GTK_SIGNAL_FUNC (close_app), NULL);
+        g_signal_connect (window, "delete_event",
+                         G_CALLBACK (close_app), NULL);
+        g_signal_connect (window, "destroy",   
+                         G_CALLBACK (close_app), NULL);
    
         vbox = gtk_vbox_new (FALSE, 0);
         gtk_container_add (GTK_CONTAINER (window), vbox);  
    
         drawing_area = gtk_drawing_area_new ();
-        gtk_widget_set_usize (GTK_WIDGET (drawing_area),
-                              gdk_pixbuf_get_width (pixbuf),
-                              gdk_pixbuf_get_height (pixbuf));
-        gtk_signal_connect (GTK_OBJECT (drawing_area), "expose_event",
-                            GTK_SIGNAL_FUNC (expose_cb), NULL);
-
-        gtk_signal_connect (GTK_OBJECT (drawing_area), "configure_event",
-                            GTK_SIGNAL_FUNC (configure_cb), NULL);
-        gtk_signal_connect (GTK_OBJECT (window), "key_press_event", 
-                            GTK_SIGNAL_FUNC (keypress_check), drawing_area);    
-        gtk_object_set_data (GTK_OBJECT (drawing_area), "pixbuf", pixbuf);
+        gtk_widget_set_size_request (GTK_WIDGET (drawing_area),
+                                     gdk_pixbuf_get_width (pixbuf),
+                                     gdk_pixbuf_get_height (pixbuf));
+        g_signal_connect (drawing_area, "expose_event",
+                         G_CALLBACK (expose_cb), NULL);
+
+        g_signal_connect (drawing_area, "configure_event",
+                         G_CALLBACK (configure_cb), NULL);
+        g_signal_connect (window, "key_press_event", 
+                         G_CALLBACK (keypress_check), drawing_area);    
+        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);