1 /* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
3 /* GdkPixbuf library - Main header file
5 * Copyright (C) 1999 The Free Software Foundation
7 * Authors: Mark Crichton <crichton@gimp.org>
8 * Miguel de Icaza <miguel@gnu.org>
9 * Federico Mena-Quintero <federico@gimp.org>
10 * Jonathan Blandford <jrb@redhat.com>
12 * This library is free software; you can redistribute it and/or
13 * modify it under the terms of the GNU Library General Public
14 * License as published by the Free Software Foundation; either
15 * version 2 of the License, or (at your option) any later version.
17 * This library is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
20 * Library General Public License for more details.
22 * You should have received a copy of the GNU Library General Public
23 * License along with this library; if not, write to the
24 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
25 * 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"
45 static GtkObjectClass *parent_class;
47 static void gdk_pixbuf_loader_class_init (GdkPixbufLoaderClass *klass);
48 static void gdk_pixbuf_loader_init (GdkPixbufLoader *loader);
49 static void gdk_pixbuf_loader_destroy (GtkObject *loader);
50 static void gdk_pixbuf_loader_finalize (GObject *loader);
52 static guint pixbuf_loader_signals[LAST_SIGNAL] = { 0 };
58 #define LOADER_HEADER_SIZE 128
62 GdkPixbufAnimation *animation;
64 guchar header_buf[LOADER_HEADER_SIZE];
65 gint header_buf_offset;
66 GdkPixbufModule *image_module;
68 } GdkPixbufLoaderPrivate;
73 typedef void (* GtkSignal_NONE__INT_INT_INT_INT) (GtkObject *object,
74 gint arg1, gint arg2, gint arg3, gint arg4,
77 gtk_marshal_NONE__INT_INT_INT_INT (GtkObject *object, GtkSignalFunc func, gpointer func_data,
80 GtkSignal_NONE__INT_INT_INT_INT rfunc;
82 rfunc = (GtkSignal_NONE__INT_INT_INT_INT) func;
84 GTK_VALUE_INT (args[0]),
85 GTK_VALUE_INT (args[1]),
86 GTK_VALUE_INT (args[2]),
87 GTK_VALUE_INT (args[3]),
94 * gdk_pixbuf_loader_get_type:
97 * Registers the #GdkPixubfLoader class if necessary, and returns the type ID
100 * Return value: The type ID of the #GdkPixbufLoader class.
103 gdk_pixbuf_loader_get_type (void)
105 static GtkType loader_type = 0;
108 static const GtkTypeInfo loader_info = {
110 sizeof (GdkPixbufLoader),
111 sizeof (GdkPixbufLoaderClass),
112 (GtkClassInitFunc) gdk_pixbuf_loader_class_init,
113 (GtkObjectInitFunc) gdk_pixbuf_loader_init,
114 /* reserved_1 */ NULL,
115 /* reserved_2 */ NULL,
116 (GtkClassInitFunc) NULL,
119 loader_type = gtk_type_unique (GTK_TYPE_OBJECT, &loader_info);
126 gdk_pixbuf_loader_class_init (GdkPixbufLoaderClass *class)
128 GObjectClass *gobject_class;
129 GtkObjectClass *object_class;
131 object_class = (GtkObjectClass *) class;
132 gobject_class = (GObjectClass *) class;
134 parent_class = gtk_type_class (gtk_object_get_type ());
136 pixbuf_loader_signals[AREA_PREPARED] =
137 gtk_signal_new ("area_prepared",
139 GTK_CLASS_TYPE (object_class),
140 GTK_SIGNAL_OFFSET (GdkPixbufLoaderClass, area_prepared),
141 gtk_marshal_NONE__NONE,
144 pixbuf_loader_signals[AREA_UPDATED] =
145 gtk_signal_new ("area_updated",
147 GTK_CLASS_TYPE (object_class),
148 GTK_SIGNAL_OFFSET (GdkPixbufLoaderClass, area_updated),
149 gtk_marshal_NONE__INT_INT_INT_INT,
156 pixbuf_loader_signals[FRAME_DONE] =
157 gtk_signal_new ("frame_done",
159 GTK_CLASS_TYPE (object_class),
160 GTK_SIGNAL_OFFSET (GdkPixbufLoaderClass, frame_done),
161 gtk_marshal_NONE__POINTER,
165 pixbuf_loader_signals[ANIMATION_DONE] =
166 gtk_signal_new ("animation_done",
168 GTK_CLASS_TYPE (object_class),
169 GTK_SIGNAL_OFFSET (GdkPixbufLoaderClass, animation_done),
170 gtk_marshal_NONE__NONE,
173 pixbuf_loader_signals[CLOSED] =
174 gtk_signal_new ("closed",
176 GTK_CLASS_TYPE (object_class),
177 GTK_SIGNAL_OFFSET (GdkPixbufLoaderClass, closed),
178 gtk_marshal_NONE__NONE,
181 gtk_object_class_add_signals (object_class, pixbuf_loader_signals, LAST_SIGNAL);
183 object_class->destroy = gdk_pixbuf_loader_destroy;
184 gobject_class->finalize = gdk_pixbuf_loader_finalize;
188 gdk_pixbuf_loader_init (GdkPixbufLoader *loader)
190 GdkPixbufLoaderPrivate *priv;
192 priv = g_new0 (GdkPixbufLoaderPrivate, 1);
193 loader->private = priv;
197 gdk_pixbuf_loader_destroy (GtkObject *object)
199 GdkPixbufLoader *loader;
200 GdkPixbufLoaderPrivate *priv = NULL;
202 g_return_if_fail (object != NULL);
203 g_return_if_fail (GDK_IS_PIXBUF_LOADER (object));
205 loader = GDK_PIXBUF_LOADER (object);
206 priv = loader->private;
209 gdk_pixbuf_loader_close (loader);
212 gdk_pixbuf_animation_unref (priv->animation);
214 gdk_pixbuf_unref (priv->pixbuf);
216 if (GTK_OBJECT_CLASS (parent_class)->destroy)
217 (* GTK_OBJECT_CLASS (parent_class)->destroy) (object);
221 gdk_pixbuf_loader_finalize (GObject *object)
223 GdkPixbufLoader *loader;
224 GdkPixbufLoaderPrivate *priv = NULL;
226 loader = GDK_PIXBUF_LOADER (object);
227 priv = loader->private;
231 if (G_OBJECT_CLASS (parent_class)->finalize)
232 (* G_OBJECT_CLASS (parent_class)->finalize) (object);
236 gdk_pixbuf_loader_prepare (GdkPixbuf *pixbuf, gpointer loader)
238 GdkPixbufLoaderPrivate *priv = NULL;
240 priv = GDK_PIXBUF_LOADER (loader)->private;
241 gdk_pixbuf_ref (pixbuf);
242 g_assert (priv->pixbuf == NULL);
244 priv->pixbuf = pixbuf;
245 gtk_signal_emit (GTK_OBJECT (loader), pixbuf_loader_signals[AREA_PREPARED]);
249 gdk_pixbuf_loader_update (GdkPixbuf *pixbuf, guint x, guint y, guint width, guint height, gpointer loader)
251 GdkPixbufLoaderPrivate *priv = NULL;
253 priv = GDK_PIXBUF_LOADER (loader)->private;
255 gtk_signal_emit (GTK_OBJECT (loader),
256 pixbuf_loader_signals[AREA_UPDATED],
258 /* sanity check in here. Defend against an errant loader */
259 MIN (width, gdk_pixbuf_get_width (priv->pixbuf)),
260 MIN (height, gdk_pixbuf_get_height (priv->pixbuf)));
264 gdk_pixbuf_loader_frame_done (GdkPixbufFrame *frame, gpointer loader)
266 GdkPixbufLoaderPrivate *priv = NULL;
268 priv = GDK_PIXBUF_LOADER (loader)->private;
272 if (priv->animation == NULL) {
273 priv->animation = g_new0 (GdkPixbufAnimation, 1);
274 priv->animation->n_frames = 0;
275 priv->animation->ref_count = 1;
276 priv->animation->width = gdk_pixbuf_get_width (frame->pixbuf) + frame->x_offset;
277 priv->animation->height = gdk_pixbuf_get_height (frame->pixbuf) + frame->y_offset;
281 /* update bbox size */
282 w = gdk_pixbuf_get_width (frame->pixbuf) + frame->x_offset;
283 h = gdk_pixbuf_get_height (frame->pixbuf) + frame->y_offset;
285 if (w > priv->animation->width) {
286 priv->animation->width = w;
288 if (h > priv->animation->height) {
289 priv->animation->height = h;
293 priv->animation->frames = g_list_append (priv->animation->frames, frame);
294 priv->animation->n_frames ++;
295 gtk_signal_emit (GTK_OBJECT (loader),
296 pixbuf_loader_signals[FRAME_DONE],
301 gdk_pixbuf_loader_animation_done (GdkPixbuf *pixbuf, gpointer loader)
303 GdkPixbufLoaderPrivate *priv = NULL;
304 GdkPixbufFrame *frame;
305 GList *current = NULL;
308 priv = GDK_PIXBUF_LOADER (loader)->private;
311 current = gdk_pixbuf_animation_get_frames (priv->animation);
314 frame = (GdkPixbufFrame *) current->data;
316 /* update bbox size */
317 w = gdk_pixbuf_get_width (frame->pixbuf) + frame->x_offset;
318 h = gdk_pixbuf_get_height (frame->pixbuf) + frame->y_offset;
320 if (w > priv->animation->width) {
321 priv->animation->width = w;
323 if (h > priv->animation->height) {
324 priv->animation->height = h;
326 current = current->next;
329 gtk_signal_emit (GTK_OBJECT (loader),
330 pixbuf_loader_signals[ANIMATION_DONE]);
336 * gdk_pixbuf_loader_new:
338 * Creates a new pixbuf loader object.
340 * Return value: A newly-created pixbuf loader.
343 gdk_pixbuf_loader_new (void)
345 return gtk_type_new (gdk_pixbuf_loader_get_type ());
349 gdk_pixbuf_loader_load_module(GdkPixbufLoader *loader)
351 GdkPixbufLoaderPrivate *priv = loader->private;
353 priv->image_module = gdk_pixbuf_get_module (priv->header_buf, priv->header_buf_offset);
355 if (priv->image_module == NULL)
358 if (priv->image_module->module == NULL)
359 gdk_pixbuf_load_module (priv->image_module);
361 if (priv->image_module->module == NULL)
364 if ((priv->image_module->begin_load == NULL) ||
365 (priv->image_module->stop_load == NULL) ||
366 (priv->image_module->load_increment == NULL)) {
367 g_warning ("module %s does not support incremental loading.\n",
368 priv->image_module->module_name);
372 priv->context = (*priv->image_module->begin_load) (gdk_pixbuf_loader_prepare,
373 gdk_pixbuf_loader_update,
374 gdk_pixbuf_loader_frame_done,
375 gdk_pixbuf_loader_animation_done,
378 if (priv->context == NULL) {
379 g_warning("Failed to begin progressive load");
383 if( (* priv->image_module->load_increment) (priv->context, priv->header_buf, priv->header_buf_offset) )
384 return priv->header_buf_offset;
390 gdk_pixbuf_loader_eat_header_write (GdkPixbufLoader *loader, const guchar *buf, size_t count)
393 GdkPixbufLoaderPrivate *priv = loader->private;
395 nbytes = MIN(LOADER_HEADER_SIZE - priv->header_buf_offset, count);
396 memcpy (priv->header_buf + priv->header_buf_offset, buf, nbytes);
398 priv->header_buf_offset += nbytes;
400 if(priv->header_buf_offset >= LOADER_HEADER_SIZE) {
401 if (gdk_pixbuf_loader_load_module(loader) == 0)
408 * gdk_pixbuf_loader_write:
409 * @loader: A pixbuf loader.
410 * @buf: Pointer to image data.
411 * @count: Length of the @buf buffer in bytes.
413 * This will cause a pixbuf loader to parse the next @count bytes of an image.
414 * It will return TRUE if the data was loaded successfully, and FALSE if an
415 * error occurred. In the latter case, the loader will be closed, and will not
416 * accept further writes.
418 * Return value: #TRUE if the write was successful, or #FALSE if the loader
419 * cannot parse the buffer.
422 gdk_pixbuf_loader_write (GdkPixbufLoader *loader, const guchar *buf, size_t count)
424 GdkPixbufLoaderPrivate *priv;
426 g_return_val_if_fail (loader != NULL, FALSE);
427 g_return_val_if_fail (GDK_IS_PIXBUF_LOADER (loader), FALSE);
429 g_return_val_if_fail (buf != NULL, FALSE);
430 g_return_val_if_fail (count >= 0, FALSE);
432 priv = loader->private;
434 /* we expect it's not to be closed */
435 g_return_val_if_fail (priv->closed == FALSE, FALSE);
437 if (priv->image_module == NULL) {
440 eaten = gdk_pixbuf_loader_eat_header_write(loader, buf, count);
448 if (count > 0 && priv->image_module->load_increment)
449 return (* priv->image_module->load_increment) (priv->context, buf, count);
455 * gdk_pixbuf_loader_get_pixbuf:
456 * @loader: A pixbuf loader.
458 * Queries the GdkPixbuf that a pixbuf loader is currently creating. In general
459 * it only makes sense to call this function afer the "area_prepared" signal has
460 * been emitted by the loader; this means that enough data has been read to know
461 * the size of the image that will be allocated. If the loader has not received
462 * enough data via gdk_pixbuf_loader_write(), then this function returns NULL.
463 * The returned pixbuf will be the same in all future calls to the loader, so
464 * simply calling gdk_pixbuf_ref() should be sufficient to continue using it.
466 * Return value: The GdkPixbuf that the loader is creating, or NULL if not
467 * enough data has been read to determine how to create the image buffer.
470 gdk_pixbuf_loader_get_pixbuf (GdkPixbufLoader *loader)
472 GdkPixbufLoaderPrivate *priv;
474 g_return_val_if_fail (loader != NULL, NULL);
475 g_return_val_if_fail (GDK_IS_PIXBUF_LOADER (loader), NULL);
477 priv = loader->private;
483 * gdk_pixbuf_loader_get_animation:
484 * @loader: A pixbuf loader
486 * Queries the GdkPixbufAnimation that a pixbuf loader is currently creating.
487 * In general it only makes sense to call this function afer the "area_prepared"
488 * signal has been emitted by the loader. If the image is not an animation,
489 * then it will return NULL.
491 * Return value: The GdkPixbufAnimation that the loader is loading, or NULL if
492 not enough data has been read to determine the information.
495 gdk_pixbuf_loader_get_animation (GdkPixbufLoader *loader)
497 GdkPixbufLoaderPrivate *priv;
499 g_return_val_if_fail (loader != NULL, NULL);
500 g_return_val_if_fail (GDK_IS_PIXBUF_LOADER (loader), NULL);
502 priv = loader->private;
504 return priv->animation;
508 * gdk_pixbuf_loader_close:
509 * @loader: A pixbuf loader.
511 * Informs a pixbuf loader that no further writes with gdk_pixbuf_load_write()
512 * will occur, so that it can free its internal loading structures.
515 gdk_pixbuf_loader_close (GdkPixbufLoader *loader)
517 GdkPixbufLoaderPrivate *priv;
519 g_return_if_fail (loader != NULL);
520 g_return_if_fail (GDK_IS_PIXBUF_LOADER (loader));
522 priv = loader->private;
524 /* we expect it's not closed */
525 g_return_if_fail (priv->closed == FALSE);
527 /* We have less the 128 bytes in the image. Flush it, and keep going. */
528 if (priv->image_module == NULL)
529 gdk_pixbuf_loader_load_module (loader);
531 if (priv->image_module && priv->image_module->stop_load)
532 (* priv->image_module->stop_load) (priv->context);
536 gtk_signal_emit (GTK_OBJECT (loader),
537 pixbuf_loader_signals[CLOSED]);