📄 rdgif.c
字号:
/*
* rdgif.c
*
* Copyright (C) 1991-1996, Thomas G. Lane.
* This file is part of the Independent JPEG Group's software.
* For conditions of distribution and use, see the accompanying README file.
*
**************************************************************************
* WARNING: You will need an LZW patent license from Unisys in order to *
* use this file legally in any commercial or shareware application. *
**************************************************************************
*
* This file contains routines to read input images in GIF format.
*
* These routines may need modification for non-Unix environments or
* specialized applications. As they stand, they assume input from
* an ordinary stdio stream. They further assume that reading begins
* at the start of the file; input_init may need work if the
* user interface has already read some data (e.g., to determine that
* the file is indeed GIF format).
*/
/*
* This code is loosely based on giftoppm from the PBMPLUS distribution
* of Feb. 1991. That file contains the following copyright notice:
* +-------------------------------------------------------------------+
* | Copyright 1990, David Koblas. |
* | Permission to use, copy, modify, and distribute this software |
* | and its documentation for any purpose and without fee is hereby |
* | granted, provided that the above copyright notice appear in all |
* | copies and that both that copyright notice and this permission |
* | notice appear in supporting documentation. This software is |
* | provided "as is" without express or implied warranty. |
* +-------------------------------------------------------------------+
*
* We are also required to state that
* "The Graphics Interchange Format(c) is the Copyright property of
* CompuServe Incorporated. GIF(sm) is a Service Mark property of
* CompuServe Incorporated."
*/
#include "cdjpeg.h" /* Common decls for cjpeg/djpeg applications */
#ifdef GIF_SUPPORTED
#define MAXCOLORMAPSIZE 256 /* max # of colors in a GIF colormap */
#define NUMCOLORS 3 /* # of colors */
#define CM_RED 0 /* color component numbers */
#define CM_GREEN 1
#define CM_BLUE 2
#define MAX_LZW_BITS 12 /* maximum LZW code size */
#define LZW_TABLE_SIZE (1<<MAX_LZW_BITS) /* # of possible LZW symbols */
/* Macros for extracting header data --- note we assume chars may be signed */
#define LM_to_uint(a,b) ((((b)&0xFF) << 8) | ((a)&0xFF))
#define BitSet(byte, bit) ((byte) & (bit))
#define INTERLACE 0x40 /* mask for bit signifying interlaced image */
#define COLORMAPFLAG 0x80 /* mask for bit signifying colormap presence */
#define ReadOK(file,buffer,len) (JFREAD(file,buffer,len) == ((size_t) (len)))
/* LZW decompression tables look like this:
* symbol_head[K] = prefix symbol of any LZW symbol K (0..LZW_TABLE_SIZE-1)
* symbol_tail[K] = suffix byte of any LZW symbol K (0..LZW_TABLE_SIZE-1)
* Note that entries 0..end_code of the above tables are not used,
* since those symbols represent raw bytes or special codes.
*
* The stack represents the not-yet-used expansion of the last LZW symbol.
* In the worst case, a symbol could expand to as many bytes as there are
* LZW symbols, so we allocate LZW_TABLE_SIZE bytes for the stack.
* (This is conservative since that number includes the raw-byte symbols.)
*
* The tables are allocated from FAR heap space since they would use up
* rather a lot of the near data space in a PC.
*/
/* Private version of data source object */
typedef struct {
struct cjpeg_source_struct pub; /* public fields */
j_compress_ptr cinfo; /* back link saves passing separate parm */
JSAMPARRAY colormap; /* GIF colormap (converted to my format) */
/* State for GetCode and LZWReadByte */
char code_buf[256+4]; /* current input data block */
int last_byte; /* # of bytes in code_buf */
int last_bit; /* # of bits in code_buf */
int cur_bit; /* next bit index to read */
boolean out_of_blocks; /* TRUE if hit terminator data block */
int input_code_size; /* codesize given in GIF file */
int clear_code,end_code; /* values for Clear and End codes */
int code_size; /* current actual code size */
int limit_code; /* 2^code_size */
int max_code; /* first unused code value */
boolean first_time; /* flags first call to LZWReadByte */
/* Private state for LZWReadByte */
int oldcode; /* previous LZW symbol */
int firstcode; /* first byte of oldcode's expansion */
/* LZW symbol table and expansion stack */
UINT16 FAR *symbol_head; /* => table of prefix symbols */
UINT8 FAR *symbol_tail; /* => table of suffix bytes */
UINT8 FAR *symbol_stack; /* => stack for symbol expansions */
UINT8 FAR *sp; /* stack pointer */
/* State for interlaced image processing */
boolean is_interlaced; /* TRUE if have interlaced image */
jvirt_sarray_ptr interlaced_image; /* full image in interlaced order */
JDIMENSION cur_row_number; /* need to know actual row number */
JDIMENSION pass2_offset; /* # of pixel rows in pass 1 */
JDIMENSION pass3_offset; /* # of pixel rows in passes 1&2 */
JDIMENSION pass4_offset; /* # of pixel rows in passes 1,2,3 */
} gif_source_struct;
typedef gif_source_struct * gif_source_ptr;
/* Forward declarations */
METHODDEF(JDIMENSION) get_pixel_rows
JPP((j_compress_ptr cinfo, cjpeg_source_ptr sinfo));
METHODDEF(JDIMENSION) load_interlaced_image
JPP((j_compress_ptr cinfo, cjpeg_source_ptr sinfo));
METHODDEF(JDIMENSION) get_interlaced_row
JPP((j_compress_ptr cinfo, cjpeg_source_ptr sinfo));
LOCAL(int)
ReadByte (gif_source_ptr sinfo)
/* Read next byte from GIF file */
{
register FILE * infile = sinfo->pub.input_file;
int c;
if ((c = getc(infile)) == EOF)
ERREXIT(sinfo->cinfo, JERR_INPUT_EOF);
return c;
}
LOCAL(int)
GetDataBlock (gif_source_ptr sinfo, char *buf)
/* Read a GIF data block, which has a leading count byte */
/* A zero-length block marks the end of a data block sequence */
{
int count;
count = ReadByte(sinfo);
if (count > 0) {
if (! ReadOK(sinfo->pub.input_file, buf, count))
ERREXIT(sinfo->cinfo, JERR_INPUT_EOF);
}
return count;
}
LOCAL(void)
SkipDataBlocks (gif_source_ptr sinfo)
/* Skip a series of data blocks, until a block terminator is found */
{
char buf[256];
while (GetDataBlock(sinfo, buf) > 0)
/* skip */;
}
LOCAL(void)
ReInitLZW (gif_source_ptr sinfo)
/* (Re)initialize LZW state; shared code for startup and Clear processing */
{
sinfo->code_size = sinfo->input_code_size + 1;
sinfo->limit_code = sinfo->clear_code << 1; /* 2^code_size */
sinfo->max_code = sinfo->clear_code + 2; /* first unused code value */
sinfo->sp = sinfo->symbol_stack; /* init stack to empty */
}
LOCAL(void)
InitLZWCode (gif_source_ptr sinfo)
/* Initialize for a series of LZWReadByte (and hence GetCode) calls */
{
/* GetCode initialization */
sinfo->last_byte = 2; /* make safe to "recopy last two bytes" */
sinfo->last_bit = 0; /* nothing in the buffer */
sinfo->cur_bit = 0; /* force buffer load on first call */
sinfo->out_of_blocks = FALSE;
/* LZWReadByte initialization: */
/* compute special code values (note that these do not change later) */
sinfo->clear_code = 1 << sinfo->input_code_size;
sinfo->end_code = sinfo->clear_code + 1;
sinfo->first_time = TRUE;
ReInitLZW(sinfo);
}
LOCAL(int)
GetCode (gif_source_ptr sinfo)
/* Fetch the next code_size bits from the GIF data */
/* We assume code_size is less than 16 */
{
register INT32 accum;
int offs, ret, count;
while ( (sinfo->cur_bit + sinfo->code_size) > sinfo->last_bit) {
/* Time to reload the buffer */
if (sinfo->out_of_blocks) {
WARNMS(sinfo->cinfo, JWRN_GIF_NOMOREDATA);
return sinfo->end_code; /* fake something useful */
}
/* preserve last two bytes of what we have -- assume code_size <= 16 */
sinfo->code_buf[0] = sinfo->code_buf[sinfo->last_byte-2];
sinfo->code_buf[1] = sinfo->code_buf[sinfo->last_byte-1];
/* Load more bytes; set flag if we reach the terminator block */
if ((count = GetDataBlock(sinfo, &sinfo->code_buf[2])) == 0) {
sinfo->out_of_blocks = TRUE;
WARNMS(sinfo->cinfo, JWRN_GIF_NOMOREDATA);
return sinfo->end_code; /* fake something useful */
}
/* Reset counters */
sinfo->cur_bit = (sinfo->cur_bit - sinfo->last_bit) + 16;
sinfo->last_byte = 2 + count;
sinfo->last_bit = sinfo->last_byte * 8;
}
/* Form up next 24 bits in accum */
offs = sinfo->cur_bit >> 3; /* byte containing cur_bit */
#ifdef CHAR_IS_UNSIGNED
accum = sinfo->code_buf[offs+2];
accum <<= 8;
accum |= sinfo->code_buf[offs+1];
accum <<= 8;
accum |= sinfo->code_buf[offs];
#else
accum = sinfo->code_buf[offs+2] & 0xFF;
accum <<= 8;
accum |= sinfo->code_buf[offs+1] & 0xFF;
accum <<= 8;
accum |= sinfo->code_buf[offs] & 0xFF;
#endif
/* Right-align cur_bit in accum, then mask off desired number of bits */
accum >>= (sinfo->cur_bit & 7);
ret = ((int) accum) & ((1 << sinfo->code_size) - 1);
sinfo->cur_bit += sinfo->code_size;
return ret;
}
LOCAL(int)
LZWReadByte (gif_source_ptr sinfo)
/* Read an LZW-compressed byte */
{
register int code; /* current working code */
int incode; /* saves actual input code */
/* First time, just eat the expected Clear code(s) and return next code, */
/* which is expected to be a raw byte. */
if (sinfo->first_time) {
sinfo->first_time = FALSE;
code = sinfo->clear_code; /* enables sharing code with Clear case */
} else {
/* If any codes are stacked from a previously read symbol, return them */
if (sinfo->sp > sinfo->symbol_stack)
return (int) *(-- sinfo->sp);
/* Time to read a new symbol */
code = GetCode(sinfo);
}
if (code == sinfo->clear_code) {
/* Reinit state, swallow any extra Clear codes, and */
/* return next code, which is expected to be a raw byte. */
ReInitLZW(sinfo);
do {
code = GetCode(sinfo);
} while (code == sinfo->clear_code);
if (code > sinfo->clear_code) { /* make sure it is a raw byte */
WARNMS(sinfo->cinfo, JWRN_GIF_BADDATA);
code = 0; /* use something valid */
}
/* make firstcode, oldcode valid! */
sinfo->firstcode = sinfo->oldcode = code;
return code;
}
if (code == sinfo->end_code) {
/* Skip the rest of the image, unless GetCode already read terminator */
if (! sinfo->out_of_blocks) {
SkipDataBlocks(sinfo);
sinfo->out_of_blocks = TRUE;
}
/* Complain that there's not enough data */
WARNMS(sinfo->cinfo, JWRN_GIF_ENDCODE);
/* Pad data with 0's */
return 0; /* fake something usable */
}
/* Got normal raw byte or LZW symbol */
incode = code; /* save for a moment */
if (code >= sinfo->max_code) { /* special case for not-yet-defined symbol */
/* code == max_code is OK; anything bigger is bad data */
if (code > sinfo->max_code) {
WARNMS(sinfo->cinfo, JWRN_GIF_BADDATA);
incode = 0; /* prevent creation of loops in symbol table */
}
/* this symbol will be defined as oldcode/firstcode */
*(sinfo->sp++) = (UINT8) sinfo->firstcode;
code = sinfo->oldcode;
}
/* If it's a symbol, expand it into the stack */
while (code >= sinfo->clear_code) {
*(sinfo->sp++) = sinfo->symbol_tail[code]; /* tail is a byte value */
code = sinfo->symbol_head[code]; /* head is another LZW symbol */
}
/* At this point code just represents a raw byte */
sinfo->firstcode = code; /* save for possible future use */
/* If there's room in table, */
if ((code = sinfo->max_code) < LZW_TABLE_SIZE) {
/* Define a new symbol = prev sym + head of this sym's expansion */
sinfo->symbol_head[code] = sinfo->oldcode;
sinfo->symbol_tail[code] = (UINT8) sinfo->firstcode;
sinfo->max_code++;
/* Is it time to increase code_size? */
if ((sinfo->max_code >= sinfo->limit_code) &&
(sinfo->code_size < MAX_LZW_BITS)) {
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -