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-alias.h"
31 #include "gdk-pixbuf-private.h"
32 #include "gdk-pixbuf-animation.h"
33 #include "gdk-pixbuf-io.h"
34 #include "gdk-pixbuf-loader.h"
35 #include "gdk-pixbuf-marshal.h"
46 static void gdk_pixbuf_loader_class_init (GdkPixbufLoaderClass *klass);
47 static void gdk_pixbuf_loader_init (GdkPixbufLoader *loader);
48 static void gdk_pixbuf_loader_finalize (GObject *loader);
50 static gpointer parent_class = NULL;
51 static guint pixbuf_loader_signals[LAST_SIGNAL] = { 0 };
55 #define LOADER_HEADER_SIZE 128
59 GdkPixbufAnimation *animation;
61 gboolean holds_threadlock;
62 guchar header_buf[LOADER_HEADER_SIZE];
63 gint header_buf_offset;
64 GdkPixbufModule *image_module;
70 } GdkPixbufLoaderPrivate;
74 * gdk_pixbuf_loader_get_type:
76 * Registers the #GdkPixbufLoader class if necessary, and returns the type ID
79 * Return value: The type ID of the #GdkPixbufLoader class.
82 gdk_pixbuf_loader_get_type (void)
84 static GType loader_type = 0;
88 static const GTypeInfo loader_info = {
89 sizeof (GdkPixbufLoaderClass),
91 (GBaseFinalizeFunc) NULL,
92 (GClassInitFunc) gdk_pixbuf_loader_class_init,
93 NULL, /* class_finalize */
94 NULL, /* class_data */
95 sizeof (GdkPixbufLoader),
97 (GInstanceInitFunc) gdk_pixbuf_loader_init
100 loader_type = g_type_register_static (G_TYPE_OBJECT,
110 gdk_pixbuf_loader_class_init (GdkPixbufLoaderClass *class)
112 GObjectClass *object_class;
114 object_class = (GObjectClass *) class;
116 parent_class = g_type_class_peek_parent (class);
118 object_class->finalize = gdk_pixbuf_loader_finalize;
121 * GdkPixbufLoader::size-prepared:
122 * @loader: the object which received the signal.
123 * @width: the original width of the image
124 * @height: the original height of the image
126 * This signal is emitted when the pixbuf loader has been fed the
127 * initial amount of data that is required to figure out the size
128 * of the image that it will create. Applications can call
129 * gdk_pixbuf_loader_set_size() in response to this signal to set
130 * the desired size to which the image should be scaled.
132 pixbuf_loader_signals[SIZE_PREPARED] =
133 g_signal_new ("size_prepared",
134 G_TYPE_FROM_CLASS (object_class),
136 G_STRUCT_OFFSET (GdkPixbufLoaderClass, size_prepared),
138 _gdk_pixbuf_marshal_VOID__INT_INT,
144 * GdkPixbufLoader::area-prepared:
145 * @loader: the object which received the signal.
147 * This signal is emitted when the pixbuf loader has allocated the
148 * pixbuf in the desired size. After this signal is emitted,
149 * applications can call gdk_pixbuf_loader_get_pixbuf() to fetch
150 * the partially-loaded pixbuf.
152 pixbuf_loader_signals[AREA_PREPARED] =
153 g_signal_new ("area_prepared",
154 G_TYPE_FROM_CLASS (object_class),
156 G_STRUCT_OFFSET (GdkPixbufLoaderClass, area_prepared),
158 _gdk_pixbuf_marshal_VOID__VOID,
162 * GdkPixbufLoader::area-updated:
163 * @loader: the object which received the signal.
164 * @x: X offset of upper-left corner of the updated area.
165 * @y: Y offset of upper-left corner of the updated area.
166 * @width: Width of updated area.
167 * @height: Height of updated area.
169 * This signal is emitted when a significant area of the image being
170 * loaded has been updated. Normally it means that a complete
171 * scanline has been read in, but it could be a different area as
172 * well. Applications can use this signal to know when to repaint
173 * areas of an image that is being loaded.
175 pixbuf_loader_signals[AREA_UPDATED] =
176 g_signal_new ("area_updated",
177 G_TYPE_FROM_CLASS (object_class),
179 G_STRUCT_OFFSET (GdkPixbufLoaderClass, area_updated),
181 _gdk_pixbuf_marshal_VOID__INT_INT_INT_INT,
189 * GdkPixbufLoader::closed:
190 * @loader: the object which received the signal.
192 * This signal is emitted when gdk_pixbuf_loader_close() is called.
193 * It can be used by different parts of an application to receive
194 * notification when an image loader is closed by the code that
197 pixbuf_loader_signals[CLOSED] =
198 g_signal_new ("closed",
199 G_TYPE_FROM_CLASS (object_class),
201 G_STRUCT_OFFSET (GdkPixbufLoaderClass, closed),
203 _gdk_pixbuf_marshal_VOID__VOID,
208 gdk_pixbuf_loader_init (GdkPixbufLoader *loader)
210 GdkPixbufLoaderPrivate *priv;
212 priv = g_new0 (GdkPixbufLoaderPrivate, 1);
220 gdk_pixbuf_loader_finalize (GObject *object)
222 GdkPixbufLoader *loader;
223 GdkPixbufLoaderPrivate *priv = NULL;
225 loader = GDK_PIXBUF_LOADER (object);
229 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.");
230 if (priv->holds_threadlock) {
231 _gdk_pixbuf_unlock (priv->image_module);
235 g_object_unref (priv->animation);
239 G_OBJECT_CLASS (parent_class)->finalize (object);
243 * gdk_pixbuf_loader_set_size:
244 * @loader: A pixbuf loader.
245 * @width: The desired width of the image being loaded.
246 * @height: The desired height of the image being loaded.
248 * Causes the image to be scaled while it is loaded. The desired
249 * image size can be determined relative to the original size of
250 * the image by calling gdk_pixbuf_loader_set_size() from a
251 * signal handler for the ::size_prepared signal.
253 * Attempts to set the desired image size are ignored after the
254 * emission of the ::size_prepared signal.
259 gdk_pixbuf_loader_set_size (GdkPixbufLoader *loader,
263 GdkPixbufLoaderPrivate *priv = GDK_PIXBUF_LOADER (loader)->priv;
264 g_return_if_fail (width >= 0 && height >= 0);
266 if (!priv->size_fixed)
269 priv->height = height;
274 gdk_pixbuf_loader_size_func (gint *width, gint *height, gpointer loader)
276 GdkPixbufLoaderPrivate *priv = GDK_PIXBUF_LOADER (loader)->priv;
278 /* allow calling gdk_pixbuf_loader_set_size() before the signal */
279 if (priv->width == -1 && priv->height == -1)
281 priv->width = *width;
282 priv->height = *height;
285 g_signal_emit (loader, pixbuf_loader_signals[SIZE_PREPARED], 0, *width, *height);
286 priv->size_fixed = TRUE;
288 *width = priv->width;
289 *height = priv->height;
293 gdk_pixbuf_loader_prepare (GdkPixbuf *pixbuf,
294 GdkPixbufAnimation *anim,
297 GdkPixbufLoaderPrivate *priv = GDK_PIXBUF_LOADER (loader)->priv;
298 g_return_if_fail (pixbuf != NULL);
300 if (!priv->size_fixed)
302 /* Defend against lazy loaders which don't call size_func */
303 gint width = gdk_pixbuf_get_width (pixbuf);
304 gint height = gdk_pixbuf_get_height (pixbuf);
306 gdk_pixbuf_loader_size_func (&width, &height, loader);
309 priv->needs_scale = FALSE;
310 if (priv->width > 0 && priv->height > 0 &&
311 (priv->width != gdk_pixbuf_get_width (pixbuf) ||
312 priv->height != gdk_pixbuf_get_height (pixbuf)))
313 priv->needs_scale = TRUE;
318 anim = gdk_pixbuf_non_anim_new (pixbuf);
320 priv->animation = anim;
322 if (!priv->needs_scale)
323 g_signal_emit (loader, pixbuf_loader_signals[AREA_PREPARED], 0);
327 gdk_pixbuf_loader_update (GdkPixbuf *pixbuf,
334 GdkPixbufLoaderPrivate *priv = GDK_PIXBUF_LOADER (loader)->priv;
336 if (!priv->needs_scale)
337 g_signal_emit (loader,
338 pixbuf_loader_signals[AREA_UPDATED],
341 /* sanity check in here. Defend against an errant loader */
342 MIN (width, gdk_pixbuf_animation_get_width (priv->animation)),
343 MIN (height, gdk_pixbuf_animation_get_height (priv->animation)));
347 gdk_pixbuf_loader_load_module (GdkPixbufLoader *loader,
348 const char *image_type,
351 GdkPixbufLoaderPrivate *priv = loader->priv;
355 priv->image_module = _gdk_pixbuf_get_named_module (image_type,
360 priv->image_module = _gdk_pixbuf_get_module (priv->header_buf,
361 priv->header_buf_offset,
366 if (priv->image_module == NULL)
369 if (priv->image_module->module == NULL)
370 if (!_gdk_pixbuf_load_module (priv->image_module, error))
373 if (priv->image_module->module == NULL)
376 if ((priv->image_module->begin_load == NULL) ||
377 (priv->image_module->stop_load == NULL) ||
378 (priv->image_module->load_increment == NULL))
382 GDK_PIXBUF_ERROR_UNSUPPORTED_OPERATION,
383 _("Incremental loading of image type '%s' is not supported"),
384 priv->image_module->module_name);
389 if (!priv->holds_threadlock) {
390 priv->holds_threadlock = _gdk_pixbuf_lock (priv->image_module);
393 priv->context = priv->image_module->begin_load (gdk_pixbuf_loader_size_func,
394 gdk_pixbuf_loader_prepare,
395 gdk_pixbuf_loader_update,
399 if (priv->context == NULL)
401 /* Defense against broken loaders; DO NOT take this as a GError
404 if (error && *error == NULL)
406 g_warning ("Bug! loader '%s' didn't set an error on failure",
407 priv->image_module->module_name);
410 GDK_PIXBUF_ERROR_FAILED,
411 _("Internal error: Image loader module '%s'"
412 " failed to begin loading an image, but didn't"
413 " give a reason for the failure"),
414 priv->image_module->module_name);
421 if (priv->header_buf_offset
422 && priv->image_module->load_increment (priv->context, priv->header_buf, priv->header_buf_offset, error))
423 return priv->header_buf_offset;
429 gdk_pixbuf_loader_eat_header_write (GdkPixbufLoader *loader,
435 GdkPixbufLoaderPrivate *priv = loader->priv;
437 n_bytes = MIN(LOADER_HEADER_SIZE - priv->header_buf_offset, count);
438 memcpy (priv->header_buf + priv->header_buf_offset, buf, n_bytes);
440 priv->header_buf_offset += n_bytes;
442 if (priv->header_buf_offset >= LOADER_HEADER_SIZE)
444 if (gdk_pixbuf_loader_load_module (loader, NULL, error) == 0)
452 * gdk_pixbuf_loader_write:
453 * @loader: A pixbuf loader.
454 * @buf: Pointer to image data.
455 * @count: Length of the @buf buffer in bytes.
456 * @error: return location for errors
458 * This will cause a pixbuf loader to parse the next @count bytes of
459 * an image. It will return %TRUE if the data was loaded successfully,
460 * and %FALSE if an error occurred. In the latter case, the loader
461 * will be closed, and will not accept further writes. If %FALSE is
462 * returned, @error will be set to an error from the #GDK_PIXBUF_ERROR
463 * or #G_FILE_ERROR domains.
465 * Return value: %TRUE if the write was successful, or %FALSE if the loader
466 * cannot parse the buffer.
469 gdk_pixbuf_loader_write (GdkPixbufLoader *loader,
474 GdkPixbufLoaderPrivate *priv;
476 g_return_val_if_fail (loader != NULL, FALSE);
477 g_return_val_if_fail (GDK_IS_PIXBUF_LOADER (loader), FALSE);
479 g_return_val_if_fail (buf != NULL, FALSE);
480 g_return_val_if_fail (count >= 0, FALSE);
481 g_return_val_if_fail (error == NULL || *error == NULL, FALSE);
485 /* we expect it's not to be closed */
486 g_return_val_if_fail (priv->closed == FALSE, FALSE);
488 if (priv->image_module == NULL)
492 eaten = gdk_pixbuf_loader_eat_header_write (loader, buf, count, error);
500 if (count > 0 && priv->image_module->load_increment)
503 retval = priv->image_module->load_increment (priv->context, buf, count,
505 if (!retval && error && *error == NULL)
507 /* Fix up busted image loader */
508 g_warning ("Bug! loader '%s' didn't set an error on failure",
509 priv->image_module->module_name);
512 GDK_PIXBUF_ERROR_FAILED,
513 _("Internal error: Image loader module '%s'"
514 " failed to begin loading an image, but didn't"
515 " give a reason for the failure"),
516 priv->image_module->module_name);
526 * gdk_pixbuf_loader_new:
528 * Creates a new pixbuf loader object.
530 * Return value: A newly-created pixbuf loader.
533 gdk_pixbuf_loader_new (void)
535 return g_object_new (GDK_TYPE_PIXBUF_LOADER, NULL);
539 * gdk_pixbuf_loader_new_with_type:
540 * @image_type: name of the image format to be loaded with the image
541 * @error: return location for an allocated #GError, or %NULL to ignore errors
543 * Creates a new pixbuf loader object that always attempts to parse
544 * image data as if it were an image of type @image_type, instead of
545 * identifying the type automatically. Useful if you want an error if
546 * the image isn't the expected type, for loading image formats
547 * that can't be reliably identified by looking at the data, or if
548 * the user manually forces a specific type.
550 * Return value: A newly-created pixbuf loader.
553 gdk_pixbuf_loader_new_with_type (const char *image_type,
556 GdkPixbufLoader *retval;
558 g_return_val_if_fail (error == NULL || *error == NULL, NULL);
560 retval = g_object_new (GDK_TYPE_PIXBUF_LOADER, NULL);
563 gdk_pixbuf_loader_load_module (retval, image_type, &tmp);
566 g_propagate_error (error, tmp);
567 g_object_unref (retval);
575 * gdk_pixbuf_loader_new_with_mime_type:
576 * @mime_type: the mime type to be loaded
577 * @error: return location for an allocated #GError, or %NULL to ignore errors
579 * Creates a new pixbuf loader object that always attempts to parse
580 * image data as if it were an image of mime type @mime_type, instead of
581 * identifying the type automatically. Useful if you want an error if
582 * the image isn't the expected mime type, for loading image formats
583 * that can't be reliably identified by looking at the data, or if
584 * the user manually forces a specific mime type.
586 * Return value: A newly-created pixbuf loader.
590 gdk_pixbuf_loader_new_with_mime_type (const char *mime_type,
593 const char * image_type = NULL;
596 GdkPixbufLoader *retval;
600 GdkPixbufFormat *info;
603 formats = gdk_pixbuf_get_formats ();
604 length = g_slist_length (formats);
606 for (i = 0; i < length && image_type == NULL; i++) {
607 info = (GdkPixbufFormat *)g_slist_nth_data (formats, i);
608 mimes = info->mime_types;
610 for (j = 0; mimes[j] != NULL; j++)
611 if (g_ascii_strcasecmp (mimes[j], mime_type) == 0) {
612 image_type = info->name;
617 g_slist_free (formats);
619 retval = g_object_new (GDK_TYPE_PIXBUF_LOADER, NULL);
622 gdk_pixbuf_loader_load_module(retval, image_type, &tmp);
625 g_propagate_error (error, tmp);
626 g_object_unref (retval);
634 * gdk_pixbuf_loader_get_pixbuf:
635 * @loader: A pixbuf loader.
637 * Queries the #GdkPixbuf that a pixbuf loader is currently creating.
638 * In general it only makes sense to call this function after the
639 * "area_prepared" signal has been emitted by the loader; this means
640 * that enough data has been read to know the size of the image that
641 * will be allocated. If the loader has not received enough data via
642 * gdk_pixbuf_loader_write(), then this function returns %NULL. The
643 * returned pixbuf will be the same in all future calls to the loader,
644 * so simply calling g_object_ref() should be sufficient to continue
645 * using it. Additionally, if the loader is an animation, it will
646 * return the "static image" of the animation
647 * (see gdk_pixbuf_animation_get_static_image()).
649 * Return value: The #GdkPixbuf that the loader is creating, or %NULL if not
650 * enough data has been read to determine how to create the image buffer.
653 gdk_pixbuf_loader_get_pixbuf (GdkPixbufLoader *loader)
655 GdkPixbufLoaderPrivate *priv;
657 g_return_val_if_fail (loader != NULL, NULL);
658 g_return_val_if_fail (GDK_IS_PIXBUF_LOADER (loader), NULL);
663 return gdk_pixbuf_animation_get_static_image (priv->animation);
669 * gdk_pixbuf_loader_get_animation:
670 * @loader: A pixbuf loader
672 * Queries the #GdkPixbufAnimation that a pixbuf loader is currently creating.
673 * In general it only makes sense to call this function after the "area_prepared"
674 * signal has been emitted by the loader. If the loader doesn't have enough
675 * bytes yet (hasn't emitted the "area_prepared" signal) this function will
678 * Return value: The #GdkPixbufAnimation that the loader is loading, or %NULL if
679 not enough data has been read to determine the information.
682 gdk_pixbuf_loader_get_animation (GdkPixbufLoader *loader)
684 GdkPixbufLoaderPrivate *priv;
686 g_return_val_if_fail (loader != NULL, NULL);
687 g_return_val_if_fail (GDK_IS_PIXBUF_LOADER (loader), NULL);
691 return priv->animation;
695 * gdk_pixbuf_loader_close:
696 * @loader: A pixbuf loader.
697 * @error: return location for a #GError, or %NULL to ignore errors
699 * Informs a pixbuf loader that no further writes with
700 * gdk_pixbuf_loader_write() will occur, so that it can free its
701 * internal loading structures. Also, tries to parse any data that
702 * hasn't yet been parsed; if the remaining data is partial or
703 * corrupt, an error will be returned. If %FALSE is returned, @error
704 * will be set to an error from the #GDK_PIXBUF_ERROR or #G_FILE_ERROR
705 * domains. If you're just cancelling a load rather than expecting it
706 * to be finished, passing %NULL for @error to ignore it is
709 * Returns: %TRUE if all image data written so far was successfully
710 passed out via the update_area signal
713 gdk_pixbuf_loader_close (GdkPixbufLoader *loader,
716 GdkPixbufLoaderPrivate *priv;
717 gboolean retval = TRUE;
719 g_return_val_if_fail (loader != NULL, TRUE);
720 g_return_val_if_fail (GDK_IS_PIXBUF_LOADER (loader), TRUE);
721 g_return_val_if_fail (error == NULL || *error == NULL, TRUE);
725 /* we expect it's not closed */
726 g_return_val_if_fail (priv->closed == FALSE, TRUE);
728 /* We have less the 128 bytes in the image. Flush it, and keep going. */
729 if (priv->image_module == NULL)
732 gdk_pixbuf_loader_load_module (loader, NULL, &tmp);
735 g_propagate_error (error, tmp);
740 if (priv->image_module && priv->image_module->stop_load && priv->context)
742 if (!priv->image_module->stop_load (priv->context, error))
747 if (priv->image_module && priv->holds_threadlock) {
748 _gdk_pixbuf_unlock (priv->image_module);
749 priv->holds_threadlock = FALSE;
752 if (priv->needs_scale)
754 GdkPixbuf *tmp, *pixbuf;
756 tmp = gdk_pixbuf_animation_get_static_image (priv->animation);
758 pixbuf = gdk_pixbuf_new (GDK_COLORSPACE_RGB, tmp->has_alpha, 8, priv->width, priv->height);
759 g_object_unref (priv->animation);
760 priv->animation = gdk_pixbuf_non_anim_new (pixbuf);
761 g_object_unref (pixbuf);
762 g_signal_emit (loader, pixbuf_loader_signals[AREA_PREPARED], 0);
763 gdk_pixbuf_scale (tmp, pixbuf, 0, 0, priv->width, priv->height, 0, 0,
764 (double) priv->width / tmp->width,
765 (double) priv->height / tmp->height,
766 GDK_INTERP_BILINEAR);
767 g_object_unref (tmp);
769 g_signal_emit (loader, pixbuf_loader_signals[AREA_UPDATED], 0,
770 0, 0, priv->width, priv->height);
774 g_signal_emit (loader, pixbuf_loader_signals[CLOSED], 0);
780 * gdk_pixbuf_loader_get_format:
781 * @loader: A pixbuf loader.
783 * Obtains the available information about the format of the
784 * currently loading image file.
786 * Returns: A #GdkPixbufFormat or %NULL. The return value is owned
787 * by GdkPixbuf and should not be freed.
792 gdk_pixbuf_loader_get_format (GdkPixbufLoader *loader)
794 GdkPixbufLoaderPrivate *priv;
796 g_return_val_if_fail (loader != NULL, NULL);
797 g_return_val_if_fail (GDK_IS_PIXBUF_LOADER (loader), NULL);
801 if (priv->image_module)
802 return _gdk_pixbuf_get_format (priv->image_module);