]> Pileus Git - ~andy/gtk/blobdiff - demos/testpixbuf.c
contrib subdir
[~andy/gtk] / demos / testpixbuf.c
index 374354a050b709b0566c9213f47d5ded48960f96..f118205e2153c802f4c402839ace0532ce33004a 100644 (file)
@@ -1,29 +1,41 @@
-
 /* testpixbuf -- test program for gdk-pixbuf code
  * Copyright (C) 1999 Mark Crichton, Larry Ewing
  *
  * This library is free software; you can redistribute it and/or
- * modify it under the terms of the GNU Library General Public
+ * modify it under the terms of the GNU Lesser General Public
  * License as published by the Free Software Foundation; either
  * version 2 of the License, or (at your option) any later version.
  *
  * This library is distributed in the hope that it will be useful,
  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
- * Library General Public License for more details.
+ * Lesser General Public License for more details.
  *
- * You should have received a copy of the GNU Library General Public
+ * You should have received a copy of the GNU Lesser General Public
  * License along with this library; if not, write to the
  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
  * Boston, MA 02111-1307, USA.
  */
 
+#include <config.h>
+#include <stdio.h>
 #include <stdlib.h>
 #include <unistd.h>
 #include <string.h>
-
 #include <gtk/gtk.h>
-#include "gdk-pixbuf.h"
+#include <gtk/gdk-pixbuf-loader.h>
+
+#include "test-inline-pixbufs.h"
+
+typedef struct {
+       FILE             *imagefile;
+       GdkPixbufLoader  *loader;
+       GtkWidget        **rgbwin;
+       guchar           *buf;
+       guint            timeout;
+       guint            readlen;
+} ProgressFileStatus;
+
 
 #define DEFAULT_WIDTH  24
 #define DEFAULT_HEIGHT 24
@@ -308,17 +320,17 @@ expose_func (GtkWidget *drawing_area, GdkEventExpose *event, gpointer data)
 
        pixbuf = (GdkPixbuf *)gtk_object_get_data(GTK_OBJECT(drawing_area), "pixbuf");
 
-       if (pixbuf->art_pixbuf->has_alpha) {
+       if (gdk_pixbuf_get_has_alpha (pixbuf)) {
                gdk_draw_rgb_32_image (drawing_area->window,
                                       drawing_area->style->black_gc,
                                       event->area.x, event->area.y, 
                                       event->area.width, 
                                       event->area.height,
                                       GDK_RGB_DITHER_MAX, 
-                                      pixbuf->art_pixbuf->pixels 
-                                      + (event->area.y * pixbuf->art_pixbuf->rowstride
-                                      + (event->area.x * pixbuf->art_pixbuf->n_channels),
-                                      pixbuf->art_pixbuf->rowstride);
+                                      gdk_pixbuf_get_pixels (pixbuf)
+                                      + (event->area.y * gdk_pixbuf_get_rowstride (pixbuf)
+                                      + (event->area.x * gdk_pixbuf_get_n_channels (pixbuf)),
+                                      gdk_pixbuf_get_rowstride (pixbuf));
        } else {
                gdk_draw_rgb_image (drawing_area->window,
                                    drawing_area->style->white_gc,
@@ -326,12 +338,12 @@ expose_func (GtkWidget *drawing_area, GdkEventExpose *event, gpointer data)
                                    event->area.width, 
                                    event->area.height,
                                    GDK_RGB_DITHER_NORMAL,
-                                   pixbuf->art_pixbuf->pixels 
-                                   + (event->area.y * pixbuf->art_pixbuf->rowstride) 
-                                   + (event->area.x * pixbuf->art_pixbuf->n_channels),
-                                   pixbuf->art_pixbuf->rowstride);
+                                   gdk_pixbuf_get_pixels (pixbuf)
+                                   + (event->area.y * gdk_pixbuf_get_rowstride (pixbuf))
+                                   + (event->area.x * gdk_pixbuf_get_n_channels (pixbuf)),
+                                   gdk_pixbuf_get_rowstride (pixbuf));
        }
-}  
+}
 
 static void
 config_func (GtkWidget *drawing_area, GdkEventConfigure *event, gpointer data)
@@ -343,28 +355,30 @@ config_func (GtkWidget *drawing_area, GdkEventConfigure *event, gpointer data)
        g_print("X:%d Y:%d\n", event->width, event->height);
 
 #if 0
-       if (((event->width) != (pixbuf->art_pixbuf->width)) ||
-           ((event->height) != (pixbuf->art_pixbuf->height))) 
+       if (((event->width) != gdk_pixbuf_get_width (pixbuf)) ||
+           ((event->height) != gdk_pixbuf_get_height (pixbuf)))
                gdk_pixbuf_scale(pixbuf, event->width, event->height);
 #endif
 }
 
-static void
-new_testrgb_window (GdkPixbuf *pixbuf)
+static GtkWidget*
+new_testrgb_window (GdkPixbuf *pixbuf, gchar *title)
 {
        GtkWidget *window;
        GtkWidget *vbox;
+       GtkWidget *temp_box;
        GtkWidget *button;
        GtkWidget *drawing_area;
        gint w, h;
-       w = pixbuf->art_pixbuf->width;
-       h = pixbuf->art_pixbuf->height;
+
+        g_return_val_if_fail (pixbuf != NULL, NULL);
+       w = gdk_pixbuf_get_width (pixbuf);
+       h = gdk_pixbuf_get_height (pixbuf);
 
        window = gtk_widget_new (gtk_window_get_type (),
                                 "GtkObject::user_data", NULL,
                                 "GtkWindow::type", GTK_WINDOW_TOPLEVEL,
-                                "GtkWindow::title", "testrgb",
+                                "GtkWindow::title", title ? title : "testrgb",
                                 "GtkWindow::allow_shrink", TRUE,
                                 NULL);
        gtk_signal_connect (GTK_OBJECT (window), "destroy",
@@ -372,10 +386,17 @@ new_testrgb_window (GdkPixbuf *pixbuf)
 
        vbox = gtk_vbox_new (FALSE, 0);
 
+       if (title)
+               gtk_box_pack_start (GTK_BOX (vbox), gtk_label_new (title),
+                                   TRUE, TRUE, 0);
+
        drawing_area = gtk_drawing_area_new ();
 
+       temp_box = gtk_hbox_new (FALSE, 0);
        gtk_drawing_area_size (GTK_DRAWING_AREA(drawing_area), w, h);
-       gtk_box_pack_start (GTK_BOX (vbox), drawing_area, TRUE, TRUE, 0);
+       gtk_box_pack_start (GTK_BOX (temp_box), drawing_area, FALSE, FALSE, 0);
+       gtk_box_pack_start (GTK_BOX (vbox), temp_box, FALSE, FALSE, 0);
+       
 
        gtk_signal_connect (GTK_OBJECT(drawing_area), "expose_event",
                            GTK_SIGNAL_FUNC(expose_func), NULL);
@@ -395,18 +416,104 @@ new_testrgb_window (GdkPixbuf *pixbuf)
        gtk_widget_show (button);
 
        gtk_container_add (GTK_CONTAINER (window), vbox);
-       gtk_widget_show (vbox);
+       gtk_widget_show_all (vbox);
 
        gtk_widget_show (window);
+
+        return window;
+}
+
+
+static gint
+update_timeout(gpointer data)
+{
+        ProgressFileStatus *status = data;
+       gboolean done, error;
+        
+       done = FALSE;
+        error = FALSE;
+       if (!feof(status->imagefile)) {
+               gint nbytes;
+
+               nbytes = fread(status->buf, 1, status->readlen, 
+                              status->imagefile);
+
+
+                error = !gdk_pixbuf_loader_write (GDK_PIXBUF_LOADER (status->loader), status->buf, nbytes);
+                if (error) {
+                        G_BREAKPOINT();
+                }
+
+        } else { /* Really done */ 
+
+                GdkPixbuf *pixbuf = gdk_pixbuf_loader_get_pixbuf (status->loader); 
+                new_testrgb_window (pixbuf, "After progressive load"); 
+                done = TRUE; 
+
+        }
+
+        if (error) { 
+                g_warning ("Serious error writing to loader"); 
+                done = TRUE; 
+        } 
+
+       if (done) {
+                gtk_widget_queue_draw(*status->rgbwin);
+               gdk_pixbuf_loader_close (GDK_PIXBUF_LOADER (status->loader));
+               gtk_object_destroy (GTK_OBJECT(status->loader));
+               fclose (status->imagefile);
+               g_free (status->buf);
+       }
+
+       return !done;
 }
 
+
+static void
+progressive_prepared_callback(GdkPixbufLoader* loader, gpointer data)
+{
+        GtkWidget** retloc = data;
+        GdkPixbuf* pixbuf;
+
+        pixbuf = gdk_pixbuf_loader_get_pixbuf(loader);
+        g_assert(pixbuf != NULL);
+
+        gdk_pixbuf_ref(pixbuf); /* for the RGB window */
+
+        *retloc = new_testrgb_window(pixbuf, "Progressive");
+
+        return;
+}
+
+
+static void
+progressive_updated_callback(GdkPixbufLoader* loader, guint x, guint y, guint width, guint height, gpointer data)
+{
+        GtkWidget** window_loc = data;
+
+/*     g_print ("progressive_updated_callback:\n\t%d\t%d\t%d\t%d\n", x, y, width, height); */
+
+        if (*window_loc != NULL)
+                gtk_widget_queue_draw_area(*window_loc,
+                                          x, y, width, height);
+
+        return;
+}
+
+static int readlen = 4096;
+
+extern void pixbuf_init();
+
 int
 main (int argc, char **argv)
 {
        int i;
-       int found_valid = FALSE; 
+       int found_valid = FALSE;
 
        GdkPixbuf *pixbuf;
+       GdkPixbufLoader *pixbuf_loader;
+
+       pixbuf_init ();
 
        gtk_init (&argc, &argv);
 
@@ -415,37 +522,90 @@ main (int argc, char **argv)
        gdk_rgb_init ();
 
        gtk_widget_set_default_colormap (gdk_rgb_get_cmap ());
-       gtk_widget_set_default_visual (gdk_rgb_get_visual ());
+
+       {
+               char *tbf_readlen = getenv("TBF_READLEN");
+               if(tbf_readlen) readlen = atoi(tbf_readlen);
+       }
+
+       {
+               char *tbf_bps = getenv("TBF_KBPS");
+               guint bps;
+
+               if (tbf_bps) {
+                       bps = atoi(tbf_bps);
+                       g_print ("Simulating %d kBytes/sec\n", bps);
+                       readlen = (bps*1024)/10;
+               }
+       }
 
        i = 1;
        if (argc == 1) {
                 const gchar*** xpmp;
                 
-               pixbuf = gdk_pixbuf_new_from_data ((guchar *) default_image, ART_PIX_RGB, FALSE,
+               pixbuf = gdk_pixbuf_new_from_data (default_image, GDK_COLORSPACE_RGB, FALSE, 8,
                                                   DEFAULT_WIDTH, DEFAULT_HEIGHT, DEFAULT_WIDTH * 3,
                                                   NULL, NULL);
-               new_testrgb_window (pixbuf);
+               new_testrgb_window (pixbuf, NULL);
 
                 xpmp = xpms;
                 while (*xpmp) {
                         pixbuf = gdk_pixbuf_new_from_xpm_data (*xpmp);
-                        new_testrgb_window (pixbuf);
+                        new_testrgb_window (pixbuf, NULL);
                         ++xpmp;
                 }
+
+                /* Test loading from inline data. */
+                pixbuf = gdk_pixbuf_new_from_inline (apple_red, FALSE, -1);
+                new_testrgb_window (pixbuf, "Red apple from inline data");
+
+                pixbuf = gdk_pixbuf_new_from_inline (gnome_foot, TRUE, sizeof (gnome_foot));
+                new_testrgb_window (pixbuf, "Foot from inline data");
                 
                found_valid = TRUE;
        } else {
                for (i = 1; i < argc; i++) {
+
                        pixbuf = gdk_pixbuf_new_from_file (argv[i]);
 #if 0
                        pixbuf = gdk_pixbuf_rotate(pixbuf, 10.0);
 #endif
 
                        if (pixbuf) {
-                               new_testrgb_window (pixbuf);
+                               new_testrgb_window (pixbuf, "File");
                                found_valid = TRUE;
                        }
                }
+#if 1
+                {
+                        GtkWidget* rgb_window = NULL;
+                       ProgressFileStatus   status;
+
+                        pixbuf_loader = gdk_pixbuf_loader_new ();
+                       status.loader = pixbuf_loader;
+
+                       status.rgbwin = &rgb_window;
+
+                       status.buf = g_malloc (readlen);
+                        gtk_signal_connect(GTK_OBJECT(pixbuf_loader),
+                                           "area_prepared",
+                                           GTK_SIGNAL_FUNC(progressive_prepared_callback),
+                                           &rgb_window);
+
+                        gtk_signal_connect(GTK_OBJECT(pixbuf_loader),
+                                           "area_updated",
+                                           GTK_SIGNAL_FUNC(progressive_updated_callback),
+                                           &rgb_window);
+
+                       
+                        status.imagefile = fopen (argv[1], "r");
+                        g_assert (status.imagefile != NULL);
+
+                       status.readlen = readlen;
+
+                        status.timeout = gtk_timeout_add(100, update_timeout, &status);
+                }
+#endif
        }
 
        if (found_valid)