]> Pileus Git - ~andy/gtk/blob - gdk-pixbuf/gdk-pixbuf-loader.c
marshaller fixes.
[~andy/gtk] / gdk-pixbuf / gdk-pixbuf-loader.c
1 /* GdkPixbuf library - Progressive loader object
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 #include "gdk-pixbuf-marshal.h"
32
33 #include <gobject/gsignal.h>
34
35 enum {
36   AREA_UPDATED,
37   AREA_PREPARED,
38   FRAME_DONE,
39   ANIMATION_DONE,
40   CLOSED,
41   LAST_SIGNAL
42 };
43
44
45 static void gdk_pixbuf_loader_class_init    (GdkPixbufLoaderClass   *klass);
46 static void gdk_pixbuf_loader_init          (GdkPixbufLoader        *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 /**
70  * gdk_pixbuf_loader_get_type:
71  * @void:
72  *
73  * Registers the #GdkPixubfLoader class if necessary, and returns the type ID
74  * associated to it.
75  *
76  * Return value: The type ID of the #GdkPixbufLoader class.
77  **/
78 GType
79 gdk_pixbuf_loader_get_type (void)
80 {
81   static GType loader_type = 0;
82   
83   if (!loader_type)
84     {
85       static const GTypeInfo loader_info = {
86         sizeof (GdkPixbufLoaderClass),
87         (GBaseInitFunc) NULL,
88         (GBaseFinalizeFunc) NULL,
89         (GClassInitFunc) gdk_pixbuf_loader_class_init,
90         NULL,           /* class_finalize */
91         NULL,           /* class_data */
92         sizeof (GdkPixbufLoader),
93         0,              /* n_preallocs */
94         (GInstanceInitFunc) gdk_pixbuf_loader_init
95       };
96       
97       loader_type = g_type_register_static (G_TYPE_OBJECT,
98                                             "GdkPixbufLoader",
99                                             &loader_info,
100                                             0);
101     }
102   
103   return loader_type;
104 }
105
106 static void
107 gdk_pixbuf_loader_class_init (GdkPixbufLoaderClass *class)
108 {
109   GObjectClass *object_class;
110   
111   object_class = (GObjectClass *) class;
112   
113   parent_class = g_type_class_peek_parent (class);
114   
115   object_class->finalize = gdk_pixbuf_loader_finalize;
116
117   pixbuf_loader_signals[AREA_PREPARED] =
118     g_signal_newc ("area_prepared",
119                    G_TYPE_FROM_CLASS (object_class),
120                    G_SIGNAL_RUN_LAST,
121                    G_STRUCT_OFFSET (GdkPixbufLoaderClass, area_prepared),
122                    NULL,
123                    gdk_pixbuf_marshal_VOID__VOID,
124                    G_TYPE_NONE, 0);
125   
126   pixbuf_loader_signals[AREA_UPDATED] =
127     g_signal_newc ("area_updated",
128                    G_TYPE_FROM_CLASS (object_class),
129                    G_SIGNAL_RUN_LAST,
130                    G_STRUCT_OFFSET (GdkPixbufLoaderClass, area_updated),
131                    NULL,
132                    gdk_pixbuf_marshal_VOID__INT_INT_INT_INT,
133                    G_TYPE_NONE, 4,
134                    G_TYPE_INT,
135                    G_TYPE_INT,
136                    G_TYPE_INT,
137                    G_TYPE_INT);
138   
139   pixbuf_loader_signals[FRAME_DONE] =
140     g_signal_newc ("frame_done",
141                    G_TYPE_FROM_CLASS (object_class),
142                    G_SIGNAL_RUN_LAST,
143                    G_STRUCT_OFFSET (GdkPixbufLoaderClass, frame_done),
144                    NULL,
145                    gdk_pixbuf_marshal_VOID__POINTER,
146                    G_TYPE_NONE, 1,
147                    GDK_TYPE_PIXBUF_FRAME);
148   
149   pixbuf_loader_signals[ANIMATION_DONE] =
150     g_signal_newc ("animation_done",
151                    G_TYPE_FROM_CLASS (object_class),
152                    G_SIGNAL_RUN_LAST,                   
153                    G_STRUCT_OFFSET (GdkPixbufLoaderClass, animation_done),
154                    NULL,
155                    gdk_pixbuf_marshal_VOID__VOID,
156                    G_TYPE_NONE, 0);
157   
158   pixbuf_loader_signals[CLOSED] =
159     g_signal_newc ("closed",
160                    G_TYPE_FROM_CLASS (object_class),
161                    G_SIGNAL_RUN_LAST,
162                    G_STRUCT_OFFSET (GdkPixbufLoaderClass, closed),
163                    NULL,
164                    gdk_pixbuf_marshal_VOID__VOID,
165                    G_TYPE_NONE, 0);
166 }
167
168 static void
169 gdk_pixbuf_loader_init (GdkPixbufLoader *loader)
170 {
171   GdkPixbufLoaderPrivate *priv;
172   
173   priv = g_new0 (GdkPixbufLoaderPrivate, 1);
174   loader->priv = priv;
175 }
176
177 static void
178 gdk_pixbuf_loader_finalize (GObject *object)
179 {
180   GdkPixbufLoader *loader;
181   GdkPixbufLoaderPrivate *priv = NULL;
182   
183   loader = GDK_PIXBUF_LOADER (object);
184   priv = loader->priv;
185
186   if (!priv->closed)
187     g_warning ("GdkPixbufLoader finalized without calling gdk_pixbuf_loader_close() - this is not allowed. You must explicitly end the data stream to the loader before dropping the last reference.");
188   
189   if (priv->animation)
190     gdk_pixbuf_animation_unref (priv->animation);
191
192   if (priv->pixbuf)
193     gdk_pixbuf_unref (priv->pixbuf);
194   
195   g_free (priv);
196   
197   G_OBJECT_CLASS (parent_class)->finalize (object);
198 }
199
200 static void
201 gdk_pixbuf_loader_prepare (GdkPixbuf *pixbuf,
202                            gpointer   loader)
203 {
204   GdkPixbufLoaderPrivate *priv = NULL;
205   
206   priv = GDK_PIXBUF_LOADER (loader)->priv;
207   gdk_pixbuf_ref (pixbuf);
208
209   g_assert (priv->pixbuf == NULL);
210   
211   priv->pixbuf = pixbuf;
212   g_signal_emit (G_OBJECT (loader), pixbuf_loader_signals[AREA_PREPARED], 0);
213 }
214
215 static void
216 gdk_pixbuf_loader_update (GdkPixbuf *pixbuf,
217                           guint      x,
218                           guint      y,
219                           guint      width,
220                           guint      height,
221                           gpointer   loader)
222 {
223   GdkPixbufLoaderPrivate *priv = NULL;
224   
225   priv = GDK_PIXBUF_LOADER (loader)->priv;
226   
227   g_signal_emit (G_OBJECT (loader),
228                  pixbuf_loader_signals[AREA_UPDATED],
229                  0,
230                  x, y,
231                  /* sanity check in here.  Defend against an errant loader */
232                  MIN (width, gdk_pixbuf_get_width (priv->pixbuf)),
233                  MIN (height, gdk_pixbuf_get_height (priv->pixbuf)));
234 }
235
236 static void
237 gdk_pixbuf_loader_frame_done (GdkPixbufFrame *frame,
238                               gpointer        loader)
239 {
240   GdkPixbufLoaderPrivate *priv = NULL;
241   
242   priv = GDK_PIXBUF_LOADER (loader)->priv;
243   
244   priv->pixbuf = NULL;
245   
246   if (priv->animation == NULL)
247     {
248       priv->animation = g_object_new (GDK_TYPE_PIXBUF_ANIMATION, NULL);
249       
250       priv->animation->n_frames = 0;
251       priv->animation->width  = gdk_pixbuf_get_width  (frame->pixbuf) + frame->x_offset;
252       priv->animation->height = gdk_pixbuf_get_height (frame->pixbuf) + frame->y_offset;
253     }
254   else
255     {
256       int w, h;
257       
258       /* update bbox size */
259       w = gdk_pixbuf_get_width (frame->pixbuf) + frame->x_offset;
260       h = gdk_pixbuf_get_height (frame->pixbuf) + frame->y_offset;
261       
262       if (w > priv->animation->width) {
263         priv->animation->width = w;
264       }
265       if (h > priv->animation->height) {
266         priv->animation->height = h;
267       }
268     }
269   
270   priv->animation->frames = g_list_append (priv->animation->frames, frame);
271   priv->animation->n_frames++;
272   g_signal_emit (G_OBJECT (loader),
273                  pixbuf_loader_signals[FRAME_DONE],
274                  0,
275                  frame);
276 }
277
278 static void
279 gdk_pixbuf_loader_animation_done (GdkPixbuf *pixbuf,
280                                   gpointer   loader)
281 {
282   GdkPixbufLoaderPrivate *priv = NULL;
283   GdkPixbufFrame    *frame;
284   GList *current = NULL;
285   gint h, w;
286   
287   priv = GDK_PIXBUF_LOADER (loader)->priv;
288   priv->pixbuf = NULL;
289   
290   current = gdk_pixbuf_animation_get_frames (priv->animation);
291   
292   while (current)
293     {
294       frame = (GdkPixbufFrame *) current->data;
295       
296       /* update bbox size */
297       w = gdk_pixbuf_get_width (frame->pixbuf) + frame->x_offset;
298       h = gdk_pixbuf_get_height (frame->pixbuf) + frame->y_offset;
299       
300       if (w > priv->animation->width)
301         priv->animation->width = w;
302       if (h > priv->animation->height)
303         priv->animation->height = h;
304       current = current->next;
305     }
306   
307   g_signal_emit (G_OBJECT (loader), pixbuf_loader_signals[ANIMATION_DONE], 0);
308 }
309
310 static gint
311 gdk_pixbuf_loader_load_module (GdkPixbufLoader *loader,
312                                const char      *image_type,
313                                GError         **error)
314 {
315   GdkPixbufLoaderPrivate *priv = loader->priv;
316
317   if (image_type)
318     {
319       priv->image_module = _gdk_pixbuf_get_named_module (image_type,
320                                                          error);
321     }
322   else
323     {
324       g_return_val_if_fail (priv->header_buf_offset > 0, 0);
325       priv->image_module = _gdk_pixbuf_get_module (priv->header_buf,
326                                                    priv->header_buf_offset,
327                                                    NULL,
328                                                    error);
329     }
330   
331   if (priv->image_module == NULL)
332     return 0;
333   
334   if (priv->image_module->module == NULL)
335     if (!_gdk_pixbuf_load_module (priv->image_module, error))
336       return 0;
337   
338   if (priv->image_module->module == NULL)
339     return 0;
340   
341   if ((priv->image_module->begin_load == NULL) ||
342       (priv->image_module->stop_load == NULL) ||
343       (priv->image_module->load_increment == NULL))
344     {
345       g_set_error (error,
346                    GDK_PIXBUF_ERROR,
347                    GDK_PIXBUF_ERROR_UNSUPPORTED_OPERATION,
348                    _("Incremental loading of image type '%s' is not supported"),
349                    image_type);
350
351       return 0;
352     }
353   
354   priv->context = priv->image_module->begin_load (gdk_pixbuf_loader_prepare,
355                                                   gdk_pixbuf_loader_update,
356                                                   gdk_pixbuf_loader_frame_done,
357                                                   gdk_pixbuf_loader_animation_done,
358                                                   loader,
359                                                   error);
360   
361   if (priv->context == NULL)
362     {
363       /* Defense against broken loaders; DO NOT take this as a GError
364        * example
365        */
366       if (error && *error == NULL)
367         {
368           g_warning ("Bug! loader '%s' didn't set an error on failure",
369                      priv->image_module->module_name);
370           g_set_error (error,
371                        GDK_PIXBUF_ERROR,
372                        GDK_PIXBUF_ERROR_FAILED,
373                        _("Internal error: Image loader module '%s'"
374                          " failed to begin loading an image, but didn't"
375                          " give a reason for the failure"),
376                        priv->image_module->module_name);
377
378         }
379       
380       return 0;
381     }
382   
383   if (priv->header_buf_offset
384       && priv->image_module->load_increment (priv->context, priv->header_buf, priv->header_buf_offset, error))
385     return priv->header_buf_offset;
386   
387   return 0;
388 }
389
390 static int
391 gdk_pixbuf_loader_eat_header_write (GdkPixbufLoader *loader,
392                                     const guchar    *buf,
393                                     gsize            count,
394                                     GError         **error)
395 {
396   gint n_bytes;
397   GdkPixbufLoaderPrivate *priv = loader->priv;
398   
399   n_bytes = MIN(LOADER_HEADER_SIZE - priv->header_buf_offset, count);
400   memcpy (priv->header_buf + priv->header_buf_offset, buf, n_bytes);
401   
402   priv->header_buf_offset += n_bytes;
403   
404   if (priv->header_buf_offset >= LOADER_HEADER_SIZE)
405     {
406       if (gdk_pixbuf_loader_load_module (loader, NULL, error) == 0)
407         return 0;
408     }
409   
410   return n_bytes;
411 }
412
413 /**
414  * gdk_pixbuf_loader_write:
415  * @loader: A pixbuf loader.
416  * @buf: Pointer to image data.
417  * @count: Length of the @buf buffer in bytes.
418  * @error: return location for errors
419  *
420  * This will cause a pixbuf loader to parse the next @count bytes of
421  * an image.  It will return TRUE if the data was loaded successfully,
422  * and FALSE if an error occurred.  In the latter case, the loader
423  * will be closed, and will not accept further writes. If FALSE is
424  * returned, @error will be set to an error from the #GDK_PIXBUF_ERROR
425  * or #G_FILE_ERROR domains.
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                          GError         **error)
435 {
436   GdkPixbufLoaderPrivate *priv;
437   
438   g_return_val_if_fail (loader != NULL, FALSE);
439   g_return_val_if_fail (GDK_IS_PIXBUF_LOADER (loader), FALSE);
440   
441   g_return_val_if_fail (buf != NULL, FALSE);
442   g_return_val_if_fail (count >= 0, FALSE);
443   
444   priv = loader->priv;
445   
446   /* we expect it's not to be closed */
447   g_return_val_if_fail (priv->closed == FALSE, FALSE);
448   
449   if (priv->image_module == NULL)
450     {
451       gint eaten;
452       
453       eaten = gdk_pixbuf_loader_eat_header_write(loader, buf, count, error);
454       if (eaten <= 0)
455         return FALSE;
456       
457       count -= eaten;
458       buf += eaten;
459     }
460   
461   if (count > 0 && priv->image_module->load_increment)
462     {
463       gboolean retval;
464       retval = priv->image_module->load_increment (priv->context, buf, count,
465                                                    error);
466       if (!retval && error && *error == NULL)
467         {
468           /* Fix up busted image loader */
469           g_warning ("Bug! loader '%s' didn't set an error on failure",
470                      priv->image_module->module_name);
471           g_set_error (error,
472                        GDK_PIXBUF_ERROR,
473                        GDK_PIXBUF_ERROR_FAILED,
474                        _("Internal error: Image loader module '%s'"
475                          " failed to begin loading an image, but didn't"
476                          " give a reason for the failure"),
477                        priv->image_module->module_name);
478         }
479
480       return retval;
481     }
482       
483   return TRUE;
484 }
485
486 /**
487  * gdk_pixbuf_loader_new:
488  *
489  * Creates a new pixbuf loader object.
490  *
491  * Return value: A newly-created pixbuf loader.
492  **/
493 GdkPixbufLoader *
494 gdk_pixbuf_loader_new (void)
495 {
496   return g_object_new (GDK_TYPE_PIXBUF_LOADER, NULL);
497 }
498
499 /**
500  * gdk_pixbuf_loader_new_with_type:
501  * @image_type: name of the image format to be loaded with the image
502  * @error: return location for an allocated #GError, or %NULL to ignore errors
503  *
504  * Creates a new pixbuf loader object that always attempts to parse
505  * image data as if it were an image of type @image_type, instead of
506  * identifying the type automatically. Useful if you want an error if
507  * the image isn't the expected type, for loading image formats
508  * that can't be reliably identified by looking at the data, or if
509  * the user manually forces a specific type.
510  *
511  * Return value: A newly-created pixbuf loader.
512  **/
513 GdkPixbufLoader *
514 gdk_pixbuf_loader_new_with_type (const char *image_type,
515                                  GError    **error)
516 {
517   GdkPixbufLoader *retval;
518   GError *tmp;
519   
520   retval = g_object_new (GDK_TYPE_PIXBUF_LOADER, NULL);
521
522   tmp = NULL;
523   gdk_pixbuf_loader_load_module(retval, image_type, &tmp);
524   if (tmp != NULL)
525     {
526       g_propagate_error (error, tmp);
527       g_object_unref (G_OBJECT (retval));
528       return NULL;
529     }
530
531   return retval;
532 }
533
534 /**
535  * gdk_pixbuf_loader_get_pixbuf:
536  * @loader: A pixbuf loader.
537  *
538  * Queries the GdkPixbuf that a pixbuf loader is currently creating.
539  * In general it only makes sense to call this function afer the
540  * "area_prepared" signal has been emitted by the loader; this means
541  * that enough data has been read to know the size of the image that
542  * will be allocated.  If the loader has not received enough data via
543  * gdk_pixbuf_loader_write(), then this function returns NULL.  The
544  * returned pixbuf will be the same in all future calls to the loader,
545  * so simply calling gdk_pixbuf_ref() should be sufficient to continue
546  * using it.  Additionally, if the loader is an animation, it will
547  * return the first frame of the animation.
548  * 
549  * Return value: The GdkPixbuf that the loader is creating, or NULL if not
550  * enough data has been read to determine how to create the image buffer.
551  **/
552 GdkPixbuf *
553 gdk_pixbuf_loader_get_pixbuf (GdkPixbufLoader *loader)
554 {
555   GdkPixbufLoaderPrivate *priv;
556   
557   g_return_val_if_fail (loader != NULL, NULL);
558   g_return_val_if_fail (GDK_IS_PIXBUF_LOADER (loader), NULL);
559   
560   priv = loader->priv;
561
562   if (priv->animation)
563     {
564       GList *list;
565       
566       list = gdk_pixbuf_animation_get_frames (priv->animation);
567       if (list != NULL)
568         {
569           GdkPixbufFrame *frame = list->data;
570           
571           return gdk_pixbuf_frame_get_pixbuf (frame);
572         }
573     }
574   
575   return priv->pixbuf;
576 }
577
578 /**
579  * gdk_pixbuf_loader_get_animation:
580  * @loader: A pixbuf loader
581  *
582  * Queries the GdkPixbufAnimation that a pixbuf loader is currently creating.
583  * In general it only makes sense to call this function afer the "area_prepared"
584  * signal has been emitted by the loader.  If the image is not an animation,
585  * then it will return NULL.
586  *
587  * Return value: The GdkPixbufAnimation that the loader is loading, or NULL if
588  not enough data has been read to determine the information.
589 **/
590 GdkPixbufAnimation *
591 gdk_pixbuf_loader_get_animation (GdkPixbufLoader *loader)
592 {
593   GdkPixbufLoaderPrivate *priv;
594   
595   g_return_val_if_fail (loader != NULL, NULL);
596   g_return_val_if_fail (GDK_IS_PIXBUF_LOADER (loader), NULL);
597   
598   priv = loader->priv;
599   
600   return priv->animation;
601 }
602
603 /**
604  * gdk_pixbuf_loader_close:
605  * @loader: A pixbuf loader.
606  * @error: return location for a #GError, or %NULL to ignore errors
607  *
608  * Informs a pixbuf loader that no further writes with
609  * gdk_pixbuf_loader_write() will occur, so that it can free its
610  * internal loading structures. Also, tries to parse any data that
611  * hasn't yet been parsed; if the remaining data is partial or
612  * corrupt, an error will be returned.  If FALSE is returned, @error
613  * will be set to an error from the #GDK_PIXBUF_ERROR or #G_FILE_ERROR
614  * domains. If you're just cancelling a load rather than expecting it
615  * to be finished, passing %NULL for @error to ignore it is
616  * reasonable.
617  *
618  * Returns: %TRUE if all image data written so far was successfully
619             passed out via the update_area signal
620  **/
621 gboolean
622 gdk_pixbuf_loader_close (GdkPixbufLoader *loader,
623                          GError         **error)
624 {
625   GdkPixbufLoaderPrivate *priv;
626   gboolean retval = TRUE;
627   
628   g_return_val_if_fail (loader != NULL, TRUE);
629   g_return_val_if_fail (GDK_IS_PIXBUF_LOADER (loader), TRUE);
630   
631   priv = loader->priv;
632   
633   /* we expect it's not closed */
634   g_return_val_if_fail (priv->closed == FALSE, TRUE);
635   
636   /* We have less the 128 bytes in the image.  Flush it, and keep going. */
637   if (priv->image_module == NULL)
638     gdk_pixbuf_loader_load_module (loader, NULL, NULL);
639   
640   if (priv->image_module && priv->image_module->stop_load)
641     retval = priv->image_module->stop_load (priv->context, error);
642   
643   priv->closed = TRUE;
644   
645   g_signal_emit (G_OBJECT (loader), pixbuf_loader_signals[CLOSED], 0);
646
647   return retval;
648 }