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.
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_new:
343 * Creates a new pixbuf loader object.
345 * Return value: A newly-created pixbuf loader.
348 gdk_pixbuf_loader_new (void)
350 return g_object_new (GDK_TYPE_PIXBUF_LOADER, NULL);
354 gdk_pixbuf_loader_load_module (GdkPixbufLoader *loader)
356 GdkPixbufLoaderPrivate *priv = loader->private;
358 priv->image_module = gdk_pixbuf_get_module (priv->header_buf, priv->header_buf_offset);
360 if (priv->image_module == NULL)
363 if (priv->image_module->module == NULL)
364 gdk_pixbuf_load_module (priv->image_module);
366 if (priv->image_module->module == NULL)
369 if ((priv->image_module->begin_load == NULL) ||
370 (priv->image_module->stop_load == NULL) ||
371 (priv->image_module->load_increment == NULL))
373 g_warning (G_STRLOC ": module %s does not support incremental loading.\n",
374 priv->image_module->module_name);
378 priv->context = priv->image_module->begin_load (gdk_pixbuf_loader_prepare,
379 gdk_pixbuf_loader_update,
380 gdk_pixbuf_loader_frame_done,
381 gdk_pixbuf_loader_animation_done,
384 if (priv->context == NULL)
386 g_warning (G_STRLOC ": Failed to begin progressive load");
390 if (priv->image_module->load_increment (priv->context, priv->header_buf, priv->header_buf_offset))
391 return priv->header_buf_offset;
397 gdk_pixbuf_loader_eat_header_write (GdkPixbufLoader *loader,
402 GdkPixbufLoaderPrivate *priv = loader->private;
404 n_bytes = MIN(LOADER_HEADER_SIZE - priv->header_buf_offset, count);
405 memcpy (priv->header_buf + priv->header_buf_offset, buf, n_bytes);
407 priv->header_buf_offset += n_bytes;
409 if (priv->header_buf_offset >= LOADER_HEADER_SIZE)
411 if (gdk_pixbuf_loader_load_module (loader) == 0)
419 * gdk_pixbuf_loader_write:
420 * @loader: A pixbuf loader.
421 * @buf: Pointer to image data.
422 * @count: Length of the @buf buffer in bytes.
424 * This will cause a pixbuf loader to parse the next @count bytes of an image.
425 * It will return TRUE if the data was loaded successfully, and FALSE if an
426 * error occurred. In the latter case, the loader will be closed, and will not
427 * accept further writes.
429 * Return value: #TRUE if the write was successful, or #FALSE if the loader
430 * cannot parse the buffer.
433 gdk_pixbuf_loader_write (GdkPixbufLoader *loader,
437 GdkPixbufLoaderPrivate *priv;
439 g_return_val_if_fail (loader != NULL, FALSE);
440 g_return_val_if_fail (GDK_IS_PIXBUF_LOADER (loader), FALSE);
442 g_return_val_if_fail (buf != NULL, FALSE);
443 g_return_val_if_fail (count >= 0, FALSE);
445 priv = loader->private;
447 /* we expect it's not to be closed */
448 g_return_val_if_fail (priv->closed == FALSE, FALSE);
450 if (priv->image_module == NULL)
454 eaten = gdk_pixbuf_loader_eat_header_write(loader, buf, count);
462 if (count > 0 && priv->image_module->load_increment)
463 return priv->image_module->load_increment (priv->context, buf, count);
469 * gdk_pixbuf_loader_get_pixbuf:
470 * @loader: A pixbuf loader.
472 * Queries the GdkPixbuf that a pixbuf loader is currently creating. In general
473 * it only makes sense to call this function afer the "area_prepared" signal has
474 * been emitted by the loader; this means that enough data has been read to know
475 * the size of the image that will be allocated. If the loader has not received
476 * enough data via gdk_pixbuf_loader_write(), then this function returns NULL.
477 * The returned pixbuf will be the same in all future calls to the loader, so
478 * simply calling gdk_pixbuf_ref() should be sufficient to continue using it.
480 * Return value: The GdkPixbuf that the loader is creating, or NULL if not
481 * enough data has been read to determine how to create the image buffer.
484 gdk_pixbuf_loader_get_pixbuf (GdkPixbufLoader *loader)
486 GdkPixbufLoaderPrivate *priv;
488 g_return_val_if_fail (loader != NULL, NULL);
489 g_return_val_if_fail (GDK_IS_PIXBUF_LOADER (loader), NULL);
491 priv = loader->private;
497 * gdk_pixbuf_loader_get_animation:
498 * @loader: A pixbuf loader
500 * Queries the GdkPixbufAnimation that a pixbuf loader is currently creating.
501 * In general it only makes sense to call this function afer the "area_prepared"
502 * signal has been emitted by the loader. If the image is not an animation,
503 * then it will return NULL.
505 * Return value: The GdkPixbufAnimation that the loader is loading, or NULL if
506 not enough data has been read to determine the information.
509 gdk_pixbuf_loader_get_animation (GdkPixbufLoader *loader)
511 GdkPixbufLoaderPrivate *priv;
513 g_return_val_if_fail (loader != NULL, NULL);
514 g_return_val_if_fail (GDK_IS_PIXBUF_LOADER (loader), NULL);
516 priv = loader->private;
518 return priv->animation;
522 * gdk_pixbuf_loader_close:
523 * @loader: A pixbuf loader.
525 * Informs a pixbuf loader that no further writes with gdk_pixbuf_load_write()
526 * will occur, so that it can free its internal loading structures.
529 gdk_pixbuf_loader_close (GdkPixbufLoader *loader)
531 GdkPixbufLoaderPrivate *priv;
533 g_return_if_fail (loader != NULL);
534 g_return_if_fail (GDK_IS_PIXBUF_LOADER (loader));
536 priv = loader->private;
538 /* we expect it's not closed */
539 g_return_if_fail (priv->closed == FALSE);
541 /* We have less the 128 bytes in the image. Flush it, and keep going. */
542 if (priv->image_module == NULL)
543 gdk_pixbuf_loader_load_module (loader);
545 if (priv->image_module && priv->image_module->stop_load)
546 priv->image_module->stop_load (priv->context);
550 gtk_signal_emit (GTK_OBJECT (loader), pixbuf_loader_signals[CLOSED]);