]> Pileus Git - ~andy/gtk/blob - tests/pixbuf-threads.c
Make GTK+ use an external gdk-pixbuf
[~andy/gtk] / tests / pixbuf-threads.c
1 /* -*- Mode: C; c-basic-offset: 2; -*- */
2 /* GdkPixbuf library - test loaders
3  *
4  * Copyright (C) 2004 Matthias Clasen <mclasen@redhat.com>
5  *
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.
10  *
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.
15  *
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.
19  */
20
21 #include "config.h"
22 #include "gdk-pixbuf/gdk-pixbuf.h"
23 #include <stdio.h>
24 #include <stdlib.h>
25 #include <string.h>
26
27 static gboolean verbose = FALSE;
28
29 static void
30 load_image (gpointer  data, 
31             gpointer user_data)
32 {
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;
41
42   self = g_thread_self ();
43   loader = gdk_pixbuf_loader_new ();
44
45   file = fopen (filename, "r");
46   g_assert (file);
47
48   if (verbose) g_print ("%p start image %s\n", self, filename);
49   while (!feof (file)) 
50     {
51       nbytes = fread (buf, 1, bufsize, file);
52       if (!gdk_pixbuf_loader_write (loader, buf, nbytes, &error)) 
53         {
54           g_warning ("Error writing %s to loader: %s", filename, error->message);
55           g_error_free (error);
56           error = NULL;
57           break;
58         }
59       if (verbose) g_print ("%p read %d bytes\n", self, nbytes);
60
61       g_thread_yield ();      
62     }
63
64   fclose (file);
65
66   if (verbose) g_print ("%p finish image %s\n", self, filename);
67
68   if (!gdk_pixbuf_loader_close (loader, &error)) 
69     {
70       g_warning ("Error closing loader for %s: %s", filename, error->message);
71       g_error_free (error);
72     }
73
74   g_object_unref (loader);
75 }
76
77 static void
78 usage (void)
79 {
80   g_print ("usage: pixbuf-threads [--verbose] <files>\n");
81   exit (EXIT_FAILURE);
82 }
83
84 int
85 main (int argc, char **argv)
86 {
87   int i, start;
88   GThreadPool *pool;
89   
90   g_type_init ();
91
92   if (!g_thread_supported ())
93     g_thread_init (NULL);
94
95   g_log_set_always_fatal (G_LOG_LEVEL_WARNING | G_LOG_LEVEL_ERROR | G_LOG_LEVEL_CRITICAL);
96
97   if (argc == 1)
98     usage();
99
100   start = 1;
101   if (strcmp (argv[1], "--verbose") == 0)
102     {
103       verbose = TRUE;
104       start = 2;
105     }
106   
107   pool = g_thread_pool_new (load_image, NULL, 20, FALSE, NULL);
108
109   i = start;
110   while (1) {
111     i++;
112     if (i == argc)
113       i = start;
114     g_thread_pool_push (pool, argv[i], NULL);
115   }
116   
117   return 0;
118 }