3 * Purpose: GIF Image Class Loader and Writer
5 /* ==========================================================
6 * CxImageGIF (c) 07/Aug/2001 Davide Pizzolato - www.xdp.it
7 * For conditions of distribution and use, see copyright notice in ximage.h
9 * Special thanks to Troels Knakkergaard for new features, enhancements and bugfixes
11 * original CImageGIF and CImageIterator implementation are:
12 * Copyright: (c) 1995, Alejandro Aguilar Sierra <asierra(at)servidor(dot)unam(dot)mx>
14 * 6/15/97 Randy Spann: Added GIF87a writing support
15 * R.Spann@ConnRiver.net
17 * DECODE.C - An LZW decoder for GIF
18 * Copyright (C) 1987, by Steven A. Bennett
19 * Copyright (C) 1994, C++ version by Alejandro Aguilar Sierra
21 * In accordance with the above, I want to credit Steve Wilhite who wrote
22 * the code which this is heavily inspired by...
24 * GIF and 'Graphics Interchange Format' are trademarks (tm) of
25 * Compuserve, Incorporated, an H&R Block Company.
27 * Release Notes: This file contains a decoder routine for GIF images
28 * which is similar, structurally, to the original routine by Steve Wilhite.
29 * It is, however, somewhat noticably faster in most cases.
31 * ==========================================================
34 #if !defined(__ximaGIF_h)
39 #if CXIMAGE_SUPPORT_GIF
41 typedef short int code_int;
43 /* Various error codes used by decoder */
44 #define OUT_OF_MEMORY -10
45 #define BAD_CODE_SIZE -20
47 #define WRITE_ERROR -2
49 #define CREATE_ERROR -4
50 #define MAX_CODES 4095
51 #define GIFBUFTAM 16383
52 #define TRANSPARENCY_CODE 0xF9
54 //LZW GIF Image compression
55 #define MAXBITSCODES 12
56 #define HSIZE 5003 /* 80% occupancy */
57 #define MAXCODE(n_bits) (((code_int) 1 << (n_bits)) - 1)
58 #define HashTabOf(i) htab[i]
59 #define CodeTabOf(i) codetab[i]
63 class DLL_EXP CxImageGIF: public CxImage
67 typedef struct tag_gifgce{
68 BYTE flags; /*res:3|dispmeth:3|userinputflag:1|transpcolflag:1*/
73 typedef struct tag_dscgif{ /* Logic Screen Descriptor */
74 char header[6]; /* Firma and version */
82 typedef struct tag_image{ /* Image Descriptor */
90 typedef struct tag_TabCol{ /* Tabla de colores */
91 short colres; /* color resolution */
92 short sogct; /* size of global color table */
93 rgb_color paleta[256]; /* paleta */
96 typedef struct tag_RLE{
115 unsigned char oblock[256];
121 CxImageGIF(): CxImage(CXIMAGE_FORMAT_GIF) {m_loops=0; info.dispmeth=0; m_comment[0]='\0';}
123 // bool Load(const TCHAR * imageFileName){ return CxImage::Load(imageFileName,CXIMAGE_FORMAT_GIF);}
124 // bool Save(const TCHAR * imageFileName){ return CxImage::Save(imageFileName,CXIMAGE_FORMAT_GIF);}
126 bool Decode(CxFile * fp);
127 bool Decode(FILE *fp) { CxIOFile file(fp); return Decode(&file); }
129 #if CXIMAGE_SUPPORT_ENCODE
130 bool Encode(CxFile * fp);
131 bool Encode(CxFile * fp, CxImage ** pImages, int pagecount, bool bLocalColorMap = false, bool bLocalDispMeth = false);
132 bool Encode(FILE *fp) { CxIOFile file(fp); return Encode(&file); }
133 bool Encode(FILE *fp, CxImage ** pImages, int pagecount, bool bLocalColorMap = false)
134 { CxIOFile file(fp); return Encode(&file, pImages, pagecount, bLocalColorMap); }
135 #endif // CXIMAGE_SUPPORT_ENCODE
137 void SetLoops(int loops);
139 void SetComment(const char* sz_comment_in);
140 void GetComment(char* sz_comment_out);
143 bool DecodeExtension(CxFile *fp);
144 void EncodeHeader(CxFile *fp);
145 void EncodeLoopExtension(CxFile *fp);
146 void EncodeExtension(CxFile *fp);
147 void EncodeBody(CxFile *fp, bool bLocalColorMap = false);
148 void EncodeComment(CxFile *fp);
149 bool EncodeRGB(CxFile *fp);
150 void GifMix(CxImage & imgsrc2, struct_image & imgdesc);
152 struct_gifgce gifgce;
156 unsigned long cur_accum;
158 int interlaced, iypos, istep, iheight, ipass;
161 BYTE buf[GIFBUFTAM + 1];
164 void Putword (int w, CxFile* fp );
165 void compressNONE (int init_bits, CxFile* outfile);
166 void compressLZW (int init_bits, CxFile* outfile);
167 void output (code_int code );
168 void cl_hash (long hsize);
169 void char_out (int c);
171 short init_exp(short size);
172 short get_next_code(CxFile*);
173 short decoder(CxFile*, CImageIterator* iter, short linewidth, int &bad_code_count);
174 int get_byte(CxFile*);
175 int out_line(CImageIterator* iter, unsigned char *pixels, int linelen);
176 int get_num_frames(CxFile *f,struct_TabCol* TabColSrc,struct_dscgif* dscgif);
177 long seek_next_image(CxFile* fp, long position);
179 short curr_size; /* The current code size */
180 short clear; /* Value for a clear code */
181 short ending; /* Value for a ending code */
182 short newcodes; /* First available code */
183 short top_slot; /* Highest code for current size */
184 short slot; /* Last read code */
186 /* The following static variables are used
187 * for seperating out codes */
188 short navail_bytes; /* # bytes left in block */
189 short nbits_left; /* # bits left in current BYTE */
190 BYTE b1; /* Current BYTE */
191 BYTE byte_buff[257]; /* Current block */
192 BYTE *pbytes; /* Pointer to next BYTE in block */
193 /* The reason we have these seperated like this instead of using
194 * a structure like the original Wilhite code did, is because this
195 * stuff generally produces significantly faster code when compiled...
196 * This code is full of similar speedups... (For a good book on writing
197 * C for speed or for space optomisation, see Efficient C by Tom Plum,
198 * published by Plum-Hall Associates...)
200 BYTE stack[MAX_CODES + 1]; /* Stack for storing pixels */
201 BYTE suffix[MAX_CODES + 1]; /* Suffix table */
202 WORD prefix[MAX_CODES + 1]; /* Prefix linked list */
204 //LZW GIF Image compression routines
206 unsigned short codetab [HSIZE];
207 int n_bits; /* number of bits/code */
208 code_int maxcode; /* maximum code, given n_bits */
209 code_int free_ent; /* first unused entry */
222 //RLE compression routines
223 void compressRLE( int init_bits, CxFile* outfile);
224 void rle_clear(struct_RLE* rle);
225 void rle_flush(struct_RLE* rle);
226 void rle_flush_withtable(int count, struct_RLE* rle);
227 void rle_flush_clearorrep(int count, struct_RLE* rle);
228 void rle_flush_fromclear(int count,struct_RLE* rle);
229 void rle_output_plain(int c,struct_RLE* rle);
230 void rle_reset_out_clear(struct_RLE* rle);
231 unsigned int rle_compute_triangle_count(unsigned int count, unsigned int nrepcodes);
232 unsigned int rle_isqrt(unsigned int x);
233 void rle_write_block(struct_RLE* rle);
234 void rle_block_out(unsigned char c, struct_RLE* rle);
235 void rle_block_flush(struct_RLE* rle);
236 void rle_output(int val, struct_RLE* rle);
237 void rle_output_flush(struct_RLE* rle);