jpayne@69: /****************************************************************************** jpayne@69: jpayne@69: gif_lib.h - service library for decoding and encoding GIF images jpayne@69: jpayne@69: SPDX-License-Identifier: MIT jpayne@69: jpayne@69: *****************************************************************************/ jpayne@69: jpayne@69: #ifndef _GIF_LIB_H_ jpayne@69: #define _GIF_LIB_H_ 1 jpayne@69: jpayne@69: #ifdef __cplusplus jpayne@69: extern "C" { jpayne@69: #endif /* __cplusplus */ jpayne@69: jpayne@69: #define GIFLIB_MAJOR 5 jpayne@69: #define GIFLIB_MINOR 2 jpayne@69: #define GIFLIB_RELEASE 2 jpayne@69: jpayne@69: #define GIF_ERROR 0 jpayne@69: #define GIF_OK 1 jpayne@69: jpayne@69: #include jpayne@69: #include jpayne@69: jpayne@69: #define GIF_STAMP "GIFVER" /* First chars in file - GIF stamp. */ jpayne@69: #define GIF_STAMP_LEN sizeof(GIF_STAMP) - 1 jpayne@69: #define GIF_VERSION_POS 3 /* Version first character in stamp. */ jpayne@69: #define GIF87_STAMP "GIF87a" /* First chars in file - GIF stamp. */ jpayne@69: #define GIF89_STAMP "GIF89a" /* First chars in file - GIF stamp. */ jpayne@69: jpayne@69: typedef unsigned char GifPixelType; jpayne@69: typedef unsigned char *GifRowType; jpayne@69: typedef unsigned char GifByteType; jpayne@69: typedef unsigned int GifPrefixType; jpayne@69: typedef int GifWord; jpayne@69: jpayne@69: typedef struct GifColorType { jpayne@69: GifByteType Red, Green, Blue; jpayne@69: } GifColorType; jpayne@69: jpayne@69: typedef struct ColorMapObject { jpayne@69: int ColorCount; jpayne@69: int BitsPerPixel; jpayne@69: bool SortFlag; jpayne@69: GifColorType *Colors; /* on malloc(3) heap */ jpayne@69: } ColorMapObject; jpayne@69: jpayne@69: typedef struct GifImageDesc { jpayne@69: GifWord Left, Top, Width, Height; /* Current image dimensions. */ jpayne@69: bool Interlace; /* Sequential/Interlaced lines. */ jpayne@69: ColorMapObject *ColorMap; /* The local color map */ jpayne@69: } GifImageDesc; jpayne@69: jpayne@69: typedef struct ExtensionBlock { jpayne@69: int ByteCount; jpayne@69: GifByteType *Bytes; /* on malloc(3) heap */ jpayne@69: int Function; /* The block function code */ jpayne@69: #define CONTINUE_EXT_FUNC_CODE 0x00 /* continuation subblock */ jpayne@69: #define COMMENT_EXT_FUNC_CODE 0xfe /* comment */ jpayne@69: #define GRAPHICS_EXT_FUNC_CODE 0xf9 /* graphics control (GIF89) */ jpayne@69: #define PLAINTEXT_EXT_FUNC_CODE 0x01 /* plaintext */ jpayne@69: #define APPLICATION_EXT_FUNC_CODE 0xff /* application block (GIF89) */ jpayne@69: } ExtensionBlock; jpayne@69: jpayne@69: typedef struct SavedImage { jpayne@69: GifImageDesc ImageDesc; jpayne@69: GifByteType *RasterBits; /* on malloc(3) heap */ jpayne@69: int ExtensionBlockCount; /* Count of extensions before image */ jpayne@69: ExtensionBlock *ExtensionBlocks; /* Extensions before image */ jpayne@69: } SavedImage; jpayne@69: jpayne@69: typedef struct GifFileType { jpayne@69: GifWord SWidth, SHeight; /* Size of virtual canvas */ jpayne@69: GifWord SColorResolution; /* How many colors can we generate? */ jpayne@69: GifWord SBackGroundColor; /* Background color for virtual canvas */ jpayne@69: GifByteType AspectByte; /* Used to compute pixel aspect ratio */ jpayne@69: ColorMapObject *SColorMap; /* Global colormap, NULL if nonexistent. */ jpayne@69: int ImageCount; /* Number of current image (both APIs) */ jpayne@69: GifImageDesc Image; /* Current image (low-level API) */ jpayne@69: SavedImage *SavedImages; /* Image sequence (high-level API) */ jpayne@69: int ExtensionBlockCount; /* Count extensions past last image */ jpayne@69: ExtensionBlock *ExtensionBlocks; /* Extensions past last image */ jpayne@69: int Error; /* Last error condition reported */ jpayne@69: void *UserData; /* hook to attach user data (TVT) */ jpayne@69: void *Private; /* Don't mess with this! */ jpayne@69: } GifFileType; jpayne@69: jpayne@69: #define GIF_ASPECT_RATIO(n) ((n) + 15.0 / 64.0) jpayne@69: jpayne@69: typedef enum { jpayne@69: UNDEFINED_RECORD_TYPE, jpayne@69: SCREEN_DESC_RECORD_TYPE, jpayne@69: IMAGE_DESC_RECORD_TYPE, /* Begin with ',' */ jpayne@69: EXTENSION_RECORD_TYPE, /* Begin with '!' */ jpayne@69: TERMINATE_RECORD_TYPE /* Begin with ';' */ jpayne@69: } GifRecordType; jpayne@69: jpayne@69: /* func type to read gif data from arbitrary sources (TVT) */ jpayne@69: typedef int (*InputFunc)(GifFileType *, GifByteType *, int); jpayne@69: jpayne@69: /* func type to write gif data to arbitrary targets. jpayne@69: * Returns count of bytes written. (MRB) jpayne@69: */ jpayne@69: typedef int (*OutputFunc)(GifFileType *, const GifByteType *, int); jpayne@69: jpayne@69: /****************************************************************************** jpayne@69: GIF89 structures jpayne@69: ******************************************************************************/ jpayne@69: jpayne@69: typedef struct GraphicsControlBlock { jpayne@69: int DisposalMode; jpayne@69: #define DISPOSAL_UNSPECIFIED 0 /* No disposal specified. */ jpayne@69: #define DISPOSE_DO_NOT 1 /* Leave image in place */ jpayne@69: #define DISPOSE_BACKGROUND 2 /* Set area too background color */ jpayne@69: #define DISPOSE_PREVIOUS 3 /* Restore to previous content */ jpayne@69: bool UserInputFlag; /* User confirmation required before disposal */ jpayne@69: int DelayTime; /* pre-display delay in 0.01sec units */ jpayne@69: int TransparentColor; /* Palette index for transparency, -1 if none */ jpayne@69: #define NO_TRANSPARENT_COLOR -1 jpayne@69: } GraphicsControlBlock; jpayne@69: jpayne@69: /****************************************************************************** jpayne@69: GIF encoding routines jpayne@69: ******************************************************************************/ jpayne@69: jpayne@69: /* Main entry points */ jpayne@69: GifFileType *EGifOpenFileName(const char *GifFileName, jpayne@69: const bool GifTestExistence, int *Error); jpayne@69: GifFileType *EGifOpenFileHandle(const int GifFileHandle, int *Error); jpayne@69: GifFileType *EGifOpen(void *userPtr, OutputFunc writeFunc, int *Error); jpayne@69: int EGifSpew(GifFileType *GifFile); jpayne@69: const char *EGifGetGifVersion(GifFileType *GifFile); /* new in 5.x */ jpayne@69: int EGifCloseFile(GifFileType *GifFile, int *ErrorCode); jpayne@69: jpayne@69: #define E_GIF_SUCCEEDED 0 jpayne@69: #define E_GIF_ERR_OPEN_FAILED 1 /* And EGif possible errors. */ jpayne@69: #define E_GIF_ERR_WRITE_FAILED 2 jpayne@69: #define E_GIF_ERR_HAS_SCRN_DSCR 3 jpayne@69: #define E_GIF_ERR_HAS_IMAG_DSCR 4 jpayne@69: #define E_GIF_ERR_NO_COLOR_MAP 5 jpayne@69: #define E_GIF_ERR_DATA_TOO_BIG 6 jpayne@69: #define E_GIF_ERR_NOT_ENOUGH_MEM 7 jpayne@69: #define E_GIF_ERR_DISK_IS_FULL 8 jpayne@69: #define E_GIF_ERR_CLOSE_FAILED 9 jpayne@69: #define E_GIF_ERR_NOT_WRITEABLE 10 jpayne@69: jpayne@69: /* These are legacy. You probably do not want to call them directly */ jpayne@69: int EGifPutScreenDesc(GifFileType *GifFile, const int GifWidth, jpayne@69: const int GifHeight, const int GifColorRes, jpayne@69: const int GifBackGround, jpayne@69: const ColorMapObject *GifColorMap); jpayne@69: int EGifPutImageDesc(GifFileType *GifFile, const int GifLeft, const int GifTop, jpayne@69: const int GifWidth, const int GifHeight, jpayne@69: const bool GifInterlace, jpayne@69: const ColorMapObject *GifColorMap); jpayne@69: void EGifSetGifVersion(GifFileType *GifFile, const bool gif89); jpayne@69: int EGifPutLine(GifFileType *GifFile, GifPixelType *GifLine, int GifLineLen); jpayne@69: int EGifPutPixel(GifFileType *GifFile, const GifPixelType GifPixel); jpayne@69: int EGifPutComment(GifFileType *GifFile, const char *GifComment); jpayne@69: int EGifPutExtensionLeader(GifFileType *GifFile, const int GifExtCode); jpayne@69: int EGifPutExtensionBlock(GifFileType *GifFile, const int GifExtLen, jpayne@69: const void *GifExtension); jpayne@69: int EGifPutExtensionTrailer(GifFileType *GifFile); jpayne@69: int EGifPutExtension(GifFileType *GifFile, const int GifExtCode, jpayne@69: const int GifExtLen, const void *GifExtension); jpayne@69: int EGifPutCode(GifFileType *GifFile, int GifCodeSize, jpayne@69: const GifByteType *GifCodeBlock); jpayne@69: int EGifPutCodeNext(GifFileType *GifFile, const GifByteType *GifCodeBlock); jpayne@69: jpayne@69: /****************************************************************************** jpayne@69: GIF decoding routines jpayne@69: ******************************************************************************/ jpayne@69: jpayne@69: /* Main entry points */ jpayne@69: GifFileType *DGifOpenFileName(const char *GifFileName, int *Error); jpayne@69: GifFileType *DGifOpenFileHandle(int GifFileHandle, int *Error); jpayne@69: int DGifSlurp(GifFileType *GifFile); jpayne@69: GifFileType *DGifOpen(void *userPtr, InputFunc readFunc, jpayne@69: int *Error); /* new one (TVT) */ jpayne@69: int DGifCloseFile(GifFileType *GifFile, int *ErrorCode); jpayne@69: jpayne@69: #define D_GIF_SUCCEEDED 0 jpayne@69: #define D_GIF_ERR_OPEN_FAILED 101 /* And DGif possible errors. */ jpayne@69: #define D_GIF_ERR_READ_FAILED 102 jpayne@69: #define D_GIF_ERR_NOT_GIF_FILE 103 jpayne@69: #define D_GIF_ERR_NO_SCRN_DSCR 104 jpayne@69: #define D_GIF_ERR_NO_IMAG_DSCR 105 jpayne@69: #define D_GIF_ERR_NO_COLOR_MAP 106 jpayne@69: #define D_GIF_ERR_WRONG_RECORD 107 jpayne@69: #define D_GIF_ERR_DATA_TOO_BIG 108 jpayne@69: #define D_GIF_ERR_NOT_ENOUGH_MEM 109 jpayne@69: #define D_GIF_ERR_CLOSE_FAILED 110 jpayne@69: #define D_GIF_ERR_NOT_READABLE 111 jpayne@69: #define D_GIF_ERR_IMAGE_DEFECT 112 jpayne@69: #define D_GIF_ERR_EOF_TOO_SOON 113 jpayne@69: jpayne@69: /* These are legacy. You probably do not want to call them directly */ jpayne@69: int DGifGetScreenDesc(GifFileType *GifFile); jpayne@69: int DGifGetRecordType(GifFileType *GifFile, GifRecordType *GifType); jpayne@69: int DGifGetImageHeader(GifFileType *GifFile); jpayne@69: int DGifGetImageDesc(GifFileType *GifFile); jpayne@69: int DGifGetLine(GifFileType *GifFile, GifPixelType *GifLine, int GifLineLen); jpayne@69: int DGifGetPixel(GifFileType *GifFile, GifPixelType GifPixel); jpayne@69: int DGifGetExtension(GifFileType *GifFile, int *GifExtCode, jpayne@69: GifByteType **GifExtension); jpayne@69: int DGifGetExtensionNext(GifFileType *GifFile, GifByteType **GifExtension); jpayne@69: int DGifGetCode(GifFileType *GifFile, int *GifCodeSize, jpayne@69: GifByteType **GifCodeBlock); jpayne@69: int DGifGetCodeNext(GifFileType *GifFile, GifByteType **GifCodeBlock); jpayne@69: int DGifGetLZCodes(GifFileType *GifFile, int *GifCode); jpayne@69: const char *DGifGetGifVersion(GifFileType *GifFile); jpayne@69: jpayne@69: /****************************************************************************** jpayne@69: Error handling and reporting. jpayne@69: ******************************************************************************/ jpayne@69: extern const char *GifErrorString(int ErrorCode); /* new in 2012 - ESR */ jpayne@69: jpayne@69: /***************************************************************************** jpayne@69: Everything below this point is new after version 1.2, supporting `slurp jpayne@69: mode' for doing I/O in two big belts with all the image-bashing in core. jpayne@69: ******************************************************************************/ jpayne@69: jpayne@69: /****************************************************************************** jpayne@69: Color map handling from gif_alloc.c jpayne@69: ******************************************************************************/ jpayne@69: jpayne@69: extern ColorMapObject *GifMakeMapObject(int ColorCount, jpayne@69: const GifColorType *ColorMap); jpayne@69: extern void GifFreeMapObject(ColorMapObject *Object); jpayne@69: extern ColorMapObject *GifUnionColorMap(const ColorMapObject *ColorIn1, jpayne@69: const ColorMapObject *ColorIn2, jpayne@69: GifPixelType ColorTransIn2[]); jpayne@69: extern int GifBitSize(int n); jpayne@69: jpayne@69: /****************************************************************************** jpayne@69: Support for the in-core structures allocation (slurp mode). jpayne@69: ******************************************************************************/ jpayne@69: jpayne@69: extern void GifApplyTranslation(SavedImage *Image, jpayne@69: const GifPixelType Translation[]); jpayne@69: extern int GifAddExtensionBlock(int *ExtensionBlock_Count, jpayne@69: ExtensionBlock **ExtensionBlocks, int Function, jpayne@69: unsigned int Len, unsigned char ExtData[]); jpayne@69: extern void GifFreeExtensions(int *ExtensionBlock_Count, jpayne@69: ExtensionBlock **ExtensionBlocks); jpayne@69: extern SavedImage *GifMakeSavedImage(GifFileType *GifFile, jpayne@69: const SavedImage *CopyFrom); jpayne@69: extern void GifFreeSavedImages(GifFileType *GifFile); jpayne@69: jpayne@69: /****************************************************************************** jpayne@69: 5.x functions for GIF89 graphics control blocks jpayne@69: ******************************************************************************/ jpayne@69: jpayne@69: int DGifExtensionToGCB(const size_t GifExtensionLength, jpayne@69: const GifByteType *GifExtension, jpayne@69: GraphicsControlBlock *GCB); jpayne@69: size_t EGifGCBToExtension(const GraphicsControlBlock *GCB, jpayne@69: GifByteType *GifExtension); jpayne@69: jpayne@69: int DGifSavedExtensionToGCB(GifFileType *GifFile, int ImageIndex, jpayne@69: GraphicsControlBlock *GCB); jpayne@69: int EGifGCBToSavedExtension(const GraphicsControlBlock *GCB, jpayne@69: GifFileType *GifFile, int ImageIndex); jpayne@69: jpayne@69: /****************************************************************************** jpayne@69: The library's internal utility font jpayne@69: ******************************************************************************/ jpayne@69: jpayne@69: #define GIF_FONT_WIDTH 8 jpayne@69: #define GIF_FONT_HEIGHT 8 jpayne@69: extern const unsigned char GifAsciiTable8x8[][GIF_FONT_WIDTH]; jpayne@69: jpayne@69: extern void GifDrawText8x8(SavedImage *Image, const int x, const int y, jpayne@69: const char *legend, const int color); jpayne@69: jpayne@69: extern void GifDrawBox(SavedImage *Image, const int x, const int y, const int w, jpayne@69: const int d, const int color); jpayne@69: jpayne@69: extern void GifDrawRectangle(SavedImage *Image, const int x, const int y, jpayne@69: const int w, const int d, const int color); jpayne@69: jpayne@69: extern void GifDrawBoxedText8x8(SavedImage *Image, const int x, const int y, jpayne@69: const char *legend, const int border, jpayne@69: const int bg, const int fg); jpayne@69: jpayne@69: #ifdef __cplusplus jpayne@69: } jpayne@69: #endif /* __cplusplus */ jpayne@69: #endif /* _GIF_LIB_H */ jpayne@69: jpayne@69: /* end */