cgif.h 4.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. #ifndef CGIF_H
  2. #define CGIF_H
  3. #include <stdint.h>
  4. #include <stdio.h>
  5. #ifdef __cplusplus
  6. extern "C" {
  7. #endif
  8. // flags to set the GIF/frame-attributes
  9. #define CGIF_ATTR_IS_ANIMATED (1uL << 1) // make an animated GIF (default is non-animated GIF)
  10. #define CGIF_ATTR_NO_GLOBAL_TABLE (1uL << 2) // disable global color table (global color table is default)
  11. #define CGIF_ATTR_HAS_TRANSPARENCY (1uL << 3) // first entry in color table contains transparency (alpha channel)
  12. #define CGIF_ATTR_NO_LOOP (1uL << 4) // don't loop a GIF animation: only play it one time.
  13. #define CGIF_GEN_KEEP_IDENT_FRAMES (1uL << 0) // keep frames that are identical to previous frame (default is to drop them)
  14. #define CGIF_FRAME_ATTR_USE_LOCAL_TABLE (1uL << 0) // use a local color table for a frame (local color table is not used by default)
  15. #define CGIF_FRAME_ATTR_HAS_ALPHA (1uL << 1) // alpha channel index provided by user (transIndex field)
  16. #define CGIF_FRAME_ATTR_HAS_SET_TRANS (1uL << 2) // transparency setting provided by user (transIndex field)
  17. #define CGIF_FRAME_ATTR_INTERLACED (1uL << 3) // encode frame interlaced (default is not interlaced)
  18. // flags to decrease GIF-size
  19. #define CGIF_FRAME_GEN_USE_TRANSPARENCY (1uL << 0) // use transparency optimization (setting pixels identical to previous frame transparent)
  20. #define CGIF_FRAME_GEN_USE_DIFF_WINDOW (1uL << 1) // do encoding just for the sub-window that has changed from previous frame
  21. #define CGIF_INFINITE_LOOP (0x0000uL) // for animated GIF: 0 specifies infinite loop
  22. typedef enum {
  23. CGIF_ERROR = -1, // something unspecified failed
  24. CGIF_OK = 0, // everything OK
  25. CGIF_EWRITE, // writing GIF data failed
  26. CGIF_EALLOC, // allocating memory failed
  27. CGIF_ECLOSE, // final call to fclose failed
  28. CGIF_EOPEN, // failed to open output file
  29. CGIF_EINDEX, // invalid index in image data provided by user
  30. // internal section (values subject to change)
  31. CGIF_PENDING,
  32. } cgif_result;
  33. typedef struct st_gif CGIF; // struct for the full GIF
  34. typedef struct st_gifconfig CGIF_Config; // global cofinguration parameters of the GIF
  35. typedef struct st_frameconfig CGIF_FrameConfig; // local configuration parameters for a frame
  36. typedef int cgif_write_fn(void* pContext, const uint8_t* pData, const size_t numBytes); // callback function for stream-based output
  37. // prototypes
  38. CGIF* cgif_newgif (CGIF_Config* pConfig); // creates a new GIF (returns pointer to new GIF or NULL on error)
  39. int cgif_addframe (CGIF* pGIF, CGIF_FrameConfig* pConfig); // adds the next frame to an existing GIF (returns 0 on success)
  40. int cgif_close (CGIF* pGIF); // close file and free allocated memory (returns 0 on success)
  41. // CGIF_Config type (parameters passed by user)
  42. // note: must stay AS IS for backward compatibility
  43. struct st_gifconfig {
  44. uint8_t* pGlobalPalette; // global color table of the GIF
  45. const char* path; // path of the GIF to be created, mutually exclusive with pWriteFn
  46. uint32_t attrFlags; // fixed attributes of the GIF (e.g. whether it is animated or not)
  47. uint32_t genFlags; // flags that determine how the GIF is generated (e.g. optimization)
  48. uint16_t width; // width of each frame in the GIF
  49. uint16_t height; // height of each frame in the GIF
  50. uint16_t numGlobalPaletteEntries; // size of the global color table
  51. uint16_t numLoops; // number of repetitons of an animated GIF (set to INFINITE_LOOP for infinite loop)
  52. cgif_write_fn *pWriteFn; // callback function for chunks of output data, mutually exclusive with path
  53. void* pContext; // opaque pointer passed as the first parameter to pWriteFn
  54. };
  55. // CGIF_FrameConfig type (parameters passed by user)
  56. // note: must stay AS IS for backward compatibility
  57. struct st_frameconfig {
  58. uint8_t* pLocalPalette; // local color table of a frame
  59. uint8_t* pImageData; // image data to be encoded
  60. uint32_t attrFlags; // fixed attributes of the GIF frame
  61. uint32_t genFlags; // flags that determine how the GIF frame is created (e.g. optimization)
  62. uint16_t delay; // delay before the next frame is shown (units of 0.01 s)
  63. uint16_t numLocalPaletteEntries; // size of the local color table
  64. uint8_t transIndex; // introduced with V0.2.0
  65. };
  66. #ifdef __cplusplus
  67. }
  68. #endif
  69. #endif // CGIF_H