1999-10-20 21:20:49 +00:00
|
|
|
|
/* GdkPixbuf library - GIF image loader
|
1999-06-29 06:07:14 +00:00
|
|
|
|
*
|
1999-06-30 15:28:43 +00:00
|
|
|
|
* Copyright (C) 1999 Mark Crichton
|
1999-10-20 21:20:49 +00:00
|
|
|
|
* Copyright (C) 1999 The Free Software Foundation
|
|
|
|
|
*
|
1999-10-30 22:04:27 +00:00
|
|
|
|
* Authors: Jonathan Blandford <jrb@redhat.com>
|
|
|
|
|
* Adapted from the gimp gif filter written by Adam Moss <adam@gimp.org>
|
1999-11-03 22:27:59 +00:00
|
|
|
|
* Gimp work based on earlier work.
|
1999-10-30 22:04:27 +00:00
|
|
|
|
* Permission to relicense under the LGPL obtained.
|
1999-06-30 15:28:43 +00:00
|
|
|
|
*
|
|
|
|
|
* This library is free software; you can redistribute it and/or
|
2000-07-26 11:33:08 +00:00
|
|
|
|
* modify it under the terms of the GNU Lesser General Public
|
1999-06-30 15:28:43 +00:00
|
|
|
|
* License as published by the Free Software Foundation; either
|
|
|
|
|
* version 2 of the License, or (at your option) any later version.
|
|
|
|
|
*
|
|
|
|
|
* This library is distributed in the hope that it will be useful,
|
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
2000-07-26 11:33:08 +00:00
|
|
|
|
* Lesser General Public License for more details.
|
1999-06-30 15:28:43 +00:00
|
|
|
|
*
|
2000-07-26 11:33:08 +00:00
|
|
|
|
* You should have received a copy of the GNU Lesser General Public
|
1999-10-20 21:20:49 +00:00
|
|
|
|
* License along with this library; if not, write to the
|
|
|
|
|
* Free Software Foundation, Inc., 59 Temple Place - Suite 330,
|
|
|
|
|
* Boston, MA 02111-1307, USA.
|
1999-06-29 06:07:14 +00:00
|
|
|
|
*/
|
1999-06-30 15:28:43 +00:00
|
|
|
|
|
1999-11-03 23:22:27 +00:00
|
|
|
|
/* This loader is very hairy code.
|
|
|
|
|
*
|
|
|
|
|
* The main loop was not designed for incremental loading, so when it was hacked
|
|
|
|
|
* in it got a bit messy. Basicly, every function is written to expect a failed
|
|
|
|
|
* read_gif, and lets you call it again assuming that the bytes are there.
|
|
|
|
|
*
|
|
|
|
|
* A note on Animations:
|
1999-12-20 22:51:05 +00:00
|
|
|
|
* Currently, it doesn't correctly read the different colormap per frame. This
|
|
|
|
|
* needs implementing sometime.
|
1999-11-03 23:22:27 +00:00
|
|
|
|
*
|
|
|
|
|
* Return vals.
|
|
|
|
|
* Unless otherwise specified, these are the return vals for most functions:
|
|
|
|
|
*
|
|
|
|
|
* 0 -> success
|
|
|
|
|
* -1 -> more bytes needed.
|
|
|
|
|
* -2 -> failure; abort the load
|
|
|
|
|
* -3 -> control needs to be passed back to the main loop
|
|
|
|
|
* \_ (most of the time returning 0 will get this, but not always)
|
2000-01-07 18:29:13 +00:00
|
|
|
|
*
|
1999-11-03 23:22:27 +00:00
|
|
|
|
* >1 -> for functions that get a guchar, the char will be returned.
|
|
|
|
|
*
|
|
|
|
|
* -jrb (11/03/1999)
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
* If you have any images that crash this code, please, please let me know and
|
|
|
|
|
* send them to me.
|
|
|
|
|
* <jrb@redhat.com>
|
|
|
|
|
*/
|
|
|
|
|
|
1999-11-04 20:34:24 +00:00
|
|
|
|
|
|
|
|
|
|
1999-06-29 06:07:14 +00:00
|
|
|
|
#include <config.h>
|
|
|
|
|
#include <stdio.h>
|
1999-10-30 19:28:53 +00:00
|
|
|
|
#include <string.h>
|
2000-04-11 07:03:25 +00:00
|
|
|
|
#include "gdk-pixbuf-private.h"
|
1999-12-02 20:44:43 +00:00
|
|
|
|
#include "gdk-pixbuf-io.h"
|
1999-10-20 21:20:49 +00:00
|
|
|
|
|
1999-11-04 20:34:24 +00:00
|
|
|
|
|
1999-10-30 19:28:53 +00:00
|
|
|
|
|
|
|
|
|
#define MAXCOLORMAPSIZE 256
|
1999-11-03 17:45:24 +00:00
|
|
|
|
#define MAX_LZW_BITS 12
|
1999-10-30 19:28:53 +00:00
|
|
|
|
|
|
|
|
|
#define INTERLACE 0x40
|
|
|
|
|
#define LOCALCOLORMAP 0x80
|
|
|
|
|
#define BitSet(byte, bit) (((byte) & (bit)) == (bit))
|
|
|
|
|
#define LM_to_uint(a,b) (((b)<<8)|(a))
|
|
|
|
|
|
1999-11-04 20:34:24 +00:00
|
|
|
|
|
|
|
|
|
|
1999-10-30 19:28:53 +00:00
|
|
|
|
typedef unsigned char CMap[3][MAXCOLORMAPSIZE];
|
|
|
|
|
|
1999-11-02 01:17:41 +00:00
|
|
|
|
/* Possible states we can be in. */
|
|
|
|
|
enum {
|
1999-11-03 21:22:38 +00:00
|
|
|
|
GIF_START,
|
1999-11-02 23:49:22 +00:00
|
|
|
|
GIF_GET_COLORMAP,
|
|
|
|
|
GIF_GET_NEXT_STEP,
|
|
|
|
|
GIF_GET_FRAME_INFO,
|
|
|
|
|
GIF_GET_EXTENTION,
|
|
|
|
|
GIF_GET_COLORMAP2,
|
|
|
|
|
GIF_PREPARE_LZW,
|
|
|
|
|
GIF_LZW_FILL_BUFFER,
|
1999-11-03 21:22:38 +00:00
|
|
|
|
GIF_LZW_CLEAR_CODE,
|
1999-11-02 23:49:22 +00:00
|
|
|
|
GIF_GET_LZW,
|
|
|
|
|
GIF_DONE,
|
1999-11-02 01:17:41 +00:00
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
1999-10-30 19:28:53 +00:00
|
|
|
|
typedef struct _Gif89 Gif89;
|
|
|
|
|
struct _Gif89
|
|
|
|
|
{
|
|
|
|
|
int transparent;
|
1999-11-10 21:06:40 +00:00
|
|
|
|
int delay_time;
|
|
|
|
|
int input_flag;
|
|
|
|
|
int disposal;
|
1999-10-30 19:28:53 +00:00
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
typedef struct _GifContext GifContext;
|
|
|
|
|
struct _GifContext
|
1999-10-20 21:20:49 +00:00
|
|
|
|
{
|
1999-11-02 01:17:41 +00:00
|
|
|
|
int state; /* really only relevant for progressive loading */
|
1999-10-30 19:28:53 +00:00
|
|
|
|
unsigned int width;
|
|
|
|
|
unsigned int height;
|
|
|
|
|
CMap color_map;
|
1999-11-10 21:06:40 +00:00
|
|
|
|
CMap frame_color_map;
|
1999-10-30 19:28:53 +00:00
|
|
|
|
unsigned int bit_pixel;
|
|
|
|
|
unsigned int color_resolution;
|
|
|
|
|
unsigned int background;
|
|
|
|
|
unsigned int aspect_ratio;
|
|
|
|
|
GdkPixbuf *pixbuf;
|
1999-12-20 22:51:05 +00:00
|
|
|
|
GdkPixbufAnimation *animation;
|
|
|
|
|
GdkPixbufFrame *frame;
|
1999-10-30 19:28:53 +00:00
|
|
|
|
Gif89 gif89;
|
1999-11-01 13:26:03 +00:00
|
|
|
|
|
|
|
|
|
/* stuff per frame. As we only support the first one, not so
|
|
|
|
|
* relevant. But still needed */
|
|
|
|
|
int frame_len;
|
|
|
|
|
int frame_height;
|
|
|
|
|
int frame_interlace;
|
2000-02-03 23:36:14 +00:00
|
|
|
|
int x_offset;
|
|
|
|
|
int y_offset;
|
1999-11-01 13:26:03 +00:00
|
|
|
|
|
|
|
|
|
/* Static read only */
|
|
|
|
|
FILE *file;
|
|
|
|
|
|
|
|
|
|
/* progressive read, only. */
|
1999-11-04 18:18:07 +00:00
|
|
|
|
ModulePreparedNotifyFunc prepare_func;
|
|
|
|
|
ModuleUpdatedNotifyFunc update_func;
|
2000-01-07 18:29:13 +00:00
|
|
|
|
ModuleFrameDoneNotifyFunc frame_done_func;
|
|
|
|
|
ModuleAnimationDoneNotifyFunc anim_done_func;
|
1999-11-01 13:26:03 +00:00
|
|
|
|
gpointer user_data;
|
|
|
|
|
guchar *buf;
|
|
|
|
|
guint ptr;
|
|
|
|
|
guint size;
|
1999-11-02 01:17:41 +00:00
|
|
|
|
guint amount_needed;
|
1999-11-01 13:26:03 +00:00
|
|
|
|
|
1999-11-02 23:49:22 +00:00
|
|
|
|
/* colormap context */
|
1999-11-02 01:17:41 +00:00
|
|
|
|
gint colormap_index;
|
|
|
|
|
gint colormap_flag;
|
1999-11-01 13:26:03 +00:00
|
|
|
|
|
1999-11-02 23:49:22 +00:00
|
|
|
|
/* extension context */
|
1999-11-02 01:17:41 +00:00
|
|
|
|
guchar extension_label;
|
|
|
|
|
guchar extension_flag;
|
1999-10-30 19:28:53 +00:00
|
|
|
|
|
1999-11-02 01:17:41 +00:00
|
|
|
|
/* get block context */
|
|
|
|
|
guchar block_count;
|
1999-11-02 23:49:22 +00:00
|
|
|
|
guchar block_buf[280];
|
1999-11-02 01:17:41 +00:00
|
|
|
|
gint block_ptr;
|
1999-10-30 19:28:53 +00:00
|
|
|
|
|
1999-11-03 21:22:38 +00:00
|
|
|
|
int old_state; /* used by lzw_fill buffer */
|
1999-11-02 23:49:22 +00:00
|
|
|
|
/* get_code context */
|
|
|
|
|
int code_curbit;
|
|
|
|
|
int code_lastbit;
|
|
|
|
|
int code_done;
|
|
|
|
|
int code_last_byte;
|
1999-11-03 21:22:38 +00:00
|
|
|
|
int lzw_code_pending;
|
|
|
|
|
|
1999-11-03 17:45:24 +00:00
|
|
|
|
/* lzw context */
|
|
|
|
|
gint lzw_fresh;
|
|
|
|
|
gint lzw_code_size;
|
|
|
|
|
guchar lzw_set_code_size;
|
|
|
|
|
gint lzw_max_code;
|
|
|
|
|
gint lzw_max_code_size;
|
|
|
|
|
gint lzw_firstcode;
|
|
|
|
|
gint lzw_oldcode;
|
|
|
|
|
gint lzw_clear_code;
|
|
|
|
|
gint lzw_end_code;
|
|
|
|
|
gint *lzw_sp;
|
|
|
|
|
|
|
|
|
|
gint lzw_table[2][(1 << MAX_LZW_BITS)];
|
|
|
|
|
gint lzw_stack[(1 << (MAX_LZW_BITS)) * 2 + 1];
|
1999-11-03 16:40:43 +00:00
|
|
|
|
|
1999-11-02 23:49:22 +00:00
|
|
|
|
/* painting context */
|
|
|
|
|
gint draw_xpos;
|
|
|
|
|
gint draw_ypos;
|
|
|
|
|
gint draw_pass;
|
2000-10-18 18:42:54 +00:00
|
|
|
|
|
|
|
|
|
/* error pointer */
|
|
|
|
|
GError **error;
|
1999-11-02 01:17:41 +00:00
|
|
|
|
};
|
1999-10-30 19:28:53 +00:00
|
|
|
|
|
1999-11-01 13:26:03 +00:00
|
|
|
|
static int GetDataBlock (GifContext *, unsigned char *);
|
1999-10-30 19:28:53 +00:00
|
|
|
|
|
1999-11-04 20:34:24 +00:00
|
|
|
|
|
|
|
|
|
|
1999-11-03 22:19:42 +00:00
|
|
|
|
#ifdef IO_GIFDEBUG
|
1999-11-03 16:40:43 +00:00
|
|
|
|
static int count = 0;
|
1999-11-03 22:19:42 +00:00
|
|
|
|
#endif
|
|
|
|
|
|
1999-11-03 23:22:27 +00:00
|
|
|
|
/* Returns TRUE if read is OK,
|
1999-11-02 23:49:22 +00:00
|
|
|
|
* FALSE if more memory is needed. */
|
1999-10-30 19:28:53 +00:00
|
|
|
|
static int
|
1999-11-03 23:22:27 +00:00
|
|
|
|
gif_read (GifContext *context, guchar *buffer, size_t len)
|
1999-11-01 13:26:03 +00:00
|
|
|
|
{
|
1999-11-03 00:20:27 +00:00
|
|
|
|
gint retval;
|
|
|
|
|
#ifdef IO_GIFDEBUG
|
|
|
|
|
gint i;
|
|
|
|
|
#endif
|
1999-11-02 23:49:22 +00:00
|
|
|
|
if (context->file) {
|
|
|
|
|
#ifdef IO_GIFDEBUG
|
1999-11-03 22:19:42 +00:00
|
|
|
|
count += len;
|
1999-11-02 23:49:22 +00:00
|
|
|
|
g_print ("Fsize :%d\tcount :%d\t", len, count);
|
|
|
|
|
#endif
|
2000-10-18 18:42:54 +00:00
|
|
|
|
retval = (fread(buffer, len, 1, context->file) != 0);
|
1999-11-02 23:49:22 +00:00
|
|
|
|
#ifdef IO_GIFDEBUG
|
|
|
|
|
if (len < 100) {
|
|
|
|
|
for (i = 0; i < len; i++)
|
|
|
|
|
g_print ("%d ", buffer[i]);
|
|
|
|
|
}
|
|
|
|
|
g_print ("\n");
|
|
|
|
|
#endif
|
2000-10-18 18:42:54 +00:00
|
|
|
|
|
1999-11-02 23:49:22 +00:00
|
|
|
|
return retval;
|
|
|
|
|
} else {
|
|
|
|
|
#ifdef IO_GIFDEBUG
|
2000-01-02 03:59:22 +00:00
|
|
|
|
/* g_print ("\tlooking for %d bytes. size == %d, ptr == %d\n", len, context->size, context->ptr); */
|
1999-11-02 23:49:22 +00:00
|
|
|
|
#endif
|
|
|
|
|
if ((context->size - context->ptr) >= len) {
|
1999-11-03 22:19:42 +00:00
|
|
|
|
#ifdef IO_GIFDEBUG
|
1999-11-02 23:49:22 +00:00
|
|
|
|
count += len;
|
1999-11-03 22:19:42 +00:00
|
|
|
|
#endif
|
|
|
|
|
memcpy (buffer, context->buf + context->ptr, len);
|
1999-11-02 01:17:41 +00:00
|
|
|
|
context->ptr += len;
|
|
|
|
|
context->amount_needed = 0;
|
1999-11-02 23:49:22 +00:00
|
|
|
|
#ifdef IO_GIFDEBUG
|
|
|
|
|
g_print ("Psize :%d\tcount :%d\t", len, count);
|
|
|
|
|
if (len < 100) {
|
|
|
|
|
for (i = 0; i < len; i++)
|
|
|
|
|
g_print ("%d ", buffer[i]);
|
|
|
|
|
}
|
|
|
|
|
g_print ("\n");
|
|
|
|
|
#endif
|
|
|
|
|
return TRUE;
|
1999-11-02 01:17:41 +00:00
|
|
|
|
}
|
|
|
|
|
context->amount_needed = len - (context->size - context->ptr);
|
|
|
|
|
}
|
1999-11-02 23:49:22 +00:00
|
|
|
|
return 0;
|
1999-11-01 13:26:03 +00:00
|
|
|
|
}
|
|
|
|
|
|
1999-11-02 01:17:41 +00:00
|
|
|
|
/* Changes the stage to be GIF_GET_COLORMAP */
|
|
|
|
|
static void
|
|
|
|
|
gif_set_get_colormap (GifContext *context)
|
1999-10-30 19:28:53 +00:00
|
|
|
|
{
|
1999-11-02 01:17:41 +00:00
|
|
|
|
context->colormap_flag = TRUE;
|
|
|
|
|
context->colormap_index = 0;
|
|
|
|
|
context->state = GIF_GET_COLORMAP;
|
|
|
|
|
}
|
1999-10-30 19:28:53 +00:00
|
|
|
|
|
1999-11-02 01:17:41 +00:00
|
|
|
|
static void
|
|
|
|
|
gif_set_get_colormap2 (GifContext *context)
|
|
|
|
|
{
|
|
|
|
|
context->colormap_flag = TRUE;
|
|
|
|
|
context->colormap_index = 0;
|
|
|
|
|
context->state = GIF_GET_COLORMAP2;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static gint
|
|
|
|
|
gif_get_colormap (GifContext *context)
|
|
|
|
|
{
|
|
|
|
|
unsigned char rgb[3];
|
1999-10-30 19:28:53 +00:00
|
|
|
|
|
1999-11-02 01:17:41 +00:00
|
|
|
|
while (context->colormap_index < context->bit_pixel) {
|
1999-11-03 23:22:27 +00:00
|
|
|
|
if (!gif_read (context, rgb, sizeof (rgb))) {
|
1999-10-30 19:28:53 +00:00
|
|
|
|
/*g_message (_("GIF: bad colormap\n"));*/
|
1999-11-02 01:17:41 +00:00
|
|
|
|
return -1;
|
1999-10-30 19:28:53 +00:00
|
|
|
|
}
|
|
|
|
|
|
1999-11-03 22:27:59 +00:00
|
|
|
|
context->color_map[0][context->colormap_index] = rgb[0];
|
|
|
|
|
context->color_map[1][context->colormap_index] = rgb[1];
|
|
|
|
|
context->color_map[2][context->colormap_index] = rgb[2];
|
1999-10-30 19:28:53 +00:00
|
|
|
|
|
1999-11-02 01:17:41 +00:00
|
|
|
|
context->colormap_flag &= (rgb[0] == rgb[1] && rgb[1] == rgb[2]);
|
|
|
|
|
context->colormap_index ++;
|
1999-10-30 19:28:53 +00:00
|
|
|
|
}
|
|
|
|
|
|
1999-11-02 01:17:41 +00:00
|
|
|
|
return 0;
|
1999-10-20 21:20:49 +00:00
|
|
|
|
}
|
1999-06-29 06:07:14 +00:00
|
|
|
|
|
1999-11-02 01:17:41 +00:00
|
|
|
|
/*
|
|
|
|
|
* in order for this function to work, we need to perform some black magic.
|
|
|
|
|
* We want to return -1 to let the calling function know, as before, that it needs
|
|
|
|
|
* more bytes. If we return 0, we were able to successfully read all block->count bytes.
|
|
|
|
|
* Problem is, we don't want to reread block_count every time, so we check to see if
|
|
|
|
|
* context->block_count is 0 before we read in the function.
|
|
|
|
|
*
|
|
|
|
|
* As a result, context->block_count MUST be 0 the first time the get_data_block is called
|
1999-11-02 23:49:22 +00:00
|
|
|
|
* within a context, and cannot be 0 the second time it's called.
|
1999-11-02 01:17:41 +00:00
|
|
|
|
*/
|
|
|
|
|
|
1999-10-30 19:28:53 +00:00
|
|
|
|
static int
|
1999-11-02 01:17:41 +00:00
|
|
|
|
get_data_block (GifContext *context,
|
|
|
|
|
unsigned char *buf,
|
|
|
|
|
gint *empty_block)
|
1999-06-29 06:07:14 +00:00
|
|
|
|
{
|
1999-11-02 01:17:41 +00:00
|
|
|
|
|
|
|
|
|
if (context->block_count == 0) {
|
1999-11-03 23:22:27 +00:00
|
|
|
|
if (!gif_read (context, &context->block_count, 1)) {
|
1999-11-02 01:17:41 +00:00
|
|
|
|
return -1;
|
1999-10-30 22:04:27 +00:00
|
|
|
|
}
|
1999-10-18 19:29:45 +00:00
|
|
|
|
}
|
1999-10-20 21:20:49 +00:00
|
|
|
|
|
1999-11-02 01:17:41 +00:00
|
|
|
|
if (context->block_count == 0)
|
1999-11-02 23:49:22 +00:00
|
|
|
|
if (empty_block) {
|
1999-11-02 01:17:41 +00:00
|
|
|
|
*empty_block = TRUE;
|
1999-11-02 23:49:22 +00:00
|
|
|
|
return 0;
|
|
|
|
|
}
|
1999-11-02 01:17:41 +00:00
|
|
|
|
|
1999-11-03 23:22:27 +00:00
|
|
|
|
if (!gif_read (context, buf, context->block_count)) {
|
1999-11-02 01:17:41 +00:00
|
|
|
|
return -1;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static void
|
|
|
|
|
gif_set_get_extension (GifContext *context)
|
|
|
|
|
{
|
|
|
|
|
context->state = GIF_GET_EXTENTION;
|
|
|
|
|
context->extension_flag = TRUE;
|
1999-11-03 05:12:02 +00:00
|
|
|
|
context->extension_label = 0;
|
1999-11-02 01:17:41 +00:00
|
|
|
|
context->block_count = 0;
|
|
|
|
|
context->block_ptr = 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static int
|
|
|
|
|
gif_get_extension (GifContext *context)
|
|
|
|
|
{
|
|
|
|
|
gint retval;
|
|
|
|
|
gint empty_block = FALSE;
|
|
|
|
|
|
|
|
|
|
if (context->extension_flag) {
|
1999-11-03 05:12:02 +00:00
|
|
|
|
if (context->extension_label == 0) {
|
|
|
|
|
/* I guess bad things can happen if we have an extension of 0 )-: */
|
|
|
|
|
/* I should look into this sometime */
|
1999-11-03 23:22:27 +00:00
|
|
|
|
if (!gif_read (context, & context->extension_label , 1)) {
|
1999-11-02 01:17:41 +00:00
|
|
|
|
return -1;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
switch (context->extension_label) {
|
|
|
|
|
case 0xf9: /* Graphic Control Extension */
|
|
|
|
|
retval = get_data_block (context, (unsigned char *) context->block_buf, NULL);
|
|
|
|
|
if (retval != 0)
|
|
|
|
|
return retval;
|
|
|
|
|
if (context->pixbuf == NULL) {
|
|
|
|
|
/* I only want to set the transparency if I haven't
|
|
|
|
|
* created the pixbuf yet. */
|
1999-12-20 22:51:05 +00:00
|
|
|
|
context->gif89.disposal = (context->block_buf[0] >> 2) & 0x7;
|
|
|
|
|
context->gif89.input_flag = (context->block_buf[0] >> 1) & 0x1;
|
|
|
|
|
context->gif89.delay_time = LM_to_uint (context->block_buf[1], context->block_buf[2]);
|
|
|
|
|
|
1999-11-03 22:19:42 +00:00
|
|
|
|
if ((context->block_buf[0] & 0x1) != 0) {
|
1999-11-02 01:17:41 +00:00
|
|
|
|
context->gif89.transparent = context->block_buf[3];
|
1999-11-03 22:19:42 +00:00
|
|
|
|
} else {
|
2000-02-20 03:08:00 +00:00
|
|
|
|
context->gif89.transparent = -1;
|
1999-11-03 22:19:42 +00:00
|
|
|
|
}
|
1999-11-02 01:17:41 +00:00
|
|
|
|
}
|
1999-11-03 21:22:38 +00:00
|
|
|
|
|
1999-11-02 01:17:41 +00:00
|
|
|
|
/* Now we've successfully loaded this one, we continue on our way */
|
1999-11-02 23:49:22 +00:00
|
|
|
|
context->block_count = 0;
|
1999-11-02 01:17:41 +00:00
|
|
|
|
context->extension_flag = FALSE;
|
|
|
|
|
default:
|
|
|
|
|
/* Unhandled extension */
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
1999-11-03 05:12:02 +00:00
|
|
|
|
/* read all blocks, until I get an empty block, in case there was an extension I didn't know about. */
|
1999-11-02 01:17:41 +00:00
|
|
|
|
do {
|
|
|
|
|
retval = get_data_block (context, (unsigned char *) context->block_buf, &empty_block);
|
|
|
|
|
if (retval != 0)
|
|
|
|
|
return retval;
|
1999-11-03 05:12:02 +00:00
|
|
|
|
context->block_count = 0;
|
1999-11-02 01:17:41 +00:00
|
|
|
|
} while (!empty_block);
|
|
|
|
|
|
|
|
|
|
return 0;
|
1999-10-30 19:28:53 +00:00
|
|
|
|
}
|
|
|
|
|
|
1999-10-31 01:13:25 +00:00
|
|
|
|
static int ZeroDataBlock = FALSE;
|
1999-10-30 19:28:53 +00:00
|
|
|
|
|
|
|
|
|
static int
|
1999-11-02 01:17:41 +00:00
|
|
|
|
GetDataBlock (GifContext *context,
|
1999-10-30 19:28:53 +00:00
|
|
|
|
unsigned char *buf)
|
|
|
|
|
{
|
2000-01-02 03:59:22 +00:00
|
|
|
|
/* unsigned char count; */
|
1999-10-30 19:28:53 +00:00
|
|
|
|
|
1999-11-03 23:22:27 +00:00
|
|
|
|
if (!gif_read (context, &context->block_count, 1)) {
|
1999-10-30 19:28:53 +00:00
|
|
|
|
/*g_message (_("GIF: error in getting DataBlock size\n"));*/
|
|
|
|
|
return -1;
|
|
|
|
|
}
|
|
|
|
|
|
1999-11-02 01:17:41 +00:00
|
|
|
|
ZeroDataBlock = context->block_count == 0;
|
1999-10-30 19:28:53 +00:00
|
|
|
|
|
1999-11-03 23:22:27 +00:00
|
|
|
|
if ((context->block_count != 0) && (!gif_read (context, buf, context->block_count))) {
|
1999-10-30 19:28:53 +00:00
|
|
|
|
/*g_message (_("GIF: error in reading DataBlock\n"));*/
|
|
|
|
|
return -1;
|
|
|
|
|
}
|
|
|
|
|
|
1999-11-02 01:17:41 +00:00
|
|
|
|
return context->block_count;
|
1999-10-30 19:28:53 +00:00
|
|
|
|
}
|
|
|
|
|
|
1999-11-02 23:49:22 +00:00
|
|
|
|
|
|
|
|
|
static void
|
|
|
|
|
gif_set_lzw_fill_buffer (GifContext *context)
|
|
|
|
|
{
|
|
|
|
|
context->block_count = 0;
|
1999-11-03 21:22:38 +00:00
|
|
|
|
context->old_state = context->state;
|
1999-11-02 23:49:22 +00:00
|
|
|
|
context->state = GIF_LZW_FILL_BUFFER;
|
|
|
|
|
}
|
1999-10-30 19:28:53 +00:00
|
|
|
|
|
|
|
|
|
static int
|
1999-11-02 23:49:22 +00:00
|
|
|
|
gif_lzw_fill_buffer (GifContext *context)
|
|
|
|
|
{
|
|
|
|
|
gint retval;
|
1999-11-03 21:22:38 +00:00
|
|
|
|
|
1999-11-02 23:49:22 +00:00
|
|
|
|
if (context->code_done) {
|
|
|
|
|
if (context->code_curbit >= context->code_lastbit) {
|
2000-10-18 18:42:54 +00:00
|
|
|
|
g_set_error (context->error,
|
|
|
|
|
GDK_PIXBUF_ERROR,
|
|
|
|
|
GDK_PIXBUF_ERROR_CORRUPT_IMAGE,
|
|
|
|
|
_("GIF file was missing some data (perhaps it was truncated somehow?)"));
|
|
|
|
|
|
1999-11-02 23:49:22 +00:00
|
|
|
|
return -2;
|
|
|
|
|
}
|
2000-10-18 18:42:54 +00:00
|
|
|
|
/* Is this supposed to be an error or what? */
|
|
|
|
|
/* g_message ("trying to read more data after we've done stuff\n"); */
|
|
|
|
|
g_set_error (context->error,
|
|
|
|
|
GDK_PIXBUF_ERROR,
|
|
|
|
|
GDK_PIXBUF_ERROR_FAILED,
|
|
|
|
|
_("Internal error in the GIF loader (%s)"),
|
|
|
|
|
G_STRLOC);
|
|
|
|
|
|
1999-11-02 23:49:22 +00:00
|
|
|
|
return -2;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
context->block_buf[0] = context->block_buf[context->code_last_byte - 2];
|
|
|
|
|
context->block_buf[1] = context->block_buf[context->code_last_byte - 1];
|
|
|
|
|
|
|
|
|
|
retval = get_data_block (context, &context->block_buf[2], NULL);
|
|
|
|
|
|
|
|
|
|
if (retval == -1)
|
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
|
|
if (context->block_count == 0)
|
|
|
|
|
context->code_done = TRUE;
|
|
|
|
|
|
|
|
|
|
context->code_last_byte = 2 + context->block_count;
|
|
|
|
|
context->code_curbit = (context->code_curbit - context->code_lastbit) + 16;
|
|
|
|
|
context->code_lastbit = (2 + context->block_count) * 8;
|
|
|
|
|
|
1999-11-03 21:22:38 +00:00
|
|
|
|
context->state = context->old_state;
|
1999-11-02 23:49:22 +00:00
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static int
|
1999-11-03 21:22:38 +00:00
|
|
|
|
get_code (GifContext *context,
|
|
|
|
|
int code_size)
|
1999-11-02 23:49:22 +00:00
|
|
|
|
{
|
|
|
|
|
int i, j, ret;
|
|
|
|
|
|
|
|
|
|
if ((context->code_curbit + code_size) >= context->code_lastbit){
|
|
|
|
|
gif_set_lzw_fill_buffer (context);
|
|
|
|
|
return -3;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
ret = 0;
|
|
|
|
|
for (i = context->code_curbit, j = 0; j < code_size; ++i, ++j)
|
|
|
|
|
ret |= ((context->block_buf[i / 8] & (1 << (i % 8))) != 0) << j;
|
|
|
|
|
|
|
|
|
|
context->code_curbit += code_size;
|
|
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
|
}
|
|
|
|
|
|
1999-11-03 21:22:38 +00:00
|
|
|
|
|
|
|
|
|
static void
|
|
|
|
|
set_gif_lzw_clear_code (GifContext *context)
|
|
|
|
|
{
|
|
|
|
|
context->state = GIF_LZW_CLEAR_CODE;
|
|
|
|
|
context->lzw_code_pending = -1;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static int
|
|
|
|
|
gif_lzw_clear_code (GifContext *context)
|
|
|
|
|
{
|
|
|
|
|
gint code;
|
|
|
|
|
|
|
|
|
|
code = get_code (context, context->lzw_code_size);
|
|
|
|
|
if (code == -3)
|
|
|
|
|
return -0;
|
|
|
|
|
|
|
|
|
|
context->lzw_firstcode = context->lzw_oldcode = code;
|
|
|
|
|
context->lzw_code_pending = code;
|
|
|
|
|
context->state = GIF_GET_LZW;
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
1999-11-02 23:49:22 +00:00
|
|
|
|
|
|
|
|
|
static int
|
1999-11-03 17:45:24 +00:00
|
|
|
|
lzw_read_byte (GifContext *context)
|
1999-10-30 19:28:53 +00:00
|
|
|
|
{
|
|
|
|
|
int code, incode;
|
1999-11-02 23:49:22 +00:00
|
|
|
|
gint retval;
|
1999-11-03 21:22:38 +00:00
|
|
|
|
gint my_retval;
|
1999-10-30 19:28:53 +00:00
|
|
|
|
register int i;
|
|
|
|
|
|
1999-11-03 21:22:38 +00:00
|
|
|
|
if (context->lzw_code_pending != -1) {
|
|
|
|
|
retval = context->lzw_code_pending;
|
|
|
|
|
context->lzw_code_pending = -1;
|
|
|
|
|
return retval;
|
|
|
|
|
}
|
|
|
|
|
|
1999-11-03 17:45:24 +00:00
|
|
|
|
if (context->lzw_fresh) {
|
|
|
|
|
context->lzw_fresh = FALSE;
|
1999-10-30 19:28:53 +00:00
|
|
|
|
do {
|
1999-11-03 21:22:38 +00:00
|
|
|
|
retval = get_code (context, context->lzw_code_size);
|
1999-11-02 23:49:22 +00:00
|
|
|
|
if (retval < 0) {
|
|
|
|
|
return retval;
|
|
|
|
|
}
|
1999-11-03 21:22:38 +00:00
|
|
|
|
|
1999-11-03 17:45:24 +00:00
|
|
|
|
context->lzw_firstcode = context->lzw_oldcode = retval;
|
|
|
|
|
} while (context->lzw_firstcode == context->lzw_clear_code);
|
|
|
|
|
return context->lzw_firstcode;
|
1999-10-30 19:28:53 +00:00
|
|
|
|
}
|
|
|
|
|
|
1999-11-03 21:22:38 +00:00
|
|
|
|
if (context->lzw_sp > context->lzw_stack) {
|
|
|
|
|
my_retval = *--(context->lzw_sp);
|
|
|
|
|
return my_retval;
|
|
|
|
|
}
|
1999-10-30 19:28:53 +00:00
|
|
|
|
|
1999-11-03 21:22:38 +00:00
|
|
|
|
while ((code = get_code (context, context->lzw_code_size)) >= 0) {
|
1999-11-03 17:45:24 +00:00
|
|
|
|
if (code == context->lzw_clear_code) {
|
|
|
|
|
for (i = 0; i < context->lzw_clear_code; ++i) {
|
|
|
|
|
context->lzw_table[0][i] = 0;
|
|
|
|
|
context->lzw_table[1][i] = i;
|
1999-10-30 19:28:53 +00:00
|
|
|
|
}
|
1999-11-03 17:45:24 +00:00
|
|
|
|
for (; i < (1 << MAX_LZW_BITS); ++i)
|
|
|
|
|
context->lzw_table[0][i] = context->lzw_table[1][i] = 0;
|
|
|
|
|
context->lzw_code_size = context->lzw_set_code_size + 1;
|
|
|
|
|
context->lzw_max_code_size = 2 * context->lzw_clear_code;
|
|
|
|
|
context->lzw_max_code = context->lzw_clear_code + 2;
|
|
|
|
|
context->lzw_sp = context->lzw_stack;
|
1999-11-03 21:22:38 +00:00
|
|
|
|
|
|
|
|
|
set_gif_lzw_clear_code (context);
|
|
|
|
|
return -3;
|
1999-11-03 17:45:24 +00:00
|
|
|
|
} else if (code == context->lzw_end_code) {
|
1999-10-30 19:28:53 +00:00
|
|
|
|
int count;
|
|
|
|
|
unsigned char buf[260];
|
|
|
|
|
|
1999-11-03 00:20:27 +00:00
|
|
|
|
/*g_error (" DID WE EVER EVER GET HERE\n");*/
|
2000-10-09 17:22:20 +00:00
|
|
|
|
g_warning ("Unhandled Case. If you have an image that causes this, let me <jrb@redhat.com> know.\n");
|
1999-11-03 21:22:38 +00:00
|
|
|
|
|
|
|
|
|
if (ZeroDataBlock) {
|
1999-10-30 19:28:53 +00:00
|
|
|
|
return -2;
|
1999-11-03 21:22:38 +00:00
|
|
|
|
}
|
1999-10-30 19:28:53 +00:00
|
|
|
|
|
1999-11-01 13:26:03 +00:00
|
|
|
|
while ((count = GetDataBlock (context, buf)) > 0)
|
1999-10-30 19:28:53 +00:00
|
|
|
|
;
|
|
|
|
|
|
1999-11-03 21:22:38 +00:00
|
|
|
|
if (count != 0) {
|
1999-10-30 19:28:53 +00:00
|
|
|
|
/*g_print (_("GIF: missing EOD in data stream (common occurence)"));*/
|
1999-11-03 21:22:38 +00:00
|
|
|
|
return -2;
|
|
|
|
|
}
|
1999-10-30 19:28:53 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
incode = code;
|
|
|
|
|
|
1999-11-03 17:45:24 +00:00
|
|
|
|
if (code >= context->lzw_max_code) {
|
|
|
|
|
*(context->lzw_sp)++ = context->lzw_firstcode;
|
|
|
|
|
code = context->lzw_oldcode;
|
1999-06-29 06:07:14 +00:00
|
|
|
|
}
|
1999-10-20 21:20:49 +00:00
|
|
|
|
|
1999-11-03 17:45:24 +00:00
|
|
|
|
while (code >= context->lzw_clear_code) {
|
|
|
|
|
*(context->lzw_sp)++ = context->lzw_table[1][code];
|
1999-11-03 21:22:38 +00:00
|
|
|
|
|
1999-11-03 17:45:24 +00:00
|
|
|
|
if (code == context->lzw_table[0][code]) {
|
2000-10-18 18:42:54 +00:00
|
|
|
|
g_set_error (context->error,
|
|
|
|
|
GDK_PIXBUF_ERROR,
|
|
|
|
|
GDK_PIXBUF_ERROR_CORRUPT_IMAGE,
|
|
|
|
|
_("Circular table entry in GIF file"));
|
1999-11-02 23:49:22 +00:00
|
|
|
|
return -2;
|
1999-10-18 19:29:45 +00:00
|
|
|
|
}
|
1999-11-03 17:45:24 +00:00
|
|
|
|
code = context->lzw_table[0][code];
|
1999-10-30 19:28:53 +00:00
|
|
|
|
}
|
1999-10-20 21:20:49 +00:00
|
|
|
|
|
1999-11-03 17:45:24 +00:00
|
|
|
|
*(context->lzw_sp)++ = context->lzw_firstcode = context->lzw_table[1][code];
|
1999-10-20 21:20:49 +00:00
|
|
|
|
|
1999-11-03 17:45:24 +00:00
|
|
|
|
if ((code = context->lzw_max_code) < (1 << MAX_LZW_BITS)) {
|
|
|
|
|
context->lzw_table[0][code] = context->lzw_oldcode;
|
|
|
|
|
context->lzw_table[1][code] = context->lzw_firstcode;
|
|
|
|
|
++context->lzw_max_code;
|
|
|
|
|
if ((context->lzw_max_code >= context->lzw_max_code_size) &&
|
|
|
|
|
(context->lzw_max_code_size < (1 << MAX_LZW_BITS))) {
|
|
|
|
|
context->lzw_max_code_size *= 2;
|
|
|
|
|
++context->lzw_code_size;
|
1999-10-18 19:29:45 +00:00
|
|
|
|
}
|
1999-10-30 19:28:53 +00:00
|
|
|
|
}
|
1999-10-20 21:20:49 +00:00
|
|
|
|
|
1999-11-03 17:45:24 +00:00
|
|
|
|
context->lzw_oldcode = incode;
|
1999-10-20 21:20:49 +00:00
|
|
|
|
|
1999-11-03 21:22:38 +00:00
|
|
|
|
if (context->lzw_sp > context->lzw_stack) {
|
|
|
|
|
my_retval = *--(context->lzw_sp);
|
|
|
|
|
return my_retval;
|
|
|
|
|
}
|
1999-10-30 19:28:53 +00:00
|
|
|
|
}
|
|
|
|
|
return code;
|
|
|
|
|
}
|
|
|
|
|
|
1999-10-30 22:04:27 +00:00
|
|
|
|
static void
|
1999-11-02 23:49:22 +00:00
|
|
|
|
gif_set_get_lzw (GifContext *context)
|
|
|
|
|
{
|
|
|
|
|
context->state = GIF_GET_LZW;
|
|
|
|
|
context->draw_xpos = 0;
|
|
|
|
|
context->draw_ypos = 0;
|
|
|
|
|
context->draw_pass = 0;
|
|
|
|
|
}
|
|
|
|
|
|
1999-11-03 00:20:27 +00:00
|
|
|
|
static void
|
|
|
|
|
gif_fill_in_pixels (GifContext *context, guchar *dest, gint offset, guchar v)
|
|
|
|
|
{
|
|
|
|
|
guchar *pixel = NULL;
|
|
|
|
|
|
2000-02-20 03:08:00 +00:00
|
|
|
|
if (context->gif89.transparent != -1) {
|
1999-11-03 00:20:27 +00:00
|
|
|
|
pixel = dest + (context->draw_ypos + offset) * gdk_pixbuf_get_rowstride (context->pixbuf) + context->draw_xpos * 4;
|
|
|
|
|
*pixel = context->color_map [0][(guchar) v];
|
|
|
|
|
*(pixel+1) = context->color_map [1][(guchar) v];
|
|
|
|
|
*(pixel+2) = context->color_map [2][(guchar) v];
|
|
|
|
|
*(pixel+3) = (guchar) ((v == context->gif89.transparent) ? 0 : 65535);
|
|
|
|
|
} else {
|
|
|
|
|
pixel = dest + (context->draw_ypos + offset) * gdk_pixbuf_get_rowstride (context->pixbuf) + context->draw_xpos * 3;
|
|
|
|
|
*pixel = context->color_map [0][(guchar) v];
|
|
|
|
|
*(pixel+1) = context->color_map [1][(guchar) v];
|
|
|
|
|
*(pixel+2) = context->color_map [2][(guchar) v];
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/* only called if progressive and interlaced */
|
|
|
|
|
static void
|
|
|
|
|
gif_fill_in_lines (GifContext *context, guchar *dest, guchar v)
|
|
|
|
|
{
|
|
|
|
|
switch (context->draw_pass) {
|
|
|
|
|
case 0:
|
|
|
|
|
if (context->draw_ypos > 4) {
|
|
|
|
|
gif_fill_in_pixels (context, dest, -4, v);
|
|
|
|
|
gif_fill_in_pixels (context, dest, -3, v);
|
|
|
|
|
}
|
|
|
|
|
if (context->draw_ypos < (context->frame_height - 4)) {
|
|
|
|
|
gif_fill_in_pixels (context, dest, 3, v);
|
|
|
|
|
gif_fill_in_pixels (context, dest, 4, v);
|
|
|
|
|
}
|
1999-12-03 21:57:07 +00:00
|
|
|
|
/* we don't need a break here. We draw the outer pixels first, then the
|
|
|
|
|
* inner ones, then the innermost ones. case 0 needs to draw all 3 bands.
|
|
|
|
|
* case 1, just the last two, and case 2 just draws the last one*/
|
1999-11-03 00:20:27 +00:00
|
|
|
|
case 1:
|
|
|
|
|
if (context->draw_ypos > 2)
|
|
|
|
|
gif_fill_in_pixels (context, dest, -2, v);
|
|
|
|
|
if (context->draw_ypos < (context->frame_height - 2))
|
|
|
|
|
gif_fill_in_pixels (context, dest, 2, v);
|
1999-12-03 21:57:07 +00:00
|
|
|
|
/* no break as above. */
|
1999-11-03 00:20:27 +00:00
|
|
|
|
case 2:
|
|
|
|
|
if (context->draw_ypos > 1)
|
|
|
|
|
gif_fill_in_pixels (context, dest, -1, v);
|
|
|
|
|
if (context->draw_ypos < (context->frame_height - 1))
|
|
|
|
|
gif_fill_in_pixels (context, dest, 1, v);
|
|
|
|
|
case 3:
|
|
|
|
|
default:
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
1999-11-02 23:49:22 +00:00
|
|
|
|
static int
|
1999-11-02 01:17:41 +00:00
|
|
|
|
gif_get_lzw (GifContext *context)
|
1999-10-30 19:28:53 +00:00
|
|
|
|
{
|
|
|
|
|
guchar *dest, *temp;
|
1999-11-04 20:34:24 +00:00
|
|
|
|
gint lower_bound, upper_bound; /* bounds for emitting the area_updated signal */
|
|
|
|
|
gboolean bound_flag;
|
|
|
|
|
gint first_pass; /* bounds for emitting the area_updated signal */
|
1999-10-30 19:28:53 +00:00
|
|
|
|
gint v;
|
|
|
|
|
|
1999-11-02 23:49:22 +00:00
|
|
|
|
if (context->pixbuf == NULL) {
|
2000-04-11 07:03:25 +00:00
|
|
|
|
context->pixbuf = gdk_pixbuf_new (GDK_COLORSPACE_RGB,
|
2000-02-20 03:08:00 +00:00
|
|
|
|
context->gif89.transparent != -1,
|
1999-11-02 23:49:22 +00:00
|
|
|
|
8,
|
2000-04-03 08:59:43 +00:00
|
|
|
|
context->frame_len,
|
|
|
|
|
context->frame_height);
|
1999-11-02 23:49:22 +00:00
|
|
|
|
|
1999-11-04 18:18:07 +00:00
|
|
|
|
if (context->prepare_func)
|
|
|
|
|
(* context->prepare_func) (context->pixbuf, context->user_data);
|
2000-04-03 08:59:43 +00:00
|
|
|
|
if (context->animation || context->frame_done_func || context->anim_done_func) {
|
1999-12-20 22:51:05 +00:00
|
|
|
|
context->frame = g_new (GdkPixbufFrame, 1);
|
2000-02-03 23:36:14 +00:00
|
|
|
|
context->frame->x_offset = context->x_offset;
|
|
|
|
|
context->frame->y_offset = context->y_offset;;
|
1999-12-20 22:51:05 +00:00
|
|
|
|
context->frame->delay_time = context->gif89.delay_time;
|
|
|
|
|
switch (context->gif89.disposal) {
|
|
|
|
|
case 0:
|
|
|
|
|
case 1:
|
|
|
|
|
context->frame->action = GDK_PIXBUF_FRAME_RETAIN;
|
|
|
|
|
break;
|
|
|
|
|
case 2:
|
|
|
|
|
context->frame->action = GDK_PIXBUF_FRAME_DISPOSE;
|
|
|
|
|
break;
|
|
|
|
|
case 3:
|
|
|
|
|
context->frame->action = GDK_PIXBUF_FRAME_REVERT;
|
|
|
|
|
break;
|
|
|
|
|
default:
|
|
|
|
|
context->frame->action = GDK_PIXBUF_FRAME_RETAIN;
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
context->frame->pixbuf = context->pixbuf;
|
2000-01-07 18:29:13 +00:00
|
|
|
|
if (context->animation) {
|
2000-04-03 08:59:43 +00:00
|
|
|
|
int w,h;
|
2000-01-07 18:29:13 +00:00
|
|
|
|
context->animation->n_frames ++;
|
|
|
|
|
context->animation->frames = g_list_append (context->animation->frames, context->frame);
|
2000-04-03 08:59:43 +00:00
|
|
|
|
w = gdk_pixbuf_get_width (context->pixbuf);
|
|
|
|
|
h = gdk_pixbuf_get_height (context->pixbuf);
|
|
|
|
|
if (w > context->animation->width)
|
|
|
|
|
context->animation->width = w;
|
|
|
|
|
if (h > context->animation->height)
|
|
|
|
|
context->animation->height = h;
|
2000-01-07 18:29:13 +00:00
|
|
|
|
}
|
1999-12-20 22:51:05 +00:00
|
|
|
|
}
|
1999-11-02 23:49:22 +00:00
|
|
|
|
}
|
1999-10-30 22:04:27 +00:00
|
|
|
|
dest = gdk_pixbuf_get_pixels (context->pixbuf);
|
1999-11-04 20:34:24 +00:00
|
|
|
|
|
|
|
|
|
bound_flag = FALSE;
|
|
|
|
|
lower_bound = upper_bound = context->draw_ypos;
|
|
|
|
|
first_pass = context->draw_pass;
|
|
|
|
|
|
1999-11-02 23:49:22 +00:00
|
|
|
|
while (TRUE) {
|
1999-11-03 17:45:24 +00:00
|
|
|
|
v = lzw_read_byte (context);
|
1999-11-02 23:49:22 +00:00
|
|
|
|
if (v < 0) {
|
1999-11-04 20:34:24 +00:00
|
|
|
|
goto finished_data;
|
1999-11-02 23:49:22 +00:00
|
|
|
|
}
|
1999-11-04 20:34:24 +00:00
|
|
|
|
bound_flag = TRUE;
|
1999-11-02 23:49:22 +00:00
|
|
|
|
|
2000-02-20 03:08:00 +00:00
|
|
|
|
if (context->gif89.transparent != -1) {
|
1999-11-02 23:49:22 +00:00
|
|
|
|
temp = dest + context->draw_ypos * gdk_pixbuf_get_rowstride (context->pixbuf) + context->draw_xpos * 4;
|
1999-11-01 13:26:03 +00:00
|
|
|
|
*temp = context->color_map [0][(guchar) v];
|
|
|
|
|
*(temp+1) = context->color_map [1][(guchar) v];
|
|
|
|
|
*(temp+2) = context->color_map [2][(guchar) v];
|
1999-11-03 22:19:42 +00:00
|
|
|
|
*(temp+3) = (guchar) ((v == context->gif89.transparent) ? 0 : -1);
|
1999-10-30 19:28:53 +00:00
|
|
|
|
} else {
|
1999-11-02 23:49:22 +00:00
|
|
|
|
temp = dest + context->draw_ypos * gdk_pixbuf_get_rowstride (context->pixbuf) + context->draw_xpos * 3;
|
1999-11-01 13:26:03 +00:00
|
|
|
|
*temp = context->color_map [0][(guchar) v];
|
|
|
|
|
*(temp+1) = context->color_map [1][(guchar) v];
|
|
|
|
|
*(temp+2) = context->color_map [2][(guchar) v];
|
1999-10-30 19:28:53 +00:00
|
|
|
|
}
|
|
|
|
|
|
1999-11-04 18:18:07 +00:00
|
|
|
|
if (context->prepare_func && context->frame_interlace)
|
1999-11-03 00:20:27 +00:00
|
|
|
|
gif_fill_in_lines (context, dest, v);
|
1999-11-03 21:22:38 +00:00
|
|
|
|
|
1999-11-02 23:49:22 +00:00
|
|
|
|
context->draw_xpos++;
|
1999-11-04 20:34:24 +00:00
|
|
|
|
|
1999-11-02 23:49:22 +00:00
|
|
|
|
if (context->draw_xpos == context->frame_len) {
|
|
|
|
|
context->draw_xpos = 0;
|
1999-11-01 13:26:03 +00:00
|
|
|
|
if (context->frame_interlace) {
|
1999-11-02 23:49:22 +00:00
|
|
|
|
switch (context->draw_pass) {
|
1999-10-30 19:28:53 +00:00
|
|
|
|
case 0:
|
|
|
|
|
case 1:
|
1999-11-02 23:49:22 +00:00
|
|
|
|
context->draw_ypos += 8;
|
1999-10-30 19:28:53 +00:00
|
|
|
|
break;
|
|
|
|
|
case 2:
|
1999-11-02 23:49:22 +00:00
|
|
|
|
context->draw_ypos += 4;
|
1999-10-30 19:28:53 +00:00
|
|
|
|
break;
|
|
|
|
|
case 3:
|
1999-11-02 23:49:22 +00:00
|
|
|
|
context->draw_ypos += 2;
|
1999-10-30 19:28:53 +00:00
|
|
|
|
break;
|
1999-10-18 19:29:45 +00:00
|
|
|
|
}
|
1999-10-30 19:28:53 +00:00
|
|
|
|
|
1999-11-02 23:49:22 +00:00
|
|
|
|
if (context->draw_ypos >= context->frame_height) {
|
|
|
|
|
context->draw_pass++;
|
|
|
|
|
switch (context->draw_pass) {
|
1999-10-30 19:28:53 +00:00
|
|
|
|
case 1:
|
1999-11-02 23:49:22 +00:00
|
|
|
|
context->draw_ypos = 4;
|
1999-10-30 19:28:53 +00:00
|
|
|
|
break;
|
|
|
|
|
case 2:
|
1999-11-02 23:49:22 +00:00
|
|
|
|
context->draw_ypos = 2;
|
1999-10-30 19:28:53 +00:00
|
|
|
|
break;
|
|
|
|
|
case 3:
|
1999-11-02 23:49:22 +00:00
|
|
|
|
context->draw_ypos = 1;
|
1999-10-30 19:28:53 +00:00
|
|
|
|
break;
|
|
|
|
|
default:
|
1999-11-02 01:17:41 +00:00
|
|
|
|
goto done;
|
1999-10-30 19:28:53 +00:00
|
|
|
|
}
|
1999-10-18 19:29:45 +00:00
|
|
|
|
}
|
1999-10-30 19:28:53 +00:00
|
|
|
|
} else {
|
1999-11-02 23:49:22 +00:00
|
|
|
|
context->draw_ypos++;
|
1999-10-30 19:28:53 +00:00
|
|
|
|
}
|
1999-11-04 20:34:24 +00:00
|
|
|
|
if (context->draw_pass != first_pass) {
|
|
|
|
|
if (context->draw_ypos > lower_bound) {
|
|
|
|
|
lower_bound = 0;
|
|
|
|
|
upper_bound = context->frame_height;
|
|
|
|
|
} else {
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
} else
|
|
|
|
|
upper_bound = context->draw_ypos;
|
1999-06-29 06:07:14 +00:00
|
|
|
|
}
|
1999-11-02 23:49:22 +00:00
|
|
|
|
if (context->draw_ypos >= context->frame_height)
|
1999-10-30 19:28:53 +00:00
|
|
|
|
break;
|
|
|
|
|
}
|
1999-11-02 01:17:41 +00:00
|
|
|
|
done:
|
|
|
|
|
/* we got enough data. there may be more (ie, newer layers) but we can quit now */
|
2000-04-03 08:59:43 +00:00
|
|
|
|
if (context->animation || context->frame_done_func || context->anim_done_func) {
|
1999-12-20 22:51:05 +00:00
|
|
|
|
context->state = GIF_GET_NEXT_STEP;
|
|
|
|
|
} else
|
|
|
|
|
context->state = GIF_DONE;
|
1999-11-04 20:34:24 +00:00
|
|
|
|
v = 0;
|
|
|
|
|
finished_data:
|
|
|
|
|
if (bound_flag && context->update_func) {
|
|
|
|
|
if (lower_bound <= upper_bound && first_pass == context->draw_pass) {
|
|
|
|
|
(* context->update_func)
|
|
|
|
|
(context->pixbuf,
|
|
|
|
|
0, lower_bound,
|
|
|
|
|
gdk_pixbuf_get_width (context->pixbuf),
|
2000-01-05 23:06:13 +00:00
|
|
|
|
upper_bound - lower_bound,
|
|
|
|
|
context->user_data);
|
1999-11-04 20:34:24 +00:00
|
|
|
|
} else {
|
|
|
|
|
if (lower_bound <= upper_bound) {
|
|
|
|
|
(* context->update_func)
|
|
|
|
|
(context->pixbuf,
|
|
|
|
|
0, 0,
|
|
|
|
|
gdk_pixbuf_get_width (context->pixbuf),
|
2000-01-05 23:06:13 +00:00
|
|
|
|
gdk_pixbuf_get_height (context->pixbuf),
|
|
|
|
|
context->user_data);
|
1999-11-04 20:34:24 +00:00
|
|
|
|
} else {
|
|
|
|
|
(* context->update_func)
|
|
|
|
|
(context->pixbuf,
|
|
|
|
|
0, 0,
|
|
|
|
|
gdk_pixbuf_get_width (context->pixbuf),
|
2000-01-05 23:06:13 +00:00
|
|
|
|
upper_bound,
|
|
|
|
|
context->user_data);
|
1999-11-04 20:34:24 +00:00
|
|
|
|
(* context->update_func)
|
|
|
|
|
(context->pixbuf,
|
|
|
|
|
0, lower_bound,
|
|
|
|
|
gdk_pixbuf_get_width (context->pixbuf),
|
2000-01-05 23:06:13 +00:00
|
|
|
|
gdk_pixbuf_get_height (context->pixbuf),
|
|
|
|
|
context->user_data);
|
1999-11-04 20:34:24 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2000-04-03 08:59:43 +00:00
|
|
|
|
if ((context->animation || context->frame_done_func || context->anim_done_func)
|
|
|
|
|
&& context->state == GIF_GET_NEXT_STEP) {
|
2000-01-07 19:18:54 +00:00
|
|
|
|
if (context->frame_done_func)
|
|
|
|
|
(* context->frame_done_func) (context->frame,
|
|
|
|
|
context->user_data);
|
|
|
|
|
if (context->frame_done_func)
|
|
|
|
|
gdk_pixbuf_unref (context->pixbuf);
|
1999-12-20 22:51:05 +00:00
|
|
|
|
context->pixbuf = NULL;
|
|
|
|
|
context->frame = NULL;
|
|
|
|
|
}
|
|
|
|
|
|
1999-11-04 20:34:24 +00:00
|
|
|
|
return v;
|
1999-10-30 19:28:53 +00:00
|
|
|
|
}
|
1999-10-20 21:20:49 +00:00
|
|
|
|
|
1999-11-02 23:49:22 +00:00
|
|
|
|
static void
|
|
|
|
|
gif_set_prepare_lzw (GifContext *context)
|
|
|
|
|
{
|
|
|
|
|
context->state = GIF_PREPARE_LZW;
|
1999-11-03 21:22:38 +00:00
|
|
|
|
context->lzw_code_pending = -1;
|
1999-11-02 23:49:22 +00:00
|
|
|
|
}
|
1999-11-02 01:17:41 +00:00
|
|
|
|
static int
|
|
|
|
|
gif_prepare_lzw (GifContext *context)
|
|
|
|
|
{
|
|
|
|
|
gint i;
|
|
|
|
|
|
1999-11-03 23:22:27 +00:00
|
|
|
|
if (!gif_read (context, &(context->lzw_set_code_size), 1)) {
|
1999-11-02 01:17:41 +00:00
|
|
|
|
/*g_message (_("GIF: EOF / read error on image data\n"));*/
|
|
|
|
|
return -1;
|
|
|
|
|
}
|
|
|
|
|
|
1999-11-03 17:45:24 +00:00
|
|
|
|
context->lzw_code_size = context->lzw_set_code_size + 1;
|
|
|
|
|
context->lzw_clear_code = 1 << context->lzw_set_code_size;
|
|
|
|
|
context->lzw_end_code = context->lzw_clear_code + 1;
|
|
|
|
|
context->lzw_max_code_size = 2 * context->lzw_clear_code;
|
|
|
|
|
context->lzw_max_code = context->lzw_clear_code + 2;
|
|
|
|
|
context->lzw_fresh = TRUE;
|
1999-11-02 23:49:22 +00:00
|
|
|
|
context->code_curbit = 0;
|
|
|
|
|
context->code_lastbit = 0;
|
1999-11-03 16:40:43 +00:00
|
|
|
|
context->code_last_byte = 0;
|
1999-11-02 23:49:22 +00:00
|
|
|
|
context->code_done = FALSE;
|
1999-11-02 01:17:41 +00:00
|
|
|
|
|
1999-11-03 17:45:24 +00:00
|
|
|
|
for (i = 0; i < context->lzw_clear_code; ++i) {
|
|
|
|
|
context->lzw_table[0][i] = 0;
|
|
|
|
|
context->lzw_table[1][i] = i;
|
1999-11-02 01:17:41 +00:00
|
|
|
|
}
|
1999-11-03 17:45:24 +00:00
|
|
|
|
for (; i < (1 << MAX_LZW_BITS); ++i)
|
|
|
|
|
context->lzw_table[0][i] = context->lzw_table[1][0] = 0;
|
1999-11-01 13:26:03 +00:00
|
|
|
|
|
1999-11-03 17:45:24 +00:00
|
|
|
|
context->lzw_sp = context->lzw_stack;
|
1999-11-02 23:49:22 +00:00
|
|
|
|
gif_set_get_lzw (context);
|
|
|
|
|
|
1999-11-02 01:17:41 +00:00
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* needs 13 bytes to proceed. */
|
1999-11-01 13:26:03 +00:00
|
|
|
|
static gint
|
1999-11-02 01:17:41 +00:00
|
|
|
|
gif_init (GifContext *context)
|
1999-10-30 19:28:53 +00:00
|
|
|
|
{
|
|
|
|
|
unsigned char buf[16];
|
|
|
|
|
char version[4];
|
|
|
|
|
|
1999-11-03 23:22:27 +00:00
|
|
|
|
if (!gif_read (context, buf, 6)) {
|
2000-10-18 18:42:54 +00:00
|
|
|
|
/* Unable to read magic number,
|
|
|
|
|
* gif_read() should have set error
|
|
|
|
|
*/
|
1999-11-02 01:17:41 +00:00
|
|
|
|
return -1;
|
1999-10-20 21:20:49 +00:00
|
|
|
|
}
|
1999-10-18 19:29:45 +00:00
|
|
|
|
|
1999-10-30 19:28:53 +00:00
|
|
|
|
if (strncmp ((char *) buf, "GIF", 3) != 0) {
|
|
|
|
|
/* Not a GIF file */
|
2000-10-18 18:42:54 +00:00
|
|
|
|
g_set_error (context->error,
|
|
|
|
|
GDK_PIXBUF_ERROR,
|
|
|
|
|
GDK_PIXBUF_ERROR_CORRUPT_IMAGE,
|
|
|
|
|
_("File does not appear to be a GIF file"));
|
1999-11-01 13:26:03 +00:00
|
|
|
|
return -1;
|
1999-10-30 19:28:53 +00:00
|
|
|
|
}
|
1999-10-18 19:29:45 +00:00
|
|
|
|
|
1999-10-30 19:28:53 +00:00
|
|
|
|
strncpy (version, (char *) buf + 3, 3);
|
|
|
|
|
version[3] = '\0';
|
1999-10-18 19:29:45 +00:00
|
|
|
|
|
1999-10-30 19:28:53 +00:00
|
|
|
|
if ((strcmp (version, "87a") != 0) && (strcmp (version, "89a") != 0)) {
|
|
|
|
|
/* bad version number, not '87a' or '89a' */
|
2000-10-18 18:42:54 +00:00
|
|
|
|
g_set_error (context->error,
|
|
|
|
|
GDK_PIXBUF_ERROR,
|
|
|
|
|
GDK_PIXBUF_ERROR_CORRUPT_IMAGE,
|
|
|
|
|
_("Version %s of the GIF file format is not supported"),
|
|
|
|
|
version);
|
1999-11-01 13:26:03 +00:00
|
|
|
|
return -1;
|
1999-10-30 19:28:53 +00:00
|
|
|
|
}
|
1999-06-29 06:07:14 +00:00
|
|
|
|
|
1999-10-30 19:28:53 +00:00
|
|
|
|
/* read the screen descriptor */
|
1999-11-03 23:22:27 +00:00
|
|
|
|
if (!gif_read (context, buf, 7)) {
|
2000-10-18 18:42:54 +00:00
|
|
|
|
/* Failed to read screen descriptor, error set */
|
1999-11-02 01:17:41 +00:00
|
|
|
|
return -1;
|
1999-10-30 19:28:53 +00:00
|
|
|
|
}
|
1999-09-22 22:30:51 +00:00
|
|
|
|
|
1999-10-30 19:28:53 +00:00
|
|
|
|
context->width = LM_to_uint (buf[0], buf[1]);
|
|
|
|
|
context->height = LM_to_uint (buf[2], buf[3]);
|
|
|
|
|
context->bit_pixel = 2 << (buf[4] & 0x07);
|
|
|
|
|
context->color_resolution = (((buf[4] & 0x70) >> 3) + 1);
|
|
|
|
|
context->background = buf[5];
|
|
|
|
|
context->aspect_ratio = buf[6];
|
|
|
|
|
|
|
|
|
|
if (BitSet (buf[4], LOCALCOLORMAP)) {
|
1999-11-02 01:17:41 +00:00
|
|
|
|
gif_set_get_colormap (context);
|
|
|
|
|
} else {
|
|
|
|
|
context->state = GIF_GET_NEXT_STEP;
|
1999-10-30 19:28:53 +00:00
|
|
|
|
}
|
1999-11-02 01:17:41 +00:00
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
1999-11-02 23:49:22 +00:00
|
|
|
|
static void
|
|
|
|
|
gif_set_get_frame_info (GifContext *context)
|
|
|
|
|
{
|
|
|
|
|
context->state = GIF_GET_FRAME_INFO;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static gint
|
|
|
|
|
gif_get_frame_info (GifContext *context)
|
|
|
|
|
{
|
|
|
|
|
unsigned char buf[9];
|
1999-11-03 23:22:27 +00:00
|
|
|
|
if (!gif_read (context, buf, 9)) {
|
1999-11-02 23:49:22 +00:00
|
|
|
|
return -1;
|
|
|
|
|
}
|
|
|
|
|
/* Okay, we got all the info we need. Lets record it */
|
|
|
|
|
context->frame_len = LM_to_uint (buf[4], buf[5]);
|
|
|
|
|
context->frame_height = LM_to_uint (buf[6], buf[7]);
|
2000-02-03 23:36:14 +00:00
|
|
|
|
context->x_offset = LM_to_uint (buf[0], buf[1]);
|
|
|
|
|
context->y_offset = LM_to_uint (buf[2], buf[3]);
|
|
|
|
|
|
1999-11-02 23:49:22 +00:00
|
|
|
|
if (context->frame_height > context->height) {
|
1999-12-20 22:51:05 +00:00
|
|
|
|
/* we don't want to resize things. So we exit */
|
1999-11-02 23:49:22 +00:00
|
|
|
|
context->state = GIF_DONE;
|
2000-10-18 18:42:54 +00:00
|
|
|
|
|
|
|
|
|
g_set_error (context->error,
|
|
|
|
|
GDK_PIXBUF_ERROR,
|
|
|
|
|
GDK_PIXBUF_ERROR_CORRUPT_IMAGE,
|
|
|
|
|
_("GIF animation contained a frame with an incorrect size"));
|
|
|
|
|
|
1999-11-02 23:49:22 +00:00
|
|
|
|
return -2;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
context->frame_interlace = BitSet (buf[8], INTERLACE);
|
|
|
|
|
if (BitSet (buf[8], LOCALCOLORMAP)) {
|
|
|
|
|
/* Does this frame have it's own colormap. */
|
|
|
|
|
/* really only relevant when looking at the first frame
|
|
|
|
|
* of an animated gif. */
|
|
|
|
|
/* if it does, we need to re-read in the colormap,
|
|
|
|
|
* the gray_scale, and the bit_pixel */
|
|
|
|
|
context->bit_pixel = 1 << ((buf[8] & 0x07) + 1);
|
|
|
|
|
gif_set_get_colormap2 (context);
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
gif_set_prepare_lzw (context);
|
|
|
|
|
return 0;
|
|
|
|
|
|
1999-11-03 21:22:38 +00:00
|
|
|
|
}
|
1999-10-20 21:20:49 +00:00
|
|
|
|
|
1999-11-02 01:17:41 +00:00
|
|
|
|
static gint
|
|
|
|
|
gif_get_next_step (GifContext *context)
|
|
|
|
|
{
|
|
|
|
|
unsigned char c;
|
1999-11-02 23:49:22 +00:00
|
|
|
|
while (TRUE) {
|
1999-11-03 23:22:27 +00:00
|
|
|
|
if (!gif_read (context, &c, 1)) {
|
1999-11-02 01:17:41 +00:00
|
|
|
|
return -1;
|
1999-10-30 19:28:53 +00:00
|
|
|
|
}
|
|
|
|
|
if (c == ';') {
|
|
|
|
|
/* GIF terminator */
|
1999-11-01 13:26:03 +00:00
|
|
|
|
/* hmm. Not 100% sure what to do about this. Should
|
|
|
|
|
* i try to return a blank image instead? */
|
1999-11-02 01:17:41 +00:00
|
|
|
|
context->state = GIF_DONE;
|
2000-10-09 17:22:20 +00:00
|
|
|
|
return 0;
|
1999-10-30 19:28:53 +00:00
|
|
|
|
}
|
1999-10-28 14:46:46 +00:00
|
|
|
|
|
1999-10-30 19:28:53 +00:00
|
|
|
|
if (c == '!') {
|
1999-11-02 01:17:41 +00:00
|
|
|
|
/* Check the extention */
|
|
|
|
|
gif_set_get_extension (context);
|
1999-11-03 05:12:02 +00:00
|
|
|
|
return 0;
|
1999-10-30 19:28:53 +00:00
|
|
|
|
}
|
1999-11-02 01:17:41 +00:00
|
|
|
|
|
|
|
|
|
/* look for frame */
|
1999-10-30 19:28:53 +00:00
|
|
|
|
if (c != ',') {
|
1999-11-02 01:17:41 +00:00
|
|
|
|
/* Not a valid start character */
|
1999-10-30 19:28:53 +00:00
|
|
|
|
continue;
|
|
|
|
|
}
|
1999-11-03 21:22:38 +00:00
|
|
|
|
/* load the frame */
|
1999-11-02 23:49:22 +00:00
|
|
|
|
gif_set_get_frame_info (context);
|
1999-11-02 01:17:41 +00:00
|
|
|
|
return 0;
|
1999-10-30 19:28:53 +00:00
|
|
|
|
}
|
1999-11-02 01:17:41 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
static gint
|
|
|
|
|
gif_main_loop (GifContext *context)
|
|
|
|
|
{
|
|
|
|
|
gint retval = 0;
|
|
|
|
|
|
|
|
|
|
do {
|
|
|
|
|
switch (context->state) {
|
|
|
|
|
case GIF_START:
|
|
|
|
|
retval = gif_init (context);
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
case GIF_GET_COLORMAP:
|
|
|
|
|
retval = gif_get_colormap (context);
|
|
|
|
|
if (retval == 0)
|
|
|
|
|
context->state = GIF_GET_NEXT_STEP;
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
case GIF_GET_NEXT_STEP:
|
|
|
|
|
retval = gif_get_next_step (context);
|
|
|
|
|
break;
|
|
|
|
|
|
1999-11-02 23:49:22 +00:00
|
|
|
|
case GIF_GET_FRAME_INFO:
|
|
|
|
|
retval = gif_get_frame_info (context);
|
|
|
|
|
break;
|
|
|
|
|
|
1999-11-02 01:17:41 +00:00
|
|
|
|
case GIF_GET_EXTENTION:
|
|
|
|
|
retval = gif_get_extension (context);
|
|
|
|
|
if (retval == 0)
|
|
|
|
|
context->state = GIF_GET_NEXT_STEP;
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
case GIF_GET_COLORMAP2:
|
|
|
|
|
retval = gif_get_colormap (context);
|
|
|
|
|
if (retval == 0)
|
1999-11-02 23:49:22 +00:00
|
|
|
|
gif_set_prepare_lzw (context);
|
1999-11-02 01:17:41 +00:00
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
case GIF_PREPARE_LZW:
|
|
|
|
|
retval = gif_prepare_lzw (context);
|
|
|
|
|
break;
|
|
|
|
|
|
1999-11-02 23:49:22 +00:00
|
|
|
|
case GIF_LZW_FILL_BUFFER:
|
|
|
|
|
retval = gif_lzw_fill_buffer (context);
|
|
|
|
|
break;
|
1999-11-03 21:22:38 +00:00
|
|
|
|
|
|
|
|
|
case GIF_LZW_CLEAR_CODE:
|
|
|
|
|
retval = gif_lzw_clear_code (context);
|
|
|
|
|
break;
|
|
|
|
|
|
1999-11-02 01:17:41 +00:00
|
|
|
|
case GIF_GET_LZW:
|
1999-11-02 23:49:22 +00:00
|
|
|
|
retval = gif_get_lzw (context);
|
1999-11-02 01:17:41 +00:00
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
case GIF_DONE:
|
|
|
|
|
default:
|
|
|
|
|
retval = 0;
|
|
|
|
|
goto done;
|
|
|
|
|
};
|
1999-11-02 23:49:22 +00:00
|
|
|
|
} while ((retval == 0) || (retval == -3));
|
1999-11-02 01:17:41 +00:00
|
|
|
|
done:
|
|
|
|
|
return retval;
|
1999-11-01 13:26:03 +00:00
|
|
|
|
}
|
|
|
|
|
|
1999-11-10 21:06:40 +00:00
|
|
|
|
static GifContext *
|
|
|
|
|
new_context (void)
|
|
|
|
|
{
|
|
|
|
|
GifContext *context;
|
|
|
|
|
|
1999-12-24 02:21:33 +00:00
|
|
|
|
context = g_new0 (GifContext, 1);
|
2000-02-03 23:36:14 +00:00
|
|
|
|
|
1999-11-10 21:06:40 +00:00
|
|
|
|
context->pixbuf = NULL;
|
|
|
|
|
context->file = NULL;
|
|
|
|
|
context->state = GIF_START;
|
|
|
|
|
context->prepare_func = NULL;
|
|
|
|
|
context->update_func = NULL;
|
2000-01-07 18:29:13 +00:00
|
|
|
|
context->frame_done_func = NULL;
|
|
|
|
|
context->anim_done_func = NULL;
|
1999-11-10 21:06:40 +00:00
|
|
|
|
context->user_data = NULL;
|
|
|
|
|
context->buf = NULL;
|
|
|
|
|
context->amount_needed = 0;
|
|
|
|
|
context->gif89.transparent = -1;
|
|
|
|
|
context->gif89.delay_time = -1;
|
|
|
|
|
context->gif89.input_flag = -1;
|
|
|
|
|
context->gif89.disposal = -1;
|
2000-02-03 23:36:14 +00:00
|
|
|
|
|
1999-11-10 21:06:40 +00:00
|
|
|
|
return context;
|
|
|
|
|
}
|
1999-11-01 13:26:03 +00:00
|
|
|
|
/* Shared library entry point */
|
|
|
|
|
GdkPixbuf *
|
2000-10-18 18:42:54 +00:00
|
|
|
|
gdk_pixbuf__gif_image_load (FILE *file, GError **error)
|
1999-11-01 13:26:03 +00:00
|
|
|
|
{
|
|
|
|
|
GifContext *context;
|
2000-03-09 00:11:30 +00:00
|
|
|
|
GdkPixbuf *pixbuf;
|
1999-11-01 13:26:03 +00:00
|
|
|
|
|
|
|
|
|
g_return_val_if_fail (file != NULL, NULL);
|
1999-10-28 14:46:46 +00:00
|
|
|
|
|
1999-11-10 21:06:40 +00:00
|
|
|
|
context = new_context ();
|
1999-11-01 13:26:03 +00:00
|
|
|
|
context->file = file;
|
2000-10-18 18:42:54 +00:00
|
|
|
|
context->error = error;
|
|
|
|
|
|
1999-11-02 01:17:41 +00:00
|
|
|
|
gif_main_loop (context);
|
1999-11-01 13:26:03 +00:00
|
|
|
|
|
2000-03-09 00:11:30 +00:00
|
|
|
|
pixbuf = context->pixbuf;
|
|
|
|
|
g_free (context);
|
|
|
|
|
|
|
|
|
|
return pixbuf;
|
1999-10-28 14:46:46 +00:00
|
|
|
|
}
|
|
|
|
|
|
1999-11-01 13:26:03 +00:00
|
|
|
|
gpointer
|
2000-02-22 00:29:00 +00:00
|
|
|
|
gdk_pixbuf__gif_image_begin_load (ModulePreparedNotifyFunc prepare_func,
|
|
|
|
|
ModuleUpdatedNotifyFunc update_func,
|
|
|
|
|
ModuleFrameDoneNotifyFunc frame_done_func,
|
|
|
|
|
ModuleAnimationDoneNotifyFunc anim_done_func,
|
2000-10-18 18:42:54 +00:00
|
|
|
|
gpointer user_data,
|
|
|
|
|
GError **error)
|
1999-11-01 13:26:03 +00:00
|
|
|
|
{
|
|
|
|
|
GifContext *context;
|
|
|
|
|
|
1999-11-03 22:19:42 +00:00
|
|
|
|
#ifdef IO_GIFDEBUG
|
1999-11-02 23:49:22 +00:00
|
|
|
|
count = 0;
|
1999-11-03 22:19:42 +00:00
|
|
|
|
#endif
|
1999-11-10 21:06:40 +00:00
|
|
|
|
context = new_context ();
|
2000-10-18 18:42:54 +00:00
|
|
|
|
context->error = error;
|
1999-11-04 18:18:07 +00:00
|
|
|
|
context->prepare_func = prepare_func;
|
1999-11-04 20:34:24 +00:00
|
|
|
|
context->update_func = update_func;
|
2000-01-07 18:29:13 +00:00
|
|
|
|
context->frame_done_func = frame_done_func;
|
|
|
|
|
context->anim_done_func = anim_done_func;
|
1999-11-01 13:26:03 +00:00
|
|
|
|
context->user_data = user_data;
|
1999-11-10 21:06:40 +00:00
|
|
|
|
|
1999-11-01 13:26:03 +00:00
|
|
|
|
return (gpointer) context;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void
|
2000-02-22 00:29:00 +00:00
|
|
|
|
gdk_pixbuf__gif_image_stop_load (gpointer data)
|
1999-11-01 13:26:03 +00:00
|
|
|
|
{
|
|
|
|
|
GifContext *context = (GifContext *) data;
|
1999-10-28 14:46:46 +00:00
|
|
|
|
|
2000-01-02 03:59:22 +00:00
|
|
|
|
/* FIXME: free the animation data */
|
|
|
|
|
|
1999-11-01 13:26:03 +00:00
|
|
|
|
if (context->pixbuf)
|
|
|
|
|
gdk_pixbuf_unref (context->pixbuf);
|
2000-01-07 18:29:13 +00:00
|
|
|
|
if (context->animation)
|
|
|
|
|
gdk_pixbuf_animation_unref (context->animation);
|
|
|
|
|
/* g_free (context->buf);*/
|
1999-11-01 13:26:03 +00:00
|
|
|
|
g_free (context);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
gboolean
|
2000-10-18 18:42:54 +00:00
|
|
|
|
gdk_pixbuf__gif_image_load_increment (gpointer data, guchar *buf, guint size,
|
|
|
|
|
GError **error)
|
1999-11-01 13:26:03 +00:00
|
|
|
|
{
|
1999-11-02 01:17:41 +00:00
|
|
|
|
gint retval;
|
1999-11-01 13:26:03 +00:00
|
|
|
|
GifContext *context = (GifContext *) data;
|
|
|
|
|
|
2000-10-18 18:42:54 +00:00
|
|
|
|
context->error = error;
|
|
|
|
|
|
1999-11-02 01:17:41 +00:00
|
|
|
|
if (context->amount_needed == 0) {
|
|
|
|
|
/* we aren't looking for some bytes. */
|
1999-11-02 23:49:22 +00:00
|
|
|
|
/* we can use buf now, but we don't want to keep it around at all.
|
|
|
|
|
* it will be gone by the end of the call. */
|
1999-11-02 01:17:41 +00:00
|
|
|
|
context->buf = buf;
|
|
|
|
|
context->ptr = 0;
|
|
|
|
|
context->size = size;
|
1999-11-02 23:49:22 +00:00
|
|
|
|
} else {
|
|
|
|
|
/* we need some bytes */
|
|
|
|
|
if (size < context->amount_needed) {
|
|
|
|
|
context->amount_needed -= size;
|
|
|
|
|
/* copy it over and return */
|
|
|
|
|
memcpy (context->buf + context->size, buf, size);
|
|
|
|
|
context->size += size;
|
|
|
|
|
return TRUE;
|
|
|
|
|
} else if (size == context->amount_needed) {
|
|
|
|
|
memcpy (context->buf + context->size, buf, size);
|
|
|
|
|
context->size += size;
|
|
|
|
|
} else {
|
|
|
|
|
context->buf = g_realloc (context->buf, context->size + size);
|
|
|
|
|
memcpy (context->buf + context->size, buf, size);
|
|
|
|
|
context->size += size;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
1999-11-02 01:17:41 +00:00
|
|
|
|
retval = gif_main_loop (context);
|
1999-11-02 23:49:22 +00:00
|
|
|
|
|
|
|
|
|
if (retval == -2)
|
1999-11-02 01:17:41 +00:00
|
|
|
|
return FALSE;
|
|
|
|
|
if (retval == -1) {
|
1999-11-02 23:49:22 +00:00
|
|
|
|
/* we didn't have enough memory */
|
|
|
|
|
/* prepare for the next image_load_increment */
|
|
|
|
|
if (context->buf == buf) {
|
|
|
|
|
g_assert (context->size == size);
|
|
|
|
|
context->buf = (guchar *)g_new (guchar, context->amount_needed + (context->size - context->ptr));
|
|
|
|
|
memcpy (context->buf, buf + context->ptr, context->size - context->ptr);
|
|
|
|
|
} else {
|
|
|
|
|
/* copy the left overs to the begining of the buffer */
|
|
|
|
|
/* and realloc the memory */
|
1999-11-03 21:52:16 +00:00
|
|
|
|
memmove (context->buf, context->buf + context->ptr, context->size - context->ptr);
|
1999-11-02 23:49:22 +00:00
|
|
|
|
context->buf = g_realloc (context->buf, context->amount_needed + (context->size - context->ptr));
|
|
|
|
|
}
|
|
|
|
|
context->size = context->size - context->ptr;
|
|
|
|
|
context->ptr = 0;
|
|
|
|
|
} else {
|
|
|
|
|
/* we are prolly all done */
|
|
|
|
|
if (context->buf == buf)
|
|
|
|
|
context->buf = NULL;
|
1999-11-02 01:17:41 +00:00
|
|
|
|
}
|
|
|
|
|
return TRUE;
|
1999-11-01 13:26:03 +00:00
|
|
|
|
}
|
1999-12-20 22:51:05 +00:00
|
|
|
|
|
|
|
|
|
GdkPixbufAnimation *
|
2000-10-18 18:42:54 +00:00
|
|
|
|
gdk_pixbuf__gif_image_load_animation (FILE *file,
|
|
|
|
|
GError **error)
|
1999-12-20 22:51:05 +00:00
|
|
|
|
{
|
|
|
|
|
GifContext *context;
|
2000-03-09 00:11:30 +00:00
|
|
|
|
GdkPixbufAnimation *animation;
|
1999-12-20 22:51:05 +00:00
|
|
|
|
|
|
|
|
|
g_return_val_if_fail (file != NULL, NULL);
|
|
|
|
|
|
|
|
|
|
context = new_context ();
|
2000-10-18 18:42:54 +00:00
|
|
|
|
|
|
|
|
|
context->error = error;
|
|
|
|
|
|
2000-07-11 04:46:11 +00:00
|
|
|
|
context->animation = g_object_new (GDK_TYPE_PIXBUF_ANIMATION, NULL);
|
2000-06-22 15:36:12 +00:00
|
|
|
|
|
1999-12-20 22:51:05 +00:00
|
|
|
|
context->animation->n_frames = 0;
|
|
|
|
|
context->animation->frames = NULL;
|
2000-04-03 08:59:43 +00:00
|
|
|
|
context->animation->width = 0;
|
|
|
|
|
context->animation->height = 0;
|
1999-12-20 22:51:05 +00:00
|
|
|
|
context->file = file;
|
|
|
|
|
|
|
|
|
|
gif_main_loop (context);
|
2000-03-09 00:11:30 +00:00
|
|
|
|
|
|
|
|
|
animation = context->animation;
|
|
|
|
|
g_free (context);
|
|
|
|
|
return animation;
|
1999-12-20 22:51:05 +00:00
|
|
|
|
}
|