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.
29 #include <gtk/gtksignal.h>
30 #include "gdk-pixbuf-private.h"
31 #include "gdk-pixbuf-loader.h"
32 #include "gdk-pixbuf-io.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 (GtkObject *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 GtkObjectClass *object_class;
130 object_class = (GtkObjectClass *) class;
132 parent_class = gtk_type_class (gtk_object_get_type ());
134 pixbuf_loader_signals[AREA_PREPARED] =
135 gtk_signal_new ("area_prepared",
138 GTK_SIGNAL_OFFSET (GdkPixbufLoaderClass, area_prepared),
139 gtk_marshal_NONE__NONE,
142 pixbuf_loader_signals[AREA_UPDATED] =
143 gtk_signal_new ("area_updated",
146 GTK_SIGNAL_OFFSET (GdkPixbufLoaderClass, area_updated),
147 gtk_marshal_NONE__INT_INT_INT_INT,
154 pixbuf_loader_signals[FRAME_DONE] =
155 gtk_signal_new ("frame_done",
158 GTK_SIGNAL_OFFSET (GdkPixbufLoaderClass, frame_done),
159 gtk_marshal_NONE__POINTER,
163 pixbuf_loader_signals[ANIMATION_DONE] =
164 gtk_signal_new ("animation_done",
167 GTK_SIGNAL_OFFSET (GdkPixbufLoaderClass, animation_done),
168 gtk_marshal_NONE__NONE,
171 pixbuf_loader_signals[CLOSED] =
172 gtk_signal_new ("closed",
175 GTK_SIGNAL_OFFSET (GdkPixbufLoaderClass, closed),
176 gtk_marshal_NONE__NONE,
179 gtk_object_class_add_signals (object_class, pixbuf_loader_signals, LAST_SIGNAL);
181 object_class->destroy = gdk_pixbuf_loader_destroy;
182 object_class->finalize = gdk_pixbuf_loader_finalize;
186 gdk_pixbuf_loader_init (GdkPixbufLoader *loader)
188 GdkPixbufLoaderPrivate *priv;
190 priv = g_new0 (GdkPixbufLoaderPrivate, 1);
191 loader->private = priv;
195 gdk_pixbuf_loader_destroy (GtkObject *object)
197 GdkPixbufLoader *loader;
198 GdkPixbufLoaderPrivate *priv = NULL;
200 g_return_if_fail (object != NULL);
201 g_return_if_fail (GDK_IS_PIXBUF_LOADER (object));
203 loader = GDK_PIXBUF_LOADER (object);
204 priv = loader->private;
207 gdk_pixbuf_loader_close (loader);
210 gdk_pixbuf_animation_unref (priv->animation);
212 gdk_pixbuf_unref (priv->pixbuf);
214 if (GTK_OBJECT_CLASS (parent_class)->destroy)
215 (* GTK_OBJECT_CLASS (parent_class)->destroy) (object);
219 gdk_pixbuf_loader_finalize (GtkObject *object)
221 GdkPixbufLoader *loader;
222 GdkPixbufLoaderPrivate *priv = NULL;
224 loader = GDK_PIXBUF_LOADER (object);
225 priv = loader->private;
229 if (GTK_OBJECT_CLASS (parent_class)->finalize)
230 (* GTK_OBJECT_CLASS (parent_class)->finalize) (object);
234 gdk_pixbuf_loader_prepare (GdkPixbuf *pixbuf, gpointer loader)
236 GdkPixbufLoaderPrivate *priv = NULL;
238 priv = GDK_PIXBUF_LOADER (loader)->private;
239 gdk_pixbuf_ref (pixbuf);
240 g_assert (priv->pixbuf == NULL);
242 priv->pixbuf = pixbuf;
243 gtk_signal_emit (GTK_OBJECT (loader), pixbuf_loader_signals[AREA_PREPARED]);
247 gdk_pixbuf_loader_update (GdkPixbuf *pixbuf, guint x, guint y, guint width, guint height, gpointer loader)
249 GdkPixbufLoaderPrivate *priv = NULL;
251 priv = GDK_PIXBUF_LOADER (loader)->private;
253 gtk_signal_emit (GTK_OBJECT (loader),
254 pixbuf_loader_signals[AREA_UPDATED],
256 /* sanity check in here. Defend against an errant loader */
257 MIN (width, gdk_pixbuf_get_width (priv->pixbuf)),
258 MIN (height, gdk_pixbuf_get_height (priv->pixbuf)));
262 gdk_pixbuf_loader_frame_done (GdkPixbufFrame *frame, gpointer loader)
264 GdkPixbufLoaderPrivate *priv = NULL;
266 priv = GDK_PIXBUF_LOADER (loader)->private;
270 if (priv->animation == NULL) {
271 priv->animation = g_new0 (GdkPixbufAnimation, 1);
272 priv->animation->n_frames = 0;
273 priv->animation->ref_count = 1;
274 priv->animation->width = gdk_pixbuf_get_width (frame->pixbuf);
275 priv->animation->height = gdk_pixbuf_get_height (frame->pixbuf);
279 /* update bbox size */
280 w = gdk_pixbuf_get_width (frame->pixbuf);
281 h = gdk_pixbuf_get_height (frame->pixbuf);
283 if (w > priv->animation->width) {
284 priv->animation->width = h;
286 if (h > priv->animation->height) {
287 priv->animation->height = h;
291 priv->animation->frames = g_list_append (priv->animation->frames, frame);
292 priv->animation->n_frames ++;
293 gtk_signal_emit (GTK_OBJECT (loader),
294 pixbuf_loader_signals[FRAME_DONE],
299 gdk_pixbuf_loader_animation_done (GdkPixbuf *pixbuf, gpointer loader)
301 gtk_signal_emit (GTK_OBJECT (loader),
302 pixbuf_loader_signals[ANIMATION_DONE]);
308 * gdk_pixbuf_loader_new:
310 * Creates a new pixbuf loader object.
312 * Return value: A newly-created pixbuf loader.
315 gdk_pixbuf_loader_new (void)
317 return gtk_type_new (gdk_pixbuf_loader_get_type ());
321 gdk_pixbuf_loader_load_module(GdkPixbufLoader *loader)
323 GdkPixbufLoaderPrivate *priv = loader->private;
325 priv->image_module = gdk_pixbuf_get_module (priv->header_buf, priv->header_buf_offset);
327 if (priv->image_module == NULL)
330 if (priv->image_module->module == NULL)
331 gdk_pixbuf_load_module (priv->image_module);
333 if (priv->image_module->module == NULL)
336 if ((priv->image_module->begin_load == NULL) ||
337 (priv->image_module->stop_load == NULL) ||
338 (priv->image_module->load_increment == NULL)) {
339 g_warning ("module %s does not support incremental loading.\n",
340 priv->image_module->module_name);
344 priv->context = (*priv->image_module->begin_load) (gdk_pixbuf_loader_prepare,
345 gdk_pixbuf_loader_update,
346 gdk_pixbuf_loader_frame_done,
347 gdk_pixbuf_loader_animation_done,
350 if (priv->context == NULL) {
351 g_warning("Failed to begin progressive load");
355 if( (* priv->image_module->load_increment) (priv->context, priv->header_buf, priv->header_buf_offset) )
356 return priv->header_buf_offset;
362 gdk_pixbuf_loader_eat_header_write (GdkPixbufLoader *loader, const guchar *buf, size_t count)
365 GdkPixbufLoaderPrivate *priv = loader->private;
367 nbytes = MIN(LOADER_HEADER_SIZE - priv->header_buf_offset, count);
368 memcpy (priv->header_buf + priv->header_buf_offset, buf, nbytes);
370 priv->header_buf_offset += nbytes;
372 if(priv->header_buf_offset >= LOADER_HEADER_SIZE) {
373 if (gdk_pixbuf_loader_load_module(loader) == 0)
380 * gdk_pixbuf_loader_write:
381 * @loader: A pixbuf loader.
382 * @buf: Pointer to image data.
383 * @count: Length of the @buf buffer in bytes.
385 * This will cause a pixbuf loader to parse the next @count bytes of an image.
386 * It will return TRUE if the data was loaded successfully, and FALSE if an
387 * error occurred. In the latter case, the loader will be closed, and will not
388 * accept further writes.
390 * Return value: #TRUE if the write was successful, or #FALSE if the loader
391 * cannot parse the buffer.
394 gdk_pixbuf_loader_write (GdkPixbufLoader *loader, const guchar *buf, size_t count)
396 GdkPixbufLoaderPrivate *priv;
398 g_return_val_if_fail (loader != NULL, FALSE);
399 g_return_val_if_fail (GDK_IS_PIXBUF_LOADER (loader), FALSE);
401 g_return_val_if_fail (buf != NULL, FALSE);
402 g_return_val_if_fail (count >= 0, FALSE);
404 priv = loader->private;
406 /* we expect it's not to be closed */
407 g_return_val_if_fail (priv->closed == FALSE, FALSE);
409 if (priv->image_module == NULL) {
412 eaten = gdk_pixbuf_loader_eat_header_write(loader, buf, count);
420 if (count > 0 && priv->image_module->load_increment)
421 return (* priv->image_module->load_increment) (priv->context, buf, count);
427 * gdk_pixbuf_loader_get_pixbuf:
428 * @loader: A pixbuf loader.
430 * Queries the GdkPixbuf that a pixbuf loader is currently creating. In general
431 * it only makes sense to call this function afer the "area_prepared" signal has
432 * been emitted by the loader; this means that enough data has been read to know
433 * the size of the image that will be allocated. If the loader has not received
434 * enough data via gdk_pixbuf_loader_write(), then this function returns NULL.
435 * The returned pixbuf will be the same in all future calls to the loader, so
436 * simply calling gdk_pixbuf_ref() should be sufficient to continue using it.
438 * Return value: The GdkPixbuf that the loader is creating, or NULL if not
439 * enough data has been read to determine how to create the image buffer.
442 gdk_pixbuf_loader_get_pixbuf (GdkPixbufLoader *loader)
444 GdkPixbufLoaderPrivate *priv;
446 g_return_val_if_fail (loader != NULL, NULL);
447 g_return_val_if_fail (GDK_IS_PIXBUF_LOADER (loader), NULL);
449 priv = loader->private;
455 * gdk_pixbuf_loader_get_animation:
456 * @loader: A pixbuf loader
458 * Queries the GdkPixbufAnimation that a pixbuf loader is currently creating.
459 * In general it only makes sense to call this function afer the "area_prepared"
460 * signal has been emitted by the loader. If the image is not an animation,
461 * then it will return NULL.
463 * Return value: The GdkPixbufAnimation that the loader is loading, or NULL if
464 not enough data has been read to determine the information.
467 gdk_pixbuf_loader_get_animation (GdkPixbufLoader *loader)
469 GdkPixbufLoaderPrivate *priv;
471 g_return_val_if_fail (loader != NULL, NULL);
472 g_return_val_if_fail (GDK_IS_PIXBUF_LOADER (loader), NULL);
474 priv = loader->private;
476 return priv->animation;
480 * gdk_pixbuf_loader_close:
481 * @loader: A pixbuf loader.
483 * Informs a pixbuf loader that no further writes with gdk_pixbuf_load_write()
484 * will occur, so that it can free its internal loading structures.
487 gdk_pixbuf_loader_close (GdkPixbufLoader *loader)
489 GdkPixbufLoaderPrivate *priv;
491 g_return_if_fail (loader != NULL);
492 g_return_if_fail (GDK_IS_PIXBUF_LOADER (loader));
494 priv = loader->private;
496 /* we expect it's not closed */
497 g_return_if_fail (priv->closed == FALSE);
499 /* We have less the 128 bytes in the image. Flush it, and keep going. */
500 if (priv->image_module == NULL)
501 gdk_pixbuf_loader_load_module (loader);
503 if (priv->image_module && priv->image_module->stop_load)
504 (* priv->image_module->stop_load) (priv->context);
508 gtk_signal_emit (GTK_OBJECT (loader),
509 pixbuf_loader_signals[CLOSED]);