1 /* GdkPixbuf library - Main header file
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 Library 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 * Library General Public License for more details.
20 * You should have received a copy of the GNU Library 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.
26 #include "gdk-pixbuf-private.h"
27 #include "gdk-pixbuf-loader.h"
28 #include "gdk-pixbuf-io.h"
30 #include "gtksignal.h"
42 static void gdk_pixbuf_loader_class_init (GdkPixbufLoaderClass *klass);
43 static void gdk_pixbuf_loader_init (GdkPixbufLoader *loader);
44 static void gdk_pixbuf_loader_destroy (GtkObject *loader);
45 static void gdk_pixbuf_loader_finalize (GObject *loader);
47 static gpointer parent_class = NULL;
48 static guint pixbuf_loader_signals[LAST_SIGNAL] = { 0 };
53 #define LOADER_HEADER_SIZE 128
58 GdkPixbufAnimation *animation;
60 guchar header_buf[LOADER_HEADER_SIZE];
61 gint header_buf_offset;
62 GdkPixbufModule *image_module;
64 } GdkPixbufLoaderPrivate;
68 typedef void (*GtkSignal_NONE__INT_INT_INT_INT) (GtkObject *object,
69 gint arg1, gint arg2, gint arg3, gint arg4,
72 gtk_marshal_NONE__INT_INT_INT_INT (GtkObject *object, GtkSignalFunc func, gpointer func_data,
75 GtkSignal_NONE__INT_INT_INT_INT rfunc;
77 rfunc = (GtkSignal_NONE__INT_INT_INT_INT) func;
79 GTK_VALUE_INT (args[0]),
80 GTK_VALUE_INT (args[1]),
81 GTK_VALUE_INT (args[2]),
82 GTK_VALUE_INT (args[3]),
88 * gdk_pixbuf_loader_get_type:
91 * Registers the #GdkPixubfLoader class if necessary, and returns the type ID
94 * Return value: The type ID of the #GdkPixbufLoader class.
97 gdk_pixbuf_loader_get_type (void)
99 static GtkType loader_type = 0;
103 static const GtkTypeInfo loader_info = {
105 sizeof (GdkPixbufLoader),
106 sizeof (GdkPixbufLoaderClass),
107 (GtkClassInitFunc) gdk_pixbuf_loader_class_init,
108 (GtkObjectInitFunc) gdk_pixbuf_loader_init,
109 /* reserved_1 */ NULL,
110 /* reserved_2 */ NULL,
111 (GtkClassInitFunc) NULL,
114 loader_type = gtk_type_unique (GTK_TYPE_OBJECT, &loader_info);
121 gdk_pixbuf_loader_class_init (GdkPixbufLoaderClass *class)
123 GObjectClass *gobject_class;
124 GtkObjectClass *object_class;
126 object_class = (GtkObjectClass *) class;
127 gobject_class = (GObjectClass *) class;
129 parent_class = gtk_type_class (GTK_TYPE_OBJECT);
131 pixbuf_loader_signals[AREA_PREPARED] =
132 gtk_signal_new ("area_prepared",
134 GTK_CLASS_TYPE (object_class),
135 GTK_SIGNAL_OFFSET (GdkPixbufLoaderClass, area_prepared),
136 gtk_marshal_NONE__NONE,
139 pixbuf_loader_signals[AREA_UPDATED] =
140 gtk_signal_new ("area_updated",
142 GTK_CLASS_TYPE (object_class),
143 GTK_SIGNAL_OFFSET (GdkPixbufLoaderClass, area_updated),
144 gtk_marshal_NONE__INT_INT_INT_INT,
151 pixbuf_loader_signals[FRAME_DONE] =
152 gtk_signal_new ("frame_done",
154 GTK_CLASS_TYPE (object_class),
155 GTK_SIGNAL_OFFSET (GdkPixbufLoaderClass, frame_done),
156 gtk_marshal_NONE__POINTER,
160 pixbuf_loader_signals[ANIMATION_DONE] =
161 gtk_signal_new ("animation_done",
163 GTK_CLASS_TYPE (object_class),
164 GTK_SIGNAL_OFFSET (GdkPixbufLoaderClass, animation_done),
165 gtk_marshal_NONE__NONE,
168 pixbuf_loader_signals[CLOSED] =
169 gtk_signal_new ("closed",
171 GTK_CLASS_TYPE (object_class),
172 GTK_SIGNAL_OFFSET (GdkPixbufLoaderClass, closed),
173 gtk_marshal_NONE__NONE,
176 gtk_object_class_add_signals (object_class, pixbuf_loader_signals, LAST_SIGNAL);
178 object_class->destroy = gdk_pixbuf_loader_destroy;
179 gobject_class->finalize = gdk_pixbuf_loader_finalize;
183 gdk_pixbuf_loader_init (GdkPixbufLoader *loader)
185 GdkPixbufLoaderPrivate *priv;
187 priv = g_new0 (GdkPixbufLoaderPrivate, 1);
188 loader->private = priv;
192 gdk_pixbuf_loader_destroy (GtkObject *object)
194 GdkPixbufLoader *loader;
195 GdkPixbufLoaderPrivate *priv = NULL;
197 g_return_if_fail (object != NULL);
198 g_return_if_fail (GDK_IS_PIXBUF_LOADER (object));
200 loader = GDK_PIXBUF_LOADER (object);
201 priv = loader->private;
204 gdk_pixbuf_loader_close (loader);
207 gdk_pixbuf_animation_unref (priv->animation);
209 gdk_pixbuf_unref (priv->pixbuf);
211 if (GTK_OBJECT_CLASS (parent_class)->destroy)
212 GTK_OBJECT_CLASS (parent_class)->destroy (object);
216 gdk_pixbuf_loader_finalize (GObject *object)
218 GdkPixbufLoader *loader;
219 GdkPixbufLoaderPrivate *priv = NULL;
221 loader = GDK_PIXBUF_LOADER (object);
222 priv = loader->private;
226 if (G_OBJECT_CLASS (parent_class)->finalize)
227 G_OBJECT_CLASS (parent_class)->finalize (object);
231 gdk_pixbuf_loader_prepare (GdkPixbuf *pixbuf,
234 GdkPixbufLoaderPrivate *priv = NULL;
236 priv = GDK_PIXBUF_LOADER (loader)->private;
237 gdk_pixbuf_ref (pixbuf);
239 g_assert (priv->pixbuf == NULL);
241 priv->pixbuf = pixbuf;
242 gtk_signal_emit (GTK_OBJECT (loader), pixbuf_loader_signals[AREA_PREPARED]);
246 gdk_pixbuf_loader_update (GdkPixbuf *pixbuf,
253 GdkPixbufLoaderPrivate *priv = NULL;
255 priv = GDK_PIXBUF_LOADER (loader)->private;
257 gtk_signal_emit (GTK_OBJECT (loader),
258 pixbuf_loader_signals[AREA_UPDATED],
260 /* sanity check in here. Defend against an errant loader */
261 MIN (width, gdk_pixbuf_get_width (priv->pixbuf)),
262 MIN (height, gdk_pixbuf_get_height (priv->pixbuf)));
266 gdk_pixbuf_loader_frame_done (GdkPixbufFrame *frame,
269 GdkPixbufLoaderPrivate *priv = NULL;
271 priv = GDK_PIXBUF_LOADER (loader)->private;
275 if (priv->animation == NULL)
277 priv->animation = GDK_PIXBUF_ANIMATION (g_type_create_instance (GDK_TYPE_PIXBUF_ANIMATION));
279 priv->animation->n_frames = 0;
280 priv->animation->width = gdk_pixbuf_get_width (frame->pixbuf) + frame->x_offset;
281 priv->animation->height = gdk_pixbuf_get_height (frame->pixbuf) + frame->y_offset;
287 /* update bbox size */
288 w = gdk_pixbuf_get_width (frame->pixbuf) + frame->x_offset;
289 h = gdk_pixbuf_get_height (frame->pixbuf) + frame->y_offset;
291 if (w > priv->animation->width) {
292 priv->animation->width = w;
294 if (h > priv->animation->height) {
295 priv->animation->height = h;
299 priv->animation->frames = g_list_append (priv->animation->frames, frame);
300 priv->animation->n_frames++;
301 gtk_signal_emit (GTK_OBJECT (loader),
302 pixbuf_loader_signals[FRAME_DONE],
307 gdk_pixbuf_loader_animation_done (GdkPixbuf *pixbuf,
310 GdkPixbufLoaderPrivate *priv = NULL;
311 GdkPixbufFrame *frame;
312 GList *current = NULL;
315 priv = GDK_PIXBUF_LOADER (loader)->private;
318 current = gdk_pixbuf_animation_get_frames (priv->animation);
322 frame = (GdkPixbufFrame *) current->data;
324 /* update bbox size */
325 w = gdk_pixbuf_get_width (frame->pixbuf) + frame->x_offset;
326 h = gdk_pixbuf_get_height (frame->pixbuf) + frame->y_offset;
328 if (w > priv->animation->width)
329 priv->animation->width = w;
330 if (h > priv->animation->height)
331 priv->animation->height = h;
332 current = current->next;
335 gtk_signal_emit (GTK_OBJECT (loader), pixbuf_loader_signals[ANIMATION_DONE]);
339 * gdk_pixbuf_loader_new:
341 * Creates a new pixbuf loader object.
343 * Return value: A newly-created pixbuf loader.
346 gdk_pixbuf_loader_new (void)
348 return g_object_new (GDK_TYPE_PIXBUF_LOADER, NULL);
352 gdk_pixbuf_loader_load_module (GdkPixbufLoader *loader)
354 GdkPixbufLoaderPrivate *priv = loader->private;
356 priv->image_module = gdk_pixbuf_get_module (priv->header_buf, priv->header_buf_offset);
358 if (priv->image_module == NULL)
361 if (priv->image_module->module == NULL)
362 gdk_pixbuf_load_module (priv->image_module);
364 if (priv->image_module->module == NULL)
367 if ((priv->image_module->begin_load == NULL) ||
368 (priv->image_module->stop_load == NULL) ||
369 (priv->image_module->load_increment == NULL))
371 g_warning (G_STRLOC ": module %s does not support incremental loading.\n",
372 priv->image_module->module_name);
376 priv->context = priv->image_module->begin_load (gdk_pixbuf_loader_prepare,
377 gdk_pixbuf_loader_update,
378 gdk_pixbuf_loader_frame_done,
379 gdk_pixbuf_loader_animation_done,
382 if (priv->context == NULL)
384 g_warning (G_STRLOC ": Failed to begin progressive load");
388 if (priv->image_module->load_increment (priv->context, priv->header_buf, priv->header_buf_offset))
389 return priv->header_buf_offset;
395 gdk_pixbuf_loader_eat_header_write (GdkPixbufLoader *loader,
400 GdkPixbufLoaderPrivate *priv = loader->private;
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) == 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.
422 * This will cause a pixbuf loader to parse the next @count bytes of an image.
423 * It will return TRUE if the data was loaded successfully, and FALSE if an
424 * error occurred. In the latter case, the loader will be closed, and will not
425 * accept further writes.
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,
435 GdkPixbufLoaderPrivate *priv;
437 g_return_val_if_fail (loader != NULL, FALSE);
438 g_return_val_if_fail (GDK_IS_PIXBUF_LOADER (loader), FALSE);
440 g_return_val_if_fail (buf != NULL, FALSE);
441 g_return_val_if_fail (count >= 0, FALSE);
443 priv = loader->private;
445 /* we expect it's not to be closed */
446 g_return_val_if_fail (priv->closed == FALSE, FALSE);
448 if (priv->image_module == NULL)
452 eaten = gdk_pixbuf_loader_eat_header_write(loader, buf, count);
460 if (count > 0 && priv->image_module->load_increment)
461 return priv->image_module->load_increment (priv->context, buf, count);
467 * gdk_pixbuf_loader_get_pixbuf:
468 * @loader: A pixbuf loader.
470 * Queries the GdkPixbuf that a pixbuf loader is currently creating. In general
471 * it only makes sense to call this function afer the "area_prepared" signal has
472 * been emitted by the loader; this means that enough data has been read to know
473 * the size of the image that will be allocated. If the loader has not received
474 * enough data via gdk_pixbuf_loader_write(), then this function returns NULL.
475 * The returned pixbuf will be the same in all future calls to the loader, so
476 * simply calling gdk_pixbuf_ref() should be sufficient to continue using it.
478 * Return value: The GdkPixbuf that the loader is creating, or NULL if not
479 * enough data has been read to determine how to create the image buffer.
482 gdk_pixbuf_loader_get_pixbuf (GdkPixbufLoader *loader)
484 GdkPixbufLoaderPrivate *priv;
486 g_return_val_if_fail (loader != NULL, NULL);
487 g_return_val_if_fail (GDK_IS_PIXBUF_LOADER (loader), NULL);
489 priv = loader->private;
495 * gdk_pixbuf_loader_get_animation:
496 * @loader: A pixbuf loader
498 * Queries the GdkPixbufAnimation that a pixbuf loader is currently creating.
499 * In general it only makes sense to call this function afer the "area_prepared"
500 * signal has been emitted by the loader. If the image is not an animation,
501 * then it will return NULL.
503 * Return value: The GdkPixbufAnimation that the loader is loading, or NULL if
504 not enough data has been read to determine the information.
507 gdk_pixbuf_loader_get_animation (GdkPixbufLoader *loader)
509 GdkPixbufLoaderPrivate *priv;
511 g_return_val_if_fail (loader != NULL, NULL);
512 g_return_val_if_fail (GDK_IS_PIXBUF_LOADER (loader), NULL);
514 priv = loader->private;
516 return priv->animation;
520 * gdk_pixbuf_loader_close:
521 * @loader: A pixbuf loader.
523 * Informs a pixbuf loader that no further writes with gdk_pixbuf_load_write()
524 * will occur, so that it can free its internal loading structures.
527 gdk_pixbuf_loader_close (GdkPixbufLoader *loader)
529 GdkPixbufLoaderPrivate *priv;
531 g_return_if_fail (loader != NULL);
532 g_return_if_fail (GDK_IS_PIXBUF_LOADER (loader));
534 priv = loader->private;
536 /* we expect it's not closed */
537 g_return_if_fail (priv->closed == FALSE);
539 /* We have less the 128 bytes in the image. Flush it, and keep going. */
540 if (priv->image_module == NULL)
541 gdk_pixbuf_loader_load_module (loader);
543 if (priv->image_module && priv->image_module->stop_load)
544 priv->image_module->stop_load (priv->context);
548 gtk_signal_emit (GTK_OBJECT (loader), pixbuf_loader_signals[CLOSED]);