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