GifDecode.c 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285
  1. /*
  2. * The Python Imaging Library.
  3. * $Id$
  4. *
  5. * a fast, suspendable GIF decoder
  6. *
  7. * history:
  8. * 95-09-03 fl Created
  9. * 95-09-05 fl Fixed sign problem on 16-bit platforms
  10. * 95-09-13 fl Added some storage shortcuts
  11. * 96-03-28 fl Revised API, integrated with PIL
  12. * 96-12-10 fl Added interlace support
  13. * 96-12-16 fl Fixed premature termination bug introduced by last fix
  14. * 97-01-05 fl Don't mess up on bogus configuration
  15. * 97-01-17 fl Don't mess up on very small, interlaced files
  16. * 99-02-07 fl Minor speedups
  17. *
  18. * Copyright (c) Secret Labs AB 1997-99.
  19. * Copyright (c) Fredrik Lundh 1995-97.
  20. *
  21. * See the README file for information on usage and redistribution.
  22. */
  23. #include "Imaging.h"
  24. #include <stdio.h>
  25. #include <memory.h> /* memcpy() */
  26. #include "Gif.h"
  27. #define NEWLINE(state, context) \
  28. { \
  29. state->x = 0; \
  30. state->y += context->step; \
  31. while (state->y >= state->ysize) switch (context->interlace) { \
  32. case 1: \
  33. context->repeat = state->y = 4; \
  34. context->interlace = 2; \
  35. break; \
  36. case 2: \
  37. context->step = 4; \
  38. context->repeat = state->y = 2; \
  39. context->interlace = 3; \
  40. break; \
  41. case 3: \
  42. context->step = 2; \
  43. context->repeat = state->y = 1; \
  44. context->interlace = 0; \
  45. break; \
  46. default: \
  47. return -1; \
  48. } \
  49. if (state->y < state->ysize) { \
  50. out = im->image8[state->y + state->yoff] + state->xoff; \
  51. } \
  52. }
  53. int
  54. ImagingGifDecode(Imaging im, ImagingCodecState state, UINT8 *buffer, Py_ssize_t bytes) {
  55. UINT8 *p;
  56. UINT8 *out;
  57. int c, i;
  58. int thiscode;
  59. GIFDECODERSTATE *context = (GIFDECODERSTATE *)state->context;
  60. UINT8 *ptr = buffer;
  61. if (!state->state) {
  62. /* Initialise state */
  63. if (context->bits < 0 || context->bits > 12) {
  64. state->errcode = IMAGING_CODEC_CONFIG;
  65. return -1;
  66. }
  67. /* Clear code */
  68. context->clear = 1 << context->bits;
  69. /* End code */
  70. context->end = context->clear + 1;
  71. /* Interlace */
  72. if (context->interlace) {
  73. context->interlace = 1;
  74. context->step = context->repeat = 8;
  75. } else {
  76. context->step = 1;
  77. }
  78. state->state = 1;
  79. }
  80. out = im->image8[state->y + state->yoff] + state->xoff + state->x;
  81. for (;;) {
  82. if (state->state == 1) {
  83. /* First free entry in table */
  84. context->next = context->clear + 2;
  85. /* Initial code size */
  86. context->codesize = context->bits + 1;
  87. context->codemask = (1 << context->codesize) - 1;
  88. /* Buffer pointer. We fill the buffer from right, which
  89. allows us to return all of it in one operation. */
  90. context->bufferindex = GIFBUFFER;
  91. state->state = 2;
  92. }
  93. if (context->bufferindex < GIFBUFFER) {
  94. /* Return whole buffer in one chunk */
  95. i = GIFBUFFER - context->bufferindex;
  96. p = &context->buffer[context->bufferindex];
  97. context->bufferindex = GIFBUFFER;
  98. } else {
  99. /* Get current symbol */
  100. while (context->bitcount < context->codesize) {
  101. if (context->blocksize > 0) {
  102. /* Read next byte */
  103. c = *ptr++;
  104. bytes--;
  105. context->blocksize--;
  106. /* New bits are shifted in from the left. */
  107. context->bitbuffer |= (INT32)c << context->bitcount;
  108. context->bitcount += 8;
  109. } else {
  110. /* New GIF block */
  111. /* We don't start decoding unless we have a full block */
  112. if (bytes < 1) {
  113. return ptr - buffer;
  114. }
  115. c = *ptr;
  116. if (bytes < c + 1) {
  117. return ptr - buffer;
  118. }
  119. context->blocksize = c;
  120. ptr++;
  121. bytes--;
  122. }
  123. }
  124. /* Extract current symbol from bit buffer. */
  125. c = (int)context->bitbuffer & context->codemask;
  126. /* Adjust buffer */
  127. context->bitbuffer >>= context->codesize;
  128. context->bitcount -= context->codesize;
  129. /* If c is less than "clear", it's a data byte. Otherwise,
  130. it's either clear/end or a code symbol which should be
  131. expanded. */
  132. if (c == context->clear) {
  133. if (state->state != 2) {
  134. state->state = 1;
  135. }
  136. continue;
  137. }
  138. if (c == context->end) {
  139. break;
  140. }
  141. i = 1;
  142. p = &context->lastdata;
  143. if (state->state == 2) {
  144. /* First valid symbol after clear; use as is */
  145. if (c > context->clear) {
  146. state->errcode = IMAGING_CODEC_BROKEN;
  147. return -1;
  148. }
  149. context->lastdata = context->lastcode = c;
  150. state->state = 3;
  151. } else {
  152. thiscode = c;
  153. if (c > context->next) {
  154. state->errcode = IMAGING_CODEC_BROKEN;
  155. return -1;
  156. }
  157. if (c == context->next) {
  158. /* c == next is allowed. not sure why. */
  159. if (context->bufferindex <= 0) {
  160. state->errcode = IMAGING_CODEC_BROKEN;
  161. return -1;
  162. }
  163. context->buffer[--context->bufferindex] = context->lastdata;
  164. c = context->lastcode;
  165. }
  166. while (c >= context->clear) {
  167. /* Copy data string to buffer (beginning from right) */
  168. if (context->bufferindex <= 0 || c >= GIFTABLE) {
  169. state->errcode = IMAGING_CODEC_BROKEN;
  170. return -1;
  171. }
  172. context->buffer[--context->bufferindex] = context->data[c];
  173. c = context->link[c];
  174. }
  175. context->lastdata = c;
  176. if (context->next < GIFTABLE) {
  177. /* We'll only add this symbol if we have room
  178. for it (take the advice, Netscape!) */
  179. context->data[context->next] = c;
  180. context->link[context->next] = context->lastcode;
  181. if (context->next == context->codemask &&
  182. context->codesize < GIFBITS) {
  183. /* Expand code size */
  184. context->codesize++;
  185. context->codemask = (1 << context->codesize) - 1;
  186. }
  187. context->next++;
  188. }
  189. context->lastcode = thiscode;
  190. }
  191. }
  192. /* Copy the bytes into the image */
  193. if (state->y >= state->ysize) {
  194. state->errcode = IMAGING_CODEC_OVERRUN;
  195. return -1;
  196. }
  197. /* To squeeze some extra pixels out of this loop, we test for
  198. some common cases and handle them separately. */
  199. /* This cannot be used if there is transparency */
  200. if (context->transparency == -1) {
  201. if (i == 1) {
  202. if (state->x < state->xsize - 1) {
  203. /* Single pixel, not at the end of the line. */
  204. *out++ = p[0];
  205. state->x++;
  206. continue;
  207. }
  208. } else if (state->x + i <= state->xsize) {
  209. /* This string fits into current line. */
  210. memcpy(out, p, i);
  211. out += i;
  212. state->x += i;
  213. if (state->x == state->xsize) {
  214. NEWLINE(state, context);
  215. }
  216. continue;
  217. }
  218. }
  219. /* No shortcut, copy pixel by pixel */
  220. for (c = 0; c < i; c++) {
  221. if (p[c] != context->transparency) {
  222. *out = p[c];
  223. }
  224. out++;
  225. if (++state->x >= state->xsize) {
  226. NEWLINE(state, context);
  227. }
  228. }
  229. }
  230. return ptr - buffer;
  231. }