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-loader.h"
29 #include "gdk-pixbuf-io.h"
30 #include <gtk/gtksignal.h>
41 static GtkObjectClass *parent_class;
43 static void gdk_pixbuf_loader_class_init (GdkPixbufLoaderClass *klass);
44 static void gdk_pixbuf_loader_init (GdkPixbufLoader *loader);
45 static void gdk_pixbuf_loader_destroy (GtkObject *loader);
46 static void gdk_pixbuf_loader_finalize (GtkObject *loader);
48 static guint pixbuf_loader_signals[LAST_SIGNAL] = { 0 };
54 #define LOADER_HEADER_SIZE 128
59 gchar header_buf[LOADER_HEADER_SIZE];
60 gint header_buf_offset;
61 GdkPixbufModule *image_module;
63 } 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]),
89 * gdk_pixbuf_loader_get_type:
92 * Registers the &GdkPixubfLoader class if necessary, and returns the type ID
95 * Return value: The type ID of the &GdkPixbufLoader class.
98 gdk_pixbuf_loader_get_type (void)
100 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 GtkObjectClass *object_class;
125 object_class = (GtkObjectClass *) class;
127 parent_class = gtk_type_class (gtk_object_get_type ());
129 pixbuf_loader_signals[AREA_PREPARED] =
130 gtk_signal_new ("area_prepared",
133 GTK_SIGNAL_OFFSET (GdkPixbufLoaderClass, area_prepared),
134 gtk_marshal_NONE__NONE,
137 pixbuf_loader_signals[AREA_UPDATED] =
138 gtk_signal_new ("area_updated",
141 GTK_SIGNAL_OFFSET (GdkPixbufLoaderClass, area_updated),
142 gtk_marshal_NONE__INT_INT_INT_INT,
149 pixbuf_loader_signals[CLOSED] =
150 gtk_signal_new ("closed",
153 GTK_SIGNAL_OFFSET (GdkPixbufLoaderClass, closed),
154 gtk_marshal_NONE__NONE,
157 gtk_object_class_add_signals (object_class, pixbuf_loader_signals, LAST_SIGNAL);
159 object_class->destroy = gdk_pixbuf_loader_destroy;
160 object_class->finalize = gdk_pixbuf_loader_finalize;
164 gdk_pixbuf_loader_init (GdkPixbufLoader *loader)
166 GdkPixbufLoaderPrivate *priv;
168 priv = g_new0 (GdkPixbufLoaderPrivate, 1);
169 loader->private = priv;
173 gdk_pixbuf_loader_destroy (GtkObject *object)
175 GdkPixbufLoader *loader;
176 GdkPixbufLoaderPrivate *priv = NULL;
178 g_return_if_fail (object != NULL);
179 g_return_if_fail (GDK_IS_PIXBUF_LOADER (object));
181 loader = GDK_PIXBUF_LOADER (object);
182 priv = loader->private;
185 gdk_pixbuf_loader_close (loader);
188 gdk_pixbuf_unref (priv->pixbuf);
190 if (GTK_OBJECT_CLASS (parent_class)->destroy)
191 (* GTK_OBJECT_CLASS (parent_class)->destroy) (object);
195 gdk_pixbuf_loader_finalize (GtkObject *object)
197 GdkPixbufLoader *loader;
198 GdkPixbufLoaderPrivate *priv = NULL;
200 loader = GDK_PIXBUF_LOADER (object);
201 priv = loader->private;
205 if (GTK_OBJECT_CLASS (parent_class)->finalize)
206 (* GTK_OBJECT_CLASS (parent_class)->finalize) (object);
210 gdk_pixbuf_loader_prepare (GdkPixbuf *pixbuf, gpointer loader)
212 GdkPixbufLoaderPrivate *priv = NULL;
214 priv = GDK_PIXBUF_LOADER (loader)->private;
215 gdk_pixbuf_ref (pixbuf);
216 g_assert (priv->pixbuf == NULL);
218 priv->pixbuf = pixbuf;
219 gtk_signal_emit (GTK_OBJECT (loader), pixbuf_loader_signals[AREA_PREPARED]);
225 * gdk_pixbuf_loader_new:
228 * Creates a new pixbuf loader object.
230 * Return value: A newly-created pixbuf loader.
233 gdk_pixbuf_loader_new (void)
235 return gtk_type_new (gdk_pixbuf_loader_get_type ());
239 gdk_pixbuf_loader_load_module(GdkPixbufLoader *loader)
241 GdkPixbufLoaderPrivate *priv = loader->private;
243 priv->image_module = gdk_pixbuf_get_module (priv->header_buf, priv->header_buf_offset);
245 if (priv->image_module == NULL)
248 if (priv->image_module->module == NULL)
249 gdk_pixbuf_load_module (priv->image_module);
251 if (priv->image_module->module == NULL)
254 if ((priv->image_module->begin_load == NULL) ||
255 (priv->image_module->stop_load == NULL) ||
256 (priv->image_module->load_increment == NULL)) {
257 g_warning ("module %s does not support incremental loading.\n",
258 priv->image_module->module_name);
262 priv->context = (*priv->image_module->begin_load) (gdk_pixbuf_loader_prepare, loader);
264 if (priv->context == NULL) {
265 g_warning("Failed to begin progressive load");
269 if( (* priv->image_module->load_increment) (priv->context, priv->header_buf, priv->header_buf_offset) )
270 return priv->header_buf_offset;
276 gdk_pixbuf_loader_eat_header_write (GdkPixbufLoader *loader, const gchar *buf, size_t count)
279 GdkPixbufLoaderPrivate *priv = loader->private;
281 nbytes = MIN(LOADER_HEADER_SIZE - priv->header_buf_offset, count);
282 memcpy (priv->header_buf + priv->header_buf_offset, buf, nbytes);
284 priv->header_buf_offset += nbytes;
286 if(priv->header_buf_offset >= LOADER_HEADER_SIZE)
287 gdk_pixbuf_loader_load_module(loader);
293 * gdk_pixbuf_loader_write:
294 * @loader: A pixbuf loader.
295 * @buf: The image data.
296 * @count: The length of @buf in bytes.
298 * This will load the next @size bytes of the image. It will return TRUE if the
299 * data was loaded successfully, and FALSE if an error occurred. In this case,
300 * the loader will be closed, and will not accept further writes.
302 * Return value: Returns TRUE if the write was successful -- FALSE if the loader
303 * cannot parse the buf.
306 gdk_pixbuf_loader_write (GdkPixbufLoader *loader, const gchar *buf, size_t count)
308 GdkPixbufLoaderPrivate *priv;
310 g_return_val_if_fail (loader != NULL, FALSE);
311 g_return_val_if_fail (GDK_IS_PIXBUF_LOADER (loader), FALSE);
313 g_return_val_if_fail (buf != NULL, FALSE);
314 g_return_val_if_fail (count >= 0, FALSE);
316 priv = loader->private;
318 /* we expect it's not to be closed */
319 g_return_val_if_fail (priv->closed == FALSE, FALSE);
321 if (priv->image_module == NULL) {
324 eaten = gdk_pixbuf_loader_eat_header_write(loader, buf, count);
332 if (count > 0 && priv->image_module->load_increment)
333 return (* priv->image_module->load_increment) (priv->context, buf, count);
339 * gdk_pixbuf_loader_get_pixbuf:
340 * @loader: A pixbuf loader.
342 * Gets the GdkPixbuf that the loader is currently loading. If the loader
343 * hasn't been enough data via gdk_pixbuf_loader_write, then NULL is returned.
344 * Any application using this function should check for this value when it is
345 * used. The pixbuf returned will be the same in all future calls to the
346 * loader, so simply calling a gdk_pixbuf_ref() should be sufficient to continue
349 * Return value: The GdkPixbuf that the loader is loading.
352 gdk_pixbuf_loader_get_pixbuf (GdkPixbufLoader *loader)
354 GdkPixbufLoaderPrivate *priv;
356 g_return_val_if_fail (loader != NULL, NULL);
357 g_return_val_if_fail (GDK_IS_PIXBUF_LOADER (loader), NULL);
359 priv = loader->private;
365 * gdk_pixbuf_loader_close:
366 * @loader: A pixbuf loader.
368 * Tells the loader to stop accepting writes.
371 gdk_pixbuf_loader_close (GdkPixbufLoader *loader)
373 GdkPixbufLoaderPrivate *priv;
375 g_return_if_fail (loader != NULL);
376 g_return_if_fail (GDK_IS_PIXBUF_LOADER (loader));
378 priv = loader->private;
380 /* we expect it's not closed */
381 g_return_if_fail (priv->closed == FALSE);
383 /* We have less the 128 bytes in the image. Flush it, and keep going. */
384 if (priv->image_module == NULL)
385 gdk_pixbuf_loader_load_module (loader);
387 if (priv->image_module && priv->image_module->stop_load)
388 (* priv->image_module->stop_load) (priv->context);