XEmacs 21.2.4
[chise/xemacs-chise.git.1] / src / gifrlib.h
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 *                                             and Jareth Hein,     Jan. 1998  *
8 *******************************************************************************
9 * History:                                                                    *
10 * 14 Jun 89 - Version 1.0 by Gershon Elber.                                   *
11 *  3 Sep 90 - Version 1.1 by Gershon Elber (Support for Gif89, Unique names). *
12 * 15 Sep 90 - Version 2.0 by Eric S. Raymond (Changes to suoport GIF slurp)   *
13 * 26 Jun 96 - Version 3.0 by Eric S. Raymond (Full GIF89 support)             *
14 * 19 Jan 98 - Version 3.1 by Jareth Hein (Support for user-defined I/O).      *
15 ******************************************************************************/
16
17 #ifndef GIF_LIB_H
18 #define GIF_LIB_H
19
20 #define GIF_ERROR       0
21 #define GIF_OK          1
22
23 #ifndef TRUE
24 #define TRUE            1
25 #define FALSE           0
26 #endif
27
28 #ifndef NULL
29 #define NULL            0
30 #endif /* NULL */
31
32 #define GIF_FILE_BUFFER_SIZE 16384  /* Files uses bigger buffers than usual. */
33
34 typedef int             GifBooleanType;
35 typedef unsigned char   GifPixelType;
36 typedef unsigned char * GifRowType;
37 typedef unsigned char   GifByteType;
38
39 #ifdef SYSV
40 #define VoidPtr char *
41 #else
42 #define VoidPtr void *
43 #endif /* SYSV */
44
45 typedef struct GifColorType {
46     GifByteType Red, Green, Blue;
47 } GifColorType;
48
49 typedef struct ColorMapObject
50 {
51     int ColorCount;
52     int BitsPerPixel;
53     GifColorType *Colors;               /* on malloc(3) heap */
54 }
55 ColorMapObject;
56
57 typedef struct GifImageDesc {
58     int Left, Top, Width, Height,       /* Current image dimensions. */
59         Interlace;                      /* Sequential/Interlaced lines. */
60     ColorMapObject *ColorMap;           /* The local color map */
61 } GifImageDesc;
62
63 /* I/O operations.  If you roll your own, they need to be semantically equivilent to
64    fread/fwrite, with an additional paramater to hold data local to your method. */
65 typedef size_t (*Gif_rw_func)(GifByteType *buffer, size_t size, VoidPtr method_data);
66 /* Finish up stream. Non-zero return indicates failure */
67 typedef int (*Gif_close_func)(VoidPtr close_data);
68 /* Error handling function */
69 typedef void (*Gif_error_func)(const char *string, VoidPtr error_data);
70
71 typedef struct GifFileType {
72     int SWidth, SHeight,                /* Screen dimensions. */
73         SColorResolution,               /* How many colors can we generate? */
74         SBackGroundColor;               /* I hope you understand this one... */
75     ColorMapObject *SColorMap;          /* NULL if it doesn't exist. */
76     int ImageCount;                     /* Number of current image */
77     GifImageDesc Image;                 /* Block describing current image */
78     struct SavedImage *SavedImages;     /* Use this to accumulate file state */
79     VoidPtr Private;                    /* Don't mess with this! */
80     VoidPtr GifIO;                      /* Contains all information for I/O */
81 } GifFileType;
82
83 typedef enum {
84     UNDEFINED_RECORD_TYPE,
85     SCREEN_DESC_RECORD_TYPE,
86     IMAGE_DESC_RECORD_TYPE,             /* Begin with ',' */
87     EXTENSION_RECORD_TYPE,              /* Begin with '!' */
88     TERMINATE_RECORD_TYPE               /* Begin with ';' */
89 } GifRecordType;
90
91 /******************************************************************************
92 *  GIF89 extension function codes                                             *
93 ******************************************************************************/
94
95 #define COMMENT_EXT_FUNC_CODE           0xfe    /* comment */
96 #define GRAPHICS_EXT_FUNC_CODE          0xf9    /* graphics control */
97 #define PLAINTEXT_EXT_FUNC_CODE         0x01    /* plaintext */
98 #define APPLICATION_EXT_FUNC_CODE       0xff    /* application block */
99
100 /******************************************************************************
101 * IO related routines.  Defined in gif_io.c                                   *
102 ******************************************************************************/
103 GifFileType *GifSetup(void);
104 void GifFree(GifFileType *GifFile);
105 void GifSetReadFunc (GifFileType *GifFile, Gif_rw_func func, VoidPtr data);
106 void GifSetWriteFunc(GifFileType *GifFile, Gif_rw_func func, VoidPtr data);
107 void GifSetCloseFunc(GifFileType *GifFile, Gif_close_func func, VoidPtr data);
108
109 /******************************************************************************
110 * O.K., here are the routines one can access in order to decode GIF file:     *
111 ******************************************************************************/
112
113 void DGifOpenFileName(GifFileType *GifFile, const char *GifFileName);
114 void DGifOpenFileHandle(GifFileType *GifFile, int GifFileHandle);
115 void DGifInitRead(GifFileType *GifFile);
116 void DGifSlurp(GifFileType *GifFile);
117 void DGifGetScreenDesc(GifFileType *GifFile);
118 void DGifGetRecordType(GifFileType *GifFile, GifRecordType *GifType);
119 void DGifGetImageDesc(GifFileType *GifFile);
120 void DGifGetLine(GifFileType *GifFile, GifPixelType *GifLine, int GifLineLen);
121 void DGifGetPixel(GifFileType *GifFile, GifPixelType GifPixel);
122 void DGifGetComment(GifFileType *GifFile, char *GifComment);
123 void DGifGetExtension(GifFileType *GifFile, int *GifExtCode,
124                                                 GifByteType **GifExtension);
125 void DGifGetExtensionNext(GifFileType *GifFile, GifByteType **GifExtension);
126 void DGifGetCode(GifFileType *GifFile, int *GifCodeSize,
127                                                 GifByteType **GifCodeBlock);
128 void DGifGetCodeNext(GifFileType *GifFile, GifByteType **GifCodeBlock);
129 void DGifGetLZCodes(GifFileType *GifFile, int *GifCode);
130 int DGifCloseFile(GifFileType *GifFile);
131
132 #define D_GIF_ERR_OPEN_FAILED   101             /* And DGif possible errors. */
133 #define D_GIF_ERR_READ_FAILED   102
134 #define D_GIF_ERR_NOT_GIF_FILE  103
135 #define D_GIF_ERR_NO_SCRN_DSCR  104
136 #define D_GIF_ERR_NO_IMAG_DSCR  105
137 #define D_GIF_ERR_NO_COLOR_MAP  106
138 #define D_GIF_ERR_WRONG_RECORD  107
139 #define D_GIF_ERR_DATA_TOO_BIG  108
140 #define GIF_ERR_NOT_ENOUGH_MEM 109
141 #define D_GIF_ERR_NOT_ENOUGH_MEM 109
142 #define D_GIF_ERR_CLOSE_FAILED  110
143 #define D_GIF_ERR_NOT_READABLE  111
144 #define D_GIF_ERR_IMAGE_DEFECT  112
145 #define D_GIF_ERR_EOF_TOO_SOON  113
146
147 /******************************************************************************
148 * O.K., here are the error routines                                           *
149 ******************************************************************************/
150 extern void GifSetErrorFunc(GifFileType *GifFile, Gif_error_func func, VoidPtr data);
151 extern void GifSetWarningFunc(GifFileType *GifFile, Gif_error_func func, VoidPtr data);
152 extern void GifInternError(GifFileType *GifFile, int errnum);
153 extern void GifInternWarning(GifFileType *GifFile, int errnum);
154 extern void GifError(GifFileType *GifFile, const char *err_str);
155 extern void GifWarning(GifFileType *GifFile, const char *err_str);
156
157 /*****************************************************************************
158  *
159  * Everything below this point is new after version 1.2, supporting `slurp
160  * mode' for doing I/O in two big belts with all the image-bashing in core.
161  *
162  *****************************************************************************/
163
164 /******************************************************************************
165 * Support for the in-core structures allocation (slurp mode).                 *
166 ******************************************************************************/
167
168 /* This is the in-core version of an extension record */
169 typedef struct {
170     int             ByteCount;
171     GifByteType *Bytes;         /* on malloc(3) heap */
172 } ExtensionBlock;
173
174 /* This holds an image header, its unpacked raster bits, and extensions */
175 typedef struct SavedImage {
176     GifImageDesc        ImageDesc;
177
178     GifPixelType        *RasterBits;            /* on malloc(3) heap */
179
180     int                 Function;
181     int                 ExtensionBlockCount;
182     ExtensionBlock      *ExtensionBlocks;       /* on malloc(3) heap */
183 } SavedImage;
184
185 extern void ApplyTranslation(SavedImage *Image, GifPixelType Translation[]);
186
187 extern void MakeExtension(SavedImage *New, int Function);
188 extern int AddExtensionBlock(SavedImage *New, int Length, GifByteType *data);
189 extern void FreeExtension(SavedImage *Image);
190
191 extern SavedImage *MakeSavedImage(GifFileType *GifFile, SavedImage *CopyFrom);
192 extern void FreeSavedImages(GifFileType *GifFile);
193
194 /*   Common defines used by encode/decode functions */
195
196 #define COMMENT_EXT_FUNC_CODE   0xfe /* Extension function code for comment. */
197 #define GIF_STAMP       "GIFVER"         /* First chars in file - GIF stamp. */
198 #define GIF_STAMP_LEN   sizeof(GIF_STAMP) - 1
199 #define GIF_VERSION_POS 3               /* Version first character in stamp. */
200 #define GIF87_STAMP     "GIF87a"         /* First chars in file - GIF stamp. */
201 #define GIF89_STAMP     "GIF89a"         /* First chars in file - GIF stamp. */
202
203 #define LZ_MAX_CODE     4095            /* Biggest code possible in 12 bits. */
204 #define LZ_BITS         12
205
206 #define FILE_STATE_READ         0x01
207 #define FILE_STATE_WRITE        0x01
208 #define FILE_STATE_SCREEN       0x02
209 #define FILE_STATE_IMAGE        0x04
210
211 #define FLUSH_OUTPUT            4096    /* Impossible code, to signal flush. */
212 #define FIRST_CODE              4097    /* Impossible code, to signal first. */
213 #define NO_SUCH_CODE            4098    /* Impossible code, to signal empty. */
214
215 #define IS_READABLE(Private)    (!(Private->FileState & FILE_STATE_READ))
216 #define IS_WRITEABLE(Private)   (Private->FileState & FILE_STATE_WRITE)
217
218 typedef struct GifFilePrivateType {
219     int FileState,
220         BitsPerPixel,       /* Bits per pixel (Codes uses at list this + 1). */
221         ClearCode,                                     /* The CLEAR LZ code. */
222         EOFCode,                                         /* The EOF LZ code. */
223         RunningCode,                /* The next code algorithm can generate. */
224         RunningBits,/* The number of bits required to represent RunningCode. */
225         MaxCode1,  /* 1 bigger than max. possible code, in RunningBits bits. */
226         LastCode,                       /* The code before the current code. */
227         CrntCode,                                 /* Current algorithm code. */
228         StackPtr,                        /* For character stack (see below). */
229         CrntShiftState;                 /* Number of bits in CrntShiftDWord. */
230     unsigned long CrntShiftDWord;     /* For bytes decomposition into codes. */
231     unsigned long PixelCount;                  /* Number of pixels in image. */
232     GifByteType Buf[256];              /* Compressed input is buffered here. */
233     GifByteType Stack[LZ_MAX_CODE];      /* Decoded pixels are stacked here. */
234     GifByteType Suffix[LZ_MAX_CODE+1];         /* So we can trace the codes. */
235     unsigned int Prefix[LZ_MAX_CODE+1];
236 } GifFilePrivateType;
237
238 typedef struct GifIODataType {
239     Gif_rw_func ReadFunc, WriteFunc;    /* Pointers to the functions that will do the I/O */
240     Gif_close_func CloseFunc;    
241     VoidPtr ReadFunc_data;              /* data to be passed to the read function */
242     VoidPtr WriteFunc_data;             /* data to be passed to the write function */
243     VoidPtr CloseFunc_data;             /* data to be passed to the close function */
244     Gif_error_func ErrorFunc;   /* MUST NOT RETURN (use lng_jmp or exit)!  */
245     Gif_error_func WarningFunc; /* For warning messages (can be ignored) */
246     VoidPtr ErrorFunc_data;
247     VoidPtr WarningFunc_data;
248 } GifIODataType;
249
250 typedef struct GifStdIODataType {
251   FILE *File;
252   int FileHandle;
253 } GifStdIODataType;
254
255 /* Install StdIO funcs on FILE into GifFile */
256 void GifStdIOInit(GifFileType *GifFile, FILE *file, int filehandle);
257
258 /* Error checking reads, writes and closes */
259 void GifRead(GifByteType *buf, size_t size, GifFileType *GifFile);
260 void GifWrite(GifByteType *buf, size_t size, GifFileType *GifFile);
261 int GifClose(GifFileType *GifFile);
262
263 /* The default Read and Write functions for files */
264 size_t GifStdRead(GifByteType *buf, size_t size, VoidPtr method_data);
265 size_t GifStdWrite(GifByteType *buf, size_t size, VoidPtr method_data);
266 int GifStdFileClose(VoidPtr method_data);
267
268 ColorMapObject *MakeMapObject(int ColorCount, GifColorType *ColorMap);
269 void FreeMapObject(ColorMapObject *Object);
270
271
272 #endif /* GIF_LIB_H */