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