1 | /****************************************************************************** |
2 | |
3 | gif_lib.h - service library for decoding and encoding GIF images |
4 | |
5 | SPDX-License-Identifier: MIT |
6 | |
7 | *****************************************************************************/ |
8 | |
9 | #ifndef _GIF_LIB_H_ |
10 | #define _GIF_LIB_H_ 1 |
11 | |
12 | #ifdef __cplusplus |
13 | extern "C" { |
14 | #endif /* __cplusplus */ |
15 | |
16 | #define GIFLIB_MAJOR 5 |
17 | #define GIFLIB_MINOR 2 |
18 | #define GIFLIB_RELEASE 1 |
19 | |
20 | #define GIF_ERROR 0 |
21 | #define GIF_OK 1 |
22 | |
23 | #include <stddef.h> |
24 | #include <stdbool.h> |
25 | |
26 | #define GIF_STAMP "GIFVER" /* First chars in file - GIF stamp. */ |
27 | #define GIF_STAMP_LEN sizeof(GIF_STAMP) - 1 |
28 | #define GIF_VERSION_POS 3 /* Version first character in stamp. */ |
29 | #define GIF87_STAMP "GIF87a" /* First chars in file - GIF stamp. */ |
30 | #define GIF89_STAMP "GIF89a" /* First chars in file - GIF stamp. */ |
31 | |
32 | typedef unsigned char GifPixelType; |
33 | typedef unsigned char *GifRowType; |
34 | typedef unsigned char GifByteType; |
35 | typedef unsigned int GifPrefixType; |
36 | typedef int GifWord; |
37 | |
38 | typedef struct GifColorType { |
39 | GifByteType Red, Green, Blue; |
40 | } GifColorType; |
41 | |
42 | typedef struct ColorMapObject { |
43 | int ColorCount; |
44 | int BitsPerPixel; |
45 | bool SortFlag; |
46 | GifColorType *Colors; /* on malloc(3) heap */ |
47 | } ColorMapObject; |
48 | |
49 | typedef struct GifImageDesc { |
50 | GifWord Left, Top, Width, Height; /* Current image dimensions. */ |
51 | bool Interlace; /* Sequential/Interlaced lines. */ |
52 | ColorMapObject *ColorMap; /* The local color map */ |
53 | } GifImageDesc; |
54 | |
55 | typedef struct ExtensionBlock { |
56 | int ByteCount; |
57 | GifByteType *Bytes; /* on malloc(3) heap */ |
58 | int Function; /* The block function code */ |
59 | #define CONTINUE_EXT_FUNC_CODE 0x00 /* continuation subblock */ |
60 | #define 0xfe /* comment */ |
61 | #define GRAPHICS_EXT_FUNC_CODE 0xf9 /* graphics control (GIF89) */ |
62 | #define PLAINTEXT_EXT_FUNC_CODE 0x01 /* plaintext */ |
63 | #define APPLICATION_EXT_FUNC_CODE 0xff /* application block (GIF89) */ |
64 | } ExtensionBlock; |
65 | |
66 | typedef struct SavedImage { |
67 | GifImageDesc ImageDesc; |
68 | GifByteType *RasterBits; /* on malloc(3) heap */ |
69 | int ExtensionBlockCount; /* Count of extensions before image */ |
70 | ExtensionBlock *ExtensionBlocks; /* Extensions before image */ |
71 | } SavedImage; |
72 | |
73 | typedef struct GifFileType { |
74 | GifWord SWidth, SHeight; /* Size of virtual canvas */ |
75 | GifWord SColorResolution; /* How many colors can we generate? */ |
76 | GifWord SBackGroundColor; /* Background color for virtual canvas */ |
77 | GifByteType AspectByte; /* Used to compute pixel aspect ratio */ |
78 | ColorMapObject *SColorMap; /* Global colormap, NULL if nonexistent. */ |
79 | int ImageCount; /* Number of current image (both APIs) */ |
80 | GifImageDesc Image; /* Current image (low-level API) */ |
81 | SavedImage *SavedImages; /* Image sequence (high-level API) */ |
82 | int ExtensionBlockCount; /* Count extensions past last image */ |
83 | ExtensionBlock *ExtensionBlocks; /* Extensions past last image */ |
84 | int Error; /* Last error condition reported */ |
85 | void *UserData; /* hook to attach user data (TVT) */ |
86 | void *Private; /* Don't mess with this! */ |
87 | } GifFileType; |
88 | |
89 | #define GIF_ASPECT_RATIO(n) ((n)+15.0/64.0) |
90 | |
91 | typedef enum { |
92 | UNDEFINED_RECORD_TYPE, |
93 | SCREEN_DESC_RECORD_TYPE, |
94 | IMAGE_DESC_RECORD_TYPE, /* Begin with ',' */ |
95 | EXTENSION_RECORD_TYPE, /* Begin with '!' */ |
96 | TERMINATE_RECORD_TYPE /* Begin with ';' */ |
97 | } GifRecordType; |
98 | |
99 | /* func type to read gif data from arbitrary sources (TVT) */ |
100 | typedef int (*InputFunc) (GifFileType *, GifByteType *, int); |
101 | |
102 | /* func type to write gif data to arbitrary targets. |
103 | * Returns count of bytes written. (MRB) |
104 | */ |
105 | typedef int (*OutputFunc) (GifFileType *, const GifByteType *, int); |
106 | |
107 | /****************************************************************************** |
108 | GIF89 structures |
109 | ******************************************************************************/ |
110 | |
111 | typedef struct GraphicsControlBlock { |
112 | int DisposalMode; |
113 | #define DISPOSAL_UNSPECIFIED 0 /* No disposal specified. */ |
114 | #define DISPOSE_DO_NOT 1 /* Leave image in place */ |
115 | #define DISPOSE_BACKGROUND 2 /* Set area too background color */ |
116 | #define DISPOSE_PREVIOUS 3 /* Restore to previous content */ |
117 | bool UserInputFlag; /* User confirmation required before disposal */ |
118 | int DelayTime; /* pre-display delay in 0.01sec units */ |
119 | int TransparentColor; /* Palette index for transparency, -1 if none */ |
120 | #define NO_TRANSPARENT_COLOR -1 |
121 | } GraphicsControlBlock; |
122 | |
123 | /****************************************************************************** |
124 | GIF encoding routines |
125 | ******************************************************************************/ |
126 | |
127 | /* Main entry points */ |
128 | GifFileType *EGifOpenFileName(const char *GifFileName, |
129 | const bool GifTestExistence, int *Error); |
130 | GifFileType *EGifOpenFileHandle(const int GifFileHandle, int *Error); |
131 | GifFileType *EGifOpen(void *userPtr, OutputFunc writeFunc, int *Error); |
132 | int EGifSpew(GifFileType * GifFile); |
133 | const char *EGifGetGifVersion(GifFileType *GifFile); /* new in 5.x */ |
134 | int EGifCloseFile(GifFileType *GifFile, int *ErrorCode); |
135 | |
136 | #define E_GIF_SUCCEEDED 0 |
137 | #define E_GIF_ERR_OPEN_FAILED 1 /* And EGif possible errors. */ |
138 | #define E_GIF_ERR_WRITE_FAILED 2 |
139 | #define E_GIF_ERR_HAS_SCRN_DSCR 3 |
140 | #define E_GIF_ERR_HAS_IMAG_DSCR 4 |
141 | #define E_GIF_ERR_NO_COLOR_MAP 5 |
142 | #define E_GIF_ERR_DATA_TOO_BIG 6 |
143 | #define E_GIF_ERR_NOT_ENOUGH_MEM 7 |
144 | #define E_GIF_ERR_DISK_IS_FULL 8 |
145 | #define E_GIF_ERR_CLOSE_FAILED 9 |
146 | #define E_GIF_ERR_NOT_WRITEABLE 10 |
147 | |
148 | /* These are legacy. You probably do not want to call them directly */ |
149 | int EGifPutScreenDesc(GifFileType *GifFile, |
150 | const int GifWidth, const int GifHeight, |
151 | const int GifColorRes, |
152 | const int GifBackGround, |
153 | const ColorMapObject *GifColorMap); |
154 | int EGifPutImageDesc(GifFileType *GifFile, |
155 | const int GifLeft, const int GifTop, |
156 | const int GifWidth, const int GifHeight, |
157 | const bool GifInterlace, |
158 | const ColorMapObject *GifColorMap); |
159 | void EGifSetGifVersion(GifFileType *GifFile, const bool gif89); |
160 | int EGifPutLine(GifFileType *GifFile, GifPixelType *GifLine, |
161 | int GifLineLen); |
162 | int EGifPutPixel(GifFileType *GifFile, const GifPixelType GifPixel); |
163 | int (GifFileType *GifFile, const char *); |
164 | int EGifPutExtensionLeader(GifFileType *GifFile, const int GifExtCode); |
165 | int EGifPutExtensionBlock(GifFileType *GifFile, |
166 | const int GifExtLen, const void *GifExtension); |
167 | int EGifPutExtensionTrailer(GifFileType *GifFile); |
168 | int EGifPutExtension(GifFileType *GifFile, const int GifExtCode, |
169 | const int GifExtLen, |
170 | const void *GifExtension); |
171 | int EGifPutCode(GifFileType *GifFile, int GifCodeSize, |
172 | const GifByteType *GifCodeBlock); |
173 | int EGifPutCodeNext(GifFileType *GifFile, |
174 | const GifByteType *GifCodeBlock); |
175 | |
176 | /****************************************************************************** |
177 | GIF decoding routines |
178 | ******************************************************************************/ |
179 | |
180 | /* Main entry points */ |
181 | GifFileType *DGifOpenFileName(const char *GifFileName, int *Error); |
182 | GifFileType *DGifOpenFileHandle(int GifFileHandle, int *Error); |
183 | int DGifSlurp(GifFileType * GifFile); |
184 | GifFileType *DGifOpen(void *userPtr, InputFunc readFunc, int *Error); /* new one (TVT) */ |
185 | int DGifCloseFile(GifFileType * GifFile, int *ErrorCode); |
186 | |
187 | #define D_GIF_SUCCEEDED 0 |
188 | #define D_GIF_ERR_OPEN_FAILED 101 /* And DGif possible errors. */ |
189 | #define D_GIF_ERR_READ_FAILED 102 |
190 | #define D_GIF_ERR_NOT_GIF_FILE 103 |
191 | #define D_GIF_ERR_NO_SCRN_DSCR 104 |
192 | #define D_GIF_ERR_NO_IMAG_DSCR 105 |
193 | #define D_GIF_ERR_NO_COLOR_MAP 106 |
194 | #define D_GIF_ERR_WRONG_RECORD 107 |
195 | #define D_GIF_ERR_DATA_TOO_BIG 108 |
196 | #define D_GIF_ERR_NOT_ENOUGH_MEM 109 |
197 | #define D_GIF_ERR_CLOSE_FAILED 110 |
198 | #define D_GIF_ERR_NOT_READABLE 111 |
199 | #define D_GIF_ERR_IMAGE_DEFECT 112 |
200 | #define D_GIF_ERR_EOF_TOO_SOON 113 |
201 | |
202 | /* These are legacy. You probably do not want to call them directly */ |
203 | int DGifGetScreenDesc(GifFileType *GifFile); |
204 | int DGifGetRecordType(GifFileType *GifFile, GifRecordType *GifType); |
205 | int (GifFileType *GifFile); |
206 | int DGifGetImageDesc(GifFileType *GifFile); |
207 | int DGifGetLine(GifFileType *GifFile, GifPixelType *GifLine, int GifLineLen); |
208 | int DGifGetPixel(GifFileType *GifFile, GifPixelType GifPixel); |
209 | int DGifGetExtension(GifFileType *GifFile, int *GifExtCode, |
210 | GifByteType **GifExtension); |
211 | int DGifGetExtensionNext(GifFileType *GifFile, GifByteType **GifExtension); |
212 | int DGifGetCode(GifFileType *GifFile, int *GifCodeSize, |
213 | GifByteType **GifCodeBlock); |
214 | int DGifGetCodeNext(GifFileType *GifFile, GifByteType **GifCodeBlock); |
215 | int DGifGetLZCodes(GifFileType *GifFile, int *GifCode); |
216 | const char *DGifGetGifVersion(GifFileType *GifFile); |
217 | |
218 | |
219 | /****************************************************************************** |
220 | Error handling and reporting. |
221 | ******************************************************************************/ |
222 | extern const char *GifErrorString(int ErrorCode); /* new in 2012 - ESR */ |
223 | |
224 | /***************************************************************************** |
225 | Everything below this point is new after version 1.2, supporting `slurp |
226 | mode' for doing I/O in two big belts with all the image-bashing in core. |
227 | ******************************************************************************/ |
228 | |
229 | /****************************************************************************** |
230 | Color map handling from gif_alloc.c |
231 | ******************************************************************************/ |
232 | |
233 | extern ColorMapObject *GifMakeMapObject(int ColorCount, |
234 | const GifColorType *ColorMap); |
235 | extern void GifFreeMapObject(ColorMapObject *Object); |
236 | extern ColorMapObject *GifUnionColorMap(const ColorMapObject *ColorIn1, |
237 | const ColorMapObject *ColorIn2, |
238 | GifPixelType ColorTransIn2[]); |
239 | extern int GifBitSize(int n); |
240 | |
241 | /****************************************************************************** |
242 | Support for the in-core structures allocation (slurp mode). |
243 | ******************************************************************************/ |
244 | |
245 | extern void GifApplyTranslation(SavedImage *Image, GifPixelType Translation[]); |
246 | extern int GifAddExtensionBlock(int *ExtensionBlock_Count, |
247 | ExtensionBlock **ExtensionBlocks, |
248 | int Function, |
249 | unsigned int Len, unsigned char ExtData[]); |
250 | extern void GifFreeExtensions(int *ExtensionBlock_Count, |
251 | ExtensionBlock **ExtensionBlocks); |
252 | extern SavedImage *GifMakeSavedImage(GifFileType *GifFile, |
253 | const SavedImage *CopyFrom); |
254 | extern void GifFreeSavedImages(GifFileType *GifFile); |
255 | |
256 | /****************************************************************************** |
257 | 5.x functions for GIF89 graphics control blocks |
258 | ******************************************************************************/ |
259 | |
260 | int DGifExtensionToGCB(const size_t GifExtensionLength, |
261 | const GifByteType *GifExtension, |
262 | GraphicsControlBlock *GCB); |
263 | size_t EGifGCBToExtension(const GraphicsControlBlock *GCB, |
264 | GifByteType *GifExtension); |
265 | |
266 | int DGifSavedExtensionToGCB(GifFileType *GifFile, |
267 | int ImageIndex, |
268 | GraphicsControlBlock *GCB); |
269 | int EGifGCBToSavedExtension(const GraphicsControlBlock *GCB, |
270 | GifFileType *GifFile, |
271 | int ImageIndex); |
272 | |
273 | /****************************************************************************** |
274 | The library's internal utility font |
275 | ******************************************************************************/ |
276 | |
277 | #define GIF_FONT_WIDTH 8 |
278 | #define GIF_FONT_HEIGHT 8 |
279 | extern const unsigned char GifAsciiTable8x8[][GIF_FONT_WIDTH]; |
280 | |
281 | extern void GifDrawText8x8(SavedImage *Image, |
282 | const int x, const int y, |
283 | const char *legend, const int color); |
284 | |
285 | extern void GifDrawBox(SavedImage *Image, |
286 | const int x, const int y, |
287 | const int w, const int d, const int color); |
288 | |
289 | extern void GifDrawRectangle(SavedImage *Image, |
290 | const int x, const int y, |
291 | const int w, const int d, const int color); |
292 | |
293 | extern void GifDrawBoxedText8x8(SavedImage *Image, |
294 | const int x, const int y, |
295 | const char *legend, |
296 | const int border, const int bg, const int fg); |
297 | |
298 | #ifdef __cplusplus |
299 | } |
300 | #endif /* __cplusplus */ |
301 | #endif /* _GIF_LIB_H */ |
302 | |
303 | /* end */ |
304 | |