GifDecode.c 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297
  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. state->x = 0;\
  29. state->y += context->step;\
  30. while (state->y >= state->ysize)\
  31. 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. int
  53. ImagingGifDecode(Imaging im, ImagingCodecState state, UINT8* buffer, Py_ssize_t bytes)
  54. {
  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. state->state = 1;
  78. }
  79. out = im->image8[state->y + state->yoff] + state->xoff + state->x;
  80. for (;;) {
  81. if (state->state == 1) {
  82. /* First free entry in table */
  83. context->next = context->clear + 2;
  84. /* Initial code size */
  85. context->codesize = context->bits + 1;
  86. context->codemask = (1 << context->codesize) - 1;
  87. /* Buffer pointer. We fill the buffer from right, which
  88. allows us to return all of it in one operation. */
  89. context->bufferindex = GIFBUFFER;
  90. state->state = 2;
  91. }
  92. if (context->bufferindex < GIFBUFFER) {
  93. /* Return whole buffer in one chunk */
  94. i = GIFBUFFER - context->bufferindex;
  95. p = &context->buffer[context->bufferindex];
  96. context->bufferindex = GIFBUFFER;
  97. } else {
  98. /* Get current symbol */
  99. while (context->bitcount < context->codesize) {
  100. if (context->blocksize > 0) {
  101. /* Read next byte */
  102. c = *ptr++; bytes--;
  103. context->blocksize--;
  104. /* New bits are shifted in from from the left. */
  105. context->bitbuffer |= (INT32) c << context->bitcount;
  106. context->bitcount += 8;
  107. } else {
  108. /* New GIF block */
  109. /* We don't start decoding unless we have a full block */
  110. if (bytes < 1)
  111. return ptr - buffer;
  112. c = *ptr;
  113. if (bytes < c+1)
  114. return ptr - buffer;
  115. context->blocksize = c;
  116. ptr++; bytes--;
  117. }
  118. }
  119. /* Extract current symbol from bit buffer. */
  120. c = (int) context->bitbuffer & context->codemask;
  121. /* Adjust buffer */
  122. context->bitbuffer >>= context->codesize;
  123. context->bitcount -= context->codesize;
  124. /* If c is less than "clear", it's a data byte. Otherwise,
  125. it's either clear/end or a code symbol which should be
  126. expanded. */
  127. if (c == context->clear) {
  128. if (state->state != 2)
  129. state->state = 1;
  130. continue;
  131. }
  132. if (c == context->end)
  133. break;
  134. i = 1;
  135. p = &context->lastdata;
  136. if (state->state == 2) {
  137. /* First valid symbol after clear; use as is */
  138. if (c > context->clear) {
  139. state->errcode = IMAGING_CODEC_BROKEN;
  140. return -1;
  141. }
  142. context->lastdata = context->lastcode = c;
  143. state->state = 3;
  144. } else {
  145. thiscode = c;
  146. if (c > context->next) {
  147. state->errcode = IMAGING_CODEC_BROKEN;
  148. return -1;
  149. }
  150. if (c == context->next) {
  151. /* c == next is allowed. not sure why. */
  152. if (context->bufferindex <= 0) {
  153. state->errcode = IMAGING_CODEC_BROKEN;
  154. return -1;
  155. }
  156. context->buffer[--context->bufferindex] =
  157. context->lastdata;
  158. c = context->lastcode;
  159. }
  160. while (c >= context->clear) {
  161. /* Copy data string to buffer (beginning from right) */
  162. if (context->bufferindex <= 0 || c >= GIFTABLE) {
  163. state->errcode = IMAGING_CODEC_BROKEN;
  164. return -1;
  165. }
  166. context->buffer[--context->bufferindex] =
  167. context->data[c];
  168. c = context->link[c];
  169. }
  170. context->lastdata = c;
  171. if (context->next < GIFTABLE) {
  172. /* We'll only add this symbol if we have room
  173. for it (take advise, Netscape!) */
  174. context->data[context->next] = c;
  175. context->link[context->next] = context->lastcode;
  176. if (context->next == context->codemask &&
  177. context->codesize < GIFBITS) {
  178. /* Expand code size */
  179. context->codesize++;
  180. context->codemask = (1 << context->codesize) - 1;
  181. }
  182. context->next++;
  183. }
  184. context->lastcode = thiscode;
  185. }
  186. }
  187. /* Copy the bytes into the image */
  188. if (state->y >= state->ysize) {
  189. state->errcode = IMAGING_CODEC_OVERRUN;
  190. return -1;
  191. }
  192. /* To squeeze some extra pixels out of this loop, we test for
  193. some common cases and handle them separately. */
  194. /* FIXME: should we handle the transparency index in here??? */
  195. if (i == 1) {
  196. if (state->x < state->xsize-1) {
  197. /* Single pixel, not at the end of the line. */
  198. *out++ = p[0];
  199. state->x++;
  200. continue;
  201. }
  202. } else if (state->x + i <= state->xsize) {
  203. /* This string fits into current line. */
  204. memcpy(out, p, i);
  205. out += i;
  206. state->x += i;
  207. if (state->x == state->xsize) {
  208. NEWLINE(state, context);
  209. }
  210. continue;
  211. }
  212. /* No shortcut, copy pixel by pixel */
  213. for (c = 0; c < i; c++) {
  214. *out++ = p[c];
  215. if (++state->x >= state->xsize) {
  216. NEWLINE(state, context);
  217. }
  218. }
  219. }
  220. return ptr - buffer;
  221. }