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;
121 gboolean frame_cmap_active;
122 CMap frame_color_map;
123 gint frame_colormap_size;
124 unsigned int frame_bit_pixel;
126 unsigned int aspect_ratio;
127 GdkPixbufGifAnim *animation;
128 GdkPixbufFrame *frame;
131 /* stuff per frame. */
138 /* Static read only */
141 /* progressive read, only. */
142 GdkPixbufModulePreparedFunc prepare_func;
143 GdkPixbufModuleUpdatedFunc update_func;
150 /* extension context */
151 guchar extension_label;
152 guchar extension_flag;
153 gboolean in_loop_extension;
155 /* get block context */
157 guchar block_buf[280];
160 int old_state; /* used by lzw_fill buffer */
161 /* get_code context */
166 int lzw_code_pending;
171 guchar lzw_set_code_size;
173 gint lzw_max_code_size;
180 gint lzw_table[2][(1 << MAX_LZW_BITS)];
181 gint lzw_stack[(1 << (MAX_LZW_BITS)) * 2 + 1];
183 /* painting context */
192 static int GetDataBlock (GifContext *, unsigned char *);
197 static int count = 0;
200 /* Returns TRUE if read is OK,
201 * FALSE if more memory is needed. */
203 gif_read (GifContext *context, guchar *buffer, size_t len)
212 g_print ("Fsize :%d\tcount :%d\t", len, count);
214 retval = (fread(buffer, len, 1, context->file) != 0);
216 if (!retval && ferror (context->file))
217 g_set_error (context->error,
219 g_file_error_from_errno (errno),
220 _("Failure reading GIF: %s"), strerror (errno));
224 for (i = 0; i < len; i++)
225 g_print ("%d ", buffer[i]);
233 /* g_print ("\tlooking for %d bytes. size == %d, ptr == %d\n", len, context->size, context->ptr); */
235 if ((context->size - context->ptr) >= len) {
239 memcpy (buffer, context->buf + context->ptr, len);
241 context->amount_needed = 0;
243 g_print ("Psize :%d\tcount :%d\t", len, count);
245 for (i = 0; i < len; i++)
246 g_print ("%d ", buffer[i]);
252 context->amount_needed = len - (context->size - context->ptr);
257 /* Changes the stage to be GIF_GET_COLORMAP */
259 gif_set_get_colormap (GifContext *context)
261 context->global_colormap_size = 0;
262 context->state = GIF_GET_COLORMAP;
266 gif_set_get_colormap2 (GifContext *context)
268 context->frame_colormap_size = 0;
269 context->state = GIF_GET_COLORMAP2;
273 gif_get_colormap (GifContext *context)
275 unsigned char rgb[3];
277 while (context->global_colormap_size < context->global_bit_pixel) {
278 if (!gif_read (context, rgb, sizeof (rgb))) {
282 context->global_color_map[0][context->global_colormap_size] = rgb[0];
283 context->global_color_map[1][context->global_colormap_size] = rgb[1];
284 context->global_color_map[2][context->global_colormap_size] = rgb[2];
286 if (context->global_colormap_size == context->background_index) {
287 context->animation->bg_red = rgb[0];
288 context->animation->bg_green = rgb[1];
289 context->animation->bg_blue = rgb[2];
292 context->global_colormap_size ++;
300 gif_get_colormap2 (GifContext *context)
302 unsigned char rgb[3];
304 while (context->frame_colormap_size < context->frame_bit_pixel) {
305 if (!gif_read (context, rgb, sizeof (rgb))) {
309 context->frame_color_map[0][context->frame_colormap_size] = rgb[0];
310 context->frame_color_map[1][context->frame_colormap_size] = rgb[1];
311 context->frame_color_map[2][context->frame_colormap_size] = rgb[2];
313 context->frame_colormap_size ++;
320 * in order for this function to work, we need to perform some black magic.
321 * We want to return -1 to let the calling function know, as before, that it needs
322 * more bytes. If we return 0, we were able to successfully read all block->count bytes.
323 * Problem is, we don't want to reread block_count every time, so we check to see if
324 * context->block_count is 0 before we read in the function.
326 * As a result, context->block_count MUST be 0 the first time the get_data_block is called
327 * within a context, and cannot be 0 the second time it's called.
331 get_data_block (GifContext *context,
336 if (context->block_count == 0) {
337 if (!gif_read (context, &context->block_count, 1)) {
342 if (context->block_count == 0)
348 if (!gif_read (context, buf, context->block_count)) {
356 gif_set_get_extension (GifContext *context)
358 context->state = GIF_GET_EXTENSION;
359 context->extension_flag = TRUE;
360 context->extension_label = 0;
361 context->block_count = 0;
362 context->block_ptr = 0;
366 gif_get_extension (GifContext *context)
369 gint empty_block = FALSE;
371 if (context->extension_flag) {
372 if (context->extension_label == 0) {
373 /* I guess bad things can happen if we have an extension of 0 )-: */
374 /* I should look into this sometime */
375 if (!gif_read (context, & context->extension_label , 1)) {
380 switch (context->extension_label) {
381 case 0xf9: /* Graphic Control Extension */
382 retval = get_data_block (context, (unsigned char *) context->block_buf, NULL);
386 if (context->frame == NULL) {
387 /* I only want to set the transparency if I haven't
388 * created the frame yet.
390 context->gif89.disposal = (context->block_buf[0] >> 2) & 0x7;
391 context->gif89.input_flag = (context->block_buf[0] >> 1) & 0x1;
392 context->gif89.delay_time = LM_to_uint (context->block_buf[1], context->block_buf[2]);
394 if ((context->block_buf[0] & 0x1) != 0) {
395 context->gif89.transparent = context->block_buf[3];
397 context->gif89.transparent = -1;
401 /* Now we've successfully loaded this one, we continue on our way */
402 context->block_count = 0;
403 context->extension_flag = FALSE;
405 case 0xff: /* application extension */
406 if (!context->in_loop_extension) {
407 retval = get_data_block (context, (unsigned char *) context->block_buf, NULL);
410 if (!strncmp (context->block_buf, "NETSCAPE2.0", 11) ||
411 !strncmp (context->block_buf, "ANIMEXTS1.0", 11)) {
412 context->in_loop_extension = TRUE;
414 context->block_count = 0;
416 if (context->in_loop_extension) {
418 retval = get_data_block (context, (unsigned char *) context->block_buf, &empty_block);
421 if (context->block_buf[0] == 0x01) {
422 context->animation->loop = context->block_buf[1] + (context->block_buf[2] << 8);
423 if (context->animation->loop != 0)
424 context->animation->loop++;
426 context->block_count = 0;
428 while (!empty_block);
429 context->in_loop_extension = FALSE;
430 context->extension_flag = FALSE;
435 /* Unhandled extension */
439 /* read all blocks, until I get an empty block, in case there was an extension I didn't know about. */
441 retval = get_data_block (context, (unsigned char *) context->block_buf, &empty_block);
444 context->block_count = 0;
445 } while (!empty_block);
450 static int ZeroDataBlock = FALSE;
453 GetDataBlock (GifContext *context,
456 /* unsigned char count; */
458 if (!gif_read (context, &context->block_count, 1)) {
459 /*g_message (_("GIF: error in getting DataBlock size\n"));*/
463 ZeroDataBlock = context->block_count == 0;
465 if ((context->block_count != 0) && (!gif_read (context, buf, context->block_count))) {
466 /*g_message (_("GIF: error in reading DataBlock\n"));*/
470 return context->block_count;
475 gif_set_lzw_fill_buffer (GifContext *context)
477 context->block_count = 0;
478 context->old_state = context->state;
479 context->state = GIF_LZW_FILL_BUFFER;
483 gif_lzw_fill_buffer (GifContext *context)
487 if (context->code_done) {
488 if (context->code_curbit >= context->code_lastbit) {
489 g_set_error (context->error,
491 GDK_PIXBUF_ERROR_CORRUPT_IMAGE,
492 _("GIF file was missing some data (perhaps it was truncated somehow?)"));
496 /* Is this supposed to be an error or what? */
497 /* g_message ("trying to read more data after we've done stuff\n"); */
498 g_set_error (context->error,
500 GDK_PIXBUF_ERROR_FAILED,
501 _("Internal error in the GIF loader (%s)"),
507 context->block_buf[0] = context->block_buf[context->code_last_byte - 2];
508 context->block_buf[1] = context->block_buf[context->code_last_byte - 1];
510 retval = get_data_block (context, &context->block_buf[2], NULL);
515 if (context->block_count == 0)
516 context->code_done = TRUE;
518 context->code_last_byte = 2 + context->block_count;
519 context->code_curbit = (context->code_curbit - context->code_lastbit) + 16;
520 context->code_lastbit = (2 + context->block_count) * 8;
522 context->state = context->old_state;
527 get_code (GifContext *context,
532 if ((context->code_curbit + code_size) >= context->code_lastbit){
533 gif_set_lzw_fill_buffer (context);
538 for (i = context->code_curbit, j = 0; j < code_size; ++i, ++j)
539 ret |= ((context->block_buf[i / 8] & (1 << (i % 8))) != 0) << j;
541 context->code_curbit += code_size;
548 set_gif_lzw_clear_code (GifContext *context)
550 context->state = GIF_LZW_CLEAR_CODE;
551 context->lzw_code_pending = -1;
555 gif_lzw_clear_code (GifContext *context)
559 code = get_code (context, context->lzw_code_size);
563 context->lzw_firstcode = context->lzw_oldcode = code;
564 context->lzw_code_pending = code;
565 context->state = GIF_GET_LZW;
569 #define CHECK_LZW_SP() G_STMT_START { \
570 if ((guchar *)context->lzw_sp >= \
571 (guchar *)context->lzw_stack + sizeof (context->lzw_stack)) { \
572 g_set_error (context->error, \
574 GDK_PIXBUF_ERROR_CORRUPT_IMAGE, \
575 _("Stack overflow")); \
581 lzw_read_byte (GifContext *context)
588 if (context->lzw_code_pending != -1) {
589 retval = context->lzw_code_pending;
590 context->lzw_code_pending = -1;
594 if (context->lzw_fresh) {
595 context->lzw_fresh = FALSE;
597 retval = get_code (context, context->lzw_code_size);
602 context->lzw_firstcode = context->lzw_oldcode = retval;
603 } while (context->lzw_firstcode == context->lzw_clear_code);
604 return context->lzw_firstcode;
607 if (context->lzw_sp > context->lzw_stack) {
608 my_retval = *--(context->lzw_sp);
612 while ((code = get_code (context, context->lzw_code_size)) >= 0) {
613 if (code == context->lzw_clear_code) {
614 for (i = 0; i < context->lzw_clear_code; ++i) {
615 context->lzw_table[0][i] = 0;
616 context->lzw_table[1][i] = i;
618 for (; i < (1 << MAX_LZW_BITS); ++i)
619 context->lzw_table[0][i] = context->lzw_table[1][i] = 0;
620 context->lzw_code_size = context->lzw_set_code_size + 1;
621 context->lzw_max_code_size = 2 * context->lzw_clear_code;
622 context->lzw_max_code = context->lzw_clear_code + 2;
623 context->lzw_sp = context->lzw_stack;
625 set_gif_lzw_clear_code (context);
627 } else if (code == context->lzw_end_code) {
629 unsigned char buf[260];
631 /* FIXME - we should handle this case */
632 g_set_error (context->error,
634 GDK_PIXBUF_ERROR_FAILED,
635 _("GIF image loader can't understand this image."));
642 while ((count = GetDataBlock (context, buf)) > 0)
646 /*g_print (_("GIF: missing EOD in data stream (common occurence)"));*/
653 if (code >= context->lzw_max_code) {
655 *(context->lzw_sp)++ = context->lzw_firstcode;
656 code = context->lzw_oldcode;
659 while (code >= context->lzw_clear_code) {
660 if (code >= (1 << MAX_LZW_BITS)) {
661 g_set_error (context->error,
663 GDK_PIXBUF_ERROR_CORRUPT_IMAGE,
664 _("Bad code encountered"));
668 *(context->lzw_sp)++ = context->lzw_table[1][code];
670 if (code == context->lzw_table[0][code]) {
671 g_set_error (context->error,
673 GDK_PIXBUF_ERROR_CORRUPT_IMAGE,
674 _("Circular table entry in GIF file"));
677 code = context->lzw_table[0][code];
681 *(context->lzw_sp)++ = context->lzw_firstcode = context->lzw_table[1][code];
683 if ((code = context->lzw_max_code) < (1 << MAX_LZW_BITS)) {
684 context->lzw_table[0][code] = context->lzw_oldcode;
685 context->lzw_table[1][code] = context->lzw_firstcode;
686 ++context->lzw_max_code;
687 if ((context->lzw_max_code >= context->lzw_max_code_size) &&
688 (context->lzw_max_code_size < (1 << MAX_LZW_BITS))) {
689 context->lzw_max_code_size *= 2;
690 ++context->lzw_code_size;
694 context->lzw_oldcode = incode;
696 if (context->lzw_sp > context->lzw_stack) {
697 my_retval = *--(context->lzw_sp);
705 gif_set_get_lzw (GifContext *context)
707 context->state = GIF_GET_LZW;
708 context->draw_xpos = 0;
709 context->draw_ypos = 0;
710 context->draw_pass = 0;
714 gif_fill_in_pixels (GifContext *context, guchar *dest, gint offset, guchar v)
716 guchar *pixel = NULL;
717 guchar (*cmap)[MAXCOLORMAPSIZE];
719 if (context->frame_cmap_active)
720 cmap = context->frame_color_map;
722 cmap = context->global_color_map;
724 if (context->gif89.transparent != -1) {
725 pixel = dest + (context->draw_ypos + offset) * gdk_pixbuf_get_rowstride (context->frame->pixbuf) + context->draw_xpos * 4;
726 *pixel = cmap [0][(guchar) v];
727 *(pixel+1) = cmap [1][(guchar) v];
728 *(pixel+2) = cmap [2][(guchar) v];
729 *(pixel+3) = (guchar) ((v == context->gif89.transparent) ? 0 : 255);
731 pixel = dest + (context->draw_ypos + offset) * gdk_pixbuf_get_rowstride (context->frame->pixbuf) + context->draw_xpos * 3;
732 *pixel = cmap [0][(guchar) v];
733 *(pixel+1) = cmap [1][(guchar) v];
734 *(pixel+2) = cmap [2][(guchar) v];
739 /* only called if progressive and interlaced */
741 gif_fill_in_lines (GifContext *context, guchar *dest, guchar v)
743 switch (context->draw_pass) {
745 if (context->draw_ypos > 4) {
746 gif_fill_in_pixels (context, dest, -4, v);
747 gif_fill_in_pixels (context, dest, -3, v);
749 if (context->draw_ypos < (context->frame_height - 4)) {
750 gif_fill_in_pixels (context, dest, 3, v);
751 gif_fill_in_pixels (context, dest, 4, v);
753 /* we don't need a break here. We draw the outer pixels first, then the
754 * inner ones, then the innermost ones. case 0 needs to draw all 3 bands.
755 * case 1, just the last two, and case 2 just draws the last one*/
757 if (context->draw_ypos > 2)
758 gif_fill_in_pixels (context, dest, -2, v);
759 if (context->draw_ypos < (context->frame_height - 2))
760 gif_fill_in_pixels (context, dest, 2, v);
761 /* no break as above. */
763 if (context->draw_ypos > 1)
764 gif_fill_in_pixels (context, dest, -1, v);
765 if (context->draw_ypos < (context->frame_height - 1))
766 gif_fill_in_pixels (context, dest, 1, v);
774 set_need_recomposite (gpointer data, gpointer user_data)
776 GdkPixbufFrame *frame = (GdkPixbufFrame *)data;
777 frame->need_recomposite = TRUE;
780 /* Clips a rectancle to the base dimensions. Returns TRUE if the clipped rectangle is non-empty. */
782 clip_frame (GifContext *context,
794 *width = MIN (context->width, orig_x + *width) - *x;
795 *height = MIN (context->height, orig_y + *height) - *y;
797 if (*width > 0 && *height > 0)
800 /* The frame is completely off-bounds */
810 /* Call update_func on the given rectangle, unless it is completely off-bounds */
812 maybe_update (GifContext *context,
818 if (clip_frame (context, &x, &y, &width, &height))
819 (*context->update_func) (context->frame->pixbuf,
825 gif_get_lzw (GifContext *context)
828 gint lower_bound, upper_bound; /* bounds for emitting the area_updated signal */
830 gint first_pass; /* bounds for emitting the area_updated signal */
833 if (context->frame == NULL) {
834 context->frame = g_new (GdkPixbufFrame, 1);
836 context->frame->composited = NULL;
837 context->frame->revert = NULL;
839 if (context->frame_len == 0 || context->frame_height == 0) {
840 /* An empty frame, we just output a single transparent
843 context->x_offset = 0;
844 context->y_offset = 0;
845 context->frame_len = 1;
846 context->frame_height = 1;
847 context->frame->pixbuf = gdk_pixbuf_new (GDK_COLORSPACE_RGB, TRUE, 8, 1, 1);
848 if (context->frame->pixbuf) {
851 pixels = gdk_pixbuf_get_pixels (context->frame->pixbuf);
858 context->frame->pixbuf =
859 gdk_pixbuf_new (GDK_COLORSPACE_RGB,
863 context->frame_height);
864 if (!context->frame->pixbuf) {
865 g_free (context->frame);
866 g_set_error (context->error,
868 GDK_PIXBUF_ERROR_INSUFFICIENT_MEMORY,
869 _("Not enough memory to load GIF file"));
873 context->frame->x_offset = context->x_offset;
874 context->frame->y_offset = context->y_offset;
875 context->frame->need_recomposite = TRUE;
877 /* GIF delay is in hundredths, we want thousandths */
878 context->frame->delay_time = context->gif89.delay_time * 10;
880 /* Some GIFs apparently have delay time of 0,
881 * that crashes everything so set it to "fast".
882 * Also, timeouts less than 20 or so just lock up
883 * the app or make the animation choppy, so fix them.
885 if (context->frame->delay_time < 20)
886 context->frame->delay_time = 20; /* 20 = "fast" */
888 context->frame->elapsed = context->animation->total_time;
889 context->animation->total_time += context->frame->delay_time;
891 switch (context->gif89.disposal) {
894 context->frame->action = GDK_PIXBUF_FRAME_RETAIN;
897 context->frame->action = GDK_PIXBUF_FRAME_DISPOSE;
900 context->frame->action = GDK_PIXBUF_FRAME_REVERT;
903 context->frame->action = GDK_PIXBUF_FRAME_RETAIN;
907 context->frame->bg_transparent = (context->gif89.transparent == context->background_index);
909 context->animation->n_frames ++;
910 context->animation->frames = g_list_append (context->animation->frames, context->frame);
912 /* Only call prepare_func for the first frame */
913 if (context->animation->frames->next == NULL) {
914 if (context->prepare_func)
915 (* context->prepare_func) (context->frame->pixbuf,
916 GDK_PIXBUF_ANIMATION (context->animation),
919 /* Otherwise init frame with last frame */
921 GdkPixbufFrame *prev_frame;
924 link = g_list_find (context->animation->frames, context->frame);
926 prev_frame = link->prev->data;
928 gdk_pixbuf_gif_anim_frame_composite (context->animation, prev_frame);
930 x = context->frame->x_offset;
931 y = context->frame->y_offset;
932 w = gdk_pixbuf_get_width (context->frame->pixbuf);
933 h = gdk_pixbuf_get_height (context->frame->pixbuf);
934 if (clip_frame (context, &x, &y, &w, &h))
935 gdk_pixbuf_copy_area (prev_frame->composited,
937 context->frame->pixbuf,
942 dest = gdk_pixbuf_get_pixels (context->frame->pixbuf);
945 lower_bound = upper_bound = context->draw_ypos;
946 first_pass = context->draw_pass;
949 guchar (*cmap)[MAXCOLORMAPSIZE];
951 if (context->frame_cmap_active)
952 cmap = context->frame_color_map;
954 cmap = context->global_color_map;
956 v = lzw_read_byte (context);
962 g_assert (gdk_pixbuf_get_has_alpha (context->frame->pixbuf));
964 temp = dest + context->draw_ypos * gdk_pixbuf_get_rowstride (context->frame->pixbuf) + context->draw_xpos * 4;
965 *temp = cmap [0][(guchar) v];
966 *(temp+1) = cmap [1][(guchar) v];
967 *(temp+2) = cmap [2][(guchar) v];
968 *(temp+3) = (guchar) ((v == context->gif89.transparent) ? 0 : 255);
970 if (context->prepare_func && context->frame_interlace)
971 gif_fill_in_lines (context, dest, v);
973 context->draw_xpos++;
975 if (context->draw_xpos == context->frame_len) {
976 context->draw_xpos = 0;
977 if (context->frame_interlace) {
978 switch (context->draw_pass) {
981 context->draw_ypos += 8;
984 context->draw_ypos += 4;
987 context->draw_ypos += 2;
991 if (context->draw_ypos >= context->frame_height) {
992 context->draw_pass++;
993 switch (context->draw_pass) {
995 context->draw_ypos = 4;
998 context->draw_ypos = 2;
1001 context->draw_ypos = 1;
1008 context->draw_ypos++;
1010 if (context->draw_pass != first_pass) {
1011 if (context->draw_ypos > lower_bound) {
1013 upper_bound = context->frame_height;
1018 upper_bound = context->draw_ypos;
1020 if (context->draw_ypos >= context->frame_height)
1026 context->state = GIF_GET_NEXT_STEP;
1033 context->frame->need_recomposite = TRUE;
1035 if (bound_flag && context->update_func) {
1036 if (lower_bound <= upper_bound && first_pass == context->draw_pass) {
1037 maybe_update (context,
1038 context->frame->x_offset,
1039 context->frame->y_offset + lower_bound,
1040 gdk_pixbuf_get_width (context->frame->pixbuf),
1041 upper_bound - lower_bound);
1043 if (lower_bound <= upper_bound) {
1044 maybe_update (context,
1045 context->frame->x_offset,
1046 context->frame->y_offset,
1047 gdk_pixbuf_get_width (context->frame->pixbuf),
1048 gdk_pixbuf_get_height (context->frame->pixbuf));
1050 maybe_update (context,
1051 context->frame->x_offset,
1052 context->frame->y_offset,
1053 gdk_pixbuf_get_width (context->frame->pixbuf),
1055 maybe_update (context,
1056 context->frame->x_offset,
1057 context->frame->y_offset + lower_bound,
1058 gdk_pixbuf_get_width (context->frame->pixbuf),
1059 gdk_pixbuf_get_height (context->frame->pixbuf) - lower_bound);
1064 if (context->state == GIF_GET_NEXT_STEP) {
1065 /* Will be freed with context->animation, we are just
1066 * marking that we're done with it (no current frame)
1068 context->frame = NULL;
1069 context->frame_cmap_active = FALSE;
1076 gif_set_prepare_lzw (GifContext *context)
1078 context->state = GIF_PREPARE_LZW;
1079 context->lzw_code_pending = -1;
1082 gif_prepare_lzw (GifContext *context)
1086 if (!gif_read (context, &(context->lzw_set_code_size), 1)) {
1087 /*g_message (_("GIF: EOF / read error on image data\n"));*/
1091 if (context->lzw_set_code_size > MAX_LZW_BITS) {
1092 g_set_error (context->error,
1094 GDK_PIXBUF_ERROR_CORRUPT_IMAGE,
1095 _("GIF image is corrupt (incorrect LZW compression)"));
1099 context->lzw_code_size = context->lzw_set_code_size + 1;
1100 context->lzw_clear_code = 1 << context->lzw_set_code_size;
1101 context->lzw_end_code = context->lzw_clear_code + 1;
1102 context->lzw_max_code_size = 2 * context->lzw_clear_code;
1103 context->lzw_max_code = context->lzw_clear_code + 2;
1104 context->lzw_fresh = TRUE;
1105 context->code_curbit = 0;
1106 context->code_lastbit = 0;
1107 context->code_last_byte = 0;
1108 context->code_done = FALSE;
1110 g_assert (context->lzw_clear_code <=
1111 G_N_ELEMENTS (context->lzw_table[0]));
1113 for (i = 0; i < context->lzw_clear_code; ++i) {
1114 context->lzw_table[0][i] = 0;
1115 context->lzw_table[1][i] = i;
1117 for (; i < (1 << MAX_LZW_BITS); ++i)
1118 context->lzw_table[0][i] = context->lzw_table[1][0] = 0;
1120 context->lzw_sp = context->lzw_stack;
1121 gif_set_get_lzw (context);
1126 /* needs 13 bytes to proceed. */
1128 gif_init (GifContext *context)
1130 unsigned char buf[16];
1133 if (!gif_read (context, buf, 6)) {
1134 /* Unable to read magic number,
1135 * gif_read() should have set error
1140 if (strncmp ((char *) buf, "GIF", 3) != 0) {
1141 /* Not a GIF file */
1142 g_set_error (context->error,
1144 GDK_PIXBUF_ERROR_CORRUPT_IMAGE,
1145 _("File does not appear to be a GIF file"));
1149 strncpy (version, (char *) buf + 3, 3);
1152 if ((strcmp (version, "87a") != 0) && (strcmp (version, "89a") != 0)) {
1153 /* bad version number, not '87a' or '89a' */
1154 g_set_error (context->error,
1156 GDK_PIXBUF_ERROR_CORRUPT_IMAGE,
1157 _("Version %s of the GIF file format is not supported"),
1162 /* read the screen descriptor */
1163 if (!gif_read (context, buf, 7)) {
1164 /* Failed to read screen descriptor, error set */
1168 context->width = LM_to_uint (buf[0], buf[1]);
1169 context->height = LM_to_uint (buf[2], buf[3]);
1171 * high bit: whether to use the background index
1172 * next 3: color resolution
1173 * next: whether colormap is sorted by priority of allocation
1174 * last 3: size of colormap
1176 context->global_bit_pixel = 2 << (buf[4] & 0x07);
1177 context->global_color_resolution = (((buf[4] & 0x70) >> 3) + 1);
1178 context->has_global_cmap = (buf[4] & 0x80) != 0;
1179 context->background_index = buf[5];
1180 context->aspect_ratio = buf[6];
1182 /* Use background of transparent black as default, though if
1183 * one isn't set explicitly no one should ever use it.
1185 context->animation->bg_red = 0;
1186 context->animation->bg_green = 0;
1187 context->animation->bg_blue = 0;
1189 context->animation->width = context->width;
1190 context->animation->height = context->height;
1192 if (context->has_global_cmap) {
1193 gif_set_get_colormap (context);
1195 context->state = GIF_GET_NEXT_STEP;
1198 #ifdef DUMP_IMAGE_DETAILS
1199 g_print (">Image width: %d height: %d global_cmap: %d background: %d\n",
1200 context->width, context->height, context->has_global_cmap, context->background_index);
1207 gif_set_get_frame_info (GifContext *context)
1209 context->state = GIF_GET_FRAME_INFO;
1213 gif_get_frame_info (GifContext *context)
1215 unsigned char buf[9];
1217 if (!gif_read (context, buf, 9)) {
1221 /* Okay, we got all the info we need. Lets record it */
1222 context->frame_len = LM_to_uint (buf[4], buf[5]);
1223 context->frame_height = LM_to_uint (buf[6], buf[7]);
1224 context->x_offset = LM_to_uint (buf[0], buf[1]);
1225 context->y_offset = LM_to_uint (buf[2], buf[3]);
1227 if (context->animation->frames == NULL &&
1228 context->gif89.disposal == 3) {
1229 /* First frame can't have "revert to previous" as its
1230 * dispose mode. Silently use "retain" instead.
1232 context->gif89.disposal = 0;
1235 context->frame_interlace = BitSet (buf[8], INTERLACE);
1237 #ifdef DUMP_IMAGE_DETAILS
1238 g_print (">width: %d height: %d xoffset: %d yoffset: %d disposal: %d delay: %d transparent: %d interlace: %d\n",
1239 context->frame_len, context->frame_height, context->x_offset, context->y_offset,
1240 context->gif89.disposal, context->gif89.delay_time, context->gif89.transparent, context->frame_interlace);
1243 if (BitSet (buf[8], LOCALCOLORMAP)) {
1245 #ifdef DUMP_IMAGE_DETAILS
1246 g_print (">has local colormap\n");
1249 /* Does this frame have it's own colormap. */
1250 /* really only relevant when looking at the first frame
1251 * of an animated gif. */
1252 /* if it does, we need to re-read in the colormap,
1253 * the gray_scale, and the bit_pixel */
1254 context->frame_cmap_active = TRUE;
1255 context->frame_bit_pixel = 1 << ((buf[8] & 0x07) + 1);
1256 gif_set_get_colormap2 (context);
1260 if (!context->has_global_cmap) {
1261 context->state = GIF_DONE;
1263 g_set_error (context->error,
1265 GDK_PIXBUF_ERROR_CORRUPT_IMAGE,
1266 _("GIF image has no global colormap, and a frame inside it has no local colormap."));
1271 gif_set_prepare_lzw (context);
1277 gif_get_next_step (GifContext *context)
1281 if (!gif_read (context, &c, 1)) {
1285 /* GIF terminator */
1286 /* hmm. Not 100% sure what to do about this. Should
1287 * i try to return a blank image instead? */
1288 context->state = GIF_DONE;
1293 /* Check the extension */
1294 gif_set_get_extension (context);
1298 /* look for frame */
1300 /* Not a valid start character */
1303 /* load the frame */
1304 gif_set_get_frame_info (context);
1310 #define LOG(x) /* g_print ("%d: %s\n", __LINE__, x); */
1313 gif_main_loop (GifContext *context)
1318 switch (context->state) {
1321 retval = gif_init (context);
1324 case GIF_GET_COLORMAP:
1325 LOG("get_colormap\n");
1326 retval = gif_get_colormap (context);
1328 context->state = GIF_GET_NEXT_STEP;
1331 case GIF_GET_NEXT_STEP:
1333 retval = gif_get_next_step (context);
1336 case GIF_GET_FRAME_INFO:
1337 LOG("frame_info\n");
1338 retval = gif_get_frame_info (context);
1341 case GIF_GET_EXTENSION:
1342 LOG("get_extension\n");
1343 retval = gif_get_extension (context);
1345 context->state = GIF_GET_NEXT_STEP;
1348 case GIF_GET_COLORMAP2:
1349 LOG("get_colormap2\n");
1350 retval = gif_get_colormap2 (context);
1352 gif_set_prepare_lzw (context);
1355 case GIF_PREPARE_LZW:
1356 LOG("prepare_lzw\n");
1357 retval = gif_prepare_lzw (context);
1360 case GIF_LZW_FILL_BUFFER:
1361 LOG("fill_buffer\n");
1362 retval = gif_lzw_fill_buffer (context);
1365 case GIF_LZW_CLEAR_CODE:
1366 LOG("clear_code\n");
1367 retval = gif_lzw_clear_code (context);
1372 retval = gif_get_lzw (context);
1381 } while ((retval == 0) || (retval == -3));
1389 GifContext *context;
1391 context = g_try_malloc (sizeof (GifContext));
1392 if (context == NULL)
1395 memset (context, 0, sizeof (GifContext));
1397 context->animation = g_object_new (GDK_TYPE_PIXBUF_GIF_ANIM, NULL);
1398 context->frame = NULL;
1399 context->file = NULL;
1400 context->state = GIF_START;
1401 context->prepare_func = NULL;
1402 context->update_func = NULL;
1403 context->user_data = NULL;
1404 context->buf = NULL;
1405 context->amount_needed = 0;
1406 context->gif89.transparent = -1;
1407 context->gif89.delay_time = -1;
1408 context->gif89.input_flag = -1;
1409 context->gif89.disposal = -1;
1410 context->animation->loop = 1;
1411 context->in_loop_extension = FALSE;
1415 /* Shared library entry point */
1417 gdk_pixbuf__gif_image_load (FILE *file, GError **error)
1419 GifContext *context;
1422 g_return_val_if_fail (file != NULL, NULL);
1424 context = new_context ();
1426 if (context == NULL) {
1429 GDK_PIXBUF_ERROR_INSUFFICIENT_MEMORY,
1430 _("Not enough memory to load GIF file"));
1434 context->file = file;
1435 context->error = error;
1437 if (gif_main_loop (context) == -1 || context->animation->frames == NULL) {
1438 if (context->error && *(context->error) == NULL)
1439 g_set_error (context->error,
1441 GDK_PIXBUF_ERROR_CORRUPT_IMAGE,
1442 _("GIF file was missing some data (perhaps it was truncated somehow?)"));
1445 pixbuf = gdk_pixbuf_animation_get_static_image (GDK_PIXBUF_ANIMATION (context->animation));
1448 g_object_ref (pixbuf);
1450 g_object_unref (context->animation);
1452 g_free (context->buf);
1459 gdk_pixbuf__gif_image_begin_load (GdkPixbufModuleSizeFunc size_func,
1460 GdkPixbufModulePreparedFunc prepare_func,
1461 GdkPixbufModuleUpdatedFunc update_func,
1465 GifContext *context;
1470 context = new_context ();
1472 if (context == NULL) {
1475 GDK_PIXBUF_ERROR_INSUFFICIENT_MEMORY,
1476 _("Not enough memory to load GIF file"));
1480 context->error = error;
1481 context->prepare_func = prepare_func;
1482 context->update_func = update_func;
1483 context->user_data = user_data;
1485 return (gpointer) context;
1489 gdk_pixbuf__gif_image_stop_load (gpointer data, GError **error)
1491 GifContext *context = (GifContext *) data;
1492 gboolean retval = TRUE;
1494 if (context->state != GIF_DONE) {
1497 GDK_PIXBUF_ERROR_CORRUPT_IMAGE,
1498 _("GIF image was truncated or incomplete."));
1503 g_object_unref (context->animation);
1505 g_free (context->buf);
1512 gdk_pixbuf__gif_image_load_increment (gpointer data,
1513 const guchar *buf, guint size,
1517 GifContext *context = (GifContext *) data;
1519 context->error = error;
1521 if (context->amount_needed == 0) {
1522 /* we aren't looking for some bytes. */
1523 /* we can use buf now, but we don't want to keep it around at all.
1524 * it will be gone by the end of the call. */
1525 context->buf = (guchar*) buf; /* very dubious const cast */
1527 context->size = size;
1529 /* we need some bytes */
1530 if (size < context->amount_needed) {
1531 context->amount_needed -= size;
1532 /* copy it over and return */
1533 memcpy (context->buf + context->size, buf, size);
1534 context->size += size;
1536 } else if (size == context->amount_needed) {
1537 memcpy (context->buf + context->size, buf, size);
1538 context->size += size;
1540 context->buf = g_realloc (context->buf, context->size + size);
1541 memcpy (context->buf + context->size, buf, size);
1542 context->size += size;
1546 retval = gif_main_loop (context);
1549 if (context->buf == buf)
1550 context->buf = NULL;
1554 /* we didn't have enough memory */
1555 /* prepare for the next image_load_increment */
1556 if (context->buf == buf) {
1557 g_assert (context->size == size);
1558 context->buf = (guchar *)g_new (guchar, context->amount_needed + (context->size - context->ptr));
1559 memcpy (context->buf, buf + context->ptr, context->size - context->ptr);
1561 /* copy the left overs to the begining of the buffer */
1562 /* and realloc the memory */
1563 memmove (context->buf, context->buf + context->ptr, context->size - context->ptr);
1564 context->buf = g_realloc (context->buf, context->amount_needed + (context->size - context->ptr));
1566 context->size = context->size - context->ptr;
1569 /* we are prolly all done */
1570 if (context->buf == buf)
1571 context->buf = NULL;
1576 static GdkPixbufAnimation *
1577 gdk_pixbuf__gif_image_load_animation (FILE *file,
1580 GifContext *context;
1581 GdkPixbufAnimation *animation;
1583 g_return_val_if_fail (file != NULL, NULL);
1585 context = new_context ();
1587 if (context == NULL) {
1590 GDK_PIXBUF_ERROR_INSUFFICIENT_MEMORY,
1591 _("Not enough memory to load GIF file"));
1595 context->error = error;
1596 context->file = file;
1598 if (gif_main_loop (context) == -1 || context->animation->frames == NULL) {
1599 if (context->error && *(context->error) == NULL)
1600 g_set_error (context->error,
1602 GDK_PIXBUF_ERROR_CORRUPT_IMAGE,
1603 _("GIF file was missing some data (perhaps it was truncated somehow?)"));
1605 g_object_unref (context->animation);
1606 context->animation = NULL;
1609 if (context->animation)
1610 animation = GDK_PIXBUF_ANIMATION (context->animation);
1614 if (context->error && *(context->error))
1615 g_print ("%s\n", (*(context->error))->message);
1617 g_free (context->buf);
1623 MODULE_ENTRY (gif, fill_vtable) (GdkPixbufModule *module)
1625 module->load = gdk_pixbuf__gif_image_load;
1626 module->begin_load = gdk_pixbuf__gif_image_begin_load;
1627 module->stop_load = gdk_pixbuf__gif_image_stop_load;
1628 module->load_increment = gdk_pixbuf__gif_image_load_increment;
1629 module->load_animation = gdk_pixbuf__gif_image_load_animation;
1633 MODULE_ENTRY (gif, fill_info) (GdkPixbufFormat *info)
1635 static GdkPixbufModulePattern signature[] = {
1636 { "GIF8", NULL, 100 },
1639 static gchar * mime_types[] = {
1643 static gchar * extensions[] = {
1649 info->signature = signature;
1650 info->description = N_("The GIF image format");
1651 info->mime_types = mime_types;
1652 info->extensions = extensions;