1 /* 2 * File: ximagif.h 3 * Purpose: GIF Image Class Loader and Writer 4 */ 5 /* === C R E D I T S & D I S C L A I M E R S ============== 6 * CxImageGIF (c) 07/Aug/2001 Davide Pizzolato - www.xdp.it 7 * Permission is given by the author to freely redistribute and include 8 * this code in any program as long as this credit is given where due. 9 * 10 * CxImage version 5.99a 08/Feb/2004 11 * See the file history.htm for the complete bugfix and news report. 12 * 13 * Special thanks to Troels Knakkergaard for new features, enhancements and bugfixes 14 * 15 * original CImageGIF and CImageIterator implementation are: 16 * Copyright: (c) 1995, Alejandro Aguilar Sierra <asierra(at)servidor(dot)unam(dot)mx> 17 * 18 * 6/15/97 Randy Spann: Added GIF87a writing support 19 * R.Spann@ConnRiver.net 20 * 21 * DECODE.C - An LZW decoder for GIF 22 * Copyright (C) 1987, by Steven A. Bennett 23 * Copyright (C) 1994, C++ version by Alejandro Aguilar Sierra 24 * 25 * In accordance with the above, I want to credit Steve Wilhite who wrote 26 * the code which this is heavily inspired by... 27 * 28 * GIF and 'Graphics Interchange Format' are trademarks (tm) of 29 * Compuserve, Incorporated, an H&R Block Company. 30 * 31 * Release Notes: This file contains a decoder routine for GIF images 32 * which is similar, structurally, to the original routine by Steve Wilhite. 33 * It is, however, somewhat noticably faster in most cases. 34 * 35 * COVERED CODE IS PROVIDED UNDER THIS LICENSE ON AN "AS IS" BASIS, WITHOUT WARRANTY 36 * OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING, WITHOUT LIMITATION, WARRANTIES 37 * THAT THE COVERED CODE IS FREE OF DEFECTS, MERCHANTABLE, FIT FOR A PARTICULAR PURPOSE 38 * OR NON-INFRINGING. THE ENTIRE RISK AS TO THE QUALITY AND PERFORMANCE OF THE COVERED 39 * CODE IS WITH YOU. SHOULD ANY COVERED CODE PROVE DEFECTIVE IN ANY RESPECT, YOU (NOT 40 * THE INITIAL DEVELOPER OR ANY OTHER CONTRIBUTOR) ASSUME THE COST OF ANY NECESSARY 41 * SERVICING, REPAIR OR CORRECTION. THIS DISCLAIMER OF WARRANTY CONSTITUTES AN ESSENTIAL 42 * PART OF THIS LICENSE. NO USE OF ANY COVERED CODE IS AUTHORIZED HEREUNDER EXCEPT UNDER 43 * THIS DISCLAIMER. 44 * 45 * Use at your own risk! 46 * ========================================================== 47 */ 48 49 #if !defined(__ximaGIF_h) 50 #define __ximaGIF_h 51 52 #include "ximage.h" 53 54 #if CXIMAGE_SUPPORT_GIF 55 56 typedef short int code_int; 57 58 /* Various error codes used by decoder */ 59 #define OUT_OF_MEMORY -10 60 #define BAD_CODE_SIZE -20 61 #define READ_ERROR -1 62 #define WRITE_ERROR -2 63 #define OPEN_ERROR -3 64 #define CREATE_ERROR -4 65 #define MAX_CODES 4095 66 #define GIFBUFTAM 16384 67 #define TRANSPARENCY_CODE 0xF9 68 69 //LZW GIF Image compression 70 #define MAXBITSCODES 12 71 #define HSIZE 5003 /* 80% occupancy */ 72 #define MAXCODE(n_bits) (((code_int) 1 << (n_bits)) - 1) 73 #define HashTabOf(i) htab[i] 74 #define CodeTabOf(i) codetab[i] 75 76 77 class CImageIterator; 78 class DLL_EXP CxImageGIF: public CxImage 79 { 80 #pragma pack(1) 81 82 typedef struct tag_gifgce{ 83 BYTE transpcolflag:1; 84 BYTE userinputflag:1; 85 BYTE dispmeth:3; 86 BYTE res:3; 87 WORD delaytime; 88 BYTE transpcolindex; 89 } struct_gifgce; 90 91 typedef struct tag_dscgif{ /* Logic Screen Descriptor */ 92 char header[6]; /* Firma and version */ 93 WORD scrwidth; 94 WORD scrheight; 95 char pflds; 96 char bcindx; 97 char pxasrat; 98 } struct_dscgif; 99 100 typedef struct tag_image{ /* Image Descriptor */ 101 WORD l; 102 WORD t; 103 WORD w; 104 WORD h; 105 BYTE pf; 106 } struct_image; 107 108 typedef struct tag_TabCol{ /* Tabla de colores */ 109 short colres; /* color resolution */ 110 short sogct; /* size of global color table */ 111 rgb_color paleta[256]; /* paleta */ 112 } struct_TabCol; 113 114 typedef struct tag_RLE{ 115 int rl_pixel; 116 int rl_basecode; 117 int rl_count; 118 int rl_table_pixel; 119 int rl_table_max; 120 int just_cleared; 121 int out_bits; 122 int out_bits_init; 123 int out_count; 124 int out_bump; 125 int out_bump_init; 126 int out_clear; 127 int out_clear_init; 128 int max_ocodes; 129 int code_clear; 130 int code_eof; 131 unsigned int obuf; 132 int obits; 133 unsigned char oblock[256]; 134 int oblen; 135 } struct_RLE; 136 #pragma pack() 137 138 public: CxImageGIF()139 CxImageGIF(): CxImage(CXIMAGE_FORMAT_GIF) {m_loops=0; m_dispmeth=0; m_comment[0]='\0';} 140 141 // bool Load(const char * imageFileName){ return CxImage::Load(imageFileName,CXIMAGE_FORMAT_GIF);} 142 // bool Save(const char * imageFileName){ return CxImage::Save(imageFileName,CXIMAGE_FORMAT_GIF);} 143 144 bool Decode(CxFile * fp); Decode(FILE * fp)145 bool Decode(FILE *fp) { CxIOFile file(fp); return Decode(&file); } 146 147 #if CXIMAGE_SUPPORT_ENCODE 148 bool Encode(CxFile * fp); 149 bool Encode(CxFile * fp, CxImage ** pImages, int pagecount, bool bLocalColorMap = false); Encode(FILE * fp)150 bool Encode(FILE *fp) { CxIOFile file(fp); return Encode(&file); } 151 bool Encode(FILE *fp, CxImage ** pImages, int pagecount, bool bLocalColorMap = false) 152 { CxIOFile file(fp); return Encode(&file, pImages, pagecount, bLocalColorMap); } 153 #endif // CXIMAGE_SUPPORT_ENCODE 154 155 void SetLoops(int loops); 156 long GetLoops(); 157 void SetComment(const char* sz_comment_in); 158 void GetComment(char* sz_comment_out); 159 void SetDisposalMethod(int dm); 160 long GetDisposalMethod(); 161 162 protected: 163 bool DecodeExtension(CxFile *fp); 164 void EncodeHeader(CxFile *fp); 165 void EncodeLoopExtension(CxFile *fp); 166 void EncodeExtension(CxFile *fp); 167 void EncodeBody(CxFile *fp, bool bLocalColorMap = false); 168 void EncodeComment(CxFile *fp); 169 bool EncodeRGB(CxFile *fp); 170 void GifMix(CxImage & imgsrc2, long lxOffset, long lyOffset); 171 172 struct_gifgce gifgce; 173 174 int curx, cury; 175 long CountDown; 176 unsigned long cur_accum; 177 int cur_bits; 178 int interlaced, iypos, istep, iheight, ipass; 179 int ibf; 180 int ibfmax; 181 BYTE buf[GIFBUFTAM + 1]; 182 // Implementation 183 int GifNextPixel (); 184 void Putword (int w, CxFile* fp ); 185 void compressNONE (int init_bits, CxFile* outfile); 186 void compressLZW (int init_bits, CxFile* outfile); 187 void output (code_int code ); 188 void cl_hash (long hsize); 189 void char_out (int c); 190 void flush_char (); 191 short init_exp(short size); 192 short get_next_code(CxFile*); 193 short decoder(CxFile*, CImageIterator* iter, short linewidth, int &bad_code_count); 194 int get_byte(CxFile*); 195 int out_line(CImageIterator* iter, unsigned char *pixels, int linelen); 196 int get_num_frames(CxFile *f,struct_TabCol* TabColSrc); 197 198 short curr_size; /* The current code size */ 199 short clear; /* Value for a clear code */ 200 short ending; /* Value for a ending code */ 201 short newcodes; /* First available code */ 202 short top_slot; /* Highest code for current size */ 203 short slot; /* Last read code */ 204 205 /* The following static variables are used 206 * for seperating out codes */ 207 short navail_bytes; /* # bytes left in block */ 208 short nbits_left; /* # bits left in current BYTE */ 209 BYTE b1; /* Current BYTE */ 210 BYTE byte_buff[257]; /* Current block */ 211 BYTE *pbytes; /* Pointer to next BYTE in block */ 212 /* The reason we have these seperated like this instead of using 213 * a structure like the original Wilhite code did, is because this 214 * stuff generally produces significantly faster code when compiled... 215 * This code is full of similar speedups... (For a good book on writing 216 * C for speed or for space optomisation, see Efficient C by Tom Plum, 217 * published by Plum-Hall Associates...) 218 */ 219 BYTE stack[MAX_CODES + 1]; /* Stack for storing pixels */ 220 BYTE suffix[MAX_CODES + 1]; /* Suffix table */ 221 WORD prefix[MAX_CODES + 1]; /* Prefix linked list */ 222 223 //LZW GIF Image compression routines 224 long htab [HSIZE]; 225 unsigned short codetab [HSIZE]; 226 int n_bits; /* number of bits/code */ 227 code_int maxcode; /* maximum code, given n_bits */ 228 code_int free_ent; /* first unused entry */ 229 int clear_flg; 230 int g_init_bits; 231 CxFile* g_outfile; 232 int ClearCode; 233 int EOFCode; 234 235 int a_count; 236 char accum[256]; 237 238 char m_comment[256]; 239 int m_loops; 240 int m_dispmeth; 241 242 //RLE compression routines 243 void compressRLE( int init_bits, CxFile* outfile); 244 void rle_clear(struct_RLE* rle); 245 void rle_flush(struct_RLE* rle); 246 void rle_flush_withtable(int count, struct_RLE* rle); 247 void rle_flush_clearorrep(int count, struct_RLE* rle); 248 void rle_flush_fromclear(int count,struct_RLE* rle); 249 void rle_output_plain(int c,struct_RLE* rle); 250 void rle_reset_out_clear(struct_RLE* rle); 251 unsigned int rle_compute_triangle_count(unsigned int count, unsigned int nrepcodes); 252 unsigned int rle_isqrt(unsigned int x); 253 void rle_write_block(struct_RLE* rle); 254 void rle_block_out(unsigned char c, struct_RLE* rle); 255 void rle_block_flush(struct_RLE* rle); 256 void rle_output(int val, struct_RLE* rle); 257 void rle_output_flush(struct_RLE* rle); 258 }; 259 260 #endif 261 262 #endif 263