GifEncode.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361
  1. /*
  2. * The Python Imaging Library.
  3. * $Id$
  4. *
  5. * encoder for uncompressed GIF data
  6. *
  7. * history:
  8. * 97-01-05 fl created (writes uncompressed data)
  9. * 97-08-27 fl fixed off-by-one error in buffer size test
  10. * 98-07-09 fl added interlace write support
  11. * 99-02-07 fl rewritten, now uses a run-length encoding strategy
  12. * 99-02-08 fl improved run-length encoding for long runs
  13. * 2020-12-12 rdg Reworked for LZW compression.
  14. *
  15. * Copyright (c) Secret Labs AB 1997-99.
  16. * Copyright (c) Fredrik Lundh 1997.
  17. *
  18. * See the README file for information on usage and redistribution.
  19. */
  20. #include "Imaging.h"
  21. #include "Gif.h"
  22. enum { INIT, ENCODE, FINISH };
  23. /* GIF LZW encoder by Raymond Gardner. */
  24. /* Released here under PIL license. */
  25. /* This LZW encoder conforms to the GIF LZW format specified in the original
  26. * Compuserve GIF 87a and GIF 89a specifications (see e.g.
  27. * https://www.w3.org/Graphics/GIF/spec-gif87.txt Appendix C and
  28. * https://www.w3.org/Graphics/GIF/spec-gif89a.txt Appendix F).
  29. */
  30. /* Return values */
  31. #define GLZW_OK 0
  32. #define GLZW_NO_INPUT_AVAIL 1
  33. #define GLZW_NO_OUTPUT_AVAIL 2
  34. #define GLZW_INTERNAL_ERROR 3
  35. #define CODE_LIMIT 4096
  36. /* Values of entry_state */
  37. enum { LZW_INITIAL, LZW_TRY_IN1, LZW_TRY_IN2, LZW_TRY_OUT1, LZW_TRY_OUT2,
  38. LZW_FINISHED };
  39. /* Values of control_state */
  40. enum { PUT_HEAD, PUT_INIT_CLEAR, PUT_CLEAR, PUT_LAST_HEAD, PUT_END };
  41. static void glzwe_reset(GIFENCODERSTATE *st) {
  42. st->next_code = st->end_code + 1;
  43. st->max_code = 2 * st->clear_code - 1;
  44. st->code_width = st->bits + 1;
  45. memset(st->codes, 0, sizeof(st->codes));
  46. }
  47. static void glzwe_init(GIFENCODERSTATE *st) {
  48. st->clear_code = 1 << st->bits;
  49. st->end_code = st->clear_code + 1;
  50. glzwe_reset(st);
  51. st->entry_state = LZW_INITIAL;
  52. st->buf_bits_left = 8;
  53. st->code_buffer = 0;
  54. }
  55. static int glzwe(GIFENCODERSTATE *st, const UINT8 *in_ptr, UINT8 *out_ptr,
  56. UINT32 *in_avail, UINT32 *out_avail,
  57. UINT32 end_of_data) {
  58. switch (st->entry_state) {
  59. case LZW_TRY_IN1:
  60. get_first_byte:
  61. if (!*in_avail) {
  62. if (end_of_data) {
  63. goto end_of_data;
  64. }
  65. st->entry_state = LZW_TRY_IN1;
  66. return GLZW_NO_INPUT_AVAIL;
  67. }
  68. st->head = *in_ptr++;
  69. (*in_avail)--;
  70. case LZW_TRY_IN2:
  71. encode_loop:
  72. if (!*in_avail) {
  73. if (end_of_data) {
  74. st->code = st->head;
  75. st->put_state = PUT_LAST_HEAD;
  76. goto put_code;
  77. }
  78. st->entry_state = LZW_TRY_IN2;
  79. return GLZW_NO_INPUT_AVAIL;
  80. }
  81. st->tail = *in_ptr++;
  82. (*in_avail)--;
  83. /* Knuth TAOCP vol 3 sec. 6.4 algorithm D. */
  84. /* Hash found experimentally to be pretty good. */
  85. /* This works ONLY with TABLE_SIZE a power of 2. */
  86. st->probe = ((st->head ^ (st->tail << 6)) * 31) & (TABLE_SIZE - 1);
  87. while (st->codes[st->probe]) {
  88. if ((st->codes[st->probe] & 0xFFFFF) ==
  89. ((st->head << 8) | st->tail)) {
  90. st->head = st->codes[st->probe] >> 20;
  91. goto encode_loop;
  92. } else {
  93. /* Reprobe decrement must be nonzero and relatively prime to table
  94. * size. So, any odd positive number for power-of-2 size. */
  95. if ((st->probe -= ((st->tail << 2) | 1)) < 0) {
  96. st->probe += TABLE_SIZE;
  97. }
  98. }
  99. }
  100. /* Key not found, probe is at empty slot. */
  101. st->code = st->head;
  102. st->put_state = PUT_HEAD;
  103. goto put_code;
  104. insert_code_or_clear: /* jump here after put_code */
  105. if (st->next_code < CODE_LIMIT) {
  106. st->codes[st->probe] = (st->next_code << 20) |
  107. (st->head << 8) | st->tail;
  108. if (st->next_code > st->max_code) {
  109. st->max_code = st->max_code * 2 + 1;
  110. st->code_width++;
  111. }
  112. st->next_code++;
  113. } else {
  114. st->code = st->clear_code;
  115. st->put_state = PUT_CLEAR;
  116. goto put_code;
  117. reset_after_clear: /* jump here after put_code */
  118. glzwe_reset(st);
  119. }
  120. st->head = st->tail;
  121. goto encode_loop;
  122. case LZW_INITIAL:
  123. glzwe_reset(st);
  124. st->code = st->clear_code;
  125. st->put_state = PUT_INIT_CLEAR;
  126. put_code:
  127. st->code_bits_left = st->code_width;
  128. check_buf_bits:
  129. if (!st->buf_bits_left) { /* out buffer full */
  130. case LZW_TRY_OUT1:
  131. if (!*out_avail) {
  132. st->entry_state = LZW_TRY_OUT1;
  133. return GLZW_NO_OUTPUT_AVAIL;
  134. }
  135. *out_ptr++ = st->code_buffer;
  136. (*out_avail)--;
  137. st->code_buffer = 0;
  138. st->buf_bits_left = 8;
  139. }
  140. /* code bits to pack */
  141. UINT32 n = st->buf_bits_left < st->code_bits_left
  142. ? st->buf_bits_left : st->code_bits_left;
  143. st->code_buffer |=
  144. (st->code & ((1 << n) - 1)) << (8 - st->buf_bits_left);
  145. st->code >>= n;
  146. st->buf_bits_left -= n;
  147. st->code_bits_left -= n;
  148. if (st->code_bits_left) {
  149. goto check_buf_bits;
  150. }
  151. switch (st->put_state) {
  152. case PUT_INIT_CLEAR:
  153. goto get_first_byte;
  154. case PUT_HEAD:
  155. goto insert_code_or_clear;
  156. case PUT_CLEAR:
  157. goto reset_after_clear;
  158. case PUT_LAST_HEAD:
  159. goto end_of_data;
  160. case PUT_END:
  161. goto flush_code_buffer;
  162. default:
  163. return GLZW_INTERNAL_ERROR;
  164. }
  165. end_of_data:
  166. st->code = st->end_code;
  167. st->put_state = PUT_END;
  168. goto put_code;
  169. flush_code_buffer: /* jump here after put_code */
  170. if (st->buf_bits_left < 8) {
  171. case LZW_TRY_OUT2:
  172. if (!*out_avail) {
  173. st->entry_state = LZW_TRY_OUT2;
  174. return GLZW_NO_OUTPUT_AVAIL;
  175. }
  176. *out_ptr++ = st->code_buffer;
  177. (*out_avail)--;
  178. }
  179. st->entry_state = LZW_FINISHED;
  180. return GLZW_OK;
  181. case LZW_FINISHED:
  182. return GLZW_OK;
  183. default:
  184. return GLZW_INTERNAL_ERROR;
  185. }
  186. }
  187. /* -END- GIF LZW encoder. */
  188. int
  189. ImagingGifEncode(Imaging im, ImagingCodecState state, UINT8* buf, int bytes) {
  190. UINT8* ptr;
  191. UINT8* sub_block_ptr;
  192. UINT8* sub_block_limit;
  193. UINT8* buf_limit;
  194. GIFENCODERSTATE *context = (GIFENCODERSTATE*) state->context;
  195. int r;
  196. UINT32 in_avail, in_used;
  197. UINT32 out_avail, out_used;
  198. if (state->state == INIT) {
  199. state->state = ENCODE;
  200. glzwe_init(context);
  201. if (context->interlace) {
  202. context->interlace = 1;
  203. context->step = 8;
  204. } else {
  205. context->step = 1;
  206. }
  207. /* Need at least 2 bytes for data sub-block; 5 for empty image */
  208. if (bytes < 5) {
  209. state->errcode = IMAGING_CODEC_CONFIG;
  210. return 0;
  211. }
  212. /* sanity check */
  213. if (state->xsize <= 0 || state->ysize <= 0) {
  214. /* Is this better than an error return? */
  215. /* This will handle any legal "LZW Minimum Code Size" */
  216. memset(buf, 0, 5);
  217. in_avail = 0;
  218. out_avail = 5;
  219. r = glzwe(context, (const UINT8 *)"", buf + 1, &in_avail, &out_avail, 1);
  220. if (r == GLZW_OK) {
  221. r = 5 - out_avail;
  222. if (r < 1 || r > 3) {
  223. state->errcode = IMAGING_CODEC_BROKEN;
  224. return 0;
  225. }
  226. buf[0] = r;
  227. state->errcode = IMAGING_CODEC_END;
  228. return r + 2;
  229. } else {
  230. /* Should not be possible unless something external to this
  231. * routine messes with our state data */
  232. state->errcode = IMAGING_CODEC_BROKEN;
  233. return 0;
  234. }
  235. }
  236. /* Init state->x to make if() below true the first time through. */
  237. state->x = state->xsize;
  238. }
  239. buf_limit = buf + bytes;
  240. sub_block_limit = sub_block_ptr = ptr = buf;
  241. /* On entry, buf is output buffer, bytes is space available in buf.
  242. * Loop here getting input until buf is full or image is all encoded. */
  243. for (;;) {
  244. /* Set up sub-block ptr and limit. sub_block_ptr stays at beginning
  245. * of sub-block until it is full. ptr will advance when any data is
  246. * placed in buf.
  247. */
  248. if (ptr >= sub_block_limit) {
  249. if (buf_limit - ptr < 2) { /* Need at least 2 for data sub-block */
  250. return ptr - buf;
  251. }
  252. sub_block_ptr = ptr;
  253. sub_block_limit = sub_block_ptr +
  254. (256 < buf_limit - sub_block_ptr ?
  255. 256 : buf_limit - sub_block_ptr);
  256. *ptr++ = 0;
  257. }
  258. /* Get next row of pixels. */
  259. /* This if() originally tested state->x==0 for the first time through.
  260. * This no longer works, as the loop will not advance state->x if
  261. * glzwe() does not consume any input; this would advance the row
  262. * spuriously. Now pre-init state->x above for first time, and avoid
  263. * entering if() when state->state is FINISH, or it will loop
  264. * infinitely.
  265. */
  266. if (state->x >= state->xsize && state->state == ENCODE) {
  267. if (!context->interlace && state->y >= state->ysize) {
  268. state->state = FINISH;
  269. continue;
  270. }
  271. /* get another line of data */
  272. state->shuffle(
  273. state->buffer,
  274. (UINT8*) im->image[state->y + state->yoff] +
  275. state->xoff * im->pixelsize, state->xsize
  276. );
  277. state->x = 0;
  278. /* step forward, according to the interlace settings */
  279. state->y += context->step;
  280. while (context->interlace && state->y >= state->ysize) {
  281. switch (context->interlace) {
  282. case 1:
  283. state->y = 4;
  284. context->interlace = 2;
  285. break;
  286. case 2:
  287. context->step = 4;
  288. state->y = 2;
  289. context->interlace = 3;
  290. break;
  291. case 3:
  292. context->step = 2;
  293. state->y = 1;
  294. context->interlace = 0;
  295. break;
  296. default:
  297. /* just make sure we don't loop forever */
  298. context->interlace = 0;
  299. }
  300. }
  301. }
  302. in_avail = state->xsize - state->x; /* bytes left in line */
  303. out_avail = sub_block_limit - ptr; /* bytes left in sub-block */
  304. r = glzwe(context, &state->buffer[state->x], ptr, &in_avail,
  305. &out_avail, state->state == FINISH);
  306. out_used = sub_block_limit - ptr - out_avail;
  307. *sub_block_ptr += out_used;
  308. ptr += out_used;
  309. in_used = state->xsize - state->x - in_avail;
  310. state->x += in_used;
  311. if (r == GLZW_OK) {
  312. /* Should not be possible when end-of-data flag is false. */
  313. state->errcode = IMAGING_CODEC_END;
  314. return ptr - buf;
  315. } else if (r == GLZW_NO_INPUT_AVAIL) {
  316. /* Used all the input line; get another line */
  317. continue;
  318. } else if (r == GLZW_NO_OUTPUT_AVAIL) {
  319. /* subblock is full */
  320. continue;
  321. } else {
  322. /* Should not be possible unless something external to this
  323. * routine messes with our state data */
  324. state->errcode = IMAGING_CODEC_BROKEN;
  325. return 0;
  326. }
  327. }
  328. }