1 /* -*- mode: C; c-file-style: "linux" -*- */
2 /* GdkPixbuf library - ANI image loader
4 * Copyright (C) 2002 The Free Software Foundation
6 * Authors: Matthias Clasen <maclas@gmx.de>
8 * This library is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Lesser General Public
10 * License as published by the Free Software Foundation; either
11 * version 2 of the License, or (at your option) any later version.
13 * This library is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * Lesser General Public License for more details.
18 * You should have received a copy of the GNU Lesser General Public
19 * License along with this library; if not, write to the
20 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
21 * Boston, MA 02111-1307, USA.
29 #include "gdk-pixbuf-private.h"
30 #include "gdk-pixbuf-io.h"
31 #include "io-ani-animation.h"
36 return src[0] | (src[1] << 8) | (src[2] << 16) | (src[3] << 24);
39 #define MAKE_TAG(a,b,c,d) ( (guint32)d << 24 | \
44 #define TAG_RIFF MAKE_TAG('R','I','F','F')
45 #define TAG_ACON MAKE_TAG('A','C','O','N')
46 #define TAG_LIST MAKE_TAG('L','I','S','T')
47 #define TAG_INAM MAKE_TAG('I','N','A','M')
48 #define TAG_IART MAKE_TAG('I','A','R','T')
49 #define TAG_anih MAKE_TAG('a','n','i','h')
50 #define TAG_seq MAKE_TAG('s','e','q',' ')
51 #define TAG_rate MAKE_TAG('r','a','t','e')
52 #define TAG_icon MAKE_TAG('i','c','o','n')
54 typedef struct _AniLoaderContext
63 ModulePreparedNotifyFunc prepared_func;
64 ModuleUpdatedNotifyFunc updated_func;
85 GdkPixbufAniAnim *animation;
86 GdkPixbufLoader *loader;
92 #define BYTES_LEFT(context) \
93 ((context)->n_bytes - ((context)->byte - (context)->buffer))
96 read_int8 (AniLoaderContext *context,
100 int total = MIN (count, BYTES_LEFT (context));
101 memcpy (data, context->byte, total);
102 context->byte += total;
103 context->cp += total;
108 read_int32 (AniLoaderContext *context)
112 read_int8 (context, (guchar*) &result, 4);
113 return lsb_32 ((guchar *) &result);
117 context_free (AniLoaderContext *context)
124 gdk_pixbuf_loader_close (context->loader, NULL);
125 g_object_unref (context->loader);
127 if (context->animation)
128 g_object_unref (context->animation);
129 g_free (context->buffer);
130 g_free (context->title);
131 g_free (context->author);
137 prepared_callback (GdkPixbufLoader *loader,
140 AniLoaderContext *context = (AniLoaderContext*)data;
143 g_print ("%d pixbuf prepared\n", context->pos);
146 GdkPixbuf *pixbuf = gdk_pixbuf_loader_get_pixbuf (loader);
150 if (gdk_pixbuf_get_width (pixbuf) > context->animation->width)
151 context->animation->width = gdk_pixbuf_get_width (pixbuf);
153 if (gdk_pixbuf_get_height (pixbuf) > context->animation->height)
154 context->animation->height = gdk_pixbuf_get_height (pixbuf);
156 if (context->title != NULL)
157 gdk_pixbuf_set_option (pixbuf, "Title", context->title);
159 if (context->author != NULL)
160 gdk_pixbuf_set_option (pixbuf, "Author", context->author);
162 g_object_ref (pixbuf);
163 context->animation->pixbufs[context->pos] = pixbuf;
165 if (context->pos == 0)
167 if (context->prepared_func)
168 (* context->prepared_func) (pixbuf,
169 GDK_PIXBUF_ANIMATION (context->animation),
173 /* FIXME - this is necessary for nice display of loading
174 animations because GtkImage ignores
175 gdk_pixbuf_animation_iter_on_currently_loading_frame()
176 and always exposes the full frame */
177 GdkPixbuf *last = context->animation->pixbufs[context->pos - 1];
178 gint width = MIN (gdk_pixbuf_get_width (last), gdk_pixbuf_get_width (pixbuf));
179 gint height = MIN (gdk_pixbuf_get_height (last), gdk_pixbuf_get_height (pixbuf));
180 gdk_pixbuf_copy_area (last, 0, 0, width, height, pixbuf, 0, 0);
187 updated_callback (GdkPixbufLoader* loader,
188 gint x, gint y, gint width, gint height,
191 AniLoaderContext *context = (AniLoaderContext*)data;
193 GdkPixbuf *pixbuf = gdk_pixbuf_loader_get_pixbuf (loader);
195 if (context->updated_func)
196 (* context->updated_func) (pixbuf,
202 ani_load_chunk (AniLoaderContext *context, GError **error)
206 if (context->chunk_id == 0x0) {
207 if (BYTES_LEFT (context) < 8)
209 context->chunk_id = read_int32 (context);
210 context->chunk_size = read_int32 (context);
211 /* Pad it up to word length */
212 if (context->chunk_size % 2)
213 context->chunk_size += (2 - (context->chunk_size % 2));
217 while (context->chunk_id == TAG_LIST)
219 if (BYTES_LEFT (context) < 12)
222 read_int32 (context);
223 context->chunk_id = read_int32 (context);
224 context->chunk_size = read_int32 (context);
225 /* Pad it up to word length */
226 if (context->chunk_size % 2)
227 context->chunk_size += (2 - (context->chunk_size % 2));
231 if (context->chunk_id == TAG_icon)
233 GError *loader_error = NULL;
237 if (context->loader == NULL)
239 if (context->pos >= context->NumFrames)
243 GDK_PIXBUF_ERROR_CORRUPT_IMAGE,
244 _("Unexpected icon chunk in animation"));
249 g_print ("opening loader\n");
251 context->loader = gdk_pixbuf_loader_new_with_type ("ico", &loader_error);
254 g_propagate_error (error, loader_error);
257 g_signal_connect (context->loader, "area_prepared",
258 G_CALLBACK (prepared_callback), context);
259 g_signal_connect (context->loader, "area_updated",
260 G_CALLBACK (updated_callback), context);
263 towrite = MIN (context->chunk_size, BYTES_LEFT (context));
264 data = context->byte;
265 context->byte += towrite;
266 context->cp += towrite;
268 g_print ("miss %d, get %d, leftover %d\n", context->chunk_size, towrite, BYTES_LEFT (context));
270 context->chunk_size -= towrite;
271 if (!gdk_pixbuf_loader_write (context->loader, data, towrite, &loader_error))
273 g_propagate_error (error, loader_error);
274 gdk_pixbuf_loader_close (context->loader, NULL);
275 g_object_unref (context->loader);
276 context->loader = NULL;
279 if (context->chunk_size == 0)
282 g_print ("closing loader\n");
284 if (!gdk_pixbuf_loader_close (context->loader, &loader_error))
286 g_propagate_error (error, loader_error);
287 g_object_unref (context->loader);
288 context->loader = NULL;
291 g_object_unref (context->loader);
292 context->loader = NULL;
293 context->chunk_id = 0x0;
295 return BYTES_LEFT (context) > 0;
298 if (BYTES_LEFT (context) < context->chunk_size)
301 if (context->chunk_id == TAG_anih)
303 context->HeaderSize = read_int32 (context);
304 context->NumFrames = read_int32 (context);
305 context->NumSteps = read_int32 (context);
306 context->Width = read_int32 (context);
307 context->Height = read_int32 (context);
308 context->BitCount = read_int32 (context);
309 context->NumPlanes = read_int32 (context);
310 context->DisplayRate = read_int32 (context);
311 context->Flags = read_int32 (context);
314 g_print ("HeaderSize \t%" G_GUINT32_FORMAT
315 "\nNumFrames \t%" G_GUINT32_FORMAT
316 "\nNumSteps \t%" G_GUINT32_FORMAT
317 "\nWidth \t%" G_GUINT32_FORMAT
318 "\nHeight \t%" G_GUINT32_FORMAT
319 "\nBitCount \t%" G_GUINT32_FORMAT
320 "\nNumPlanes \t%" G_GUINT32_FORMAT
321 "\nDisplayRate \t%" G_GUINT32_FORMAT
322 "\nSequenceFlag \t%d"
325 context->HeaderSize, context->NumFrames,
326 context->NumSteps, context->Width,
327 context->Height, context->BitCount,
328 context->NumPlanes, context->DisplayRate,
329 (context->Flags & 0x2) != 0,
330 (context->Flags & 0x1) != 0);
332 if (!context->Flags & 0x2)
336 GDK_PIXBUF_ERROR_CORRUPT_IMAGE,
337 _("Unsupported animation type"));
340 if (context->NumFrames == 0 ||
341 context->NumFrames >= 1024 ||
342 context->NumSteps == 0 ||
343 context->NumSteps >= 1024)
347 GDK_PIXBUF_ERROR_CORRUPT_IMAGE,
348 _("Invalid header in animation"));
352 context->animation = g_object_new (GDK_TYPE_PIXBUF_ANI_ANIM, NULL);
353 if (!context->animation)
357 GDK_PIXBUF_ERROR_INSUFFICIENT_MEMORY,
358 _("Not enough memory to load animation"));
362 context->animation->n_pixbufs = context->NumFrames;
363 context->animation->n_frames = context->NumSteps;
365 context->animation->total_time = context->NumSteps * (context->DisplayRate * 1000 / 60);
366 context->animation->width = 0;
367 context->animation->height = 0;
369 context->animation->pixbufs = (GdkPixbuf**) g_try_malloc (sizeof (GdkPixbuf*) * context->NumFrames);
370 if (context->animation->pixbufs)
371 memset (context->animation->pixbufs, 0, sizeof (GdkPixbuf*) * context->NumFrames);
373 context->animation->delay = (guint32*) g_try_malloc (sizeof (guint32) * context->NumSteps);
374 context->animation->sequence = (guint32*) g_try_malloc (sizeof (guint32) * context->NumSteps);
375 if (!context->animation->pixbufs ||
376 !context->animation->delay ||
377 !context->animation->sequence)
381 GDK_PIXBUF_ERROR_INSUFFICIENT_MEMORY,
382 _("Not enough memory to load animation"));
386 for (i = 0; i < context->NumSteps; i++)
388 /* default values if the corresponding chunks are absent */
389 context->animation->delay[i] = context->DisplayRate * 1000 / 60;
390 context->animation->sequence[i] = MIN (i, context->NumFrames - 1);
393 else if (context->chunk_id == TAG_rate)
395 if (context->chunk_size != 4 * context->NumSteps)
399 GDK_PIXBUF_ERROR_CORRUPT_IMAGE,
400 _("Malformed chunk in animation"));
403 context->animation->total_time = 0;
404 for (i = 0; i < context->NumSteps; i++)
406 context->animation->delay[i] = read_int32 (context) * 1000 / 60;
407 context->animation->total_time += context->animation->delay[i];
410 else if (context->chunk_id == TAG_seq)
412 if (context->chunk_size != 4 * context->NumSteps)
416 GDK_PIXBUF_ERROR_CORRUPT_IMAGE,
417 _("Malformed chunk in animation"));
420 for (i = 0; i < context->NumSteps; i++)
422 context->animation->sequence[i] = read_int32 (context);
423 if (context->animation->sequence[i] >= context->NumFrames)
427 GDK_PIXBUF_ERROR_CORRUPT_IMAGE,
428 _("Malformed chunk in animation"));
433 else if (context->chunk_id == TAG_INAM)
435 context->title = g_try_malloc (context->chunk_size + 1);
440 GDK_PIXBUF_ERROR_INSUFFICIENT_MEMORY,
441 _("Not enough memory to load animation"));
444 context->title[context->chunk_size] = 0;
445 read_int8 (context, context->title, context->chunk_size);
447 g_print ("INAM %s\n", context->title);
449 for (i = 0; i < context->pos; i++)
450 gdk_pixbuf_set_option (context->animation->pixbufs[i], "Title", context->title);
452 else if (context->chunk_id == TAG_IART)
454 context->author = g_try_malloc (context->chunk_size + 1);
455 if (!context->author)
459 GDK_PIXBUF_ERROR_INSUFFICIENT_MEMORY,
460 _("Not enough memory to load animation"));
463 context->author[context->chunk_size] = 0;
464 read_int8 (context, context->author, context->chunk_size);
466 g_print ("IART %s\n", context->author);
468 for (i = 0; i < context->pos; i++)
469 gdk_pixbuf_set_option (context->animation->pixbufs[i], "Author", context->author);
474 gint32 dummy = lsb_32 ((guchar *)&context->chunk_id);
476 g_print ("Loaded chunk with ID '%c%c%c%c' and length %" G_GUINT32_FORMAT "\n",
477 ((char*)&dummy)[0], ((char*)&dummy)[1],
478 ((char*)&dummy)[2], ((char*)&dummy)[3],
479 context->chunk_size);
483 context->chunk_id = 0x0;
488 gdk_pixbuf__ani_image_load_increment (gpointer data,
489 const guchar *buf, guint size,
492 AniLoaderContext *context = (AniLoaderContext *)data;
494 if (context->n_bytes + size >= context->buffer_size) {
495 int drop = context->byte - context->buffer;
496 memmove (context->buffer, context->byte, context->n_bytes - drop);
497 context->n_bytes -= drop;
498 context->byte = context->buffer;
499 if (context->n_bytes + size >= context->buffer_size) {
501 context->buffer_size = MAX (context->n_bytes + size, context->buffer_size + 4096);
503 g_print ("growing buffer to %" G_GUINT32_FORMAT "\n", context->buffer_size);
505 tmp = g_try_realloc (context->buffer, context->buffer_size);
510 GDK_PIXBUF_ERROR_INSUFFICIENT_MEMORY,
511 _("Not enough memory to load animation"));
514 context->byte = context->buffer = tmp;
517 memcpy (context->buffer + context->n_bytes, buf, size);
518 context->n_bytes += size;
520 if (context->data_size == 0)
522 guint32 riff_id, chunk_id;
524 if (BYTES_LEFT (context) < 12)
527 riff_id = read_int32 (context);
528 context->data_size = read_int32 (context);
529 chunk_id = read_int32 (context);
531 if (riff_id != TAG_RIFF ||
532 context->data_size == 0 ||
533 chunk_id != TAG_ACON)
537 GDK_PIXBUF_ERROR_CORRUPT_IMAGE,
538 _("Invalid header in animation"));
543 if (context->cp < context->data_size + 8)
545 GError *chunk_error = NULL;
547 while (ani_load_chunk (context, &chunk_error)) ;
550 g_propagate_error (error, chunk_error);
559 gdk_pixbuf__ani_image_begin_load (ModuleSizeFunc size_func,
560 ModulePreparedNotifyFunc prepared_func,
561 ModuleUpdatedNotifyFunc updated_func,
565 AniLoaderContext *context;
567 context = g_new0 (AniLoaderContext, 1);
569 context->prepared_func = prepared_func;
570 context->updated_func = updated_func;
571 context->user_data = user_data;
575 context->buffer_size = 4096;
576 context->buffer = g_try_malloc (context->buffer_size);
577 if (!context->buffer)
579 context_free (context);
582 GDK_PIXBUF_ERROR_INSUFFICIENT_MEMORY,
583 _("Not enough memory to load animation"));
587 context->byte = context->buffer;
588 context->n_bytes = 0;
590 return (gpointer) context;
594 gdk_pixbuf__ani_image_stop_load (gpointer data,
597 AniLoaderContext *context = (AniLoaderContext *) data;
599 g_return_val_if_fail (context != NULL, TRUE);
600 context_free (context);
606 prepared_notify (GdkPixbuf *pixbuf,
607 GdkPixbufAnimation *anim,
612 *((GdkPixbufAnimation **)user_data) = anim;
615 static GdkPixbufAnimation *
616 gdk_pixbuf__ani_image_load_animation (FILE *f, GError **error)
620 GdkPixbufAnimation *anim = NULL;
623 context = gdk_pixbuf__ani_image_begin_load (NULL, prepared_notify,
630 length = fread (buffer, 1, sizeof (buffer), f);
632 if (!gdk_pixbuf__ani_image_load_increment (context, buffer, length, error)) {
633 gdk_pixbuf__ani_image_stop_load (context, NULL);
635 g_object_unref (anim);
640 if (!gdk_pixbuf__ani_image_stop_load (context, error)) {
642 g_object_unref (anim);
650 gdk_pixbuf__ani_fill_vtable (GdkPixbufModule *module)
652 module->load_animation = gdk_pixbuf__ani_image_load_animation;
653 module->begin_load = gdk_pixbuf__ani_image_begin_load;
654 module->stop_load = gdk_pixbuf__ani_image_stop_load;
655 module->load_increment = gdk_pixbuf__ani_image_load_increment;