Move src/objects.c to src/ui
[sxemacs] / src / ui / 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         int ColorCount;
47         int BitsPerPixel;
48         GifColorType *Colors;   /* on malloc(3) heap */
49 } ColorMapObject;
50
51 typedef struct GifImageDesc {
52         int Left, Top, Width, Height,   /* Current image dimensions. */
53          Interlace;             /* Sequential/Interlaced lines. */
54         ColorMapObject *ColorMap;       /* The local color map */
55 } GifImageDesc;
56
57 /* I/O operations.  If you roll your own, they need to be semantically equivilent to
58    fread/fwrite, with an additional paramater to hold data local to your method. */
59 typedef size_t(*Gif_rw_func) (GifByteType * buffer, size_t size,
60                               VoidPtr method_data);
61 /* Finish up stream. Non-zero return indicates failure */
62 typedef int (*Gif_close_func) (VoidPtr close_data);
63 /* Error handling function */
64 typedef void (*Gif_error_func) (const char *string, VoidPtr error_data);
65
66 typedef struct GifFileType {
67         int SWidth, SHeight,    /* Screen dimensions. */
68          SColorResolution,      /* How many colors can we generate? */
69          SBackGroundColor;      /* I hope you understand this one... */
70         ColorMapObject *SColorMap;      /* NULL if it doesn't exist. */
71         int ImageCount;         /* Number of current image */
72         GifImageDesc Image;     /* Block describing current image */
73         struct SavedImage *SavedImages; /* Use this to accumulate file state */
74         VoidPtr Private;        /* Don't mess with this! */
75         VoidPtr GifIO;          /* Contains all information for I/O */
76 } GifFileType;
77
78 typedef enum {
79         UNDEFINED_RECORD_TYPE,
80         SCREEN_DESC_RECORD_TYPE,
81         IMAGE_DESC_RECORD_TYPE, /* Begin with ',' */
82         EXTENSION_RECORD_TYPE,  /* Begin with '!' */
83         TERMINATE_RECORD_TYPE   /* Begin with ';' */
84 } GifRecordType;
85
86 /******************************************************************************
87 *  GIF89 extension function codes                                             *
88 ******************************************************************************/
89
90 #define COMMENT_EXT_FUNC_CODE           0xfe    /* comment */
91 #define GRAPHICS_EXT_FUNC_CODE          0xf9    /* graphics control */
92 #define PLAINTEXT_EXT_FUNC_CODE         0x01    /* plaintext */
93 #define APPLICATION_EXT_FUNC_CODE       0xff    /* application block */
94
95 /******************************************************************************
96 * IO related routines.  Defined in gif_io.c                                   *
97 ******************************************************************************/
98 GifFileType *GifSetup(void);
99 void GifFree(GifFileType * GifFile);
100 void GifSetReadFunc(GifFileType * GifFile, Gif_rw_func func, VoidPtr data);
101 void GifSetWriteFunc(GifFileType * GifFile, Gif_rw_func func, VoidPtr data);
102 void GifSetCloseFunc(GifFileType * GifFile, Gif_close_func func, VoidPtr data);
103
104 /******************************************************************************
105 * O.K., here are the routines one can access in order to decode GIF file:     *
106 ******************************************************************************/
107
108 void DGifOpenFileName(GifFileType * GifFile, const char *GifFileName);
109 void DGifOpenFileHandle(GifFileType * GifFile, int GifFileHandle);
110 void DGifInitRead(GifFileType * GifFile);
111 void DGifSlurp(GifFileType * GifFile);
112 void DGifGetScreenDesc(GifFileType * GifFile);
113 void DGifGetRecordType(GifFileType * GifFile, GifRecordType * GifType);
114 void DGifGetImageDesc(GifFileType * GifFile);
115 void DGifGetLine(GifFileType * GifFile, GifPixelType * GifLine, int GifLineLen);
116 void DGifGetPixel(GifFileType * GifFile, GifPixelType GifPixel);
117 void DGifGetComment(GifFileType * GifFile, char *GifComment);
118 void DGifGetExtension(GifFileType * GifFile, int *GifExtCode,
119                       GifByteType ** GifExtension);
120 void DGifGetExtensionNext(GifFileType * GifFile, GifByteType ** GifExtension);
121 void DGifGetCode(GifFileType * GifFile, int *GifCodeSize,
122                  GifByteType ** GifCodeBlock);
123 void DGifGetCodeNext(GifFileType * GifFile, GifByteType ** GifCodeBlock);
124 void DGifGetLZCodes(GifFileType * GifFile, int *GifCode);
125 int DGifCloseFile(GifFileType * GifFile);
126
127 #define D_GIF_ERR_OPEN_FAILED   101     /* And DGif possible errors. */
128 #define D_GIF_ERR_READ_FAILED   102
129 #define D_GIF_ERR_NOT_GIF_FILE  103
130 #define D_GIF_ERR_NO_SCRN_DSCR  104
131 #define D_GIF_ERR_NO_IMAG_DSCR  105
132 #define D_GIF_ERR_NO_COLOR_MAP  106
133 #define D_GIF_ERR_WRONG_RECORD  107
134 #define D_GIF_ERR_DATA_TOO_BIG  108
135 #define GIF_ERR_NOT_ENOUGH_MEM 109
136 #define D_GIF_ERR_NOT_ENOUGH_MEM 109
137 #define D_GIF_ERR_CLOSE_FAILED  110
138 #define D_GIF_ERR_NOT_READABLE  111
139 #define D_GIF_ERR_IMAGE_DEFECT  112
140 #define D_GIF_ERR_EOF_TOO_SOON  113
141
142 /******************************************************************************
143 * O.K., here are the error routines                                           *
144 ******************************************************************************/
145 extern void GifSetErrorFunc(GifFileType * GifFile, Gif_error_func func,
146                             VoidPtr data);
147 extern void GifSetWarningFunc(GifFileType * GifFile, Gif_error_func func,
148                               VoidPtr data);
149 extern void GifInternError(GifFileType * GifFile, int errnum);
150 extern void GifInternWarning(GifFileType * GifFile, int errnum);
151 extern void GifError(GifFileType * GifFile, const char *err_str);
152 extern void GifWarning(GifFileType * GifFile, const char *err_str);
153
154 /*****************************************************************************
155  *
156  * Everything below this point is new after version 1.2, supporting `slurp
157  * mode' for doing I/O in two big belts with all the image-bashing in core.
158  *
159  *****************************************************************************/
160
161 /******************************************************************************
162 * Support for the in-core structures allocation (slurp mode).                 *
163 ******************************************************************************/
164
165 /* This is the in-core version of an extension record */
166 typedef struct {
167         int ByteCount;
168         GifByteType *Bytes;     /* on malloc(3) heap */
169 } ExtensionBlock;
170
171 /* This holds an image header, its unpacked raster bits, and extensions */
172 typedef struct SavedImage {
173         GifImageDesc ImageDesc;
174
175         GifPixelType *RasterBits;       /* on malloc(3) heap */
176
177         int Function;
178         int ExtensionBlockCount;
179         ExtensionBlock *ExtensionBlocks;        /* on malloc(3) heap */
180 } SavedImage;
181
182 extern void ApplyTranslation(SavedImage * Image, GifPixelType Translation[]);
183
184 extern void MakeExtension(SavedImage * New, int Function);
185 extern int AddExtensionBlock(SavedImage * New, int Length, GifByteType * data);
186 extern void FreeExtension(SavedImage * Image);
187
188 extern SavedImage *MakeSavedImage(GifFileType * GifFile, SavedImage * CopyFrom);
189 extern void FreeSavedImages(GifFileType * GifFile);
190
191 /*   Common defines used by encode/decode functions */
192
193 #define COMMENT_EXT_FUNC_CODE   0xfe    /* Extension function code for comment. */
194 #define GIF_STAMP       "GIFVER"        /* First chars in file - GIF stamp. */
195 #define GIF_STAMP_LEN   sizeof(GIF_STAMP) - 1
196 #define GIF_VERSION_POS 3       /* Version first character in stamp. */
197 #define GIF87_STAMP     "GIF87a"        /* First chars in file - GIF stamp. */
198 #define GIF89_STAMP     "GIF89a"        /* First chars in file - GIF stamp. */
199
200 #define LZ_MAX_CODE     4095    /* Biggest code possible in 12 bits. */
201 #define LZ_BITS         12
202
203 #define FILE_STATE_READ         0x01
204 #define FILE_STATE_WRITE        0x01
205 #define FILE_STATE_SCREEN       0x02
206 #define FILE_STATE_IMAGE        0x04
207
208 #define FLUSH_OUTPUT            4096    /* Impossible code, to signal flush. */
209 #define FIRST_CODE              4097    /* Impossible code, to signal first. */
210 #define NO_SUCH_CODE            4098    /* Impossible code, to signal empty. */
211
212 #define IS_READABLE(Private)    (!(Private->FileState & FILE_STATE_READ))
213 #define IS_WRITEABLE(Private)   (Private->FileState & FILE_STATE_WRITE)
214
215 typedef struct GifFilePrivateType {
216         int FileState, 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_ */