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