Annotation of parser3/src/classes/gd/gif.h, revision 1.23.2.5.2.2

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

E-mail: