Blame gdk-pixbuf/io-gif.c

Packit a4058c
/* -*- Mode: C; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 8 -*- */
Packit a4058c
/* GdkPixbuf library - GIF image loader
Packit a4058c
 *
Packit a4058c
 * Copyright (C) 1999 Mark Crichton
Packit a4058c
 * Copyright (C) 1999 The Free Software Foundation
Packit a4058c
 *
Packit a4058c
 * Authors: Jonathan Blandford <jrb@redhat.com>
Packit a4058c
 *          Adapted from the gimp gif filter written by Adam Moss <adam@gimp.org>
Packit a4058c
 *          Gimp work based on earlier work.
Packit a4058c
 *          Permission to relicense under the LGPL obtained.
Packit a4058c
 *
Packit a4058c
 * This library is free software; you can redistribute it and/or
Packit a4058c
 * modify it under the terms of the GNU Lesser General Public
Packit a4058c
 * License as published by the Free Software Foundation; either
Packit a4058c
 * version 2 of the License, or (at your option) any later version.
Packit a4058c
 *
Packit a4058c
 * This library is distributed in the hope that it will be useful,
Packit a4058c
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
Packit a4058c
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
Packit a4058c
 * Lesser General Public License for more details.
Packit a4058c
 *
Packit a4058c
 * You should have received a copy of the GNU Lesser General Public
Packit a4058c
 * License along with this library; if not, see <http://www.gnu.org/licenses/>.
Packit a4058c
 */
Packit a4058c
Packit a4058c
/* This loader is very hairy code.
Packit a4058c
 *
Packit a4058c
 * The main loop was not designed for incremental loading, so when it was hacked
Packit a4058c
 * in it got a bit messy.  Basically, every function is written to expect a failed
Packit a4058c
 * read_gif, and lets you call it again assuming that the bytes are there.
Packit a4058c
 *
Packit a4058c
 * Return vals.
Packit a4058c
 * Unless otherwise specified, these are the return vals for most functions:
Packit a4058c
 *
Packit a4058c
 *  0 -> success
Packit a4058c
 * -1 -> more bytes needed.
Packit a4058c
 * -2 -> failure; abort the load
Packit a4058c
 * -3 -> control needs to be passed back to the main loop
Packit a4058c
 *        \_ (most of the time returning 0 will get this, but not always)
Packit a4058c
 *
Packit a4058c
 * >1 -> for functions that get a guchar, the char will be returned.
Packit a4058c
 *
Packit a4058c
 * -jrb (11/03/1999)
Packit a4058c
 */
Packit a4058c
Packit a4058c
/*
Packit a4058c
 * If you have any images that crash this code, please, please let me know and
Packit a4058c
 * send them to me.
Packit a4058c
 *                                            <jrb@redhat.com>
Packit a4058c
 */
Packit a4058c
Packit a4058c

Packit a4058c
Packit a4058c
#include "config.h"
Packit a4058c
#include <stdio.h>
Packit a4058c
#include <string.h>
Packit a4058c
#include <errno.h>
Packit a4058c
#include "gdk-pixbuf-private.h"
Packit a4058c
#include "io-gif-animation.h"
Packit a4058c
Packit a4058c

Packit a4058c
Packit a4058c
#undef DUMP_IMAGE_DETAILS 
Packit a4058c
#undef IO_GIFDEBUG
Packit a4058c
Packit a4058c
#define MAXCOLORMAPSIZE  256
Packit a4058c
#define MAX_LZW_BITS     12
Packit a4058c
Packit a4058c
#define INTERLACE          0x40
Packit a4058c
#define LOCALCOLORMAP      0x80
Packit a4058c
#define BitSet(byte, bit)  (((byte) & (bit)) == (bit))
Packit a4058c
#define LM_to_uint(a,b)         (((b)<<8)|(a))
Packit a4058c
Packit a4058c

Packit a4058c
Packit a4058c
typedef unsigned char CMap[3][MAXCOLORMAPSIZE];
Packit a4058c
Packit a4058c
/* Possible states we can be in. */
Packit a4058c
enum {
Packit a4058c
	GIF_START,
Packit a4058c
	GIF_GET_COLORMAP,
Packit a4058c
	GIF_GET_NEXT_STEP,
Packit a4058c
	GIF_GET_FRAME_INFO,
Packit a4058c
	GIF_GET_EXTENSION,
Packit a4058c
	GIF_GET_COLORMAP2,
Packit a4058c
	GIF_PREPARE_LZW,
Packit a4058c
	GIF_LZW_FILL_BUFFER,
Packit a4058c
	GIF_LZW_CLEAR_CODE,
Packit a4058c
	GIF_GET_LZW,
Packit a4058c
	GIF_DONE
Packit a4058c
};
Packit a4058c
Packit a4058c
Packit a4058c
typedef struct _Gif89 Gif89;
Packit a4058c
struct _Gif89
Packit a4058c
{
Packit a4058c
	int transparent;
Packit a4058c
	int delay_time;
Packit a4058c
	int input_flag;
Packit a4058c
	int disposal;
Packit a4058c
};
Packit a4058c
Packit a4058c
typedef struct _GifContext GifContext;
Packit a4058c
struct _GifContext
Packit a4058c
{
Packit a4058c
	int state; /* really only relevant for progressive loading */
Packit a4058c
	unsigned int width;
Packit a4058c
	unsigned int height;
Packit a4058c
Packit a4058c
        gboolean has_global_cmap;
Packit a4058c
Packit a4058c
        CMap global_color_map;
Packit a4058c
        gint global_colormap_size;
Packit a4058c
        unsigned int global_bit_pixel;
Packit a4058c
	unsigned int global_color_resolution;
Packit a4058c
        unsigned int background_index;
Packit a4058c
        gboolean stop_after_first_frame;
Packit a4058c
Packit a4058c
        gboolean frame_cmap_active;
Packit a4058c
        CMap frame_color_map;
Packit a4058c
        gint frame_colormap_size;
Packit a4058c
        unsigned int frame_bit_pixel;
Packit a4058c
Packit a4058c
	unsigned int aspect_ratio;
Packit a4058c
	GdkPixbufGifAnim *animation;
Packit a4058c
	GdkPixbufFrame *frame;
Packit a4058c
	Gif89 gif89;
Packit a4058c
Packit a4058c
	/* stuff per frame. */
Packit a4058c
	int frame_len;
Packit a4058c
	int frame_height;
Packit a4058c
	int frame_interlace;
Packit a4058c
	int x_offset;
Packit a4058c
	int y_offset;
Packit a4058c
Packit a4058c
	/* Static read only */
Packit a4058c
	FILE *file;
Packit a4058c
Packit a4058c
	/* progressive read, only. */
Packit a4058c
	GdkPixbufModuleSizeFunc size_func;
Packit a4058c
	GdkPixbufModulePreparedFunc prepare_func;
Packit a4058c
	GdkPixbufModuleUpdatedFunc update_func;
Packit a4058c
	gpointer user_data;
Packit a4058c
        guchar *buf;
Packit a4058c
	gsize ptr;
Packit a4058c
	gsize size;
Packit a4058c
	gsize amount_needed;
Packit a4058c
Packit a4058c
	/* extension context */
Packit a4058c
	guchar extension_label;
Packit a4058c
	guchar extension_flag;
Packit a4058c
        gboolean in_loop_extension;
Packit a4058c
Packit a4058c
	/* get block context */
Packit a4058c
	guchar block_count;
Packit a4058c
	guchar block_buf[280];
Packit a4058c
	gint block_ptr;
Packit a4058c
Packit a4058c
	int old_state; /* used by lzw_fill buffer */
Packit a4058c
	/* get_code context */
Packit a4058c
	int code_curbit;
Packit a4058c
	int code_lastbit;
Packit a4058c
	int code_done;
Packit a4058c
	int code_last_byte;
Packit a4058c
	int lzw_code_pending;
Packit a4058c
Packit a4058c
	/* lzw context */
Packit a4058c
	gint lzw_fresh;
Packit a4058c
	gint lzw_code_size;
Packit a4058c
	guchar lzw_set_code_size;
Packit a4058c
	gint lzw_max_code;
Packit a4058c
	gint lzw_max_code_size;
Packit a4058c
	gint lzw_firstcode;
Packit a4058c
	gint lzw_oldcode;
Packit a4058c
	gint lzw_clear_code;
Packit a4058c
	gint lzw_end_code;
Packit a4058c
	gint *lzw_sp;
Packit a4058c
Packit a4058c
	gint lzw_table[2][(1 << MAX_LZW_BITS)];
Packit a4058c
	gint lzw_stack[(1 << (MAX_LZW_BITS)) * 2 + 1];
Packit a4058c
Packit a4058c
	/* painting context */
Packit a4058c
	gint draw_xpos;
Packit a4058c
	gint draw_ypos;
Packit a4058c
	gint draw_pass;
Packit a4058c
Packit a4058c
        /* error pointer */
Packit a4058c
        GError **error;
Packit a4058c
};
Packit a4058c
Packit a4058c
/* The buffer must be at least 255 bytes long. */
Packit a4058c
static int GetDataBlock (GifContext *, unsigned char *);
Packit a4058c
Packit a4058c

Packit a4058c
Packit a4058c
#ifdef IO_GIFDEBUG
Packit a4058c
static int count = 0;
Packit a4058c
#endif
Packit a4058c
Packit a4058c
/* Returns TRUE if read is OK,
Packit a4058c
 * FALSE if more memory is needed. */
Packit a4058c
static gboolean
Packit a4058c
gif_read (GifContext *context, guchar *buffer, size_t len)
Packit a4058c
{
Packit a4058c
	gboolean retval;
Packit a4058c
#ifdef IO_GIFDEBUG
Packit a4058c
	gint i;
Packit a4058c
#endif
Packit a4058c
	if (context->file) {
Packit a4058c
#ifdef IO_GIFDEBUG
Packit a4058c
		count += len;
Packit a4058c
		g_print ("Fsize :%d\tcount :%d\t", len, count);
Packit a4058c
#endif
Packit a4058c
		retval = (fread (buffer, 1, len, context->file) == len);
Packit a4058c
Packit a4058c
                if (!retval && ferror (context->file)) {
Packit a4058c
                        gint save_errno = errno;
Packit a4058c
                        g_set_error (context->error,
Packit a4058c
                                     G_FILE_ERROR,
Packit a4058c
                                     g_file_error_from_errno (save_errno),
Packit a4058c
                                     _("Failure reading GIF: %s"), 
Packit a4058c
                                     g_strerror (save_errno));
Packit a4058c
                }
Packit a4058c
                
Packit a4058c
#ifdef IO_GIFDEBUG
Packit a4058c
		if (len < 100) {
Packit a4058c
			for (i = 0; i < len; i++)
Packit a4058c
				g_print ("%d ", buffer[i]);
Packit a4058c
		}
Packit a4058c
		g_print ("\n");
Packit a4058c
#endif
Packit a4058c
                
Packit a4058c
		return retval;
Packit a4058c
	} else {
Packit a4058c
#ifdef IO_GIFDEBUG
Packit a4058c
/*  		g_print ("\tlooking for %d bytes.  size == %d, ptr == %d\n", len, context->size, context->ptr); */
Packit a4058c
#endif
Packit a4058c
		if ((context->size - context->ptr) >= len) {
Packit a4058c
#ifdef IO_GIFDEBUG
Packit a4058c
			count += len;
Packit a4058c
#endif
Packit a4058c
			memcpy (buffer, context->buf + context->ptr, len);
Packit a4058c
			context->ptr += len;
Packit a4058c
			context->amount_needed = 0;
Packit a4058c
#ifdef IO_GIFDEBUG
Packit a4058c
			g_print ("Psize :%d\tcount :%d\t", len, count);
Packit a4058c
			if (len < 100) {
Packit a4058c
				for (i = 0; i < len; i++)
Packit a4058c
					g_print ("%d ", buffer[i]);
Packit a4058c
			}
Packit a4058c
			g_print ("\n");
Packit a4058c
#endif
Packit a4058c
			return TRUE;
Packit a4058c
		}
Packit a4058c
		context->amount_needed = len - (context->size - context->ptr);
Packit a4058c
	}
Packit a4058c
	return FALSE;
Packit a4058c
}
Packit a4058c
Packit a4058c
/* Changes the stage to be GIF_GET_COLORMAP */
Packit a4058c
static void
Packit a4058c
gif_set_get_colormap (GifContext *context)
Packit a4058c
{
Packit a4058c
	context->global_colormap_size = 0;
Packit a4058c
	context->state = GIF_GET_COLORMAP;
Packit a4058c
}
Packit a4058c
Packit a4058c
static void
Packit a4058c
gif_set_get_colormap2 (GifContext *context)
Packit a4058c
{
Packit a4058c
	context->frame_colormap_size = 0;
Packit a4058c
	context->state = GIF_GET_COLORMAP2;
Packit a4058c
}
Packit a4058c
Packit a4058c
static gint
Packit a4058c
gif_get_colormap (GifContext *context)
Packit a4058c
{
Packit a4058c
	unsigned char rgb[3];
Packit a4058c
Packit a4058c
	while (context->global_colormap_size < context->global_bit_pixel) {
Packit a4058c
		if (!gif_read (context, rgb, sizeof (rgb))) {
Packit a4058c
			return -1;
Packit a4058c
		}
Packit a4058c
Packit a4058c
		context->global_color_map[0][context->global_colormap_size] = rgb[0];
Packit a4058c
		context->global_color_map[1][context->global_colormap_size] = rgb[1];
Packit a4058c
		context->global_color_map[2][context->global_colormap_size] = rgb[2];
Packit a4058c
Packit a4058c
                if (context->global_colormap_size == context->background_index) {
Packit a4058c
                        context->animation->bg_red = rgb[0];
Packit a4058c
                        context->animation->bg_green = rgb[1];
Packit a4058c
                        context->animation->bg_blue = rgb[2];
Packit a4058c
                }
Packit a4058c
Packit a4058c
		context->global_colormap_size ++;
Packit a4058c
	}
Packit a4058c
Packit a4058c
	return 0;
Packit a4058c
}
Packit a4058c
Packit a4058c
Packit a4058c
static gint
Packit a4058c
gif_get_colormap2 (GifContext *context)
Packit a4058c
{
Packit a4058c
	unsigned char rgb[3];
Packit a4058c
Packit a4058c
	while (context->frame_colormap_size < context->frame_bit_pixel) {
Packit a4058c
		if (!gif_read (context, rgb, sizeof (rgb))) {
Packit a4058c
			return -1;
Packit a4058c
		}
Packit a4058c
Packit a4058c
		context->frame_color_map[0][context->frame_colormap_size] = rgb[0];
Packit a4058c
		context->frame_color_map[1][context->frame_colormap_size] = rgb[1];
Packit a4058c
		context->frame_color_map[2][context->frame_colormap_size] = rgb[2];
Packit a4058c
Packit a4058c
		context->frame_colormap_size ++;
Packit a4058c
	}
Packit a4058c
Packit a4058c
	return 0;
Packit a4058c
}
Packit a4058c
Packit a4058c
/*
Packit a4058c
 * in order for this function to work, we need to perform some black magic.
Packit a4058c
 * We want to return -1 to let the calling function know, as before, that it needs
Packit a4058c
 * more bytes.  If we return 0, we were able to successfully read all block->count bytes.
Packit a4058c
 * Problem is, we don't want to reread block_count every time, so we check to see if
Packit a4058c
 * context->block_count is 0 before we read in the function.
Packit a4058c
 *
Packit a4058c
 * As a result, context->block_count MUST be 0 the first time the get_data_block is called
Packit a4058c
 * within a context, and cannot be 0 the second time it's called.
Packit a4058c
 */
Packit a4058c
Packit a4058c
static int
Packit a4058c
get_data_block (GifContext *context,
Packit a4058c
		unsigned char *buf,
Packit a4058c
		gint *empty_block)
Packit a4058c
{
Packit a4058c
Packit a4058c
	if (context->block_count == 0) {
Packit a4058c
		if (!gif_read (context, &context->block_count, 1)) {
Packit a4058c
			return -1;
Packit a4058c
		}
Packit a4058c
	}
Packit a4058c
Packit a4058c
	if (context->block_count == 0)
Packit a4058c
		if (empty_block) {
Packit a4058c
			*empty_block = TRUE;
Packit a4058c
			return 0;
Packit a4058c
		}
Packit a4058c
Packit a4058c
	if (!gif_read (context, buf, context->block_count)) {
Packit a4058c
		return -1;
Packit a4058c
	}
Packit a4058c
Packit a4058c
	return 0;
Packit a4058c
}
Packit a4058c
Packit a4058c
static void
Packit a4058c
gif_set_get_extension (GifContext *context)
Packit a4058c
{
Packit a4058c
	context->state = GIF_GET_EXTENSION;
Packit a4058c
	context->extension_flag = TRUE;
Packit a4058c
	context->extension_label = 0;
Packit a4058c
	context->block_count = 0;
Packit a4058c
	context->block_ptr = 0;
Packit a4058c
}
Packit a4058c
Packit a4058c
static int
Packit a4058c
gif_get_extension (GifContext *context)
Packit a4058c
{
Packit a4058c
	gint retval;
Packit a4058c
	gint empty_block = FALSE;
Packit a4058c
Packit a4058c
	if (context->extension_flag) {
Packit a4058c
		if (context->extension_label == 0) {
Packit a4058c
			/* I guess bad things can happen if we have an extension of 0 )-: */
Packit a4058c
			/* I should look into this sometime */
Packit a4058c
			if (!gif_read (context, & context->extension_label , 1)) {
Packit a4058c
				return -1;
Packit a4058c
			}
Packit a4058c
		}
Packit a4058c
Packit a4058c
		switch (context->extension_label) {
Packit a4058c
                case 0xf9:			/* Graphic Control Extension */
Packit a4058c
                        retval = get_data_block (context, (unsigned char *) context->block_buf, NULL);
Packit a4058c
			if (retval != 0)
Packit a4058c
				return retval;
Packit a4058c
Packit a4058c
			if (context->frame == NULL) {
Packit a4058c
				/* I only want to set the transparency if I haven't
Packit a4058c
				 * created the frame yet.
Packit a4058c
                                 */
Packit a4058c
				context->gif89.disposal = (context->block_buf[0] >> 2) & 0x7;
Packit a4058c
				context->gif89.input_flag = (context->block_buf[0] >> 1) & 0x1;
Packit a4058c
				context->gif89.delay_time = LM_to_uint (context->block_buf[1], context->block_buf[2]);
Packit a4058c
				
Packit a4058c
				if ((context->block_buf[0] & 0x1) != 0) {
Packit a4058c
					context->gif89.transparent = context->block_buf[3];
Packit a4058c
				} else {
Packit a4058c
					context->gif89.transparent = -1;
Packit a4058c
				}
Packit a4058c
			}
Packit a4058c
Packit a4058c
			/* Now we've successfully loaded this one, we continue on our way */
Packit a4058c
			context->block_count = 0;
Packit a4058c
			context->extension_flag = FALSE;
Packit a4058c
			break;
Packit a4058c
                case 0xff: /* application extension */
Packit a4058c
                        if (!context->in_loop_extension) { 
Packit a4058c
                                retval = get_data_block (context, (unsigned char *) context->block_buf, NULL);
Packit a4058c
                                if (retval != 0)
Packit a4058c
                                        return retval;
Packit a4058c
                                if (!strncmp ((gchar *)context->block_buf, "NETSCAPE2.0", 11) ||
Packit a4058c
                                    !strncmp ((gchar *)context->block_buf, "ANIMEXTS1.0", 11)) {
Packit a4058c
                                        context->in_loop_extension = TRUE;
Packit a4058c
                                }
Packit a4058c
                                context->block_count = 0;
Packit a4058c
                        }
Packit a4058c
                        if (context->in_loop_extension) {
Packit a4058c
                                do {
Packit a4058c
                                        retval = get_data_block (context, (unsigned char *) context->block_buf, &empty_block);
Packit a4058c
                                        if (retval != 0)
Packit a4058c
                                                return retval;
Packit a4058c
                                        if (context->block_buf[0] == 0x01) {
Packit a4058c
                                                context->animation->loop = context->block_buf[1] + (context->block_buf[2] << 8);
Packit a4058c
                                                if (context->animation->loop != 0) 
Packit a4058c
                                                        context->animation->loop++;
Packit a4058c
                                        }
Packit a4058c
                                        context->block_count = 0;
Packit a4058c
                                }
Packit a4058c
                                while (!empty_block);
Packit a4058c
                                context->in_loop_extension = FALSE;
Packit a4058c
                                context->extension_flag = FALSE;
Packit a4058c
                                return 0;
Packit a4058c
                        }
Packit a4058c
			break;                          
Packit a4058c
		default:
Packit a4058c
			/* Unhandled extension */
Packit a4058c
			break;
Packit a4058c
		}
Packit a4058c
	}
Packit a4058c
	/* read all blocks, until I get an empty block, in case there was an extension I didn't know about. */
Packit a4058c
	do {
Packit a4058c
		retval = get_data_block (context, (unsigned char *) context->block_buf, &empty_block);
Packit a4058c
		if (retval != 0)
Packit a4058c
			return retval;
Packit a4058c
		context->block_count = 0;
Packit a4058c
	} while (!empty_block);
Packit a4058c
Packit a4058c
	return 0;
Packit a4058c
}
Packit a4058c
Packit a4058c
static int ZeroDataBlock = FALSE;
Packit a4058c
Packit a4058c
/* @buf must be at least 255 bytes long. */
Packit a4058c
static int
Packit a4058c
GetDataBlock (GifContext *context,
Packit a4058c
	      unsigned char *buf)
Packit a4058c
{
Packit a4058c
/*  	unsigned char count; */
Packit a4058c
Packit a4058c
	if (!gif_read (context, &context->block_count, 1)) {
Packit a4058c
		/*g_message (_("GIF: error in getting DataBlock size\n"));*/
Packit a4058c
		return -1;
Packit a4058c
	}
Packit a4058c
Packit a4058c
	ZeroDataBlock = context->block_count == 0;
Packit a4058c
Packit a4058c
	if ((context->block_count != 0) && (!gif_read (context, buf, context->block_count))) {
Packit a4058c
		/*g_message (_("GIF: error in reading DataBlock\n"));*/
Packit a4058c
		return -1;
Packit a4058c
	}
Packit a4058c
Packit a4058c
	return context->block_count;
Packit a4058c
}
Packit a4058c
Packit a4058c
Packit a4058c
static void
Packit a4058c
gif_set_lzw_fill_buffer (GifContext *context)
Packit a4058c
{
Packit a4058c
	context->block_count = 0;
Packit a4058c
	context->old_state = context->state;
Packit a4058c
	context->state = GIF_LZW_FILL_BUFFER;
Packit a4058c
}
Packit a4058c
Packit a4058c
static int
Packit a4058c
gif_lzw_fill_buffer (GifContext *context)
Packit a4058c
{
Packit a4058c
	gint retval;
Packit a4058c
Packit a4058c
	if (context->code_done) {
Packit a4058c
		if (context->code_curbit >= context->code_lastbit) {
Packit a4058c
                        g_set_error_literal (context->error,
Packit a4058c
                                             GDK_PIXBUF_ERROR,
Packit a4058c
                                             GDK_PIXBUF_ERROR_CORRUPT_IMAGE,
Packit a4058c
                                             _("GIF file was missing some data (perhaps it was truncated somehow?)"));
Packit a4058c
Packit a4058c
			return -2;
Packit a4058c
		}
Packit a4058c
                /* Is this supposed to be an error or what? */
Packit a4058c
		/* g_message ("trying to read more data after we've done stuff\n"); */
Packit a4058c
                g_set_error (context->error,
Packit a4058c
                             GDK_PIXBUF_ERROR,
Packit a4058c
                             GDK_PIXBUF_ERROR_FAILED,
Packit a4058c
                             _("Internal error in the GIF loader (%s)"),
Packit a4058c
                             G_STRLOC);
Packit a4058c
                
Packit a4058c
		return -2;
Packit a4058c
	}
Packit a4058c
Packit a4058c
	context->block_buf[0] = context->block_buf[context->code_last_byte - 2];
Packit a4058c
	context->block_buf[1] = context->block_buf[context->code_last_byte - 1];
Packit a4058c
Packit a4058c
	retval = get_data_block (context, &context->block_buf[2], NULL);
Packit a4058c
Packit a4058c
	if (retval == -1)
Packit a4058c
		return -1;
Packit a4058c
Packit a4058c
	if (context->block_count == 0)
Packit a4058c
		context->code_done = TRUE;
Packit a4058c
Packit a4058c
	context->code_last_byte = 2 + context->block_count;
Packit a4058c
	context->code_curbit = (context->code_curbit - context->code_lastbit) + 16;
Packit a4058c
	context->code_lastbit = (2 + context->block_count) * 8;
Packit a4058c
Packit a4058c
	context->state = context->old_state;
Packit a4058c
	return 0;
Packit a4058c
}
Packit a4058c
Packit a4058c
static int
Packit a4058c
get_code (GifContext *context,
Packit a4058c
	  int   code_size)
Packit a4058c
{
Packit a4058c
	int i, j, ret;
Packit a4058c
Packit a4058c
	if ((context->code_curbit + code_size) >= context->code_lastbit){
Packit a4058c
		gif_set_lzw_fill_buffer (context);
Packit a4058c
		return -3;
Packit a4058c
	}
Packit a4058c
Packit a4058c
	ret = 0;
Packit a4058c
	for (i = context->code_curbit, j = 0; j < code_size; ++i, ++j)
Packit a4058c
		ret |= ((context->block_buf[i / 8] & (1 << (i % 8))) != 0) << j;
Packit a4058c
Packit a4058c
	context->code_curbit += code_size;
Packit a4058c
Packit a4058c
	return ret;
Packit a4058c
}
Packit a4058c
Packit a4058c
Packit a4058c
static void
Packit a4058c
set_gif_lzw_clear_code (GifContext *context)
Packit a4058c
{
Packit a4058c
	context->state = GIF_LZW_CLEAR_CODE;
Packit a4058c
	context->lzw_code_pending = -1;
Packit a4058c
}
Packit a4058c
Packit a4058c
static int
Packit a4058c
gif_lzw_clear_code (GifContext *context)
Packit a4058c
{
Packit a4058c
	gint code;
Packit a4058c
Packit a4058c
	code = get_code (context, context->lzw_code_size);
Packit a4058c
	if (code == -3)
Packit a4058c
		return -0;
Packit a4058c
Packit a4058c
	context->lzw_firstcode = context->lzw_oldcode = code;
Packit a4058c
	context->lzw_code_pending = code;
Packit a4058c
	context->state = GIF_GET_LZW;
Packit a4058c
	return 0;
Packit a4058c
}
Packit a4058c
Packit a4058c
#define CHECK_LZW_SP() G_STMT_START {                                           \
Packit a4058c
        if ((guchar *)context->lzw_sp >=                                        \
Packit a4058c
            (guchar *)context->lzw_stack + sizeof (context->lzw_stack)) {       \
Packit a4058c
                 g_set_error_literal (context->error,                           \
Packit a4058c
                                      GDK_PIXBUF_ERROR,                         \
Packit a4058c
                                      GDK_PIXBUF_ERROR_CORRUPT_IMAGE,           \
Packit a4058c
                                      _("Stack overflow"));                     \
Packit a4058c
                return -2;                                                      \
Packit a4058c
        }                                                                       \
Packit a4058c
} G_STMT_END
Packit a4058c
Packit a4058c
static int
Packit a4058c
lzw_read_byte (GifContext *context)
Packit a4058c
{
Packit a4058c
	int code, incode;
Packit a4058c
	gint retval;
Packit a4058c
	gint my_retval;
Packit a4058c
	register int i;
Packit a4058c
Packit a4058c
	if (context->lzw_code_pending != -1) {
Packit a4058c
		retval = context->lzw_code_pending;
Packit a4058c
		context->lzw_code_pending = -1;
Packit a4058c
		return retval;
Packit a4058c
	}
Packit a4058c
Packit a4058c
	if (context->lzw_fresh) {
Packit a4058c
		context->lzw_fresh = FALSE;
Packit a4058c
		do {
Packit a4058c
			retval = get_code (context, context->lzw_code_size);
Packit a4058c
			if (retval < 0) {
Packit a4058c
				return retval;
Packit a4058c
			}
Packit a4058c
Packit a4058c
			context->lzw_firstcode = context->lzw_oldcode = retval;
Packit a4058c
		} while (context->lzw_firstcode == context->lzw_clear_code);
Packit a4058c
		return context->lzw_firstcode;
Packit a4058c
	}
Packit a4058c
Packit a4058c
	if (context->lzw_sp > context->lzw_stack) {
Packit a4058c
		my_retval = *--(context->lzw_sp);
Packit a4058c
		return my_retval;
Packit a4058c
	}
Packit a4058c
Packit a4058c
	while ((code = get_code (context, context->lzw_code_size)) >= 0) {
Packit a4058c
		if (code == context->lzw_clear_code) {
Packit a4058c
			for (i = 0; i < context->lzw_clear_code; ++i) {
Packit a4058c
				context->lzw_table[0][i] = 0;
Packit a4058c
				context->lzw_table[1][i] = i;
Packit a4058c
			}
Packit a4058c
			for (; i < (1 << MAX_LZW_BITS); ++i)
Packit a4058c
				context->lzw_table[0][i] = context->lzw_table[1][i] = 0;
Packit a4058c
			context->lzw_code_size = context->lzw_set_code_size + 1;
Packit a4058c
			context->lzw_max_code_size = 2 * context->lzw_clear_code;
Packit a4058c
			context->lzw_max_code = context->lzw_clear_code + 2;
Packit a4058c
			context->lzw_sp = context->lzw_stack;
Packit a4058c
Packit a4058c
			set_gif_lzw_clear_code (context);
Packit a4058c
			return -3;
Packit a4058c
		} else if (code == context->lzw_end_code) {
Packit a4058c
			int count;
Packit a4058c
			unsigned char buf[260];
Packit a4058c
Packit a4058c
                        /*  FIXME - we should handle this case */
Packit a4058c
                        g_set_error_literal (context->error,
Packit a4058c
                                             GDK_PIXBUF_ERROR,
Packit a4058c
                                             GDK_PIXBUF_ERROR_FAILED,
Packit a4058c
                                             _("GIF image loader cannot understand this image."));
Packit a4058c
                        return -2;
Packit a4058c
                        
Packit a4058c
			if (ZeroDataBlock) {
Packit a4058c
				return -2;
Packit a4058c
			}
Packit a4058c
Packit a4058c
			while ((count = GetDataBlock (context, buf)) > 0)
Packit a4058c
				;
Packit a4058c
Packit a4058c
			if (count != 0) {
Packit a4058c
				/*g_print (_("GIF: missing EOD in data stream (common occurence)"));*/
Packit a4058c
				return -2;
Packit a4058c
			}
Packit a4058c
		}
Packit a4058c
Packit a4058c
		incode = code;
Packit a4058c
Packit a4058c
		if (code >= context->lzw_max_code) {
Packit a4058c
                        CHECK_LZW_SP ();
Packit a4058c
			*(context->lzw_sp)++ = context->lzw_firstcode;
Packit a4058c
			code = context->lzw_oldcode;
Packit a4058c
		}
Packit a4058c
Packit a4058c
		while (code >= context->lzw_clear_code) {
Packit a4058c
                        if (code >= (1 << MAX_LZW_BITS)) {
Packit a4058c
                                g_set_error_literal (context->error,
Packit a4058c
                                                     GDK_PIXBUF_ERROR,
Packit a4058c
                                                     GDK_PIXBUF_ERROR_CORRUPT_IMAGE,
Packit a4058c
                                                     _("Bad code encountered"));
Packit a4058c
				return -2;
Packit a4058c
                        }
Packit a4058c
                        CHECK_LZW_SP ();
Packit a4058c
			*(context->lzw_sp)++ = context->lzw_table[1][code];
Packit a4058c
Packit a4058c
			if (code == context->lzw_table[0][code]) {
Packit a4058c
                                g_set_error_literal (context->error,
Packit a4058c
                                                     GDK_PIXBUF_ERROR,
Packit a4058c
                                                     GDK_PIXBUF_ERROR_CORRUPT_IMAGE,
Packit a4058c
                                                     _("Circular table entry in GIF file"));
Packit a4058c
				return -2;
Packit a4058c
			}
Packit a4058c
			code = context->lzw_table[0][code];
Packit a4058c
		}
Packit a4058c
Packit a4058c
                CHECK_LZW_SP ();
Packit a4058c
		*(context->lzw_sp)++ = context->lzw_firstcode = context->lzw_table[1][code];
Packit a4058c
Packit a4058c
		if ((code = context->lzw_max_code) < (1 << MAX_LZW_BITS)) {
Packit a4058c
			context->lzw_table[0][code] = context->lzw_oldcode;
Packit a4058c
			context->lzw_table[1][code] = context->lzw_firstcode;
Packit a4058c
			++context->lzw_max_code;
Packit a4058c
			if ((context->lzw_max_code >= context->lzw_max_code_size) &&
Packit a4058c
			    (context->lzw_max_code_size < (1 << MAX_LZW_BITS))) {
Packit a4058c
				context->lzw_max_code_size *= 2;
Packit a4058c
				++context->lzw_code_size;
Packit a4058c
			}
Packit a4058c
		}
Packit a4058c
Packit a4058c
		context->lzw_oldcode = incode;
Packit a4058c
Packit a4058c
		if (context->lzw_sp > context->lzw_stack) {
Packit a4058c
			my_retval = *--(context->lzw_sp);
Packit a4058c
			return my_retval;
Packit a4058c
		}
Packit a4058c
	}
Packit a4058c
	return code;
Packit a4058c
}
Packit a4058c
Packit a4058c
static void
Packit a4058c
gif_set_get_lzw (GifContext *context)
Packit a4058c
{
Packit a4058c
	context->state = GIF_GET_LZW;
Packit a4058c
	context->draw_xpos = 0;
Packit a4058c
	context->draw_ypos = 0;
Packit a4058c
	context->draw_pass = 0;
Packit a4058c
}
Packit a4058c
Packit a4058c
static void
Packit a4058c
gif_fill_in_pixels (GifContext *context, guchar *dest, gint offset, guchar v)
Packit a4058c
{
Packit a4058c
	guchar *pixel = NULL;
Packit a4058c
        guchar (*cmap)[MAXCOLORMAPSIZE];
Packit a4058c
Packit a4058c
        if (context->frame_cmap_active)
Packit a4058c
                cmap = context->frame_color_map;
Packit a4058c
        else
Packit a4058c
                cmap = context->global_color_map;
Packit a4058c
        
Packit a4058c
	if (context->gif89.transparent != -1) {
Packit a4058c
		pixel = dest + (context->draw_ypos + offset) * gdk_pixbuf_get_rowstride (context->frame->pixbuf) + context->draw_xpos * 4;
Packit a4058c
		*pixel = cmap [0][(guchar) v];
Packit a4058c
		*(pixel+1) = cmap [1][(guchar) v];
Packit a4058c
		*(pixel+2) = cmap [2][(guchar) v];
Packit a4058c
		*(pixel+3) = (guchar) ((v == context->gif89.transparent) ? 0 : 255);
Packit a4058c
	} else {
Packit a4058c
		pixel = dest + (context->draw_ypos + offset) * gdk_pixbuf_get_rowstride (context->frame->pixbuf) + context->draw_xpos * 3;
Packit a4058c
		*pixel = cmap [0][(guchar) v];
Packit a4058c
		*(pixel+1) = cmap [1][(guchar) v];
Packit a4058c
		*(pixel+2) = cmap [2][(guchar) v];
Packit a4058c
	}
Packit a4058c
}
Packit a4058c
Packit a4058c
Packit a4058c
/* only called if progressive and interlaced */
Packit a4058c
static void
Packit a4058c
gif_fill_in_lines (GifContext *context, guchar *dest, guchar v)
Packit a4058c
{
Packit a4058c
	switch (context->draw_pass) {
Packit a4058c
	case 0:
Packit a4058c
		if (context->draw_ypos > 4) {
Packit a4058c
			gif_fill_in_pixels (context, dest, -4, v);
Packit a4058c
			gif_fill_in_pixels (context, dest, -3, v);
Packit a4058c
		}
Packit a4058c
		if (context->draw_ypos < (context->frame_height - 4)) {
Packit a4058c
			gif_fill_in_pixels (context, dest, 3, v);
Packit a4058c
			gif_fill_in_pixels (context, dest, 4, v);
Packit a4058c
		}
Packit a4058c
		/* we don't need a break here.  We draw the outer pixels first, then the
Packit a4058c
		 * inner ones, then the innermost ones.  case 0 needs to draw all 3 bands.
Packit a4058c
		 * case 1, just the last two, and case 2 just draws the last one*/
Packit a4058c
	case 1:
Packit a4058c
		if (context->draw_ypos > 2)
Packit a4058c
			gif_fill_in_pixels (context, dest, -2, v);
Packit a4058c
		if (context->draw_ypos < (context->frame_height - 2))
Packit a4058c
			gif_fill_in_pixels (context, dest, 2, v);
Packit a4058c
		/* no break as above. */
Packit a4058c
	case 2:
Packit a4058c
		if (context->draw_ypos > 1)
Packit a4058c
			gif_fill_in_pixels (context, dest, -1, v);
Packit a4058c
		if (context->draw_ypos < (context->frame_height - 1))
Packit a4058c
			gif_fill_in_pixels (context, dest, 1, v);
Packit a4058c
	case 3:
Packit a4058c
	default:
Packit a4058c
		break;
Packit a4058c
	}
Packit a4058c
}
Packit a4058c
Packit a4058c
/* Clips a rectancle to the base dimensions. Returns TRUE if the clipped rectangle is non-empty. */
Packit a4058c
static gboolean
Packit a4058c
clip_frame (GifContext *context, 
Packit a4058c
            gint       *x, 
Packit a4058c
            gint       *y, 
Packit a4058c
            gint       *width, 
Packit a4058c
            gint       *height)
Packit a4058c
{
Packit a4058c
        gint orig_x, orig_y;
Packit a4058c
        
Packit a4058c
        orig_x = *x;
Packit a4058c
        orig_y = *y;
Packit a4058c
	*x = MAX (0, *x);
Packit a4058c
	*y = MAX (0, *y);
Packit a4058c
	*width = MIN (context->width, orig_x + *width) - *x;
Packit a4058c
	*height = MIN (context->height, orig_y + *height) - *y;
Packit a4058c
Packit a4058c
	if (*width > 0 && *height > 0)
Packit a4058c
		return TRUE;
Packit a4058c
Packit a4058c
	/* The frame is completely off-bounds */
Packit a4058c
Packit a4058c
	*x = 0;
Packit a4058c
	*y = 0;
Packit a4058c
	*width = 0;
Packit a4058c
	*height = 0;
Packit a4058c
Packit a4058c
        return FALSE;
Packit a4058c
}
Packit a4058c
Packit a4058c
/* Call update_func on the given rectangle, unless it is completely off-bounds */
Packit a4058c
static void
Packit a4058c
maybe_update (GifContext *context,
Packit a4058c
              gint        x,
Packit a4058c
              gint        y,
Packit a4058c
              gint        width,
Packit a4058c
              gint        height)
Packit a4058c
{
Packit a4058c
        if (clip_frame (context, &x, &y, &width, &height))
Packit a4058c
                (*context->update_func) (context->frame->pixbuf, 
Packit a4058c
                                         x, y, width, height,
Packit a4058c
                                         context->user_data);
Packit a4058c
}
Packit a4058c
Packit a4058c
static int
Packit a4058c
gif_get_lzw (GifContext *context)
Packit a4058c
{
Packit a4058c
	guchar *dest, *temp;
Packit a4058c
	gint lower_bound, upper_bound; /* bounds for emitting the area_updated signal */
Packit a4058c
	gboolean bound_flag;
Packit a4058c
	gint first_pass; /* bounds for emitting the area_updated signal */
Packit a4058c
	gint v;
Packit a4058c
Packit a4058c
	if (context->frame == NULL) {
Packit a4058c
                context->frame = g_new (GdkPixbufFrame, 1);
Packit a4058c
Packit a4058c
                context->frame->composited = NULL;
Packit a4058c
                context->frame->revert = NULL;
Packit a4058c
                
Packit a4058c
                if (context->frame_len == 0 || context->frame_height == 0) {
Packit a4058c
                        /* An empty frame, we just output a single transparent
Packit a4058c
                         * pixel at (0, 0).
Packit a4058c
                         */
Packit a4058c
                        context->x_offset = 0;
Packit a4058c
                        context->y_offset = 0;
Packit a4058c
                        context->frame_len = 1;
Packit a4058c
                        context->frame_height = 1;
Packit a4058c
                        context->frame->pixbuf = gdk_pixbuf_new (GDK_COLORSPACE_RGB, TRUE, 8, 1, 1);
Packit a4058c
                        if (context->frame->pixbuf) {
Packit a4058c
                                guchar *pixels;
Packit a4058c
Packit a4058c
                                pixels = gdk_pixbuf_get_pixels (context->frame->pixbuf);
Packit a4058c
                                pixels[0] = 0;
Packit a4058c
                                pixels[1] = 0;
Packit a4058c
                                pixels[2] = 0;
Packit a4058c
                                pixels[3] = 0;
Packit a4058c
                        }
Packit a4058c
                } else {
Packit a4058c
                        int rowstride;
Packit a4058c
                        guint64 len;
Packit a4058c
Packit a4058c
                        rowstride = gdk_pixbuf_calculate_rowstride (GDK_COLORSPACE_RGB,
Packit a4058c
                                                                    TRUE,
Packit a4058c
                                                                    8,
Packit a4058c
                                                                    context->frame_len,
Packit a4058c
                                                                    context->frame_height);
Packit a4058c
                        if (rowstride > 0 &&
Packit a4058c
                            g_uint64_checked_mul (&len, rowstride, context->frame_height) &&
Packit a4058c
                            len <= G_MAXINT) {
Packit a4058c
                                context->frame->pixbuf =
Packit a4058c
                                        gdk_pixbuf_new (GDK_COLORSPACE_RGB,
Packit a4058c
                                                        TRUE,
Packit a4058c
                                                        8,
Packit a4058c
                                                        context->frame_len,
Packit a4058c
                                                        context->frame_height);
Packit a4058c
                        } else {
Packit a4058c
                                context->frame->pixbuf = NULL;
Packit a4058c
                        }
Packit a4058c
                }
Packit a4058c
Packit a4058c
                if (!context->frame->pixbuf) {
Packit a4058c
                        g_free (context->frame);
Packit a4058c
                        g_set_error_literal (context->error,
Packit a4058c
                                             GDK_PIXBUF_ERROR,
Packit a4058c
                                             GDK_PIXBUF_ERROR_INSUFFICIENT_MEMORY,
Packit a4058c
                                             _("Not enough memory to load GIF file"));
Packit a4058c
                        return -2;
Packit a4058c
                }
Packit a4058c
Packit a4058c
                context->frame->x_offset = context->x_offset;
Packit a4058c
                context->frame->y_offset = context->y_offset;
Packit a4058c
                context->frame->need_recomposite = TRUE;
Packit a4058c
                
Packit a4058c
                /* GIF delay is in hundredths, we want thousandths */
Packit a4058c
                context->frame->delay_time = context->gif89.delay_time * 10;
Packit a4058c
Packit a4058c
                /* GIFs with delay time 0 are mostly broken, but they
Packit a4058c
                 * just want a default, "not that fast" delay.
Packit a4058c
                 */
Packit a4058c
                if (context->frame->delay_time == 0)
Packit a4058c
                        context->frame->delay_time = 100;
Packit a4058c
Packit a4058c
                /* No GIFs gets to play faster than 50 fps. They just
Packit a4058c
                 * lock up poor gtk.
Packit a4058c
                 */
Packit a4058c
                if (context->frame->delay_time < 20)
Packit a4058c
                        context->frame->delay_time = 20; /* 20 = "fast" */
Packit a4058c
                
Packit a4058c
                context->frame->elapsed = context->animation->total_time;
Packit a4058c
                context->animation->total_time += context->frame->delay_time;                
Packit a4058c
                
Packit a4058c
                switch (context->gif89.disposal) {
Packit a4058c
                case 0:
Packit a4058c
                case 1:
Packit a4058c
                        context->frame->action = GDK_PIXBUF_FRAME_RETAIN;
Packit a4058c
                        break;
Packit a4058c
                case 2:
Packit a4058c
                        context->frame->action = GDK_PIXBUF_FRAME_DISPOSE;
Packit a4058c
                        break;
Packit a4058c
                case 3:
Packit a4058c
                        context->frame->action = GDK_PIXBUF_FRAME_REVERT;
Packit a4058c
                        break;
Packit a4058c
                default:
Packit a4058c
                        context->frame->action = GDK_PIXBUF_FRAME_RETAIN;
Packit a4058c
                        break;
Packit a4058c
                }
Packit a4058c
Packit a4058c
                context->frame->bg_transparent = (context->gif89.transparent == context->background_index);
Packit a4058c
                
Packit a4058c
                context->animation->n_frames ++;
Packit a4058c
                context->animation->frames = g_list_append (context->animation->frames, context->frame);
Packit a4058c
Packit a4058c
                /* Only call prepare_func for the first frame */
Packit a4058c
		if (context->animation->frames->next == NULL) { 
Packit a4058c
                        if (context->animation->width == 0 )
Packit a4058c
                                context->animation->width = gdk_pixbuf_get_width(context->frame->pixbuf);
Packit a4058c
                        if (context->animation->height == 0)
Packit a4058c
                                context->animation->height = gdk_pixbuf_get_height (context->frame->pixbuf);
Packit a4058c
Packit a4058c
                        if (context->prepare_func)
Packit a4058c
                                (* context->prepare_func) (context->frame->pixbuf,
Packit a4058c
                                                           GDK_PIXBUF_ANIMATION (context->animation),
Packit a4058c
                                                           context->user_data);
Packit a4058c
                } else {
Packit a4058c
                        /* Otherwise init frame with last frame */
Packit a4058c
                        GList *link;
Packit a4058c
                        GdkPixbufFrame *prev_frame;
Packit a4058c
                        gint x, y, w, h;
Packit a4058c
                        
Packit a4058c
                        link = g_list_find (context->animation->frames, context->frame);
Packit a4058c
Packit a4058c
                        prev_frame = link->prev->data;
Packit a4058c
Packit a4058c
                        gdk_pixbuf_gif_anim_frame_composite (context->animation, prev_frame);
Packit a4058c
Packit a4058c
                        /* Composite failed */
Packit a4058c
                        if (prev_frame->composited == NULL) {
Packit a4058c
                                GdkPixbufFrame *frame = NULL;
Packit a4058c
                                link = g_list_first (context->animation->frames);
Packit a4058c
                                while (link != NULL) {
Packit a4058c
                                        frame = (GdkPixbufFrame *)link->data;
Packit a4058c
                                        if (frame != NULL) {
Packit a4058c
                                                if (frame->pixbuf != NULL)
Packit a4058c
                                                        g_object_unref (frame->pixbuf);
Packit a4058c
                                                if (frame->composited != NULL)
Packit a4058c
                                                        g_object_unref (frame->composited);
Packit a4058c
                                                if (frame->revert != NULL)
Packit a4058c
                                                        g_object_unref (frame->revert);
Packit a4058c
                                                g_free (frame);
Packit a4058c
                                        }
Packit a4058c
                                        link = link->next;
Packit a4058c
                                }
Packit a4058c
                                
Packit a4058c
                                g_list_free (context->animation->frames);
Packit a4058c
                                context->animation->frames = NULL;
Packit a4058c
                                
Packit a4058c
                                g_set_error_literal (context->error,
Packit a4058c
                                                     GDK_PIXBUF_ERROR,
Packit a4058c
                                                     GDK_PIXBUF_ERROR_INSUFFICIENT_MEMORY,
Packit a4058c
                                                     _("Not enough memory to composite a frame in GIF file"));
Packit a4058c
                                return -2;
Packit a4058c
                        }
Packit a4058c
                    
Packit a4058c
                        x = context->frame->x_offset;
Packit a4058c
                        y = context->frame->y_offset;
Packit a4058c
                        w = gdk_pixbuf_get_width (context->frame->pixbuf);
Packit a4058c
                        h = gdk_pixbuf_get_height (context->frame->pixbuf);
Packit a4058c
                        if (clip_frame (context, &x, &y, &w, &h))
Packit a4058c
                                gdk_pixbuf_copy_area (prev_frame->composited,
Packit a4058c
                                                      x, y, w, h,
Packit a4058c
                                                      context->frame->pixbuf,
Packit a4058c
                                                      0, 0);
Packit a4058c
                }
Packit a4058c
        }
Packit a4058c
Packit a4058c
	dest = gdk_pixbuf_get_pixels (context->frame->pixbuf);
Packit a4058c
Packit a4058c
	bound_flag = FALSE;
Packit a4058c
	lower_bound = upper_bound = context->draw_ypos;
Packit a4058c
	first_pass = context->draw_pass;
Packit a4058c
Packit a4058c
	while (TRUE) {
Packit a4058c
                guchar (*cmap)[MAXCOLORMAPSIZE];
Packit a4058c
Packit a4058c
                if (context->frame_cmap_active)
Packit a4058c
                        cmap = context->frame_color_map;
Packit a4058c
                else
Packit a4058c
                        cmap = context->global_color_map;
Packit a4058c
                
Packit a4058c
		v = lzw_read_byte (context);
Packit a4058c
		if (v < 0) {
Packit a4058c
			goto finished_data;
Packit a4058c
		}
Packit a4058c
		bound_flag = TRUE;
Packit a4058c
Packit a4058c
                g_assert (gdk_pixbuf_get_has_alpha (context->frame->pixbuf));
Packit a4058c
                
Packit a4058c
                temp = dest + context->draw_ypos * gdk_pixbuf_get_rowstride (context->frame->pixbuf) + context->draw_xpos * 4;
Packit a4058c
                *temp = cmap [0][(guchar) v];
Packit a4058c
                *(temp+1) = cmap [1][(guchar) v];
Packit a4058c
                *(temp+2) = cmap [2][(guchar) v];
Packit a4058c
                *(temp+3) = (guchar) ((v == context->gif89.transparent) ? 0 : 255);
Packit a4058c
Packit a4058c
		if (context->prepare_func && context->frame_interlace)
Packit a4058c
			gif_fill_in_lines (context, dest, v);
Packit a4058c
Packit a4058c
		context->draw_xpos++;
Packit a4058c
                
Packit a4058c
		if (context->draw_xpos == context->frame_len) {
Packit a4058c
			context->draw_xpos = 0;
Packit a4058c
			if (context->frame_interlace) {
Packit a4058c
				switch (context->draw_pass) {
Packit a4058c
				case 0:
Packit a4058c
				case 1:
Packit a4058c
					context->draw_ypos += 8;
Packit a4058c
					break;
Packit a4058c
				case 2:
Packit a4058c
					context->draw_ypos += 4;
Packit a4058c
					break;
Packit a4058c
				case 3:
Packit a4058c
					context->draw_ypos += 2;
Packit a4058c
					break;
Packit a4058c
				}
Packit a4058c
Packit a4058c
				if (context->draw_ypos >= context->frame_height) {
Packit a4058c
					context->draw_pass++;
Packit a4058c
					switch (context->draw_pass) {
Packit a4058c
					case 1:
Packit a4058c
						context->draw_ypos = 4;
Packit a4058c
						break;
Packit a4058c
					case 2:
Packit a4058c
						context->draw_ypos = 2;
Packit a4058c
						break;
Packit a4058c
					case 3:
Packit a4058c
						context->draw_ypos = 1;
Packit a4058c
						break;
Packit a4058c
					default:
Packit a4058c
						goto done;
Packit a4058c
					}
Packit a4058c
				}
Packit a4058c
			} else {
Packit a4058c
				context->draw_ypos++;
Packit a4058c
			}
Packit a4058c
			if (context->draw_pass != first_pass) {
Packit a4058c
				if (context->draw_ypos > lower_bound) {
Packit a4058c
					lower_bound = 0;
Packit a4058c
					upper_bound = context->frame_height;
Packit a4058c
				} else {
Packit a4058c
                                        
Packit a4058c
				}
Packit a4058c
			} else
Packit a4058c
				upper_bound = context->draw_ypos;
Packit a4058c
		}
Packit a4058c
		if (context->draw_ypos >= context->frame_height)
Packit a4058c
			break;
Packit a4058c
	}
Packit a4058c
Packit a4058c
 done:
Packit a4058c
Packit a4058c
        context->state = GIF_GET_NEXT_STEP;
Packit a4058c
Packit a4058c
        v = 0;
Packit a4058c
Packit a4058c
 finished_data:
Packit a4058c
        
Packit a4058c
        if (bound_flag)
Packit a4058c
                context->frame->need_recomposite = TRUE;
Packit a4058c
        
Packit a4058c
	if (bound_flag && context->update_func) {
Packit a4058c
		if (lower_bound <= upper_bound && first_pass == context->draw_pass) {
Packit a4058c
                        maybe_update (context,
Packit a4058c
                                      context->frame->x_offset,
Packit a4058c
                                      context->frame->y_offset + lower_bound,
Packit a4058c
                                      gdk_pixbuf_get_width (context->frame->pixbuf),
Packit a4058c
                                      upper_bound - lower_bound + 1);
Packit a4058c
		} else {
Packit a4058c
			if (lower_bound <= upper_bound) {
Packit a4058c
                                maybe_update (context,
Packit a4058c
                                              context->frame->x_offset,
Packit a4058c
                                              context->frame->y_offset,
Packit a4058c
                                              gdk_pixbuf_get_width (context->frame->pixbuf),
Packit a4058c
                                              gdk_pixbuf_get_height (context->frame->pixbuf));
Packit a4058c
			} else {
Packit a4058c
                                maybe_update (context,
Packit a4058c
                                              context->frame->x_offset,
Packit a4058c
                                              context->frame->y_offset,
Packit a4058c
                                              gdk_pixbuf_get_width (context->frame->pixbuf),
Packit a4058c
                                              upper_bound);
Packit a4058c
                                maybe_update (context,
Packit a4058c
                                              context->frame->x_offset,
Packit a4058c
                                              context->frame->y_offset + lower_bound,
Packit a4058c
                                              gdk_pixbuf_get_width (context->frame->pixbuf),
Packit a4058c
                                              gdk_pixbuf_get_height (context->frame->pixbuf) - lower_bound);
Packit a4058c
			}
Packit a4058c
		}
Packit a4058c
	}
Packit a4058c
Packit a4058c
	if (context->state == GIF_GET_NEXT_STEP) {
Packit a4058c
                /* Will be freed with context->animation, we are just
Packit a4058c
                 * marking that we're done with it (no current frame)
Packit a4058c
                 */
Packit a4058c
		context->frame = NULL;
Packit a4058c
                context->frame_cmap_active = FALSE;
Packit a4058c
Packit a4058c
                if (context->stop_after_first_frame)
Packit a4058c
                        context->state =  GIF_DONE;
Packit a4058c
	}
Packit a4058c
	
Packit a4058c
	return v;
Packit a4058c
}
Packit a4058c
Packit a4058c
static void
Packit a4058c
gif_set_prepare_lzw (GifContext *context)
Packit a4058c
{
Packit a4058c
	context->state = GIF_PREPARE_LZW;
Packit a4058c
	context->lzw_code_pending = -1;
Packit a4058c
}
Packit a4058c
static int
Packit a4058c
gif_prepare_lzw (GifContext *context)
Packit a4058c
{
Packit a4058c
	gint i;
Packit a4058c
Packit a4058c
	if (!gif_read (context, &(context->lzw_set_code_size), 1)) {
Packit a4058c
		/*g_message (_("GIF: EOF / read error on image data\n"));*/
Packit a4058c
		return -1;
Packit a4058c
	}
Packit a4058c
        
Packit a4058c
        if (context->lzw_set_code_size > MAX_LZW_BITS) {
Packit a4058c
                g_set_error_literal (context->error,
Packit a4058c
                                     GDK_PIXBUF_ERROR,
Packit a4058c
                                     GDK_PIXBUF_ERROR_CORRUPT_IMAGE,
Packit a4058c
                                     _("GIF image is corrupt (incorrect LZW compression)"));
Packit a4058c
                return -2;
Packit a4058c
        }
Packit a4058c
Packit a4058c
	context->lzw_code_size = context->lzw_set_code_size + 1;
Packit a4058c
	context->lzw_clear_code = 1 << context->lzw_set_code_size;
Packit a4058c
	context->lzw_end_code = context->lzw_clear_code + 1;
Packit a4058c
	context->lzw_max_code_size = 2 * context->lzw_clear_code;
Packit a4058c
	context->lzw_max_code = context->lzw_clear_code + 2;
Packit a4058c
	context->lzw_fresh = TRUE;
Packit a4058c
	context->code_curbit = 0;
Packit a4058c
	context->code_lastbit = 0;
Packit a4058c
	context->code_last_byte = 0;
Packit a4058c
	context->code_done = FALSE;
Packit a4058c
Packit a4058c
        g_assert (context->lzw_clear_code <= 
Packit a4058c
                  G_N_ELEMENTS (context->lzw_table[0]));
Packit a4058c
Packit a4058c
	for (i = 0; i < context->lzw_clear_code; ++i) {
Packit a4058c
		context->lzw_table[0][i] = 0;
Packit a4058c
		context->lzw_table[1][i] = i;
Packit a4058c
	}
Packit a4058c
	for (; i < (1 << MAX_LZW_BITS); ++i)
Packit a4058c
		context->lzw_table[0][i] = context->lzw_table[1][0] = 0;
Packit a4058c
Packit a4058c
	context->lzw_sp = context->lzw_stack;
Packit a4058c
	gif_set_get_lzw (context);
Packit a4058c
Packit a4058c
	return 0;
Packit a4058c
}
Packit a4058c
Packit a4058c
/* needs 13 bytes to proceed. */
Packit a4058c
static gint
Packit a4058c
gif_init (GifContext *context)
Packit a4058c
{
Packit a4058c
	unsigned char buf[16];
Packit a4058c
	char version[4];
Packit a4058c
Packit a4058c
	if (!gif_read (context, buf, 6)) {
Packit a4058c
		/* Unable to read magic number,
Packit a4058c
                 * gif_read() should have set error
Packit a4058c
                 */
Packit a4058c
		return -1;
Packit a4058c
	}
Packit a4058c
Packit a4058c
	if (strncmp ((char *) buf, "GIF", 3) != 0) {
Packit a4058c
		/* Not a GIF file */
Packit a4058c
                g_set_error_literal (context->error,
Packit a4058c
                                     GDK_PIXBUF_ERROR,
Packit a4058c
                                     GDK_PIXBUF_ERROR_CORRUPT_IMAGE,
Packit a4058c
                                     _("File does not appear to be a GIF file"));
Packit a4058c
		return -2;
Packit a4058c
	}
Packit a4058c
Packit a4058c
	strncpy (version, (char *) buf + 3, 3);
Packit a4058c
	version[3] = '\0';
Packit a4058c
Packit a4058c
	if ((strcmp (version, "87a") != 0) && (strcmp (version, "89a") != 0)) {
Packit a4058c
		/* bad version number, not '87a' or '89a' */
Packit a4058c
                g_set_error (context->error,
Packit a4058c
                             GDK_PIXBUF_ERROR,
Packit a4058c
                             GDK_PIXBUF_ERROR_CORRUPT_IMAGE,
Packit a4058c
                             _("Version %s of the GIF file format is not supported"),
Packit a4058c
                             version);
Packit a4058c
		return -2;
Packit a4058c
	}
Packit a4058c
Packit a4058c
	/* read the screen descriptor */
Packit a4058c
	if (!gif_read (context, buf, 7)) {
Packit a4058c
		/* Failed to read screen descriptor, error set */
Packit a4058c
		return -1;
Packit a4058c
	}
Packit a4058c
Packit a4058c
	context->width = LM_to_uint (buf[0], buf[1]);
Packit a4058c
	context->height = LM_to_uint (buf[2], buf[3]);
Packit a4058c
        /* The 4th byte is
Packit a4058c
         * high bit: whether to use the background index
Packit a4058c
         * next 3:   color resolution
Packit a4058c
         * next:     whether colormap is sorted by priority of allocation
Packit a4058c
         * last 3:   size of colormap
Packit a4058c
         */
Packit a4058c
	context->global_bit_pixel = 2 << (buf[4] & 0x07);
Packit a4058c
	context->global_color_resolution = (((buf[4] & 0x70) >> 3) + 1);
Packit a4058c
        context->has_global_cmap = (buf[4] & 0x80) != 0;
Packit a4058c
	context->background_index = buf[5];
Packit a4058c
	context->aspect_ratio = buf[6];
Packit a4058c
Packit a4058c
        /* Use background of transparent black as default, though if
Packit a4058c
         * one isn't set explicitly no one should ever use it.
Packit a4058c
         */
Packit a4058c
        context->animation->bg_red = 0;
Packit a4058c
        context->animation->bg_green = 0;
Packit a4058c
        context->animation->bg_blue = 0;
Packit a4058c
Packit a4058c
        context->animation->width = context->width;
Packit a4058c
        context->animation->height = context->height;
Packit a4058c
Packit a4058c
        if (context->size_func) {
Packit a4058c
                gint width, height;
Packit a4058c
Packit a4058c
                width = context->width;
Packit a4058c
                height = context->height;
Packit a4058c
Packit a4058c
                (*context->size_func) (&width, &height, context->user_data);
Packit a4058c
Packit a4058c
                if (width == 0 || height == 0) {
Packit a4058c
                        g_set_error_literal (context->error,
Packit a4058c
                                             GDK_PIXBUF_ERROR,
Packit a4058c
                                             GDK_PIXBUF_ERROR_CORRUPT_IMAGE,
Packit a4058c
                                             _("Resulting GIF image has zero size"));
Packit a4058c
                        return -2;
Packit a4058c
                }
Packit a4058c
        }
Packit a4058c
Packit a4058c
	if (context->has_global_cmap) {
Packit a4058c
		gif_set_get_colormap (context);
Packit a4058c
	} else {
Packit a4058c
		context->state = GIF_GET_NEXT_STEP;
Packit a4058c
	}
Packit a4058c
Packit a4058c
#ifdef DUMP_IMAGE_DETAILS
Packit a4058c
        g_print (">Image width: %d height: %d global_cmap: %d background: %d\n",
Packit a4058c
                 context->width, context->height, context->has_global_cmap, context->background_index);
Packit a4058c
#endif
Packit a4058c
        
Packit a4058c
	return 0;
Packit a4058c
}
Packit a4058c
Packit a4058c
static void
Packit a4058c
gif_set_get_frame_info (GifContext *context)
Packit a4058c
{
Packit a4058c
	context->state = GIF_GET_FRAME_INFO;
Packit a4058c
}
Packit a4058c
Packit a4058c
static gint
Packit a4058c
gif_get_frame_info (GifContext *context)
Packit a4058c
{
Packit a4058c
	unsigned char buf[9];
Packit a4058c
        
Packit a4058c
	if (!gif_read (context, buf, 9)) {
Packit a4058c
		return -1;
Packit a4058c
	}
Packit a4058c
        
Packit a4058c
	/* Okay, we got all the info we need.  Lets record it */
Packit a4058c
	context->frame_len = LM_to_uint (buf[4], buf[5]);
Packit a4058c
	context->frame_height = LM_to_uint (buf[6], buf[7]);
Packit a4058c
	context->x_offset = LM_to_uint (buf[0], buf[1]);
Packit a4058c
	context->y_offset = LM_to_uint (buf[2], buf[3]);
Packit a4058c
Packit a4058c
	if (context->animation->frames == NULL &&
Packit a4058c
            context->gif89.disposal == 3) {
Packit a4058c
                /* First frame can't have "revert to previous" as its
Packit a4058c
                 * dispose mode. Silently use "retain" instead.
Packit a4058c
                 */
Packit a4058c
                context->gif89.disposal = 0;
Packit a4058c
	}
Packit a4058c
Packit a4058c
	context->frame_interlace = BitSet (buf[8], INTERLACE);
Packit a4058c
Packit a4058c
#ifdef DUMP_IMAGE_DETAILS
Packit a4058c
        g_print (">width: %d height: %d xoffset: %d yoffset: %d disposal: %d delay: %d transparent: %d interlace: %d\n",
Packit a4058c
                 context->frame_len, context->frame_height, context->x_offset, context->y_offset,
Packit a4058c
                 context->gif89.disposal, context->gif89.delay_time, context->gif89.transparent, context->frame_interlace);
Packit a4058c
#endif
Packit a4058c
        
Packit a4058c
	if (BitSet (buf[8], LOCALCOLORMAP)) {
Packit a4058c
Packit a4058c
#ifdef DUMP_IMAGE_DETAILS
Packit a4058c
                g_print (">has local colormap\n");
Packit a4058c
#endif
Packit a4058c
                
Packit a4058c
		/* Does this frame have it's own colormap. */
Packit a4058c
		/* really only relevant when looking at the first frame
Packit a4058c
		 * of an animated gif. */
Packit a4058c
		/* if it does, we need to re-read in the colormap,
Packit a4058c
		 * the gray_scale, and the bit_pixel */
Packit a4058c
                context->frame_cmap_active = TRUE;
Packit a4058c
		context->frame_bit_pixel = 1 << ((buf[8] & 0x07) + 1);
Packit a4058c
		gif_set_get_colormap2 (context);
Packit a4058c
		return 0;
Packit a4058c
	}
Packit a4058c
Packit a4058c
        if (!context->has_global_cmap) {
Packit a4058c
                context->state = GIF_DONE;
Packit a4058c
                
Packit a4058c
                g_set_error_literal (context->error,
Packit a4058c
                                     GDK_PIXBUF_ERROR,
Packit a4058c
                                     GDK_PIXBUF_ERROR_CORRUPT_IMAGE,
Packit a4058c
                                     _("GIF image has no global colormap, and a frame inside it has no local colormap."));
Packit a4058c
                
Packit a4058c
		return -2;
Packit a4058c
        }
Packit a4058c
Packit a4058c
	gif_set_prepare_lzw (context);
Packit a4058c
	return 0;
Packit a4058c
Packit a4058c
}
Packit a4058c
Packit a4058c
static gint
Packit a4058c
gif_get_next_step (GifContext *context)
Packit a4058c
{
Packit a4058c
	unsigned char c;
Packit a4058c
	while (TRUE) {
Packit a4058c
		if (!gif_read (context, &c, 1)) {
Packit a4058c
			return -1;
Packit a4058c
		}
Packit a4058c
		if (c == ';') {
Packit a4058c
			/* GIF terminator */
Packit a4058c
			/* hmm.  Not 100% sure what to do about this.  Should
Packit a4058c
			 * i try to return a blank image instead? */
Packit a4058c
			context->state = GIF_DONE;
Packit a4058c
			return 0;
Packit a4058c
		}
Packit a4058c
Packit a4058c
		if (c == '!') {
Packit a4058c
			/* Check the extension */
Packit a4058c
			gif_set_get_extension (context);
Packit a4058c
			return 0;
Packit a4058c
		}
Packit a4058c
Packit a4058c
		/* look for frame */
Packit a4058c
		if (c != ',') {
Packit a4058c
			/* Not a valid start character */
Packit a4058c
			continue;
Packit a4058c
		}
Packit a4058c
		/* load the frame */
Packit a4058c
		gif_set_get_frame_info (context);
Packit a4058c
		return 0;
Packit a4058c
	}
Packit a4058c
}
Packit a4058c
Packit a4058c
Packit a4058c
#define LOG(x) /* g_print ("%s: %s\n", G_STRLOC, x); */
Packit a4058c
Packit a4058c
static gint
Packit a4058c
gif_main_loop (GifContext *context)
Packit a4058c
{
Packit a4058c
	gint retval = 0;
Packit a4058c
Packit a4058c
	do {
Packit a4058c
		switch (context->state) {
Packit a4058c
		case GIF_START:
Packit a4058c
                        LOG("start\n");
Packit a4058c
			retval = gif_init (context);
Packit a4058c
			break;
Packit a4058c
Packit a4058c
		case GIF_GET_COLORMAP:
Packit a4058c
                        LOG("get_colormap\n");
Packit a4058c
			retval = gif_get_colormap (context);
Packit a4058c
			if (retval == 0)
Packit a4058c
				context->state = GIF_GET_NEXT_STEP;
Packit a4058c
			break;
Packit a4058c
Packit a4058c
		case GIF_GET_NEXT_STEP:
Packit a4058c
                        LOG("next_step\n");
Packit a4058c
			retval = gif_get_next_step (context);
Packit a4058c
			break;
Packit a4058c
Packit a4058c
		case GIF_GET_FRAME_INFO:
Packit a4058c
                        LOG("frame_info\n");
Packit a4058c
			retval = gif_get_frame_info (context);
Packit a4058c
			break;
Packit a4058c
Packit a4058c
		case GIF_GET_EXTENSION:
Packit a4058c
                        LOG("get_extension\n");
Packit a4058c
			retval = gif_get_extension (context);
Packit a4058c
			if (retval == 0)
Packit a4058c
				context->state = GIF_GET_NEXT_STEP;
Packit a4058c
			break;
Packit a4058c
Packit a4058c
		case GIF_GET_COLORMAP2:
Packit a4058c
                        LOG("get_colormap2\n");
Packit a4058c
			retval = gif_get_colormap2 (context);
Packit a4058c
			if (retval == 0)
Packit a4058c
				gif_set_prepare_lzw (context);
Packit a4058c
			break;
Packit a4058c
Packit a4058c
		case GIF_PREPARE_LZW:
Packit a4058c
                        LOG("prepare_lzw\n");
Packit a4058c
			retval = gif_prepare_lzw (context);
Packit a4058c
			break;
Packit a4058c
Packit a4058c
		case GIF_LZW_FILL_BUFFER:
Packit a4058c
                        LOG("fill_buffer\n");
Packit a4058c
			retval = gif_lzw_fill_buffer (context);
Packit a4058c
			break;
Packit a4058c
Packit a4058c
		case GIF_LZW_CLEAR_CODE:
Packit a4058c
                        LOG("clear_code\n");
Packit a4058c
			retval = gif_lzw_clear_code (context);
Packit a4058c
			break;
Packit a4058c
Packit a4058c
		case GIF_GET_LZW:
Packit a4058c
                        LOG("get_lzw\n");
Packit a4058c
			retval = gif_get_lzw (context);
Packit a4058c
			break;
Packit a4058c
Packit a4058c
		case GIF_DONE:
Packit a4058c
                        LOG("done\n");
Packit a4058c
                        /* fall through */
Packit a4058c
		default:
Packit a4058c
			retval = 0;
Packit a4058c
			goto done;
Packit a4058c
		};
Packit a4058c
	} while ((retval == 0) || (retval == -3));
Packit a4058c
 done:
Packit a4058c
	return retval;
Packit a4058c
}
Packit a4058c
Packit a4058c
static GifContext *
Packit a4058c
new_context (void)
Packit a4058c
{
Packit a4058c
	GifContext *context;
Packit a4058c
Packit a4058c
	context = g_try_malloc (sizeof (GifContext));
Packit a4058c
        if (context == NULL)
Packit a4058c
                return NULL;
Packit a4058c
Packit a4058c
        memset (context, 0, sizeof (GifContext));
Packit a4058c
        
Packit a4058c
        context->animation = g_object_new (GDK_TYPE_PIXBUF_GIF_ANIM, NULL);
Packit a4058c
	context->frame = NULL;
Packit a4058c
	context->file = NULL;
Packit a4058c
	context->state = GIF_START;
Packit a4058c
	context->size_func = NULL;
Packit a4058c
	context->prepare_func = NULL;
Packit a4058c
	context->update_func = NULL;
Packit a4058c
	context->user_data = NULL;
Packit a4058c
	context->buf = NULL;
Packit a4058c
	context->amount_needed = 13;
Packit a4058c
	context->buf = g_new (guchar, context->amount_needed);
Packit a4058c
	context->gif89.transparent = -1;
Packit a4058c
	context->gif89.delay_time = -1;
Packit a4058c
	context->gif89.input_flag = -1;
Packit a4058c
	context->gif89.disposal = -1;
Packit a4058c
        context->animation->loop = 1;
Packit a4058c
        context->in_loop_extension = FALSE;
Packit a4058c
        context->stop_after_first_frame = FALSE;
Packit a4058c
Packit a4058c
	return context;
Packit a4058c
}
Packit a4058c
/* Shared library entry point */
Packit a4058c
static GdkPixbuf *
Packit a4058c
gdk_pixbuf__gif_image_load (FILE *file, GError **error)
Packit a4058c
{
Packit a4058c
	GifContext *context;
Packit a4058c
	GdkPixbuf *pixbuf;
Packit a4058c
        gint retval;
Packit a4058c
Packit a4058c
	g_return_val_if_fail (file != NULL, NULL);
Packit a4058c
Packit a4058c
	context = new_context ();
Packit a4058c
Packit a4058c
        if (context == NULL) {
Packit a4058c
                g_set_error_literal (error,
Packit a4058c
                                     GDK_PIXBUF_ERROR,
Packit a4058c
                                     GDK_PIXBUF_ERROR_INSUFFICIENT_MEMORY,
Packit a4058c
                                     _("Not enough memory to load GIF file"));
Packit a4058c
                return NULL;
Packit a4058c
        }
Packit a4058c
        
Packit a4058c
	context->file = file;
Packit a4058c
        context->error = error;
Packit a4058c
        context->stop_after_first_frame = TRUE;
Packit a4058c
Packit a4058c
        retval = gif_main_loop (context);
Packit a4058c
	if (retval == -1 || context->animation->frames == NULL) {
Packit a4058c
                if (context->error && *(context->error) == NULL)
Packit a4058c
                        g_set_error_literal (context->error,
Packit a4058c
                                             GDK_PIXBUF_ERROR,
Packit a4058c
                                             GDK_PIXBUF_ERROR_CORRUPT_IMAGE,
Packit a4058c
                                             _("GIF file was missing some data (perhaps it was truncated somehow?)"));
Packit a4058c
        }
Packit a4058c
        else if (retval == -2) {
Packit a4058c
                pixbuf = NULL;
Packit a4058c
                goto out;
Packit a4058c
        }
Packit a4058c
        
Packit a4058c
        pixbuf = gdk_pixbuf_animation_get_static_image (GDK_PIXBUF_ANIMATION (context->animation));
Packit a4058c
Packit a4058c
        if (pixbuf)
Packit a4058c
                g_object_ref (pixbuf);
Packit a4058c
Packit a4058c
out:
Packit a4058c
        g_object_unref (context->animation);
Packit a4058c
        
Packit a4058c
        g_free (context->buf);
Packit a4058c
	g_free (context);
Packit a4058c
 
Packit a4058c
	return pixbuf;
Packit a4058c
}
Packit a4058c
Packit a4058c
static gpointer
Packit a4058c
gdk_pixbuf__gif_image_begin_load (GdkPixbufModuleSizeFunc size_func,
Packit a4058c
                                  GdkPixbufModulePreparedFunc prepare_func,
Packit a4058c
				  GdkPixbufModuleUpdatedFunc update_func,
Packit a4058c
				  gpointer user_data,
Packit a4058c
                                  GError **error)
Packit a4058c
{
Packit a4058c
	GifContext *context;
Packit a4058c
Packit a4058c
#ifdef IO_GIFDEBUG
Packit a4058c
	count = 0;
Packit a4058c
#endif
Packit a4058c
	context = new_context ();
Packit a4058c
Packit a4058c
        if (context == NULL) {
Packit a4058c
                g_set_error_literal (error,
Packit a4058c
                                     GDK_PIXBUF_ERROR,
Packit a4058c
                                     GDK_PIXBUF_ERROR_INSUFFICIENT_MEMORY,
Packit a4058c
                                     _("Not enough memory to load GIF file"));
Packit a4058c
                return NULL;
Packit a4058c
        }
Packit a4058c
        
Packit a4058c
        context->error = error;
Packit a4058c
	context->size_func = size_func;
Packit a4058c
	context->prepare_func = prepare_func;
Packit a4058c
	context->update_func = update_func;
Packit a4058c
	context->user_data = user_data;
Packit a4058c
Packit a4058c
	return (gpointer) context;
Packit a4058c
}
Packit a4058c
Packit a4058c
static gboolean
Packit a4058c
gdk_pixbuf__gif_image_stop_load (gpointer data, GError **error)
Packit a4058c
{
Packit a4058c
	GifContext *context = (GifContext *) data;
Packit a4058c
        gboolean retval = TRUE;
Packit a4058c
        
Packit a4058c
        if (context->animation->frames == NULL) {
Packit a4058c
                g_set_error_literal (error,
Packit a4058c
                                     GDK_PIXBUF_ERROR,
Packit a4058c
                                     GDK_PIXBUF_ERROR_CORRUPT_IMAGE,
Packit a4058c
                                     _("GIF image was truncated or incomplete."));
Packit a4058c
Packit a4058c
                retval = FALSE;
Packit a4058c
        } else if (context->state != GIF_DONE) {
Packit a4058c
                g_set_error_literal (error,
Packit a4058c
                                     GDK_PIXBUF_ERROR,
Packit a4058c
                                     GDK_PIXBUF_ERROR_INCOMPLETE_ANIMATION,
Packit a4058c
                                     _("Not all frames of the GIF image were loaded."));
Packit a4058c
Packit a4058c
                retval = FALSE;
Packit a4058c
        }
Packit a4058c
Packit a4058c
        g_object_unref (context->animation);
Packit a4058c
Packit a4058c
  	g_free (context->buf);
Packit a4058c
	g_free (context);
Packit a4058c
Packit a4058c
        return retval;
Packit a4058c
}
Packit a4058c
Packit a4058c
static gboolean
Packit a4058c
gdk_pixbuf__gif_image_load_increment (gpointer data,
Packit a4058c
                                      const guchar *buf, guint size,
Packit a4058c
                                      GError **error)
Packit a4058c
{
Packit a4058c
	gint retval;
Packit a4058c
	GifContext *context = (GifContext *) data;
Packit a4058c
Packit a4058c
        context->error = error;
Packit a4058c
        
Packit a4058c
	if (context->amount_needed == 0) {
Packit a4058c
		/* we aren't looking for some bytes. */
Packit a4058c
		/* we can use buf now, but we don't want to keep it around at all.
Packit a4058c
		 * it will be gone by the end of the call. */
Packit a4058c
		context->buf = (guchar*) buf; /* very dubious const cast */
Packit a4058c
		context->ptr = 0;
Packit a4058c
		context->size = size;
Packit a4058c
	} else {
Packit a4058c
		/* we need some bytes */
Packit a4058c
		if (size < context->amount_needed) {
Packit a4058c
			context->amount_needed -= size;
Packit a4058c
			/* copy it over and return */
Packit a4058c
			memcpy (context->buf + context->size, buf, size);
Packit a4058c
			context->size += size;
Packit a4058c
			return TRUE;
Packit a4058c
		} else if (size == context->amount_needed) {
Packit a4058c
			memcpy (context->buf + context->size, buf, size);
Packit a4058c
			context->size += size;
Packit a4058c
		} else {
Packit a4058c
			context->buf = g_realloc (context->buf, context->size + size);
Packit a4058c
			memcpy (context->buf + context->size, buf, size);
Packit a4058c
			context->size += size;
Packit a4058c
		}
Packit a4058c
	}
Packit a4058c
Packit a4058c
	retval = gif_main_loop (context);
Packit a4058c
Packit a4058c
	if (retval == -2) {
Packit a4058c
		if (context->buf == buf)
Packit a4058c
                        context->buf = NULL;
Packit a4058c
		return FALSE;
Packit a4058c
        }
Packit a4058c
	if (retval == -1) {
Packit a4058c
		/* we didn't have enough memory */
Packit a4058c
		/* prepare for the next image_load_increment */
Packit a4058c
		if (context->buf == buf) {
Packit a4058c
			g_assert (context->size == size);
Packit a4058c
			context->buf = g_new (guchar, context->amount_needed + (context->size - context->ptr));
Packit a4058c
			memcpy (context->buf, buf + context->ptr, context->size - context->ptr);
Packit a4058c
		} else {
Packit a4058c
			/* copy the left overs to the begining of the buffer */
Packit a4058c
			/* and realloc the memory */
Packit a4058c
			memmove (context->buf, context->buf + context->ptr, context->size - context->ptr);
Packit a4058c
			context->buf = g_realloc (context->buf, context->amount_needed + (context->size - context->ptr));
Packit a4058c
		}
Packit a4058c
		context->size = context->size - context->ptr;
Packit a4058c
		context->ptr = 0;
Packit a4058c
	} else {
Packit a4058c
		/* we are prolly all done */
Packit a4058c
		if (context->buf == buf)
Packit a4058c
			context->buf = NULL;
Packit a4058c
	}
Packit a4058c
	return TRUE;
Packit a4058c
}
Packit a4058c
Packit a4058c
static GdkPixbufAnimation *
Packit a4058c
gdk_pixbuf__gif_image_load_animation (FILE *file,
Packit a4058c
                                      GError **error)
Packit a4058c
{
Packit a4058c
	GifContext *context;
Packit a4058c
	GdkPixbufAnimation *animation;
Packit a4058c
Packit a4058c
	g_return_val_if_fail (file != NULL, NULL);
Packit a4058c
Packit a4058c
	context = new_context ();
Packit a4058c
Packit a4058c
        if (context == NULL) {
Packit a4058c
                g_set_error_literal (error,
Packit a4058c
                                     GDK_PIXBUF_ERROR,
Packit a4058c
                                     GDK_PIXBUF_ERROR_INSUFFICIENT_MEMORY,
Packit a4058c
                                     _("Not enough memory to load GIF file"));
Packit a4058c
                return NULL;
Packit a4058c
        }
Packit a4058c
        
Packit a4058c
        context->error = error;
Packit a4058c
	context->file = file;
Packit a4058c
Packit a4058c
	if (gif_main_loop (context) == -1 || context->animation->frames == NULL) {
Packit a4058c
                if (context->error && *(context->error) == NULL)
Packit a4058c
                        g_set_error_literal (context->error,
Packit a4058c
                                             GDK_PIXBUF_ERROR,
Packit a4058c
                                             GDK_PIXBUF_ERROR_CORRUPT_IMAGE,
Packit a4058c
                                             _("GIF file was missing some data (perhaps it was truncated somehow?)"));
Packit a4058c
Packit a4058c
                g_object_unref (context->animation);
Packit a4058c
                context->animation = NULL;
Packit a4058c
        }
Packit a4058c
Packit a4058c
        if (context->animation)
Packit a4058c
                animation = GDK_PIXBUF_ANIMATION (context->animation);
Packit a4058c
        else
Packit a4058c
                animation = NULL;
Packit a4058c
Packit a4058c
        if (context->error && *(context->error))
Packit a4058c
                g_print ("%s\n", (*(context->error))->message);
Packit a4058c
        
Packit a4058c
        g_free (context->buf);
Packit a4058c
	g_free (context);
Packit a4058c
	return animation;
Packit a4058c
}
Packit a4058c
Packit a4058c
#ifndef INCLUDE_gif
Packit a4058c
#define MODULE_ENTRY(function) G_MODULE_EXPORT void function
Packit a4058c
#else
Packit a4058c
#define MODULE_ENTRY(function) void _gdk_pixbuf__gif_ ## function
Packit a4058c
#endif
Packit a4058c
Packit a4058c
MODULE_ENTRY (fill_vtable) (GdkPixbufModule *module)
Packit a4058c
{
Packit a4058c
        module->load = gdk_pixbuf__gif_image_load;
Packit a4058c
        module->begin_load = gdk_pixbuf__gif_image_begin_load;
Packit a4058c
        module->stop_load = gdk_pixbuf__gif_image_stop_load;
Packit a4058c
        module->load_increment = gdk_pixbuf__gif_image_load_increment;
Packit a4058c
        module->load_animation = gdk_pixbuf__gif_image_load_animation;
Packit a4058c
}
Packit a4058c
Packit a4058c
MODULE_ENTRY (fill_info) (GdkPixbufFormat *info)
Packit a4058c
{
Packit a4058c
        static const GdkPixbufModulePattern signature[] = {
Packit a4058c
                { "GIF8", NULL, 100 },
Packit a4058c
                { NULL, NULL, 0 }
Packit a4058c
        };
Packit a4058c
	static const gchar *mime_types[] = {
Packit a4058c
		"image/gif",
Packit a4058c
		NULL
Packit a4058c
	};
Packit a4058c
	static const gchar *extensions[] = {
Packit a4058c
		"gif",
Packit a4058c
		NULL
Packit a4058c
	};
Packit a4058c
Packit a4058c
	info->name = "gif";
Packit a4058c
        info->signature = (GdkPixbufModulePattern *) signature;
Packit a4058c
	info->description = NC_("image format", "GIF");
Packit a4058c
	info->mime_types = (gchar **) mime_types;
Packit a4058c
	info->extensions = (gchar **) extensions;
Packit a4058c
	info->flags = GDK_PIXBUF_FORMAT_THREADSAFE;
Packit a4058c
	info->license = "LGPL";
Packit a4058c
}