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