Annotation of parser3/src/classes/gd/gif.h, revision 1.12
1.1 paf 1: /** @file
1.4 paf 2: Parser: image manipulations decls.
3:
4: Author: Alexander Petrosyan <paf@design.ru> (http://design.ru/paf)
5:
6:
7: based on:
1.1 paf 8: gd.h: declarations file for the gifdraw module.
9:
10: Written by Tom Boutell, 5/94.
11: Copyright 1994, Cold Spring Harbor Labs.
12: Permission granted to use this code in any fashion provided
13: that this notice is retained and any alterations are
14: labeled as such. It is requested, but not required, that
15: you share extensions to this module with us so that we
1.2 paf 16: can incorporate them into new versions.
17: */
1.1 paf 18:
19: #ifndef GIF_H
20: #define GIF_H 1
21:
1.6 paf 22: #include "pa_config_includes.h"
23:
1.2 paf 24: #include "pa_pool.h"
1.3 paf 25: #include "pa_string.h"
1.2 paf 26:
1.1 paf 27: #define gdMaxColors 0x100
1.2 paf 28: #define HSIZE 5003 /* 80% occupancy */
29:
30: /** Image type.
31: See functions below; you will not need to change
1.1 paf 32: the elements directly. Use the provided macros to
33: access sx, sy, the color table, and colorsTotal for
1.2 paf 34: read-only purposes.
35: */
36: class gdImage : public Pooled {
37:
1.7 paf 38: public:
39:
40: //@{
41: /// @name Functions to manipulate images
1.2 paf 42: gdImage(Pool& pool) : Pooled(pool) {}
43: void Create(int asx, int asy);
44: bool CreateFromGif(FILE *fd);
45: void SetPixel(int x, int y, int color);
46: int GetPixel(int x, int y);
47: void Line(int x1, int y1, int x2, int y2, int color);
1.11 parser 48: void StyledLine(int x1, int y1, int x2, int y2, int color, const char *lineStyle);
1.2 paf 49: void Rectangle(int x1, int y1, int x2, int y2, int color);
50: void LineReplaceColor(int x1, int y1, int x2, int y2, int a, int b);
51: void FilledRectangle(int x1, int y1, int x2, int y2, int color);
1.7 paf 52: //@}
1.2 paf 53:
1.7 paf 54: /// Point type for use in polygon drawing.
1.2 paf 55: struct Point {
56: int x, y;
57: };
58:
1.11 parser 59: void Polygon(Point *p, int n, int c, bool closed=true);
1.2 paf 60: void FilledPolygon(Point *p, int n, int c);
61: void FilledPolygonReplaceColor(Point *p, int n, int a, int b);
62:
63: int ColorAllocate(int r, int g, int b);
64: int ColorClosest(int r, int g, int b);
65: int ColorExact(int r, int g, int b);
66: int ColorRGB(int r, int g, int b);
67: int Color(unsigned int rgb);
68: void ColorDeallocate(int color);
69: void SetColorTransparent(int color);
70:
71: int BoundsSafe(int x, int y);
72: void DoSetPixel(int x, int y, int color);
73:
1.3 paf 74: void Gif(String& out);
1.2 paf 75: void Arc(int cx, int cy, int w, int h, int s, int e, int color);
76: void FillToBorder(int x, int y, int border, int color);
77: void Fill(int x, int y, int color);
78: void Copy(gdImage& dst, int dstX, int dstY, int srcX, int srcY, int w, int h);
79: void CopyResized(gdImage& dst, int dstX, int dstY, int srcX, int srcY, int dstW, int dstH, int srcW, int srcH);
1.11 parser 80: void SetLineWidth(int width);
81: void SetLineStyle(const char *aLineStyle);
1.2 paf 82: void SetInterlace(int interlaceArg); /* On or off(1 or 0) */
83:
1.7 paf 84: public:
1.2 paf 85:
1.7 paf 86: //@{
87: /// @name information about image. READ ONLY
1.2 paf 88: int SX() { return sx; }
89: int SY() { return sy; }
90: int ColorsTotal() { return colorsTotal; }
91: int Red(int c) { return red[c]; }
92: int Green(int c) { return green[c]; }
93: int Blue(int c) { return blue[c]; }
94: int GetTransparent() { return transparent; }
95: int GetInterlaced() { return interlace; }
1.7 paf 96: //@}
1.2 paf 97:
98: private:
1.1 paf 99:
100: unsigned char ** pixels;
101: int sx;
102: int sy;
103: int colorsTotal;
104: int red[gdMaxColors];
105: int green[gdMaxColors];
106: int blue[gdMaxColors];
107: int open[gdMaxColors];
108: int transparent;
109: int *polyInts;
110: int polyAllocated;
1.11 parser 111: int lineWidth; const char *lineStyle;
1.1 paf 112: int interlace;
113:
1.2 paf 114: private: // read gif
1.1 paf 115:
1.2 paf 116: int GetDataBlock(FILE *fd, unsigned char *buf);
117: int LWZReadByte(FILE *fd, int flag, int input_code_size);
118: void ReadImage(FILE *fd, int len, int height, unsigned char(*cmap)[256], int interlace, int ignore);
119: int DoExtension(FILE *fd, int label, int *Transparent);
120: int GetCode(FILE *fd, int code_size, int flag);
121:
122: private: // read gif
123:
124: int ZeroDataBlock;
125:
126: };
127:
1.7 paf 128: /// used by gdImage::Gif to produce buffer with bytes in GIF format
1.2 paf 129: class gdGifEncoder : public Pooled {
130: public:
131:
1.3 paf 132: gdGifEncoder(Pool& pool, gdImage& aim, String& afp);
1.2 paf 133:
1.3 paf 134: void encode(
1.2 paf 135: int GWidth, int GHeight,
136: int GInterlace, int Background,
137: int Transparent, int BitsPerPixel,
138: int *Red, int *Green, int *Blue);
139:
140: private:
141:
1.7 paf 142: /// a code_int must be able to hold 2**GIFBITS values of type int, and also -1
1.2 paf 143: typedef int code_int;
1.10 parser 144: #ifdef SIGNED_COMPARE_SLOW
1.2 paf 145: typedef unsigned long int count_int;
146: typedef unsigned short int count_short;
1.10 parser 147: #else
1.2 paf 148: typedef long int count_int;
1.10 parser 149: #endif
1.2 paf 150:
151: private:
152:
1.3 paf 153: void Putbyte(int c);
154: void Putword(int w);
155: void Write(void *buf, size_t size);
156:
1.2 paf 157: void prepare_encoder(void);
158: void BumpPixel(void);
159: int GIFNextPixel();
1.3 paf 160: void compress(int init_bits);
1.2 paf 161: void output(code_int code);
162: void cl_block(void);
163: void cl_hash(count_int hsize);
164: void char_init(void);
165: void char_out(int c);
166: void flush_char(void);
167:
168: private:
169:
170: gdImage& im;
1.3 paf 171: String& fp;
1.2 paf 172:
173: int Width, Height;
174: int curx, cury;
175: long CountDown;
176: int Pass;
177: int Interlace;
178:
179: int g_init_bits;
180:
181: int ClearCode;
182: int EOFCode;
183:
184: int n_bits; /* number of bits/code */
185: int maxbits; /* user settable max # bits/code */
186:
187: code_int maxcode; /* maximum code, given n_bits */
188: code_int maxmaxcode; /* should NEVER generate this code */
189:
190: count_int htab [HSIZE];
191: unsigned short codetab [HSIZE];
192: code_int hsize; /* for dynamic table sizing */
193:
194: code_int free_ent; /* first unused entry */
195:
196: /*
197: * block compression parameters -- after all codes are used up,
198: * and compression rate changes, start over.
199: */
200: int clear_flg;
201:
202: int offset;
203: long int in_count; /* length of input */
204: long int out_count; /* # of codes output(for debugging) */
205:
206: unsigned long cur_accum;
207: int cur_bits;
208:
209: /*
210: * Number of characters so far in this 'packet'
211: */
212: int a_count;
213:
214: /*
215: * Define the storage for the packet accumulator
216: */
217: char accum[ 256 ];
218:
219: };
220:
221: inline int gdImage::BoundsSafe(int x, int y){
222: return(!(((y < 0) ||(y >= sy)) ||((x < 0) ||(x >= sx))));
223: }
224:
225: inline /*paf int*/void gdImage::DoSetPixel(int x, int y, int color){
226: if(BoundsSafe(x, y)) pixels[x][y] = color;
227: }
1.1 paf 228:
229: #endif
E-mail: