1 /******************************************************************************
2  * In order to make life a little bit easier when using the GIF file format,
3  * this library was written, and which does all the dirty work...
4  *
5  *                                        Written by Gershon Elber,  Jun. 1989
6  *                                        Hacks by Eric S. Raymond,  Sep. 1992
7  ******************************************************************************
8  * History:
9  * 14 Jun 89 - Version 1.0 by Gershon Elber.
10  *  3 Sep 90 - Version 1.1 by Gershon Elber (Support for Gif89, Unique names)
11  * 15 Sep 90 - Version 2.0 by Eric S. Raymond (Changes to suoport GIF slurp)
12  * 26 Jun 96 - Version 3.0 by Eric S. Raymond (Full GIF89 support)
13  * 17 Dec 98 - Version 4.0 by Toshio Kuratomi (Fix extension writing code)
14  *****************************************************************************/
15 
16 #ifndef _GIF_LIB_H_
17 #define _GIF_LIB_H_ 1
18 
19 #ifdef __cplusplus
20 extern "C" {
21 #endif /* __cplusplus */
22 
23 #define GIF_LIB_VERSION " Version 4.1, "
24 
25 #define GIF_ERROR   0
26 #define GIF_OK      1
27 
28 #ifndef TRUE
29 #define TRUE        1
30 #endif /* TRUE */
31 #ifndef FALSE
32 #define FALSE       0
33 #endif /* FALSE */
34 
35 #ifndef NULL
36 #define NULL        0
37 #endif /* NULL */
38 
39 #define GIF_STAMP "GIFVER"          /* First chars in file - GIF stamp.  */
40 #define GIF_STAMP_LEN sizeof(GIF_STAMP) - 1
41 #define GIF_VERSION_POS 3           /* Version first character in stamp. */
42 #define GIF87_STAMP "GIF87a"        /* First chars in file - GIF stamp.  */
43 #define GIF89_STAMP "GIF89a"        /* First chars in file - GIF stamp.  */
44 
45 #define GIF_FILE_BUFFER_SIZE 16384  /* Files uses bigger buffers than usual. */
46 
47 typedef int GifBooleanType;
48 typedef unsigned char GifPixelType;
49 typedef unsigned char *GifRowType;
50 typedef unsigned char GifByteType;
51 
52 #define GIF_MESSAGE(Msg) fprintf(stderr, "\n%s: %s\n", PROGRAM_NAME, Msg)
53 #define GIF_EXIT(Msg)    { GIF_MESSAGE(Msg); exit(-3); }
54 
55 #ifdef SYSV
56 #define VoidPtr char *
57 #else
58 #define VoidPtr void *
59 #endif /* SYSV */
60 
61 typedef struct GifColorType {
62     GifByteType Red, Green, Blue;
63 } GifColorType;
64 
65 typedef struct ColorMapObject {
66     int ColorCount;
67     int BitsPerPixel;
68     GifColorType *Colors;    /* on malloc(3) heap */
69 } ColorMapObject;
70 
71 typedef struct GifImageDesc {
72     int Left, Top, Width, Height,   /* Current image dimensions. */
73       Interlace;                    /* Sequential/Interlaced lines. */
74     ColorMapObject *ColorMap;       /* The local color map */
75 } GifImageDesc;
76 
77 typedef struct GifFileType {
78     int SWidth, SHeight,        /* Screen dimensions. */
79       SColorResolution,         /* How many colors can we generate? */
80       SBackGroundColor;         /* I hope you understand this one... */
81     ColorMapObject *SColorMap;  /* NULL if not exists. */
82     int ImageCount;             /* Number of current image */
83     GifImageDesc Image;         /* Block describing current image */
84     struct SavedImage *SavedImages; /* Use this to accumulate file state */
85     VoidPtr UserData;           /* hook to attach user data (TVT) */
86     VoidPtr Private;            /* Don't mess with this! */
87 } GifFileType;
88 
89 typedef enum {
90     UNDEFINED_RECORD_TYPE,
91     SCREEN_DESC_RECORD_TYPE,
92     IMAGE_DESC_RECORD_TYPE, /* Begin with ',' */
93     EXTENSION_RECORD_TYPE,  /* Begin with '!' */
94     TERMINATE_RECORD_TYPE   /* Begin with ';' */
95 } GifRecordType;
96 
97 /* DumpScreen2Gif routine constants identify type of window/screen to dump.
98  * Note all values below 1000 are reserved for the IBMPC different display
99  * devices (it has many!) and are compatible with the numbering TC2.0
100  * (Turbo C 2.0 compiler for IBM PC) gives to these devices.
101  */
102 typedef enum {
103     GIF_DUMP_SGI_WINDOW = 1000,
104     GIF_DUMP_X_WINDOW = 1001
105 } GifScreenDumpType;
106 
107 /* func type to read gif data from arbitrary sources (TVT) */
108 typedef int (*InputFunc) (GifFileType *, GifByteType *, int);
109 
110 /* func type to write gif data ro arbitrary targets.
111  * Returns count of bytes written. (MRB)
112  */
113 typedef int (*OutputFunc) (GifFileType *, const GifByteType *, int);
114 
115 /******************************************************************************
116  *  GIF89 extension function codes
117 ******************************************************************************/
118 
119 #define COMMENT_EXT_FUNC_CODE     0xfe    /* comment */
120 #define GRAPHICS_EXT_FUNC_CODE    0xf9    /* graphics control */
121 #define PLAINTEXT_EXT_FUNC_CODE   0x01    /* plaintext */
122 #define APPLICATION_EXT_FUNC_CODE 0xff    /* application block */
123 
124 /******************************************************************************
125  * O.K., here are the routines one can access in order to encode GIF file:
126  * (GIF_LIB file EGIF_LIB.C).
127 ******************************************************************************/
128 
129 GifFileType *EGifOpenFileName(const char *GifFileName,
130                               int GifTestExistance);
131 GifFileType *EGifOpenFileHandle(int GifFileHandle);
132 GifFileType *EGifOpen(void *userPtr, OutputFunc writeFunc);
133 
134 int EGifSpew(GifFileType * GifFile);
135 void EGifSetGifVersion(const char *Version);
136 int EGifPutScreenDesc(GifFileType * GifFile,
137                       int GifWidth, int GifHeight, int GifColorRes,
138                       int GifBackGround,
139                       const ColorMapObject * GifColorMap);
140 int EGifPutImageDesc(GifFileType * GifFile, int GifLeft, int GifTop,
141                      int Width, int GifHeight, int GifInterlace,
142                      const ColorMapObject * GifColorMap);
143 int EGifPutLine(GifFileType * GifFile, GifPixelType * GifLine,
144                 int GifLineLen);
145 int EGifPutPixel(GifFileType * GifFile, GifPixelType GifPixel);
146 int EGifPutComment(GifFileType * GifFile, const char *GifComment);
147 int EGifPutExtensionFirst(GifFileType * GifFile, int GifExtCode,
148                           int GifExtLen, const VoidPtr GifExtension);
149 int EGifPutExtensionNext(GifFileType * GifFile, int GifExtCode,
150                          int GifExtLen, const VoidPtr GifExtension);
151 int EGifPutExtensionLast(GifFileType * GifFile, int GifExtCode,
152                          int GifExtLen, const VoidPtr GifExtension);
153 int EGifPutExtension(GifFileType * GifFile, int GifExtCode, int GifExtLen,
154                      const VoidPtr GifExtension);
155 int EGifPutCode(GifFileType * GifFile, int GifCodeSize,
156                 const GifByteType * GifCodeBlock);
157 int EGifPutCodeNext(GifFileType * GifFile,
158                     const GifByteType * GifCodeBlock);
159 int EGifCloseFile(GifFileType * GifFile);
160 
161 #define E_GIF_ERR_OPEN_FAILED    1    /* And EGif possible errors. */
162 #define E_GIF_ERR_WRITE_FAILED   2
163 #define E_GIF_ERR_HAS_SCRN_DSCR  3
164 #define E_GIF_ERR_HAS_IMAG_DSCR  4
165 #define E_GIF_ERR_NO_COLOR_MAP   5
166 #define E_GIF_ERR_DATA_TOO_BIG   6
167 #define E_GIF_ERR_NOT_ENOUGH_MEM 7
168 #define E_GIF_ERR_DISK_IS_FULL   8
169 #define E_GIF_ERR_CLOSE_FAILED   9
170 #define E_GIF_ERR_NOT_WRITEABLE  10
171 
172 /******************************************************************************
173  * O.K., here are the routines one can access in order to decode GIF file:
174  * (GIF_LIB file DGIF_LIB.C).
175  *****************************************************************************/
176 
177 GifFileType *DGifOpenFileName(const char *GifFileName);
178 GifFileType *DGifOpenFileHandle(int GifFileHandle);
179 GifFileType *DGifOpen(void *userPtr, InputFunc readFunc);    /* new one
180                                                              * (TVT) */
181 int DGifSlurp(GifFileType * GifFile);
182 int DGifGetScreenDesc(GifFileType * GifFile);
183 int DGifGetRecordType(GifFileType * GifFile, GifRecordType * GifType);
184 int DGifGetImageDesc(GifFileType * GifFile);
185 int DGifGetLine(GifFileType * GifFile, GifPixelType * GifLine, int GifLineLen);
186 int DGifGetPixel(GifFileType * GifFile, GifPixelType GifPixel);
187 int DGifGetComment(GifFileType * GifFile, char *GifComment);
188 int DGifGetExtension(GifFileType * GifFile, int *GifExtCode,
189                      GifByteType ** GifExtension);
190 int DGifGetExtensionNext(GifFileType * GifFile, GifByteType ** GifExtension);
191 int DGifGetCode(GifFileType * GifFile, int *GifCodeSize,
192                 GifByteType ** GifCodeBlock);
193 int DGifGetCodeNext(GifFileType * GifFile, GifByteType ** GifCodeBlock);
194 int DGifGetLZCodes(GifFileType * GifFile, int *GifCode);
195 int DGifCloseFile(GifFileType * GifFile);
196 
197 #define D_GIF_ERR_OPEN_FAILED    101    /* And DGif possible errors. */
198 #define D_GIF_ERR_READ_FAILED    102
199 #define D_GIF_ERR_NOT_GIF_FILE   103
200 #define D_GIF_ERR_NO_SCRN_DSCR   104
201 #define D_GIF_ERR_NO_IMAG_DSCR   105
202 #define D_GIF_ERR_NO_COLOR_MAP   106
203 #define D_GIF_ERR_WRONG_RECORD   107
204 #define D_GIF_ERR_DATA_TOO_BIG   108
205 #define D_GIF_ERR_NOT_ENOUGH_MEM 109
206 #define D_GIF_ERR_CLOSE_FAILED   110
207 #define D_GIF_ERR_NOT_READABLE   111
208 #define D_GIF_ERR_IMAGE_DEFECT   112
209 #define D_GIF_ERR_EOF_TOO_SOON   113
210 
211 /******************************************************************************
212  * O.K., here are the routines from GIF_LIB file QUANTIZE.C.
213 ******************************************************************************/
214 int QuantizeBuffer(unsigned int Width, unsigned int Height,
215                    int *ColorMapSize, GifByteType * RedInput,
216                    GifByteType * GreenInput, GifByteType * BlueInput,
217                    GifByteType * OutputBuffer,
218                    GifColorType * OutputColorMap);
219 
220 /******************************************************************************
221  * O.K., here are the routines from GIF_LIB file QPRINTF.C.
222 ******************************************************************************/
223 extern int GifQuietPrint;
224 
225 #ifdef HAVE_STDARG_H
226     extern void GifQprintf(char *Format, ...);
227 #elif defined (HAVE_VARARGS_H)
228     extern void GifQprintf();
229 #endif /* HAVE_STDARG_H */
230 
231 /******************************************************************************
232  * O.K., here are the routines from GIF_LIB file GIF_ERR.C.
233 ******************************************************************************/
234 extern void PrintGifError(void);
235 extern int GifLastError(void);
236 
237 /******************************************************************************
238  * O.K., here are the routines from GIF_LIB file DEV2GIF.C.
239 ******************************************************************************/
240 extern int DumpScreen2Gif(const char *FileName,
241                           int ReqGraphDriver,
242                           long ReqGraphMode1,
243                           long ReqGraphMode2,
244                           long ReqGraphMode3);
245 
246 /*****************************************************************************
247  *
248  * Everything below this point is new after version 1.2, supporting `slurp
249  * mode' for doing I/O in two big belts with all the image-bashing in core.
250  *
251  *****************************************************************************/
252 
253 /******************************************************************************
254  * Color Map handling from ALLOCGIF.C
255  *****************************************************************************/
256 
257 extern ColorMapObject *MakeMapObject(int ColorCount,
258                                      const GifColorType * ColorMap);
259 extern void FreeMapObject(ColorMapObject * Object);
260 extern ColorMapObject *UnionColorMap(const ColorMapObject * ColorIn1,
261                                      const ColorMapObject * ColorIn2,
262                                      GifPixelType ColorTransIn2[]);
263 extern int BitSize(int n);
264 
265 /******************************************************************************
266  * Support for the in-core structures allocation (slurp mode).
267  *****************************************************************************/
268 
269 /* This is the in-core version of an extension record */
270 typedef struct {
271     int ByteCount;
272     char *Bytes;    /* on malloc(3) heap */
273     int Function;   /* Holds the type of the Extension block. */
274 } ExtensionBlock;
275 
276 /* This holds an image header, its unpacked raster bits, and extensions */
277 typedef struct SavedImage {
278     GifImageDesc ImageDesc;
279     unsigned char *RasterBits;  /* on malloc(3) heap */
280     int Function;   /* DEPRECATED: Use ExtensionBlocks[x].Function instead */
281     int ExtensionBlockCount;
282     ExtensionBlock *ExtensionBlocks;    /* on malloc(3) heap */
283 } SavedImage;
284 
285 extern void ApplyTranslation(SavedImage * Image, GifPixelType Translation[]);
286 extern void MakeExtension(SavedImage * New, int Function);
287 extern int AddExtensionBlock(SavedImage * New, int Len,
288                              unsigned char ExtData[]);
289 extern void FreeExtension(SavedImage * Image);
290 extern SavedImage *MakeSavedImage(GifFileType * GifFile,
291                                   const SavedImage * CopyFrom);
292 extern void FreeSavedImages(GifFileType * GifFile);
293 
294 /******************************************************************************
295  * The library's internal utility font
296  *****************************************************************************/
297 
298 #define GIF_FONT_WIDTH  8
299 #define GIF_FONT_HEIGHT 8
300 extern unsigned char AsciiTable[][GIF_FONT_WIDTH];
301 
302 extern void DrawText(SavedImage * Image,
303                      const int x, const int y,
304                      const char *legend, const int color);
305 
306 extern void DrawBox(SavedImage * Image,
307                     const int x, const int y,
308                     const int w, const int d, const int color);
309 
310 void DrawRectangle(SavedImage * Image,
311                    const int x, const int y,
312                    const int w, const int d, const int color);
313 
314 extern void DrawBoxedText(SavedImage * Image,
315                           const int x, const int y,
316                           const char *legend,
317                           const int border, const int bg, const int fg);
318 
319 #ifdef __cplusplus
320 }
321 #endif /* __cplusplus */
322 #endif /* _GIF_LIB_H */
323