xref: /aosp_15_r20/external/giflib/gif_lib.h (revision 324bb76b8d05e2a05aa88511fff61cf3f9ca5892)
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 <stdbool.h>
24 #include <stddef.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 COMMENT_EXT_FUNC_CODE 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, const int GifWidth,
150                       const int GifHeight, const int GifColorRes,
151                       const int GifBackGround,
152                       const ColorMapObject *GifColorMap);
153 int EGifPutImageDesc(GifFileType *GifFile, const int GifLeft, const int GifTop,
154                      const int GifWidth, const int GifHeight,
155                      const bool GifInterlace,
156                      const ColorMapObject *GifColorMap);
157 void EGifSetGifVersion(GifFileType *GifFile, const bool gif89);
158 int EGifPutLine(GifFileType *GifFile, GifPixelType *GifLine, int GifLineLen);
159 int EGifPutPixel(GifFileType *GifFile, const GifPixelType GifPixel);
160 int EGifPutComment(GifFileType *GifFile, const char *GifComment);
161 int EGifPutExtensionLeader(GifFileType *GifFile, const int GifExtCode);
162 int EGifPutExtensionBlock(GifFileType *GifFile, const int GifExtLen,
163                           const void *GifExtension);
164 int EGifPutExtensionTrailer(GifFileType *GifFile);
165 int EGifPutExtension(GifFileType *GifFile, const int GifExtCode,
166                      const int GifExtLen, const void *GifExtension);
167 int EGifPutCode(GifFileType *GifFile, int GifCodeSize,
168                 const GifByteType *GifCodeBlock);
169 int EGifPutCodeNext(GifFileType *GifFile, const GifByteType *GifCodeBlock);
170 
171 /******************************************************************************
172  GIF decoding routines
173 ******************************************************************************/
174 
175 /* Main entry points */
176 GifFileType *DGifOpenFileName(const char *GifFileName, int *Error);
177 GifFileType *DGifOpenFileHandle(int GifFileHandle, int *Error);
178 int DGifSlurp(GifFileType *GifFile);
179 GifFileType *DGifOpen(void *userPtr, InputFunc readFunc,
180                       int *Error); /* new one (TVT) */
181 int DGifCloseFile(GifFileType *GifFile, int *ErrorCode);
182 
183 #define D_GIF_SUCCEEDED 0
184 #define D_GIF_ERR_OPEN_FAILED 101 /* And DGif possible errors. */
185 #define D_GIF_ERR_READ_FAILED 102
186 #define D_GIF_ERR_NOT_GIF_FILE 103
187 #define D_GIF_ERR_NO_SCRN_DSCR 104
188 #define D_GIF_ERR_NO_IMAG_DSCR 105
189 #define D_GIF_ERR_NO_COLOR_MAP 106
190 #define D_GIF_ERR_WRONG_RECORD 107
191 #define D_GIF_ERR_DATA_TOO_BIG 108
192 #define D_GIF_ERR_NOT_ENOUGH_MEM 109
193 #define D_GIF_ERR_CLOSE_FAILED 110
194 #define D_GIF_ERR_NOT_READABLE 111
195 #define D_GIF_ERR_IMAGE_DEFECT 112
196 #define D_GIF_ERR_EOF_TOO_SOON 113
197 
198 /* These are legacy.  You probably do not want to call them directly */
199 int DGifGetScreenDesc(GifFileType *GifFile);
200 int DGifGetRecordType(GifFileType *GifFile, GifRecordType *GifType);
201 int DGifGetImageHeader(GifFileType *GifFile);
202 int DGifGetImageDesc(GifFileType *GifFile);
203 int DGifGetLine(GifFileType *GifFile, GifPixelType *GifLine, int GifLineLen);
204 int DGifGetPixel(GifFileType *GifFile, GifPixelType GifPixel);
205 int DGifGetExtension(GifFileType *GifFile, int *GifExtCode,
206                      GifByteType **GifExtension);
207 int DGifGetExtensionNext(GifFileType *GifFile, GifByteType **GifExtension);
208 int DGifGetCode(GifFileType *GifFile, int *GifCodeSize,
209                 GifByteType **GifCodeBlock);
210 int DGifGetCodeNext(GifFileType *GifFile, GifByteType **GifCodeBlock);
211 int DGifGetLZCodes(GifFileType *GifFile, int *GifCode);
212 const char *DGifGetGifVersion(GifFileType *GifFile);
213 
214 /******************************************************************************
215  Error handling and reporting.
216 ******************************************************************************/
217 extern const char *GifErrorString(int ErrorCode); /* new in 2012 - ESR */
218 
219 /*****************************************************************************
220  Everything below this point is new after version 1.2, supporting `slurp
221  mode' for doing I/O in two big belts with all the image-bashing in core.
222 ******************************************************************************/
223 
224 /******************************************************************************
225  Color map handling from gif_alloc.c
226 ******************************************************************************/
227 
228 extern ColorMapObject *GifMakeMapObject(int ColorCount,
229                                         const GifColorType *ColorMap);
230 extern void GifFreeMapObject(ColorMapObject *Object);
231 extern ColorMapObject *GifUnionColorMap(const ColorMapObject *ColorIn1,
232                                         const ColorMapObject *ColorIn2,
233                                         GifPixelType ColorTransIn2[]);
234 extern int GifBitSize(int n);
235 
236 /******************************************************************************
237  Support for the in-core structures allocation (slurp mode).
238 ******************************************************************************/
239 
240 extern void GifApplyTranslation(SavedImage *Image,
241                                 const GifPixelType Translation[]);
242 extern int GifAddExtensionBlock(int *ExtensionBlock_Count,
243                                 ExtensionBlock **ExtensionBlocks, int Function,
244                                 unsigned int Len, unsigned char ExtData[]);
245 extern void GifFreeExtensions(int *ExtensionBlock_Count,
246                               ExtensionBlock **ExtensionBlocks);
247 extern SavedImage *GifMakeSavedImage(GifFileType *GifFile,
248                                      const SavedImage *CopyFrom);
249 extern void GifFreeSavedImages(GifFileType *GifFile);
250 
251 /******************************************************************************
252  5.x functions for GIF89 graphics control blocks
253 ******************************************************************************/
254 
255 int DGifExtensionToGCB(const size_t GifExtensionLength,
256                        const GifByteType *GifExtension,
257                        GraphicsControlBlock *GCB);
258 size_t EGifGCBToExtension(const GraphicsControlBlock *GCB,
259                           GifByteType *GifExtension);
260 
261 int DGifSavedExtensionToGCB(GifFileType *GifFile, int ImageIndex,
262                             GraphicsControlBlock *GCB);
263 int EGifGCBToSavedExtension(const GraphicsControlBlock *GCB,
264                             GifFileType *GifFile, int ImageIndex);
265 
266 /******************************************************************************
267  The library's internal utility font
268 ******************************************************************************/
269 
270 #define GIF_FONT_WIDTH 8
271 #define GIF_FONT_HEIGHT 8
272 extern const unsigned char GifAsciiTable8x8[][GIF_FONT_WIDTH];
273 
274 extern void GifDrawText8x8(SavedImage *Image, const int x, const int y,
275                            const char *legend, const int color);
276 
277 extern void GifDrawBox(SavedImage *Image, const int x, const int y, const int w,
278                        const int d, const int color);
279 
280 extern void GifDrawRectangle(SavedImage *Image, const int x, const int y,
281                              const int w, const int d, const int color);
282 
283 extern void GifDrawBoxedText8x8(SavedImage *Image, const int x, const int y,
284                                 const char *legend, const int border,
285                                 const int bg, const int fg);
286 
287 #ifdef __cplusplus
288 }
289 #endif /* __cplusplus */
290 #endif /* _GIF_LIB_H */
291 
292 /* end */
293