1 /* -*- Mode: C; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 8 -*- */
2 /* GdkPixbuf library - GIF image loader
4 * Copyright (C) 1999 Mark Crichton
5 * Copyright (C) 1999 The Free Software Foundation
7 * Authors: Jonathan Blandford <jrb@redhat.com>
8 * Adapted from the gimp gif filter written by Adam Moss <adam@gimp.org>
9 * Gimp work based on earlier work.
10 * Permission to relicense under the LGPL obtained.
12 * This library is free software; you can redistribute it and/or
13 * modify it under the terms of the GNU Lesser 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 * Lesser General Public License for more details.
22 * You should have received a copy of the GNU Lesser 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 /* This loader is very hairy code.
30 * The main loop was not designed for incremental loading, so when it was hacked
31 * in it got a bit messy. Basicly, every function is written to expect a failed
32 * read_gif, and lets you call it again assuming that the bytes are there.
35 * Unless otherwise specified, these are the return vals for most functions:
38 * -1 -> more bytes needed.
39 * -2 -> failure; abort the load
40 * -3 -> control needs to be passed back to the main loop
41 * \_ (most of the time returning 0 will get this, but not always)
43 * >1 -> for functions that get a guchar, the char will be returned.
49 * If you have any images that crash this code, please, please let me know and
60 #include "gdk-pixbuf-private.h"
61 #include "gdk-pixbuf-io.h"
62 #include "io-gif-animation.h"
66 #undef DUMP_IMAGE_DETAILS
69 #define MAXCOLORMAPSIZE 256
70 #define MAX_LZW_BITS 12
72 #define INTERLACE 0x40
73 #define LOCALCOLORMAP 0x80
74 #define BitSet(byte, bit) (((byte) & (bit)) == (bit))
75 #define LM_to_uint(a,b) (((b)<<8)|(a))
79 typedef unsigned char CMap[3][MAXCOLORMAPSIZE];
81 /* Possible states we can be in. */
97 typedef struct _Gif89 Gif89;
106 typedef struct _GifContext GifContext;
109 int state; /* really only relevant for progressive loading */
113 gboolean has_global_cmap;
115 CMap global_color_map;
116 gint global_colormap_size;
117 unsigned int global_bit_pixel;
118 unsigned int global_color_resolution;
119 unsigned int background_index;
120 gboolean stop_after_first_frame;
122 gboolean frame_cmap_active;
123 CMap frame_color_map;
124 gint frame_colormap_size;
125 unsigned int frame_bit_pixel;
127 unsigned int aspect_ratio;
128 GdkPixbufGifAnim *animation;
129 GdkPixbufFrame *frame;
132 /* stuff per frame. */
139 /* Static read only */
142 /* progressive read, only. */
143 GdkPixbufModulePreparedFunc prepare_func;
144 GdkPixbufModuleUpdatedFunc update_func;
151 /* extension context */
152 guchar extension_label;
153 guchar extension_flag;
154 gboolean in_loop_extension;
156 /* get block context */
158 guchar block_buf[280];
161 int old_state; /* used by lzw_fill buffer */
162 /* get_code context */
167 int lzw_code_pending;
172 guchar lzw_set_code_size;
174 gint lzw_max_code_size;
181 gint lzw_table[2][(1 << MAX_LZW_BITS)];
182 gint lzw_stack[(1 << (MAX_LZW_BITS)) * 2 + 1];
184 /* painting context */
193 static int GetDataBlock (GifContext *, unsigned char *);
198 static int count = 0;
201 /* Returns TRUE if read is OK,
202 * FALSE if more memory is needed. */
204 gif_read (GifContext *context, guchar *buffer, size_t len)
213 g_print ("Fsize :%d\tcount :%d\t", len, count);
215 retval = (fread(buffer, len, 1, context->file) != 0);
217 if (!retval && ferror (context->file)) {
218 gint save_errno = errno;
219 g_set_error (context->error,
221 g_file_error_from_errno (save_errno),
222 _("Failure reading GIF: %s"),
223 g_strerror (save_errno));
228 for (i = 0; i < len; i++)
229 g_print ("%d ", buffer[i]);
237 /* g_print ("\tlooking for %d bytes. size == %d, ptr == %d\n", len, context->size, context->ptr); */
239 if ((context->size - context->ptr) >= len) {
243 memcpy (buffer, context->buf + context->ptr, len);
245 context->amount_needed = 0;
247 g_print ("Psize :%d\tcount :%d\t", len, count);
249 for (i = 0; i < len; i++)
250 g_print ("%d ", buffer[i]);
256 context->amount_needed = len - (context->size - context->ptr);
261 /* Changes the stage to be GIF_GET_COLORMAP */
263 gif_set_get_colormap (GifContext *context)
265 context->global_colormap_size = 0;
266 context->state = GIF_GET_COLORMAP;
270 gif_set_get_colormap2 (GifContext *context)
272 context->frame_colormap_size = 0;
273 context->state = GIF_GET_COLORMAP2;
277 gif_get_colormap (GifContext *context)
279 unsigned char rgb[3];
281 while (context->global_colormap_size < context->global_bit_pixel) {
282 if (!gif_read (context, rgb, sizeof (rgb))) {
286 context->global_color_map[0][context->global_colormap_size] = rgb[0];
287 context->global_color_map[1][context->global_colormap_size] = rgb[1];
288 context->global_color_map[2][context->global_colormap_size] = rgb[2];
290 if (context->global_colormap_size == context->background_index) {
291 context->animation->bg_red = rgb[0];
292 context->animation->bg_green = rgb[1];
293 context->animation->bg_blue = rgb[2];
296 context->global_colormap_size ++;
304 gif_get_colormap2 (GifContext *context)
306 unsigned char rgb[3];
308 while (context->frame_colormap_size < context->frame_bit_pixel) {
309 if (!gif_read (context, rgb, sizeof (rgb))) {
313 context->frame_color_map[0][context->frame_colormap_size] = rgb[0];
314 context->frame_color_map[1][context->frame_colormap_size] = rgb[1];
315 context->frame_color_map[2][context->frame_colormap_size] = rgb[2];
317 context->frame_colormap_size ++;
324 * in order for this function to work, we need to perform some black magic.
325 * We want to return -1 to let the calling function know, as before, that it needs
326 * more bytes. If we return 0, we were able to successfully read all block->count bytes.
327 * Problem is, we don't want to reread block_count every time, so we check to see if
328 * context->block_count is 0 before we read in the function.
330 * As a result, context->block_count MUST be 0 the first time the get_data_block is called
331 * within a context, and cannot be 0 the second time it's called.
335 get_data_block (GifContext *context,
340 if (context->block_count == 0) {
341 if (!gif_read (context, &context->block_count, 1)) {
346 if (context->block_count == 0)
352 if (!gif_read (context, buf, context->block_count)) {
360 gif_set_get_extension (GifContext *context)
362 context->state = GIF_GET_EXTENSION;
363 context->extension_flag = TRUE;
364 context->extension_label = 0;
365 context->block_count = 0;
366 context->block_ptr = 0;
370 gif_get_extension (GifContext *context)
373 gint empty_block = FALSE;
375 if (context->extension_flag) {
376 if (context->extension_label == 0) {
377 /* I guess bad things can happen if we have an extension of 0 )-: */
378 /* I should look into this sometime */
379 if (!gif_read (context, & context->extension_label , 1)) {
384 switch (context->extension_label) {
385 case 0xf9: /* Graphic Control Extension */
386 retval = get_data_block (context, (unsigned char *) context->block_buf, NULL);
390 if (context->frame == NULL) {
391 /* I only want to set the transparency if I haven't
392 * created the frame yet.
394 context->gif89.disposal = (context->block_buf[0] >> 2) & 0x7;
395 context->gif89.input_flag = (context->block_buf[0] >> 1) & 0x1;
396 context->gif89.delay_time = LM_to_uint (context->block_buf[1], context->block_buf[2]);
398 if ((context->block_buf[0] & 0x1) != 0) {
399 context->gif89.transparent = context->block_buf[3];
401 context->gif89.transparent = -1;
405 /* Now we've successfully loaded this one, we continue on our way */
406 context->block_count = 0;
407 context->extension_flag = FALSE;
409 case 0xff: /* application extension */
410 if (!context->in_loop_extension) {
411 retval = get_data_block (context, (unsigned char *) context->block_buf, NULL);
414 if (!strncmp ((gchar *)context->block_buf, "NETSCAPE2.0", 11) ||
415 !strncmp ((gchar *)context->block_buf, "ANIMEXTS1.0", 11)) {
416 context->in_loop_extension = TRUE;
418 context->block_count = 0;
420 if (context->in_loop_extension) {
422 retval = get_data_block (context, (unsigned char *) context->block_buf, &empty_block);
425 if (context->block_buf[0] == 0x01) {
426 context->animation->loop = context->block_buf[1] + (context->block_buf[2] << 8);
427 if (context->animation->loop != 0)
428 context->animation->loop++;
430 context->block_count = 0;
432 while (!empty_block);
433 context->in_loop_extension = FALSE;
434 context->extension_flag = FALSE;
439 /* Unhandled extension */
443 /* read all blocks, until I get an empty block, in case there was an extension I didn't know about. */
445 retval = get_data_block (context, (unsigned char *) context->block_buf, &empty_block);
448 context->block_count = 0;
449 } while (!empty_block);
454 static int ZeroDataBlock = FALSE;
457 GetDataBlock (GifContext *context,
460 /* unsigned char count; */
462 if (!gif_read (context, &context->block_count, 1)) {
463 /*g_message (_("GIF: error in getting DataBlock size\n"));*/
467 ZeroDataBlock = context->block_count == 0;
469 if ((context->block_count != 0) && (!gif_read (context, buf, context->block_count))) {
470 /*g_message (_("GIF: error in reading DataBlock\n"));*/
474 return context->block_count;
479 gif_set_lzw_fill_buffer (GifContext *context)
481 context->block_count = 0;
482 context->old_state = context->state;
483 context->state = GIF_LZW_FILL_BUFFER;
487 gif_lzw_fill_buffer (GifContext *context)
491 if (context->code_done) {
492 if (context->code_curbit >= context->code_lastbit) {
493 g_set_error_literal (context->error,
495 GDK_PIXBUF_ERROR_CORRUPT_IMAGE,
496 _("GIF file was missing some data (perhaps it was truncated somehow?)"));
500 /* Is this supposed to be an error or what? */
501 /* g_message ("trying to read more data after we've done stuff\n"); */
502 g_set_error (context->error,
504 GDK_PIXBUF_ERROR_FAILED,
505 _("Internal error in the GIF loader (%s)"),
511 context->block_buf[0] = context->block_buf[context->code_last_byte - 2];
512 context->block_buf[1] = context->block_buf[context->code_last_byte - 1];
514 retval = get_data_block (context, &context->block_buf[2], NULL);
519 if (context->block_count == 0)
520 context->code_done = TRUE;
522 context->code_last_byte = 2 + context->block_count;
523 context->code_curbit = (context->code_curbit - context->code_lastbit) + 16;
524 context->code_lastbit = (2 + context->block_count) * 8;
526 context->state = context->old_state;
531 get_code (GifContext *context,
536 if ((context->code_curbit + code_size) >= context->code_lastbit){
537 gif_set_lzw_fill_buffer (context);
542 for (i = context->code_curbit, j = 0; j < code_size; ++i, ++j)
543 ret |= ((context->block_buf[i / 8] & (1 << (i % 8))) != 0) << j;
545 context->code_curbit += code_size;
552 set_gif_lzw_clear_code (GifContext *context)
554 context->state = GIF_LZW_CLEAR_CODE;
555 context->lzw_code_pending = -1;
559 gif_lzw_clear_code (GifContext *context)
563 code = get_code (context, context->lzw_code_size);
567 context->lzw_firstcode = context->lzw_oldcode = code;
568 context->lzw_code_pending = code;
569 context->state = GIF_GET_LZW;
573 #define CHECK_LZW_SP() G_STMT_START { \
574 if ((guchar *)context->lzw_sp >= \
575 (guchar *)context->lzw_stack + sizeof (context->lzw_stack)) { \
576 g_set_error_literal (context->error, \
578 GDK_PIXBUF_ERROR_CORRUPT_IMAGE, \
579 _("Stack overflow")); \
585 lzw_read_byte (GifContext *context)
592 if (context->lzw_code_pending != -1) {
593 retval = context->lzw_code_pending;
594 context->lzw_code_pending = -1;
598 if (context->lzw_fresh) {
599 context->lzw_fresh = FALSE;
601 retval = get_code (context, context->lzw_code_size);
606 context->lzw_firstcode = context->lzw_oldcode = retval;
607 } while (context->lzw_firstcode == context->lzw_clear_code);
608 return context->lzw_firstcode;
611 if (context->lzw_sp > context->lzw_stack) {
612 my_retval = *--(context->lzw_sp);
616 while ((code = get_code (context, context->lzw_code_size)) >= 0) {
617 if (code == context->lzw_clear_code) {
618 for (i = 0; i < context->lzw_clear_code; ++i) {
619 context->lzw_table[0][i] = 0;
620 context->lzw_table[1][i] = i;
622 for (; i < (1 << MAX_LZW_BITS); ++i)
623 context->lzw_table[0][i] = context->lzw_table[1][i] = 0;
624 context->lzw_code_size = context->lzw_set_code_size + 1;
625 context->lzw_max_code_size = 2 * context->lzw_clear_code;
626 context->lzw_max_code = context->lzw_clear_code + 2;
627 context->lzw_sp = context->lzw_stack;
629 set_gif_lzw_clear_code (context);
631 } else if (code == context->lzw_end_code) {
633 unsigned char buf[260];
635 /* FIXME - we should handle this case */
636 g_set_error_literal (context->error,
638 GDK_PIXBUF_ERROR_FAILED,
639 _("GIF image loader cannot understand this image."));
646 while ((count = GetDataBlock (context, buf)) > 0)
650 /*g_print (_("GIF: missing EOD in data stream (common occurence)"));*/
657 if (code >= context->lzw_max_code) {
659 *(context->lzw_sp)++ = context->lzw_firstcode;
660 code = context->lzw_oldcode;
663 while (code >= context->lzw_clear_code) {
664 if (code >= (1 << MAX_LZW_BITS)) {
665 g_set_error_literal (context->error,
667 GDK_PIXBUF_ERROR_CORRUPT_IMAGE,
668 _("Bad code encountered"));
672 *(context->lzw_sp)++ = context->lzw_table[1][code];
674 if (code == context->lzw_table[0][code]) {
675 g_set_error_literal (context->error,
677 GDK_PIXBUF_ERROR_CORRUPT_IMAGE,
678 _("Circular table entry in GIF file"));
681 code = context->lzw_table[0][code];
685 *(context->lzw_sp)++ = context->lzw_firstcode = context->lzw_table[1][code];
687 if ((code = context->lzw_max_code) < (1 << MAX_LZW_BITS)) {
688 context->lzw_table[0][code] = context->lzw_oldcode;
689 context->lzw_table[1][code] = context->lzw_firstcode;
690 ++context->lzw_max_code;
691 if ((context->lzw_max_code >= context->lzw_max_code_size) &&
692 (context->lzw_max_code_size < (1 << MAX_LZW_BITS))) {
693 context->lzw_max_code_size *= 2;
694 ++context->lzw_code_size;
698 context->lzw_oldcode = incode;
700 if (context->lzw_sp > context->lzw_stack) {
701 my_retval = *--(context->lzw_sp);
709 gif_set_get_lzw (GifContext *context)
711 context->state = GIF_GET_LZW;
712 context->draw_xpos = 0;
713 context->draw_ypos = 0;
714 context->draw_pass = 0;
718 gif_fill_in_pixels (GifContext *context, guchar *dest, gint offset, guchar v)
720 guchar *pixel = NULL;
721 guchar (*cmap)[MAXCOLORMAPSIZE];
723 if (context->frame_cmap_active)
724 cmap = context->frame_color_map;
726 cmap = context->global_color_map;
728 if (context->gif89.transparent != -1) {
729 pixel = dest + (context->draw_ypos + offset) * gdk_pixbuf_get_rowstride (context->frame->pixbuf) + context->draw_xpos * 4;
730 *pixel = cmap [0][(guchar) v];
731 *(pixel+1) = cmap [1][(guchar) v];
732 *(pixel+2) = cmap [2][(guchar) v];
733 *(pixel+3) = (guchar) ((v == context->gif89.transparent) ? 0 : 255);
735 pixel = dest + (context->draw_ypos + offset) * gdk_pixbuf_get_rowstride (context->frame->pixbuf) + context->draw_xpos * 3;
736 *pixel = cmap [0][(guchar) v];
737 *(pixel+1) = cmap [1][(guchar) v];
738 *(pixel+2) = cmap [2][(guchar) v];
743 /* only called if progressive and interlaced */
745 gif_fill_in_lines (GifContext *context, guchar *dest, guchar v)
747 switch (context->draw_pass) {
749 if (context->draw_ypos > 4) {
750 gif_fill_in_pixels (context, dest, -4, v);
751 gif_fill_in_pixels (context, dest, -3, v);
753 if (context->draw_ypos < (context->frame_height - 4)) {
754 gif_fill_in_pixels (context, dest, 3, v);
755 gif_fill_in_pixels (context, dest, 4, v);
757 /* we don't need a break here. We draw the outer pixels first, then the
758 * inner ones, then the innermost ones. case 0 needs to draw all 3 bands.
759 * case 1, just the last two, and case 2 just draws the last one*/
761 if (context->draw_ypos > 2)
762 gif_fill_in_pixels (context, dest, -2, v);
763 if (context->draw_ypos < (context->frame_height - 2))
764 gif_fill_in_pixels (context, dest, 2, v);
765 /* no break as above. */
767 if (context->draw_ypos > 1)
768 gif_fill_in_pixels (context, dest, -1, v);
769 if (context->draw_ypos < (context->frame_height - 1))
770 gif_fill_in_pixels (context, dest, 1, v);
777 /* Clips a rectancle to the base dimensions. Returns TRUE if the clipped rectangle is non-empty. */
779 clip_frame (GifContext *context,
791 *width = MIN (context->width, orig_x + *width) - *x;
792 *height = MIN (context->height, orig_y + *height) - *y;
794 if (*width > 0 && *height > 0)
797 /* The frame is completely off-bounds */
807 /* Call update_func on the given rectangle, unless it is completely off-bounds */
809 maybe_update (GifContext *context,
815 if (clip_frame (context, &x, &y, &width, &height))
816 (*context->update_func) (context->frame->pixbuf,
822 gif_get_lzw (GifContext *context)
825 gint lower_bound, upper_bound; /* bounds for emitting the area_updated signal */
827 gint first_pass; /* bounds for emitting the area_updated signal */
830 if (context->frame == NULL) {
831 context->frame = g_new (GdkPixbufFrame, 1);
833 context->frame->composited = NULL;
834 context->frame->revert = NULL;
836 if (context->frame_len == 0 || context->frame_height == 0) {
837 /* An empty frame, we just output a single transparent
840 context->x_offset = 0;
841 context->y_offset = 0;
842 context->frame_len = 1;
843 context->frame_height = 1;
844 context->frame->pixbuf = gdk_pixbuf_new (GDK_COLORSPACE_RGB, TRUE, 8, 1, 1);
845 if (context->frame->pixbuf) {
848 pixels = gdk_pixbuf_get_pixels (context->frame->pixbuf);
855 context->frame->pixbuf =
856 gdk_pixbuf_new (GDK_COLORSPACE_RGB,
860 context->frame_height);
861 if (!context->frame->pixbuf) {
862 g_free (context->frame);
863 g_set_error_literal (context->error,
865 GDK_PIXBUF_ERROR_INSUFFICIENT_MEMORY,
866 _("Not enough memory to load GIF file"));
870 context->frame->x_offset = context->x_offset;
871 context->frame->y_offset = context->y_offset;
872 context->frame->need_recomposite = TRUE;
874 /* GIF delay is in hundredths, we want thousandths */
875 context->frame->delay_time = context->gif89.delay_time * 10;
877 /* GIFs with delay time 0 are mostly broken, but they
878 * just want a default, "not that fast" delay.
880 if (context->frame->delay_time == 0)
881 context->frame->delay_time = 100;
883 /* No GIFs gets to play faster than 50 fps. They just
886 if (context->frame->delay_time < 20)
887 context->frame->delay_time = 20; /* 20 = "fast" */
889 context->frame->elapsed = context->animation->total_time;
890 context->animation->total_time += context->frame->delay_time;
892 switch (context->gif89.disposal) {
895 context->frame->action = GDK_PIXBUF_FRAME_RETAIN;
898 context->frame->action = GDK_PIXBUF_FRAME_DISPOSE;
901 context->frame->action = GDK_PIXBUF_FRAME_REVERT;
904 context->frame->action = GDK_PIXBUF_FRAME_RETAIN;
908 context->frame->bg_transparent = (context->gif89.transparent == context->background_index);
910 context->animation->n_frames ++;
911 context->animation->frames = g_list_append (context->animation->frames, context->frame);
913 /* Only call prepare_func for the first frame */
914 if (context->animation->frames->next == NULL) {
915 if (context->animation->width == 0 )
916 context->animation->width = gdk_pixbuf_get_width(context->frame->pixbuf);
917 if (context->animation->height == 0)
918 context->animation->height = gdk_pixbuf_get_height (context->frame->pixbuf);
920 if (context->prepare_func)
921 (* context->prepare_func) (context->frame->pixbuf,
922 GDK_PIXBUF_ANIMATION (context->animation),
925 /* Otherwise init frame with last frame */
927 GdkPixbufFrame *prev_frame;
930 link = g_list_find (context->animation->frames, context->frame);
932 prev_frame = link->prev->data;
934 gdk_pixbuf_gif_anim_frame_composite (context->animation, prev_frame);
936 /* Composite failed */
937 if (prev_frame->composited == NULL) {
938 GdkPixbufFrame *frame = NULL;
939 link = g_list_first (context->animation->frames);
940 while (link != NULL) {
941 frame = (GdkPixbufFrame *)link->data;
943 if (frame->pixbuf != NULL)
944 g_object_unref (frame->pixbuf);
945 if (frame->composited != NULL)
946 g_object_unref (frame->composited);
947 if (frame->revert != NULL)
948 g_object_unref (frame->revert);
954 g_list_free (context->animation->frames);
955 context->animation->frames = NULL;
957 g_set_error_literal (context->error,
959 GDK_PIXBUF_ERROR_INSUFFICIENT_MEMORY,
960 _("Not enough memory to composite a frame in GIF file"));
964 x = context->frame->x_offset;
965 y = context->frame->y_offset;
966 w = gdk_pixbuf_get_width (context->frame->pixbuf);
967 h = gdk_pixbuf_get_height (context->frame->pixbuf);
968 if (clip_frame (context, &x, &y, &w, &h))
969 gdk_pixbuf_copy_area (prev_frame->composited,
971 context->frame->pixbuf,
976 dest = gdk_pixbuf_get_pixels (context->frame->pixbuf);
979 lower_bound = upper_bound = context->draw_ypos;
980 first_pass = context->draw_pass;
983 guchar (*cmap)[MAXCOLORMAPSIZE];
985 if (context->frame_cmap_active)
986 cmap = context->frame_color_map;
988 cmap = context->global_color_map;
990 v = lzw_read_byte (context);
996 g_assert (gdk_pixbuf_get_has_alpha (context->frame->pixbuf));
998 temp = dest + context->draw_ypos * gdk_pixbuf_get_rowstride (context->frame->pixbuf) + context->draw_xpos * 4;
999 *temp = cmap [0][(guchar) v];
1000 *(temp+1) = cmap [1][(guchar) v];
1001 *(temp+2) = cmap [2][(guchar) v];
1002 *(temp+3) = (guchar) ((v == context->gif89.transparent) ? 0 : 255);
1004 if (context->prepare_func && context->frame_interlace)
1005 gif_fill_in_lines (context, dest, v);
1007 context->draw_xpos++;
1009 if (context->draw_xpos == context->frame_len) {
1010 context->draw_xpos = 0;
1011 if (context->frame_interlace) {
1012 switch (context->draw_pass) {
1015 context->draw_ypos += 8;
1018 context->draw_ypos += 4;
1021 context->draw_ypos += 2;
1025 if (context->draw_ypos >= context->frame_height) {
1026 context->draw_pass++;
1027 switch (context->draw_pass) {
1029 context->draw_ypos = 4;
1032 context->draw_ypos = 2;
1035 context->draw_ypos = 1;
1042 context->draw_ypos++;
1044 if (context->draw_pass != first_pass) {
1045 if (context->draw_ypos > lower_bound) {
1047 upper_bound = context->frame_height;
1052 upper_bound = context->draw_ypos;
1054 if (context->draw_ypos >= context->frame_height)
1060 context->state = GIF_GET_NEXT_STEP;
1067 context->frame->need_recomposite = TRUE;
1069 if (bound_flag && context->update_func) {
1070 if (lower_bound <= upper_bound && first_pass == context->draw_pass) {
1071 maybe_update (context,
1072 context->frame->x_offset,
1073 context->frame->y_offset + lower_bound,
1074 gdk_pixbuf_get_width (context->frame->pixbuf),
1075 upper_bound - lower_bound);
1077 if (lower_bound <= upper_bound) {
1078 maybe_update (context,
1079 context->frame->x_offset,
1080 context->frame->y_offset,
1081 gdk_pixbuf_get_width (context->frame->pixbuf),
1082 gdk_pixbuf_get_height (context->frame->pixbuf));
1084 maybe_update (context,
1085 context->frame->x_offset,
1086 context->frame->y_offset,
1087 gdk_pixbuf_get_width (context->frame->pixbuf),
1089 maybe_update (context,
1090 context->frame->x_offset,
1091 context->frame->y_offset + lower_bound,
1092 gdk_pixbuf_get_width (context->frame->pixbuf),
1093 gdk_pixbuf_get_height (context->frame->pixbuf) - lower_bound);
1098 if (context->state == GIF_GET_NEXT_STEP) {
1099 /* Will be freed with context->animation, we are just
1100 * marking that we're done with it (no current frame)
1102 context->frame = NULL;
1103 context->frame_cmap_active = FALSE;
1105 if (context->stop_after_first_frame)
1106 context->state = GIF_DONE;
1113 gif_set_prepare_lzw (GifContext *context)
1115 context->state = GIF_PREPARE_LZW;
1116 context->lzw_code_pending = -1;
1119 gif_prepare_lzw (GifContext *context)
1123 if (!gif_read (context, &(context->lzw_set_code_size), 1)) {
1124 /*g_message (_("GIF: EOF / read error on image data\n"));*/
1128 if (context->lzw_set_code_size > MAX_LZW_BITS) {
1129 g_set_error_literal (context->error,
1131 GDK_PIXBUF_ERROR_CORRUPT_IMAGE,
1132 _("GIF image is corrupt (incorrect LZW compression)"));
1136 context->lzw_code_size = context->lzw_set_code_size + 1;
1137 context->lzw_clear_code = 1 << context->lzw_set_code_size;
1138 context->lzw_end_code = context->lzw_clear_code + 1;
1139 context->lzw_max_code_size = 2 * context->lzw_clear_code;
1140 context->lzw_max_code = context->lzw_clear_code + 2;
1141 context->lzw_fresh = TRUE;
1142 context->code_curbit = 0;
1143 context->code_lastbit = 0;
1144 context->code_last_byte = 0;
1145 context->code_done = FALSE;
1147 g_assert (context->lzw_clear_code <=
1148 G_N_ELEMENTS (context->lzw_table[0]));
1150 for (i = 0; i < context->lzw_clear_code; ++i) {
1151 context->lzw_table[0][i] = 0;
1152 context->lzw_table[1][i] = i;
1154 for (; i < (1 << MAX_LZW_BITS); ++i)
1155 context->lzw_table[0][i] = context->lzw_table[1][0] = 0;
1157 context->lzw_sp = context->lzw_stack;
1158 gif_set_get_lzw (context);
1163 /* needs 13 bytes to proceed. */
1165 gif_init (GifContext *context)
1167 unsigned char buf[16];
1170 if (!gif_read (context, buf, 6)) {
1171 /* Unable to read magic number,
1172 * gif_read() should have set error
1177 if (strncmp ((char *) buf, "GIF", 3) != 0) {
1178 /* Not a GIF file */
1179 g_set_error_literal (context->error,
1181 GDK_PIXBUF_ERROR_CORRUPT_IMAGE,
1182 _("File does not appear to be a GIF file"));
1186 strncpy (version, (char *) buf + 3, 3);
1189 if ((strcmp (version, "87a") != 0) && (strcmp (version, "89a") != 0)) {
1190 /* bad version number, not '87a' or '89a' */
1191 g_set_error (context->error,
1193 GDK_PIXBUF_ERROR_CORRUPT_IMAGE,
1194 _("Version %s of the GIF file format is not supported"),
1199 /* read the screen descriptor */
1200 if (!gif_read (context, buf, 7)) {
1201 /* Failed to read screen descriptor, error set */
1205 context->width = LM_to_uint (buf[0], buf[1]);
1206 context->height = LM_to_uint (buf[2], buf[3]);
1208 * high bit: whether to use the background index
1209 * next 3: color resolution
1210 * next: whether colormap is sorted by priority of allocation
1211 * last 3: size of colormap
1213 context->global_bit_pixel = 2 << (buf[4] & 0x07);
1214 context->global_color_resolution = (((buf[4] & 0x70) >> 3) + 1);
1215 context->has_global_cmap = (buf[4] & 0x80) != 0;
1216 context->background_index = buf[5];
1217 context->aspect_ratio = buf[6];
1219 /* Use background of transparent black as default, though if
1220 * one isn't set explicitly no one should ever use it.
1222 context->animation->bg_red = 0;
1223 context->animation->bg_green = 0;
1224 context->animation->bg_blue = 0;
1226 context->animation->width = context->width;
1227 context->animation->height = context->height;
1229 if (context->has_global_cmap) {
1230 gif_set_get_colormap (context);
1232 context->state = GIF_GET_NEXT_STEP;
1235 #ifdef DUMP_IMAGE_DETAILS
1236 g_print (">Image width: %d height: %d global_cmap: %d background: %d\n",
1237 context->width, context->height, context->has_global_cmap, context->background_index);
1244 gif_set_get_frame_info (GifContext *context)
1246 context->state = GIF_GET_FRAME_INFO;
1250 gif_get_frame_info (GifContext *context)
1252 unsigned char buf[9];
1254 if (!gif_read (context, buf, 9)) {
1258 /* Okay, we got all the info we need. Lets record it */
1259 context->frame_len = LM_to_uint (buf[4], buf[5]);
1260 context->frame_height = LM_to_uint (buf[6], buf[7]);
1261 context->x_offset = LM_to_uint (buf[0], buf[1]);
1262 context->y_offset = LM_to_uint (buf[2], buf[3]);
1264 if (context->animation->frames == NULL &&
1265 context->gif89.disposal == 3) {
1266 /* First frame can't have "revert to previous" as its
1267 * dispose mode. Silently use "retain" instead.
1269 context->gif89.disposal = 0;
1272 context->frame_interlace = BitSet (buf[8], INTERLACE);
1274 #ifdef DUMP_IMAGE_DETAILS
1275 g_print (">width: %d height: %d xoffset: %d yoffset: %d disposal: %d delay: %d transparent: %d interlace: %d\n",
1276 context->frame_len, context->frame_height, context->x_offset, context->y_offset,
1277 context->gif89.disposal, context->gif89.delay_time, context->gif89.transparent, context->frame_interlace);
1280 if (BitSet (buf[8], LOCALCOLORMAP)) {
1282 #ifdef DUMP_IMAGE_DETAILS
1283 g_print (">has local colormap\n");
1286 /* Does this frame have it's own colormap. */
1287 /* really only relevant when looking at the first frame
1288 * of an animated gif. */
1289 /* if it does, we need to re-read in the colormap,
1290 * the gray_scale, and the bit_pixel */
1291 context->frame_cmap_active = TRUE;
1292 context->frame_bit_pixel = 1 << ((buf[8] & 0x07) + 1);
1293 gif_set_get_colormap2 (context);
1297 if (!context->has_global_cmap) {
1298 context->state = GIF_DONE;
1300 g_set_error_literal (context->error,
1302 GDK_PIXBUF_ERROR_CORRUPT_IMAGE,
1303 _("GIF image has no global colormap, and a frame inside it has no local colormap."));
1308 gif_set_prepare_lzw (context);
1314 gif_get_next_step (GifContext *context)
1318 if (!gif_read (context, &c, 1)) {
1322 /* GIF terminator */
1323 /* hmm. Not 100% sure what to do about this. Should
1324 * i try to return a blank image instead? */
1325 context->state = GIF_DONE;
1330 /* Check the extension */
1331 gif_set_get_extension (context);
1335 /* look for frame */
1337 /* Not a valid start character */
1340 /* load the frame */
1341 gif_set_get_frame_info (context);
1347 #define LOG(x) /* g_print ("%s: %s\n", G_STRLOC, x); */
1350 gif_main_loop (GifContext *context)
1355 switch (context->state) {
1358 retval = gif_init (context);
1361 case GIF_GET_COLORMAP:
1362 LOG("get_colormap\n");
1363 retval = gif_get_colormap (context);
1365 context->state = GIF_GET_NEXT_STEP;
1368 case GIF_GET_NEXT_STEP:
1370 retval = gif_get_next_step (context);
1373 case GIF_GET_FRAME_INFO:
1374 LOG("frame_info\n");
1375 retval = gif_get_frame_info (context);
1378 case GIF_GET_EXTENSION:
1379 LOG("get_extension\n");
1380 retval = gif_get_extension (context);
1382 context->state = GIF_GET_NEXT_STEP;
1385 case GIF_GET_COLORMAP2:
1386 LOG("get_colormap2\n");
1387 retval = gif_get_colormap2 (context);
1389 gif_set_prepare_lzw (context);
1392 case GIF_PREPARE_LZW:
1393 LOG("prepare_lzw\n");
1394 retval = gif_prepare_lzw (context);
1397 case GIF_LZW_FILL_BUFFER:
1398 LOG("fill_buffer\n");
1399 retval = gif_lzw_fill_buffer (context);
1402 case GIF_LZW_CLEAR_CODE:
1403 LOG("clear_code\n");
1404 retval = gif_lzw_clear_code (context);
1409 retval = gif_get_lzw (context);
1418 } while ((retval == 0) || (retval == -3));
1426 GifContext *context;
1428 context = g_try_malloc (sizeof (GifContext));
1429 if (context == NULL)
1432 memset (context, 0, sizeof (GifContext));
1434 context->animation = g_object_new (GDK_TYPE_PIXBUF_GIF_ANIM, NULL);
1435 context->frame = NULL;
1436 context->file = NULL;
1437 context->state = GIF_START;
1438 context->prepare_func = NULL;
1439 context->update_func = NULL;
1440 context->user_data = NULL;
1441 context->buf = NULL;
1442 context->amount_needed = 0;
1443 context->gif89.transparent = -1;
1444 context->gif89.delay_time = -1;
1445 context->gif89.input_flag = -1;
1446 context->gif89.disposal = -1;
1447 context->animation->loop = 1;
1448 context->in_loop_extension = FALSE;
1449 context->stop_after_first_frame = FALSE;
1453 /* Shared library entry point */
1455 gdk_pixbuf__gif_image_load (FILE *file, GError **error)
1457 GifContext *context;
1460 g_return_val_if_fail (file != NULL, NULL);
1462 context = new_context ();
1464 if (context == NULL) {
1465 g_set_error_literal (error,
1467 GDK_PIXBUF_ERROR_INSUFFICIENT_MEMORY,
1468 _("Not enough memory to load GIF file"));
1472 context->file = file;
1473 context->error = error;
1474 context->stop_after_first_frame = TRUE;
1476 if (gif_main_loop (context) == -1 || context->animation->frames == NULL) {
1477 if (context->error && *(context->error) == NULL)
1478 g_set_error_literal (context->error,
1480 GDK_PIXBUF_ERROR_CORRUPT_IMAGE,
1481 _("GIF file was missing some data (perhaps it was truncated somehow?)"));
1484 pixbuf = gdk_pixbuf_animation_get_static_image (GDK_PIXBUF_ANIMATION (context->animation));
1487 g_object_ref (pixbuf);
1489 g_object_unref (context->animation);
1491 g_free (context->buf);
1498 gdk_pixbuf__gif_image_begin_load (GdkPixbufModuleSizeFunc size_func,
1499 GdkPixbufModulePreparedFunc prepare_func,
1500 GdkPixbufModuleUpdatedFunc update_func,
1504 GifContext *context;
1509 context = new_context ();
1511 if (context == NULL) {
1512 g_set_error_literal (error,
1514 GDK_PIXBUF_ERROR_INSUFFICIENT_MEMORY,
1515 _("Not enough memory to load GIF file"));
1519 context->error = error;
1520 context->prepare_func = prepare_func;
1521 context->update_func = update_func;
1522 context->user_data = user_data;
1524 return (gpointer) context;
1528 gdk_pixbuf__gif_image_stop_load (gpointer data, GError **error)
1530 GifContext *context = (GifContext *) data;
1531 gboolean retval = TRUE;
1533 if (context->state != GIF_DONE || context->animation->frames == NULL) {
1534 g_set_error_literal (error,
1536 GDK_PIXBUF_ERROR_CORRUPT_IMAGE,
1537 _("GIF image was truncated or incomplete."));
1542 g_object_unref (context->animation);
1544 g_free (context->buf);
1551 gdk_pixbuf__gif_image_load_increment (gpointer data,
1552 const guchar *buf, guint size,
1556 GifContext *context = (GifContext *) data;
1558 context->error = error;
1560 if (context->amount_needed == 0) {
1561 /* we aren't looking for some bytes. */
1562 /* we can use buf now, but we don't want to keep it around at all.
1563 * it will be gone by the end of the call. */
1564 context->buf = (guchar*) buf; /* very dubious const cast */
1566 context->size = size;
1568 /* we need some bytes */
1569 if (size < context->amount_needed) {
1570 context->amount_needed -= size;
1571 /* copy it over and return */
1572 memcpy (context->buf + context->size, buf, size);
1573 context->size += size;
1575 } else if (size == context->amount_needed) {
1576 memcpy (context->buf + context->size, buf, size);
1577 context->size += size;
1579 context->buf = g_realloc (context->buf, context->size + size);
1580 memcpy (context->buf + context->size, buf, size);
1581 context->size += size;
1585 retval = gif_main_loop (context);
1588 if (context->buf == buf)
1589 context->buf = NULL;
1593 /* we didn't have enough memory */
1594 /* prepare for the next image_load_increment */
1595 if (context->buf == buf) {
1596 g_assert (context->size == size);
1597 context->buf = g_new (guchar, context->amount_needed + (context->size - context->ptr));
1598 memcpy (context->buf, buf + context->ptr, context->size - context->ptr);
1600 /* copy the left overs to the begining of the buffer */
1601 /* and realloc the memory */
1602 memmove (context->buf, context->buf + context->ptr, context->size - context->ptr);
1603 context->buf = g_realloc (context->buf, context->amount_needed + (context->size - context->ptr));
1605 context->size = context->size - context->ptr;
1608 /* we are prolly all done */
1609 if (context->buf == buf)
1610 context->buf = NULL;
1615 static GdkPixbufAnimation *
1616 gdk_pixbuf__gif_image_load_animation (FILE *file,
1619 GifContext *context;
1620 GdkPixbufAnimation *animation;
1622 g_return_val_if_fail (file != NULL, NULL);
1624 context = new_context ();
1626 if (context == NULL) {
1627 g_set_error_literal (error,
1629 GDK_PIXBUF_ERROR_INSUFFICIENT_MEMORY,
1630 _("Not enough memory to load GIF file"));
1634 context->error = error;
1635 context->file = file;
1637 if (gif_main_loop (context) == -1 || context->animation->frames == NULL) {
1638 if (context->error && *(context->error) == NULL)
1639 g_set_error_literal (context->error,
1641 GDK_PIXBUF_ERROR_CORRUPT_IMAGE,
1642 _("GIF file was missing some data (perhaps it was truncated somehow?)"));
1644 g_object_unref (context->animation);
1645 context->animation = NULL;
1648 if (context->animation)
1649 animation = GDK_PIXBUF_ANIMATION (context->animation);
1653 if (context->error && *(context->error))
1654 g_print ("%s\n", (*(context->error))->message);
1656 g_free (context->buf);
1662 #define MODULE_ENTRY(function) G_MODULE_EXPORT void function
1664 #define MODULE_ENTRY(function) void _gdk_pixbuf__gif_ ## function
1667 MODULE_ENTRY (fill_vtable) (GdkPixbufModule *module)
1669 module->load = gdk_pixbuf__gif_image_load;
1670 module->begin_load = gdk_pixbuf__gif_image_begin_load;
1671 module->stop_load = gdk_pixbuf__gif_image_stop_load;
1672 module->load_increment = gdk_pixbuf__gif_image_load_increment;
1673 module->load_animation = gdk_pixbuf__gif_image_load_animation;
1676 MODULE_ENTRY (fill_info) (GdkPixbufFormat *info)
1678 static GdkPixbufModulePattern signature[] = {
1679 { "GIF8", NULL, 100 },
1682 static gchar * mime_types[] = {
1686 static gchar * extensions[] = {
1692 info->signature = signature;
1693 info->description = N_("The GIF image format");
1694 info->mime_types = mime_types;
1695 info->extensions = extensions;
1696 info->flags = GDK_PIXBUF_FORMAT_THREADSAFE;
1697 info->license = "LGPL";