JPEGView/Independent JPEG Group/rdgif.c

1 line
22 KiB
C
Raw Normal View History

/* * rdgif.c * * Copyright (C) 1991-1994, 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. * * 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 ca