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