]> Pileus Git - ~andy/gtk/blob - gdk-pixbuf/gdk-pixbuf-loader.c
Fix some issues with action widgets
[~andy/gtk] / gdk-pixbuf / gdk-pixbuf-loader.c
1 /* -*- Mode: C; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 8 -*- */
2 /* GdkPixbuf library - Progressive loader object
3  *
4  * Copyright (C) 1999 The Free Software Foundation
5  *
6  * Authors: Mark Crichton <crichton@gimp.org>
7  *          Miguel de Icaza <miguel@gnu.org>
8  *          Federico Mena-Quintero <federico@gimp.org>
9  *          Jonathan Blandford <jrb@redhat.com>
10  *
11  * This library is free software; you can redistribute it and/or
12  * modify it under the terms of the GNU Lesser General Public
13  * License as published by the Free Software Foundation; either
14  * version 2 of the License, or (at your option) any later version.
15  *
16  * This library is distributed in the hope that it will be useful,
17  * but WITHOUT ANY WARRANTY; without even the implied warranty of
18  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
19  * Lesser General Public License for more details.
20  *
21  * You should have received a copy of the GNU Lesser General Public
22  * License along with this library; if not, write to the
23  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
24  * Boston, MA 02111-1307, USA.
25  */
26
27 #include "config.h"
28 #include <string.h>
29
30 #include "gdk-pixbuf-private.h"
31 #include "gdk-pixbuf-animation.h"
32 #include "gdk-pixbuf-scaled-anim.h"
33 #include "gdk-pixbuf-io.h"
34 #include "gdk-pixbuf-loader.h"
35 #include "gdk-pixbuf-marshal.h"
36 #include "gdk-pixbuf-alias.h"
37
38 enum {
39         SIZE_PREPARED,
40         AREA_PREPARED,
41         AREA_UPDATED,
42         CLOSED,
43         LAST_SIGNAL
44 };
45
46
47 static void gdk_pixbuf_loader_finalize (GObject *loader);
48
49 static guint    pixbuf_loader_signals[LAST_SIGNAL] = { 0 };
50
51 /* Internal data */
52
53 #define LOADER_HEADER_SIZE 1024
54
55 typedef struct
56 {
57         GdkPixbufAnimation *animation;
58         gboolean closed;
59         gboolean holds_threadlock;
60         guchar header_buf[LOADER_HEADER_SIZE];
61         gint header_buf_offset;
62         GdkPixbufModule *image_module;
63         gpointer context;
64         gint width;
65         gint height;
66         gboolean size_fixed;
67         gboolean needs_scale;
68 } GdkPixbufLoaderPrivate;
69
70 G_DEFINE_TYPE (GdkPixbufLoader, gdk_pixbuf_loader, G_TYPE_OBJECT)
71
72 static void
73 gdk_pixbuf_loader_class_init (GdkPixbufLoaderClass *class)
74 {
75         GObjectClass *object_class;
76   
77         object_class = (GObjectClass *) class;
78   
79         object_class->finalize = gdk_pixbuf_loader_finalize;
80
81         /**
82          * GdkPixbufLoader::size-prepared:
83          * @loader: the object which received the signal.
84          * @width: the original width of the image
85          * @height: the original height of the image
86          *
87          * This signal is emitted when the pixbuf loader has been fed the
88          * initial amount of data that is required to figure out the size
89          * of the image that it will create.  Applications can call  
90          * gdk_pixbuf_loader_set_size() in response to this signal to set
91          * the desired size to which the image should be scaled.
92          */
93         pixbuf_loader_signals[SIZE_PREPARED] =
94                 g_signal_new ("size-prepared",
95                               G_TYPE_FROM_CLASS (object_class),
96                               G_SIGNAL_RUN_LAST,
97                               G_STRUCT_OFFSET (GdkPixbufLoaderClass, size_prepared),
98                               NULL, NULL,
99                               _gdk_pixbuf_marshal_VOID__INT_INT,
100                               G_TYPE_NONE, 2, 
101                               G_TYPE_INT,
102                               G_TYPE_INT);
103   
104         /**
105          * GdkPixbufLoader::area-prepared:
106          * @loader: the object which received the signal.
107          *
108          * This signal is emitted when the pixbuf loader has allocated the 
109          * pixbuf in the desired size.  After this signal is emitted, 
110          * applications can call gdk_pixbuf_loader_get_pixbuf() to fetch 
111          * the partially-loaded pixbuf.
112          */
113         pixbuf_loader_signals[AREA_PREPARED] =
114                 g_signal_new ("area-prepared",
115                               G_TYPE_FROM_CLASS (object_class),
116                               G_SIGNAL_RUN_LAST,
117                               G_STRUCT_OFFSET (GdkPixbufLoaderClass, area_prepared),
118                               NULL, NULL,
119                               _gdk_pixbuf_marshal_VOID__VOID,
120                               G_TYPE_NONE, 0);
121
122         /**
123          * GdkPixbufLoader::area-updated:
124          * @loader: the object which received the signal.
125          * @x: X offset of upper-left corner of the updated area.
126          * @y: Y offset of upper-left corner of the updated area.
127          * @width: Width of updated area.
128          * @height: Height of updated area.
129          *
130          * This signal is emitted when a significant area of the image being
131          * loaded has been updated.  Normally it means that a complete
132          * scanline has been read in, but it could be a different area as
133          * well.  Applications can use this signal to know when to repaint
134          * areas of an image that is being loaded.
135          */
136         pixbuf_loader_signals[AREA_UPDATED] =
137                 g_signal_new ("area-updated",
138                               G_TYPE_FROM_CLASS (object_class),
139                               G_SIGNAL_RUN_LAST,
140                               G_STRUCT_OFFSET (GdkPixbufLoaderClass, area_updated),
141                               NULL, NULL,
142                               _gdk_pixbuf_marshal_VOID__INT_INT_INT_INT,
143                               G_TYPE_NONE, 4,
144                               G_TYPE_INT,
145                               G_TYPE_INT,
146                               G_TYPE_INT,
147                               G_TYPE_INT);
148   
149         /**
150          * GdkPixbufLoader::closed:
151          * @loader: the object which received the signal.
152          *
153          * This signal is emitted when gdk_pixbuf_loader_close() is called.
154          * It can be used by different parts of an application to receive
155          * notification when an image loader is closed by the code that
156          * drives it.
157          */
158         pixbuf_loader_signals[CLOSED] =
159                 g_signal_new ("closed",
160                               G_TYPE_FROM_CLASS (object_class),
161                               G_SIGNAL_RUN_LAST,
162                               G_STRUCT_OFFSET (GdkPixbufLoaderClass, closed),
163                               NULL, 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         priv->width = -1;
175         priv->height = -1;
176
177         loader->priv = priv;
178 }
179
180 static void
181 gdk_pixbuf_loader_finalize (GObject *object)
182 {
183         GdkPixbufLoader *loader;
184         GdkPixbufLoaderPrivate *priv = NULL;
185   
186         loader = GDK_PIXBUF_LOADER (object);
187         priv = loader->priv;
188
189         if (!priv->closed) {
190                 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.");
191                 if (priv->holds_threadlock) {
192                         _gdk_pixbuf_unlock (priv->image_module);
193                 }
194         }
195         if (priv->animation)
196                 g_object_unref (priv->animation);
197   
198         g_free (priv);
199   
200         G_OBJECT_CLASS (gdk_pixbuf_loader_parent_class)->finalize (object);
201 }
202
203 /**
204  * gdk_pixbuf_loader_set_size:
205  * @loader: A pixbuf loader.
206  * @width: The desired width of the image being loaded.
207  * @height: The desired height of the image being loaded.
208  *
209  * Causes the image to be scaled while it is loaded. The desired
210  * image size can be determined relative to the original size of
211  * the image by calling gdk_pixbuf_loader_set_size() from a
212  * signal handler for the ::size-prepared signal.
213  *
214  * Attempts to set the desired image size  are ignored after the 
215  * emission of the ::size-prepared signal.
216  *
217  * Since: 2.2
218  */
219 void 
220 gdk_pixbuf_loader_set_size (GdkPixbufLoader *loader,
221                             gint             width,
222                             gint             height)
223 {
224         GdkPixbufLoaderPrivate *priv = GDK_PIXBUF_LOADER (loader)->priv;
225         g_return_if_fail (width >= 0 && height >= 0);
226
227         if (!priv->size_fixed) 
228                 {
229                         priv->width = width;
230                         priv->height = height;
231                 }
232 }
233
234 static void
235 gdk_pixbuf_loader_size_func (gint *width, gint *height, gpointer loader)
236 {
237         GdkPixbufLoaderPrivate *priv = GDK_PIXBUF_LOADER (loader)->priv;
238
239         /* allow calling gdk_pixbuf_loader_set_size() before the signal */
240         if (priv->width == -1 && priv->height == -1) 
241                 {
242                         priv->width = *width;
243                         priv->height = *height;
244                 }
245
246         g_signal_emit (loader, pixbuf_loader_signals[SIZE_PREPARED], 0, *width, *height);
247         priv->size_fixed = TRUE;
248
249         *width = priv->width;
250         *height = priv->height;
251 }
252
253 static void
254 gdk_pixbuf_loader_prepare (GdkPixbuf          *pixbuf,
255                            GdkPixbufAnimation *anim,
256                            gpointer            loader)
257 {
258         GdkPixbufLoaderPrivate *priv = GDK_PIXBUF_LOADER (loader)->priv;
259         g_return_if_fail (pixbuf != NULL);
260
261         if (!priv->size_fixed) 
262                 {
263                         /* Defend against lazy loaders which don't call size_func */
264                         gint width = gdk_pixbuf_get_width (pixbuf);
265                         gint height = gdk_pixbuf_get_height (pixbuf);
266                         
267                         gdk_pixbuf_loader_size_func (&width, &height, loader);
268                 }
269
270         priv->needs_scale = FALSE;
271         if (priv->width > 0 && priv->height > 0 &&
272             (priv->width != gdk_pixbuf_get_width (pixbuf) ||
273              priv->height != gdk_pixbuf_get_height (pixbuf)))
274                 priv->needs_scale = TRUE;
275
276         if (anim)
277                 g_object_ref (anim);
278         else
279                 anim = gdk_pixbuf_non_anim_new (pixbuf);
280   
281         if (priv->needs_scale) {
282                 priv->animation  = GDK_PIXBUF_ANIMATION (_gdk_pixbuf_scaled_anim_new (anim,
283                                          (double) priv->width / gdk_pixbuf_get_width (pixbuf),
284                                          (double) priv->height / gdk_pixbuf_get_height (pixbuf),
285                                           1.0));
286                         g_object_unref (anim);
287         }
288         else
289                 priv->animation = anim;
290   
291         if (!priv->needs_scale)
292                 g_signal_emit (loader, pixbuf_loader_signals[AREA_PREPARED], 0);
293 }
294
295 static void
296 gdk_pixbuf_loader_update (GdkPixbuf *pixbuf,
297                           gint       x,
298                           gint       y,
299                           gint       width,
300                           gint       height,
301                           gpointer   loader)
302 {
303         GdkPixbufLoaderPrivate *priv = GDK_PIXBUF_LOADER (loader)->priv;
304   
305         if (!priv->needs_scale)
306                 g_signal_emit (loader,
307                                pixbuf_loader_signals[AREA_UPDATED],
308                                0,
309                                x, y,
310                                /* sanity check in here.  Defend against an errant loader */
311                                MIN (width, gdk_pixbuf_animation_get_width (priv->animation)),
312                                MIN (height, gdk_pixbuf_animation_get_height (priv->animation)));
313 }
314
315 /* Defense against broken loaders; DO NOT take this as a GError example! */
316 static void
317 gdk_pixbuf_loader_ensure_error (GdkPixbufLoader *loader,
318                                 GError         **error)
319
320         GdkPixbufLoaderPrivate *priv = loader->priv;
321
322         if (error == NULL || *error != NULL)
323                 return;
324
325         g_warning ("Bug! loader '%s' didn't set an error on failure",
326                    priv->image_module->module_name);
327         g_set_error (error,
328                      GDK_PIXBUF_ERROR,
329                      GDK_PIXBUF_ERROR_FAILED,
330                      _("Internal error: Image loader module '%s' failed to"
331                        " complete an operation, but didn't give a reason for"
332                        " the failure"),
333                      priv->image_module->module_name);
334 }
335
336 static gint
337 gdk_pixbuf_loader_load_module (GdkPixbufLoader *loader,
338                                const char      *image_type,
339                                GError         **error)
340 {
341         GdkPixbufLoaderPrivate *priv = loader->priv;
342
343         if (image_type)
344                 {
345                         priv->image_module = _gdk_pixbuf_get_named_module (image_type,
346                                                                            error);
347                 }
348         else
349                 {
350                         priv->image_module = _gdk_pixbuf_get_module (priv->header_buf,
351                                                                      priv->header_buf_offset,
352                                                                      NULL,
353                                                                      error);
354                 }
355   
356         if (priv->image_module == NULL)
357                 return 0;
358   
359         if (!_gdk_pixbuf_load_module (priv->image_module, error))
360                 return 0;
361   
362         if (priv->image_module->module == NULL)
363                 return 0;
364   
365         if ((priv->image_module->begin_load == NULL) ||
366             (priv->image_module->stop_load == NULL) ||
367             (priv->image_module->load_increment == NULL))
368                 {
369                         g_set_error (error,
370                                      GDK_PIXBUF_ERROR,
371                                      GDK_PIXBUF_ERROR_UNSUPPORTED_OPERATION,
372                                      _("Incremental loading of image type '%s' is not supported"),
373                                      priv->image_module->module_name);
374
375                         return 0;
376                 }
377
378         if (!priv->holds_threadlock) {
379                 priv->holds_threadlock = _gdk_pixbuf_lock (priv->image_module);
380         }
381
382         priv->context = priv->image_module->begin_load (gdk_pixbuf_loader_size_func,
383                                                         gdk_pixbuf_loader_prepare,
384                                                         gdk_pixbuf_loader_update,
385                                                         loader,
386                                                         error);
387   
388         if (priv->context == NULL)
389                 {
390                         gdk_pixbuf_loader_ensure_error (loader, error);
391                         return 0;
392                 }
393   
394         if (priv->header_buf_offset
395             && priv->image_module->load_increment (priv->context, priv->header_buf, priv->header_buf_offset, error))
396                 return priv->header_buf_offset;
397   
398         return 0;
399 }
400
401 static int
402 gdk_pixbuf_loader_eat_header_write (GdkPixbufLoader *loader,
403                                     const guchar    *buf,
404                                     gsize            count,
405                                     GError         **error)
406 {
407         gint n_bytes;
408         GdkPixbufLoaderPrivate *priv = loader->priv;
409   
410         n_bytes = MIN(LOADER_HEADER_SIZE - priv->header_buf_offset, count);
411         memcpy (priv->header_buf + priv->header_buf_offset, buf, n_bytes);
412   
413         priv->header_buf_offset += n_bytes;
414   
415         if (priv->header_buf_offset >= LOADER_HEADER_SIZE)
416                 {
417                         if (gdk_pixbuf_loader_load_module (loader, NULL, error) == 0)
418                                 return 0;
419                 }
420   
421         return n_bytes;
422 }
423
424 /**
425  * gdk_pixbuf_loader_write:
426  * @loader: A pixbuf loader.
427  * @buf: Pointer to image data.
428  * @count: Length of the @buf buffer in bytes.
429  * @error: return location for errors
430  *
431  * This will cause a pixbuf loader to parse the next @count bytes of
432  * an image.  It will return %TRUE if the data was loaded successfully,
433  * and %FALSE if an error occurred.  In the latter case, the loader
434  * will be closed, and will not accept further writes. If %FALSE is
435  * returned, @error will be set to an error from the #GDK_PIXBUF_ERROR
436  * or #G_FILE_ERROR domains.
437  *
438  * Return value: %TRUE if the write was successful, or %FALSE if the loader
439  * cannot parse the buffer.
440  **/
441 gboolean
442 gdk_pixbuf_loader_write (GdkPixbufLoader *loader,
443                          const guchar    *buf,
444                          gsize            count,
445                          GError         **error)
446 {
447         GdkPixbufLoaderPrivate *priv;
448   
449         g_return_val_if_fail (GDK_IS_PIXBUF_LOADER (loader), FALSE);
450   
451         g_return_val_if_fail (buf != NULL, FALSE);
452         g_return_val_if_fail (error == NULL || *error == NULL, FALSE);
453   
454         priv = loader->priv;
455
456         /* we expect it's not to be closed */
457         g_return_val_if_fail (priv->closed == FALSE, FALSE);
458   
459         if (count > 0 && priv->image_module == NULL)
460                 {
461                         gint eaten;
462       
463                         eaten = gdk_pixbuf_loader_eat_header_write (loader, buf, count, error);
464                         if (eaten <= 0)
465                                goto fail; 
466       
467                         count -= eaten;
468                         buf += eaten;
469                 }
470   
471         if (count > 0 && priv->image_module->load_increment)
472                 {
473                         if (!priv->image_module->load_increment (priv->context, buf, count,
474                                                                  error))
475                                 goto fail;
476                 }
477       
478         return TRUE;
479
480  fail:
481         gdk_pixbuf_loader_ensure_error (loader, error);
482         gdk_pixbuf_loader_close (loader, NULL);
483
484         return FALSE;
485 }
486
487 /**
488  * gdk_pixbuf_loader_new:
489  *
490  * Creates a new pixbuf loader object.
491  *
492  * Return value: A newly-created pixbuf loader.
493  **/
494 GdkPixbufLoader *
495 gdk_pixbuf_loader_new (void)
496 {
497         return g_object_new (GDK_TYPE_PIXBUF_LOADER, NULL);
498 }
499
500 /**
501  * gdk_pixbuf_loader_new_with_type:
502  * @image_type: name of the image format to be loaded with the image
503  * @error: return location for an allocated #GError, or %NULL to ignore errors
504  *
505  * Creates a new pixbuf loader object that always attempts to parse
506  * image data as if it were an image of type @image_type, instead of
507  * identifying the type automatically. Useful if you want an error if
508  * the image isn't the expected type, for loading image formats
509  * that can't be reliably identified by looking at the data, or if
510  * the user manually forces a specific type.
511  *
512  * The list of supported image formats depends on what image loaders
513  * are installed, but typically "png", "jpeg", "gif", "tiff" and 
514  * "xpm" are among the supported formats. To obtain the full list of
515  * supported image formats, call gdk_pixbuf_format_get_name() on each 
516  * of the #GdkPixbufFormat structs returned by gdk_pixbuf_get_formats().
517  *
518  * Return value: A newly-created pixbuf loader.
519  **/
520 GdkPixbufLoader *
521 gdk_pixbuf_loader_new_with_type (const char *image_type,
522                                  GError    **error)
523 {
524         GdkPixbufLoader *retval;
525         GError *tmp;
526         g_return_val_if_fail (error == NULL || *error == NULL, NULL);
527   
528         retval = g_object_new (GDK_TYPE_PIXBUF_LOADER, NULL);
529
530         tmp = NULL;
531         gdk_pixbuf_loader_load_module (retval, image_type, &tmp);
532         if (tmp != NULL)
533                 {
534                         g_propagate_error (error, tmp);
535                         gdk_pixbuf_loader_close (retval, NULL);
536                         g_object_unref (retval);
537                         return NULL;
538                 }
539
540         return retval;
541 }
542
543 /**
544  * gdk_pixbuf_loader_new_with_mime_type:
545  * @mime_type: the mime type to be loaded 
546  * @error: return location for an allocated #GError, or %NULL to ignore errors
547  *
548  * Creates a new pixbuf loader object that always attempts to parse
549  * image data as if it were an image of mime type @mime_type, instead of
550  * identifying the type automatically. Useful if you want an error if
551  * the image isn't the expected mime type, for loading image formats
552  * that can't be reliably identified by looking at the data, or if
553  * the user manually forces a specific mime type.
554  *
555  * The list of supported mime types depends on what image loaders
556  * are installed, but typically "image/png", "image/jpeg", "image/gif", 
557  * "image/tiff" and "image/x-xpixmap" are among the supported mime types. 
558  * To obtain the full list of supported mime types, call 
559  * gdk_pixbuf_format_get_mime_types() on each of the #GdkPixbufFormat 
560  * structs returned by gdk_pixbuf_get_formats().
561  *
562  * Return value: A newly-created pixbuf loader.
563  * Since: 2.4
564  **/
565 GdkPixbufLoader *
566 gdk_pixbuf_loader_new_with_mime_type (const char *mime_type,
567                                       GError    **error)
568 {
569         const char * image_type = NULL;
570         char ** mimes;
571
572         GdkPixbufLoader *retval;
573         GError *tmp;
574   
575         GSList * formats;
576         GdkPixbufFormat *info;
577         int i, j, length;
578
579         formats = gdk_pixbuf_get_formats ();
580         length = g_slist_length (formats);
581
582         for (i = 0; i < length && image_type == NULL; i++) {
583                 info = (GdkPixbufFormat *)g_slist_nth_data (formats, i);
584                 mimes = info->mime_types;
585                 
586                 for (j = 0; mimes[j] != NULL; j++)
587                         if (g_ascii_strcasecmp (mimes[j], mime_type) == 0) {
588                                 image_type = info->name;
589                                 break;
590                         }
591         }
592
593         g_slist_free (formats);
594
595         retval = g_object_new (GDK_TYPE_PIXBUF_LOADER, NULL);
596
597         tmp = NULL;
598         gdk_pixbuf_loader_load_module (retval, image_type, &tmp);
599         if (tmp != NULL)
600                 {
601                         g_propagate_error (error, tmp);
602                         gdk_pixbuf_loader_close (retval, NULL);
603                         g_object_unref (retval);
604                         return NULL;
605                 }
606
607         return retval;
608 }
609
610 /**
611  * gdk_pixbuf_loader_get_pixbuf:
612  * @loader: A pixbuf loader.
613  *
614  * Queries the #GdkPixbuf that a pixbuf loader is currently creating.
615  * In general it only makes sense to call this function after the
616  * "area-prepared" signal has been emitted by the loader; this means
617  * that enough data has been read to know the size of the image that
618  * will be allocated.  If the loader has not received enough data via
619  * gdk_pixbuf_loader_write(), then this function returns %NULL.  The
620  * returned pixbuf will be the same in all future calls to the loader,
621  * so simply calling g_object_ref() should be sufficient to continue
622  * using it.  Additionally, if the loader is an animation, it will
623  * return the "static image" of the animation
624  * (see gdk_pixbuf_animation_get_static_image()).
625  * 
626  * Return value: The #GdkPixbuf that the loader is creating, or %NULL if not
627  * enough data has been read to determine how to create the image buffer.
628  **/
629 GdkPixbuf *
630 gdk_pixbuf_loader_get_pixbuf (GdkPixbufLoader *loader)
631 {
632         GdkPixbufLoaderPrivate *priv;
633   
634         g_return_val_if_fail (GDK_IS_PIXBUF_LOADER (loader), NULL);
635   
636         priv = loader->priv;
637
638         if (priv->animation)
639                 return gdk_pixbuf_animation_get_static_image (priv->animation);
640         else
641                 return NULL;
642 }
643
644 /**
645  * gdk_pixbuf_loader_get_animation:
646  * @loader: A pixbuf loader
647  *
648  * Queries the #GdkPixbufAnimation that a pixbuf loader is currently creating.
649  * In general it only makes sense to call this function after the "area-prepared"
650  * signal has been emitted by the loader. If the loader doesn't have enough
651  * bytes yet (hasn't emitted the "area-prepared" signal) this function will 
652  * return %NULL.
653  *
654  * Return value: The #GdkPixbufAnimation that the loader is loading, or %NULL if
655  not enough data has been read to determine the information.
656 **/
657 GdkPixbufAnimation *
658 gdk_pixbuf_loader_get_animation (GdkPixbufLoader *loader)
659 {
660         GdkPixbufLoaderPrivate *priv;
661   
662         g_return_val_if_fail (GDK_IS_PIXBUF_LOADER (loader), NULL);
663   
664         priv = loader->priv;
665   
666         return priv->animation;
667 }
668
669 /**
670  * gdk_pixbuf_loader_close:
671  * @loader: A pixbuf loader.
672  * @error: return location for a #GError, or %NULL to ignore errors
673  *
674  * Informs a pixbuf loader that no further writes with
675  * gdk_pixbuf_loader_write() will occur, so that it can free its
676  * internal loading structures. Also, tries to parse any data that
677  * hasn't yet been parsed; if the remaining data is partial or
678  * corrupt, an error will be returned.  If %FALSE is returned, @error
679  * will be set to an error from the #GDK_PIXBUF_ERROR or #G_FILE_ERROR
680  * domains. If you're just cancelling a load rather than expecting it
681  * to be finished, passing %NULL for @error to ignore it is
682  * reasonable.
683  *
684  * Returns: %TRUE if all image data written so far was successfully
685             passed out via the update_area signal
686  **/
687 gboolean
688 gdk_pixbuf_loader_close (GdkPixbufLoader *loader,
689                          GError         **error)
690 {
691         GdkPixbufLoaderPrivate *priv;
692         gboolean retval = TRUE;
693   
694         g_return_val_if_fail (GDK_IS_PIXBUF_LOADER (loader), TRUE);
695         g_return_val_if_fail (error == NULL || *error == NULL, TRUE);
696   
697         priv = loader->priv;
698   
699         if (priv->closed)
700                 return TRUE;
701   
702         /* We have less the LOADER_HEADER_SIZE bytes in the image.  
703          * Flush it, and keep going. 
704          */
705         if (priv->image_module == NULL)
706                 {
707                         GError *tmp = NULL;
708                         gdk_pixbuf_loader_load_module (loader, NULL, &tmp);
709                         if (tmp != NULL)
710                                 {
711                                         g_propagate_error (error, tmp);
712                                         retval = FALSE;
713                                 }
714                 }  
715
716         if (priv->image_module && priv->image_module->stop_load && priv->context) 
717                 {
718                         GError *tmp = NULL;
719                         if (!priv->image_module->stop_load (priv->context, &tmp) || tmp)
720                                 {
721                                         /* don't call gdk_pixbuf_loader_ensure_error()
722                                          * here, since we might not get an error in the
723                                          * gdk_pixbuf_get_file_info() case
724                                          */
725                                         if (tmp) {
726                                                 if (error && *error == NULL)
727                                                         g_propagate_error (error, tmp);
728                                                 else
729                                                         g_error_free (tmp);
730                                         }
731                                         retval = FALSE;
732                                 }
733                 }
734   
735         priv->closed = TRUE;
736         if (priv->image_module && priv->holds_threadlock) {
737                 _gdk_pixbuf_unlock (priv->image_module);
738                 priv->holds_threadlock = FALSE;
739         }
740
741         if (priv->needs_scale) 
742                 {
743
744                         g_signal_emit (loader, pixbuf_loader_signals[AREA_PREPARED], 0);
745                         g_signal_emit (loader, pixbuf_loader_signals[AREA_UPDATED], 0, 
746                                        0, 0, priv->width, priv->height);
747                 }
748
749         
750         g_signal_emit (loader, pixbuf_loader_signals[CLOSED], 0);
751
752         return retval;
753 }
754
755 /**
756  * gdk_pixbuf_loader_get_format:
757  * @loader: A pixbuf loader.
758  *
759  * Obtains the available information about the format of the 
760  * currently loading image file.
761  *
762  * Returns: A #GdkPixbufFormat or %NULL. The return value is owned 
763  * by GdkPixbuf and should not be freed.
764  * 
765  * Since: 2.2
766  */
767 GdkPixbufFormat *
768 gdk_pixbuf_loader_get_format (GdkPixbufLoader *loader)
769 {
770         GdkPixbufLoaderPrivate *priv;
771   
772         g_return_val_if_fail (GDK_IS_PIXBUF_LOADER (loader), NULL);
773   
774         priv = loader->priv;
775
776         if (priv->image_module)
777                 return _gdk_pixbuf_get_format (priv->image_module);
778         else
779                 return NULL;
780 }
781
782
783 #define __GDK_PIXBUF_LOADER_C__
784 #include "gdk-pixbuf-aliasdef.c"
785