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.
29 #include "gdk-pixbuf-private.h"
30 #include "gdk-pixbuf-animation.h"
31 #include "gdk-pixbuf-io.h"
32 #include "gdk-pixbuf-loader.h"
33 #include "gdk-pixbuf-marshal.h"
44 static void gdk_pixbuf_loader_class_init (GdkPixbufLoaderClass *klass);
45 static void gdk_pixbuf_loader_init (GdkPixbufLoader *loader);
46 static void gdk_pixbuf_loader_finalize (GObject *loader);
48 static gpointer parent_class = NULL;
49 static guint pixbuf_loader_signals[LAST_SIGNAL] = { 0 };
54 #define LOADER_HEADER_SIZE 128
58 GdkPixbufAnimation *animation;
60 guchar header_buf[LOADER_HEADER_SIZE];
61 gint header_buf_offset;
62 GdkPixbufModule *image_module;
68 } GdkPixbufLoaderPrivate;
72 * gdk_pixbuf_loader_get_type:
75 * Registers the #GdkPixbufLoader class if necessary, and returns the type ID
78 * Return value: The type ID of the #GdkPixbufLoader class.
81 gdk_pixbuf_loader_get_type (void)
83 static GType loader_type = 0;
87 static const GTypeInfo loader_info = {
88 sizeof (GdkPixbufLoaderClass),
90 (GBaseFinalizeFunc) NULL,
91 (GClassInitFunc) gdk_pixbuf_loader_class_init,
92 NULL, /* class_finalize */
93 NULL, /* class_data */
94 sizeof (GdkPixbufLoader),
96 (GInstanceInitFunc) gdk_pixbuf_loader_init
99 loader_type = g_type_register_static (G_TYPE_OBJECT,
109 gdk_pixbuf_loader_class_init (GdkPixbufLoaderClass *class)
111 GObjectClass *object_class;
113 object_class = (GObjectClass *) class;
115 parent_class = g_type_class_peek_parent (class);
117 object_class->finalize = gdk_pixbuf_loader_finalize;
119 pixbuf_loader_signals[SIZE_PREPARED] =
120 g_signal_new ("size_prepared",
121 G_TYPE_FROM_CLASS (object_class),
123 G_STRUCT_OFFSET (GdkPixbufLoaderClass, size_prepared),
125 gdk_pixbuf_marshal_VOID__INT_INT,
130 pixbuf_loader_signals[AREA_PREPARED] =
131 g_signal_new ("area_prepared",
132 G_TYPE_FROM_CLASS (object_class),
134 G_STRUCT_OFFSET (GdkPixbufLoaderClass, area_prepared),
136 gdk_pixbuf_marshal_VOID__VOID,
139 pixbuf_loader_signals[AREA_UPDATED] =
140 g_signal_new ("area_updated",
141 G_TYPE_FROM_CLASS (object_class),
143 G_STRUCT_OFFSET (GdkPixbufLoaderClass, area_updated),
145 gdk_pixbuf_marshal_VOID__INT_INT_INT_INT,
152 pixbuf_loader_signals[CLOSED] =
153 g_signal_new ("closed",
154 G_TYPE_FROM_CLASS (object_class),
156 G_STRUCT_OFFSET (GdkPixbufLoaderClass, closed),
158 gdk_pixbuf_marshal_VOID__VOID,
163 gdk_pixbuf_loader_init (GdkPixbufLoader *loader)
165 GdkPixbufLoaderPrivate *priv;
167 priv = g_new0 (GdkPixbufLoaderPrivate, 1);
172 gdk_pixbuf_loader_finalize (GObject *object)
174 GdkPixbufLoader *loader;
175 GdkPixbufLoaderPrivate *priv = NULL;
177 loader = GDK_PIXBUF_LOADER (object);
181 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.");
184 g_object_unref (priv->animation);
188 G_OBJECT_CLASS (parent_class)->finalize (object);
192 * gdk_pixbuf_loader_set_size:
193 * @loader: A pixbuf loader.
194 * @width: The desired width of the image being loaded.
195 * @height: The desired height of the image being loaded.
197 * Causes the image to be scaled while it is loaded. The desired
198 * image size can be determined relative to the original size of
199 * the image by calling gdk_pixbuf_loader_set_size() from a
200 * signal handler for the ::size_prepared signal.
202 * Attempts to set the desired image size are ignored after the
203 * emission of the ::size_prepared signal.
206 gdk_pixbuf_loader_set_size (GdkPixbufLoader *loader,
210 GdkPixbufLoaderPrivate *priv = GDK_PIXBUF_LOADER (loader)->priv;
211 g_return_if_fail (width > 0 && height > 0);
213 if (!priv->size_fixed)
216 priv->height = height;
221 gdk_pixbuf_loader_size_func (gint *width, gint *height, gpointer loader)
223 GdkPixbufLoaderPrivate *priv = GDK_PIXBUF_LOADER (loader)->priv;
225 /* allow calling gdk_pixbuf_loader_set_size() before the signal */
226 if (priv->width == 0 && priv->height == 0)
228 priv->width = *width;
229 priv->height = *height;
232 g_signal_emit (loader, pixbuf_loader_signals[SIZE_PREPARED], 0, *width, *height);
233 priv->size_fixed = TRUE;
235 *width = priv->width;
236 *height = priv->height;
240 gdk_pixbuf_loader_prepare (GdkPixbuf *pixbuf,
241 GdkPixbufAnimation *anim,
244 GdkPixbufLoaderPrivate *priv = GDK_PIXBUF_LOADER (loader)->priv;
245 g_return_if_fail (pixbuf != NULL);
247 if (!priv->size_fixed)
249 /* Defend against lazy loaders which don't call size_func */
250 gint width = gdk_pixbuf_get_width (pixbuf);
251 gint height = gdk_pixbuf_get_height (pixbuf);
253 gdk_pixbuf_loader_size_func (&width, &height, loader);
256 priv->needs_scale = FALSE;
257 if (priv->width > 0 && priv->height > 0 &&
258 (priv->width != gdk_pixbuf_get_width (pixbuf) ||
259 priv->height != gdk_pixbuf_get_height (pixbuf)))
260 priv->needs_scale = TRUE;
265 anim = gdk_pixbuf_non_anim_new (pixbuf);
267 priv->animation = anim;
269 if (!priv->needs_scale)
270 g_signal_emit (loader, pixbuf_loader_signals[AREA_PREPARED], 0);
274 gdk_pixbuf_loader_update (GdkPixbuf *pixbuf,
281 GdkPixbufLoaderPrivate *priv = GDK_PIXBUF_LOADER (loader)->priv;
283 if (!priv->needs_scale)
284 g_signal_emit (loader,
285 pixbuf_loader_signals[AREA_UPDATED],
288 /* sanity check in here. Defend against an errant loader */
289 MIN (width, gdk_pixbuf_animation_get_width (priv->animation)),
290 MIN (height, gdk_pixbuf_animation_get_height (priv->animation)));
294 gdk_pixbuf_loader_load_module (GdkPixbufLoader *loader,
295 const char *image_type,
298 GdkPixbufLoaderPrivate *priv = loader->priv;
302 priv->image_module = _gdk_pixbuf_get_named_module (image_type,
307 priv->image_module = _gdk_pixbuf_get_module (priv->header_buf,
308 priv->header_buf_offset,
313 if (priv->image_module == NULL)
316 if (priv->image_module->module == NULL)
317 if (!_gdk_pixbuf_load_module (priv->image_module, error))
320 if (priv->image_module->module == NULL)
323 if ((priv->image_module->begin_load == NULL) ||
324 (priv->image_module->stop_load == NULL) ||
325 (priv->image_module->load_increment == NULL))
329 GDK_PIXBUF_ERROR_UNSUPPORTED_OPERATION,
330 _("Incremental loading of image type '%s' is not supported"),
331 priv->image_module->module_name);
336 priv->context = priv->image_module->begin_load (gdk_pixbuf_loader_size_func,
337 gdk_pixbuf_loader_prepare,
338 gdk_pixbuf_loader_update,
342 if (priv->context == NULL)
344 /* Defense against broken loaders; DO NOT take this as a GError
347 if (error && *error == NULL)
349 g_warning ("Bug! loader '%s' didn't set an error on failure",
350 priv->image_module->module_name);
353 GDK_PIXBUF_ERROR_FAILED,
354 _("Internal error: Image loader module '%s'"
355 " failed to begin loading an image, but didn't"
356 " give a reason for the failure"),
357 priv->image_module->module_name);
364 if (priv->header_buf_offset
365 && priv->image_module->load_increment (priv->context, priv->header_buf, priv->header_buf_offset, error))
366 return priv->header_buf_offset;
372 gdk_pixbuf_loader_eat_header_write (GdkPixbufLoader *loader,
378 GdkPixbufLoaderPrivate *priv = loader->priv;
380 n_bytes = MIN(LOADER_HEADER_SIZE - priv->header_buf_offset, count);
381 memcpy (priv->header_buf + priv->header_buf_offset, buf, n_bytes);
383 priv->header_buf_offset += n_bytes;
385 if (priv->header_buf_offset >= LOADER_HEADER_SIZE)
387 if (gdk_pixbuf_loader_load_module (loader, NULL, error) == 0)
395 * gdk_pixbuf_loader_write:
396 * @loader: A pixbuf loader.
397 * @buf: Pointer to image data.
398 * @count: Length of the @buf buffer in bytes.
399 * @error: return location for errors
401 * This will cause a pixbuf loader to parse the next @count bytes of
402 * an image. It will return %TRUE if the data was loaded successfully,
403 * and %FALSE if an error occurred. In the latter case, the loader
404 * will be closed, and will not accept further writes. If %FALSE is
405 * returned, @error will be set to an error from the #GDK_PIXBUF_ERROR
406 * or #G_FILE_ERROR domains.
408 * Return value: %TRUE if the write was successful, or %FALSE if the loader
409 * cannot parse the buffer.
412 gdk_pixbuf_loader_write (GdkPixbufLoader *loader,
417 GdkPixbufLoaderPrivate *priv;
419 g_return_val_if_fail (loader != NULL, FALSE);
420 g_return_val_if_fail (GDK_IS_PIXBUF_LOADER (loader), FALSE);
422 g_return_val_if_fail (buf != NULL, FALSE);
423 g_return_val_if_fail (count >= 0, FALSE);
427 /* we expect it's not to be closed */
428 g_return_val_if_fail (priv->closed == FALSE, FALSE);
430 if (priv->image_module == NULL)
434 eaten = gdk_pixbuf_loader_eat_header_write (loader, buf, count, error);
442 if (count > 0 && priv->image_module->load_increment)
445 retval = priv->image_module->load_increment (priv->context, buf, count,
447 if (!retval && error && *error == NULL)
449 /* Fix up busted image loader */
450 g_warning ("Bug! loader '%s' didn't set an error on failure",
451 priv->image_module->module_name);
454 GDK_PIXBUF_ERROR_FAILED,
455 _("Internal error: Image loader module '%s'"
456 " failed to begin loading an image, but didn't"
457 " give a reason for the failure"),
458 priv->image_module->module_name);
468 * gdk_pixbuf_loader_new:
470 * Creates a new pixbuf loader object.
472 * Return value: A newly-created pixbuf loader.
475 gdk_pixbuf_loader_new (void)
477 return g_object_new (GDK_TYPE_PIXBUF_LOADER, NULL);
481 * gdk_pixbuf_loader_new_with_type:
482 * @image_type: name of the image format to be loaded with the image
483 * @error: return location for an allocated #GError, or %NULL to ignore errors
485 * Creates a new pixbuf loader object that always attempts to parse
486 * image data as if it were an image of type @image_type, instead of
487 * identifying the type automatically. Useful if you want an error if
488 * the image isn't the expected type, for loading image formats
489 * that can't be reliably identified by looking at the data, or if
490 * the user manually forces a specific type.
492 * Return value: A newly-created pixbuf loader.
495 gdk_pixbuf_loader_new_with_type (const char *image_type,
498 GdkPixbufLoader *retval;
501 retval = g_object_new (GDK_TYPE_PIXBUF_LOADER, NULL);
504 gdk_pixbuf_loader_load_module(retval, image_type, &tmp);
507 g_propagate_error (error, tmp);
508 g_object_unref (retval);
516 * gdk_pixbuf_loader_get_pixbuf:
517 * @loader: A pixbuf loader.
519 * Queries the #GdkPixbuf that a pixbuf loader is currently creating.
520 * In general it only makes sense to call this function after the
521 * "area_prepared" signal has been emitted by the loader; this means
522 * that enough data has been read to know the size of the image that
523 * will be allocated. If the loader has not received enough data via
524 * gdk_pixbuf_loader_write(), then this function returns %NULL. The
525 * returned pixbuf will be the same in all future calls to the loader,
526 * so simply calling g_object_ref() should be sufficient to continue
527 * using it. Additionally, if the loader is an animation, it will
528 * return the "static image" of the animation
529 * (see gdk_pixbuf_animation_get_static_image()).
531 * Return value: The #GdkPixbuf that the loader is creating, or %NULL if not
532 * enough data has been read to determine how to create the image buffer.
535 gdk_pixbuf_loader_get_pixbuf (GdkPixbufLoader *loader)
537 GdkPixbufLoaderPrivate *priv;
539 g_return_val_if_fail (loader != NULL, NULL);
540 g_return_val_if_fail (GDK_IS_PIXBUF_LOADER (loader), NULL);
545 return gdk_pixbuf_animation_get_static_image (priv->animation);
551 * gdk_pixbuf_loader_get_animation:
552 * @loader: A pixbuf loader
554 * Queries the #GdkPixbufAnimation that a pixbuf loader is currently creating.
555 * In general it only makes sense to call this function after the "area_prepared"
556 * signal has been emitted by the loader. If the loader doesn't have enough
557 * bytes yet (hasn't emitted the "area_prepared" signal) this function will
560 * Return value: The #GdkPixbufAnimation that the loader is loading, or %NULL if
561 not enough data has been read to determine the information.
564 gdk_pixbuf_loader_get_animation (GdkPixbufLoader *loader)
566 GdkPixbufLoaderPrivate *priv;
568 g_return_val_if_fail (loader != NULL, NULL);
569 g_return_val_if_fail (GDK_IS_PIXBUF_LOADER (loader), NULL);
573 return priv->animation;
577 * gdk_pixbuf_loader_close:
578 * @loader: A pixbuf loader.
579 * @error: return location for a #GError, or %NULL to ignore errors
581 * Informs a pixbuf loader that no further writes with
582 * gdk_pixbuf_loader_write() will occur, so that it can free its
583 * internal loading structures. Also, tries to parse any data that
584 * hasn't yet been parsed; if the remaining data is partial or
585 * corrupt, an error will be returned. If %FALSE is returned, @error
586 * will be set to an error from the #GDK_PIXBUF_ERROR or #G_FILE_ERROR
587 * domains. If you're just cancelling a load rather than expecting it
588 * to be finished, passing %NULL for @error to ignore it is
591 * Returns: %TRUE if all image data written so far was successfully
592 passed out via the update_area signal
595 gdk_pixbuf_loader_close (GdkPixbufLoader *loader,
598 GdkPixbufLoaderPrivate *priv;
599 gboolean retval = TRUE;
601 g_return_val_if_fail (loader != NULL, TRUE);
602 g_return_val_if_fail (GDK_IS_PIXBUF_LOADER (loader), TRUE);
606 /* we expect it's not closed */
607 g_return_val_if_fail (priv->closed == FALSE, TRUE);
609 /* We have less the 128 bytes in the image. Flush it, and keep going. */
610 if (priv->image_module == NULL)
613 gdk_pixbuf_loader_load_module (loader, NULL, &tmp);
616 g_propagate_error (error, tmp);
621 if (priv->image_module && priv->image_module->stop_load && priv->context)
623 if (!priv->image_module->stop_load (priv->context, error))
629 if (priv->needs_scale)
631 GdkPixbuf *tmp, *pixbuf;
633 tmp = gdk_pixbuf_animation_get_static_image (priv->animation);
635 pixbuf = gdk_pixbuf_new (GDK_COLORSPACE_RGB, tmp->has_alpha, 8, priv->width, priv->height);
636 g_object_unref (priv->animation);
637 priv->animation = gdk_pixbuf_non_anim_new (pixbuf);
638 g_object_unref (pixbuf);
639 g_signal_emit (loader, pixbuf_loader_signals[AREA_PREPARED], 0);
640 gdk_pixbuf_scale (tmp, pixbuf, 0, 0, priv->width, priv->height, 0, 0,
641 (double) priv->width / tmp->width,
642 (double) priv->height / tmp->height,
643 GDK_INTERP_BILINEAR);
644 g_object_unref (tmp);
646 g_signal_emit (loader, pixbuf_loader_signals[AREA_UPDATED], 0,
647 0, 0, priv->width, priv->height);
651 g_signal_emit (loader, pixbuf_loader_signals[CLOSED], 0);
657 * gdk_pixbuf_loader_get_format:
658 * @loader: A pixbuf loader.
660 * Obtains the available information about the format of the
661 * currently loading image file.
663 * Returns: A #GdkPixbufFormat or %NULL. The return value is owned
664 * by GdkPixbuf and should not be freed.
667 gdk_pixbuf_loader_get_format (GdkPixbufLoader *loader)
669 GdkPixbufLoaderPrivate *priv;
671 g_return_val_if_fail (loader != NULL, NULL);
672 g_return_val_if_fail (GDK_IS_PIXBUF_LOADER (loader), NULL);
676 if (priv->image_module)
677 return _gdk_pixbuf_get_format (priv->image_module);