1 /* GdkPixbuf library - Progressive loader object
3 * Copyright (C) 1999 The Free Software Foundation
5 * Authors: Mark Crichton <crichton@gimp.org>
6 * Miguel de Icaza <miguel@gnu.org>
7 * Federico Mena-Quintero <federico@gimp.org>
8 * Jonathan Blandford <jrb@redhat.com>
10 * This library is free software; you can redistribute it and/or
11 * modify it under the terms of the GNU Lesser General Public
12 * License as published by the Free Software Foundation; either
13 * version 2 of the License, or (at your option) any later version.
15 * This library is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18 * Lesser General Public License for more details.
20 * You should have received a copy of the GNU Lesser General Public
21 * License along with this library; if not, write to the
22 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
23 * Boston, MA 02111-1307, USA.
28 #include "gdk-pixbuf-private.h"
29 #include "gdk-pixbuf-loader.h"
30 #include "gdk-pixbuf-io.h"
31 #include "gdk-pixbuf-marshal.h"
33 #include <gobject/gsignal.h>
45 static void gdk_pixbuf_loader_class_init (GdkPixbufLoaderClass *klass);
46 static void gdk_pixbuf_loader_init (GdkPixbufLoader *loader);
47 static void gdk_pixbuf_loader_finalize (GObject *loader);
49 static gpointer parent_class = NULL;
50 static guint pixbuf_loader_signals[LAST_SIGNAL] = { 0 };
55 #define LOADER_HEADER_SIZE 128
60 GdkPixbufAnimation *animation;
62 guchar header_buf[LOADER_HEADER_SIZE];
63 gint header_buf_offset;
64 GdkPixbufModule *image_module;
66 } GdkPixbufLoaderPrivate;
70 * gdk_pixbuf_loader_get_type:
73 * Registers the #GdkPixubfLoader class if necessary, and returns the type ID
76 * Return value: The type ID of the #GdkPixbufLoader class.
79 gdk_pixbuf_loader_get_type (void)
81 static GType loader_type = 0;
85 static const GTypeInfo loader_info = {
86 sizeof (GdkPixbufLoaderClass),
88 (GBaseFinalizeFunc) NULL,
89 (GClassInitFunc) gdk_pixbuf_loader_class_init,
90 NULL, /* class_finalize */
91 NULL, /* class_data */
92 sizeof (GdkPixbufLoader),
94 (GInstanceInitFunc) gdk_pixbuf_loader_init
97 loader_type = g_type_register_static (G_TYPE_OBJECT,
107 gdk_pixbuf_loader_class_init (GdkPixbufLoaderClass *class)
109 GObjectClass *object_class;
111 object_class = (GObjectClass *) class;
113 parent_class = g_type_class_peek_parent (class);
115 object_class->finalize = gdk_pixbuf_loader_finalize;
117 pixbuf_loader_signals[AREA_PREPARED] =
118 g_signal_newc ("area_prepared",
119 G_TYPE_FROM_CLASS (object_class),
121 G_STRUCT_OFFSET (GdkPixbufLoaderClass, area_prepared),
123 gdk_pixbuf_marshal_VOID__VOID,
126 pixbuf_loader_signals[AREA_UPDATED] =
127 g_signal_newc ("area_updated",
128 G_TYPE_FROM_CLASS (object_class),
130 G_STRUCT_OFFSET (GdkPixbufLoaderClass, area_updated),
132 gdk_pixbuf_marshal_VOID__INT_INT_INT_INT,
139 pixbuf_loader_signals[FRAME_DONE] =
140 g_signal_newc ("frame_done",
141 G_TYPE_FROM_CLASS (object_class),
143 G_STRUCT_OFFSET (GdkPixbufLoaderClass, frame_done),
145 gdk_pixbuf_marshal_VOID__POINTER,
147 GDK_TYPE_PIXBUF_FRAME);
149 pixbuf_loader_signals[ANIMATION_DONE] =
150 g_signal_newc ("animation_done",
151 G_TYPE_FROM_CLASS (object_class),
153 G_STRUCT_OFFSET (GdkPixbufLoaderClass, animation_done),
155 gdk_pixbuf_marshal_VOID__VOID,
158 pixbuf_loader_signals[CLOSED] =
159 g_signal_newc ("closed",
160 G_TYPE_FROM_CLASS (object_class),
162 G_STRUCT_OFFSET (GdkPixbufLoaderClass, closed),
164 gdk_pixbuf_marshal_VOID__VOID,
169 gdk_pixbuf_loader_init (GdkPixbufLoader *loader)
171 GdkPixbufLoaderPrivate *priv;
173 priv = g_new0 (GdkPixbufLoaderPrivate, 1);
178 gdk_pixbuf_loader_finalize (GObject *object)
180 GdkPixbufLoader *loader;
181 GdkPixbufLoaderPrivate *priv = NULL;
183 loader = GDK_PIXBUF_LOADER (object);
187 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 gdk_pixbuf_animation_unref (priv->animation);
193 gdk_pixbuf_unref (priv->pixbuf);
197 G_OBJECT_CLASS (parent_class)->finalize (object);
201 gdk_pixbuf_loader_prepare (GdkPixbuf *pixbuf,
204 GdkPixbufLoaderPrivate *priv = NULL;
206 priv = GDK_PIXBUF_LOADER (loader)->priv;
207 gdk_pixbuf_ref (pixbuf);
209 g_assert (priv->pixbuf == NULL);
211 priv->pixbuf = pixbuf;
212 g_signal_emit (G_OBJECT (loader), pixbuf_loader_signals[AREA_PREPARED], 0);
216 gdk_pixbuf_loader_update (GdkPixbuf *pixbuf,
223 GdkPixbufLoaderPrivate *priv = NULL;
225 priv = GDK_PIXBUF_LOADER (loader)->priv;
227 g_signal_emit (G_OBJECT (loader),
228 pixbuf_loader_signals[AREA_UPDATED],
231 /* sanity check in here. Defend against an errant loader */
232 MIN (width, gdk_pixbuf_get_width (priv->pixbuf)),
233 MIN (height, gdk_pixbuf_get_height (priv->pixbuf)));
237 gdk_pixbuf_loader_frame_done (GdkPixbufFrame *frame,
240 GdkPixbufLoaderPrivate *priv = NULL;
242 priv = GDK_PIXBUF_LOADER (loader)->priv;
246 if (priv->animation == NULL)
248 priv->animation = g_object_new (GDK_TYPE_PIXBUF_ANIMATION, NULL);
250 priv->animation->n_frames = 0;
251 priv->animation->width = gdk_pixbuf_get_width (frame->pixbuf) + frame->x_offset;
252 priv->animation->height = gdk_pixbuf_get_height (frame->pixbuf) + frame->y_offset;
258 /* update bbox size */
259 w = gdk_pixbuf_get_width (frame->pixbuf) + frame->x_offset;
260 h = gdk_pixbuf_get_height (frame->pixbuf) + frame->y_offset;
262 if (w > priv->animation->width) {
263 priv->animation->width = w;
265 if (h > priv->animation->height) {
266 priv->animation->height = h;
270 priv->animation->frames = g_list_append (priv->animation->frames, frame);
271 priv->animation->n_frames++;
272 g_signal_emit (G_OBJECT (loader),
273 pixbuf_loader_signals[FRAME_DONE],
279 gdk_pixbuf_loader_animation_done (GdkPixbuf *pixbuf,
282 GdkPixbufLoaderPrivate *priv = NULL;
283 GdkPixbufFrame *frame;
284 GList *current = NULL;
287 priv = GDK_PIXBUF_LOADER (loader)->priv;
290 current = gdk_pixbuf_animation_get_frames (priv->animation);
294 frame = (GdkPixbufFrame *) current->data;
296 /* update bbox size */
297 w = gdk_pixbuf_get_width (frame->pixbuf) + frame->x_offset;
298 h = gdk_pixbuf_get_height (frame->pixbuf) + frame->y_offset;
300 if (w > priv->animation->width)
301 priv->animation->width = w;
302 if (h > priv->animation->height)
303 priv->animation->height = h;
304 current = current->next;
307 g_signal_emit (G_OBJECT (loader), pixbuf_loader_signals[ANIMATION_DONE], 0);
311 gdk_pixbuf_loader_load_module (GdkPixbufLoader *loader,
312 const char *image_type,
315 GdkPixbufLoaderPrivate *priv = loader->priv;
319 priv->image_module = _gdk_pixbuf_get_named_module (image_type,
324 g_return_val_if_fail (priv->header_buf_offset > 0, 0);
325 priv->image_module = _gdk_pixbuf_get_module (priv->header_buf,
326 priv->header_buf_offset,
331 if (priv->image_module == NULL)
334 if (priv->image_module->module == NULL)
335 if (!_gdk_pixbuf_load_module (priv->image_module, error))
338 if (priv->image_module->module == NULL)
341 if ((priv->image_module->begin_load == NULL) ||
342 (priv->image_module->stop_load == NULL) ||
343 (priv->image_module->load_increment == NULL))
347 GDK_PIXBUF_ERROR_UNSUPPORTED_OPERATION,
348 _("Incremental loading of image type '%s' is not supported"),
354 priv->context = priv->image_module->begin_load (gdk_pixbuf_loader_prepare,
355 gdk_pixbuf_loader_update,
356 gdk_pixbuf_loader_frame_done,
357 gdk_pixbuf_loader_animation_done,
361 if (priv->context == NULL)
363 /* Defense against broken loaders; DO NOT take this as a GError
366 if (error && *error == NULL)
368 g_warning ("Bug! loader '%s' didn't set an error on failure",
369 priv->image_module->module_name);
372 GDK_PIXBUF_ERROR_FAILED,
373 _("Internal error: Image loader module '%s'"
374 " failed to begin loading an image, but didn't"
375 " give a reason for the failure"),
376 priv->image_module->module_name);
383 if (priv->header_buf_offset
384 && priv->image_module->load_increment (priv->context, priv->header_buf, priv->header_buf_offset, error))
385 return priv->header_buf_offset;
391 gdk_pixbuf_loader_eat_header_write (GdkPixbufLoader *loader,
397 GdkPixbufLoaderPrivate *priv = loader->priv;
399 n_bytes = MIN(LOADER_HEADER_SIZE - priv->header_buf_offset, count);
400 memcpy (priv->header_buf + priv->header_buf_offset, buf, n_bytes);
402 priv->header_buf_offset += n_bytes;
404 if (priv->header_buf_offset >= LOADER_HEADER_SIZE)
406 if (gdk_pixbuf_loader_load_module (loader, NULL, error) == 0)
414 * gdk_pixbuf_loader_write:
415 * @loader: A pixbuf loader.
416 * @buf: Pointer to image data.
417 * @count: Length of the @buf buffer in bytes.
418 * @error: return location for errors
420 * This will cause a pixbuf loader to parse the next @count bytes of
421 * an image. It will return TRUE if the data was loaded successfully,
422 * and FALSE if an error occurred. In the latter case, the loader
423 * will be closed, and will not accept further writes. If FALSE is
424 * returned, @error will be set to an error from the #GDK_PIXBUF_ERROR
425 * or #G_FILE_ERROR domains.
427 * Return value: #TRUE if the write was successful, or #FALSE if the loader
428 * cannot parse the buffer.
431 gdk_pixbuf_loader_write (GdkPixbufLoader *loader,
436 GdkPixbufLoaderPrivate *priv;
438 g_return_val_if_fail (loader != NULL, FALSE);
439 g_return_val_if_fail (GDK_IS_PIXBUF_LOADER (loader), FALSE);
441 g_return_val_if_fail (buf != NULL, FALSE);
442 g_return_val_if_fail (count >= 0, FALSE);
446 /* we expect it's not to be closed */
447 g_return_val_if_fail (priv->closed == FALSE, FALSE);
449 if (priv->image_module == NULL)
453 eaten = gdk_pixbuf_loader_eat_header_write(loader, buf, count, error);
461 if (count > 0 && priv->image_module->load_increment)
464 retval = priv->image_module->load_increment (priv->context, buf, count,
466 if (!retval && error && *error == NULL)
468 /* Fix up busted image loader */
469 g_warning ("Bug! loader '%s' didn't set an error on failure",
470 priv->image_module->module_name);
473 GDK_PIXBUF_ERROR_FAILED,
474 _("Internal error: Image loader module '%s'"
475 " failed to begin loading an image, but didn't"
476 " give a reason for the failure"),
477 priv->image_module->module_name);
487 * gdk_pixbuf_loader_new:
489 * Creates a new pixbuf loader object.
491 * Return value: A newly-created pixbuf loader.
494 gdk_pixbuf_loader_new (void)
496 return g_object_new (GDK_TYPE_PIXBUF_LOADER, NULL);
500 * gdk_pixbuf_loader_new_with_type:
501 * @image_type: name of the image format to be loaded with the image
502 * @error: return location for an allocated #GError, or %NULL to ignore errors
504 * Creates a new pixbuf loader object that always attempts to parse
505 * image data as if it were an image of type @image_type, instead of
506 * identifying the type automatically. Useful if you want an error if
507 * the image isn't the expected type, for loading image formats
508 * that can't be reliably identified by looking at the data, or if
509 * the user manually forces a specific type.
511 * Return value: A newly-created pixbuf loader.
514 gdk_pixbuf_loader_new_with_type (const char *image_type,
517 GdkPixbufLoader *retval;
520 retval = g_object_new (GDK_TYPE_PIXBUF_LOADER, NULL);
523 gdk_pixbuf_loader_load_module(retval, image_type, &tmp);
526 g_propagate_error (error, tmp);
527 g_object_unref (G_OBJECT (retval));
535 * gdk_pixbuf_loader_get_pixbuf:
536 * @loader: A pixbuf loader.
538 * Queries the GdkPixbuf that a pixbuf loader is currently creating.
539 * In general it only makes sense to call this function afer the
540 * "area_prepared" signal has been emitted by the loader; this means
541 * that enough data has been read to know the size of the image that
542 * will be allocated. If the loader has not received enough data via
543 * gdk_pixbuf_loader_write(), then this function returns NULL. The
544 * returned pixbuf will be the same in all future calls to the loader,
545 * so simply calling gdk_pixbuf_ref() should be sufficient to continue
546 * using it. Additionally, if the loader is an animation, it will
547 * return the first frame of the animation.
549 * Return value: The GdkPixbuf that the loader is creating, or NULL if not
550 * enough data has been read to determine how to create the image buffer.
553 gdk_pixbuf_loader_get_pixbuf (GdkPixbufLoader *loader)
555 GdkPixbufLoaderPrivate *priv;
557 g_return_val_if_fail (loader != NULL, NULL);
558 g_return_val_if_fail (GDK_IS_PIXBUF_LOADER (loader), NULL);
566 list = gdk_pixbuf_animation_get_frames (priv->animation);
569 GdkPixbufFrame *frame = list->data;
571 return gdk_pixbuf_frame_get_pixbuf (frame);
579 * gdk_pixbuf_loader_get_animation:
580 * @loader: A pixbuf loader
582 * Queries the GdkPixbufAnimation that a pixbuf loader is currently creating.
583 * In general it only makes sense to call this function afer the "area_prepared"
584 * signal has been emitted by the loader. If the image is not an animation,
585 * then it will return NULL.
587 * Return value: The GdkPixbufAnimation that the loader is loading, or NULL if
588 not enough data has been read to determine the information.
591 gdk_pixbuf_loader_get_animation (GdkPixbufLoader *loader)
593 GdkPixbufLoaderPrivate *priv;
595 g_return_val_if_fail (loader != NULL, NULL);
596 g_return_val_if_fail (GDK_IS_PIXBUF_LOADER (loader), NULL);
600 return priv->animation;
604 * gdk_pixbuf_loader_close:
605 * @loader: A pixbuf loader.
606 * @error: return location for a #GError, or %NULL to ignore errors
608 * Informs a pixbuf loader that no further writes with
609 * gdk_pixbuf_loader_write() will occur, so that it can free its
610 * internal loading structures. Also, tries to parse any data that
611 * hasn't yet been parsed; if the remaining data is partial or
612 * corrupt, an error will be returned. If FALSE is returned, @error
613 * will be set to an error from the #GDK_PIXBUF_ERROR or #G_FILE_ERROR
614 * domains. If you're just cancelling a load rather than expecting it
615 * to be finished, passing %NULL for @error to ignore it is
618 * Returns: %TRUE if all image data written so far was successfully
619 passed out via the update_area signal
622 gdk_pixbuf_loader_close (GdkPixbufLoader *loader,
625 GdkPixbufLoaderPrivate *priv;
626 gboolean retval = TRUE;
628 g_return_val_if_fail (loader != NULL, TRUE);
629 g_return_val_if_fail (GDK_IS_PIXBUF_LOADER (loader), TRUE);
633 /* we expect it's not closed */
634 g_return_val_if_fail (priv->closed == FALSE, TRUE);
636 /* We have less the 128 bytes in the image. Flush it, and keep going. */
637 if (priv->image_module == NULL)
638 gdk_pixbuf_loader_load_module (loader, NULL, NULL);
640 if (priv->image_module && priv->image_module->stop_load)
641 retval = priv->image_module->stop_load (priv->context, error);
645 g_signal_emit (G_OBJECT (loader), pixbuf_loader_signals[CLOSED], 0);