[95] | 1 | /*
|
---|
| 2 | * File: ximagif.h
|
---|
| 3 | * Purpose: GIF Image Class Loader and Writer
|
---|
| 4 | */
|
---|
| 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
|
---|
| 8 | *
|
---|
| 9 | * Special thanks to Troels Knakkergaard for new features, enhancements and bugfixes
|
---|
| 10 | *
|
---|
| 11 | * original CImageGIF and CImageIterator implementation are:
|
---|
| 12 | * Copyright: (c) 1995, Alejandro Aguilar Sierra <asierra(at)servidor(dot)unam(dot)mx>
|
---|
| 13 | *
|
---|
| 14 | * 6/15/97 Randy Spann: Added GIF87a writing support
|
---|
| 15 | * R.Spann@ConnRiver.net
|
---|
| 16 | *
|
---|
| 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
|
---|
| 20 | *
|
---|
| 21 | * In accordance with the above, I want to credit Steve Wilhite who wrote
|
---|
| 22 | * the code which this is heavily inspired by...
|
---|
| 23 | *
|
---|
| 24 | * GIF and 'Graphics Interchange Format' are trademarks (tm) of
|
---|
| 25 | * Compuserve, Incorporated, an H&R Block Company.
|
---|
| 26 | *
|
---|
| 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.
|
---|
| 30 | *
|
---|
| 31 | * ==========================================================
|
---|
| 32 | */
|
---|
| 33 |
|
---|
| 34 | #if !defined(__ximaGIF_h)
|
---|
| 35 | #define __ximaGIF_h
|
---|
| 36 |
|
---|
| 37 | #include "ximage.h"
|
---|
| 38 |
|
---|
| 39 | #if CXIMAGE_SUPPORT_GIF
|
---|
| 40 |
|
---|
| 41 | typedef short int code_int;
|
---|
| 42 |
|
---|
| 43 | /* Various error codes used by decoder */
|
---|
| 44 | #define OUT_OF_MEMORY -10
|
---|
| 45 | #define BAD_CODE_SIZE -20
|
---|
| 46 | #define READ_ERROR -1
|
---|
| 47 | #define WRITE_ERROR -2
|
---|
| 48 | #define OPEN_ERROR -3
|
---|
| 49 | #define CREATE_ERROR -4
|
---|
| 50 | #define MAX_CODES 4095
|
---|
| 51 | #define GIFBUFTAM 16383
|
---|
| 52 | #define TRANSPARENCY_CODE 0xF9
|
---|
| 53 |
|
---|
| 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]
|
---|
| 60 |
|
---|
| 61 |
|
---|
| 62 | class CImageIterator;
|
---|
| 63 | class DLL_EXP CxImageGIF: public CxImage
|
---|
| 64 | {
|
---|
| 65 | #pragma pack(1)
|
---|
| 66 |
|
---|
| 67 | typedef struct tag_gifgce{
|
---|
| 68 | BYTE flags; /*res:3|dispmeth:3|userinputflag:1|transpcolflag:1*/
|
---|
| 69 | WORD delaytime;
|
---|
| 70 | BYTE transpcolindex;
|
---|
| 71 | } struct_gifgce;
|
---|
| 72 |
|
---|
| 73 | typedef struct tag_dscgif{ /* Logic Screen Descriptor */
|
---|
| 74 | char header[6]; /* Firma and version */
|
---|
| 75 | WORD scrwidth;
|
---|
| 76 | WORD scrheight;
|
---|
| 77 | char pflds;
|
---|
| 78 | char bcindx;
|
---|
| 79 | char pxasrat;
|
---|
| 80 | } struct_dscgif;
|
---|
| 81 |
|
---|
| 82 | typedef struct tag_image{ /* Image Descriptor */
|
---|
| 83 | WORD l;
|
---|
| 84 | WORD t;
|
---|
| 85 | WORD w;
|
---|
| 86 | WORD h;
|
---|
| 87 | BYTE pf;
|
---|
| 88 | } struct_image;
|
---|
| 89 |
|
---|
| 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 */
|
---|
| 94 | } struct_TabCol;
|
---|
| 95 |
|
---|
| 96 | typedef struct tag_RLE{
|
---|
| 97 | int rl_pixel;
|
---|
| 98 | int rl_basecode;
|
---|
| 99 | int rl_count;
|
---|
| 100 | int rl_table_pixel;
|
---|
| 101 | int rl_table_max;
|
---|
| 102 | int just_cleared;
|
---|
| 103 | int out_bits;
|
---|
| 104 | int out_bits_init;
|
---|
| 105 | int out_count;
|
---|
| 106 | int out_bump;
|
---|
| 107 | int out_bump_init;
|
---|
| 108 | int out_clear;
|
---|
| 109 | int out_clear_init;
|
---|
| 110 | int max_ocodes;
|
---|
| 111 | int code_clear;
|
---|
| 112 | int code_eof;
|
---|
| 113 | unsigned int obuf;
|
---|
| 114 | int obits;
|
---|
| 115 | unsigned char oblock[256];
|
---|
| 116 | int oblen;
|
---|
| 117 | } struct_RLE;
|
---|
| 118 | #pragma pack()
|
---|
| 119 |
|
---|
| 120 | public:
|
---|
| 121 | CxImageGIF(): CxImage(CXIMAGE_FORMAT_GIF) {m_loops=0; info.dispmeth=0; m_comment[0]='\0';}
|
---|
| 122 |
|
---|
| 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);}
|
---|
| 125 |
|
---|
| 126 | bool Decode(CxFile * fp);
|
---|
| 127 | bool Decode(FILE *fp) { CxIOFile file(fp); return Decode(&file); }
|
---|
| 128 |
|
---|
| 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
|
---|
| 136 |
|
---|
| 137 | void SetLoops(int loops);
|
---|
| 138 | long GetLoops();
|
---|
| 139 | void SetComment(const char* sz_comment_in);
|
---|
| 140 | void GetComment(char* sz_comment_out);
|
---|
| 141 |
|
---|
| 142 | protected:
|
---|
| 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);
|
---|
| 151 |
|
---|
| 152 | struct_gifgce gifgce;
|
---|
| 153 |
|
---|
| 154 | int curx, cury;
|
---|
| 155 | long CountDown;
|
---|
| 156 | unsigned long cur_accum;
|
---|
| 157 | int cur_bits;
|
---|
| 158 | int interlaced, iypos, istep, iheight, ipass;
|
---|
| 159 | int ibf;
|
---|
| 160 | int ibfmax;
|
---|
| 161 | BYTE buf[GIFBUFTAM + 1];
|
---|
| 162 | // Implementation
|
---|
| 163 | int GifNextPixel ();
|
---|
| 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);
|
---|
| 170 | void flush_char ();
|
---|
| 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);
|
---|
| 178 |
|
---|
| 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 */
|
---|
| 185 |
|
---|
| 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...)
|
---|
| 199 | */
|
---|
| 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 */
|
---|
| 203 |
|
---|
| 204 | //LZW GIF Image compression routines
|
---|
| 205 | long htab [HSIZE];
|
---|
| 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 */
|
---|
| 210 | int clear_flg;
|
---|
| 211 | int g_init_bits;
|
---|
| 212 | CxFile* g_outfile;
|
---|
| 213 | int ClearCode;
|
---|
| 214 | int EOFCode;
|
---|
| 215 |
|
---|
| 216 | int a_count;
|
---|
| 217 | char accum[256];
|
---|
| 218 |
|
---|
| 219 | char m_comment[256];
|
---|
| 220 | int m_loops;
|
---|
| 221 |
|
---|
| 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);
|
---|
| 238 | };
|
---|
| 239 |
|
---|
| 240 | #endif
|
---|
| 241 |
|
---|
| 242 | #endif
|
---|