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