gif_lib.h 13 KB

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