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