]> Pileus Git - ~andy/gtk/blob - gdk-pixbuf/gdk-pixbuf-loader.c
Build Wintab support always on Windows. Don't require the Wintab SDK.
[~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                                goto fail; 
459       
460                         count -= eaten;
461                         buf += eaten;
462                 }
463   
464         if (count > 0 && priv->image_module->load_increment)
465                 {
466                         if (!priv->image_module->load_increment (priv->context, buf, count,
467                                                                  error))
468                                 goto fail;
469                 }
470       
471         return TRUE;
472
473  fail:
474         gdk_pixbuf_loader_ensure_error (loader, error);
475         gdk_pixbuf_loader_close (loader, NULL);
476
477         return FALSE;
478 }
479
480 /**
481  * gdk_pixbuf_loader_new:
482  *
483  * Creates a new pixbuf loader object.
484  *
485  * Return value: A newly-created pixbuf loader.
486  **/
487 GdkPixbufLoader *
488 gdk_pixbuf_loader_new (void)
489 {
490         return g_object_new (GDK_TYPE_PIXBUF_LOADER, NULL);
491 }
492
493 /**
494  * gdk_pixbuf_loader_new_with_type:
495  * @image_type: name of the image format to be loaded with the image
496  * @error: return location for an allocated #GError, or %NULL to ignore errors
497  *
498  * Creates a new pixbuf loader object that always attempts to parse
499  * image data as if it were an image of type @image_type, instead of
500  * identifying the type automatically. Useful if you want an error if
501  * the image isn't the expected type, for loading image formats
502  * that can't be reliably identified by looking at the data, or if
503  * the user manually forces a specific type.
504  *
505  * The list of supported image formats depends on what image loaders
506  * are installed, but typically "png", "jpeg", "gif", "tiff" and 
507  * "xpm" are among the supported formats. To obtain the full list of
508  * supported image formats, call gdk_pixbuf_format_get_name() on each 
509  * of the #GdkPixbufFormat structs returned by gdk_pixbuf_get_formats().
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         g_return_val_if_fail (error == NULL || *error == NULL, NULL);
520   
521         retval = g_object_new (GDK_TYPE_PIXBUF_LOADER, NULL);
522
523         tmp = NULL;
524         gdk_pixbuf_loader_load_module (retval, image_type, &tmp);
525         if (tmp != NULL)
526                 {
527                         g_propagate_error (error, tmp);
528                         gdk_pixbuf_loader_close (retval, NULL);
529                         g_object_unref (retval);
530                         return NULL;
531                 }
532
533         return retval;
534 }
535
536 /**
537  * gdk_pixbuf_loader_new_with_mime_type:
538  * @mime_type: the mime type to be loaded 
539  * @error: return location for an allocated #GError, or %NULL to ignore errors
540  *
541  * Creates a new pixbuf loader object that always attempts to parse
542  * image data as if it were an image of mime type @mime_type, instead of
543  * identifying the type automatically. Useful if you want an error if
544  * the image isn't the expected mime type, for loading image formats
545  * that can't be reliably identified by looking at the data, or if
546  * the user manually forces a specific mime type.
547  *
548  * The list of supported mime types depends on what image loaders
549  * are installed, but typically "image/png", "image/jpeg", "image/gif", 
550  * "image/tiff" and "image/x-xpixmap" are among the supported mime types. 
551  * To obtain the full list of supported mime types, call 
552  * gdk_pixbuf_format_get_mime_types() on each of the #GdkPixbufFormat 
553  * structs returned by gdk_pixbuf_get_formats().
554  *
555  * Return value: A newly-created pixbuf loader.
556  * Since: 2.4
557  **/
558 GdkPixbufLoader *
559 gdk_pixbuf_loader_new_with_mime_type (const char *mime_type,
560                                       GError    **error)
561 {
562         const char * image_type = NULL;
563         char ** mimes;
564
565         GdkPixbufLoader *retval;
566         GError *tmp;
567   
568         GSList * formats;
569         GdkPixbufFormat *info;
570         int i, j, length;
571
572         formats = gdk_pixbuf_get_formats ();
573         length = g_slist_length (formats);
574
575         for (i = 0; i < length && image_type == NULL; i++) {
576                 info = (GdkPixbufFormat *)g_slist_nth_data (formats, i);
577                 mimes = info->mime_types;
578                 
579                 for (j = 0; mimes[j] != NULL; j++)
580                         if (g_ascii_strcasecmp (mimes[j], mime_type) == 0) {
581                                 image_type = info->name;
582                                 break;
583                         }
584         }
585
586         g_slist_free (formats);
587
588         retval = g_object_new (GDK_TYPE_PIXBUF_LOADER, NULL);
589
590         tmp = NULL;
591         gdk_pixbuf_loader_load_module (retval, image_type, &tmp);
592         if (tmp != NULL)
593                 {
594                         g_propagate_error (error, tmp);
595                         gdk_pixbuf_loader_close (retval, NULL);
596                         g_object_unref (retval);
597                         return NULL;
598                 }
599
600         return retval;
601 }
602
603 /**
604  * gdk_pixbuf_loader_get_pixbuf:
605  * @loader: A pixbuf loader.
606  *
607  * Queries the #GdkPixbuf that a pixbuf loader is currently creating.
608  * In general it only makes sense to call this function after the
609  * "area_prepared" signal has been emitted by the loader; this means
610  * that enough data has been read to know the size of the image that
611  * will be allocated.  If the loader has not received enough data via
612  * gdk_pixbuf_loader_write(), then this function returns %NULL.  The
613  * returned pixbuf will be the same in all future calls to the loader,
614  * so simply calling g_object_ref() should be sufficient to continue
615  * using it.  Additionally, if the loader is an animation, it will
616  * return the "static image" of the animation
617  * (see gdk_pixbuf_animation_get_static_image()).
618  * 
619  * Return value: The #GdkPixbuf that the loader is creating, or %NULL if not
620  * enough data has been read to determine how to create the image buffer.
621  **/
622 GdkPixbuf *
623 gdk_pixbuf_loader_get_pixbuf (GdkPixbufLoader *loader)
624 {
625         GdkPixbufLoaderPrivate *priv;
626   
627         g_return_val_if_fail (loader != NULL, NULL);
628         g_return_val_if_fail (GDK_IS_PIXBUF_LOADER (loader), NULL);
629   
630         priv = loader->priv;
631
632         if (priv->animation)
633                 return gdk_pixbuf_animation_get_static_image (priv->animation);
634         else
635                 return NULL;
636 }
637
638 /**
639  * gdk_pixbuf_loader_get_animation:
640  * @loader: A pixbuf loader
641  *
642  * Queries the #GdkPixbufAnimation that a pixbuf loader is currently creating.
643  * In general it only makes sense to call this function after the "area_prepared"
644  * signal has been emitted by the loader. If the loader doesn't have enough
645  * bytes yet (hasn't emitted the "area_prepared" signal) this function will 
646  * return %NULL.
647  *
648  * Return value: The #GdkPixbufAnimation that the loader is loading, or %NULL if
649  not enough data has been read to determine the information.
650 **/
651 GdkPixbufAnimation *
652 gdk_pixbuf_loader_get_animation (GdkPixbufLoader *loader)
653 {
654         GdkPixbufLoaderPrivate *priv;
655   
656         g_return_val_if_fail (loader != NULL, NULL);
657         g_return_val_if_fail (GDK_IS_PIXBUF_LOADER (loader), NULL);
658   
659         priv = loader->priv;
660   
661         return priv->animation;
662 }
663
664 /**
665  * gdk_pixbuf_loader_close:
666  * @loader: A pixbuf loader.
667  * @error: return location for a #GError, or %NULL to ignore errors
668  *
669  * Informs a pixbuf loader that no further writes with
670  * gdk_pixbuf_loader_write() will occur, so that it can free its
671  * internal loading structures. Also, tries to parse any data that
672  * hasn't yet been parsed; if the remaining data is partial or
673  * corrupt, an error will be returned.  If %FALSE is returned, @error
674  * will be set to an error from the #GDK_PIXBUF_ERROR or #G_FILE_ERROR
675  * domains. If you're just cancelling a load rather than expecting it
676  * to be finished, passing %NULL for @error to ignore it is
677  * reasonable.
678  *
679  * Returns: %TRUE if all image data written so far was successfully
680             passed out via the update_area signal
681  **/
682 gboolean
683 gdk_pixbuf_loader_close (GdkPixbufLoader *loader,
684                          GError         **error)
685 {
686         GdkPixbufLoaderPrivate *priv;
687         gboolean retval = TRUE;
688   
689         g_return_val_if_fail (loader != NULL, TRUE);
690         g_return_val_if_fail (GDK_IS_PIXBUF_LOADER (loader), TRUE);
691         g_return_val_if_fail (error == NULL || *error == NULL, TRUE);
692   
693         priv = loader->priv;
694   
695         if (priv->closed)
696                 return TRUE;
697   
698         /* We have less the LOADER_HEADER_SIZE bytes in the image.  
699          * Flush it, and keep going. 
700          */
701         if (priv->image_module == NULL)
702                 {
703                         GError *tmp = NULL;
704                         gdk_pixbuf_loader_load_module (loader, NULL, &tmp);
705                         if (tmp != NULL)
706                                 {
707                                         g_propagate_error (error, tmp);
708                                         retval = FALSE;
709                                 }
710                 }  
711
712         if (priv->image_module && priv->image_module->stop_load && priv->context) 
713                 {
714                         if (!priv->image_module->stop_load (priv->context, error))
715                                 {
716                                         gdk_pixbuf_loader_ensure_error (loader, error);
717                                         retval = FALSE;
718                                 }
719                 }
720   
721         priv->closed = TRUE;
722         if (priv->image_module && priv->holds_threadlock) {
723                 _gdk_pixbuf_unlock (priv->image_module);
724                 priv->holds_threadlock = FALSE;
725         }
726
727         if (priv->needs_scale) 
728                 {
729                         GdkPixbuf *tmp, *pixbuf;
730                         
731                         tmp = gdk_pixbuf_animation_get_static_image (priv->animation);
732                         g_object_ref (tmp);
733                         pixbuf = gdk_pixbuf_new (GDK_COLORSPACE_RGB, tmp->has_alpha, 8, priv->width, priv->height);
734                         g_object_unref (priv->animation);
735                         priv->animation = gdk_pixbuf_non_anim_new (pixbuf);
736                         g_object_unref (pixbuf);
737                         g_signal_emit (loader, pixbuf_loader_signals[AREA_PREPARED], 0);
738                         gdk_pixbuf_scale (tmp, pixbuf, 0, 0, priv->width, priv->height, 0, 0,
739                                           (double) priv->width / tmp->width,
740                                           (double) priv->height / tmp->height,
741                                           GDK_INTERP_BILINEAR); 
742                         g_object_unref (tmp);
743                         
744                         g_signal_emit (loader, pixbuf_loader_signals[AREA_UPDATED], 0, 
745                                        0, 0, priv->width, priv->height);
746                 }
747
748         
749         g_signal_emit (loader, pixbuf_loader_signals[CLOSED], 0);
750
751         return retval;
752 }
753
754 /**
755  * gdk_pixbuf_loader_get_format:
756  * @loader: A pixbuf loader.
757  *
758  * Obtains the available information about the format of the 
759  * currently loading image file.
760  *
761  * Returns: A #GdkPixbufFormat or %NULL. The return value is owned 
762  * by GdkPixbuf and should not be freed.
763  * 
764  * Since: 2.2
765  */
766 GdkPixbufFormat *
767 gdk_pixbuf_loader_get_format (GdkPixbufLoader *loader)
768 {
769         GdkPixbufLoaderPrivate *priv;
770   
771         g_return_val_if_fail (loader != NULL, NULL);
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