]> Pileus Git - ~andy/gtk/blob - gdk-pixbuf/gdk-pixbuf-loader.c
2.9.1
[~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 static gint
307 gdk_pixbuf_loader_load_module (GdkPixbufLoader *loader,
308                                const char      *image_type,
309                                GError         **error)
310 {
311         GdkPixbufLoaderPrivate *priv = loader->priv;
312
313         if (image_type)
314                 {
315                         priv->image_module = _gdk_pixbuf_get_named_module (image_type,
316                                                                            error);
317                 }
318         else
319                 {
320                         priv->image_module = _gdk_pixbuf_get_module (priv->header_buf,
321                                                                      priv->header_buf_offset,
322                                                                      NULL,
323                                                                      error);
324                 }
325   
326         if (priv->image_module == NULL)
327                 return 0;
328   
329         if (priv->image_module->module == NULL)
330                 if (!_gdk_pixbuf_load_module (priv->image_module, error))
331                         return 0;
332   
333         if (priv->image_module->module == NULL)
334                 return 0;
335   
336         if ((priv->image_module->begin_load == NULL) ||
337             (priv->image_module->stop_load == NULL) ||
338             (priv->image_module->load_increment == NULL))
339                 {
340                         g_set_error (error,
341                                      GDK_PIXBUF_ERROR,
342                                      GDK_PIXBUF_ERROR_UNSUPPORTED_OPERATION,
343                                      _("Incremental loading of image type '%s' is not supported"),
344                                      priv->image_module->module_name);
345
346                         return 0;
347                 }
348
349         if (!priv->holds_threadlock) {
350                 priv->holds_threadlock = _gdk_pixbuf_lock (priv->image_module);
351         }
352
353         priv->context = priv->image_module->begin_load (gdk_pixbuf_loader_size_func,
354                                                         gdk_pixbuf_loader_prepare,
355                                                         gdk_pixbuf_loader_update,
356                                                         loader,
357                                                         error);
358   
359         if (priv->context == NULL)
360                 {
361                         /* Defense against broken loaders; DO NOT take this as a GError
362                          * example
363                          */
364                         if (error && *error == NULL)
365                                 {
366                                         g_warning ("Bug! loader '%s' didn't set an error on failure",
367                                                    priv->image_module->module_name);
368                                         g_set_error (error,
369                                                      GDK_PIXBUF_ERROR,
370                                                      GDK_PIXBUF_ERROR_FAILED,
371                                                      _("Internal error: Image loader module '%s'"
372                                                        " failed to begin loading an image, but didn't"
373                                                        " give a reason for the failure"),
374                                                      priv->image_module->module_name);
375
376                                 }
377       
378                         return 0;
379                 }
380   
381         if (priv->header_buf_offset
382             && priv->image_module->load_increment (priv->context, priv->header_buf, priv->header_buf_offset, error))
383                 return priv->header_buf_offset;
384   
385         return 0;
386 }
387
388 static int
389 gdk_pixbuf_loader_eat_header_write (GdkPixbufLoader *loader,
390                                     const guchar    *buf,
391                                     gsize            count,
392                                     GError         **error)
393 {
394         gint n_bytes;
395         GdkPixbufLoaderPrivate *priv = loader->priv;
396   
397         n_bytes = MIN(LOADER_HEADER_SIZE - priv->header_buf_offset, count);
398         memcpy (priv->header_buf + priv->header_buf_offset, buf, n_bytes);
399   
400         priv->header_buf_offset += n_bytes;
401   
402         if (priv->header_buf_offset >= LOADER_HEADER_SIZE)
403                 {
404                         if (gdk_pixbuf_loader_load_module (loader, NULL, error) == 0)
405                                 return 0;
406                 }
407   
408         return n_bytes;
409 }
410
411 /**
412  * gdk_pixbuf_loader_write:
413  * @loader: A pixbuf loader.
414  * @buf: Pointer to image data.
415  * @count: Length of the @buf buffer in bytes.
416  * @error: return location for errors
417  *
418  * This will cause a pixbuf loader to parse the next @count bytes of
419  * an image.  It will return %TRUE if the data was loaded successfully,
420  * and %FALSE if an error occurred.  In the latter case, the loader
421  * will be closed, and will not accept further writes. If %FALSE is
422  * returned, @error will be set to an error from the #GDK_PIXBUF_ERROR
423  * or #G_FILE_ERROR domains.
424  *
425  * Return value: %TRUE if the write was successful, or %FALSE if the loader
426  * cannot parse the buffer.
427  **/
428 gboolean
429 gdk_pixbuf_loader_write (GdkPixbufLoader *loader,
430                          const guchar    *buf,
431                          gsize            count,
432                          GError         **error)
433 {
434         GdkPixbufLoaderPrivate *priv;
435   
436         g_return_val_if_fail (loader != NULL, FALSE);
437         g_return_val_if_fail (GDK_IS_PIXBUF_LOADER (loader), FALSE);
438   
439         g_return_val_if_fail (buf != NULL, FALSE);
440         g_return_val_if_fail (error == NULL || *error == NULL, FALSE);
441   
442         priv = loader->priv;
443
444         /* we expect it's not to be closed */
445         g_return_val_if_fail (priv->closed == FALSE, FALSE);
446   
447         if (count > 0 && priv->image_module == NULL)
448                 {
449                         gint eaten;
450       
451                         eaten = gdk_pixbuf_loader_eat_header_write (loader, buf, count, error);
452                         if (eaten <= 0)
453                                 return FALSE;
454       
455                         count -= eaten;
456                         buf += eaten;
457                 }
458   
459         if (count > 0 && priv->image_module->load_increment)
460                 {
461                         gboolean retval;
462                         retval = priv->image_module->load_increment (priv->context, buf, count,
463                                                                      error);
464                         if (!retval && error && *error == NULL)
465                                 {
466                                         /* Fix up busted image loader */
467                                         g_warning ("Bug! loader '%s' didn't set an error on failure",
468                                                    priv->image_module->module_name);
469                                         g_set_error (error,
470                                                      GDK_PIXBUF_ERROR,
471                                                      GDK_PIXBUF_ERROR_FAILED,
472                                                      _("Internal error: Image loader module '%s'"
473                                                        " failed to begin loading an image, but didn't"
474                                                        " give a reason for the failure"),
475                                                      priv->image_module->module_name);
476                                 }
477
478                         return retval;
479                 }
480       
481         return TRUE;
482 }
483
484 /**
485  * gdk_pixbuf_loader_new:
486  *
487  * Creates a new pixbuf loader object.
488  *
489  * Return value: A newly-created pixbuf loader.
490  **/
491 GdkPixbufLoader *
492 gdk_pixbuf_loader_new (void)
493 {
494         return g_object_new (GDK_TYPE_PIXBUF_LOADER, NULL);
495 }
496
497 /**
498  * gdk_pixbuf_loader_new_with_type:
499  * @image_type: name of the image format to be loaded with the image
500  * @error: return location for an allocated #GError, or %NULL to ignore errors
501  *
502  * Creates a new pixbuf loader object that always attempts to parse
503  * image data as if it were an image of type @image_type, instead of
504  * identifying the type automatically. Useful if you want an error if
505  * the image isn't the expected type, for loading image formats
506  * that can't be reliably identified by looking at the data, or if
507  * the user manually forces a specific type.
508  *
509  * The list of supported image formats depends on what image loaders
510  * are installed, but typically "png", "jpeg", "gif", "tiff" and 
511  * "xpm" are among the supported formats. To obtain the full list of
512  * supported image formats, call gdk_pixbuf_format_get_name() on each 
513  * of the #GdkPixbufFormat structs returned by gdk_pixbuf_get_formats().
514  *
515  * Return value: A newly-created pixbuf loader.
516  **/
517 GdkPixbufLoader *
518 gdk_pixbuf_loader_new_with_type (const char *image_type,
519                                  GError    **error)
520 {
521         GdkPixbufLoader *retval;
522         GError *tmp;
523         g_return_val_if_fail (error == NULL || *error == NULL, NULL);
524   
525         retval = g_object_new (GDK_TYPE_PIXBUF_LOADER, NULL);
526
527         tmp = NULL;
528         gdk_pixbuf_loader_load_module (retval, image_type, &tmp);
529         if (tmp != NULL)
530                 {
531                         g_propagate_error (error, tmp);
532                         gdk_pixbuf_loader_close (retval, NULL);
533                         g_object_unref (retval);
534                         return NULL;
535                 }
536
537         return retval;
538 }
539
540 /**
541  * gdk_pixbuf_loader_new_with_mime_type:
542  * @mime_type: the mime type to be loaded 
543  * @error: return location for an allocated #GError, or %NULL to ignore errors
544  *
545  * Creates a new pixbuf loader object that always attempts to parse
546  * image data as if it were an image of mime type @mime_type, instead of
547  * identifying the type automatically. Useful if you want an error if
548  * the image isn't the expected mime type, for loading image formats
549  * that can't be reliably identified by looking at the data, or if
550  * the user manually forces a specific mime type.
551  *
552  * The list of supported mime types depends on what image loaders
553  * are installed, but typically "image/png", "image/jpeg", "image/gif", 
554  * "image/tiff" and "image/x-xpixmap" are among the supported mime types. 
555  * To obtain the full list of supported mime types, call 
556  * gdk_pixbuf_format_get_mime_types() on each of the #GdkPixbufFormat 
557  * structs returned by gdk_pixbuf_get_formats().
558  *
559  * Return value: A newly-created pixbuf loader.
560  * Since: 2.4
561  **/
562 GdkPixbufLoader *
563 gdk_pixbuf_loader_new_with_mime_type (const char *mime_type,
564                                       GError    **error)
565 {
566         const char * image_type = NULL;
567         char ** mimes;
568
569         GdkPixbufLoader *retval;
570         GError *tmp;
571   
572         GSList * formats;
573         GdkPixbufFormat *info;
574         int i, j, length;
575
576         formats = gdk_pixbuf_get_formats ();
577         length = g_slist_length (formats);
578
579         for (i = 0; i < length && image_type == NULL; i++) {
580                 info = (GdkPixbufFormat *)g_slist_nth_data (formats, i);
581                 mimes = info->mime_types;
582                 
583                 for (j = 0; mimes[j] != NULL; j++)
584                         if (g_ascii_strcasecmp (mimes[j], mime_type) == 0) {
585                                 image_type = info->name;
586                                 break;
587                         }
588         }
589
590         g_slist_free (formats);
591
592         retval = g_object_new (GDK_TYPE_PIXBUF_LOADER, NULL);
593
594         tmp = NULL;
595         gdk_pixbuf_loader_load_module (retval, image_type, &tmp);
596         if (tmp != NULL)
597                 {
598                         g_propagate_error (error, tmp);
599                         gdk_pixbuf_loader_close (retval, NULL);
600                         g_object_unref (retval);
601                         return NULL;
602                 }
603
604         return retval;
605 }
606
607 /**
608  * gdk_pixbuf_loader_get_pixbuf:
609  * @loader: A pixbuf loader.
610  *
611  * Queries the #GdkPixbuf that a pixbuf loader is currently creating.
612  * In general it only makes sense to call this function after the
613  * "area_prepared" signal has been emitted by the loader; this means
614  * that enough data has been read to know the size of the image that
615  * will be allocated.  If the loader has not received enough data via
616  * gdk_pixbuf_loader_write(), then this function returns %NULL.  The
617  * returned pixbuf will be the same in all future calls to the loader,
618  * so simply calling g_object_ref() should be sufficient to continue
619  * using it.  Additionally, if the loader is an animation, it will
620  * return the "static image" of the animation
621  * (see gdk_pixbuf_animation_get_static_image()).
622  * 
623  * Return value: The #GdkPixbuf that the loader is creating, or %NULL if not
624  * enough data has been read to determine how to create the image buffer.
625  **/
626 GdkPixbuf *
627 gdk_pixbuf_loader_get_pixbuf (GdkPixbufLoader *loader)
628 {
629         GdkPixbufLoaderPrivate *priv;
630   
631         g_return_val_if_fail (loader != NULL, NULL);
632         g_return_val_if_fail (GDK_IS_PIXBUF_LOADER (loader), NULL);
633   
634         priv = loader->priv;
635
636         if (priv->animation)
637                 return gdk_pixbuf_animation_get_static_image (priv->animation);
638         else
639                 return NULL;
640 }
641
642 /**
643  * gdk_pixbuf_loader_get_animation:
644  * @loader: A pixbuf loader
645  *
646  * Queries the #GdkPixbufAnimation that a pixbuf loader is currently creating.
647  * In general it only makes sense to call this function after the "area_prepared"
648  * signal has been emitted by the loader. If the loader doesn't have enough
649  * bytes yet (hasn't emitted the "area_prepared" signal) this function will 
650  * return %NULL.
651  *
652  * Return value: The #GdkPixbufAnimation that the loader is loading, or %NULL if
653  not enough data has been read to determine the information.
654 **/
655 GdkPixbufAnimation *
656 gdk_pixbuf_loader_get_animation (GdkPixbufLoader *loader)
657 {
658         GdkPixbufLoaderPrivate *priv;
659   
660         g_return_val_if_fail (loader != NULL, NULL);
661         g_return_val_if_fail (GDK_IS_PIXBUF_LOADER (loader), NULL);
662   
663         priv = loader->priv;
664   
665         return priv->animation;
666 }
667
668 /**
669  * gdk_pixbuf_loader_close:
670  * @loader: A pixbuf loader.
671  * @error: return location for a #GError, or %NULL to ignore errors
672  *
673  * Informs a pixbuf loader that no further writes with
674  * gdk_pixbuf_loader_write() will occur, so that it can free its
675  * internal loading structures. Also, tries to parse any data that
676  * hasn't yet been parsed; if the remaining data is partial or
677  * corrupt, an error will be returned.  If %FALSE is returned, @error
678  * will be set to an error from the #GDK_PIXBUF_ERROR or #G_FILE_ERROR
679  * domains. If you're just cancelling a load rather than expecting it
680  * to be finished, passing %NULL for @error to ignore it is
681  * reasonable.
682  *
683  * Returns: %TRUE if all image data written so far was successfully
684             passed out via the update_area signal
685  **/
686 gboolean
687 gdk_pixbuf_loader_close (GdkPixbufLoader *loader,
688                          GError         **error)
689 {
690         GdkPixbufLoaderPrivate *priv;
691         gboolean retval = TRUE;
692   
693         g_return_val_if_fail (loader != NULL, TRUE);
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         /* we expect it's not closed */
700         g_return_val_if_fail (priv->closed == FALSE, 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                         if (!priv->image_module->stop_load (priv->context, error))
719                                 retval = FALSE;
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