]> Pileus Git - ~andy/gtk/blob - gtk/gdk-pixbuf-loader.c
63b9f1c598cecff72845f1bace351f3829fb7994
[~andy/gtk] / gtk / 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) + frame->x_offset;
275                 priv->animation->height = gdk_pixbuf_get_height (frame->pixbuf) + frame->y_offset;
276         } else {
277                 int w, h;
278
279                 /* update bbox size */
280                 w = gdk_pixbuf_get_width (frame->pixbuf) + frame->x_offset;
281                 h = gdk_pixbuf_get_height (frame->pixbuf) + frame->y_offset;
282
283                 if (w > priv->animation->width) {
284                         priv->animation->width = w;
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         GdkPixbufLoaderPrivate *priv = NULL;
302         GdkPixbufFrame    *frame;
303         GList *current = NULL;
304         gint h, w;
305
306         priv = GDK_PIXBUF_LOADER (loader)->private;
307         priv->pixbuf = NULL;
308
309         current = gdk_pixbuf_animation_get_frames (priv->animation);
310
311         while (current) {
312                 frame = (GdkPixbufFrame *) current->data;
313
314                 /* update bbox size */
315                 w = gdk_pixbuf_get_width (frame->pixbuf) + frame->x_offset;
316                 h = gdk_pixbuf_get_height (frame->pixbuf) + frame->y_offset;
317
318                 if (w > priv->animation->width) {
319                         priv->animation->width = w;
320                 }
321                 if (h > priv->animation->height) {
322                         priv->animation->height = h;
323                 }
324                 current = current->next;
325         }
326
327         gtk_signal_emit (GTK_OBJECT (loader),
328                          pixbuf_loader_signals[ANIMATION_DONE]);
329 }
330
331 \f
332
333 /**
334  * gdk_pixbuf_loader_new:
335  *
336  * Creates a new pixbuf loader object.
337  *
338  * Return value: A newly-created pixbuf loader.
339  **/
340 GdkPixbufLoader *
341 gdk_pixbuf_loader_new (void)
342 {
343         return gtk_type_new (gdk_pixbuf_loader_get_type ());
344 }
345
346 static int
347 gdk_pixbuf_loader_load_module(GdkPixbufLoader *loader)
348 {
349         GdkPixbufLoaderPrivate *priv = loader->private;
350
351         priv->image_module = gdk_pixbuf_get_module (priv->header_buf, priv->header_buf_offset);
352
353         if (priv->image_module == NULL)
354                 return 0;
355
356         if (priv->image_module->module == NULL)
357                 gdk_pixbuf_load_module (priv->image_module);
358
359         if (priv->image_module->module == NULL)
360                 return 0;
361
362         if ((priv->image_module->begin_load == NULL) ||
363             (priv->image_module->stop_load == NULL) ||
364             (priv->image_module->load_increment == NULL)) {
365                 g_warning ("module %s does not support incremental loading.\n",
366                            priv->image_module->module_name);
367                 return 0;
368         }
369
370         priv->context = (*priv->image_module->begin_load) (gdk_pixbuf_loader_prepare,
371                                                            gdk_pixbuf_loader_update,
372                                                            gdk_pixbuf_loader_frame_done,
373                                                            gdk_pixbuf_loader_animation_done,
374                                                            loader);
375
376         if (priv->context == NULL) {
377                 g_warning("Failed to begin progressive load");
378                 return 0;
379         }
380
381         if( (* priv->image_module->load_increment) (priv->context, priv->header_buf, priv->header_buf_offset) )
382                 return priv->header_buf_offset;
383
384         return 0;
385 }
386
387 static int
388 gdk_pixbuf_loader_eat_header_write (GdkPixbufLoader *loader, const guchar *buf, size_t count)
389 {
390         int nbytes;
391         GdkPixbufLoaderPrivate *priv = loader->private;
392
393         nbytes = MIN(LOADER_HEADER_SIZE - priv->header_buf_offset, count);
394         memcpy (priv->header_buf + priv->header_buf_offset, buf, nbytes);
395
396         priv->header_buf_offset += nbytes;
397
398         if(priv->header_buf_offset >= LOADER_HEADER_SIZE) {
399                 if (gdk_pixbuf_loader_load_module(loader) == 0)
400                         return 0;
401         }
402         return nbytes;
403 }
404
405 /**
406  * gdk_pixbuf_loader_write:
407  * @loader: A pixbuf loader.
408  * @buf: Pointer to image data.
409  * @count: Length of the @buf buffer in bytes.
410  *
411  * This will cause a pixbuf loader to parse the next @count bytes of an image.
412  * It will return TRUE if the data was loaded successfully, and FALSE if an
413  * error occurred.  In the latter case, the loader will be closed, and will not
414  * accept further writes.
415  *
416  * Return value: #TRUE if the write was successful, or #FALSE if the loader
417  * cannot parse the buffer.
418  **/
419 gboolean
420 gdk_pixbuf_loader_write (GdkPixbufLoader *loader, const guchar *buf, size_t count)
421 {
422         GdkPixbufLoaderPrivate *priv;
423
424         g_return_val_if_fail (loader != NULL, FALSE);
425         g_return_val_if_fail (GDK_IS_PIXBUF_LOADER (loader), FALSE);
426
427         g_return_val_if_fail (buf != NULL, FALSE);
428         g_return_val_if_fail (count >= 0, FALSE);
429
430         priv = loader->private;
431
432         /* we expect it's not to be closed */
433         g_return_val_if_fail (priv->closed == FALSE, FALSE);
434
435         if (priv->image_module == NULL) {
436                 int eaten;
437
438                 eaten = gdk_pixbuf_loader_eat_header_write(loader, buf, count);
439                 if (eaten <= 0)
440                         return FALSE;
441
442                 count -= eaten;
443                 buf += eaten;
444         }
445
446         if (count > 0 && priv->image_module->load_increment)
447                 return (* priv->image_module->load_increment) (priv->context, buf, count);
448
449         return TRUE;
450 }
451
452 /**
453  * gdk_pixbuf_loader_get_pixbuf:
454  * @loader: A pixbuf loader.
455  *
456  * Queries the GdkPixbuf that a pixbuf loader is currently creating.  In general
457  * it only makes sense to call this function afer the "area_prepared" signal has
458  * been emitted by the loader; this means that enough data has been read to know
459  * the size of the image that will be allocated.  If the loader has not received
460  * enough data via gdk_pixbuf_loader_write(), then this function returns NULL.
461  * The returned pixbuf will be the same in all future calls to the loader, so
462  * simply calling gdk_pixbuf_ref() should be sufficient to continue using it.
463  *
464  * Return value: The GdkPixbuf that the loader is creating, or NULL if not
465  * enough data has been read to determine how to create the image buffer.
466  **/
467 GdkPixbuf *
468 gdk_pixbuf_loader_get_pixbuf (GdkPixbufLoader *loader)
469 {
470         GdkPixbufLoaderPrivate *priv;
471
472         g_return_val_if_fail (loader != NULL, NULL);
473         g_return_val_if_fail (GDK_IS_PIXBUF_LOADER (loader), NULL);
474
475         priv = loader->private;
476
477         return priv->pixbuf;
478 }
479
480 /**
481  * gdk_pixbuf_loader_get_animation:
482  * @loader: A pixbuf loader
483  *
484  * Queries the GdkPixbufAnimation that a pixbuf loader is currently creating.
485  * In general it only makes sense to call this function afer the "area_prepared"
486  * signal has been emitted by the loader.  If the image is not an animation,
487  * then it will return NULL.
488  *
489  * Return value: The GdkPixbufAnimation that the loader is loading, or NULL if
490  not enough data has been read to determine the information.
491  **/
492 GdkPixbufAnimation *
493 gdk_pixbuf_loader_get_animation (GdkPixbufLoader *loader)
494 {
495         GdkPixbufLoaderPrivate *priv;
496
497         g_return_val_if_fail (loader != NULL, NULL);
498         g_return_val_if_fail (GDK_IS_PIXBUF_LOADER (loader), NULL);
499
500         priv = loader->private;
501
502         return priv->animation;
503 }
504
505 /**
506  * gdk_pixbuf_loader_close:
507  * @loader: A pixbuf loader.
508  *
509  * Informs a pixbuf loader that no further writes with gdk_pixbuf_load_write()
510  * will occur, so that it can free its internal loading structures.
511  **/
512 void
513 gdk_pixbuf_loader_close (GdkPixbufLoader *loader)
514 {
515         GdkPixbufLoaderPrivate *priv;
516
517         g_return_if_fail (loader != NULL);
518         g_return_if_fail (GDK_IS_PIXBUF_LOADER (loader));
519
520         priv = loader->private;
521
522         /* we expect it's not closed */
523         g_return_if_fail (priv->closed == FALSE);
524
525         /* We have less the 128 bytes in the image.  Flush it, and keep going. */
526         if (priv->image_module == NULL)
527                 gdk_pixbuf_loader_load_module (loader);
528
529         if (priv->image_module && priv->image_module->stop_load)
530                 (* priv->image_module->stop_load) (priv->context);
531
532         priv->closed = TRUE;
533
534         gtk_signal_emit (GTK_OBJECT (loader),
535                          pixbuf_loader_signals[CLOSED]);
536 }
537