1999-12-17 21:43:04 +00:00
|
|
|
|
|
|
|
/* 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
|
2000-07-26 11:33:08 +00:00
|
|
|
* modify it under the terms of the GNU Lesser General Public
|
1999-12-17 21:43:04 +00:00
|
|
|
* 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
|
2000-07-26 11:33:08 +00:00
|
|
|
* Lesser General Public License for more details.
|
1999-12-17 21:43:04 +00:00
|
|
|
*
|
2000-07-26 11:33:08 +00:00
|
|
|
* You should have received a copy of the GNU Lesser General Public
|
2012-02-27 13:01:10 +00:00
|
|
|
* License along with this library. If not, see <http://www.gnu.org/licenses/>.
|
1999-12-17 21:43:04 +00:00
|
|
|
*/
|
|
|
|
|
2008-06-22 14:28:52 +00:00
|
|
|
#include "config.h"
|
2000-04-11 07:03:25 +00:00
|
|
|
#include <stdio.h>
|
1999-12-17 21:43:04 +00:00
|
|
|
#include <stdlib.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
#include <string.h>
|
2001-05-07 15:58:47 +00:00
|
|
|
#include <errno.h>
|
1999-12-17 21:43:04 +00:00
|
|
|
#include <gtk/gtk.h>
|
2001-05-07 15:58:47 +00:00
|
|
|
|
|
|
|
typedef struct _LoadContext LoadContext;
|
|
|
|
|
|
|
|
struct _LoadContext
|
|
|
|
{
|
|
|
|
gchar *filename;
|
|
|
|
GtkWidget *window;
|
|
|
|
GdkPixbufLoader *pixbuf_loader;
|
|
|
|
guint load_timeout;
|
|
|
|
FILE* image_stream;
|
1999-12-17 21:43:04 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
static void
|
2001-05-07 15:58:47 +00:00
|
|
|
destroy_context (gpointer data)
|
1999-12-17 21:43:04 +00:00
|
|
|
{
|
2001-05-07 15:58:47 +00:00
|
|
|
LoadContext *lc = data;
|
|
|
|
|
|
|
|
g_free (lc->filename);
|
|
|
|
|
|
|
|
if (lc->load_timeout)
|
|
|
|
g_source_remove (lc->load_timeout);
|
|
|
|
|
|
|
|
if (lc->image_stream)
|
|
|
|
fclose (lc->image_stream);
|
|
|
|
|
|
|
|
if (lc->pixbuf_loader)
|
|
|
|
{
|
|
|
|
gdk_pixbuf_loader_close (lc->pixbuf_loader, NULL);
|
2002-09-29 21:24:24 +00:00
|
|
|
g_object_unref (lc->pixbuf_loader);
|
2001-05-07 15:58:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
g_free (lc);
|
1999-12-17 21:43:04 +00:00
|
|
|
}
|
|
|
|
|
2001-05-07 15:58:47 +00:00
|
|
|
static LoadContext*
|
|
|
|
get_load_context (GtkWidget *image)
|
1999-12-17 21:43:04 +00:00
|
|
|
{
|
2001-05-07 15:58:47 +00:00
|
|
|
LoadContext *lc;
|
|
|
|
|
|
|
|
lc = g_object_get_data (G_OBJECT (image), "lc");
|
|
|
|
|
|
|
|
if (lc == NULL)
|
|
|
|
{
|
|
|
|
lc = g_new0 (LoadContext, 1);
|
|
|
|
|
|
|
|
g_object_set_data_full (G_OBJECT (image),
|
|
|
|
"lc",
|
|
|
|
lc,
|
|
|
|
destroy_context);
|
|
|
|
}
|
|
|
|
|
|
|
|
return lc;
|
1999-12-17 21:43:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2001-05-07 15:58:47 +00:00
|
|
|
progressive_prepared_callback (GdkPixbufLoader* loader,
|
|
|
|
gpointer data)
|
1999-12-17 21:43:04 +00:00
|
|
|
{
|
2001-05-07 15:58:47 +00:00
|
|
|
GdkPixbuf* pixbuf;
|
|
|
|
GtkWidget* image;
|
1999-12-17 21:43:04 +00:00
|
|
|
|
2001-05-07 15:58:47 +00:00
|
|
|
image = GTK_WIDGET (data);
|
|
|
|
|
|
|
|
pixbuf = gdk_pixbuf_loader_get_pixbuf (loader);
|
|
|
|
|
|
|
|
/* Avoid displaying random memory contents, since the pixbuf
|
|
|
|
* isn't filled in yet.
|
|
|
|
*/
|
|
|
|
gdk_pixbuf_fill (pixbuf, 0xaaaaaaff);
|
|
|
|
|
|
|
|
/* Could set the pixbuf instead, if we only wanted to display
|
|
|
|
* static images.
|
|
|
|
*/
|
|
|
|
gtk_image_set_from_animation (GTK_IMAGE (image),
|
|
|
|
gdk_pixbuf_loader_get_animation (loader));
|
1999-12-17 21:43:04 +00:00
|
|
|
}
|
|
|
|
|
2001-05-07 15:58:47 +00:00
|
|
|
static void
|
|
|
|
progressive_updated_callback (GdkPixbufLoader* loader,
|
|
|
|
gint x, gint y, gint width, gint height,
|
|
|
|
gpointer data)
|
1999-12-17 21:43:04 +00:00
|
|
|
{
|
2001-05-07 15:58:47 +00:00
|
|
|
GtkWidget* image;
|
|
|
|
|
|
|
|
image = GTK_WIDGET (data);
|
|
|
|
|
|
|
|
/* We know the pixbuf inside the GtkImage has changed, but the image
|
|
|
|
* itself doesn't know this; so queue a redraw. If we wanted to be
|
|
|
|
* really efficient, we could use a drawing area or something
|
|
|
|
* instead of a GtkImage, so we could control the exact position of
|
|
|
|
* the pixbuf on the display, then we could queue a draw for only
|
|
|
|
* the updated area of the image.
|
|
|
|
*/
|
|
|
|
|
|
|
|
/* We only really need to redraw if the image's animation iterator
|
|
|
|
* is gdk_pixbuf_animation_iter_on_currently_loading_frame(), but
|
|
|
|
* who cares.
|
|
|
|
*/
|
|
|
|
|
|
|
|
gtk_widget_queue_draw (image);
|
1999-12-17 21:43:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static gint
|
2001-05-07 15:58:47 +00:00
|
|
|
progressive_timeout (gpointer data)
|
1999-12-17 21:43:04 +00:00
|
|
|
{
|
2001-05-07 15:58:47 +00:00
|
|
|
GtkWidget *image;
|
|
|
|
LoadContext *lc;
|
|
|
|
|
|
|
|
image = GTK_WIDGET (data);
|
|
|
|
lc = get_load_context (image);
|
|
|
|
|
|
|
|
/* This shows off fully-paranoid error handling, so looks scary.
|
|
|
|
* You could factor out the error handling code into a nice separate
|
|
|
|
* function to make things nicer.
|
|
|
|
*/
|
|
|
|
|
|
|
|
if (lc->image_stream)
|
|
|
|
{
|
|
|
|
size_t bytes_read;
|
|
|
|
guchar buf[256];
|
|
|
|
GError *error = NULL;
|
|
|
|
|
|
|
|
bytes_read = fread (buf, 1, 256, lc->image_stream);
|
|
|
|
|
|
|
|
if (ferror (lc->image_stream))
|
|
|
|
{
|
|
|
|
GtkWidget *dialog;
|
|
|
|
|
|
|
|
dialog = gtk_message_dialog_new (GTK_WINDOW (lc->window),
|
|
|
|
GTK_DIALOG_DESTROY_WITH_PARENT,
|
|
|
|
GTK_MESSAGE_ERROR,
|
|
|
|
GTK_BUTTONS_CLOSE,
|
|
|
|
"Failure reading image file 'alphatest.png': %s",
|
|
|
|
g_strerror (errno));
|
|
|
|
|
2001-10-20 23:39:32 +00:00
|
|
|
g_signal_connect (dialog, "response",
|
|
|
|
G_CALLBACK (gtk_widget_destroy), NULL);
|
2001-05-07 15:58:47 +00:00
|
|
|
|
|
|
|
fclose (lc->image_stream);
|
|
|
|
lc->image_stream = NULL;
|
|
|
|
|
|
|
|
gtk_widget_show (dialog);
|
|
|
|
|
|
|
|
lc->load_timeout = 0;
|
|
|
|
|
|
|
|
return FALSE; /* uninstall the timeout */
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!gdk_pixbuf_loader_write (lc->pixbuf_loader,
|
|
|
|
buf, bytes_read,
|
|
|
|
&error))
|
|
|
|
{
|
|
|
|
GtkWidget *dialog;
|
|
|
|
|
|
|
|
dialog = gtk_message_dialog_new (GTK_WINDOW (lc->window),
|
|
|
|
GTK_DIALOG_DESTROY_WITH_PARENT,
|
|
|
|
GTK_MESSAGE_ERROR,
|
|
|
|
GTK_BUTTONS_CLOSE,
|
|
|
|
"Failed to load image: %s",
|
|
|
|
error->message);
|
1999-12-17 21:43:04 +00:00
|
|
|
|
2001-05-07 15:58:47 +00:00
|
|
|
g_error_free (error);
|
|
|
|
|
2001-10-20 23:39:32 +00:00
|
|
|
g_signal_connect (dialog, "response",
|
|
|
|
G_CALLBACK (gtk_widget_destroy), NULL);
|
2001-05-07 15:58:47 +00:00
|
|
|
|
|
|
|
fclose (lc->image_stream);
|
|
|
|
lc->image_stream = NULL;
|
|
|
|
|
|
|
|
gtk_widget_show (dialog);
|
|
|
|
|
|
|
|
lc->load_timeout = 0;
|
|
|
|
|
|
|
|
return FALSE; /* uninstall the timeout */
|
|
|
|
}
|
|
|
|
|
|
|
|
if (feof (lc->image_stream))
|
|
|
|
{
|
|
|
|
fclose (lc->image_stream);
|
|
|
|
lc->image_stream = NULL;
|
|
|
|
|
|
|
|
/* Errors can happen on close, e.g. if the image
|
|
|
|
* file was truncated we'll know on close that
|
|
|
|
* it was incomplete.
|
|
|
|
*/
|
|
|
|
error = NULL;
|
|
|
|
if (!gdk_pixbuf_loader_close (lc->pixbuf_loader,
|
|
|
|
&error))
|
|
|
|
{
|
|
|
|
GtkWidget *dialog;
|
|
|
|
|
|
|
|
dialog = gtk_message_dialog_new (GTK_WINDOW (lc->window),
|
|
|
|
GTK_DIALOG_DESTROY_WITH_PARENT,
|
|
|
|
GTK_MESSAGE_ERROR,
|
|
|
|
GTK_BUTTONS_CLOSE,
|
|
|
|
"Failed to load image: %s",
|
|
|
|
error->message);
|
|
|
|
|
|
|
|
g_error_free (error);
|
|
|
|
|
2001-10-20 23:39:32 +00:00
|
|
|
g_signal_connect (dialog, "response",
|
|
|
|
G_CALLBACK (gtk_widget_destroy), NULL);
|
2001-05-07 15:58:47 +00:00
|
|
|
|
|
|
|
gtk_widget_show (dialog);
|
|
|
|
|
2002-09-29 21:24:24 +00:00
|
|
|
g_object_unref (lc->pixbuf_loader);
|
2001-05-07 15:58:47 +00:00
|
|
|
lc->pixbuf_loader = NULL;
|
|
|
|
|
|
|
|
lc->load_timeout = 0;
|
|
|
|
|
|
|
|
return FALSE; /* uninstall the timeout */
|
|
|
|
}
|
|
|
|
|
2002-09-29 21:24:24 +00:00
|
|
|
g_object_unref (lc->pixbuf_loader);
|
2001-05-07 15:58:47 +00:00
|
|
|
lc->pixbuf_loader = NULL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
lc->image_stream = fopen (lc->filename, "r");
|
|
|
|
|
|
|
|
if (lc->image_stream == NULL)
|
|
|
|
{
|
|
|
|
GtkWidget *dialog;
|
|
|
|
|
|
|
|
dialog = gtk_message_dialog_new (GTK_WINDOW (lc->window),
|
|
|
|
GTK_DIALOG_DESTROY_WITH_PARENT,
|
|
|
|
GTK_MESSAGE_ERROR,
|
|
|
|
GTK_BUTTONS_CLOSE,
|
|
|
|
"Unable to open image file '%s': %s",
|
|
|
|
lc->filename,
|
|
|
|
g_strerror (errno));
|
|
|
|
|
2001-10-20 23:39:32 +00:00
|
|
|
g_signal_connect (dialog, "response",
|
|
|
|
G_CALLBACK (gtk_widget_destroy), NULL);
|
2001-05-07 15:58:47 +00:00
|
|
|
|
|
|
|
gtk_widget_show (dialog);
|
|
|
|
|
|
|
|
lc->load_timeout = 0;
|
|
|
|
|
|
|
|
return FALSE; /* uninstall the timeout */
|
|
|
|
}
|
|
|
|
|
|
|
|
if (lc->pixbuf_loader)
|
|
|
|
{
|
|
|
|
gdk_pixbuf_loader_close (lc->pixbuf_loader, NULL);
|
2002-09-29 21:24:24 +00:00
|
|
|
g_object_unref (lc->pixbuf_loader);
|
2001-05-07 15:58:47 +00:00
|
|
|
lc->pixbuf_loader = NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
lc->pixbuf_loader = gdk_pixbuf_loader_new ();
|
|
|
|
|
2001-10-25 21:07:53 +00:00
|
|
|
g_signal_connect (lc->pixbuf_loader, "area_prepared",
|
2001-10-20 23:39:32 +00:00
|
|
|
G_CALLBACK (progressive_prepared_callback), image);
|
2001-10-25 21:07:53 +00:00
|
|
|
g_signal_connect (lc->pixbuf_loader, "area_updated",
|
2001-10-20 23:39:32 +00:00
|
|
|
G_CALLBACK (progressive_updated_callback), image);
|
2001-05-07 15:58:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* leave timeout installed */
|
|
|
|
return TRUE;
|
1999-12-17 21:43:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2001-05-07 15:58:47 +00:00
|
|
|
start_progressive_loading (GtkWidget *image)
|
1999-12-17 21:43:04 +00:00
|
|
|
{
|
2001-05-07 15:58:47 +00:00
|
|
|
LoadContext *lc;
|
|
|
|
|
|
|
|
lc = get_load_context (image);
|
|
|
|
|
|
|
|
/* This is obviously totally contrived (we slow down loading
|
|
|
|
* on purpose to show how incremental loading works).
|
|
|
|
* The real purpose of incremental loading is the case where
|
|
|
|
* you are reading data from a slow source such as the network.
|
|
|
|
* The timeout simply simulates a slow data source by inserting
|
|
|
|
* pauses in the reading process.
|
|
|
|
*/
|
2006-12-22 19:10:43 +00:00
|
|
|
lc->load_timeout = gdk_threads_add_timeout (100,
|
2001-05-07 15:58:47 +00:00
|
|
|
progressive_timeout,
|
|
|
|
image);
|
1999-12-17 21:43:04 +00:00
|
|
|
}
|
|
|
|
|
2001-05-07 15:58:47 +00:00
|
|
|
static GtkWidget *
|
|
|
|
do_image (const char *filename)
|
|
|
|
{
|
|
|
|
GtkWidget *frame;
|
|
|
|
GtkWidget *vbox;
|
|
|
|
GtkWidget *image;
|
|
|
|
GtkWidget *label;
|
|
|
|
GtkWidget *window;
|
|
|
|
gchar *str, *escaped;
|
|
|
|
LoadContext *lc;
|
|
|
|
|
|
|
|
window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
|
|
|
|
gtk_window_set_title (GTK_WINDOW (window), "Image Loading");
|
|
|
|
|
2010-10-31 17:07:20 +00:00
|
|
|
vbox = gtk_box_new (GTK_ORIENTATION_VERTICAL, 8);
|
2001-05-07 15:58:47 +00:00
|
|
|
gtk_container_add (GTK_CONTAINER (window), vbox);
|
|
|
|
|
|
|
|
label = gtk_label_new (NULL);
|
|
|
|
gtk_label_set_line_wrap (GTK_LABEL (label), TRUE);
|
|
|
|
escaped = g_markup_escape_text (filename, -1);
|
|
|
|
str = g_strdup_printf ("Progressively loading: <b>%s</b>", escaped);
|
|
|
|
gtk_label_set_markup (GTK_LABEL (label),
|
|
|
|
str);
|
|
|
|
g_free (escaped);
|
|
|
|
g_free (str);
|
2016-10-02 15:37:22 +00:00
|
|
|
|
2017-04-21 20:34:36 +00:00
|
|
|
gtk_box_pack_start (GTK_BOX (vbox), label);
|
2016-10-02 15:37:22 +00:00
|
|
|
|
2001-05-07 15:58:47 +00:00
|
|
|
frame = gtk_frame_new (NULL);
|
|
|
|
gtk_frame_set_shadow_type (GTK_FRAME (frame), GTK_SHADOW_IN);
|
2014-05-24 00:42:47 +00:00
|
|
|
gtk_widget_set_halign (frame, GTK_ALIGN_CENTER);
|
|
|
|
gtk_widget_set_valign (frame, GTK_ALIGN_CENTER);
|
2017-04-21 20:34:36 +00:00
|
|
|
gtk_box_pack_start (GTK_BOX (vbox), frame);
|
2001-05-07 15:58:47 +00:00
|
|
|
|
|
|
|
image = gtk_image_new_from_pixbuf (NULL);
|
|
|
|
gtk_container_add (GTK_CONTAINER (frame), image);
|
|
|
|
|
|
|
|
lc = get_load_context (image);
|
|
|
|
|
|
|
|
lc->window = window;
|
|
|
|
lc->filename = g_strdup (filename);
|
|
|
|
|
|
|
|
start_progressive_loading (image);
|
|
|
|
|
2002-09-29 21:24:24 +00:00
|
|
|
g_signal_connect (window, "destroy",
|
2002-09-03 23:51:36 +00:00
|
|
|
G_CALLBACK (gtk_main_quit), NULL);
|
|
|
|
|
2002-09-29 21:24:24 +00:00
|
|
|
g_signal_connect (window, "delete_event",
|
2002-09-03 23:51:36 +00:00
|
|
|
G_CALLBACK (gtk_main_quit), NULL);
|
|
|
|
|
2017-01-19 09:02:04 +00:00
|
|
|
gtk_widget_show (window);
|
2001-05-07 15:58:47 +00:00
|
|
|
|
|
|
|
return window;
|
|
|
|
}
|
1999-12-17 21:43:04 +00:00
|
|
|
|
|
|
|
static void
|
2001-05-07 15:58:47 +00:00
|
|
|
do_nonprogressive (const gchar *filename)
|
1999-12-17 21:43:04 +00:00
|
|
|
{
|
2001-05-07 15:58:47 +00:00
|
|
|
GtkWidget *frame;
|
|
|
|
GtkWidget *vbox;
|
|
|
|
GtkWidget *image;
|
|
|
|
GtkWidget *label;
|
|
|
|
GtkWidget *window;
|
|
|
|
gchar *str, *escaped;
|
|
|
|
|
|
|
|
window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
|
|
|
|
gtk_window_set_title (GTK_WINDOW (window), "Animation");
|
|
|
|
|
2010-10-31 17:07:20 +00:00
|
|
|
vbox = gtk_box_new (GTK_ORIENTATION_VERTICAL, 8);
|
2001-05-07 15:58:47 +00:00
|
|
|
gtk_container_add (GTK_CONTAINER (window), vbox);
|
|
|
|
|
|
|
|
label = gtk_label_new (NULL);
|
|
|
|
gtk_label_set_line_wrap (GTK_LABEL (label), TRUE);
|
|
|
|
escaped = g_markup_escape_text (filename, -1);
|
|
|
|
str = g_strdup_printf ("Loaded from file: <b>%s</b>", escaped);
|
|
|
|
gtk_label_set_markup (GTK_LABEL (label),
|
|
|
|
str);
|
|
|
|
g_free (escaped);
|
|
|
|
g_free (str);
|
2016-10-02 15:37:22 +00:00
|
|
|
|
2017-04-21 20:34:36 +00:00
|
|
|
gtk_box_pack_start (GTK_BOX (vbox), label);
|
2016-10-02 15:37:22 +00:00
|
|
|
|
2001-05-07 15:58:47 +00:00
|
|
|
frame = gtk_frame_new (NULL);
|
|
|
|
gtk_frame_set_shadow_type (GTK_FRAME (frame), GTK_SHADOW_IN);
|
2014-05-24 00:42:47 +00:00
|
|
|
gtk_widget_set_halign (frame, GTK_ALIGN_CENTER);
|
|
|
|
gtk_widget_set_valign (frame, GTK_ALIGN_CENTER);
|
2017-04-21 20:34:36 +00:00
|
|
|
gtk_box_pack_start (GTK_BOX (vbox), frame);
|
2001-05-07 15:58:47 +00:00
|
|
|
|
|
|
|
image = gtk_image_new_from_file (filename);
|
|
|
|
gtk_container_add (GTK_CONTAINER (frame), image);
|
|
|
|
|
2002-09-29 21:24:24 +00:00
|
|
|
g_signal_connect (window, "destroy",
|
2002-09-03 23:51:36 +00:00
|
|
|
G_CALLBACK (gtk_main_quit), NULL);
|
2016-10-02 15:37:22 +00:00
|
|
|
|
2002-09-29 21:24:24 +00:00
|
|
|
g_signal_connect (window, "delete_event",
|
2002-09-03 23:51:36 +00:00
|
|
|
G_CALLBACK (gtk_main_quit), NULL);
|
|
|
|
|
2017-01-19 09:02:04 +00:00
|
|
|
gtk_widget_show (window);
|
1999-12-17 21:43:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
int
|
2001-05-07 15:58:47 +00:00
|
|
|
main (int argc,
|
|
|
|
char **argv)
|
1999-12-17 21:43:04 +00:00
|
|
|
{
|
2001-05-07 15:58:47 +00:00
|
|
|
gint i;
|
|
|
|
|
2016-12-28 13:53:22 +00:00
|
|
|
gtk_init ();
|
2001-05-07 15:58:47 +00:00
|
|
|
|
|
|
|
i = 1;
|
|
|
|
while (i < argc)
|
|
|
|
{
|
|
|
|
do_image (argv[i]);
|
|
|
|
do_nonprogressive (argv[i]);
|
|
|
|
|
|
|
|
++i;
|
|
|
|
}
|
|
|
|
|
|
|
|
gtk_main ();
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
2000-10-18 18:42:54 +00:00
|
|
|
|