]> Pileus Git - ~andy/gtk/blob - gdk-pixbuf/gdk-pixbuf-loader.c
Welcome aboard, gdk-pixbuf.
[~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 "gdk-pixbuf-private.h"
29 #include "gdk-pixbuf-loader.h"
30 #include "gdk-pixbuf-io.h"
31
32 #include "gtksignal.h"
33
34 \f
35
36 enum {
37         AREA_UPDATED,
38         AREA_PREPARED,
39         FRAME_DONE,
40         ANIMATION_DONE,
41         CLOSED,
42         LAST_SIGNAL
43 };
44
45 static GtkObjectClass *parent_class;
46
47 static void gdk_pixbuf_loader_class_init    (GdkPixbufLoaderClass   *klass);
48 static void gdk_pixbuf_loader_init          (GdkPixbufLoader        *loader);
49 static void gdk_pixbuf_loader_destroy       (GtkObject              *loader);
50 static void gdk_pixbuf_loader_finalize      (GObject                *loader);
51
52 static guint pixbuf_loader_signals[LAST_SIGNAL] = { 0 };
53
54 \f
55
56 /* Internal data */
57
58 #define LOADER_HEADER_SIZE 128
59
60 typedef struct {
61         GdkPixbuf *pixbuf;
62         GdkPixbufAnimation *animation;
63         gboolean closed;
64         guchar header_buf[LOADER_HEADER_SIZE];
65         gint header_buf_offset;
66         GdkPixbufModule *image_module;
67         gpointer context;
68 } GdkPixbufLoaderPrivate;
69
70 \f
71
72 /* our marshaller */
73 typedef void (* GtkSignal_NONE__INT_INT_INT_INT) (GtkObject *object,
74                                                   gint arg1, gint arg2, gint arg3, gint arg4,
75                                                   gpointer user_data);
76 static void
77 gtk_marshal_NONE__INT_INT_INT_INT (GtkObject *object, GtkSignalFunc func, gpointer func_data,
78                                    GtkArg * args)
79 {
80         GtkSignal_NONE__INT_INT_INT_INT rfunc;
81
82         rfunc = (GtkSignal_NONE__INT_INT_INT_INT) func;
83         (*rfunc) (object,
84                   GTK_VALUE_INT (args[0]),
85                   GTK_VALUE_INT (args[1]),
86                   GTK_VALUE_INT (args[2]),
87                   GTK_VALUE_INT (args[3]),
88                   func_data);
89 }
90
91 \f
92
93 /**
94  * gdk_pixbuf_loader_get_type:
95  * @void:
96  *
97  * Registers the #GdkPixubfLoader class if necessary, and returns the type ID
98  * associated to it.
99  *
100  * Return value: The type ID of the #GdkPixbufLoader class.
101  **/
102 GtkType
103 gdk_pixbuf_loader_get_type (void)
104 {
105         static GtkType loader_type = 0;
106
107         if (!loader_type) {
108                 static const GtkTypeInfo loader_info = {
109                         "GdkPixbufLoader",
110                         sizeof (GdkPixbufLoader),
111                         sizeof (GdkPixbufLoaderClass),
112                         (GtkClassInitFunc) gdk_pixbuf_loader_class_init,
113                         (GtkObjectInitFunc) gdk_pixbuf_loader_init,
114                         /* reserved_1 */ NULL,
115                         /* reserved_2 */ NULL,
116                         (GtkClassInitFunc) NULL,
117                 };
118
119                 loader_type = gtk_type_unique (GTK_TYPE_OBJECT, &loader_info);
120         }
121
122         return loader_type;
123 }
124
125 static void
126 gdk_pixbuf_loader_class_init (GdkPixbufLoaderClass *class)
127 {
128         GObjectClass *gobject_class;
129         GtkObjectClass *object_class;
130
131         object_class = (GtkObjectClass *) class;
132         gobject_class = (GObjectClass *) class;
133
134         parent_class = gtk_type_class (gtk_object_get_type ());
135
136         pixbuf_loader_signals[AREA_PREPARED] =
137                 gtk_signal_new ("area_prepared",
138                                 GTK_RUN_LAST,
139                                 GTK_CLASS_TYPE (object_class),
140                                 GTK_SIGNAL_OFFSET (GdkPixbufLoaderClass, area_prepared),
141                                 gtk_marshal_NONE__NONE,
142                                 GTK_TYPE_NONE, 0);
143
144         pixbuf_loader_signals[AREA_UPDATED] =
145                 gtk_signal_new ("area_updated",
146                                 GTK_RUN_LAST,
147                                 GTK_CLASS_TYPE (object_class),
148                                 GTK_SIGNAL_OFFSET (GdkPixbufLoaderClass, area_updated),
149                                 gtk_marshal_NONE__INT_INT_INT_INT,
150                                 GTK_TYPE_NONE, 4,
151                                 GTK_TYPE_INT,
152                                 GTK_TYPE_INT,
153                                 GTK_TYPE_INT,
154                                 GTK_TYPE_INT);
155
156         pixbuf_loader_signals[FRAME_DONE] =
157                 gtk_signal_new ("frame_done",
158                                 GTK_RUN_LAST,
159                                 GTK_CLASS_TYPE (object_class),
160                                 GTK_SIGNAL_OFFSET (GdkPixbufLoaderClass, frame_done),
161                                 gtk_marshal_NONE__POINTER,
162                                 GTK_TYPE_NONE, 1,
163                                 GTK_TYPE_POINTER);
164
165         pixbuf_loader_signals[ANIMATION_DONE] =
166                 gtk_signal_new ("animation_done",
167                                 GTK_RUN_LAST,
168                                 GTK_CLASS_TYPE (object_class),
169                                 GTK_SIGNAL_OFFSET (GdkPixbufLoaderClass, animation_done),
170                                 gtk_marshal_NONE__NONE,
171                                 GTK_TYPE_NONE, 0);
172
173         pixbuf_loader_signals[CLOSED] =
174                 gtk_signal_new ("closed",
175                                 GTK_RUN_LAST,
176                                 GTK_CLASS_TYPE (object_class),
177                                 GTK_SIGNAL_OFFSET (GdkPixbufLoaderClass, closed),
178                                 gtk_marshal_NONE__NONE,
179                                 GTK_TYPE_NONE, 0);
180
181         gtk_object_class_add_signals (object_class, pixbuf_loader_signals, LAST_SIGNAL);
182
183         object_class->destroy = gdk_pixbuf_loader_destroy;
184         gobject_class->finalize = gdk_pixbuf_loader_finalize;
185 }
186
187 static void
188 gdk_pixbuf_loader_init (GdkPixbufLoader *loader)
189 {
190         GdkPixbufLoaderPrivate *priv;
191
192         priv = g_new0 (GdkPixbufLoaderPrivate, 1);
193         loader->private = priv;
194 }
195
196 static void
197 gdk_pixbuf_loader_destroy (GtkObject *object)
198 {
199         GdkPixbufLoader *loader;
200         GdkPixbufLoaderPrivate *priv = NULL;
201
202         g_return_if_fail (object != NULL);
203         g_return_if_fail (GDK_IS_PIXBUF_LOADER (object));
204
205         loader = GDK_PIXBUF_LOADER (object);
206         priv = loader->private;
207
208         if (!priv->closed)
209                 gdk_pixbuf_loader_close (loader);
210
211         if (priv->animation)
212                 gdk_pixbuf_animation_unref (priv->animation);
213         if (priv->pixbuf)
214                 gdk_pixbuf_unref (priv->pixbuf);
215
216         if (GTK_OBJECT_CLASS (parent_class)->destroy)
217                 (* GTK_OBJECT_CLASS (parent_class)->destroy) (object);
218 }
219
220 static void
221 gdk_pixbuf_loader_finalize (GObject *object)
222 {
223         GdkPixbufLoader *loader;
224         GdkPixbufLoaderPrivate *priv = NULL;
225
226         loader = GDK_PIXBUF_LOADER (object);
227         priv = loader->private;
228
229         g_free (priv);
230
231         if (G_OBJECT_CLASS (parent_class)->finalize)
232                 (* G_OBJECT_CLASS (parent_class)->finalize) (object);
233 }
234
235 static void
236 gdk_pixbuf_loader_prepare (GdkPixbuf *pixbuf, gpointer loader)
237 {
238         GdkPixbufLoaderPrivate *priv = NULL;
239
240         priv = GDK_PIXBUF_LOADER (loader)->private;
241         gdk_pixbuf_ref (pixbuf);
242         g_assert (priv->pixbuf == NULL);
243
244         priv->pixbuf = pixbuf;
245         gtk_signal_emit (GTK_OBJECT (loader), pixbuf_loader_signals[AREA_PREPARED]);
246 }
247
248 static void
249 gdk_pixbuf_loader_update (GdkPixbuf *pixbuf, guint x, guint y, guint width, guint height, gpointer loader)
250 {
251         GdkPixbufLoaderPrivate *priv = NULL;
252
253         priv = GDK_PIXBUF_LOADER (loader)->private;
254
255         gtk_signal_emit (GTK_OBJECT (loader),
256                          pixbuf_loader_signals[AREA_UPDATED],
257                          x, y,
258                          /* sanity check in here.  Defend against an errant loader */
259                          MIN (width, gdk_pixbuf_get_width (priv->pixbuf)),
260                          MIN (height, gdk_pixbuf_get_height (priv->pixbuf)));
261 }
262
263 static void
264 gdk_pixbuf_loader_frame_done (GdkPixbufFrame *frame, gpointer loader)
265 {
266         GdkPixbufLoaderPrivate *priv = NULL;
267
268         priv = GDK_PIXBUF_LOADER (loader)->private;
269
270         priv->pixbuf = NULL;
271
272         if (priv->animation == NULL) {
273                 priv->animation = g_new0 (GdkPixbufAnimation, 1);
274                 priv->animation->n_frames = 0;
275                 priv->animation->ref_count = 1;
276                 priv->animation->width  = gdk_pixbuf_get_width  (frame->pixbuf) + frame->x_offset;
277                 priv->animation->height = gdk_pixbuf_get_height (frame->pixbuf) + frame->y_offset;
278         } else {
279                 int w, h;
280
281                 /* update bbox size */
282                 w = gdk_pixbuf_get_width (frame->pixbuf) + frame->x_offset;
283                 h = gdk_pixbuf_get_height (frame->pixbuf) + frame->y_offset;
284
285                 if (w > priv->animation->width) {
286                         priv->animation->width = w;
287                 }
288                 if (h > priv->animation->height) {
289                         priv->animation->height = h;
290                 }
291         }
292
293         priv->animation->frames = g_list_append (priv->animation->frames, frame);
294         priv->animation->n_frames ++;
295         gtk_signal_emit (GTK_OBJECT (loader),
296                          pixbuf_loader_signals[FRAME_DONE],
297                          frame);
298 }
299
300 static void
301 gdk_pixbuf_loader_animation_done (GdkPixbuf *pixbuf, gpointer loader)
302 {
303         GdkPixbufLoaderPrivate *priv = NULL;
304         GdkPixbufFrame    *frame;
305         GList *current = NULL;
306         gint h, w;
307
308         priv = GDK_PIXBUF_LOADER (loader)->private;
309         priv->pixbuf = NULL;
310
311         current = gdk_pixbuf_animation_get_frames (priv->animation);
312
313         while (current) {
314                 frame = (GdkPixbufFrame *) current->data;
315
316                 /* update bbox size */
317                 w = gdk_pixbuf_get_width (frame->pixbuf) + frame->x_offset;
318                 h = gdk_pixbuf_get_height (frame->pixbuf) + frame->y_offset;
319
320                 if (w > priv->animation->width) {
321                         priv->animation->width = w;
322                 }
323                 if (h > priv->animation->height) {
324                         priv->animation->height = h;
325                 }
326                 current = current->next;
327         }
328
329         gtk_signal_emit (GTK_OBJECT (loader),
330                          pixbuf_loader_signals[ANIMATION_DONE]);
331 }
332
333 \f
334
335 /**
336  * gdk_pixbuf_loader_new:
337  *
338  * Creates a new pixbuf loader object.
339  *
340  * Return value: A newly-created pixbuf loader.
341  **/
342 GdkPixbufLoader *
343 gdk_pixbuf_loader_new (void)
344 {
345         return gtk_type_new (gdk_pixbuf_loader_get_type ());
346 }
347
348 static int
349 gdk_pixbuf_loader_load_module(GdkPixbufLoader *loader)
350 {
351         GdkPixbufLoaderPrivate *priv = loader->private;
352
353         priv->image_module = gdk_pixbuf_get_module (priv->header_buf, priv->header_buf_offset);
354
355         if (priv->image_module == NULL)
356                 return 0;
357
358         if (priv->image_module->module == NULL)
359                 gdk_pixbuf_load_module (priv->image_module);
360
361         if (priv->image_module->module == NULL)
362                 return 0;
363
364         if ((priv->image_module->begin_load == NULL) ||
365             (priv->image_module->stop_load == NULL) ||
366             (priv->image_module->load_increment == NULL)) {
367                 g_warning ("module %s does not support incremental loading.\n",
368                            priv->image_module->module_name);
369                 return 0;
370         }
371
372         priv->context = (*priv->image_module->begin_load) (gdk_pixbuf_loader_prepare,
373                                                            gdk_pixbuf_loader_update,
374                                                            gdk_pixbuf_loader_frame_done,
375                                                            gdk_pixbuf_loader_animation_done,
376                                                            loader);
377
378         if (priv->context == NULL) {
379                 g_warning("Failed to begin progressive load");
380                 return 0;
381         }
382
383         if( (* priv->image_module->load_increment) (priv->context, priv->header_buf, priv->header_buf_offset) )
384                 return priv->header_buf_offset;
385
386         return 0;
387 }
388
389 static int
390 gdk_pixbuf_loader_eat_header_write (GdkPixbufLoader *loader, const guchar *buf, size_t count)
391 {
392         int nbytes;
393         GdkPixbufLoaderPrivate *priv = loader->private;
394
395         nbytes = MIN(LOADER_HEADER_SIZE - priv->header_buf_offset, count);
396         memcpy (priv->header_buf + priv->header_buf_offset, buf, nbytes);
397
398         priv->header_buf_offset += nbytes;
399
400         if(priv->header_buf_offset >= LOADER_HEADER_SIZE) {
401                 if (gdk_pixbuf_loader_load_module(loader) == 0)
402                         return 0;
403         }
404         return nbytes;
405 }
406
407 /**
408  * gdk_pixbuf_loader_write:
409  * @loader: A pixbuf loader.
410  * @buf: Pointer to image data.
411  * @count: Length of the @buf buffer in bytes.
412  *
413  * This will cause a pixbuf loader to parse the next @count bytes of an image.
414  * It will return TRUE if the data was loaded successfully, and FALSE if an
415  * error occurred.  In the latter case, the loader will be closed, and will not
416  * accept further writes.
417  *
418  * Return value: #TRUE if the write was successful, or #FALSE if the loader
419  * cannot parse the buffer.
420  **/
421 gboolean
422 gdk_pixbuf_loader_write (GdkPixbufLoader *loader, const guchar *buf, size_t count)
423 {
424         GdkPixbufLoaderPrivate *priv;
425
426         g_return_val_if_fail (loader != NULL, FALSE);
427         g_return_val_if_fail (GDK_IS_PIXBUF_LOADER (loader), FALSE);
428
429         g_return_val_if_fail (buf != NULL, FALSE);
430         g_return_val_if_fail (count >= 0, FALSE);
431
432         priv = loader->private;
433
434         /* we expect it's not to be closed */
435         g_return_val_if_fail (priv->closed == FALSE, FALSE);
436
437         if (priv->image_module == NULL) {
438                 int eaten;
439
440                 eaten = gdk_pixbuf_loader_eat_header_write(loader, buf, count);
441                 if (eaten <= 0)
442                         return FALSE;
443
444                 count -= eaten;
445                 buf += eaten;
446         }
447
448         if (count > 0 && priv->image_module->load_increment)
449                 return (* priv->image_module->load_increment) (priv->context, buf, count);
450
451         return TRUE;
452 }
453
454 /**
455  * gdk_pixbuf_loader_get_pixbuf:
456  * @loader: A pixbuf loader.
457  *
458  * Queries the GdkPixbuf that a pixbuf loader is currently creating.  In general
459  * it only makes sense to call this function afer the "area_prepared" signal has
460  * been emitted by the loader; this means that enough data has been read to know
461  * the size of the image that will be allocated.  If the loader has not received
462  * enough data via gdk_pixbuf_loader_write(), then this function returns NULL.
463  * The returned pixbuf will be the same in all future calls to the loader, so
464  * simply calling gdk_pixbuf_ref() should be sufficient to continue using it.
465  *
466  * Return value: The GdkPixbuf that the loader is creating, or NULL if not
467  * enough data has been read to determine how to create the image buffer.
468  **/
469 GdkPixbuf *
470 gdk_pixbuf_loader_get_pixbuf (GdkPixbufLoader *loader)
471 {
472         GdkPixbufLoaderPrivate *priv;
473
474         g_return_val_if_fail (loader != NULL, NULL);
475         g_return_val_if_fail (GDK_IS_PIXBUF_LOADER (loader), NULL);
476
477         priv = loader->private;
478
479         return priv->pixbuf;
480 }
481
482 /**
483  * gdk_pixbuf_loader_get_animation:
484  * @loader: A pixbuf loader
485  *
486  * Queries the GdkPixbufAnimation that a pixbuf loader is currently creating.
487  * In general it only makes sense to call this function afer the "area_prepared"
488  * signal has been emitted by the loader.  If the image is not an animation,
489  * then it will return NULL.
490  *
491  * Return value: The GdkPixbufAnimation that the loader is loading, or NULL if
492  not enough data has been read to determine the information.
493  **/
494 GdkPixbufAnimation *
495 gdk_pixbuf_loader_get_animation (GdkPixbufLoader *loader)
496 {
497         GdkPixbufLoaderPrivate *priv;
498
499         g_return_val_if_fail (loader != NULL, NULL);
500         g_return_val_if_fail (GDK_IS_PIXBUF_LOADER (loader), NULL);
501
502         priv = loader->private;
503
504         return priv->animation;
505 }
506
507 /**
508  * gdk_pixbuf_loader_close:
509  * @loader: A pixbuf loader.
510  *
511  * Informs a pixbuf loader that no further writes with gdk_pixbuf_load_write()
512  * will occur, so that it can free its internal loading structures.
513  **/
514 void
515 gdk_pixbuf_loader_close (GdkPixbufLoader *loader)
516 {
517         GdkPixbufLoaderPrivate *priv;
518
519         g_return_if_fail (loader != NULL);
520         g_return_if_fail (GDK_IS_PIXBUF_LOADER (loader));
521
522         priv = loader->private;
523
524         /* we expect it's not closed */
525         g_return_if_fail (priv->closed == FALSE);
526
527         /* We have less the 128 bytes in the image.  Flush it, and keep going. */
528         if (priv->image_module == NULL)
529                 gdk_pixbuf_loader_load_module (loader);
530
531         if (priv->image_module && priv->image_module->stop_load)
532                 (* priv->image_module->stop_load) (priv->context);
533
534         priv->closed = TRUE;
535
536         gtk_signal_emit (GTK_OBJECT (loader),
537                          pixbuf_loader_signals[CLOSED]);
538 }
539