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 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"
32 #include "gtksignal.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_destroy (GtkObject *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 typedef void (*GtkSignal_NONE__INT_INT_INT_INT) (GtkObject *object,
71 gint arg1, gint arg2, gint arg3, gint arg4,
74 gtk_marshal_NONE__INT_INT_INT_INT (GtkObject *object, GtkSignalFunc func, gpointer func_data,
77 GtkSignal_NONE__INT_INT_INT_INT rfunc;
79 rfunc = (GtkSignal_NONE__INT_INT_INT_INT) func;
81 GTK_VALUE_INT (args[0]),
82 GTK_VALUE_INT (args[1]),
83 GTK_VALUE_INT (args[2]),
84 GTK_VALUE_INT (args[3]),
90 * gdk_pixbuf_loader_get_type:
93 * Registers the #GdkPixubfLoader class if necessary, and returns the type ID
96 * Return value: The type ID of the #GdkPixbufLoader class.
99 gdk_pixbuf_loader_get_type (void)
101 static GtkType loader_type = 0;
105 static const GtkTypeInfo loader_info = {
107 sizeof (GdkPixbufLoader),
108 sizeof (GdkPixbufLoaderClass),
109 (GtkClassInitFunc) gdk_pixbuf_loader_class_init,
110 (GtkObjectInitFunc) gdk_pixbuf_loader_init,
111 /* reserved_1 */ NULL,
112 /* reserved_2 */ NULL,
113 (GtkClassInitFunc) NULL,
116 loader_type = gtk_type_unique (GTK_TYPE_OBJECT, &loader_info);
123 gdk_pixbuf_loader_class_init (GdkPixbufLoaderClass *class)
125 GObjectClass *gobject_class;
126 GtkObjectClass *object_class;
128 object_class = (GtkObjectClass *) class;
129 gobject_class = (GObjectClass *) class;
131 parent_class = gtk_type_class (GTK_TYPE_OBJECT);
133 pixbuf_loader_signals[AREA_PREPARED] =
134 gtk_signal_new ("area_prepared",
136 GTK_CLASS_TYPE (object_class),
137 GTK_SIGNAL_OFFSET (GdkPixbufLoaderClass, area_prepared),
138 gtk_marshal_NONE__NONE,
141 pixbuf_loader_signals[AREA_UPDATED] =
142 gtk_signal_new ("area_updated",
144 GTK_CLASS_TYPE (object_class),
145 GTK_SIGNAL_OFFSET (GdkPixbufLoaderClass, area_updated),
146 gtk_marshal_NONE__INT_INT_INT_INT,
153 pixbuf_loader_signals[FRAME_DONE] =
154 gtk_signal_new ("frame_done",
156 GTK_CLASS_TYPE (object_class),
157 GTK_SIGNAL_OFFSET (GdkPixbufLoaderClass, frame_done),
158 gtk_marshal_NONE__POINTER,
162 pixbuf_loader_signals[ANIMATION_DONE] =
163 gtk_signal_new ("animation_done",
165 GTK_CLASS_TYPE (object_class),
166 GTK_SIGNAL_OFFSET (GdkPixbufLoaderClass, animation_done),
167 gtk_marshal_NONE__NONE,
170 pixbuf_loader_signals[CLOSED] =
171 gtk_signal_new ("closed",
173 GTK_CLASS_TYPE (object_class),
174 GTK_SIGNAL_OFFSET (GdkPixbufLoaderClass, closed),
175 gtk_marshal_NONE__NONE,
178 gtk_object_class_add_signals (object_class, pixbuf_loader_signals, LAST_SIGNAL);
180 object_class->destroy = gdk_pixbuf_loader_destroy;
181 gobject_class->finalize = gdk_pixbuf_loader_finalize;
185 gdk_pixbuf_loader_init (GdkPixbufLoader *loader)
187 GdkPixbufLoaderPrivate *priv;
189 priv = g_new0 (GdkPixbufLoaderPrivate, 1);
190 loader->private = priv;
194 gdk_pixbuf_loader_destroy (GtkObject *object)
196 GdkPixbufLoader *loader;
197 GdkPixbufLoaderPrivate *priv = NULL;
199 g_return_if_fail (object != NULL);
200 g_return_if_fail (GDK_IS_PIXBUF_LOADER (object));
202 loader = GDK_PIXBUF_LOADER (object);
203 priv = loader->private;
206 gdk_pixbuf_loader_close (loader);
209 gdk_pixbuf_animation_unref (priv->animation);
211 gdk_pixbuf_unref (priv->pixbuf);
213 if (GTK_OBJECT_CLASS (parent_class)->destroy)
214 GTK_OBJECT_CLASS (parent_class)->destroy (object);
218 gdk_pixbuf_loader_finalize (GObject *object)
220 GdkPixbufLoader *loader;
221 GdkPixbufLoaderPrivate *priv = NULL;
223 loader = GDK_PIXBUF_LOADER (object);
224 priv = loader->private;
228 if (G_OBJECT_CLASS (parent_class)->finalize)
229 G_OBJECT_CLASS (parent_class)->finalize (object);
233 gdk_pixbuf_loader_prepare (GdkPixbuf *pixbuf,
236 GdkPixbufLoaderPrivate *priv = NULL;
238 priv = GDK_PIXBUF_LOADER (loader)->private;
239 gdk_pixbuf_ref (pixbuf);
241 g_assert (priv->pixbuf == NULL);
243 priv->pixbuf = pixbuf;
244 gtk_signal_emit (GTK_OBJECT (loader), pixbuf_loader_signals[AREA_PREPARED]);
248 gdk_pixbuf_loader_update (GdkPixbuf *pixbuf,
255 GdkPixbufLoaderPrivate *priv = NULL;
257 priv = GDK_PIXBUF_LOADER (loader)->private;
259 gtk_signal_emit (GTK_OBJECT (loader),
260 pixbuf_loader_signals[AREA_UPDATED],
262 /* sanity check in here. Defend against an errant loader */
263 MIN (width, gdk_pixbuf_get_width (priv->pixbuf)),
264 MIN (height, gdk_pixbuf_get_height (priv->pixbuf)));
268 gdk_pixbuf_loader_frame_done (GdkPixbufFrame *frame,
271 GdkPixbufLoaderPrivate *priv = NULL;
273 priv = GDK_PIXBUF_LOADER (loader)->private;
277 if (priv->animation == NULL)
279 priv->animation = g_object_new (GDK_TYPE_PIXBUF_ANIMATION, NULL);
281 priv->animation->n_frames = 0;
282 priv->animation->width = gdk_pixbuf_get_width (frame->pixbuf) + frame->x_offset;
283 priv->animation->height = gdk_pixbuf_get_height (frame->pixbuf) + frame->y_offset;
289 /* update bbox size */
290 w = gdk_pixbuf_get_width (frame->pixbuf) + frame->x_offset;
291 h = gdk_pixbuf_get_height (frame->pixbuf) + frame->y_offset;
293 if (w > priv->animation->width) {
294 priv->animation->width = w;
296 if (h > priv->animation->height) {
297 priv->animation->height = h;
301 priv->animation->frames = g_list_append (priv->animation->frames, frame);
302 priv->animation->n_frames++;
303 gtk_signal_emit (GTK_OBJECT (loader),
304 pixbuf_loader_signals[FRAME_DONE],
309 gdk_pixbuf_loader_animation_done (GdkPixbuf *pixbuf,
312 GdkPixbufLoaderPrivate *priv = NULL;
313 GdkPixbufFrame *frame;
314 GList *current = NULL;
317 priv = GDK_PIXBUF_LOADER (loader)->private;
320 current = gdk_pixbuf_animation_get_frames (priv->animation);
324 frame = (GdkPixbufFrame *) current->data;
326 /* update bbox size */
327 w = gdk_pixbuf_get_width (frame->pixbuf) + frame->x_offset;
328 h = gdk_pixbuf_get_height (frame->pixbuf) + frame->y_offset;
330 if (w > priv->animation->width)
331 priv->animation->width = w;
332 if (h > priv->animation->height)
333 priv->animation->height = h;
334 current = current->next;
337 gtk_signal_emit (GTK_OBJECT (loader), pixbuf_loader_signals[ANIMATION_DONE]);
341 gdk_pixbuf_loader_load_module (GdkPixbufLoader *loader, const char *image_type)
343 GdkPixbufLoaderPrivate *priv = loader->private;
346 priv->image_module = gdk_pixbuf_get_named_module (image_type);
348 priv->image_module = gdk_pixbuf_get_module (priv->header_buf, priv->header_buf_offset);
350 if (priv->image_module == NULL)
353 if (priv->image_module->module == NULL)
354 gdk_pixbuf_load_module (priv->image_module);
356 if (priv->image_module->module == NULL)
359 if ((priv->image_module->begin_load == NULL) ||
360 (priv->image_module->stop_load == NULL) ||
361 (priv->image_module->load_increment == NULL))
363 g_warning (G_STRLOC ": module %s does not support incremental loading.\n",
364 priv->image_module->module_name);
368 priv->context = priv->image_module->begin_load (gdk_pixbuf_loader_prepare,
369 gdk_pixbuf_loader_update,
370 gdk_pixbuf_loader_frame_done,
371 gdk_pixbuf_loader_animation_done,
374 if (priv->context == NULL)
376 g_warning (G_STRLOC ": Failed to begin progressive load");
380 if (priv->header_buf_offset
381 && priv->image_module->load_increment (priv->context, priv->header_buf, priv->header_buf_offset))
382 return priv->header_buf_offset;
388 gdk_pixbuf_loader_eat_header_write (GdkPixbufLoader *loader,
393 GdkPixbufLoaderPrivate *priv = loader->private;
395 n_bytes = MIN(LOADER_HEADER_SIZE - priv->header_buf_offset, count);
396 memcpy (priv->header_buf + priv->header_buf_offset, buf, n_bytes);
398 priv->header_buf_offset += n_bytes;
400 if (priv->header_buf_offset >= LOADER_HEADER_SIZE)
402 if (gdk_pixbuf_loader_load_module (loader, NULL) == 0)
410 * gdk_pixbuf_loader_write:
411 * @loader: A pixbuf loader.
412 * @buf: Pointer to image data.
413 * @count: Length of the @buf buffer in bytes.
415 * This will cause a pixbuf loader to parse the next @count bytes of an image.
416 * It will return TRUE if the data was loaded successfully, and FALSE if an
417 * error occurred. In the latter case, the loader will be closed, and will not
418 * accept further writes.
420 * Return value: #TRUE if the write was successful, or #FALSE if the loader
421 * cannot parse the buffer.
424 gdk_pixbuf_loader_write (GdkPixbufLoader *loader,
428 GdkPixbufLoaderPrivate *priv;
430 g_return_val_if_fail (loader != NULL, FALSE);
431 g_return_val_if_fail (GDK_IS_PIXBUF_LOADER (loader), FALSE);
433 g_return_val_if_fail (buf != NULL, FALSE);
434 g_return_val_if_fail (count >= 0, FALSE);
436 priv = loader->private;
438 /* we expect it's not to be closed */
439 g_return_val_if_fail (priv->closed == FALSE, FALSE);
441 if (priv->image_module == NULL)
445 eaten = gdk_pixbuf_loader_eat_header_write(loader, buf, count);
453 if (count > 0 && priv->image_module->load_increment)
454 return priv->image_module->load_increment (priv->context, buf, count);
460 * gdk_pixbuf_loader_new:
462 * Creates a new pixbuf loader object.
464 * Return value: A newly-created pixbuf loader.
467 gdk_pixbuf_loader_new (void)
469 return g_object_new (GDK_TYPE_PIXBUF_LOADER, NULL);
473 * gdk_pixbuf_loader_new_with_type:
475 * Creates a new pixbuf loader object.
477 * Return value: A newly-created pixbuf loader.
480 gdk_pixbuf_loader_new_with_type (const char *image_type)
482 GdkPixbufLoader *retval;
484 retval = g_object_new (GDK_TYPE_PIXBUF_LOADER, NULL);
486 gdk_pixbuf_loader_load_module(retval, image_type);
492 * gdk_pixbuf_loader_get_pixbuf:
493 * @loader: A pixbuf loader.
495 * Queries the GdkPixbuf that a pixbuf loader is currently creating. In general
496 * it only makes sense to call this function afer the "area_prepared" signal has
497 * been emitted by the loader; this means that enough data has been read to know
498 * the size of the image that will be allocated. If the loader has not received
499 * enough data via gdk_pixbuf_loader_write(), then this function returns NULL.
500 * The returned pixbuf will be the same in all future calls to the loader, so
501 * simply calling gdk_pixbuf_ref() should be sufficient to continue using it.
503 * Return value: The GdkPixbuf that the loader is creating, or NULL if not
504 * enough data has been read to determine how to create the image buffer.
507 gdk_pixbuf_loader_get_pixbuf (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;
520 * gdk_pixbuf_loader_get_animation:
521 * @loader: A pixbuf loader
523 * Queries the GdkPixbufAnimation that a pixbuf loader is currently creating.
524 * In general it only makes sense to call this function afer the "area_prepared"
525 * signal has been emitted by the loader. If the image is not an animation,
526 * then it will return NULL.
528 * Return value: The GdkPixbufAnimation that the loader is loading, or NULL if
529 not enough data has been read to determine the information.
532 gdk_pixbuf_loader_get_animation (GdkPixbufLoader *loader)
534 GdkPixbufLoaderPrivate *priv;
536 g_return_val_if_fail (loader != NULL, NULL);
537 g_return_val_if_fail (GDK_IS_PIXBUF_LOADER (loader), NULL);
539 priv = loader->private;
541 return priv->animation;
545 * gdk_pixbuf_loader_close:
546 * @loader: A pixbuf loader.
548 * Informs a pixbuf loader that no further writes with gdk_pixbuf_load_write()
549 * will occur, so that it can free its internal loading structures.
552 gdk_pixbuf_loader_close (GdkPixbufLoader *loader)
554 GdkPixbufLoaderPrivate *priv;
556 g_return_if_fail (loader != NULL);
557 g_return_if_fail (GDK_IS_PIXBUF_LOADER (loader));
559 priv = loader->private;
561 /* we expect it's not closed */
562 g_return_if_fail (priv->closed == FALSE);
564 /* We have less the 128 bytes in the image. Flush it, and keep going. */
565 if (priv->image_module == NULL)
566 gdk_pixbuf_loader_load_module (loader, NULL);
568 if (priv->image_module && priv->image_module->stop_load)
569 priv->image_module->stop_load (priv->context);
573 gtk_signal_emit (GTK_OBJECT (loader), pixbuf_loader_signals[CLOSED]);