1 /* -*- Mode: C; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 8 -*- */
2 /* GdkPixbuf library - Progressive loader object
4 * Copyright (C) 1999 The Free Software Foundation
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>
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.
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.
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.
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"
46 static void gdk_pixbuf_loader_finalize (GObject *loader);
48 static guint pixbuf_loader_signals[LAST_SIGNAL] = { 0 };
52 #define LOADER_HEADER_SIZE 1024
56 GdkPixbufAnimation *animation;
58 gboolean holds_threadlock;
59 guchar header_buf[LOADER_HEADER_SIZE];
60 gint header_buf_offset;
61 GdkPixbufModule *image_module;
67 } GdkPixbufLoaderPrivate;
69 G_DEFINE_TYPE (GdkPixbufLoader, gdk_pixbuf_loader, G_TYPE_OBJECT)
72 gdk_pixbuf_loader_class_init (GdkPixbufLoaderClass *class)
74 GObjectClass *object_class;
76 object_class = (GObjectClass *) class;
78 object_class->finalize = gdk_pixbuf_loader_finalize;
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
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.
92 pixbuf_loader_signals[SIZE_PREPARED] =
93 g_signal_new ("size_prepared",
94 G_TYPE_FROM_CLASS (object_class),
96 G_STRUCT_OFFSET (GdkPixbufLoaderClass, size_prepared),
98 _gdk_pixbuf_marshal_VOID__INT_INT,
104 * GdkPixbufLoader::area-prepared:
105 * @loader: the object which received the signal.
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.
112 pixbuf_loader_signals[AREA_PREPARED] =
113 g_signal_new ("area_prepared",
114 G_TYPE_FROM_CLASS (object_class),
116 G_STRUCT_OFFSET (GdkPixbufLoaderClass, area_prepared),
118 _gdk_pixbuf_marshal_VOID__VOID,
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.
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.
135 pixbuf_loader_signals[AREA_UPDATED] =
136 g_signal_new ("area_updated",
137 G_TYPE_FROM_CLASS (object_class),
139 G_STRUCT_OFFSET (GdkPixbufLoaderClass, area_updated),
141 _gdk_pixbuf_marshal_VOID__INT_INT_INT_INT,
149 * GdkPixbufLoader::closed:
150 * @loader: the object which received the signal.
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
157 pixbuf_loader_signals[CLOSED] =
158 g_signal_new ("closed",
159 G_TYPE_FROM_CLASS (object_class),
161 G_STRUCT_OFFSET (GdkPixbufLoaderClass, closed),
163 _gdk_pixbuf_marshal_VOID__VOID,
168 gdk_pixbuf_loader_init (GdkPixbufLoader *loader)
170 GdkPixbufLoaderPrivate *priv;
172 priv = g_new0 (GdkPixbufLoaderPrivate, 1);
180 gdk_pixbuf_loader_finalize (GObject *object)
182 GdkPixbufLoader *loader;
183 GdkPixbufLoaderPrivate *priv = NULL;
185 loader = GDK_PIXBUF_LOADER (object);
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);
195 g_object_unref (priv->animation);
199 G_OBJECT_CLASS (gdk_pixbuf_loader_parent_class)->finalize (object);
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.
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.
213 * Attempts to set the desired image size are ignored after the
214 * emission of the ::size_prepared signal.
219 gdk_pixbuf_loader_set_size (GdkPixbufLoader *loader,
223 GdkPixbufLoaderPrivate *priv = GDK_PIXBUF_LOADER (loader)->priv;
224 g_return_if_fail (width >= 0 && height >= 0);
226 if (!priv->size_fixed)
229 priv->height = height;
234 gdk_pixbuf_loader_size_func (gint *width, gint *height, gpointer loader)
236 GdkPixbufLoaderPrivate *priv = GDK_PIXBUF_LOADER (loader)->priv;
238 /* allow calling gdk_pixbuf_loader_set_size() before the signal */
239 if (priv->width == -1 && priv->height == -1)
241 priv->width = *width;
242 priv->height = *height;
245 g_signal_emit (loader, pixbuf_loader_signals[SIZE_PREPARED], 0, *width, *height);
246 priv->size_fixed = TRUE;
248 *width = priv->width;
249 *height = priv->height;
253 gdk_pixbuf_loader_prepare (GdkPixbuf *pixbuf,
254 GdkPixbufAnimation *anim,
257 GdkPixbufLoaderPrivate *priv = GDK_PIXBUF_LOADER (loader)->priv;
258 g_return_if_fail (pixbuf != NULL);
260 if (!priv->size_fixed)
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);
266 gdk_pixbuf_loader_size_func (&width, &height, loader);
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;
278 anim = gdk_pixbuf_non_anim_new (pixbuf);
280 priv->animation = anim;
282 if (!priv->needs_scale)
283 g_signal_emit (loader, pixbuf_loader_signals[AREA_PREPARED], 0);
287 gdk_pixbuf_loader_update (GdkPixbuf *pixbuf,
294 GdkPixbufLoaderPrivate *priv = GDK_PIXBUF_LOADER (loader)->priv;
296 if (!priv->needs_scale)
297 g_signal_emit (loader,
298 pixbuf_loader_signals[AREA_UPDATED],
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)));
307 gdk_pixbuf_loader_load_module (GdkPixbufLoader *loader,
308 const char *image_type,
311 GdkPixbufLoaderPrivate *priv = loader->priv;
315 priv->image_module = _gdk_pixbuf_get_named_module (image_type,
320 priv->image_module = _gdk_pixbuf_get_module (priv->header_buf,
321 priv->header_buf_offset,
326 if (priv->image_module == NULL)
329 if (priv->image_module->module == NULL)
330 if (!_gdk_pixbuf_load_module (priv->image_module, error))
333 if (priv->image_module->module == NULL)
336 if ((priv->image_module->begin_load == NULL) ||
337 (priv->image_module->stop_load == NULL) ||
338 (priv->image_module->load_increment == NULL))
342 GDK_PIXBUF_ERROR_UNSUPPORTED_OPERATION,
343 _("Incremental loading of image type '%s' is not supported"),
344 priv->image_module->module_name);
349 if (!priv->holds_threadlock) {
350 priv->holds_threadlock = _gdk_pixbuf_lock (priv->image_module);
353 priv->context = priv->image_module->begin_load (gdk_pixbuf_loader_size_func,
354 gdk_pixbuf_loader_prepare,
355 gdk_pixbuf_loader_update,
359 if (priv->context == NULL)
361 /* Defense against broken loaders; DO NOT take this as a GError
364 if (error && *error == NULL)
366 g_warning ("Bug! loader '%s' didn't set an error on failure",
367 priv->image_module->module_name);
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);
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;
389 gdk_pixbuf_loader_eat_header_write (GdkPixbufLoader *loader,
395 GdkPixbufLoaderPrivate *priv = loader->priv;
397 n_bytes = MIN(LOADER_HEADER_SIZE - priv->header_buf_offset, count);
398 memcpy (priv->header_buf + priv->header_buf_offset, buf, n_bytes);
400 priv->header_buf_offset += n_bytes;
402 if (priv->header_buf_offset >= LOADER_HEADER_SIZE)
404 if (gdk_pixbuf_loader_load_module (loader, NULL, error) == 0)
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
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.
425 * Return value: %TRUE if the write was successful, or %FALSE if the loader
426 * cannot parse the buffer.
429 gdk_pixbuf_loader_write (GdkPixbufLoader *loader,
434 GdkPixbufLoaderPrivate *priv;
436 g_return_val_if_fail (loader != NULL, FALSE);
437 g_return_val_if_fail (GDK_IS_PIXBUF_LOADER (loader), FALSE);
439 g_return_val_if_fail (buf != NULL, FALSE);
440 g_return_val_if_fail (error == NULL || *error == NULL, FALSE);
444 /* we expect it's not to be closed */
445 g_return_val_if_fail (priv->closed == FALSE, FALSE);
447 if (count > 0 && priv->image_module == NULL)
451 eaten = gdk_pixbuf_loader_eat_header_write (loader, buf, count, error);
459 if (count > 0 && priv->image_module->load_increment)
462 retval = priv->image_module->load_increment (priv->context, buf, count,
464 if (!retval && error && *error == NULL)
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);
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);
485 * gdk_pixbuf_loader_new:
487 * Creates a new pixbuf loader object.
489 * Return value: A newly-created pixbuf loader.
492 gdk_pixbuf_loader_new (void)
494 return g_object_new (GDK_TYPE_PIXBUF_LOADER, NULL);
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
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.
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().
515 * Return value: A newly-created pixbuf loader.
518 gdk_pixbuf_loader_new_with_type (const char *image_type,
521 GdkPixbufLoader *retval;
523 g_return_val_if_fail (error == NULL || *error == NULL, NULL);
525 retval = g_object_new (GDK_TYPE_PIXBUF_LOADER, NULL);
528 gdk_pixbuf_loader_load_module (retval, image_type, &tmp);
531 g_propagate_error (error, tmp);
532 gdk_pixbuf_loader_close (retval, NULL);
533 g_object_unref (retval);
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
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.
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().
559 * Return value: A newly-created pixbuf loader.
563 gdk_pixbuf_loader_new_with_mime_type (const char *mime_type,
566 const char * image_type = NULL;
569 GdkPixbufLoader *retval;
573 GdkPixbufFormat *info;
576 formats = gdk_pixbuf_get_formats ();
577 length = g_slist_length (formats);
579 for (i = 0; i < length && image_type == NULL; i++) {
580 info = (GdkPixbufFormat *)g_slist_nth_data (formats, i);
581 mimes = info->mime_types;
583 for (j = 0; mimes[j] != NULL; j++)
584 if (g_ascii_strcasecmp (mimes[j], mime_type) == 0) {
585 image_type = info->name;
590 g_slist_free (formats);
592 retval = g_object_new (GDK_TYPE_PIXBUF_LOADER, NULL);
595 gdk_pixbuf_loader_load_module (retval, image_type, &tmp);
598 g_propagate_error (error, tmp);
599 gdk_pixbuf_loader_close (retval, NULL);
600 g_object_unref (retval);
608 * gdk_pixbuf_loader_get_pixbuf:
609 * @loader: A pixbuf loader.
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()).
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.
627 gdk_pixbuf_loader_get_pixbuf (GdkPixbufLoader *loader)
629 GdkPixbufLoaderPrivate *priv;
631 g_return_val_if_fail (loader != NULL, NULL);
632 g_return_val_if_fail (GDK_IS_PIXBUF_LOADER (loader), NULL);
637 return gdk_pixbuf_animation_get_static_image (priv->animation);
643 * gdk_pixbuf_loader_get_animation:
644 * @loader: A pixbuf loader
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
652 * Return value: The #GdkPixbufAnimation that the loader is loading, or %NULL if
653 not enough data has been read to determine the information.
656 gdk_pixbuf_loader_get_animation (GdkPixbufLoader *loader)
658 GdkPixbufLoaderPrivate *priv;
660 g_return_val_if_fail (loader != NULL, NULL);
661 g_return_val_if_fail (GDK_IS_PIXBUF_LOADER (loader), NULL);
665 return priv->animation;
669 * gdk_pixbuf_loader_close:
670 * @loader: A pixbuf loader.
671 * @error: return location for a #GError, or %NULL to ignore errors
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
683 * Returns: %TRUE if all image data written so far was successfully
684 passed out via the update_area signal
687 gdk_pixbuf_loader_close (GdkPixbufLoader *loader,
690 GdkPixbufLoaderPrivate *priv;
691 gboolean retval = TRUE;
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);
699 /* we expect it's not closed */
700 g_return_val_if_fail (priv->closed == FALSE, TRUE);
702 /* We have less the LOADER_HEADER_SIZE bytes in the image.
703 * Flush it, and keep going.
705 if (priv->image_module == NULL)
708 gdk_pixbuf_loader_load_module (loader, NULL, &tmp);
711 g_propagate_error (error, tmp);
716 if (priv->image_module && priv->image_module->stop_load && priv->context)
718 if (!priv->image_module->stop_load (priv->context, error))
723 if (priv->image_module && priv->holds_threadlock) {
724 _gdk_pixbuf_unlock (priv->image_module);
725 priv->holds_threadlock = FALSE;
728 if (priv->needs_scale)
730 GdkPixbuf *tmp, *pixbuf;
732 tmp = gdk_pixbuf_animation_get_static_image (priv->animation);
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);
745 g_signal_emit (loader, pixbuf_loader_signals[AREA_UPDATED], 0,
746 0, 0, priv->width, priv->height);
750 g_signal_emit (loader, pixbuf_loader_signals[CLOSED], 0);
756 * gdk_pixbuf_loader_get_format:
757 * @loader: A pixbuf loader.
759 * Obtains the available information about the format of the
760 * currently loading image file.
762 * Returns: A #GdkPixbufFormat or %NULL. The return value is owned
763 * by GdkPixbuf and should not be freed.
768 gdk_pixbuf_loader_get_format (GdkPixbufLoader *loader)
770 GdkPixbufLoaderPrivate *priv;
772 g_return_val_if_fail (loader != NULL, NULL);
773 g_return_val_if_fail (GDK_IS_PIXBUF_LOADER (loader), NULL);
777 if (priv->image_module)
778 return _gdk_pixbuf_get_format (priv->image_module);
784 #define __GDK_PIXBUF_LOADER_C__
785 #include "gdk-pixbuf-aliasdef.c"