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