Add powerbox hook
[gtk-with-powerbox.git] / tests / pixbuf-threads.c
blob267efe9b370c668a3eed4bb17e8e62e6d9f0ddd0
1 /* -*- Mode: C; c-basic-offset: 2; -*- */
2 /* GdkPixbuf library - test loaders
4 * Copyright (C) 2004 Matthias Clasen <mclasen@redhat.com>
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA.
21 #include "config.h"
22 #include "gdk-pixbuf/gdk-pixbuf.h"
23 #include <stdio.h>
24 #include <stdlib.h>
25 #include <string.h>
27 static gboolean verbose = FALSE;
29 static void
30 load_image (gpointer data,
31 gpointer user_data)
33 gchar *filename = data;
34 FILE *file;
35 int nbytes;
36 guchar buf[1024];
37 size_t bufsize = 1024;
38 GdkPixbufLoader *loader;
39 GError *error = NULL;
40 GThread *self;
42 self = g_thread_self ();
43 loader = gdk_pixbuf_loader_new ();
45 file = fopen (filename, "r");
46 g_assert (file);
48 if (verbose) g_print ("%p start image %s\n", self, filename);
49 while (!feof (file))
51 nbytes = fread (buf, 1, bufsize, file);
52 if (!gdk_pixbuf_loader_write (loader, buf, nbytes, &error))
54 g_warning ("Error writing %s to loader: %s", filename, error->message);
55 g_error_free (error);
56 error = NULL;
57 break;
59 if (verbose) g_print ("%p read %d bytes\n", self, nbytes);
61 g_thread_yield ();
64 fclose (file);
66 if (verbose) g_print ("%p finish image %s\n", self, filename);
68 if (!gdk_pixbuf_loader_close (loader, &error))
70 g_warning ("Error closing loader for %s: %s", filename, error->message);
71 g_error_free (error);
74 g_object_unref (loader);
77 static void
78 usage (void)
80 g_print ("usage: pixbuf-threads [--verbose] <files>\n");
81 exit (EXIT_FAILURE);
84 int
85 main (int argc, char **argv)
87 int i, start;
88 GThreadPool *pool;
90 g_type_init ();
92 if (!g_thread_supported ())
93 g_thread_init (NULL);
95 g_log_set_always_fatal (G_LOG_LEVEL_WARNING | G_LOG_LEVEL_ERROR | G_LOG_LEVEL_CRITICAL);
97 if (argc == 1)
98 usage();
100 start = 1;
101 if (strcmp (argv[1], "--verbose") == 0)
103 verbose = TRUE;
104 start = 2;
107 pool = g_thread_pool_new (load_image, NULL, 20, FALSE, NULL);
109 i = start;
110 while (1) {
111 i++;
112 if (i == argc)
113 i = start;
114 g_thread_pool_push (pool, argv[i], NULL);
117 return 0;