]> Pileus Git - ~andy/gtk/blob - gdk-pixbuf/gdk-pixbuf-loader.c
Your eyes are bloodshot.
[~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 <config.h>
29 #include <gtk/gtksignal.h>
30 #include "gdk-pixbuf-private.h"
31 #include "gdk-pixbuf-loader.h"
32 #include "gdk-pixbuf-io.h"
33
34 \f
35
36 enum {
37         AREA_UPDATED,
38         AREA_PREPARED,
39         FRAME_DONE,
40         ANIMATION_DONE,
41         CLOSED,
42         LAST_SIGNAL
43 };
44
45 static GtkObjectClass *parent_class;
46
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);
51
52 static guint pixbuf_loader_signals[LAST_SIGNAL] = { 0 };
53
54 \f
55
56 /* Internal data */
57
58 #define LOADER_HEADER_SIZE 128
59
60 typedef struct {
61         GdkPixbuf *pixbuf;
62         GdkPixbufAnimation *animation;
63         gboolean closed;
64         guchar header_buf[LOADER_HEADER_SIZE];
65         gint header_buf_offset;
66         GdkPixbufModule *image_module;
67         gpointer context;
68 } GdkPixbufLoaderPrivate;
69
70 \f
71
72 /* our marshaller */
73 typedef void (* GtkSignal_NONE__INT_INT_INT_INT) (GtkObject *object,
74                                                   gint arg1, gint arg2, gint arg3, gint arg4,
75                                                   gpointer user_data);
76 static void
77 gtk_marshal_NONE__INT_INT_INT_INT (GtkObject *object, GtkSignalFunc func, gpointer func_data,
78                                    GtkArg * args)
79 {
80         GtkSignal_NONE__INT_INT_INT_INT rfunc;
81
82         rfunc = (GtkSignal_NONE__INT_INT_INT_INT) func;
83         (*rfunc) (object,
84                   GTK_VALUE_INT (args[0]),
85                   GTK_VALUE_INT (args[1]),
86                   GTK_VALUE_INT (args[2]),
87                   GTK_VALUE_INT (args[3]),
88                   func_data);
89 }
90
91 \f
92
93 /**
94  * gdk_pixbuf_loader_get_type:
95  * @void:
96  *
97  * Registers the #GdkPixubfLoader class if necessary, and returns the type ID
98  * associated to it.
99  *
100  * Return value: The type ID of the #GdkPixbufLoader class.
101  **/
102 GtkType
103 gdk_pixbuf_loader_get_type (void)
104 {
105         static GtkType loader_type = 0;
106
107         if (!loader_type) {
108                 static const GtkTypeInfo loader_info = {
109                         "GdkPixbufLoader",
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,
117                 };
118
119                 loader_type = gtk_type_unique (GTK_TYPE_OBJECT, &loader_info);
120         }
121
122         return loader_type;
123 }
124
125 static void
126 gdk_pixbuf_loader_class_init (GdkPixbufLoaderClass *class)
127 {
128         GtkObjectClass *object_class;
129
130         object_class = (GtkObjectClass *) class;
131
132         parent_class = gtk_type_class (gtk_object_get_type ());
133
134         pixbuf_loader_signals[AREA_PREPARED] =
135                 gtk_signal_new ("area_prepared",
136                                 GTK_RUN_LAST,
137                                 parent_class->type,
138                                 GTK_SIGNAL_OFFSET (GdkPixbufLoaderClass, area_prepared),
139                                 gtk_marshal_NONE__NONE,
140                                 GTK_TYPE_NONE, 0);
141
142         pixbuf_loader_signals[AREA_UPDATED] =
143                 gtk_signal_new ("area_updated",
144                                 GTK_RUN_LAST,
145                                 parent_class->type,
146                                 GTK_SIGNAL_OFFSET (GdkPixbufLoaderClass, area_updated),
147                                 gtk_marshal_NONE__INT_INT_INT_INT,
148                                 GTK_TYPE_NONE, 4,
149                                 GTK_TYPE_INT,
150                                 GTK_TYPE_INT,
151                                 GTK_TYPE_INT,
152                                 GTK_TYPE_INT);
153
154         pixbuf_loader_signals[FRAME_DONE] =
155                 gtk_signal_new ("frame_done",
156                                 GTK_RUN_LAST,
157                                 parent_class->type,
158                                 GTK_SIGNAL_OFFSET (GdkPixbufLoaderClass, frame_done),
159                                 gtk_marshal_NONE__POINTER,
160                                 GTK_TYPE_NONE, 1,
161                                 GTK_TYPE_POINTER);
162
163         pixbuf_loader_signals[ANIMATION_DONE] =
164                 gtk_signal_new ("animation_done",
165                                 GTK_RUN_LAST,
166                                 parent_class->type,
167                                 GTK_SIGNAL_OFFSET (GdkPixbufLoaderClass, animation_done),
168                                 gtk_marshal_NONE__NONE,
169                                 GTK_TYPE_NONE, 0);
170
171         pixbuf_loader_signals[CLOSED] =
172                 gtk_signal_new ("closed",
173                                 GTK_RUN_LAST,
174                                 parent_class->type,
175                                 GTK_SIGNAL_OFFSET (GdkPixbufLoaderClass, closed),
176                                 gtk_marshal_NONE__NONE,
177                                 GTK_TYPE_NONE, 0);
178
179         gtk_object_class_add_signals (object_class, pixbuf_loader_signals, LAST_SIGNAL);
180
181         object_class->destroy = gdk_pixbuf_loader_destroy;
182         object_class->finalize = gdk_pixbuf_loader_finalize;
183 }
184
185 static void
186 gdk_pixbuf_loader_init (GdkPixbufLoader *loader)
187 {
188         GdkPixbufLoaderPrivate *priv;
189
190         priv = g_new0 (GdkPixbufLoaderPrivate, 1);
191         loader->private = priv;
192 }
193
194 static void
195 gdk_pixbuf_loader_destroy (GtkObject *object)
196 {
197         GdkPixbufLoader *loader;
198         GdkPixbufLoaderPrivate *priv = NULL;
199
200         g_return_if_fail (object != NULL);
201         g_return_if_fail (GDK_IS_PIXBUF_LOADER (object));
202
203         loader = GDK_PIXBUF_LOADER (object);
204         priv = loader->private;
205
206         if (!priv->closed)
207                 gdk_pixbuf_loader_close (loader);
208
209         if (priv->animation)
210                 gdk_pixbuf_animation_unref (priv->animation);
211         if (priv->pixbuf)
212                 gdk_pixbuf_unref (priv->pixbuf);
213
214         if (GTK_OBJECT_CLASS (parent_class)->destroy)
215                 (* GTK_OBJECT_CLASS (parent_class)->destroy) (object);
216 }
217
218 static void
219 gdk_pixbuf_loader_finalize (GtkObject *object)
220 {
221         GdkPixbufLoader *loader;
222         GdkPixbufLoaderPrivate *priv = NULL;
223
224         loader = GDK_PIXBUF_LOADER (object);
225         priv = loader->private;
226
227         g_free (priv);
228
229         if (GTK_OBJECT_CLASS (parent_class)->finalize)
230                 (* GTK_OBJECT_CLASS (parent_class)->finalize) (object);
231 }
232
233 static void
234 gdk_pixbuf_loader_prepare (GdkPixbuf *pixbuf, gpointer loader)
235 {
236         GdkPixbufLoaderPrivate *priv = NULL;
237
238         priv = GDK_PIXBUF_LOADER (loader)->private;
239         gdk_pixbuf_ref (pixbuf);
240         g_assert (priv->pixbuf == NULL);
241
242         priv->pixbuf = pixbuf;
243         gtk_signal_emit (GTK_OBJECT (loader), pixbuf_loader_signals[AREA_PREPARED]);
244 }
245
246 static void
247 gdk_pixbuf_loader_update (GdkPixbuf *pixbuf, guint x, guint y, guint width, guint height, gpointer loader)
248 {
249         GdkPixbufLoaderPrivate *priv = NULL;
250
251         priv = GDK_PIXBUF_LOADER (loader)->private;
252
253         gtk_signal_emit (GTK_OBJECT (loader),
254                          pixbuf_loader_signals[AREA_UPDATED],
255                          x, y,
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)));
259 }
260
261 static void
262 gdk_pixbuf_loader_frame_done (GdkPixbufFrame *frame, gpointer loader)
263 {
264         GdkPixbufLoaderPrivate *priv = NULL;
265
266         priv = GDK_PIXBUF_LOADER (loader)->private;
267
268         priv->pixbuf = NULL;
269
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);
276         } else {
277                 int w, h;
278
279                 /* update bbox size */
280                 w = gdk_pixbuf_get_width (frame->pixbuf);
281                 h = gdk_pixbuf_get_height (frame->pixbuf);
282
283                 if (w > priv->animation->width) {
284                         priv->animation->width = h;
285                 }
286                 if (h > priv->animation->height) {
287                         priv->animation->height = h;
288                 }
289         }
290
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],
295                          frame);
296 }
297
298 static void
299 gdk_pixbuf_loader_animation_done (GdkPixbuf *pixbuf, gpointer loader)
300 {
301         gtk_signal_emit (GTK_OBJECT (loader),
302                          pixbuf_loader_signals[ANIMATION_DONE]);
303 }
304
305 \f
306
307 /**
308  * gdk_pixbuf_loader_new:
309  *
310  * Creates a new pixbuf loader object.
311  *
312  * Return value: A newly-created pixbuf loader.
313  **/
314 GdkPixbufLoader *
315 gdk_pixbuf_loader_new (void)
316 {
317         return gtk_type_new (gdk_pixbuf_loader_get_type ());
318 }
319
320 static int
321 gdk_pixbuf_loader_load_module(GdkPixbufLoader *loader)
322 {
323         GdkPixbufLoaderPrivate *priv = loader->private;
324
325         priv->image_module = gdk_pixbuf_get_module (priv->header_buf, priv->header_buf_offset);
326
327         if (priv->image_module == NULL)
328                 return 0;
329
330         if (priv->image_module->module == NULL)
331                 gdk_pixbuf_load_module (priv->image_module);
332
333         if (priv->image_module->module == NULL)
334                 return 0;
335
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);
341                 return 0;
342         }
343
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,
348                                                            loader);
349
350         if (priv->context == NULL) {
351                 g_warning("Failed to begin progressive load");
352                 return 0;
353         }
354
355         if( (* priv->image_module->load_increment) (priv->context, priv->header_buf, priv->header_buf_offset) )
356                 return priv->header_buf_offset;
357
358         return 0;
359 }
360
361 static int
362 gdk_pixbuf_loader_eat_header_write (GdkPixbufLoader *loader, const guchar *buf, size_t count)
363 {
364         int nbytes;
365         GdkPixbufLoaderPrivate *priv = loader->private;
366
367         nbytes = MIN(LOADER_HEADER_SIZE - priv->header_buf_offset, count);
368         memcpy (priv->header_buf + priv->header_buf_offset, buf, nbytes);
369
370         priv->header_buf_offset += nbytes;
371
372         if(priv->header_buf_offset >= LOADER_HEADER_SIZE) {
373                 if (gdk_pixbuf_loader_load_module(loader) == 0)
374                         return 0;
375         }
376         return nbytes;
377 }
378
379 /**
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.
384  *
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.
389  *
390  * Return value: #TRUE if the write was successful, or #FALSE if the loader
391  * cannot parse the buffer.
392  **/
393 gboolean
394 gdk_pixbuf_loader_write (GdkPixbufLoader *loader, const guchar *buf, size_t count)
395 {
396         GdkPixbufLoaderPrivate *priv;
397
398         g_return_val_if_fail (loader != NULL, FALSE);
399         g_return_val_if_fail (GDK_IS_PIXBUF_LOADER (loader), FALSE);
400
401         g_return_val_if_fail (buf != NULL, FALSE);
402         g_return_val_if_fail (count >= 0, FALSE);
403
404         priv = loader->private;
405
406         /* we expect it's not to be closed */
407         g_return_val_if_fail (priv->closed == FALSE, FALSE);
408
409         if (priv->image_module == NULL) {
410                 int eaten;
411
412                 eaten = gdk_pixbuf_loader_eat_header_write(loader, buf, count);
413                 if (eaten <= 0)
414                         return FALSE;
415
416                 count -= eaten;
417                 buf += eaten;
418         }
419
420         if (count > 0 && priv->image_module->load_increment)
421                 return (* priv->image_module->load_increment) (priv->context, buf, count);
422
423         return TRUE;
424 }
425
426 /**
427  * gdk_pixbuf_loader_get_pixbuf:
428  * @loader: A pixbuf loader.
429  *
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.
437  *
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.
440  **/
441 GdkPixbuf *
442 gdk_pixbuf_loader_get_pixbuf (GdkPixbufLoader *loader)
443 {
444         GdkPixbufLoaderPrivate *priv;
445
446         g_return_val_if_fail (loader != NULL, NULL);
447         g_return_val_if_fail (GDK_IS_PIXBUF_LOADER (loader), NULL);
448
449         priv = loader->private;
450
451         return priv->pixbuf;
452 }
453
454 /**
455  * gdk_pixbuf_loader_get_animation:
456  * @loader: A pixbuf loader
457  *
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.
462  *
463  * Return value: The GdkPixbufAnimation that the loader is loading, or NULL if
464  not enough data has been read to determine the information.
465  **/
466 GdkPixbufAnimation *
467 gdk_pixbuf_loader_get_animation (GdkPixbufLoader *loader)
468 {
469         GdkPixbufLoaderPrivate *priv;
470
471         g_return_val_if_fail (loader != NULL, NULL);
472         g_return_val_if_fail (GDK_IS_PIXBUF_LOADER (loader), NULL);
473
474         priv = loader->private;
475
476         return priv->animation;
477 }
478
479 /**
480  * gdk_pixbuf_loader_close:
481  * @loader: A pixbuf loader.
482  *
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.
485  **/
486 void
487 gdk_pixbuf_loader_close (GdkPixbufLoader *loader)
488 {
489         GdkPixbufLoaderPrivate *priv;
490
491         g_return_if_fail (loader != NULL);
492         g_return_if_fail (GDK_IS_PIXBUF_LOADER (loader));
493
494         priv = loader->private;
495
496         /* we expect it's not closed */
497         g_return_if_fail (priv->closed == FALSE);
498
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);
502
503         if (priv->image_module && priv->image_module->stop_load)
504                 (* priv->image_module->stop_load) (priv->context);
505
506         priv->closed = TRUE;
507
508         gtk_signal_emit (GTK_OBJECT (loader),
509                          pixbuf_loader_signals[CLOSED]);
510 }
511