gif_lib.h 13 KB

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