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 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 (context->block_buf, "NETSCAPE2.0", 11) ||
415 !strncmp (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 (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 (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 (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 (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 (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 (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 /* Some GIFs apparently have delay time of 0,
878 * that crashes everything so set it to "fast".
879 * Also, timeouts less than 20 or so just lock up
880 * the app or make the animation choppy, so fix them.
882 if (context->frame->delay_time < 20)
883 context->frame->delay_time = 20; /* 20 = "fast" */
885 context->frame->elapsed = context->animation->total_time;
886 context->animation->total_time += context->frame->delay_time;
888 switch (context->gif89.disposal) {
891 context->frame->action = GDK_PIXBUF_FRAME_RETAIN;
894 context->frame->action = GDK_PIXBUF_FRAME_DISPOSE;
897 context->frame->action = GDK_PIXBUF_FRAME_REVERT;
900 context->frame->action = GDK_PIXBUF_FRAME_RETAIN;
904 context->frame->bg_transparent = (context->gif89.transparent == context->background_index);
906 context->animation->n_frames ++;
907 context->animation->frames = g_list_append (context->animation->frames, context->frame);
909 /* Only call prepare_func for the first frame */
910 if (context->animation->frames->next == NULL) {
911 if (context->animation->width == 0 )
912 context->animation->width = gdk_pixbuf_get_width(context->frame->pixbuf);
913 if (context->animation->height == 0)
914 context->animation->height = gdk_pixbuf_get_height (context->frame->pixbuf);
916 if (context->prepare_func)
917 (* context->prepare_func) (context->frame->pixbuf,
918 GDK_PIXBUF_ANIMATION (context->animation),
921 /* Otherwise init frame with last frame */
923 GdkPixbufFrame *prev_frame;
926 link = g_list_find (context->animation->frames, context->frame);
928 prev_frame = link->prev->data;
930 gdk_pixbuf_gif_anim_frame_composite (context->animation, prev_frame);
932 /* Composite failed */
933 if (prev_frame->composited == NULL) {
934 GdkPixbufFrame *frame = NULL;
935 link = g_list_first (context->animation->frames);
936 while (link != NULL) {
937 frame = (GdkPixbufFrame *)link->data;
939 if (frame->pixbuf != NULL)
940 g_object_unref (frame->pixbuf);
941 if (frame->composited != NULL)
942 g_object_unref (frame->composited);
943 if (frame->revert != NULL)
944 g_object_unref (frame->revert);
950 g_list_free (context->animation->frames);
951 context->animation->frames = NULL;
953 g_set_error (context->error,
955 GDK_PIXBUF_ERROR_INSUFFICIENT_MEMORY,
956 _("Not enough memory to composite a frame in GIF file"));
960 x = context->frame->x_offset;
961 y = context->frame->y_offset;
962 w = gdk_pixbuf_get_width (context->frame->pixbuf);
963 h = gdk_pixbuf_get_height (context->frame->pixbuf);
964 if (clip_frame (context, &x, &y, &w, &h))
965 gdk_pixbuf_copy_area (prev_frame->composited,
967 context->frame->pixbuf,
972 dest = gdk_pixbuf_get_pixels (context->frame->pixbuf);
975 lower_bound = upper_bound = context->draw_ypos;
976 first_pass = context->draw_pass;
979 guchar (*cmap)[MAXCOLORMAPSIZE];
981 if (context->frame_cmap_active)
982 cmap = context->frame_color_map;
984 cmap = context->global_color_map;
986 v = lzw_read_byte (context);
992 g_assert (gdk_pixbuf_get_has_alpha (context->frame->pixbuf));
994 temp = dest + context->draw_ypos * gdk_pixbuf_get_rowstride (context->frame->pixbuf) + context->draw_xpos * 4;
995 *temp = cmap [0][(guchar) v];
996 *(temp+1) = cmap [1][(guchar) v];
997 *(temp+2) = cmap [2][(guchar) v];
998 *(temp+3) = (guchar) ((v == context->gif89.transparent) ? 0 : 255);
1000 if (context->prepare_func && context->frame_interlace)
1001 gif_fill_in_lines (context, dest, v);
1003 context->draw_xpos++;
1005 if (context->draw_xpos == context->frame_len) {
1006 context->draw_xpos = 0;
1007 if (context->frame_interlace) {
1008 switch (context->draw_pass) {
1011 context->draw_ypos += 8;
1014 context->draw_ypos += 4;
1017 context->draw_ypos += 2;
1021 if (context->draw_ypos >= context->frame_height) {
1022 context->draw_pass++;
1023 switch (context->draw_pass) {
1025 context->draw_ypos = 4;
1028 context->draw_ypos = 2;
1031 context->draw_ypos = 1;
1038 context->draw_ypos++;
1040 if (context->draw_pass != first_pass) {
1041 if (context->draw_ypos > lower_bound) {
1043 upper_bound = context->frame_height;
1048 upper_bound = context->draw_ypos;
1050 if (context->draw_ypos >= context->frame_height)
1056 context->state = GIF_GET_NEXT_STEP;
1063 context->frame->need_recomposite = TRUE;
1065 if (bound_flag && context->update_func) {
1066 if (lower_bound <= upper_bound && first_pass == context->draw_pass) {
1067 maybe_update (context,
1068 context->frame->x_offset,
1069 context->frame->y_offset + lower_bound,
1070 gdk_pixbuf_get_width (context->frame->pixbuf),
1071 upper_bound - lower_bound);
1073 if (lower_bound <= upper_bound) {
1074 maybe_update (context,
1075 context->frame->x_offset,
1076 context->frame->y_offset,
1077 gdk_pixbuf_get_width (context->frame->pixbuf),
1078 gdk_pixbuf_get_height (context->frame->pixbuf));
1080 maybe_update (context,
1081 context->frame->x_offset,
1082 context->frame->y_offset,
1083 gdk_pixbuf_get_width (context->frame->pixbuf),
1085 maybe_update (context,
1086 context->frame->x_offset,
1087 context->frame->y_offset + lower_bound,
1088 gdk_pixbuf_get_width (context->frame->pixbuf),
1089 gdk_pixbuf_get_height (context->frame->pixbuf) - lower_bound);
1094 if (context->state == GIF_GET_NEXT_STEP) {
1095 /* Will be freed with context->animation, we are just
1096 * marking that we're done with it (no current frame)
1098 context->frame = NULL;
1099 context->frame_cmap_active = FALSE;
1101 if (context->stop_after_first_frame)
1102 context->state = GIF_DONE;
1109 gif_set_prepare_lzw (GifContext *context)
1111 context->state = GIF_PREPARE_LZW;
1112 context->lzw_code_pending = -1;
1115 gif_prepare_lzw (GifContext *context)
1119 if (!gif_read (context, &(context->lzw_set_code_size), 1)) {
1120 /*g_message (_("GIF: EOF / read error on image data\n"));*/
1124 if (context->lzw_set_code_size > MAX_LZW_BITS) {
1125 g_set_error (context->error,
1127 GDK_PIXBUF_ERROR_CORRUPT_IMAGE,
1128 _("GIF image is corrupt (incorrect LZW compression)"));
1132 context->lzw_code_size = context->lzw_set_code_size + 1;
1133 context->lzw_clear_code = 1 << context->lzw_set_code_size;
1134 context->lzw_end_code = context->lzw_clear_code + 1;
1135 context->lzw_max_code_size = 2 * context->lzw_clear_code;
1136 context->lzw_max_code = context->lzw_clear_code + 2;
1137 context->lzw_fresh = TRUE;
1138 context->code_curbit = 0;
1139 context->code_lastbit = 0;
1140 context->code_last_byte = 0;
1141 context->code_done = FALSE;
1143 g_assert (context->lzw_clear_code <=
1144 G_N_ELEMENTS (context->lzw_table[0]));
1146 for (i = 0; i < context->lzw_clear_code; ++i) {
1147 context->lzw_table[0][i] = 0;
1148 context->lzw_table[1][i] = i;
1150 for (; i < (1 << MAX_LZW_BITS); ++i)
1151 context->lzw_table[0][i] = context->lzw_table[1][0] = 0;
1153 context->lzw_sp = context->lzw_stack;
1154 gif_set_get_lzw (context);
1159 /* needs 13 bytes to proceed. */
1161 gif_init (GifContext *context)
1163 unsigned char buf[16];
1166 if (!gif_read (context, buf, 6)) {
1167 /* Unable to read magic number,
1168 * gif_read() should have set error
1173 if (strncmp ((char *) buf, "GIF", 3) != 0) {
1174 /* Not a GIF file */
1175 g_set_error (context->error,
1177 GDK_PIXBUF_ERROR_CORRUPT_IMAGE,
1178 _("File does not appear to be a GIF file"));
1182 strncpy (version, (char *) buf + 3, 3);
1185 if ((strcmp (version, "87a") != 0) && (strcmp (version, "89a") != 0)) {
1186 /* bad version number, not '87a' or '89a' */
1187 g_set_error (context->error,
1189 GDK_PIXBUF_ERROR_CORRUPT_IMAGE,
1190 _("Version %s of the GIF file format is not supported"),
1195 /* read the screen descriptor */
1196 if (!gif_read (context, buf, 7)) {
1197 /* Failed to read screen descriptor, error set */
1201 context->width = LM_to_uint (buf[0], buf[1]);
1202 context->height = LM_to_uint (buf[2], buf[3]);
1204 * high bit: whether to use the background index
1205 * next 3: color resolution
1206 * next: whether colormap is sorted by priority of allocation
1207 * last 3: size of colormap
1209 context->global_bit_pixel = 2 << (buf[4] & 0x07);
1210 context->global_color_resolution = (((buf[4] & 0x70) >> 3) + 1);
1211 context->has_global_cmap = (buf[4] & 0x80) != 0;
1212 context->background_index = buf[5];
1213 context->aspect_ratio = buf[6];
1215 /* Use background of transparent black as default, though if
1216 * one isn't set explicitly no one should ever use it.
1218 context->animation->bg_red = 0;
1219 context->animation->bg_green = 0;
1220 context->animation->bg_blue = 0;
1222 context->animation->width = context->width;
1223 context->animation->height = context->height;
1225 if (context->has_global_cmap) {
1226 gif_set_get_colormap (context);
1228 context->state = GIF_GET_NEXT_STEP;
1231 #ifdef DUMP_IMAGE_DETAILS
1232 g_print (">Image width: %d height: %d global_cmap: %d background: %d\n",
1233 context->width, context->height, context->has_global_cmap, context->background_index);
1240 gif_set_get_frame_info (GifContext *context)
1242 context->state = GIF_GET_FRAME_INFO;
1246 gif_get_frame_info (GifContext *context)
1248 unsigned char buf[9];
1250 if (!gif_read (context, buf, 9)) {
1254 /* Okay, we got all the info we need. Lets record it */
1255 context->frame_len = LM_to_uint (buf[4], buf[5]);
1256 context->frame_height = LM_to_uint (buf[6], buf[7]);
1257 context->x_offset = LM_to_uint (buf[0], buf[1]);
1258 context->y_offset = LM_to_uint (buf[2], buf[3]);
1260 if (context->animation->frames == NULL &&
1261 context->gif89.disposal == 3) {
1262 /* First frame can't have "revert to previous" as its
1263 * dispose mode. Silently use "retain" instead.
1265 context->gif89.disposal = 0;
1268 context->frame_interlace = BitSet (buf[8], INTERLACE);
1270 #ifdef DUMP_IMAGE_DETAILS
1271 g_print (">width: %d height: %d xoffset: %d yoffset: %d disposal: %d delay: %d transparent: %d interlace: %d\n",
1272 context->frame_len, context->frame_height, context->x_offset, context->y_offset,
1273 context->gif89.disposal, context->gif89.delay_time, context->gif89.transparent, context->frame_interlace);
1276 if (BitSet (buf[8], LOCALCOLORMAP)) {
1278 #ifdef DUMP_IMAGE_DETAILS
1279 g_print (">has local colormap\n");
1282 /* Does this frame have it's own colormap. */
1283 /* really only relevant when looking at the first frame
1284 * of an animated gif. */
1285 /* if it does, we need to re-read in the colormap,
1286 * the gray_scale, and the bit_pixel */
1287 context->frame_cmap_active = TRUE;
1288 context->frame_bit_pixel = 1 << ((buf[8] & 0x07) + 1);
1289 gif_set_get_colormap2 (context);
1293 if (!context->has_global_cmap) {
1294 context->state = GIF_DONE;
1296 g_set_error (context->error,
1298 GDK_PIXBUF_ERROR_CORRUPT_IMAGE,
1299 _("GIF image has no global colormap, and a frame inside it has no local colormap."));
1304 gif_set_prepare_lzw (context);
1310 gif_get_next_step (GifContext *context)
1314 if (!gif_read (context, &c, 1)) {
1318 /* GIF terminator */
1319 /* hmm. Not 100% sure what to do about this. Should
1320 * i try to return a blank image instead? */
1321 context->state = GIF_DONE;
1326 /* Check the extension */
1327 gif_set_get_extension (context);
1331 /* look for frame */
1333 /* Not a valid start character */
1336 /* load the frame */
1337 gif_set_get_frame_info (context);
1343 #define LOG(x) /* g_print ("%d: %s\n", __LINE__, x); */
1346 gif_main_loop (GifContext *context)
1351 switch (context->state) {
1354 retval = gif_init (context);
1357 case GIF_GET_COLORMAP:
1358 LOG("get_colormap\n");
1359 retval = gif_get_colormap (context);
1361 context->state = GIF_GET_NEXT_STEP;
1364 case GIF_GET_NEXT_STEP:
1366 retval = gif_get_next_step (context);
1369 case GIF_GET_FRAME_INFO:
1370 LOG("frame_info\n");
1371 retval = gif_get_frame_info (context);
1374 case GIF_GET_EXTENSION:
1375 LOG("get_extension\n");
1376 retval = gif_get_extension (context);
1378 context->state = GIF_GET_NEXT_STEP;
1381 case GIF_GET_COLORMAP2:
1382 LOG("get_colormap2\n");
1383 retval = gif_get_colormap2 (context);
1385 gif_set_prepare_lzw (context);
1388 case GIF_PREPARE_LZW:
1389 LOG("prepare_lzw\n");
1390 retval = gif_prepare_lzw (context);
1393 case GIF_LZW_FILL_BUFFER:
1394 LOG("fill_buffer\n");
1395 retval = gif_lzw_fill_buffer (context);
1398 case GIF_LZW_CLEAR_CODE:
1399 LOG("clear_code\n");
1400 retval = gif_lzw_clear_code (context);
1405 retval = gif_get_lzw (context);
1414 } while ((retval == 0) || (retval == -3));
1422 GifContext *context;
1424 context = g_try_malloc (sizeof (GifContext));
1425 if (context == NULL)
1428 memset (context, 0, sizeof (GifContext));
1430 context->animation = g_object_new (GDK_TYPE_PIXBUF_GIF_ANIM, NULL);
1431 context->frame = NULL;
1432 context->file = NULL;
1433 context->state = GIF_START;
1434 context->prepare_func = NULL;
1435 context->update_func = NULL;
1436 context->user_data = NULL;
1437 context->buf = NULL;
1438 context->amount_needed = 0;
1439 context->gif89.transparent = -1;
1440 context->gif89.delay_time = -1;
1441 context->gif89.input_flag = -1;
1442 context->gif89.disposal = -1;
1443 context->animation->loop = 1;
1444 context->in_loop_extension = FALSE;
1445 context->stop_after_first_frame = FALSE;
1449 /* Shared library entry point */
1451 gdk_pixbuf__gif_image_load (FILE *file, GError **error)
1453 GifContext *context;
1456 g_return_val_if_fail (file != NULL, NULL);
1458 context = new_context ();
1460 if (context == NULL) {
1463 GDK_PIXBUF_ERROR_INSUFFICIENT_MEMORY,
1464 _("Not enough memory to load GIF file"));
1468 context->file = file;
1469 context->error = error;
1470 context->stop_after_first_frame = TRUE;
1472 if (gif_main_loop (context) == -1 || context->animation->frames == NULL) {
1473 if (context->error && *(context->error) == NULL)
1474 g_set_error (context->error,
1476 GDK_PIXBUF_ERROR_CORRUPT_IMAGE,
1477 _("GIF file was missing some data (perhaps it was truncated somehow?)"));
1480 pixbuf = gdk_pixbuf_animation_get_static_image (GDK_PIXBUF_ANIMATION (context->animation));
1483 g_object_ref (pixbuf);
1485 g_object_unref (context->animation);
1487 g_free (context->buf);
1494 gdk_pixbuf__gif_image_begin_load (GdkPixbufModuleSizeFunc size_func,
1495 GdkPixbufModulePreparedFunc prepare_func,
1496 GdkPixbufModuleUpdatedFunc update_func,
1500 GifContext *context;
1505 context = new_context ();
1507 if (context == NULL) {
1510 GDK_PIXBUF_ERROR_INSUFFICIENT_MEMORY,
1511 _("Not enough memory to load GIF file"));
1515 context->error = error;
1516 context->prepare_func = prepare_func;
1517 context->update_func = update_func;
1518 context->user_data = user_data;
1520 return (gpointer) context;
1524 gdk_pixbuf__gif_image_stop_load (gpointer data, GError **error)
1526 GifContext *context = (GifContext *) data;
1527 gboolean retval = TRUE;
1529 if (context->state != GIF_DONE || context->animation->frames == NULL) {
1532 GDK_PIXBUF_ERROR_CORRUPT_IMAGE,
1533 _("GIF image was truncated or incomplete."));
1538 g_object_unref (context->animation);
1540 g_free (context->buf);
1547 gdk_pixbuf__gif_image_load_increment (gpointer data,
1548 const guchar *buf, guint size,
1552 GifContext *context = (GifContext *) data;
1554 context->error = error;
1556 if (context->amount_needed == 0) {
1557 /* we aren't looking for some bytes. */
1558 /* we can use buf now, but we don't want to keep it around at all.
1559 * it will be gone by the end of the call. */
1560 context->buf = (guchar*) buf; /* very dubious const cast */
1562 context->size = size;
1564 /* we need some bytes */
1565 if (size < context->amount_needed) {
1566 context->amount_needed -= size;
1567 /* copy it over and return */
1568 memcpy (context->buf + context->size, buf, size);
1569 context->size += size;
1571 } else if (size == context->amount_needed) {
1572 memcpy (context->buf + context->size, buf, size);
1573 context->size += size;
1575 context->buf = g_realloc (context->buf, context->size + size);
1576 memcpy (context->buf + context->size, buf, size);
1577 context->size += size;
1581 retval = gif_main_loop (context);
1584 if (context->buf == buf)
1585 context->buf = NULL;
1589 /* we didn't have enough memory */
1590 /* prepare for the next image_load_increment */
1591 if (context->buf == buf) {
1592 g_assert (context->size == size);
1593 context->buf = g_new (guchar, context->amount_needed + (context->size - context->ptr));
1594 memcpy (context->buf, buf + context->ptr, context->size - context->ptr);
1596 /* copy the left overs to the begining of the buffer */
1597 /* and realloc the memory */
1598 memmove (context->buf, context->buf + context->ptr, context->size - context->ptr);
1599 context->buf = g_realloc (context->buf, context->amount_needed + (context->size - context->ptr));
1601 context->size = context->size - context->ptr;
1604 /* we are prolly all done */
1605 if (context->buf == buf)
1606 context->buf = NULL;
1611 static GdkPixbufAnimation *
1612 gdk_pixbuf__gif_image_load_animation (FILE *file,
1615 GifContext *context;
1616 GdkPixbufAnimation *animation;
1618 g_return_val_if_fail (file != NULL, NULL);
1620 context = new_context ();
1622 if (context == NULL) {
1625 GDK_PIXBUF_ERROR_INSUFFICIENT_MEMORY,
1626 _("Not enough memory to load GIF file"));
1630 context->error = error;
1631 context->file = file;
1633 if (gif_main_loop (context) == -1 || context->animation->frames == NULL) {
1634 if (context->error && *(context->error) == NULL)
1635 g_set_error (context->error,
1637 GDK_PIXBUF_ERROR_CORRUPT_IMAGE,
1638 _("GIF file was missing some data (perhaps it was truncated somehow?)"));
1640 g_object_unref (context->animation);
1641 context->animation = NULL;
1644 if (context->animation)
1645 animation = GDK_PIXBUF_ANIMATION (context->animation);
1649 if (context->error && *(context->error))
1650 g_print ("%s\n", (*(context->error))->message);
1652 g_free (context->buf);
1658 #define MODULE_ENTRY(type,function) function
1660 #define MODULE_ENTRY(type,function) _gdk_pixbuf__ ## type ## _ ## function
1664 MODULE_ENTRY (gif, fill_vtable) (GdkPixbufModule *module)
1666 module->load = gdk_pixbuf__gif_image_load;
1667 module->begin_load = gdk_pixbuf__gif_image_begin_load;
1668 module->stop_load = gdk_pixbuf__gif_image_stop_load;
1669 module->load_increment = gdk_pixbuf__gif_image_load_increment;
1670 module->load_animation = gdk_pixbuf__gif_image_load_animation;
1674 MODULE_ENTRY (gif, fill_info) (GdkPixbufFormat *info)
1676 static GdkPixbufModulePattern signature[] = {
1677 { "GIF8", NULL, 100 },
1680 static gchar * mime_types[] = {
1684 static gchar * extensions[] = {
1690 info->signature = signature;
1691 info->description = N_("The GIF image format");
1692 info->mime_types = mime_types;
1693 info->extensions = extensions;
1694 info->flags = GDK_PIXBUF_FORMAT_THREADSAFE;
1695 info->license = "LGPL";