ZipDecode.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299
  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 "ZipCodecs.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
  28. get_row_len(ImagingCodecState state, int pass) {
  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. ZIPSTATE *context = (ZIPSTATE *)state->context;
  38. int err;
  39. int n;
  40. UINT8 *ptr;
  41. int i, bpp;
  42. int row_len;
  43. if (!state->state) {
  44. /* Initialization */
  45. if (context->mode == ZIP_PNG || context->mode == ZIP_PNG_PALETTE) {
  46. context->prefix = 1; /* PNG */
  47. }
  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 = row_len + context->prefix - context->last_output;
  96. err = inflate(&context->z_stream, Z_NO_FLUSH);
  97. if (err < 0) {
  98. /* Something went wrong inside the compression library */
  99. if (err == Z_DATA_ERROR) {
  100. state->errcode = IMAGING_CODEC_BROKEN;
  101. } else if (err == Z_MEM_ERROR) {
  102. state->errcode = IMAGING_CODEC_MEMORY;
  103. } else {
  104. state->errcode = IMAGING_CODEC_CONFIG;
  105. }
  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. }
  128. break;
  129. case 2:
  130. /* up */
  131. for (i = 1; i <= row_len; i++) {
  132. state->buffer[i] += context->previous[i];
  133. }
  134. break;
  135. case 3:
  136. /* average */
  137. bpp = (state->bits + 7) / 8;
  138. for (i = 1; i <= bpp; i++) {
  139. state->buffer[i] += context->previous[i] / 2;
  140. }
  141. for (; i <= row_len; i++) {
  142. state->buffer[i] +=
  143. (state->buffer[i - bpp] + context->previous[i]) / 2;
  144. }
  145. break;
  146. case 4:
  147. /* paeth filtering */
  148. bpp = (state->bits + 7) / 8;
  149. for (i = 1; i <= bpp; i++) {
  150. state->buffer[i] += context->previous[i];
  151. }
  152. for (; i <= row_len; i++) {
  153. int a, b, c;
  154. int pa, pb, pc;
  155. /* fetch pixels */
  156. a = state->buffer[i - bpp];
  157. b = context->previous[i];
  158. c = context->previous[i - bpp];
  159. /* distances to surrounding pixels */
  160. pa = abs(b - c);
  161. pb = abs(a - c);
  162. pc = abs(a + b - 2 * c);
  163. /* pick predictor with the shortest distance */
  164. state->buffer[i] += (pa <= pb && pa <= pc) ? a
  165. : (pb <= pc) ? b
  166. : c;
  167. }
  168. break;
  169. default:
  170. state->errcode = IMAGING_CODEC_UNKNOWN;
  171. free(context->previous);
  172. context->previous = NULL;
  173. inflateEnd(&context->z_stream);
  174. return -1;
  175. }
  176. break;
  177. case ZIP_TIFF_PREDICTOR:
  178. bpp = (state->bits + 7) / 8;
  179. for (i = bpp + 1; i <= row_len; i++) {
  180. state->buffer[i] += state->buffer[i - bpp];
  181. }
  182. break;
  183. }
  184. /* Stuff data into the image */
  185. if (context->interlaced) {
  186. int col = STARTING_COL[context->pass];
  187. if (state->bits >= 8) {
  188. /* Stuff pixels in their correct location, one by one */
  189. for (i = 0; i < row_len; i += ((state->bits + 7) / 8)) {
  190. state->shuffle(
  191. (UINT8 *)im->image[state->y] + col * im->pixelsize,
  192. state->buffer + context->prefix + i,
  193. 1);
  194. col += COL_INCREMENT[context->pass];
  195. }
  196. } else {
  197. /* Handle case with more than a pixel in each byte */
  198. int row_bits = ((state->xsize + OFFSET[context->pass]) /
  199. COL_INCREMENT[context->pass]) *
  200. state->bits;
  201. for (i = 0; i < row_bits; i += state->bits) {
  202. UINT8 byte = *(state->buffer + context->prefix + (i / 8));
  203. byte <<= (i % 8);
  204. state->shuffle(
  205. (UINT8 *)im->image[state->y] + col * im->pixelsize, &byte, 1);
  206. col += COL_INCREMENT[context->pass];
  207. }
  208. }
  209. /* Find next valid scanline */
  210. state->y += ROW_INCREMENT[context->pass];
  211. while (state->y >= state->ysize || row_len <= 0) {
  212. context->pass++;
  213. if (context->pass == 7) {
  214. /* Force exit below */
  215. state->y = state->ysize;
  216. break;
  217. }
  218. state->y = STARTING_ROW[context->pass];
  219. row_len = get_row_len(state, context->pass);
  220. /* Since we're moving to the "first" line, the previous line
  221. * should be black to make filters work correctly */
  222. memset(state->buffer, 0, state->bytes + 1);
  223. }
  224. } else {
  225. state->shuffle(
  226. (UINT8 *)im->image[state->y + state->yoff] +
  227. state->xoff * im->pixelsize,
  228. state->buffer + context->prefix,
  229. state->xsize);
  230. state->y++;
  231. }
  232. /* all inflate output has been consumed */
  233. context->last_output = 0;
  234. if (state->y >= state->ysize || err == Z_STREAM_END) {
  235. /* The image and the data should end simultaneously */
  236. /* if (state->y < state->ysize || err != Z_STREAM_END)
  237. state->errcode = IMAGING_CODEC_BROKEN; */
  238. free(context->previous);
  239. context->previous = NULL;
  240. inflateEnd(&context->z_stream);
  241. return -1; /* end of file (errcode=0) */
  242. }
  243. /* Swap buffer pointers */
  244. ptr = state->buffer;
  245. state->buffer = context->previous;
  246. context->previous = ptr;
  247. }
  248. return bytes; /* consumed all of it */
  249. }
  250. int
  251. ImagingZipDecodeCleanup(ImagingCodecState state) {
  252. /* called to free the decompression engine when the decode terminates
  253. due to a corrupt or truncated image
  254. */
  255. ZIPSTATE *context = (ZIPSTATE *)state->context;
  256. /* Clean up */
  257. if (context->previous) {
  258. inflateEnd(&context->z_stream);
  259. free(context->previous);
  260. context->previous = NULL;
  261. }
  262. return -1;
  263. }
  264. #endif