]> Pileus Git - ~andy/gtk/blob - gdk-pixbuf/gdk-pixbuf-loader.c
Fixed bug sent directly to me. Basically, we weren't checking a failure
[~andy/gtk] / gdk-pixbuf / gdk-pixbuf-loader.c
1 /* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
2
3 /* GdkPixbuf library - Main header file
4  *
5  * Copyright (C) 1999 The Free Software Foundation
6  *
7  * Authors: Mark Crichton <crichton@gimp.org>
8  *          Miguel de Icaza <miguel@gnu.org>
9  *          Federico Mena-Quintero <federico@gimp.org>
10  *          Jonathan Blandford <jrb@redhat.com>
11  *
12  * This library is free software; you can redistribute it and/or
13  * modify it under the terms of the GNU Library General Public
14  * License as published by the Free Software Foundation; either
15  * version 2 of the License, or (at your option) any later version.
16  *
17  * This library is distributed in the hope that it will be useful,
18  * but WITHOUT ANY WARRANTY; without even the implied warranty of
19  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
20  * Library General Public License for more details.
21  *
22  * You should have received a copy of the GNU Library General Public
23  * License along with this library; if not, write to the
24  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
25  * Boston, MA 02111-1307, USA.
26  */
27
28 #include <config.h>
29 #include <gtk/gtksignal.h>
30 #include "gdk-pixbuf-loader.h"
31 #include "gdk-pixbuf-io.h"
32
33 \f
34
35 enum {
36         AREA_UPDATED,
37         AREA_PREPARED,
38         CLOSED,
39         LAST_SIGNAL
40 };
41
42 static GtkObjectClass *parent_class;
43
44 static void gdk_pixbuf_loader_class_init    (GdkPixbufLoaderClass   *klass);
45 static void gdk_pixbuf_loader_init          (GdkPixbufLoader        *loader);
46 static void gdk_pixbuf_loader_destroy       (GtkObject              *loader);
47 static void gdk_pixbuf_loader_finalize      (GtkObject              *loader);
48
49 static guint pixbuf_loader_signals[LAST_SIGNAL] = { 0 };
50
51 \f
52
53 /* Internal data */
54
55 #define LOADER_HEADER_SIZE 128
56
57 typedef struct {
58         GdkPixbuf *pixbuf;
59         GdkPixbufAnimation *animation;
60         gboolean closed;
61         guchar header_buf[LOADER_HEADER_SIZE];
62         gint header_buf_offset;
63         GdkPixbufModule *image_module;
64         gpointer context;
65 } GdkPixbufLoaderPrivate;
66
67 \f
68
69 /* our marshaller */
70 typedef void (* GtkSignal_NONE__INT_INT_INT_INT) (GtkObject *object,
71                                                   gint arg1, gint arg2, gint arg3, gint arg4,
72                                                   gpointer user_data);
73 static void
74 gtk_marshal_NONE__INT_INT_INT_INT (GtkObject *object, GtkSignalFunc func, gpointer func_data,
75                                    GtkArg * args)
76 {
77         GtkSignal_NONE__INT_INT_INT_INT rfunc;
78
79         rfunc = (GtkSignal_NONE__INT_INT_INT_INT) func;
80         (*rfunc) (object,
81                   GTK_VALUE_INT (args[0]),
82                   GTK_VALUE_INT (args[1]),
83                   GTK_VALUE_INT (args[2]),
84                   GTK_VALUE_INT (args[3]),
85                   func_data);
86 }
87
88 \f
89
90 /**
91  * gdk_pixbuf_loader_get_type:
92  * @void: 
93  * 
94  * Registers the #GdkPixubfLoader class if necessary, and returns the type ID
95  * associated to it.
96  * 
97  * Return value: The type ID of the #GdkPixbufLoader class.
98  **/
99 GtkType
100 gdk_pixbuf_loader_get_type (void)
101 {
102         static GtkType loader_type = 0;
103
104         if (!loader_type) {
105                 static const GtkTypeInfo loader_info = {
106                         "GdkPixbufLoader",
107                         sizeof (GdkPixbufLoader),
108                         sizeof (GdkPixbufLoaderClass),
109                         (GtkClassInitFunc) gdk_pixbuf_loader_class_init,
110                         (GtkObjectInitFunc) gdk_pixbuf_loader_init,
111                         /* reserved_1 */ NULL,
112                         /* reserved_2 */ NULL,
113                         (GtkClassInitFunc) NULL,
114                 };
115
116                 loader_type = gtk_type_unique (GTK_TYPE_OBJECT, &loader_info);
117         }
118
119         return loader_type;
120 }
121
122 static void
123 gdk_pixbuf_loader_class_init (GdkPixbufLoaderClass *class)
124 {
125         GtkObjectClass *object_class;
126
127         object_class = (GtkObjectClass *) class;
128
129         parent_class = gtk_type_class (gtk_object_get_type ());
130
131         pixbuf_loader_signals[AREA_PREPARED] =
132                 gtk_signal_new ("area_prepared",
133                                 GTK_RUN_LAST,
134                                 parent_class->type,
135                                 GTK_SIGNAL_OFFSET (GdkPixbufLoaderClass, area_prepared),
136                                 gtk_marshal_NONE__NONE,
137                                 GTK_TYPE_NONE, 0);
138
139         pixbuf_loader_signals[AREA_UPDATED] =
140                 gtk_signal_new ("area_updated",
141                                 GTK_RUN_LAST,
142                                 parent_class->type,
143                                 GTK_SIGNAL_OFFSET (GdkPixbufLoaderClass, area_updated),
144                                 gtk_marshal_NONE__INT_INT_INT_INT,
145                                 GTK_TYPE_NONE, 4,
146                                 GTK_TYPE_INT,
147                                 GTK_TYPE_INT,
148                                 GTK_TYPE_INT,
149                                 GTK_TYPE_INT);
150
151         pixbuf_loader_signals[CLOSED] =
152                 gtk_signal_new ("closed",
153                                 GTK_RUN_LAST,
154                                 parent_class->type,
155                                 GTK_SIGNAL_OFFSET (GdkPixbufLoaderClass, closed),
156                                 gtk_marshal_NONE__NONE,
157                                 GTK_TYPE_NONE, 0);
158
159         gtk_object_class_add_signals (object_class, pixbuf_loader_signals, LAST_SIGNAL);
160
161         object_class->destroy = gdk_pixbuf_loader_destroy;
162         object_class->finalize = gdk_pixbuf_loader_finalize;
163 }
164
165 static void
166 gdk_pixbuf_loader_init (GdkPixbufLoader *loader)
167 {
168         GdkPixbufLoaderPrivate *priv;
169
170         priv = g_new0 (GdkPixbufLoaderPrivate, 1);
171         loader->private = priv;
172 }
173
174 static void
175 gdk_pixbuf_loader_destroy (GtkObject *object)
176 {
177         GdkPixbufLoader *loader;
178         GdkPixbufLoaderPrivate *priv = NULL;
179
180         g_return_if_fail (object != NULL);
181         g_return_if_fail (GDK_IS_PIXBUF_LOADER (object));
182
183         loader = GDK_PIXBUF_LOADER (object);
184         priv = loader->private;
185
186         if (!priv->closed)
187                 gdk_pixbuf_loader_close (loader);
188
189         if (priv->pixbuf)
190                 gdk_pixbuf_unref (priv->pixbuf);
191
192         if (GTK_OBJECT_CLASS (parent_class)->destroy)
193                 (* GTK_OBJECT_CLASS (parent_class)->destroy) (object);
194 }
195
196 static void
197 gdk_pixbuf_loader_finalize (GtkObject *object)
198 {
199         GdkPixbufLoader *loader;
200         GdkPixbufLoaderPrivate *priv = NULL;
201
202         loader = GDK_PIXBUF_LOADER (object);
203         priv = loader->private;
204
205         g_free (priv);
206
207         if (GTK_OBJECT_CLASS (parent_class)->finalize)
208                 (* GTK_OBJECT_CLASS (parent_class)->finalize) (object);
209 }
210
211 static void
212 gdk_pixbuf_loader_prepare (GdkPixbuf *pixbuf, gpointer loader)
213 {
214         GdkPixbufLoaderPrivate *priv = NULL;
215
216         priv = GDK_PIXBUF_LOADER (loader)->private;
217         gdk_pixbuf_ref (pixbuf);
218         g_assert (priv->pixbuf == NULL);
219
220         priv->pixbuf = pixbuf;
221         gtk_signal_emit (GTK_OBJECT (loader), pixbuf_loader_signals[AREA_PREPARED]);
222 }
223
224 static void
225 gdk_pixbuf_loader_update (GdkPixbuf *pixbuf, guint x, guint y, guint width, guint height, gpointer loader)
226 {
227         GdkPixbufLoaderPrivate *priv = NULL;
228
229         priv = GDK_PIXBUF_LOADER (loader)->private;
230
231         gtk_signal_emit (GTK_OBJECT (loader),
232                          pixbuf_loader_signals[AREA_UPDATED],
233                          x, y,
234                          /* sanity check in here.  Defend against an errant loader */
235                          MIN (width, gdk_pixbuf_get_width (priv->pixbuf)),
236                          MIN (height, gdk_pixbuf_get_height (priv->pixbuf)));
237 }
238
239 \f
240
241 /**
242  * gdk_pixbuf_loader_new:
243  * @void: 
244  * 
245  * Creates a new pixbuf loader object.
246  * 
247  * Return value: A newly-created pixbuf loader.
248  **/
249 GdkPixbufLoader *
250 gdk_pixbuf_loader_new (void)
251 {
252         return gtk_type_new (gdk_pixbuf_loader_get_type ());
253 }
254
255 static int
256 gdk_pixbuf_loader_load_module(GdkPixbufLoader *loader)
257 {
258         GdkPixbufLoaderPrivate *priv = loader->private;
259
260         priv->image_module = gdk_pixbuf_get_module (priv->header_buf, priv->header_buf_offset);
261
262         if (priv->image_module == NULL)
263                 return 0;
264
265         if (priv->image_module->module == NULL)
266                 gdk_pixbuf_load_module (priv->image_module);
267
268         if (priv->image_module->module == NULL)
269                 return 0;
270
271         if ((priv->image_module->begin_load == NULL) ||
272             (priv->image_module->stop_load == NULL) ||
273             (priv->image_module->load_increment == NULL)) {
274                 g_warning ("module %s does not support incremental loading.\n",
275                            priv->image_module->module_name);
276                 return 0;
277         }
278
279         priv->context = (*priv->image_module->begin_load) (gdk_pixbuf_loader_prepare, gdk_pixbuf_loader_update, NULL, NULL, loader);
280
281         if (priv->context == NULL) {
282                 g_warning("Failed to begin progressive load");
283                 return 0;
284         }
285
286         if( (* priv->image_module->load_increment) (priv->context, priv->header_buf, priv->header_buf_offset) )
287                 return priv->header_buf_offset;
288  
289         return 0;
290 }
291
292 static int
293 gdk_pixbuf_loader_eat_header_write (GdkPixbufLoader *loader, const guchar *buf, size_t count)
294 {
295         int nbytes;
296         GdkPixbufLoaderPrivate *priv = loader->private;
297
298         nbytes = MIN(LOADER_HEADER_SIZE - priv->header_buf_offset, count);
299         memcpy (priv->header_buf + priv->header_buf_offset, buf, nbytes);
300             
301         priv->header_buf_offset += nbytes;
302             
303         if(priv->header_buf_offset >= LOADER_HEADER_SIZE) {
304                 if (gdk_pixbuf_loader_load_module(loader) == 0)
305                         return 0;
306         }
307         return nbytes;
308 }
309
310 /**
311  * gdk_pixbuf_loader_write:
312  * @loader: A pixbuf loader.
313  * @buf: Pointer to image data.
314  * @count: Length of the @buf buffer in bytes.
315  *
316  * This will cause a pixbuf loader to parse the next @count bytes of an image.
317  * It will return TRUE if the data was loaded successfully, and FALSE if an
318  * error occurred.  In the latter case, the loader will be closed, and will not
319  * accept further writes.
320  *
321  * Return value: #TRUE if the write was successful, or #FALSE if the loader
322  * cannot parse the buffer.
323  **/
324 gboolean
325 gdk_pixbuf_loader_write (GdkPixbufLoader *loader, const guchar *buf, size_t count)
326 {
327         GdkPixbufLoaderPrivate *priv;
328
329         g_return_val_if_fail (loader != NULL, FALSE);
330         g_return_val_if_fail (GDK_IS_PIXBUF_LOADER (loader), FALSE);
331
332         g_return_val_if_fail (buf != NULL, FALSE);
333         g_return_val_if_fail (count >= 0, FALSE);
334
335         priv = loader->private;
336
337         /* we expect it's not to be closed */
338         g_return_val_if_fail (priv->closed == FALSE, FALSE);
339
340         if (priv->image_module == NULL) {
341                 int eaten;
342
343                 eaten = gdk_pixbuf_loader_eat_header_write(loader, buf, count);
344                 if (eaten <= 0)
345                         return FALSE;
346
347                 count -= eaten;
348                 buf += eaten;
349         }
350
351         if (count > 0 && priv->image_module->load_increment)
352                 return (* priv->image_module->load_increment) (priv->context, buf, count);
353
354         return TRUE;
355 }
356
357 /**
358  * gdk_pixbuf_loader_get_pixbuf:
359  * @loader: A pixbuf loader.
360  *
361  * Queries the GdkPixbuf that a pixbuf loader is currently creating.  In general
362  * it only makes sense to call this function afer the "area_prepared" signal has
363  * been emitted by the loader; this means that enough data has been read to know
364  * the size of the image that will be allocated.  If the loader has not received
365  * enough data via gdk_pixbuf_loader_write(), then this function returns NULL.
366  * The returned pixbuf will be the same in all future calls to the loader, so
367  * simply calling gdk_pixbuf_ref() should be sufficient to continue using it.
368  *
369  * Return value: The GdkPixbuf that the loader is creating, or NULL if not
370  * enough data has been read to determine how to create the image buffer.
371  **/
372 GdkPixbuf *
373 gdk_pixbuf_loader_get_pixbuf (GdkPixbufLoader *loader)
374 {
375         GdkPixbufLoaderPrivate *priv;
376
377         g_return_val_if_fail (loader != NULL, NULL);
378         g_return_val_if_fail (GDK_IS_PIXBUF_LOADER (loader), NULL);
379
380         priv = loader->private;
381
382         return priv->pixbuf;
383 }
384
385 /**
386  * gdk_pixbuf_loader_get_animation:
387  * @loader: A pixbuf loader
388  * 
389  * Queries the GdkPixbufAnimation that a pixbuf loader is currently creating.
390  * In general it only makes sense to call this function afer the "area_prepared"
391  * signal has been emitted by the loader.
392  * 
393  * Return value: The GdkPixbufAnimation that the loader is loading, or NULL if
394  not enough data has been read to determine the information.
395  **/
396 GdkPixbufAnimation *
397 gdk_pixbuf_loader_get_animation (GdkPixbufLoader *loader)
398 {
399         GdkPixbufLoaderPrivate *priv;
400
401         g_return_val_if_fail (loader != NULL, NULL);
402         g_return_val_if_fail (GDK_IS_PIXBUF_LOADER (loader), NULL);
403
404         priv = loader->private;
405
406         return priv->animation;
407 }
408
409 /**
410  * gdk_pixbuf_loader_close:
411  * @loader: A pixbuf loader.
412  *
413  * Informs a pixbuf loader that no further writes with gdk_pixbuf_load_write()
414  * will occur, so that it can free its internal loading structures.
415  **/
416 void
417 gdk_pixbuf_loader_close (GdkPixbufLoader *loader)
418 {
419         GdkPixbufLoaderPrivate *priv;
420
421         g_return_if_fail (loader != NULL);
422         g_return_if_fail (GDK_IS_PIXBUF_LOADER (loader));
423
424         priv = loader->private;
425
426         /* we expect it's not closed */
427         g_return_if_fail (priv->closed == FALSE);
428
429         /* We have less the 128 bytes in the image.  Flush it, and keep going. */
430         if (priv->image_module == NULL)
431                 gdk_pixbuf_loader_load_module (loader);
432
433         if (priv->image_module && priv->image_module->stop_load)
434                 (* priv->image_module->stop_load) (priv->context);
435
436         priv->closed = TRUE;
437
438         gtk_signal_emit (GTK_OBJECT (loader),
439                          pixbuf_loader_signals[CLOSED]);
440 }