libnsgif.h 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. /*
  2. * Copyright 2004 Richard Wilson <[email protected]>
  3. * Copyright 2008 Sean Fox <[email protected]>
  4. *
  5. * This file is part of NetSurf's libnsgif, http://www.netsurf-browser.org/
  6. * Licenced under the MIT License,
  7. * http://www.opensource.org/licenses/mit-license.php
  8. */
  9. /** \file
  10. * Progressive animated GIF file decoding (interface).
  11. */
  12. #ifndef _LIBNSGIF_H_
  13. #define _LIBNSGIF_H_
  14. #include <stdbool.h>
  15. #include <inttypes.h>
  16. #if defined(__cplusplus)
  17. extern "C"
  18. {
  19. #endif
  20. /* Error return values
  21. */
  22. typedef enum {
  23. GIF_WORKING = 1,
  24. GIF_OK = 0,
  25. GIF_INSUFFICIENT_FRAME_DATA = -1,
  26. GIF_FRAME_DATA_ERROR = -2,
  27. GIF_INSUFFICIENT_DATA = -3,
  28. GIF_DATA_ERROR = -4,
  29. GIF_INSUFFICIENT_MEMORY = -5,
  30. GIF_FRAME_NO_DISPLAY = -6,
  31. GIF_END_OF_FRAME = -7
  32. } gif_result;
  33. /* Maximum LZW bits available
  34. */
  35. #define GIF_MAX_LZW 12
  36. /* The GIF frame data
  37. */
  38. typedef struct gif_frame {
  39. bool display; /**< whether the frame should be displayed/animated */
  40. unsigned int frame_delay; /**< delay (in 100th second intervals) before animating the frame */
  41. /** Internal members are listed below
  42. */
  43. unsigned int frame_pointer; /**< offset (in bytes) to the GIF frame data */
  44. bool virgin; /**< whether the frame has previously been used */
  45. bool opaque; /**< whether the frame is totally opaque */
  46. bool redraw_required; /**< whether a forcable screen redraw is required */
  47. unsigned char disposal_method; /**< how the previous frame should be disposed; affects plotting */
  48. bool transparency; /**< whether we acknowledge transparency */
  49. unsigned char transparency_index; /**< the index designating a transparent pixel */
  50. unsigned int redraw_x; /**< x co-ordinate of redraw rectangle */
  51. unsigned int redraw_y; /**< y co-ordinate of redraw rectangle */
  52. unsigned int redraw_width; /**< width of redraw rectangle */
  53. unsigned int redraw_height; /**< height of redraw rectangle */
  54. } gif_frame;
  55. /* API for Bitmap callbacks
  56. */
  57. typedef void* (*gif_bitmap_cb_create)(int width, int height);
  58. typedef void (*gif_bitmap_cb_destroy)(void *bitmap);
  59. typedef unsigned char* (*gif_bitmap_cb_get_buffer)(void *bitmap);
  60. typedef void (*gif_bitmap_cb_set_opaque)(void *bitmap, bool opaque);
  61. typedef bool (*gif_bitmap_cb_test_opaque)(void *bitmap);
  62. typedef void (*gif_bitmap_cb_modified)(void *bitmap);
  63. /* The Bitmap callbacks function table
  64. */
  65. typedef struct gif_bitmap_callback_vt {
  66. gif_bitmap_cb_create bitmap_create; /**< Create a bitmap. */
  67. gif_bitmap_cb_destroy bitmap_destroy; /**< Free a bitmap. */
  68. gif_bitmap_cb_get_buffer bitmap_get_buffer; /**< Return a pointer to the pixel data in a bitmap. */
  69. /** Members below are optional
  70. */
  71. gif_bitmap_cb_set_opaque bitmap_set_opaque; /**< Sets whether a bitmap should be plotted opaque. */
  72. gif_bitmap_cb_test_opaque bitmap_test_opaque; /**< Tests whether a bitmap has an opaque alpha channel. */
  73. gif_bitmap_cb_modified bitmap_modified; /**< The bitmap image has changed, so flush any persistent cache. */
  74. } gif_bitmap_callback_vt;
  75. /* The GIF animation data
  76. */
  77. typedef struct gif_animation {
  78. gif_bitmap_callback_vt bitmap_callbacks; /**< callbacks for bitmap functions */
  79. unsigned char *gif_data; /**< pointer to GIF data */
  80. unsigned int width; /**< width of GIF (may increase during decoding) */
  81. unsigned int height; /**< height of GIF (may increase during decoding) */
  82. unsigned int frame_count; /**< number of frames decoded */
  83. unsigned int frame_count_partial; /**< number of frames partially decoded */
  84. gif_frame *frames; /**< decoded frames */
  85. int decoded_frame; /**< current frame decoded to bitmap */
  86. void *frame_image; /**< currently decoded image; stored as bitmap from bitmap_create callback */
  87. int loop_count; /**< number of times to loop animation */
  88. gif_result current_error; /**< current error type, or 0 for none*/
  89. /** Internal members are listed below
  90. */
  91. unsigned int buffer_position; /**< current index into GIF data */
  92. unsigned int buffer_size; /**< total number of bytes of GIF data available */
  93. unsigned int frame_holders; /**< current number of frame holders */
  94. unsigned int background_index; /**< index in the colour table for the background colour */
  95. unsigned int aspect_ratio; /**< image aspect ratio (ignored) */
  96. unsigned int colour_table_size; /**< size of colour table (in entries) */
  97. bool global_colours; /**< whether the GIF has a global colour table */
  98. unsigned int *global_colour_table; /**< global colour table */
  99. unsigned int *local_colour_table; /**< local colour table */
  100. /* General LZW values. They are NO LONGER shared for all GIFs being decoded BECAUSE
  101. THAT IS A TERRIBLE IDEA TO SAVE 10Kb or so per GIF.
  102. */
  103. unsigned char buf[4];
  104. unsigned char *direct;
  105. int table[2][(1 << GIF_MAX_LZW)];
  106. unsigned char stack[(1 << GIF_MAX_LZW) * 2];
  107. unsigned char *stack_pointer;
  108. int code_size, set_code_size;
  109. int max_code, max_code_size;
  110. int clear_code, end_code;
  111. int curbit, lastbit, last_byte;
  112. int firstcode, oldcode;
  113. bool zero_data_block;
  114. bool get_done;
  115. /* Whether to clear the decoded image rather than plot
  116. */
  117. bool clear_image;
  118. } gif_animation;
  119. void gif_create(gif_animation *gif, gif_bitmap_callback_vt *bitmap_callbacks);
  120. gif_result gif_initialise(gif_animation *gif, size_t size, unsigned char *data);
  121. gif_result gif_decode_frame(gif_animation *gif, unsigned int frame);
  122. void gif_finalise(gif_animation *gif);
  123. #if defined(__cplusplus)
  124. };
  125. #endif
  126. #endif