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)));
306 /* Defense against broken loaders; DO NOT take this as a GError example! */
308 gdk_pixbuf_loader_ensure_error (GdkPixbufLoader *loader,
311 GdkPixbufLoaderPrivate *priv = loader->priv;
313 if (error == NULL || *error != NULL)
316 g_warning ("Bug! loader '%s' didn't set an error on failure",
317 priv->image_module->module_name);
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"
324 priv->image_module->module_name);
328 gdk_pixbuf_loader_load_module (GdkPixbufLoader *loader,
329 const char *image_type,
332 GdkPixbufLoaderPrivate *priv = loader->priv;
336 priv->image_module = _gdk_pixbuf_get_named_module (image_type,
341 priv->image_module = _gdk_pixbuf_get_module (priv->header_buf,
342 priv->header_buf_offset,
347 if (priv->image_module == NULL)
350 if (priv->image_module->module == NULL)
351 if (!_gdk_pixbuf_load_module (priv->image_module, error))
354 if (priv->image_module->module == NULL)
357 if ((priv->image_module->begin_load == NULL) ||
358 (priv->image_module->stop_load == NULL) ||
359 (priv->image_module->load_increment == NULL))
363 GDK_PIXBUF_ERROR_UNSUPPORTED_OPERATION,
364 _("Incremental loading of image type '%s' is not supported"),
365 priv->image_module->module_name);
370 if (!priv->holds_threadlock) {
371 priv->holds_threadlock = _gdk_pixbuf_lock (priv->image_module);
374 priv->context = priv->image_module->begin_load (gdk_pixbuf_loader_size_func,
375 gdk_pixbuf_loader_prepare,
376 gdk_pixbuf_loader_update,
380 if (priv->context == NULL)
382 gdk_pixbuf_loader_ensure_error (loader, error);
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;
394 gdk_pixbuf_loader_eat_header_write (GdkPixbufLoader *loader,
400 GdkPixbufLoaderPrivate *priv = loader->priv;
402 n_bytes = MIN(LOADER_HEADER_SIZE - priv->header_buf_offset, count);
403 memcpy (priv->header_buf + priv->header_buf_offset, buf, n_bytes);
405 priv->header_buf_offset += n_bytes;
407 if (priv->header_buf_offset >= LOADER_HEADER_SIZE)
409 if (gdk_pixbuf_loader_load_module (loader, NULL, error) == 0)
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
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.
430 * Return value: %TRUE if the write was successful, or %FALSE if the loader
431 * cannot parse the buffer.
434 gdk_pixbuf_loader_write (GdkPixbufLoader *loader,
439 GdkPixbufLoaderPrivate *priv;
441 g_return_val_if_fail (loader != NULL, FALSE);
442 g_return_val_if_fail (GDK_IS_PIXBUF_LOADER (loader), FALSE);
444 g_return_val_if_fail (buf != NULL, FALSE);
445 g_return_val_if_fail (error == NULL || *error == NULL, FALSE);
449 /* we expect it's not to be closed */
450 g_return_val_if_fail (priv->closed == FALSE, FALSE);
452 if (count > 0 && priv->image_module == NULL)
456 eaten = gdk_pixbuf_loader_eat_header_write (loader, buf, count, error);
464 if (count > 0 && priv->image_module->load_increment)
466 if (!priv->image_module->load_increment (priv->context, buf, count,
474 gdk_pixbuf_loader_ensure_error (loader, error);
475 gdk_pixbuf_loader_close (loader, NULL);
481 * gdk_pixbuf_loader_new:
483 * Creates a new pixbuf loader object.
485 * Return value: A newly-created pixbuf loader.
488 gdk_pixbuf_loader_new (void)
490 return g_object_new (GDK_TYPE_PIXBUF_LOADER, NULL);
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
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.
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().
511 * Return value: A newly-created pixbuf loader.
514 gdk_pixbuf_loader_new_with_type (const char *image_type,
517 GdkPixbufLoader *retval;
519 g_return_val_if_fail (error == NULL || *error == NULL, NULL);
521 retval = g_object_new (GDK_TYPE_PIXBUF_LOADER, NULL);
524 gdk_pixbuf_loader_load_module (retval, image_type, &tmp);
527 g_propagate_error (error, tmp);
528 gdk_pixbuf_loader_close (retval, NULL);
529 g_object_unref (retval);
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
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.
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().
555 * Return value: A newly-created pixbuf loader.
559 gdk_pixbuf_loader_new_with_mime_type (const char *mime_type,
562 const char * image_type = NULL;
565 GdkPixbufLoader *retval;
569 GdkPixbufFormat *info;
572 formats = gdk_pixbuf_get_formats ();
573 length = g_slist_length (formats);
575 for (i = 0; i < length && image_type == NULL; i++) {
576 info = (GdkPixbufFormat *)g_slist_nth_data (formats, i);
577 mimes = info->mime_types;
579 for (j = 0; mimes[j] != NULL; j++)
580 if (g_ascii_strcasecmp (mimes[j], mime_type) == 0) {
581 image_type = info->name;
586 g_slist_free (formats);
588 retval = g_object_new (GDK_TYPE_PIXBUF_LOADER, NULL);
591 gdk_pixbuf_loader_load_module (retval, image_type, &tmp);
594 g_propagate_error (error, tmp);
595 gdk_pixbuf_loader_close (retval, NULL);
596 g_object_unref (retval);
604 * gdk_pixbuf_loader_get_pixbuf:
605 * @loader: A pixbuf loader.
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()).
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.
623 gdk_pixbuf_loader_get_pixbuf (GdkPixbufLoader *loader)
625 GdkPixbufLoaderPrivate *priv;
627 g_return_val_if_fail (loader != NULL, NULL);
628 g_return_val_if_fail (GDK_IS_PIXBUF_LOADER (loader), NULL);
633 return gdk_pixbuf_animation_get_static_image (priv->animation);
639 * gdk_pixbuf_loader_get_animation:
640 * @loader: A pixbuf loader
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
648 * Return value: The #GdkPixbufAnimation that the loader is loading, or %NULL if
649 not enough data has been read to determine the information.
652 gdk_pixbuf_loader_get_animation (GdkPixbufLoader *loader)
654 GdkPixbufLoaderPrivate *priv;
656 g_return_val_if_fail (loader != NULL, NULL);
657 g_return_val_if_fail (GDK_IS_PIXBUF_LOADER (loader), NULL);
661 return priv->animation;
665 * gdk_pixbuf_loader_close:
666 * @loader: A pixbuf loader.
667 * @error: return location for a #GError, or %NULL to ignore errors
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
679 * Returns: %TRUE if all image data written so far was successfully
680 passed out via the update_area signal
683 gdk_pixbuf_loader_close (GdkPixbufLoader *loader,
686 GdkPixbufLoaderPrivate *priv;
687 gboolean retval = TRUE;
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);
698 /* We have less the LOADER_HEADER_SIZE bytes in the image.
699 * Flush it, and keep going.
701 if (priv->image_module == NULL)
704 gdk_pixbuf_loader_load_module (loader, NULL, &tmp);
707 g_propagate_error (error, tmp);
712 if (priv->image_module && priv->image_module->stop_load && priv->context)
714 if (!priv->image_module->stop_load (priv->context, error))
716 gdk_pixbuf_loader_ensure_error (loader, error);
722 if (priv->image_module && priv->holds_threadlock) {
723 _gdk_pixbuf_unlock (priv->image_module);
724 priv->holds_threadlock = FALSE;
727 if (priv->needs_scale)
729 GdkPixbuf *tmp, *pixbuf;
731 tmp = gdk_pixbuf_animation_get_static_image (priv->animation);
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);
744 g_signal_emit (loader, pixbuf_loader_signals[AREA_UPDATED], 0,
745 0, 0, priv->width, priv->height);
749 g_signal_emit (loader, pixbuf_loader_signals[CLOSED], 0);
755 * gdk_pixbuf_loader_get_format:
756 * @loader: A pixbuf loader.
758 * Obtains the available information about the format of the
759 * currently loading image file.
761 * Returns: A #GdkPixbufFormat or %NULL. The return value is owned
762 * by GdkPixbuf and should not be freed.
767 gdk_pixbuf_loader_get_format (GdkPixbufLoader *loader)
769 GdkPixbufLoaderPrivate *priv;
771 g_return_val_if_fail (loader != NULL, NULL);
772 g_return_val_if_fail (GDK_IS_PIXBUF_LOADER (loader), NULL);
776 if (priv->image_module)
777 return _gdk_pixbuf_get_format (priv->image_module);
783 #define __GDK_PIXBUF_LOADER_C__
784 #include "gdk-pixbuf-aliasdef.c"