]> Pileus Git - ~andy/gtk/blob - gdk-pixbuf/gdk-pixbuf-loader.c
include <string.h>.
[~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 <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 /**
341  * gdk_pixbuf_loader_new:
342  *
343  * Creates a new pixbuf loader object.
344  *
345  * Return value: A newly-created pixbuf loader.
346  **/
347 GdkPixbufLoader *
348 gdk_pixbuf_loader_new (void)
349 {
350   return g_object_new (GDK_TYPE_PIXBUF_LOADER, NULL);
351 }
352
353 static gint
354 gdk_pixbuf_loader_load_module (GdkPixbufLoader *loader)
355 {
356   GdkPixbufLoaderPrivate *priv = loader->private;
357   
358   priv->image_module = gdk_pixbuf_get_module (priv->header_buf, priv->header_buf_offset);
359   
360   if (priv->image_module == NULL)
361     return 0;
362   
363   if (priv->image_module->module == NULL)
364     gdk_pixbuf_load_module (priv->image_module);
365   
366   if (priv->image_module->module == NULL)
367     return 0;
368   
369   if ((priv->image_module->begin_load == NULL) ||
370       (priv->image_module->stop_load == NULL) ||
371       (priv->image_module->load_increment == NULL))
372     {
373       g_warning (G_STRLOC ": module %s does not support incremental loading.\n",
374                  priv->image_module->module_name);
375       return 0;
376     }
377   
378   priv->context = priv->image_module->begin_load (gdk_pixbuf_loader_prepare,
379                                                   gdk_pixbuf_loader_update,
380                                                   gdk_pixbuf_loader_frame_done,
381                                                   gdk_pixbuf_loader_animation_done,
382                                                   loader);
383   
384   if (priv->context == NULL)
385     {
386       g_warning (G_STRLOC ": Failed to begin progressive load");
387       return 0;
388     }
389   
390   if (priv->image_module->load_increment (priv->context, priv->header_buf, priv->header_buf_offset))
391     return priv->header_buf_offset;
392   
393   return 0;
394 }
395
396 static int
397 gdk_pixbuf_loader_eat_header_write (GdkPixbufLoader *loader,
398                                     const guchar    *buf,
399                                     gsize            count)
400 {
401   gint n_bytes;
402   GdkPixbufLoaderPrivate *priv = loader->private;
403   
404   n_bytes = MIN(LOADER_HEADER_SIZE - priv->header_buf_offset, count);
405   memcpy (priv->header_buf + priv->header_buf_offset, buf, n_bytes);
406   
407   priv->header_buf_offset += n_bytes;
408   
409   if (priv->header_buf_offset >= LOADER_HEADER_SIZE)
410     {
411       if (gdk_pixbuf_loader_load_module (loader) == 0)
412         return 0;
413     }
414   
415   return n_bytes;
416 }
417
418 /**
419  * gdk_pixbuf_loader_write:
420  * @loader: A pixbuf loader.
421  * @buf: Pointer to image data.
422  * @count: Length of the @buf buffer in bytes.
423  *
424  * This will cause a pixbuf loader to parse the next @count bytes of an image.
425  * It will return TRUE if the data was loaded successfully, and FALSE if an
426  * error occurred.  In the latter case, the loader will be closed, and will not
427  * accept further writes.
428  *
429  * Return value: #TRUE if the write was successful, or #FALSE if the loader
430  * cannot parse the buffer.
431  **/
432 gboolean
433 gdk_pixbuf_loader_write (GdkPixbufLoader *loader,
434                          const guchar    *buf,
435                          gsize            count)
436 {
437   GdkPixbufLoaderPrivate *priv;
438   
439   g_return_val_if_fail (loader != NULL, FALSE);
440   g_return_val_if_fail (GDK_IS_PIXBUF_LOADER (loader), FALSE);
441   
442   g_return_val_if_fail (buf != NULL, FALSE);
443   g_return_val_if_fail (count >= 0, FALSE);
444   
445   priv = loader->private;
446   
447   /* we expect it's not to be closed */
448   g_return_val_if_fail (priv->closed == FALSE, FALSE);
449   
450   if (priv->image_module == NULL)
451     {
452       gint eaten;
453       
454       eaten = gdk_pixbuf_loader_eat_header_write(loader, buf, count);
455       if (eaten <= 0)
456         return FALSE;
457       
458       count -= eaten;
459       buf += eaten;
460     }
461   
462   if (count > 0 && priv->image_module->load_increment)
463     return priv->image_module->load_increment (priv->context, buf, count);
464   
465   return TRUE;
466 }
467
468 /**
469  * gdk_pixbuf_loader_get_pixbuf:
470  * @loader: A pixbuf loader.
471  *
472  * Queries the GdkPixbuf that a pixbuf loader is currently creating.  In general
473  * it only makes sense to call this function afer the "area_prepared" signal has
474  * been emitted by the loader; this means that enough data has been read to know
475  * the size of the image that will be allocated.  If the loader has not received
476  * enough data via gdk_pixbuf_loader_write(), then this function returns NULL.
477  * The returned pixbuf will be the same in all future calls to the loader, so
478  * simply calling gdk_pixbuf_ref() should be sufficient to continue using it.
479  *
480  * Return value: The GdkPixbuf that the loader is creating, or NULL if not
481  * enough data has been read to determine how to create the image buffer.
482  **/
483 GdkPixbuf *
484 gdk_pixbuf_loader_get_pixbuf (GdkPixbufLoader *loader)
485 {
486   GdkPixbufLoaderPrivate *priv;
487   
488   g_return_val_if_fail (loader != NULL, NULL);
489   g_return_val_if_fail (GDK_IS_PIXBUF_LOADER (loader), NULL);
490   
491   priv = loader->private;
492   
493   return priv->pixbuf;
494 }
495
496 /**
497  * gdk_pixbuf_loader_get_animation:
498  * @loader: A pixbuf loader
499  *
500  * Queries the GdkPixbufAnimation that a pixbuf loader is currently creating.
501  * In general it only makes sense to call this function afer the "area_prepared"
502  * signal has been emitted by the loader.  If the image is not an animation,
503  * then it will return NULL.
504  *
505  * Return value: The GdkPixbufAnimation that the loader is loading, or NULL if
506  not enough data has been read to determine the information.
507 **/
508 GdkPixbufAnimation *
509 gdk_pixbuf_loader_get_animation (GdkPixbufLoader *loader)
510 {
511   GdkPixbufLoaderPrivate *priv;
512   
513   g_return_val_if_fail (loader != NULL, NULL);
514   g_return_val_if_fail (GDK_IS_PIXBUF_LOADER (loader), NULL);
515   
516   priv = loader->private;
517   
518   return priv->animation;
519 }
520
521 /**
522  * gdk_pixbuf_loader_close:
523  * @loader: A pixbuf loader.
524  *
525  * Informs a pixbuf loader that no further writes with gdk_pixbuf_load_write()
526  * will occur, so that it can free its internal loading structures.
527  **/
528 void
529 gdk_pixbuf_loader_close (GdkPixbufLoader *loader)
530 {
531   GdkPixbufLoaderPrivate *priv;
532   
533   g_return_if_fail (loader != NULL);
534   g_return_if_fail (GDK_IS_PIXBUF_LOADER (loader));
535   
536   priv = loader->private;
537   
538   /* we expect it's not closed */
539   g_return_if_fail (priv->closed == FALSE);
540   
541   /* We have less the 128 bytes in the image.  Flush it, and keep going. */
542   if (priv->image_module == NULL)
543     gdk_pixbuf_loader_load_module (loader);
544   
545   if (priv->image_module && priv->image_module->stop_load)
546     priv->image_module->stop_load (priv->context);
547   
548   priv->closed = TRUE;
549   
550   gtk_signal_emit (GTK_OBJECT (loader), pixbuf_loader_signals[CLOSED]);
551 }