ZipDecode.c 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298
  1. /*
  2. * The Python Imaging Library.
  3. * $Id$
  4. *
  5. * decoder for ZIP (deflated) image data.
  6. *
  7. * history:
  8. * 1996-12-14 fl Created (for PNG)
  9. * 1997-01-15 fl Prepared to read TIFF/ZIP
  10. * 2001-11-19 fl PNG incomplete read patch (from Bernhard Herzog)
  11. *
  12. * Copyright (c) Fredrik Lundh 1996.
  13. * Copyright (c) Secret Labs AB 1997-2001.
  14. *
  15. * See the README file for information on usage and redistribution.
  16. */
  17. #include "Imaging.h"
  18. #ifdef HAVE_LIBZ
  19. #include "Zip.h"
  20. static const int OFFSET[] = { 7, 3, 3, 1, 1, 0, 0 };
  21. static const int STARTING_COL[] = { 0, 4, 0, 2, 0, 1, 0 };
  22. static const int STARTING_ROW[] = { 0, 0, 4, 0, 2, 0, 1 };
  23. static const int COL_INCREMENT[] = { 8, 8, 4, 4, 2, 2, 1 };
  24. static const int ROW_INCREMENT[] = { 8, 8, 8, 4, 4, 2, 2 };
  25. /* Get the length in bytes of a scanline in the pass specified,
  26. * for interlaced images */
  27. static int get_row_len(ImagingCodecState state, int pass)
  28. {
  29. int row_len = (state->xsize + OFFSET[pass]) / COL_INCREMENT[pass];
  30. return ((row_len * state->bits) + 7) / 8;
  31. }
  32. /* -------------------------------------------------------------------- */
  33. /* Decoder */
  34. /* -------------------------------------------------------------------- */
  35. int
  36. ImagingZipDecode(Imaging im, ImagingCodecState state, UINT8* buf, Py_ssize_t bytes)
  37. {
  38. ZIPSTATE* context = (ZIPSTATE*) state->context;
  39. int err;
  40. int n;
  41. UINT8* ptr;
  42. int i, bpp;
  43. int row_len;
  44. if (!state->state) {
  45. /* Initialization */
  46. if (context->mode == ZIP_PNG || context->mode == ZIP_PNG_PALETTE)
  47. context->prefix = 1; /* PNG */
  48. /* overflow check for malloc */
  49. if (state->bytes > INT_MAX - 1) {
  50. state->errcode = IMAGING_CODEC_MEMORY;
  51. return -1;
  52. }
  53. /* Expand standard buffer to make room for the (optional) filter
  54. prefix, and allocate a buffer to hold the previous line */
  55. free(state->buffer);
  56. /* malloc check ok, overflow checked above */
  57. state->buffer = (UINT8*) malloc(state->bytes+1);
  58. context->previous = (UINT8*) malloc(state->bytes+1);
  59. if (!state->buffer || !context->previous) {
  60. state->errcode = IMAGING_CODEC_MEMORY;
  61. return -1;
  62. }
  63. context->last_output = 0;
  64. /* Initialize to black */
  65. memset(context->previous, 0, state->bytes+1);
  66. /* Setup decompression context */
  67. context->z_stream.zalloc = (alloc_func) NULL;
  68. context->z_stream.zfree = (free_func) NULL;
  69. context->z_stream.opaque = (voidpf) NULL;
  70. err = inflateInit(&context->z_stream);
  71. if (err < 0) {
  72. state->errcode = IMAGING_CODEC_CONFIG;
  73. free(context->previous);
  74. context->previous = NULL;
  75. return -1;
  76. }
  77. if (context->interlaced) {
  78. context->pass = 0;
  79. state->y = STARTING_ROW[context->pass];
  80. }
  81. /* Ready to decode */
  82. state->state = 1;
  83. }
  84. if (context->interlaced) {
  85. row_len = get_row_len(state, context->pass);
  86. } else {
  87. row_len = state->bytes;
  88. }
  89. /* Setup the source buffer */
  90. context->z_stream.next_in = buf;
  91. context->z_stream.avail_in = bytes;
  92. /* Decompress what we've got this far */
  93. while (context->z_stream.avail_in > 0) {
  94. context->z_stream.next_out = state->buffer + context->last_output;
  95. context->z_stream.avail_out =
  96. row_len + context->prefix - context->last_output;
  97. err = inflate(&context->z_stream, Z_NO_FLUSH);
  98. if (err < 0) {
  99. /* Something went wrong inside the compression library */
  100. if (err == Z_DATA_ERROR)
  101. state->errcode = IMAGING_CODEC_BROKEN;
  102. else if (err == Z_MEM_ERROR)
  103. state->errcode = IMAGING_CODEC_MEMORY;
  104. else
  105. state->errcode = IMAGING_CODEC_CONFIG;
  106. free(context->previous);
  107. context->previous = NULL;
  108. inflateEnd(&context->z_stream);
  109. return -1;
  110. }
  111. n = row_len + context->prefix - context->z_stream.avail_out;
  112. if (n < row_len + context->prefix) {
  113. context->last_output = n;
  114. break; /* need more input data */
  115. }
  116. /* Apply predictor */
  117. switch (context->mode) {
  118. case ZIP_PNG:
  119. switch (state->buffer[0]) {
  120. case 0:
  121. break;
  122. case 1:
  123. /* prior */
  124. bpp = (state->bits + 7) / 8;
  125. for (i = bpp+1; i <= row_len; i++)
  126. state->buffer[i] += state->buffer[i-bpp];
  127. break;
  128. case 2:
  129. /* up */
  130. for (i = 1; i <= row_len; i++)
  131. state->buffer[i] += context->previous[i];
  132. break;
  133. case 3:
  134. /* average */
  135. bpp = (state->bits + 7) / 8;
  136. for (i = 1; i <= bpp; i++)
  137. state->buffer[i] += context->previous[i]/2;
  138. for (; i <= row_len; i++)
  139. state->buffer[i] +=
  140. (state->buffer[i-bpp] + context->previous[i])/2;
  141. break;
  142. case 4:
  143. /* paeth filtering */
  144. bpp = (state->bits + 7) / 8;
  145. for (i = 1; i <= bpp; i++)
  146. state->buffer[i] += context->previous[i];
  147. for (; i <= row_len; i++) {
  148. int a, b, c;
  149. int pa, pb, pc;
  150. /* fetch pixels */
  151. a = state->buffer[i-bpp];
  152. b = context->previous[i];
  153. c = context->previous[i-bpp];
  154. /* distances to surrounding pixels */
  155. pa = abs(b - c);
  156. pb = abs(a - c);
  157. pc = abs(a + b - 2*c);
  158. /* pick predictor with the shortest distance */
  159. state->buffer[i] +=
  160. (pa <= pb && pa <= pc) ? a : (pb <= pc) ? b : c;
  161. }
  162. break;
  163. default:
  164. state->errcode = IMAGING_CODEC_UNKNOWN;
  165. free(context->previous);
  166. context->previous = NULL;
  167. inflateEnd(&context->z_stream);
  168. return -1;
  169. }
  170. break;
  171. case ZIP_TIFF_PREDICTOR:
  172. bpp = (state->bits + 7) / 8;
  173. for (i = bpp+1; i <= row_len; i++)
  174. state->buffer[i] += state->buffer[i-bpp];
  175. break;
  176. }
  177. /* Stuff data into the image */
  178. if (context->interlaced) {
  179. int col = STARTING_COL[context->pass];
  180. if (state->bits >= 8) {
  181. /* Stuff pixels in their correct location, one by one */
  182. for (i = 0; i < row_len; i += ((state->bits + 7) / 8)) {
  183. state->shuffle((UINT8*) im->image[state->y] +
  184. col * im->pixelsize,
  185. state->buffer + context->prefix + i, 1);
  186. col += COL_INCREMENT[context->pass];
  187. }
  188. } else {
  189. /* Handle case with more than a pixel in each byte */
  190. int row_bits = ((state->xsize + OFFSET[context->pass])
  191. / COL_INCREMENT[context->pass]) * state->bits;
  192. for (i = 0; i < row_bits; i += state->bits) {
  193. UINT8 byte = *(state->buffer + context->prefix + (i / 8));
  194. byte <<= (i % 8);
  195. state->shuffle((UINT8*) im->image[state->y] +
  196. col * im->pixelsize, &byte, 1);
  197. col += COL_INCREMENT[context->pass];
  198. }
  199. }
  200. /* Find next valid scanline */
  201. state->y += ROW_INCREMENT[context->pass];
  202. while (state->y >= state->ysize || row_len <= 0) {
  203. context->pass++;
  204. if (context->pass == 7) {
  205. /* Force exit below */
  206. state->y = state->ysize;
  207. break;
  208. }
  209. state->y = STARTING_ROW[context->pass];
  210. row_len = get_row_len(state, context->pass);
  211. /* Since we're moving to the "first" line, the previous line
  212. * should be black to make filters work correctly */
  213. memset(state->buffer, 0, state->bytes+1);
  214. }
  215. } else {
  216. state->shuffle((UINT8*) im->image[state->y + state->yoff] +
  217. state->xoff * im->pixelsize,
  218. state->buffer + context->prefix,
  219. state->xsize);
  220. state->y++;
  221. }
  222. /* all inflate output has been consumed */
  223. context->last_output = 0;
  224. if (state->y >= state->ysize || err == Z_STREAM_END) {
  225. /* The image and the data should end simultaneously */
  226. /* if (state->y < state->ysize || err != Z_STREAM_END)
  227. state->errcode = IMAGING_CODEC_BROKEN; */
  228. free(context->previous);
  229. context->previous = NULL;
  230. inflateEnd(&context->z_stream);
  231. return -1; /* end of file (errcode=0) */
  232. }
  233. /* Swap buffer pointers */
  234. ptr = state->buffer;
  235. state->buffer = context->previous;
  236. context->previous = ptr;
  237. }
  238. return bytes; /* consumed all of it */
  239. }
  240. int ImagingZipDecodeCleanup(ImagingCodecState state){
  241. /* called to free the decompression engine when the decode terminates
  242. due to a corrupt or truncated image
  243. */
  244. ZIPSTATE* context = (ZIPSTATE*) state->context;
  245. /* Clean up */
  246. if (context->previous) {
  247. inflateEnd(&context->z_stream);
  248. free(context->previous);
  249. context->previous = NULL;
  250. }
  251. return -1;
  252. }
  253. #endif