1 /* cgif.h -- a merge of some GIF-decoding files from giflib by E.S.Raymond
2  * by pts@fazekas.hu at Wed Feb 27 13:18:04 CET 2002
3 
4 The GIFLIB distribution is Copyright (c) 1997  Eric S. Raymond
5 
6 Permission is hereby granted, free of charge, to any person obtaining a copy
7 of this software and associated documentation files (the "Software"), to deal
8 in the Software without restriction, including without limitation the rights
9 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 copies of the Software, and to permit persons to whom the Software is
11 furnished to do so, subject to the following conditions:
12 
13 The above copyright notice and this permission notice shall be included in
14 all copies or substantial portions of the Software.
15 
16 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL THE
19 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22 THE SOFTWARE.
23  */
24 
25 /******************************************************************************
26 * In order to make life a little bit easier when using the GIF file format,   *
27 * this library was written, and which does all the dirty work...	      *
28 *									      *
29 *					Written by Gershon Elber,  Jun. 1989  *
30 *					Hacks by Eric S. Raymond,  Sep. 1992  *
31 *******************************************************************************
32 * History:								      *
33 * 14 Jun 89 - Version 1.0 by Gershon Elber.				      *
34 *  3 Sep 90 - Version 1.1 by Gershon Elber (Support for Gif89, Unique names). *
35 * 15 Sep 90 - Version 2.0 by Eric S. Raymond (Changes to suoport GIF slurp)   *
36 * 26 Jun 96 - Version 3.0 by Eric S. Raymond (Full GIF89 support)
37 ******************************************************************************/
38 
39 #ifndef CGIF_H
40 #define CGIF_H
41 
42 #ifdef __GNUC__
43 #ifndef __clang__
44 #pragma interface
45 #endif
46 #endif
47 
48 #include "config2.h"  /* PTS_const */
49 
50 #define GIF_LIB_VERSION	" Version 3.0, "
51 
52 #define	GIF_ERROR	0
53 #define GIF_OK		1
54 
55 #ifndef TRUE
56 #define TRUE		1
57 #define FALSE		0
58 #endif
59 
60 #ifndef NULL
61 #define NULL		0
62 #endif /* NULL */
63 
64 #if _MSC_VER > 1000
65 # undef  __PROTOTYPES__
66 # define __PROTOTYPES__ 1
67 # pragma warning(disable: 4127) /* conditional expression is constant */
68 # pragma warning(disable: 4244) /* =' : conversion from 'int ' to 'unsigned char ', possible loss of data */
69 #endif
70 
71 /* at Wed Dec 11 15:33:22 CET 2002 */
72 /* #define USE_CGIF_FDOPEN 1 */
73 #undef USE_CGIF_FDOPEN
74 
75 #ifdef __cplusplus
76 class CGIF { public:
77 #define EXTERN static
78 #else
79 #define EXTERN extern
80 #endif
81 
82 typedef	int		GifBooleanType;
83 typedef	unsigned char	GifPixelType;
84 typedef unsigned char *	GifRowType;
85 typedef unsigned char	GifByteType;
86 
87 #define GIF_MESSAGE(Msg) fprintf(stderr, "\n%s: %s\n", PROGRAM_NAME, Msg)
88 #define GIF_EXIT(Msg)	{ GIF_MESSAGE(Msg); exit(-3); }
89 
90 #ifdef SYSV
91 #define VoidPtr char *
92 #else
93 #define VoidPtr void *
94 #endif /* SYSV */
95 
96 typedef struct GifColorType {
97     GifByteType Red, Green, Blue;
98 } GifColorType;
99 
100 typedef struct ColorMapObject
101 {
102     int	ColorCount;
103     int BitsPerPixel;
104     GifColorType *Colors;		/* on malloc(3) heap */
105 }
106 ColorMapObject;
107 
108 typedef struct GifImageDesc {
109     int Left, Top, Width, Height,	/* Current image dimensions. */
110 	Interlace;			/* Sequential/Interlaced lines. */
111     ColorMapObject *ColorMap;		/* The local color map */
112 } GifImageDesc;
113 
114 struct SavedImage;
115 typedef struct GifFileType {
116     int SWidth, SHeight,		/* Screen dimensions. */
117 	SColorResolution, 		/* How many colors can we generate? */
118 	SBackGroundColor;		/* I hope you understand this one... */
119     ColorMapObject *SColorMap;		/* NULL if not exists. */
120     int ImageCount;			/* Number of current image */
121     /*GifImageDesc Image; */		/* Block describing current image */
122     struct SavedImage *SavedImages;	/* Use this to accumulate file state */
123     VoidPtr Private;	  		/* Don't mess with this! */
124 } GifFileType;
125 
126 typedef enum {
127     UNDEFINED_RECORD_TYPE,
128     SCREEN_DESC_RECORD_TYPE,
129     IMAGE_DESC_RECORD_TYPE,		/* Begin with ',' */
130     EXTENSION_RECORD_TYPE,		/* Begin with '!' */
131     TERMINATE_RECORD_TYPE		/* Begin with ';' */
132 } GifRecordType;
133 
134 /******************************************************************************
135 *  GIF89 extension function codes                                             *
136 ******************************************************************************/
137 
138 #define COMMENT_EXT_FUNC_CODE		0xfe	/* comment */
139 #define GRAPHICS_EXT_FUNC_CODE		0xf9	/* graphics control */
140 #define PLAINTEXT_EXT_FUNC_CODE		0x01	/* plaintext */
141 #define APPLICATION_EXT_FUNC_CODE	0xff	/* application block */
142 
143 /******************************************************************************
144 * O.K., here are the routines one can access in order to decode GIF file:     *
145 * (GIF_LIB file DGIF_LIB.C).						      *
146 ******************************************************************************/
147 
148 EXTERN GifFileType *DGifOpenFileName(const char *GifFileName);
149 #if USE_CGIF_FDOPEN
150 EXTERN GifFileType *DGifOpenFileHandle(int GifFileHandle);
151 #endif
152 EXTERN GifFileType *DGifOpenFILE(void/*FILE*/ *f);
153 EXTERN int DGifSlurp(GifFileType *GifFile);
154 EXTERN int DGifGetScreenDesc(GifFileType *GifFile);
155 EXTERN int DGifGetRecordType(GifFileType *GifFile, GifRecordType *GifType);
156 EXTERN int DGifGetImageDesc(GifFileType *GifFile);
157 EXTERN int DGifGetLine(GifFileType *GifFile, GifPixelType *GifLine, int GifLineLen);
158 EXTERN int DGifGetPixel(GifFileType *GifFile, GifPixelType GifPixel);
159 EXTERN int DGifGetComment(GifFileType *GifFile, char *GifComment);
160 EXTERN int DGifGetExtension(GifFileType *GifFile, int *GifExtCode,
161 						GifByteType **GifExtension);
162 EXTERN int DGifGetExtensionNext(GifFileType *GifFile, GifByteType **GifExtension);
163 EXTERN int DGifGetCode(GifFileType *GifFile, int *GifCodeSize,
164 						GifByteType **GifCodeBlock);
165 EXTERN int DGifGetCodeNext(GifFileType *GifFile, GifByteType **GifCodeBlock);
166 EXTERN int DGifGetLZCodes(GifFileType *GifFile, int *GifCode);
167 EXTERN int DGifCloseFile(GifFileType *GifFile);
168 
169 #define	D_GIF_ERR_OPEN_FAILED	101		/* And DGif possible errors. */
170 #define	D_GIF_ERR_READ_FAILED	102
171 #define	D_GIF_ERR_NOT_GIF_FILE	103
172 #define D_GIF_ERR_NO_SCRN_DSCR	104
173 #define D_GIF_ERR_NO_IMAG_DSCR	105
174 #define D_GIF_ERR_NO_COLOR_MAP	106
175 #define D_GIF_ERR_WRONG_RECORD	107
176 #define D_GIF_ERR_DATA_TOO_BIG	108
177 #define D_GIF_ERR_NOT_ENOUGH_MEM 109
178 #define D_GIF_ERR_CLOSE_FAILED	110
179 #define D_GIF_ERR_NOT_READABLE	111
180 #define D_GIF_ERR_IMAGE_DEFECT	112
181 #define D_GIF_ERR_EOF_TOO_SOON	113
182 
183 
184 /******************************************************************************
185 * O.K., here are the routines from GIF_LIB file GIF_ERR.C.		      *
186 ******************************************************************************/
187 EXTERN void PrintGifError(void);
188 EXTERN PTS_const char *GetGifError(void);
189 EXTERN int GifLastError(void);
190 
191 /*****************************************************************************
192  *
193  * Everything below this point is new after version 1.2, supporting `slurp
194  * mode' for doing I/O in two big belts with all the image-bashing in core.
195  *
196  *****************************************************************************/
197 
198 /******************************************************************************
199 * Color Map handling from ALLOCGIF.C					      *
200 ******************************************************************************/
201 
202 EXTERN ColorMapObject *MakeMapObject(int ColorCount, GifColorType *ColorMap);
203 EXTERN void FreeMapObject(ColorMapObject *Object);
204 
205 /******************************************************************************
206 * Support for the in-core structures allocation (slurp mode).		      *
207 ******************************************************************************/
208 
209 /* This is the in-core version of an extension record */
210 typedef struct {
211     /** Extension code:
212      * 0xf9 Graphics Control extension (not stored!)
213      * 0xfe Comment extension
214      * 0xff Netscape Loop extension (not stored!)
215      * .... ???
216      */
217     int		code;
218     int		ByteCount;
219     /**** pts ****/
220     GifByteType	*Bytes;		/* on malloc(3) heap */
221 } ExtensionBlock;
222 
223 /* This holds an image header, its unpacked raster bits, and extensions */
224 typedef struct SavedImage {
225     GifImageDesc	ImageDesc;
226 
227     /**** pts ****/
228     GifPixelType	*RasterBits;		/* on malloc(3) heap */
229     int			ExtensionBlockCount;
230     ExtensionBlock	*ExtensionBlocks;	/* on malloc(3) heap */
231 
232     /**** pts ****/
233     /** from Graphics Control extension: 0..63 */
234     unsigned char dispose;
235     /** from Graphics Control extension: 0..65535 */
236     unsigned short delay;
237     /** from Graphics Control extension: transparency index: -1 or 0..255 */
238     signed short transp;
239     /** from Netscape Loop extension: iteration count: 0..65535 */
240     unsigned short iter;
241 } SavedImage;
242 
243 EXTERN void MakeExtension(SavedImage *New, int Function);
244 EXTERN int AddExtensionBlock(SavedImage *New, int Len, GifByteType ExtData[]);
245 EXTERN void FreeExtension(SavedImage *Image);
246 
247 EXTERN SavedImage *MakeSavedImage(GifFileType *GifFile, SavedImage *CopyFrom);
248 EXTERN void FreeSavedImages(GifFileType *GifFile);
249 
250 #ifdef __cplusplus
251   static int _GifError;
252 };
253 #else
254   extern int _GifError;
255 #endif
256 
257 #endif /* CGIF_H */
258