[gdk-pixbuf] Add pure gdk-pixbuf tests from GTK+
- From: Matthias Clasen <matthiasc src gnome org>
- To: commits-list gnome org
- Cc:
- Subject: [gdk-pixbuf] Add pure gdk-pixbuf tests from GTK+
- Date: Sat, 26 Jun 2010 05:08:50 +0000 (UTC)
commit c77574b63ca97c85267f609591192391a0013c77
Author: Matthias Clasen <mclasen redhat com>
Date: Sat Jun 26 01:07:27 2010 -0400
Add pure gdk-pixbuf tests from GTK+
Makefile.am | 2 +-
configure.ac | 1 +
tests/Makefile.am | 30 +++++
tests/pixbuf-lowmem.c | 249 ++++++++++++++++++++++++++++++++++++++
tests/pixbuf-random.c | 170 ++++++++++++++++++++++++++
tests/pixbuf-randomly-modified.c | 159 ++++++++++++++++++++++++
tests/pixbuf-read.c | 87 +++++++++++++
tests/pixbuf-threads.c | 118 ++++++++++++++++++
8 files changed, 815 insertions(+), 1 deletions(-)
---
diff --git a/Makefile.am b/Makefile.am
index 32eb764..9f846fb 100644
--- a/Makefile.am
+++ b/Makefile.am
@@ -1,6 +1,6 @@
include $(top_srcdir)/Makefile.decl
-SUBDIRS = gdk-pixbuf po docs
+SUBDIRS = gdk-pixbuf po docs tests
pkgconfigdir = $(libdir)/pkgconfig
pkgconfig_DATA = gdk-pixbuf-2.0.pc
diff --git a/configure.ac b/configure.ac
index 47554ae..70785c2 100644
--- a/configure.ac
+++ b/configure.ac
@@ -1007,6 +1007,7 @@ docs/reference/Makefile
docs/reference/gdk-pixbuf/Makefile
docs/reference/gdk-pixbuf/version.xml
po/Makefile.in
+tests/Makefile
])
AC_OUTPUT
diff --git a/tests/Makefile.am b/tests/Makefile.am
new file mode 100644
index 0000000..7129c42
--- /dev/null
+++ b/tests/Makefile.am
@@ -0,0 +1,30 @@
+include $(top_srcdir)/Makefile.decl
+
+INCLUDES = \
+ -I$(top_srcdir) \
+ -I$(top_srcdir)/gdk-pixbuf \
+ $(GDK_PIXBUF_DEBUG_FLAGS) \
+ $(GDK_PIXBUF_DEP_CFLAGS)
+
+DEPS = \
+ $(top_builddir)/gdk-pixbuf/libgdk_pixbuf-$(GDK_PIXBUF_API_VERSION).la
+
+LDADDS = \
+ $(top_builddir)/gdk-pixbuf/libgdk_pixbuf-$(GDK_PIXBUF_API_VERSION).la \
+ $(GDK_PIXBUF_DEP_LIBS)
+
+noinst_PROGRAMS = \
+ pixbuf-read \
+ pixbuf-lowmem \
+ pixbuf-randomly-modified \
+ pixbuf-random \
+ pixbuf-threads
+
+pixbuf_read_LDADD = $(LDADDS)
+pixbuf_lowmem_LDADD = $(LDADDS)
+pixbuf_randomly_modified_LDADD = $(LDADDS)
+pixbuf_random_LDADD = $(LDADDS)
+pixbuf_threads_LDADD = $(LDADDS) $(GLIB_LIBS)
+
+
+-include $(top_srcdir)/git.mk
diff --git a/tests/pixbuf-lowmem.c b/tests/pixbuf-lowmem.c
new file mode 100644
index 0000000..172c162
--- /dev/null
+++ b/tests/pixbuf-lowmem.c
@@ -0,0 +1,249 @@
+/* -*- Mode: C; c-basic-offset: 2; -*- */
+/* GdkPixbuf library - test loaders
+ *
+ * Copyright (C) 2001 Søren Sandmann (sandmann daimi au dk)
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program 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 General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA.
+ */
+
+#include "config.h"
+#include "gdk-pixbuf/gdk-pixbuf.h"
+#include <stdio.h>
+#include <stdlib.h>
+#include <time.h>
+#include <string.h>
+
+#define PRETEND_MEM_SIZE (16 * 1024 * 1024)
+#define REMAINING_MEM_SIZE 100000
+
+
+static int current_allocation = 0;
+static int max_allocation = 0;
+
+#define HEADER_SPACE sizeof(void*)
+
+static gpointer
+record_bytes (gpointer mem, gsize bytes)
+{
+ if (mem == NULL ||
+ (current_allocation + bytes) > max_allocation)
+ {
+ if (mem)
+ free (mem);
+
+ return NULL;
+ }
+
+ *(void **)mem = GINT_TO_POINTER (bytes);
+
+ g_assert (GPOINTER_TO_INT (*(void**)mem) == bytes);
+
+ g_assert (current_allocation >= 0);
+ current_allocation += bytes;
+ g_assert (current_allocation >= 0);
+
+ g_assert ( mem == (void*) ((((char*)mem) + HEADER_SPACE) - HEADER_SPACE) );
+ return ((char*)mem) + HEADER_SPACE;
+}
+
+static gpointer
+limited_try_malloc (gsize n_bytes)
+{
+ return record_bytes (malloc (n_bytes + HEADER_SPACE), n_bytes);
+}
+
+static gpointer
+limited_malloc (gsize n_bytes)
+{
+ return limited_try_malloc (n_bytes);
+}
+
+static gpointer
+limited_calloc (gsize n_blocks,
+ gsize n_block_bytes)
+{
+ int bytes = n_blocks * n_block_bytes + HEADER_SPACE;
+ gpointer mem = malloc (bytes);
+ memset (mem, 0, bytes);
+ return record_bytes (mem, n_blocks * n_block_bytes);
+}
+
+static void
+limited_free (gpointer mem)
+{
+ gpointer real = ((char*)mem) - HEADER_SPACE;
+
+ g_assert (current_allocation >= 0);
+ current_allocation -= GPOINTER_TO_INT (*(void**)real);
+ g_assert (current_allocation >= 0);
+
+ free (real);
+}
+
+static gpointer
+limited_try_realloc (gpointer mem,
+ gsize n_bytes)
+{
+ if (mem == NULL)
+ {
+ return limited_try_malloc (n_bytes);
+ }
+ else
+ {
+ gpointer real;
+
+ g_assert (mem);
+
+ real = ((char*)mem) - HEADER_SPACE;
+
+ g_assert (current_allocation >= 0);
+ current_allocation -= GPOINTER_TO_INT (*(void**)real);
+ g_assert (current_allocation >= 0);
+
+ return record_bytes (realloc (real, n_bytes + HEADER_SPACE), n_bytes);
+ }
+}
+
+static gpointer
+limited_realloc (gpointer mem,
+ gsize n_bytes)
+{
+ return limited_try_realloc (mem, n_bytes);
+}
+
+static GMemVTable limited_table = {
+ limited_malloc,
+ limited_realloc,
+ limited_free,
+ limited_calloc,
+ limited_try_malloc,
+ limited_try_realloc
+};
+
+static void
+mem_test (const gchar *bytes, gsize len)
+{
+ gboolean did_fail = FALSE;
+ GError *err = NULL;
+ GdkPixbufLoader *loader;
+ GList *loaders = NULL;
+ GList *i;
+
+ do {
+ loader = gdk_pixbuf_loader_new ();
+ gdk_pixbuf_loader_write (loader, (guchar *) bytes, len, &err);
+ if (err)
+ {
+ g_error_free (err);
+ err = NULL;
+ did_fail = TRUE;
+ }
+ gdk_pixbuf_loader_close (loader, NULL);
+ if (err)
+ {
+ g_error_free (err);
+ err = NULL;
+ did_fail = TRUE;
+ }
+ loaders = g_list_prepend (loaders, loader);
+ } while (!did_fail);
+
+ for (i = loaders; i != NULL; i = i->next)
+ g_object_unref (i->data);
+ g_list_free (loaders);
+}
+
+static void
+almost_exhaust_memory (void)
+{
+ gpointer x = g_malloc (REMAINING_MEM_SIZE);
+ while (g_try_malloc (REMAINING_MEM_SIZE / 10))
+ ;
+ g_free (x);
+}
+
+static void
+usage (void)
+{
+ g_print ("usage: pixbuf-lowmem <pretend_memory_size> <files>\n");
+ exit (EXIT_FAILURE);
+}
+
+int
+main (int argc, char **argv)
+{
+ int i;
+ char *endptr;
+
+ if (argc <= 2)
+ usage();
+
+ max_allocation = strtol (argv[1], &endptr, 10);
+ if (endptr == argv[1])
+ usage();
+
+ /* Set a malloc which emulates low mem */
+ g_mem_set_vtable (&limited_table);
+
+ g_type_init ();
+ g_log_set_always_fatal (G_LOG_LEVEL_WARNING | G_LOG_LEVEL_ERROR | G_LOG_LEVEL_CRITICAL);
+
+ /* memory tests */
+
+ /* How do the loaders behave when memory is low?
+ It depends on the state the above tests left the
+ memory in.
+
+ - Sometimes the png loader tries to report an
+ "out of memory", but then g_strdup_printf() calls
+ g_malloc(), which fails.
+
+ - There are unchecked realloc()s inside libtiff, which means it
+ will never work with low memory, unless something drastic is
+ done, like allocating a lot of memory upfront and release it
+ before entering libtiff. Also, some TIFFReadRGBAImage calls
+ returns successfully, even though they have called the error
+ handler with an 'out of memory' message.
+ */
+
+ almost_exhaust_memory ();
+
+ g_print ("Allocated %dK of %dK, %dK free during tests\n",
+ current_allocation / 1024, max_allocation / 1024,
+ (max_allocation - current_allocation) / 1024);
+
+ for (i = 2; i < argc; ++i)
+ {
+ gchar *contents;
+ gsize size;
+ GError *err = NULL;
+
+ if (!g_file_get_contents (argv[i], &contents, &size, &err))
+ {
+ g_print ("couldn't read %s: %s\n", argv[i], err->message);
+ exit (EXIT_FAILURE);
+ }
+ else
+ {
+ g_print ("%-40s memory ", argv[i]);
+ fflush (stdout);
+ mem_test (contents, size);
+ g_print ("\tpassed\n");
+ g_free (contents);
+ }
+ }
+
+ return 0;
+}
diff --git a/tests/pixbuf-random.c b/tests/pixbuf-random.c
new file mode 100644
index 0000000..e94b631
--- /dev/null
+++ b/tests/pixbuf-random.c
@@ -0,0 +1,170 @@
+/* -*- Mode: C; c-basic-offset: 2; -*- */
+/* GdkPixbuf library - assault loaders with random data
+ *
+ * Copyright (C) 2001 Søren Sandmann (sandmann daimi au dk)
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program 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 General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA.
+ */
+
+#include "config.h"
+#include "gdk-pixbuf/gdk-pixbuf.h"
+#include <stdio.h>
+#include <stdlib.h>
+#include <time.h>
+#include <string.h>
+
+static void
+assault (const guchar *header, gsize header_size, int n_images)
+{
+ FILE *f;
+ enum { N_CHARACTERS = 10000 };
+
+ int j;
+
+ for (j = 0; j < n_images; ++j)
+ {
+ GError *err = NULL;
+ int i;
+ GdkPixbufLoader *loader;
+
+ f = fopen ("pixbuf-random-image", "w");
+ if (!f)
+ {
+ perror ("fopen");
+ exit (EXIT_FAILURE);
+ }
+
+ loader = gdk_pixbuf_loader_new ();
+
+ gdk_pixbuf_loader_write (loader, header, header_size, &err);
+ if (err)
+ {
+ g_error_free (err);
+ continue;
+ }
+
+ for (i = 0; i < N_CHARACTERS; ++i)
+ {
+ int r = g_random_int ();
+
+ fwrite (&r, 1, sizeof (r), f);
+ if (ferror (f))
+ {
+ perror ("fwrite");
+ exit (EXIT_FAILURE);
+ }
+
+ gdk_pixbuf_loader_write (loader, (guchar *)&r, sizeof (r), &err);
+ if (err)
+ {
+ g_error_free (err);
+ err = NULL;
+ break;
+ }
+ }
+
+ fclose (f);
+
+ gdk_pixbuf_loader_close (loader, &err);
+ if (err)
+ {
+ g_error_free (err);
+ err = NULL;
+ }
+
+ g_object_unref (loader);
+ }
+}
+
+static void
+write_seed (int seed)
+{
+ FILE *f;
+ /* write this so you can reproduce failed tests */
+ f = fopen ("pixbuf-random-seed", "w");
+ if (!f)
+ {
+ perror ("fopen");
+ exit (EXIT_FAILURE);
+ }
+ if (fprintf (f, "%d\n", seed) < 0)
+ {
+ perror ("fprintf");
+ exit (EXIT_FAILURE);
+ }
+ if (fclose (f) < 0)
+ {
+ perror ("fclose");
+ exit (EXIT_FAILURE);
+ }
+ g_print ("seed: %d\n", seed);
+}
+
+int
+main (int argc, char **argv)
+{
+ int seed;
+
+ if (argc > 1)
+ seed = atoi (argv[1]);
+ else
+ {
+ seed = time (NULL);
+ write_seed (seed);
+ }
+ g_print ("the last tested image is saved to the file \"pixbuf-random-image\"\n\n");
+
+ g_type_init ();
+ g_log_set_always_fatal (G_LOG_LEVEL_WARNING | G_LOG_LEVEL_ERROR | G_LOG_LEVEL_CRITICAL);
+
+ g_random_set_seed (seed);
+
+#define GIF_HEADER 'G', 'I', 'F', '8', '9', 'a'
+#define PNG_HEADER 0x89, 'P', 'N', 'G', 0x0d, 0x0a, 0x1a, 0x0a
+#define TIFF1_HEADER 'M', 'M', 0x00, 0x2a
+#define TIFF2_HEADER 'I', 'I', 0x2a, 0x00
+#define JPEG_HEADER 0xFF, 0xd8
+#define PNM_HEADER 'P', '6'
+#define XBM_HEADER '#', 'd', 'e', 'f', 'i', 'n', 'e', ' '
+#define BMP_HEADER 'B', 'M'
+#define XPM_HEADER '/', '*', ' ', 'X', 'P', 'M', ' ', '*', '/'
+#define RAS_HEADER 0x59, 0xA6, 0x6A, 0x95
+
+#define TEST_RANDOM(header, n_img) \
+do { \
+ static guchar h[] = { header }; \
+ g_print (#header); \
+ fflush (stdout); \
+ assault (h, sizeof (h), n_img); \
+ g_print ("\t\tpassed\n"); \
+} while (0)
+
+ for (;;)
+ {
+ TEST_RANDOM (GIF_HEADER, 150);
+ TEST_RANDOM (PNG_HEADER, 110);
+ TEST_RANDOM (JPEG_HEADER, 800);
+ TEST_RANDOM (TIFF1_HEADER, 150);
+ TEST_RANDOM (TIFF2_HEADER, 150);
+ TEST_RANDOM (PNM_HEADER, 150);
+ TEST_RANDOM (XBM_HEADER, 150);
+ TEST_RANDOM (BMP_HEADER, 150);
+ TEST_RANDOM (XPM_HEADER, 150);
+ TEST_RANDOM (RAS_HEADER, 300);
+ g_print ("===========================\n");
+ }
+
+ return 0;
+}
diff --git a/tests/pixbuf-randomly-modified.c b/tests/pixbuf-randomly-modified.c
new file mode 100644
index 0000000..fb9eac7
--- /dev/null
+++ b/tests/pixbuf-randomly-modified.c
@@ -0,0 +1,159 @@
+/* -*- Mode: C; c-basic-offset: 2; -*- */
+/* GdkPixbuf library - test loaders
+ *
+ * Copyright (C) 2001 Søren Sandmann (sandmann daimi au dk)
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program 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 General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA.
+ */
+
+#include "config.h"
+#include "gdk-pixbuf/gdk-pixbuf.h"
+#include "glib.h"
+#include <stdio.h>
+#include <stdlib.h>
+#include <time.h>
+#include <string.h>
+
+static void
+disaster (const char *what)
+{
+ perror (what);
+ exit (EXIT_FAILURE);
+}
+
+static void
+randomly_modify (const gchar *image, guint size)
+{
+ int i;
+
+ guchar *img_copy = g_malloc (size);
+ g_memmove (img_copy, image, size);
+
+ for (i = 0; i < size / 4; i++)
+ {
+ FILE *f;
+ GdkPixbufLoader *loader;
+
+ guint index = g_random_int_range (0, size);
+ guchar byte = g_random_int_range (0, 256);
+
+ img_copy[index] = byte;
+ f = fopen ("pixbuf-randomly-modified-image", "w");
+ if (!f)
+ disaster ("fopen");
+ fwrite (img_copy, size, 1, f);
+ if (ferror (f))
+ disaster ("fwrite");
+ fclose (f);
+
+ loader = gdk_pixbuf_loader_new ();
+ gdk_pixbuf_loader_write (loader, img_copy, size, NULL);
+ gdk_pixbuf_loader_close (loader, NULL);
+ g_object_unref (loader);
+ }
+ g_free (img_copy);
+}
+
+static void
+write_seed (int seed)
+{
+ FILE *f;
+ /* write this so you can reproduce failed tests */
+ f = fopen ("pixbuf-randomly-modified-seed", "w");
+
+ if (!f)
+ disaster ("fopen");
+
+ if (fprintf (f, "%d\n", seed) < 0)
+ disaster ("fprintf");
+
+ if (fclose (f) < 0)
+ disaster ("fclose");
+
+ g_print ("seed: %d\n", seed);
+}
+
+static void
+usage (void)
+{
+ g_print ("usage: pixbuf-randomly-modified [-s <seed>] <files> ... \n");
+ exit (EXIT_FAILURE);
+}
+
+int
+main (int argc, char **argv)
+{
+ int seed, i;
+ gboolean got_seed = FALSE;
+ GPtrArray *files = g_ptr_array_new ();
+
+ if (argc == 1)
+ usage ();
+
+ seed = time (NULL);
+
+ for (i = 1; i < argc; ++i)
+ {
+ if (strncmp (argv[i], "-s", 2) == 0)
+ {
+ if (strlen (argv[i]) > 2)
+ usage();
+ if (i+1 < argc)
+ {
+ seed = atoi (argv[i+1]);
+ got_seed = TRUE;
+ ++i;
+ }
+ else
+ usage();
+ }
+ else
+ g_ptr_array_add (files, strdup (argv[i]));
+ }
+
+ if (!got_seed)
+ write_seed (seed);
+
+ g_random_set_seed (seed);
+
+ g_print ("the last tested image is saved to pixbuf-randomly-modified-image\n");
+
+ g_type_init ();
+ g_log_set_always_fatal (G_LOG_LEVEL_WARNING | G_LOG_LEVEL_ERROR | G_LOG_LEVEL_CRITICAL);
+
+ for (;;)
+ for (i = 0; i < files->len; ++i)
+ {
+ gchar *contents;
+ gsize size;
+ GError *err = NULL;
+
+ fflush (stdout);
+ if (!g_file_get_contents (files->pdata[i], &contents, &size, &err))
+ {
+ g_print ("%s: error: %s\n", (char *)files->pdata[i], err->message);
+ }
+ else
+ {
+ g_print ("%s\t\t", (char *)files->pdata[i]);
+ randomly_modify (contents, size);
+ g_print ("done\n");
+
+ g_free (contents);
+ }
+ }
+
+ return 0;
+}
diff --git a/tests/pixbuf-read.c b/tests/pixbuf-read.c
new file mode 100644
index 0000000..a65af3f
--- /dev/null
+++ b/tests/pixbuf-read.c
@@ -0,0 +1,87 @@
+/* -*- Mode: C; c-basic-offset: 2; -*- */
+/* GdkPixbuf library - test loaders
+ *
+ * Copyright (C) 2001 Søren Sandmann (sandmann daimi au dk)
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program 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 General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA.
+ */
+
+#include "config.h"
+#include "gdk-pixbuf/gdk-pixbuf.h"
+#include <stdio.h>
+#include <stdlib.h>
+
+static gboolean
+test_loader (const guchar *bytes, gsize len, GError **err)
+{
+ GdkPixbufLoader *loader;
+
+ loader = gdk_pixbuf_loader_new ();
+ gdk_pixbuf_loader_write (loader, bytes, len, err);
+ if (*err)
+ return FALSE;
+
+ gdk_pixbuf_loader_close (loader, err);
+ if (*err)
+ return FALSE;
+
+ return TRUE;
+}
+
+static void
+usage (void)
+{
+ g_print ("usage: pixbuf-read <files>\n");
+ exit (EXIT_FAILURE);
+}
+
+int
+main (int argc, char **argv)
+{
+ int i;
+
+ g_type_init ();
+ g_log_set_always_fatal (G_LOG_LEVEL_WARNING | G_LOG_LEVEL_ERROR | G_LOG_LEVEL_CRITICAL);
+
+ if (argc == 1)
+ usage();
+
+ for (i = 1; i < argc; ++i)
+ {
+ gchar *contents;
+ gsize size;
+ GError *err = NULL;
+
+ g_print ("%s\t\t", argv[i]);
+ fflush (stdout);
+ if (!g_file_get_contents (argv[i], &contents, &size, &err))
+ {
+ fprintf (stderr, "%s: error: %s\n", argv[i], err->message);
+ }
+ else
+ {
+ err = NULL;
+
+ if (test_loader ((guchar *) contents, size, &err))
+ g_print ("success\n");
+ else
+ g_print ("error: %s\n", err->message);
+
+ g_free (contents);
+ }
+ }
+
+ return 0;
+}
diff --git a/tests/pixbuf-threads.c b/tests/pixbuf-threads.c
new file mode 100644
index 0000000..267efe9
--- /dev/null
+++ b/tests/pixbuf-threads.c
@@ -0,0 +1,118 @@
+/* -*- Mode: C; c-basic-offset: 2; -*- */
+/* GdkPixbuf library - test loaders
+ *
+ * Copyright (C) 2004 Matthias Clasen <mclasen redhat com>
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program 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 General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA.
+ */
+
+#include "config.h"
+#include "gdk-pixbuf/gdk-pixbuf.h"
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+
+static gboolean verbose = FALSE;
+
+static void
+load_image (gpointer data,
+ gpointer user_data)
+{
+ gchar *filename = data;
+ FILE *file;
+ int nbytes;
+ guchar buf[1024];
+ size_t bufsize = 1024;
+ GdkPixbufLoader *loader;
+ GError *error = NULL;
+ GThread *self;
+
+ self = g_thread_self ();
+ loader = gdk_pixbuf_loader_new ();
+
+ file = fopen (filename, "r");
+ g_assert (file);
+
+ if (verbose) g_print ("%p start image %s\n", self, filename);
+ while (!feof (file))
+ {
+ nbytes = fread (buf, 1, bufsize, file);
+ if (!gdk_pixbuf_loader_write (loader, buf, nbytes, &error))
+ {
+ g_warning ("Error writing %s to loader: %s", filename, error->message);
+ g_error_free (error);
+ error = NULL;
+ break;
+ }
+ if (verbose) g_print ("%p read %d bytes\n", self, nbytes);
+
+ g_thread_yield ();
+ }
+
+ fclose (file);
+
+ if (verbose) g_print ("%p finish image %s\n", self, filename);
+
+ if (!gdk_pixbuf_loader_close (loader, &error))
+ {
+ g_warning ("Error closing loader for %s: %s", filename, error->message);
+ g_error_free (error);
+ }
+
+ g_object_unref (loader);
+}
+
+static void
+usage (void)
+{
+ g_print ("usage: pixbuf-threads [--verbose] <files>\n");
+ exit (EXIT_FAILURE);
+}
+
+int
+main (int argc, char **argv)
+{
+ int i, start;
+ GThreadPool *pool;
+
+ g_type_init ();
+
+ if (!g_thread_supported ())
+ g_thread_init (NULL);
+
+ g_log_set_always_fatal (G_LOG_LEVEL_WARNING | G_LOG_LEVEL_ERROR | G_LOG_LEVEL_CRITICAL);
+
+ if (argc == 1)
+ usage();
+
+ start = 1;
+ if (strcmp (argv[1], "--verbose") == 0)
+ {
+ verbose = TRUE;
+ start = 2;
+ }
+
+ pool = g_thread_pool_new (load_image, NULL, 20, FALSE, NULL);
+
+ i = start;
+ while (1) {
+ i++;
+ if (i == argc)
+ i = start;
+ g_thread_pool_push (pool, argv[i], NULL);
+ }
+
+ return 0;
+}
[
Date Prev][
Date Next] [
Thread Prev][
Thread Next]
[
Thread Index]
[
Date Index]
[
Author Index]