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
68 #define MAXCOLORMAPSIZE 256
69 #define MAX_LZW_BITS 12
71 #define INTERLACE 0x40
72 #define LOCALCOLORMAP 0x80
73 #define BitSet(byte, bit) (((byte) & (bit)) == (bit))
74 #define LM_to_uint(a,b) (((b)<<8)|(a))
78 typedef unsigned char CMap[3][MAXCOLORMAPSIZE];
80 /* Possible states we can be in. */
96 typedef struct _Gif89 Gif89;
105 typedef struct _GifContext GifContext;
108 int state; /* really only relevant for progressive loading */
112 gboolean has_global_cmap;
114 CMap global_color_map;
115 gint global_colormap_size;
116 unsigned int global_bit_pixel;
117 unsigned int global_color_resolution;
118 unsigned int background_index;
120 gboolean frame_cmap_active;
121 CMap frame_color_map;
122 gint frame_colormap_size;
123 unsigned int frame_bit_pixel;
125 unsigned int aspect_ratio;
126 GdkPixbufGifAnim *animation;
127 GdkPixbufFrame *frame;
130 /* stuff per frame. */
137 /* Static read only */
140 /* progressive read, only. */
141 ModulePreparedNotifyFunc prepare_func;
142 ModuleUpdatedNotifyFunc update_func;
149 /* extension context */
150 guchar extension_label;
151 guchar extension_flag;
153 /* get block context */
155 guchar block_buf[280];
158 int old_state; /* used by lzw_fill buffer */
159 /* get_code context */
164 int lzw_code_pending;
169 guchar lzw_set_code_size;
171 gint lzw_max_code_size;
178 gint lzw_table[2][(1 << MAX_LZW_BITS)];
179 gint lzw_stack[(1 << (MAX_LZW_BITS)) * 2 + 1];
181 /* painting context */
190 static int GetDataBlock (GifContext *, unsigned char *);
195 static int count = 0;
198 /* Returns TRUE if read is OK,
199 * FALSE if more memory is needed. */
201 gif_read (GifContext *context, guchar *buffer, size_t len)
210 g_print ("Fsize :%d\tcount :%d\t", len, count);
212 retval = (fread(buffer, len, 1, context->file) != 0);
214 if (!retval && ferror (context->file))
215 g_set_error (context->error,
217 g_file_error_from_errno (errno),
218 _("Failure reading GIF: %s"), strerror (errno));
222 for (i = 0; i < len; i++)
223 g_print ("%d ", buffer[i]);
231 /* g_print ("\tlooking for %d bytes. size == %d, ptr == %d\n", len, context->size, context->ptr); */
233 if ((context->size - context->ptr) >= len) {
237 memcpy (buffer, context->buf + context->ptr, len);
239 context->amount_needed = 0;
241 g_print ("Psize :%d\tcount :%d\t", len, count);
243 for (i = 0; i < len; i++)
244 g_print ("%d ", buffer[i]);
250 context->amount_needed = len - (context->size - context->ptr);
255 /* Changes the stage to be GIF_GET_COLORMAP */
257 gif_set_get_colormap (GifContext *context)
259 context->global_colormap_size = 0;
260 context->state = GIF_GET_COLORMAP;
264 gif_set_get_colormap2 (GifContext *context)
266 context->frame_colormap_size = 0;
267 context->state = GIF_GET_COLORMAP2;
271 gif_get_colormap (GifContext *context)
273 unsigned char rgb[3];
275 while (context->global_colormap_size < context->global_bit_pixel) {
276 if (!gif_read (context, rgb, sizeof (rgb))) {
280 context->global_color_map[0][context->global_colormap_size] = rgb[0];
281 context->global_color_map[1][context->global_colormap_size] = rgb[1];
282 context->global_color_map[2][context->global_colormap_size] = rgb[2];
284 if (context->global_colormap_size == context->background_index) {
285 context->animation->bg_red = rgb[0];
286 context->animation->bg_green = rgb[1];
287 context->animation->bg_blue = rgb[2];
290 context->global_colormap_size ++;
298 gif_get_colormap2 (GifContext *context)
300 unsigned char rgb[3];
302 while (context->frame_colormap_size < context->frame_bit_pixel) {
303 if (!gif_read (context, rgb, sizeof (rgb))) {
307 context->frame_color_map[0][context->frame_colormap_size] = rgb[0];
308 context->frame_color_map[1][context->frame_colormap_size] = rgb[1];
309 context->frame_color_map[2][context->frame_colormap_size] = rgb[2];
311 context->frame_colormap_size ++;
318 * in order for this function to work, we need to perform some black magic.
319 * We want to return -1 to let the calling function know, as before, that it needs
320 * more bytes. If we return 0, we were able to successfully read all block->count bytes.
321 * Problem is, we don't want to reread block_count every time, so we check to see if
322 * context->block_count is 0 before we read in the function.
324 * As a result, context->block_count MUST be 0 the first time the get_data_block is called
325 * within a context, and cannot be 0 the second time it's called.
329 get_data_block (GifContext *context,
334 if (context->block_count == 0) {
335 if (!gif_read (context, &context->block_count, 1)) {
340 if (context->block_count == 0)
346 if (!gif_read (context, buf, context->block_count)) {
354 gif_set_get_extension (GifContext *context)
356 context->state = GIF_GET_EXTENTION;
357 context->extension_flag = TRUE;
358 context->extension_label = 0;
359 context->block_count = 0;
360 context->block_ptr = 0;
364 gif_get_extension (GifContext *context)
367 gint empty_block = FALSE;
369 if (context->extension_flag) {
370 if (context->extension_label == 0) {
371 /* I guess bad things can happen if we have an extension of 0 )-: */
372 /* I should look into this sometime */
373 if (!gif_read (context, & context->extension_label , 1)) {
378 switch (context->extension_label) {
379 case 0xf9: /* Graphic Control Extension */
380 retval = get_data_block (context, (unsigned char *) context->block_buf, NULL);
384 if (context->frame == NULL) {
385 /* I only want to set the transparency if I haven't
386 * created the frame yet.
388 context->gif89.disposal = (context->block_buf[0] >> 2) & 0x7;
389 context->gif89.input_flag = (context->block_buf[0] >> 1) & 0x1;
390 context->gif89.delay_time = LM_to_uint (context->block_buf[1], context->block_buf[2]);
392 if ((context->block_buf[0] & 0x1) != 0) {
393 context->gif89.transparent = context->block_buf[3];
395 context->gif89.transparent = -1;
399 /* Now we've successfully loaded this one, we continue on our way */
400 context->block_count = 0;
401 context->extension_flag = FALSE;
403 /* Unhandled extension */
407 /* read all blocks, until I get an empty block, in case there was an extension I didn't know about. */
409 retval = get_data_block (context, (unsigned char *) context->block_buf, &empty_block);
412 context->block_count = 0;
413 } while (!empty_block);
418 static int ZeroDataBlock = FALSE;
421 GetDataBlock (GifContext *context,
424 /* unsigned char count; */
426 if (!gif_read (context, &context->block_count, 1)) {
427 /*g_message (_("GIF: error in getting DataBlock size\n"));*/
431 ZeroDataBlock = context->block_count == 0;
433 if ((context->block_count != 0) && (!gif_read (context, buf, context->block_count))) {
434 /*g_message (_("GIF: error in reading DataBlock\n"));*/
438 return context->block_count;
443 gif_set_lzw_fill_buffer (GifContext *context)
445 context->block_count = 0;
446 context->old_state = context->state;
447 context->state = GIF_LZW_FILL_BUFFER;
451 gif_lzw_fill_buffer (GifContext *context)
455 if (context->code_done) {
456 if (context->code_curbit >= context->code_lastbit) {
457 g_set_error (context->error,
459 GDK_PIXBUF_ERROR_CORRUPT_IMAGE,
460 _("GIF file was missing some data (perhaps it was truncated somehow?)"));
464 /* Is this supposed to be an error or what? */
465 /* g_message ("trying to read more data after we've done stuff\n"); */
466 g_set_error (context->error,
468 GDK_PIXBUF_ERROR_FAILED,
469 _("Internal error in the GIF loader (%s)"),
475 context->block_buf[0] = context->block_buf[context->code_last_byte - 2];
476 context->block_buf[1] = context->block_buf[context->code_last_byte - 1];
478 retval = get_data_block (context, &context->block_buf[2], NULL);
483 if (context->block_count == 0)
484 context->code_done = TRUE;
486 context->code_last_byte = 2 + context->block_count;
487 context->code_curbit = (context->code_curbit - context->code_lastbit) + 16;
488 context->code_lastbit = (2 + context->block_count) * 8;
490 context->state = context->old_state;
495 get_code (GifContext *context,
500 if ((context->code_curbit + code_size) >= context->code_lastbit){
501 gif_set_lzw_fill_buffer (context);
506 for (i = context->code_curbit, j = 0; j < code_size; ++i, ++j)
507 ret |= ((context->block_buf[i / 8] & (1 << (i % 8))) != 0) << j;
509 context->code_curbit += code_size;
516 set_gif_lzw_clear_code (GifContext *context)
518 context->state = GIF_LZW_CLEAR_CODE;
519 context->lzw_code_pending = -1;
523 gif_lzw_clear_code (GifContext *context)
527 code = get_code (context, context->lzw_code_size);
531 context->lzw_firstcode = context->lzw_oldcode = code;
532 context->lzw_code_pending = code;
533 context->state = GIF_GET_LZW;
538 lzw_read_byte (GifContext *context)
545 if (context->lzw_code_pending != -1) {
546 retval = context->lzw_code_pending;
547 context->lzw_code_pending = -1;
551 if (context->lzw_fresh) {
552 context->lzw_fresh = FALSE;
554 retval = get_code (context, context->lzw_code_size);
559 context->lzw_firstcode = context->lzw_oldcode = retval;
560 } while (context->lzw_firstcode == context->lzw_clear_code);
561 return context->lzw_firstcode;
564 if (context->lzw_sp > context->lzw_stack) {
565 my_retval = *--(context->lzw_sp);
569 while ((code = get_code (context, context->lzw_code_size)) >= 0) {
570 if (code == context->lzw_clear_code) {
571 for (i = 0; i < context->lzw_clear_code; ++i) {
572 context->lzw_table[0][i] = 0;
573 context->lzw_table[1][i] = i;
575 for (; i < (1 << MAX_LZW_BITS); ++i)
576 context->lzw_table[0][i] = context->lzw_table[1][i] = 0;
577 context->lzw_code_size = context->lzw_set_code_size + 1;
578 context->lzw_max_code_size = 2 * context->lzw_clear_code;
579 context->lzw_max_code = context->lzw_clear_code + 2;
580 context->lzw_sp = context->lzw_stack;
582 set_gif_lzw_clear_code (context);
584 } else if (code == context->lzw_end_code) {
586 unsigned char buf[260];
588 /* FIXME - we should handle this case */
589 g_set_error (context->error,
591 GDK_PIXBUF_ERROR_FAILED,
592 _("GIF image loader can't understand this image."));
599 while ((count = GetDataBlock (context, buf)) > 0)
603 /*g_print (_("GIF: missing EOD in data stream (common occurence)"));*/
610 if (code >= context->lzw_max_code) {
611 *(context->lzw_sp)++ = context->lzw_firstcode;
612 code = context->lzw_oldcode;
615 while (code >= context->lzw_clear_code) {
616 if ((code >= (1 << MAX_LZW_BITS))
617 || (context->lzw_sp >= context->lzw_stack + ((1 << (MAX_LZW_BITS)) * 2 + 1))) {
618 g_set_error (context->error,
620 GDK_PIXBUF_ERROR_CORRUPT_IMAGE,
621 _("Bad code encountered"));
624 *(context->lzw_sp)++ = context->lzw_table[1][code];
626 if (code == context->lzw_table[0][code]) {
627 g_set_error (context->error,
629 GDK_PIXBUF_ERROR_CORRUPT_IMAGE,
630 _("Circular table entry in GIF file"));
633 code = context->lzw_table[0][code];
636 *(context->lzw_sp)++ = context->lzw_firstcode = context->lzw_table[1][code];
638 if ((code = context->lzw_max_code) < (1 << MAX_LZW_BITS)) {
639 context->lzw_table[0][code] = context->lzw_oldcode;
640 context->lzw_table[1][code] = context->lzw_firstcode;
641 ++context->lzw_max_code;
642 if ((context->lzw_max_code >= context->lzw_max_code_size) &&
643 (context->lzw_max_code_size < (1 << MAX_LZW_BITS))) {
644 context->lzw_max_code_size *= 2;
645 ++context->lzw_code_size;
649 context->lzw_oldcode = incode;
651 if (context->lzw_sp > context->lzw_stack) {
652 my_retval = *--(context->lzw_sp);
660 gif_set_get_lzw (GifContext *context)
662 context->state = GIF_GET_LZW;
663 context->draw_xpos = 0;
664 context->draw_ypos = 0;
665 context->draw_pass = 0;
669 gif_fill_in_pixels (GifContext *context, guchar *dest, gint offset, guchar v)
671 guchar *pixel = NULL;
672 guchar (*cmap)[MAXCOLORMAPSIZE];
674 if (context->frame_cmap_active)
675 cmap = context->frame_color_map;
677 cmap = context->global_color_map;
679 if (context->gif89.transparent != -1) {
680 pixel = dest + (context->draw_ypos + offset) * gdk_pixbuf_get_rowstride (context->frame->pixbuf) + context->draw_xpos * 4;
681 *pixel = cmap [0][(guchar) v];
682 *(pixel+1) = cmap [1][(guchar) v];
683 *(pixel+2) = cmap [2][(guchar) v];
684 *(pixel+3) = (guchar) ((v == context->gif89.transparent) ? 0 : 255);
686 pixel = dest + (context->draw_ypos + offset) * gdk_pixbuf_get_rowstride (context->frame->pixbuf) + context->draw_xpos * 3;
687 *pixel = cmap [0][(guchar) v];
688 *(pixel+1) = cmap [1][(guchar) v];
689 *(pixel+2) = cmap [2][(guchar) v];
694 /* only called if progressive and interlaced */
696 gif_fill_in_lines (GifContext *context, guchar *dest, guchar v)
698 switch (context->draw_pass) {
700 if (context->draw_ypos > 4) {
701 gif_fill_in_pixels (context, dest, -4, v);
702 gif_fill_in_pixels (context, dest, -3, v);
704 if (context->draw_ypos < (context->frame_height - 4)) {
705 gif_fill_in_pixels (context, dest, 3, v);
706 gif_fill_in_pixels (context, dest, 4, v);
708 /* we don't need a break here. We draw the outer pixels first, then the
709 * inner ones, then the innermost ones. case 0 needs to draw all 3 bands.
710 * case 1, just the last two, and case 2 just draws the last one*/
712 if (context->draw_ypos > 2)
713 gif_fill_in_pixels (context, dest, -2, v);
714 if (context->draw_ypos < (context->frame_height - 2))
715 gif_fill_in_pixels (context, dest, 2, v);
716 /* no break as above. */
718 if (context->draw_ypos > 1)
719 gif_fill_in_pixels (context, dest, -1, v);
720 if (context->draw_ypos < (context->frame_height - 1))
721 gif_fill_in_pixels (context, dest, 1, v);
729 set_need_recomposite (gpointer data, gpointer user_data)
731 GdkPixbufFrame *frame = (GdkPixbufFrame *)data;
732 frame->need_recomposite = TRUE;
737 gif_get_lzw (GifContext *context)
740 gint lower_bound, upper_bound; /* bounds for emitting the area_updated signal */
742 gint first_pass; /* bounds for emitting the area_updated signal */
745 if (context->frame == NULL) {
746 context->frame = g_new (GdkPixbufFrame, 1);
748 context->frame->composited = NULL;
749 context->frame->revert = NULL;
751 context->frame->pixbuf =
752 gdk_pixbuf_new (GDK_COLORSPACE_RGB,
756 context->frame_height);
757 if (!context->frame->pixbuf) {
758 g_free (context->frame);
759 g_set_error (context->error,
761 GDK_PIXBUF_ERROR_INSUFFICIENT_MEMORY,
762 _("Not enough memory to load GIF file"));
766 context->frame->x_offset = context->x_offset;
767 context->frame->y_offset = context->y_offset;
768 context->frame->need_recomposite = TRUE;
770 /* GIF delay is in hundredths, we want thousandths */
771 context->frame->delay_time = context->gif89.delay_time * 10;
773 /* Some GIFs apparently have delay time of 0,
774 * that crashes everything so set it to "fast".
775 * Also, timeouts less than 20 or so just lock up
776 * the app or make the animation choppy, so fix them.
778 if (context->frame->delay_time < 20)
779 context->frame->delay_time = 20; /* 20 = "fast" */
781 context->frame->elapsed = context->animation->total_time;
782 context->animation->total_time += context->frame->delay_time;
784 switch (context->gif89.disposal) {
787 context->frame->action = GDK_PIXBUF_FRAME_RETAIN;
790 context->frame->action = GDK_PIXBUF_FRAME_DISPOSE;
793 context->frame->action = GDK_PIXBUF_FRAME_REVERT;
796 context->frame->action = GDK_PIXBUF_FRAME_RETAIN;
800 context->frame->bg_transparent = (context->gif89.transparent == context->background_index);
803 /* Update animation size */
806 context->animation->n_frames ++;
807 context->animation->frames = g_list_append (context->animation->frames, context->frame);
809 w = context->frame->x_offset +
810 gdk_pixbuf_get_width (context->frame->pixbuf);
811 h = context->frame->y_offset +
812 gdk_pixbuf_get_height (context->frame->pixbuf);
813 if (w > context->animation->width || h > context->animation->height) {
814 g_list_foreach (context->animation->frames, set_need_recomposite, NULL);
816 if (w > context->animation->width)
817 context->animation->width = w;
818 if (h > context->animation->height)
819 context->animation->height = h;
822 /* Only call prepare_func for the first frame */
823 if (context->animation->frames->next == NULL) {
824 if (context->prepare_func)
825 (* context->prepare_func) (context->frame->pixbuf,
826 GDK_PIXBUF_ANIMATION (context->animation),
829 /* Otherwise init frame with last frame */
831 GdkPixbufFrame *prev_frame;
833 link = g_list_find (context->animation->frames, context->frame);
835 prev_frame = link->prev->data;
837 gdk_pixbuf_gif_anim_frame_composite (context->animation, prev_frame);
839 gdk_pixbuf_copy_area (prev_frame->composited,
840 context->frame->x_offset,
841 context->frame->y_offset,
842 gdk_pixbuf_get_width (context->frame->pixbuf),
843 gdk_pixbuf_get_height (context->frame->pixbuf),
844 context->frame->pixbuf,
849 dest = gdk_pixbuf_get_pixels (context->frame->pixbuf);
852 lower_bound = upper_bound = context->draw_ypos;
853 first_pass = context->draw_pass;
856 guchar (*cmap)[MAXCOLORMAPSIZE];
858 if (context->frame_cmap_active)
859 cmap = context->frame_color_map;
861 cmap = context->global_color_map;
863 v = lzw_read_byte (context);
869 g_assert (gdk_pixbuf_get_has_alpha (context->frame->pixbuf));
871 temp = dest + context->draw_ypos * gdk_pixbuf_get_rowstride (context->frame->pixbuf) + context->draw_xpos * 4;
872 *temp = cmap [0][(guchar) v];
873 *(temp+1) = cmap [1][(guchar) v];
874 *(temp+2) = cmap [2][(guchar) v];
875 *(temp+3) = (guchar) ((v == context->gif89.transparent) ? 0 : 255);
877 if (context->prepare_func && context->frame_interlace)
878 gif_fill_in_lines (context, dest, v);
880 context->draw_xpos++;
882 if (context->draw_xpos == context->frame_len) {
883 context->draw_xpos = 0;
884 if (context->frame_interlace) {
885 switch (context->draw_pass) {
888 context->draw_ypos += 8;
891 context->draw_ypos += 4;
894 context->draw_ypos += 2;
898 if (context->draw_ypos >= context->frame_height) {
899 context->draw_pass++;
900 switch (context->draw_pass) {
902 context->draw_ypos = 4;
905 context->draw_ypos = 2;
908 context->draw_ypos = 1;
915 context->draw_ypos++;
917 if (context->draw_pass != first_pass) {
918 if (context->draw_ypos > lower_bound) {
920 upper_bound = context->frame_height;
925 upper_bound = context->draw_ypos;
927 if (context->draw_ypos >= context->frame_height)
933 context->state = GIF_GET_NEXT_STEP;
940 context->frame->need_recomposite = TRUE;
942 if (bound_flag && context->update_func) {
943 if (lower_bound <= upper_bound && first_pass == context->draw_pass) {
944 (* context->update_func)
945 (context->frame->pixbuf,
947 gdk_pixbuf_get_width (context->frame->pixbuf),
948 upper_bound - lower_bound,
951 if (lower_bound <= upper_bound) {
952 (* context->update_func)
953 (context->frame->pixbuf,
954 context->frame->x_offset,
955 context->frame->y_offset,
956 gdk_pixbuf_get_width (context->frame->pixbuf),
957 gdk_pixbuf_get_height (context->frame->pixbuf),
960 (* context->update_func)
961 (context->frame->pixbuf,
962 context->frame->x_offset,
963 context->frame->y_offset,
964 gdk_pixbuf_get_width (context->frame->pixbuf),
967 (* context->update_func)
968 (context->frame->pixbuf,
969 context->frame->x_offset,
970 lower_bound + context->frame->y_offset,
971 gdk_pixbuf_get_width (context->frame->pixbuf),
972 gdk_pixbuf_get_height (context->frame->pixbuf),
978 if (context->state == GIF_GET_NEXT_STEP) {
979 /* Will be freed with context->animation, we are just
980 * marking that we're done with it (no current frame)
982 context->frame = NULL;
983 context->frame_cmap_active = FALSE;
990 gif_set_prepare_lzw (GifContext *context)
992 context->state = GIF_PREPARE_LZW;
993 context->lzw_code_pending = -1;
996 gif_prepare_lzw (GifContext *context)
1000 if (!gif_read (context, &(context->lzw_set_code_size), 1)) {
1001 /*g_message (_("GIF: EOF / read error on image data\n"));*/
1005 if (context->lzw_set_code_size > MAX_LZW_BITS) {
1006 g_set_error (context->error,
1008 GDK_PIXBUF_ERROR_CORRUPT_IMAGE,
1009 _("GIF image is corrupt (incorrect LZW compression)"));
1013 context->lzw_code_size = context->lzw_set_code_size + 1;
1014 context->lzw_clear_code = 1 << context->lzw_set_code_size;
1015 context->lzw_end_code = context->lzw_clear_code + 1;
1016 context->lzw_max_code_size = 2 * context->lzw_clear_code;
1017 context->lzw_max_code = context->lzw_clear_code + 2;
1018 context->lzw_fresh = TRUE;
1019 context->code_curbit = 0;
1020 context->code_lastbit = 0;
1021 context->code_last_byte = 0;
1022 context->code_done = FALSE;
1024 g_assert (context->lzw_clear_code <=
1025 G_N_ELEMENTS (context->lzw_table[0]));
1027 for (i = 0; i < context->lzw_clear_code; ++i) {
1028 context->lzw_table[0][i] = 0;
1029 context->lzw_table[1][i] = i;
1031 for (; i < (1 << MAX_LZW_BITS); ++i)
1032 context->lzw_table[0][i] = context->lzw_table[1][0] = 0;
1034 context->lzw_sp = context->lzw_stack;
1035 gif_set_get_lzw (context);
1040 /* needs 13 bytes to proceed. */
1042 gif_init (GifContext *context)
1044 unsigned char buf[16];
1047 if (!gif_read (context, buf, 6)) {
1048 /* Unable to read magic number,
1049 * gif_read() should have set error
1054 if (strncmp ((char *) buf, "GIF", 3) != 0) {
1055 /* Not a GIF file */
1056 g_set_error (context->error,
1058 GDK_PIXBUF_ERROR_CORRUPT_IMAGE,
1059 _("File does not appear to be a GIF file"));
1063 strncpy (version, (char *) buf + 3, 3);
1066 if ((strcmp (version, "87a") != 0) && (strcmp (version, "89a") != 0)) {
1067 /* bad version number, not '87a' or '89a' */
1068 g_set_error (context->error,
1070 GDK_PIXBUF_ERROR_CORRUPT_IMAGE,
1071 _("Version %s of the GIF file format is not supported"),
1076 /* read the screen descriptor */
1077 if (!gif_read (context, buf, 7)) {
1078 /* Failed to read screen descriptor, error set */
1082 context->width = LM_to_uint (buf[0], buf[1]);
1083 context->height = LM_to_uint (buf[2], buf[3]);
1085 * high bit: whether to use the background index
1086 * next 3: color resolution
1087 * next: whether colormap is sorted by priority of allocation
1088 * last 3: size of colormap
1090 context->global_bit_pixel = 2 << (buf[4] & 0x07);
1091 context->global_color_resolution = (((buf[4] & 0x70) >> 3) + 1);
1092 context->has_global_cmap = (buf[4] & 0x80) != 0;
1093 context->background_index = buf[5];
1094 context->aspect_ratio = buf[6];
1096 /* Use background of transparent black as default, though if
1097 * one isn't set explicitly no one should ever use it.
1099 context->animation->bg_red = 0;
1100 context->animation->bg_green = 0;
1101 context->animation->bg_blue = 0;
1103 if (context->has_global_cmap) {
1104 gif_set_get_colormap (context);
1106 context->state = GIF_GET_NEXT_STEP;
1109 #ifdef DUMP_IMAGE_DETAILS
1110 g_print (">Image width: %d height: %d global_cmap: %d background: %d\n",
1111 context->width, context->height, context->has_global_cmap, context->background_index);
1118 gif_set_get_frame_info (GifContext *context)
1120 context->state = GIF_GET_FRAME_INFO;
1124 gif_get_frame_info (GifContext *context)
1126 unsigned char buf[9];
1128 if (!gif_read (context, buf, 9)) {
1132 /* Okay, we got all the info we need. Lets record it */
1133 context->frame_len = LM_to_uint (buf[4], buf[5]);
1134 context->frame_height = LM_to_uint (buf[6], buf[7]);
1135 context->x_offset = LM_to_uint (buf[0], buf[1]);
1136 context->y_offset = LM_to_uint (buf[2], buf[3]);
1138 if (((context->frame_height + context->y_offset) > context->height) ||
1139 ((context->frame_len + context->x_offset) > context->width)) {
1140 /* All frames must fit in the image bounds */
1141 context->state = GIF_DONE;
1143 g_set_error (context->error,
1145 GDK_PIXBUF_ERROR_CORRUPT_IMAGE,
1146 _("GIF image contained a frame appearing outside the image bounds."));
1151 if (context->animation->frames == NULL &&
1152 context->gif89.disposal == 3) {
1153 /* First frame can't have "revert to previous" as its
1157 context->state = GIF_DONE;
1159 g_set_error (context->error,
1161 GDK_PIXBUF_ERROR_CORRUPT_IMAGE,
1162 _("First frame of GIF image had 'revert to previous' as its disposal mode."));
1167 #ifdef DUMP_IMAGE_DETAILS
1168 g_print (">width: %d height: %d xoffset: %d yoffset: %d disposal: %d delay: %d transparent: %d\n",
1169 context->frame_len, context->frame_height, context->x_offset, context->y_offset,
1170 context->gif89.disposal, context->gif89.delay_time, context->gif89.transparent);
1173 context->frame_interlace = BitSet (buf[8], INTERLACE);
1174 if (BitSet (buf[8], LOCALCOLORMAP)) {
1176 #ifdef DUMP_IMAGE_DETAILS
1177 g_print (">has local colormap\n");
1180 /* Does this frame have it's own colormap. */
1181 /* really only relevant when looking at the first frame
1182 * of an animated gif. */
1183 /* if it does, we need to re-read in the colormap,
1184 * the gray_scale, and the bit_pixel */
1185 context->frame_cmap_active = TRUE;
1186 context->frame_bit_pixel = 1 << ((buf[8] & 0x07) + 1);
1187 gif_set_get_colormap2 (context);
1191 if (!context->has_global_cmap) {
1192 context->state = GIF_DONE;
1194 g_set_error (context->error,
1196 GDK_PIXBUF_ERROR_CORRUPT_IMAGE,
1197 _("GIF image has no global colormap, and a frame inside it has no local colormap."));
1202 gif_set_prepare_lzw (context);
1208 gif_get_next_step (GifContext *context)
1212 if (!gif_read (context, &c, 1)) {
1216 /* GIF terminator */
1217 /* hmm. Not 100% sure what to do about this. Should
1218 * i try to return a blank image instead? */
1219 context->state = GIF_DONE;
1224 /* Check the extention */
1225 gif_set_get_extension (context);
1229 /* look for frame */
1231 /* Not a valid start character */
1234 /* load the frame */
1235 gif_set_get_frame_info (context);
1241 #define LOG(x) /* g_print ("%d: %s\n", __LINE__, x); */
1244 gif_main_loop (GifContext *context)
1249 switch (context->state) {
1252 retval = gif_init (context);
1255 case GIF_GET_COLORMAP:
1256 LOG("get_colormap\n");
1257 retval = gif_get_colormap (context);
1259 context->state = GIF_GET_NEXT_STEP;
1262 case GIF_GET_NEXT_STEP:
1264 retval = gif_get_next_step (context);
1267 case GIF_GET_FRAME_INFO:
1268 LOG("frame_info\n");
1269 retval = gif_get_frame_info (context);
1272 case GIF_GET_EXTENTION:
1273 LOG("get_extension\n");
1274 retval = gif_get_extension (context);
1276 context->state = GIF_GET_NEXT_STEP;
1279 case GIF_GET_COLORMAP2:
1280 LOG("get_colormap2\n");
1281 retval = gif_get_colormap2 (context);
1283 gif_set_prepare_lzw (context);
1286 case GIF_PREPARE_LZW:
1287 LOG("prepare_lzw\n");
1288 retval = gif_prepare_lzw (context);
1291 case GIF_LZW_FILL_BUFFER:
1292 LOG("fill_buffer\n");
1293 retval = gif_lzw_fill_buffer (context);
1296 case GIF_LZW_CLEAR_CODE:
1297 LOG("clear_code\n");
1298 retval = gif_lzw_clear_code (context);
1303 retval = gif_get_lzw (context);
1312 } while ((retval == 0) || (retval == -3));
1320 GifContext *context;
1322 context = g_try_malloc (sizeof (GifContext));
1323 if (context == NULL)
1326 memset (context, 0, sizeof (GifContext));
1328 context->animation = g_object_new (GDK_TYPE_PIXBUF_GIF_ANIM, NULL);
1329 context->frame = NULL;
1330 context->file = NULL;
1331 context->state = GIF_START;
1332 context->prepare_func = NULL;
1333 context->update_func = NULL;
1334 context->user_data = NULL;
1335 context->buf = NULL;
1336 context->amount_needed = 0;
1337 context->gif89.transparent = -1;
1338 context->gif89.delay_time = -1;
1339 context->gif89.input_flag = -1;
1340 context->gif89.disposal = -1;
1344 /* Shared library entry point */
1346 gdk_pixbuf__gif_image_load (FILE *file, GError **error)
1348 GifContext *context;
1351 g_return_val_if_fail (file != NULL, NULL);
1353 context = new_context ();
1355 if (context == NULL) {
1358 GDK_PIXBUF_ERROR_INSUFFICIENT_MEMORY,
1359 _("Not enough memory to load GIF file"));
1363 context->file = file;
1364 context->error = error;
1366 if (gif_main_loop (context) == -1 || context->animation->frames == NULL) {
1367 if (context->error && *(context->error) == NULL)
1368 g_set_error (context->error,
1370 GDK_PIXBUF_ERROR_CORRUPT_IMAGE,
1371 _("GIF file was missing some data (perhaps it was truncated somehow?)"));
1374 pixbuf = gdk_pixbuf_animation_get_static_image (GDK_PIXBUF_ANIMATION (context->animation));
1377 g_object_ref (pixbuf);
1379 g_object_unref (context->animation);
1381 g_free (context->buf);
1388 gdk_pixbuf__gif_image_begin_load (ModulePreparedNotifyFunc prepare_func,
1389 ModuleUpdatedNotifyFunc update_func,
1393 GifContext *context;
1398 context = new_context ();
1400 if (context == NULL) {
1403 GDK_PIXBUF_ERROR_INSUFFICIENT_MEMORY,
1404 _("Not enough memory to load GIF file"));
1408 context->error = error;
1409 context->prepare_func = prepare_func;
1410 context->update_func = update_func;
1411 context->user_data = user_data;
1413 return (gpointer) context;
1417 gdk_pixbuf__gif_image_stop_load (gpointer data, GError **error)
1419 GifContext *context = (GifContext *) data;
1420 gboolean retval = TRUE;
1422 if (context->state != GIF_DONE) {
1425 GDK_PIXBUF_ERROR_CORRUPT_IMAGE,
1426 _("GIF image was truncated or incomplete."));
1431 g_object_unref (context->animation);
1433 g_free (context->buf);
1440 gdk_pixbuf__gif_image_load_increment (gpointer data,
1441 const guchar *buf, guint size,
1445 GifContext *context = (GifContext *) data;
1447 context->error = error;
1449 if (context->amount_needed == 0) {
1450 /* we aren't looking for some bytes. */
1451 /* we can use buf now, but we don't want to keep it around at all.
1452 * it will be gone by the end of the call. */
1453 context->buf = (guchar*) buf; /* very dubious const cast */
1455 context->size = size;
1457 /* we need some bytes */
1458 if (size < context->amount_needed) {
1459 context->amount_needed -= size;
1460 /* copy it over and return */
1461 memcpy (context->buf + context->size, buf, size);
1462 context->size += size;
1464 } else if (size == context->amount_needed) {
1465 memcpy (context->buf + context->size, buf, size);
1466 context->size += size;
1468 context->buf = g_realloc (context->buf, context->size + size);
1469 memcpy (context->buf + context->size, buf, size);
1470 context->size += size;
1474 retval = gif_main_loop (context);
1477 context->buf = NULL;
1481 /* we didn't have enough memory */
1482 /* prepare for the next image_load_increment */
1483 if (context->buf == buf) {
1484 g_assert (context->size == size);
1485 context->buf = (guchar *)g_new (guchar, context->amount_needed + (context->size - context->ptr));
1486 memcpy (context->buf, buf + context->ptr, context->size - context->ptr);
1488 /* copy the left overs to the begining of the buffer */
1489 /* and realloc the memory */
1490 memmove (context->buf, context->buf + context->ptr, context->size - context->ptr);
1491 context->buf = g_realloc (context->buf, context->amount_needed + (context->size - context->ptr));
1493 context->size = context->size - context->ptr;
1496 /* we are prolly all done */
1497 if (context->buf == buf)
1498 context->buf = NULL;
1503 static GdkPixbufAnimation *
1504 gdk_pixbuf__gif_image_load_animation (FILE *file,
1507 GifContext *context;
1508 GdkPixbufAnimation *animation;
1510 g_return_val_if_fail (file != NULL, NULL);
1512 context = new_context ();
1514 if (context == NULL) {
1517 GDK_PIXBUF_ERROR_INSUFFICIENT_MEMORY,
1518 _("Not enough memory to load GIF file"));
1522 context->error = error;
1523 context->file = file;
1525 if (gif_main_loop (context) == -1 || context->animation->frames == NULL) {
1526 if (context->error && *(context->error) == NULL)
1527 g_set_error (context->error,
1529 GDK_PIXBUF_ERROR_CORRUPT_IMAGE,
1530 _("GIF file was missing some data (perhaps it was truncated somehow?)"));
1532 g_object_unref (context->animation);
1533 context->animation = NULL;
1536 if (context->animation)
1537 animation = GDK_PIXBUF_ANIMATION (context->animation);
1541 if (context->error && *(context->error))
1542 g_print ("%s\n", (*(context->error))->message);
1544 g_free (context->buf);
1550 gdk_pixbuf__gif_fill_vtable (GdkPixbufModule *module)
1552 module->load = gdk_pixbuf__gif_image_load;
1553 module->begin_load = gdk_pixbuf__gif_image_begin_load;
1554 module->stop_load = gdk_pixbuf__gif_image_stop_load;
1555 module->load_increment = gdk_pixbuf__gif_image_load_increment;
1556 module->load_animation = gdk_pixbuf__gif_image_load_animation;