Annotation of parser3/src/lib/gd/gif.h, revision 1.4
1.1 paf 1: /** @file
2: Parser: image manipulations decls.
3:
1.4 ! paf 4: Copyright (c) 2001-2005 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.4 ! paf 21: static const char * const IDENT_GIF_H="$Date: 2004/03/01 13:33:11 $";
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);
1.3 paf 100: unsigned int DecodeColor(int color);
1.1 paf 101: void ColorDeallocate(int color);
102: void SetColorTransparent(int color);
103:
104: int BoundsSafe(int x, int y);
105: void DoSetPixel(int x, int y, int color);
106:
107: gdBuf Gif();
108: void Arc(int cx, int cy, int w, int h, int s, int e, int color);
109: void Sector(int cx, int cy, int w, int h, int s, int e, int color);
110: void FillToBorder(int x, int y, int border, int color);
111: void Fill(int x, int y, int color);
112: void Copy(gdImage& dst, int dstX, int dstY, int srcX, int srcY, int w, int h);
113: void CopyResampled(gdImage& dst, int dstX, int dstY, int srcX, int srcY, int dstW, int dstH, int srcW, int srcH, int tolerance);
114: void SetLineWidth(int width);
115: void SetLineStyle(const char* aLineStyle);
116: void SetInterlace(int interlaceArg); /* On or off(1 or 0) */
117:
118: public:
119:
120: //@{
121: /// @name information about image. READ ONLY
122: int SX() { return sx; }
123: int SY() { return sy; }
124: int ColorsTotal() { return colorsTotal; }
125: int Red(int c) { return red[c]; }
126: int Green(int c) { return green[c]; }
127: int Blue(int c) { return blue[c]; }
128: int GetTransparent() { return transparent; }
129: int GetInterlaced() { return interlace; }
130: //@}
131:
132: private:
133:
134: unsigned char ** pixels;
135: int sx;
136: int sy;
137: int colorsTotal;
138: int red[gdMaxColors];
139: int green[gdMaxColors];
140: int blue[gdMaxColors];
141: int open[gdMaxColors];
142: int transparent;
143: int *polyInts;
144: int polyAllocated;
145: int lineWidth; const char* lineStyle;
146: int interlace;
147:
148: private: // read gif
149:
150: int GetDataBlock(FILE *fd, unsigned char *buf);
151: int LWZReadByte(FILE *fd, int flag, int input_code_size);
152: void ReadImage(FILE *fd, int len, int height, unsigned char(*cmap)[256], int interlace, int ignore);
153: int DoExtension(FILE *fd, int label, int *Transparent);
154: int GetCode(FILE *fd, int code_size, int flag);
155:
156: private: // read gif
157:
158: int ZeroDataBlock;
159:
160: };
161:
162: /// used by gdImage::Gif to produce buffer with bytes in GIF format
163: class gdGifEncoder: public PA_Object {
164: public:
165:
166: gdGifEncoder(gdImage& aim);
167:
168: gdBuf encode(
169: int GWidth, int GHeight,
170: int GInterlace, int Background,
171: int Transparent, int BitsPerPixel,
172: int *Red, int *Green, int *Blue);
173:
174: private:
175:
176: /// a code_int must be able to hold 2**GIFBITS values of type int, and also -1
177: typedef int code_int;
178: #ifdef SIGNED_COMPARE_SLOW
179: typedef unsigned long int count_int;
180: typedef unsigned short int count_short;
181: #else
182: typedef long int count_int;
183: #endif
184:
185: private:
186:
187: void Putbyte(unsigned char c);
188: void Putword(int w);
189: void Write(void *buf, size_t size);
190:
191: void prepare_encoder(void);
192: void BumpPixel(void);
193: int GIFNextPixel();
194: void compress(int init_bits);
195: void output(code_int code);
196: void cl_block(void);
197: void cl_hash(count_int hsize);
198: void char_init(void);
199: void char_out(int c);
200: void flush_char(void);
201:
202: private:
203:
204: gdImage& im;
205: gdGrowingBuf buf;
206:
207: int Width, Height;
208: int curx, cury;
209: long CountDown;
210: int Pass;
211: int Interlace;
212:
213: int g_init_bits;
214:
215: int ClearCode;
216: int EOFCode;
217:
218: int n_bits; /* number of bits/code */
219: int maxbits; /* user settable max # bits/code */
220:
221: code_int maxcode; /* maximum code, given n_bits */
222: code_int maxmaxcode; /* should NEVER generate this code */
223:
224: count_int htab [HSIZE];
225: unsigned short codetab [HSIZE];
226: code_int hsize; /* for dynamic table sizing */
227:
228: code_int free_ent; /* first unused entry */
229:
230: /*
231: * block compression parameters -- after all codes are used up,
232: * and compression rate changes, start over.
233: */
234: int clear_flg;
235:
236: int offset;
237: long int in_count; /* length of input */
238: long int out_count; /* # of codes output(for debugging) */
239:
240: unsigned long cur_accum;
241: int cur_bits;
242:
243: /*
244: * Number of characters so far in this 'packet'
245: */
246: int a_count;
247:
248: /*
249: * Define the storage for the packet accumulator
250: */
251: char accum[ 256 ];
252:
253: };
254:
255: inline int gdImage::BoundsSafe(int x, int y){
256: return(!(((y < 0) ||(y >= sy)) ||((x < 0) ||(x >= sx))));
257: }
258:
259: inline /*paf int*/void gdImage::DoSetPixel(int x, int y, int color){
260: if(BoundsSafe(x, y)) pixels[x][y] = (unsigned char)color;
261: }
262:
263: #endif
E-mail: