]> Pileus Git - ~andy/gtk/blob - gdk-pixbuf/gdk-pixbuf-loader.c
make the symlink. Does not work for srcdir != buildir != . Beats me why; I
[~andy/gtk] / gdk-pixbuf / gdk-pixbuf-loader.c
1 /* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
2
3 /* GdkPixbuf library - Main header file
4  *
5  * Copyright (C) 1999 The Free Software Foundation
6  *
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>
11  *
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.
16  *
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.
21  *
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.
26  */
27
28 #include "gdk-pixbuf/gdk-pixbuf-loader.h"
29 #include "gdk-pixbuf/gdk-pixbuf-io.h"
30 #include <gtk/gtksignal.h>
31
32 \f
33
34 enum {
35         AREA_UPDATED,
36         AREA_PREPARED,
37         CLOSED,
38         LAST_SIGNAL
39 };
40
41 static GtkObjectClass *parent_class;
42
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);
47
48 static guint pixbuf_loader_signals[LAST_SIGNAL] = { 0 };
49
50 \f
51
52 /* Internal data */
53
54 #define LOADER_HEADER_SIZE 128
55
56 typedef struct {
57         GdkPixbuf *pixbuf;
58         gboolean closed;
59         gchar header_buf[LOADER_HEADER_SIZE];
60         gint header_buf_offset;
61         GdkPixbufModule *image_module;
62         gpointer context;
63 } GdkPixbufLoaderPrivate;
64
65 \f
66
67 /* our marshaller */
68 typedef void (* GtkSignal_NONE__INT_INT_INT_INT) (GtkObject *object,
69                                                   gint arg1, gint arg2, gint arg3, gint arg4,
70                                                   gpointer user_data);
71 static void
72 gtk_marshal_NONE__INT_INT_INT_INT (GtkObject *object, GtkSignalFunc func, gpointer func_data,
73                                    GtkArg * args)
74 {
75         GtkSignal_NONE__INT_INT_INT_INT rfunc;
76
77         rfunc = (GtkSignal_NONE__INT_INT_INT_INT) func;
78         (*rfunc) (object,
79                   GTK_VALUE_INT (args[0]),
80                   GTK_VALUE_INT (args[1]),
81                   GTK_VALUE_INT (args[2]),
82                   GTK_VALUE_INT (args[3]),
83                   func_data);
84 }
85
86 \f
87
88 /**
89  * gdk_pixbuf_loader_get_type:
90  * @void: 
91  * 
92  * Registers the #GdkPixubfLoader class if necessary, and returns the type ID
93  * associated to it.
94  * 
95  * Return value: The type ID of the #GdkPixbufLoader class.
96  **/
97 GtkType
98 gdk_pixbuf_loader_get_type (void)
99 {
100         static GtkType loader_type = 0;
101
102         if (!loader_type) {
103                 static const GtkTypeInfo loader_info = {
104                         "GdkPixbufLoader",
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,
112                 };
113
114                 loader_type = gtk_type_unique (GTK_TYPE_OBJECT, &loader_info);
115         }
116
117         return loader_type;
118 }
119
120 static void
121 gdk_pixbuf_loader_class_init (GdkPixbufLoaderClass *class)
122 {
123         GtkObjectClass *object_class;
124
125         object_class = (GtkObjectClass *) class;
126
127         parent_class = gtk_type_class (gtk_object_get_type ());
128
129         pixbuf_loader_signals[AREA_PREPARED] =
130                 gtk_signal_new ("area_prepared",
131                                 GTK_RUN_LAST,
132                                 parent_class->type,
133                                 GTK_SIGNAL_OFFSET (GdkPixbufLoaderClass, area_prepared),
134                                 gtk_marshal_NONE__NONE,
135                                 GTK_TYPE_NONE, 0);
136
137         pixbuf_loader_signals[AREA_UPDATED] =
138                 gtk_signal_new ("area_updated",
139                                 GTK_RUN_LAST,
140                                 parent_class->type,
141                                 GTK_SIGNAL_OFFSET (GdkPixbufLoaderClass, area_updated),
142                                 gtk_marshal_NONE__INT_INT_INT_INT,
143                                 GTK_TYPE_NONE, 4,
144                                 GTK_TYPE_INT,
145                                 GTK_TYPE_INT,
146                                 GTK_TYPE_INT,
147                                 GTK_TYPE_INT);
148
149         pixbuf_loader_signals[CLOSED] =
150                 gtk_signal_new ("closed",
151                                 GTK_RUN_LAST,
152                                 parent_class->type,
153                                 GTK_SIGNAL_OFFSET (GdkPixbufLoaderClass, closed),
154                                 gtk_marshal_NONE__NONE,
155                                 GTK_TYPE_NONE, 0);
156
157         gtk_object_class_add_signals (object_class, pixbuf_loader_signals, LAST_SIGNAL);
158
159         object_class->destroy = gdk_pixbuf_loader_destroy;
160         object_class->finalize = gdk_pixbuf_loader_finalize;
161 }
162
163 static void
164 gdk_pixbuf_loader_init (GdkPixbufLoader *loader)
165 {
166         GdkPixbufLoaderPrivate *priv;
167
168         priv = g_new0 (GdkPixbufLoaderPrivate, 1);
169         loader->private = priv;
170 }
171
172 static void
173 gdk_pixbuf_loader_destroy (GtkObject *object)
174 {
175         GdkPixbufLoader *loader;
176         GdkPixbufLoaderPrivate *priv = NULL;
177
178         g_return_if_fail (object != NULL);
179         g_return_if_fail (GDK_IS_PIXBUF_LOADER (object));
180
181         loader = GDK_PIXBUF_LOADER (object);
182         priv = loader->private;
183
184         if (!priv->closed)
185                 gdk_pixbuf_loader_close (loader);
186
187         if (priv->pixbuf)
188                 gdk_pixbuf_unref (priv->pixbuf);
189
190         if (GTK_OBJECT_CLASS (parent_class)->destroy)
191                 (* GTK_OBJECT_CLASS (parent_class)->destroy) (object);
192 }
193
194 static void
195 gdk_pixbuf_loader_finalize (GtkObject *object)
196 {
197         GdkPixbufLoader *loader;
198         GdkPixbufLoaderPrivate *priv = NULL;
199
200         loader = GDK_PIXBUF_LOADER (object);
201         priv = loader->private;
202
203         g_free (priv);
204
205         if (GTK_OBJECT_CLASS (parent_class)->finalize)
206                 (* GTK_OBJECT_CLASS (parent_class)->finalize) (object);
207 }
208
209 static void
210 gdk_pixbuf_loader_prepare (GdkPixbuf *pixbuf, gpointer loader)
211 {
212         GdkPixbufLoaderPrivate *priv = NULL;
213
214         priv = GDK_PIXBUF_LOADER (loader)->private;
215         gdk_pixbuf_ref (pixbuf);
216         g_assert (priv->pixbuf == NULL);
217
218         priv->pixbuf = pixbuf;
219         gtk_signal_emit (GTK_OBJECT (loader), pixbuf_loader_signals[AREA_PREPARED]);
220 }
221
222 static void
223 gdk_pixbuf_loader_update (GdkPixbuf *pixbuf, gpointer loader, guint x, guint y, guint width, guint height)
224 {
225         GdkPixbufLoaderPrivate *priv = NULL;
226
227         priv = GDK_PIXBUF_LOADER (loader)->private;
228
229         gtk_signal_emit (GTK_OBJECT (loader),
230                          pixbuf_loader_signals[AREA_UPDATED],
231                          x, y,
232                          /* sanity check in here.  Defend against an errant loader */
233                          MIN (width, gdk_pixbuf_get_width (priv->pixbuf)),
234                          MIN (height, gdk_pixbuf_get_height (priv->pixbuf)));
235 }
236
237 \f
238
239 /**
240  * gdk_pixbuf_loader_new:
241  * @void: 
242  * 
243  * Creates a new pixbuf loader object.
244  * 
245  * Return value: A newly-created pixbuf loader.
246  **/
247 GdkPixbufLoader *
248 gdk_pixbuf_loader_new (void)
249 {
250         return gtk_type_new (gdk_pixbuf_loader_get_type ());
251 }
252
253 static int
254 gdk_pixbuf_loader_load_module(GdkPixbufLoader *loader)
255 {
256         GdkPixbufLoaderPrivate *priv = loader->private;
257
258         priv->image_module = gdk_pixbuf_get_module (priv->header_buf, priv->header_buf_offset);
259
260         if (priv->image_module == NULL)
261                 return 0;
262
263         if (priv->image_module->module == NULL)
264                 gdk_pixbuf_load_module (priv->image_module);
265
266         if (priv->image_module->module == NULL)
267                 return 0;
268
269         if ((priv->image_module->begin_load == NULL) ||
270             (priv->image_module->stop_load == NULL) ||
271             (priv->image_module->load_increment == NULL)) {
272                 g_warning ("module %s does not support incremental loading.\n",
273                            priv->image_module->module_name);
274                 return 0;
275         }
276
277         priv->context = (*priv->image_module->begin_load) (gdk_pixbuf_loader_prepare, gdk_pixbuf_loader_update, loader);
278
279         if (priv->context == NULL) {
280                 g_warning("Failed to begin progressive load");
281                 return 0;
282         }
283
284         if( (* priv->image_module->load_increment) (priv->context, priv->header_buf, priv->header_buf_offset) )
285                 return priv->header_buf_offset;
286  
287         return 0;
288 }
289
290 static int
291 gdk_pixbuf_loader_eat_header_write (GdkPixbufLoader *loader, const gchar *buf, size_t count)
292 {
293         int nbytes;
294         GdkPixbufLoaderPrivate *priv = loader->private;
295
296         nbytes = MIN(LOADER_HEADER_SIZE - priv->header_buf_offset, count);
297         memcpy (priv->header_buf + priv->header_buf_offset, buf, nbytes);
298             
299         priv->header_buf_offset += nbytes;
300             
301         if(priv->header_buf_offset >= LOADER_HEADER_SIZE)
302                 gdk_pixbuf_loader_load_module(loader);
303
304         return nbytes;
305 }
306
307 /**
308  * gdk_pixbuf_loader_write:
309  * @loader: A pixbuf loader.
310  * @buf: Pointer to image data.
311  * @count: Length of the @buf buffer in bytes.
312  *
313  * This will cause a pixbuf loader to parse the next @count bytes of an image.
314  * It will return TRUE if the data was loaded successfully, and FALSE if an
315  * error occurred.  In the latter case, the loader will be closed, and will not
316  * accept further writes.
317  *
318  * Return value: #TRUE if the write was successful, or #FALSE if the loader
319  * cannot parse the buffer.
320  **/
321 gboolean
322 gdk_pixbuf_loader_write (GdkPixbufLoader *loader, const gchar *buf, size_t count)
323 {
324         GdkPixbufLoaderPrivate *priv;
325
326         g_return_val_if_fail (loader != NULL, FALSE);
327         g_return_val_if_fail (GDK_IS_PIXBUF_LOADER (loader), FALSE);
328
329         g_return_val_if_fail (buf != NULL, FALSE);
330         g_return_val_if_fail (count >= 0, FALSE);
331
332         priv = loader->private;
333
334         /* we expect it's not to be closed */
335         g_return_val_if_fail (priv->closed == FALSE, FALSE);
336
337         if (priv->image_module == NULL) {
338                 int eaten;
339
340                 eaten = gdk_pixbuf_loader_eat_header_write(loader, buf, count);
341                 if (eaten <= 0)
342                         return FALSE;
343
344                 count -= eaten;
345                 buf += eaten;
346         }
347
348         if (count > 0 && priv->image_module->load_increment)
349                 return (* priv->image_module->load_increment) (priv->context, buf, count);
350
351         return TRUE;
352 }
353
354 /**
355  * gdk_pixbuf_loader_get_pixbuf:
356  * @loader: A pixbuf loader.
357  *
358  * Queries the GdkPixbuf that a pixbuf loader is currently creating.  In general
359  * it only makes sense to call this function afer the "area_prepared" signal has
360  * been emitted by the loader; this means that enough data has been read to know
361  * the size of the image that will be allocated.  If the loader has not received
362  * enough data via gdk_pixbuf_loader_write(), then this function returns NULL.
363  * The returned pixbuf will be the same in all future calls to the loader, so
364  * simply calling gdk_pixbuf_ref() should be sufficient to continue using it.
365  *
366  * Return value: The GdkPixbuf that the loader is creating, or NULL if not
367  * enough data has been read to determine how to create the image buffer.
368  **/
369 GdkPixbuf *
370 gdk_pixbuf_loader_get_pixbuf (GdkPixbufLoader *loader)
371 {
372         GdkPixbufLoaderPrivate *priv;
373
374         g_return_val_if_fail (loader != NULL, NULL);
375         g_return_val_if_fail (GDK_IS_PIXBUF_LOADER (loader), NULL);
376
377         priv = loader->private;
378
379         return priv->pixbuf;
380 }
381
382 /**
383  * gdk_pixbuf_loader_close:
384  * @loader: A pixbuf loader.
385  *
386  * Informs a pixbuf loader that no further writes with gdk_pixbuf_load_write()
387  * will occur, so that it can free its internal loading structures.
388  **/
389 void
390 gdk_pixbuf_loader_close (GdkPixbufLoader *loader)
391 {
392         GdkPixbufLoaderPrivate *priv;
393
394         g_return_if_fail (loader != NULL);
395         g_return_if_fail (GDK_IS_PIXBUF_LOADER (loader));
396
397         priv = loader->private;
398
399         /* we expect it's not closed */
400         g_return_if_fail (priv->closed == FALSE);
401
402         /* We have less the 128 bytes in the image.  Flush it, and keep going. */
403         if (priv->image_module == NULL)
404                 gdk_pixbuf_loader_load_module (loader);
405
406         if (priv->image_module && priv->image_module->stop_load)
407                 (* priv->image_module->stop_load) (priv->context);
408
409         priv->closed = TRUE;
410
411         gtk_signal_emit (GTK_OBJECT (loader),
412                          pixbuf_loader_signals[CLOSED]);
413 }