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