ZipEncode.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375
  1. /*
  2. * The Python Imaging Library.
  3. * $Id$
  4. *
  5. * coder for ZIP (deflated) image data
  6. *
  7. * History:
  8. * 96-12-29 fl created
  9. * 96-12-30 fl adaptive filter selection, encoder tuning
  10. *
  11. * Copyright (c) Fredrik Lundh 1996.
  12. * Copyright (c) Secret Labs AB 1997.
  13. *
  14. * See the README file for information on usage and redistribution.
  15. */
  16. #include "Imaging.h"
  17. #ifdef HAVE_LIBZ
  18. #include "Zip.h"
  19. int
  20. ImagingZipEncode(Imaging im, ImagingCodecState state, UINT8* buf, int bytes)
  21. {
  22. ZIPSTATE* context = (ZIPSTATE*) state->context;
  23. int err;
  24. int compress_level, compress_type;
  25. UINT8* ptr;
  26. int i, bpp, s, sum;
  27. ImagingSectionCookie cookie;
  28. if (!state->state) {
  29. /* Initialization */
  30. /* Valid modes are ZIP_PNG, ZIP_PNG_PALETTE, and ZIP_TIFF */
  31. /* overflow check for malloc */
  32. if (state->bytes > INT_MAX - 1) {
  33. state->errcode = IMAGING_CODEC_MEMORY;
  34. return -1;
  35. }
  36. /* Expand standard buffer to make room for the filter selector,
  37. and allocate filter buffers */
  38. free(state->buffer);
  39. /* malloc check ok, overflow checked above */
  40. state->buffer = (UINT8*) malloc(state->bytes+1);
  41. context->previous = (UINT8*) malloc(state->bytes+1);
  42. context->prior = (UINT8*) malloc(state->bytes+1);
  43. context->up = (UINT8*) malloc(state->bytes+1);
  44. context->average = (UINT8*) malloc(state->bytes+1);
  45. context->paeth = (UINT8*) malloc(state->bytes+1);
  46. if (!state->buffer || !context->previous || !context->prior ||
  47. !context->up || !context->average || !context->paeth) {
  48. free(context->paeth);
  49. free(context->average);
  50. free(context->up);
  51. free(context->prior);
  52. free(context->previous);
  53. state->errcode = IMAGING_CODEC_MEMORY;
  54. return -1;
  55. }
  56. /* Initialise filter buffers */
  57. state->buffer[0] = 0;
  58. context->prior[0] = 1;
  59. context->up[0] = 2;
  60. context->average[0] = 3;
  61. context->paeth[0] = 4;
  62. /* Initialise previous buffer to black */
  63. memset(context->previous, 0, state->bytes+1);
  64. /* Setup compression context */
  65. context->z_stream.zalloc = (alloc_func)0;
  66. context->z_stream.zfree = (free_func)0;
  67. context->z_stream.opaque = (voidpf)0;
  68. context->z_stream.next_in = 0;
  69. context->z_stream.avail_in = 0;
  70. compress_level = (context->optimize) ? Z_BEST_COMPRESSION
  71. : context->compress_level;
  72. if (context->compress_type == -1) {
  73. compress_type = (context->mode == ZIP_PNG) ? Z_FILTERED
  74. : Z_DEFAULT_STRATEGY;
  75. } else {
  76. compress_type = context->compress_type;
  77. }
  78. err = deflateInit2(&context->z_stream,
  79. /* compression level */
  80. compress_level,
  81. /* compression method */
  82. Z_DEFLATED,
  83. /* compression memory resources */
  84. 15, 9,
  85. /* compression strategy (image data are filtered)*/
  86. compress_type);
  87. if (err < 0) {
  88. state->errcode = IMAGING_CODEC_CONFIG;
  89. return -1;
  90. }
  91. if (context->dictionary && context->dictionary_size > 0) {
  92. err = deflateSetDictionary(&context->z_stream, (unsigned char *)context->dictionary,
  93. context->dictionary_size);
  94. if (err < 0) {
  95. state->errcode = IMAGING_CODEC_CONFIG;
  96. return -1;
  97. }
  98. }
  99. /* Ready to decode */
  100. state->state = 1;
  101. }
  102. /* Setup the destination buffer */
  103. context->z_stream.next_out = buf;
  104. context->z_stream.avail_out = bytes;
  105. if (context->z_stream.next_in && context->z_stream.avail_in > 0) {
  106. /* We have some data from previous round, deflate it first */
  107. err = deflate(&context->z_stream, Z_NO_FLUSH);
  108. if (err < 0) {
  109. /* Something went wrong inside the compression library */
  110. if (err == Z_DATA_ERROR)
  111. state->errcode = IMAGING_CODEC_BROKEN;
  112. else if (err == Z_MEM_ERROR)
  113. state->errcode = IMAGING_CODEC_MEMORY;
  114. else
  115. state->errcode = IMAGING_CODEC_CONFIG;
  116. free(context->paeth);
  117. free(context->average);
  118. free(context->up);
  119. free(context->prior);
  120. free(context->previous);
  121. deflateEnd(&context->z_stream);
  122. return -1;
  123. }
  124. }
  125. ImagingSectionEnter(&cookie);
  126. for (;;) {
  127. switch (state->state) {
  128. case 1:
  129. /* Compress image data */
  130. while (context->z_stream.avail_out > 0) {
  131. if (state->y >= state->ysize) {
  132. /* End of image; now flush compressor buffers */
  133. state->state = 2;
  134. break;
  135. }
  136. /* Stuff image data into the compressor */
  137. state->shuffle(state->buffer+1,
  138. (UINT8*) im->image[state->y + state->yoff] +
  139. state->xoff * im->pixelsize,
  140. state->xsize);
  141. state->y++;
  142. context->output = state->buffer;
  143. if (context->mode == ZIP_PNG) {
  144. /* Filter the image data. For each line, select
  145. the filter that gives the least total distance
  146. from zero for the filtered data (taken from
  147. LIBPNG) */
  148. bpp = (state->bits + 7) / 8;
  149. /* 0. No filter */
  150. for (i = 1, sum = 0; i <= state->bytes; i++) {
  151. UINT8 v = state->buffer[i];
  152. sum += (v < 128) ? v : 256 - v;
  153. }
  154. /* 2. Up. We'll test this first to save time when
  155. an image line is identical to the one above. */
  156. if (sum > 0) {
  157. for (i = 1, s = 0; i <= state->bytes; i++) {
  158. UINT8 v = state->buffer[i] - context->previous[i];
  159. context->up[i] = v;
  160. s += (v < 128) ? v : 256 - v;
  161. }
  162. if (s < sum) {
  163. context->output = context->up;
  164. sum = s; /* 0 if line was duplicated */
  165. }
  166. }
  167. /* 1. Prior */
  168. if (sum > 0) {
  169. for (i = 1, s = 0; i <= bpp; i++) {
  170. UINT8 v = state->buffer[i];
  171. context->prior[i] = v;
  172. s += (v < 128) ? v : 256 - v;
  173. }
  174. for (; i <= state->bytes; i++) {
  175. UINT8 v = state->buffer[i] - state->buffer[i-bpp];
  176. context->prior[i] = v;
  177. s += (v < 128) ? v : 256 - v;
  178. }
  179. if (s < sum) {
  180. context->output = context->prior;
  181. sum = s; /* 0 if line is solid */
  182. }
  183. }
  184. /* 3. Average (not very common in real-life images,
  185. so its only used with the optimize option) */
  186. if (context->optimize && sum > 0) {
  187. for (i = 1, s = 0; i <= bpp; i++) {
  188. UINT8 v = state->buffer[i] - context->previous[i]/2;
  189. context->average[i] = v;
  190. s += (v < 128) ? v : 256 - v;
  191. }
  192. for (; i <= state->bytes; i++) {
  193. UINT8 v = state->buffer[i] -
  194. (state->buffer[i-bpp] + context->previous[i])/2;
  195. context->average[i] = v;
  196. s += (v < 128) ? v : 256 - v;
  197. }
  198. if (s < sum) {
  199. context->output = context->average;
  200. sum = s;
  201. }
  202. }
  203. /* 4. Paeth */
  204. if (sum > 0) {
  205. for (i = 1, s = 0; i <= bpp; i++) {
  206. UINT8 v = state->buffer[i] - context->previous[i];
  207. context->paeth[i] = v;
  208. s += (v < 128) ? v : 256 - v;
  209. }
  210. for (; i <= state->bytes; i++) {
  211. UINT8 v;
  212. int a, b, c;
  213. int pa, pb, pc;
  214. /* fetch pixels */
  215. a = state->buffer[i-bpp];
  216. b = context->previous[i];
  217. c = context->previous[i-bpp];
  218. /* distances to surrounding pixels */
  219. pa = abs(b - c);
  220. pb = abs(a - c);
  221. pc = abs(a + b - 2*c);
  222. /* pick predictor with the shortest distance */
  223. v = state->buffer[i] -
  224. ((pa <= pb && pa <= pc) ? a :
  225. (pb <= pc) ? b : c);
  226. context->paeth[i] = v;
  227. s += (v < 128) ? v : 256 - v;
  228. }
  229. if (s < sum) {
  230. context->output = context->paeth;
  231. sum = s;
  232. }
  233. }
  234. }
  235. /* Compress this line */
  236. context->z_stream.next_in = context->output;
  237. context->z_stream.avail_in = state->bytes+1;
  238. err = deflate(&context->z_stream, Z_NO_FLUSH);
  239. if (err < 0) {
  240. /* Something went wrong inside the compression library */
  241. if (err == Z_DATA_ERROR)
  242. state->errcode = IMAGING_CODEC_BROKEN;
  243. else if (err == Z_MEM_ERROR)
  244. state->errcode = IMAGING_CODEC_MEMORY;
  245. else
  246. state->errcode = IMAGING_CODEC_CONFIG;
  247. free(context->paeth);
  248. free(context->average);
  249. free(context->up);
  250. free(context->prior);
  251. free(context->previous);
  252. deflateEnd(&context->z_stream);
  253. ImagingSectionLeave(&cookie);
  254. return -1;
  255. }
  256. /* Swap buffer pointers */
  257. ptr = state->buffer;
  258. state->buffer = context->previous;
  259. context->previous = ptr;
  260. }
  261. if (context->z_stream.avail_out == 0)
  262. break; /* Buffer full */
  263. case 2:
  264. /* End of image data; flush compressor buffers */
  265. while (context->z_stream.avail_out > 0) {
  266. err = deflate(&context->z_stream, Z_FINISH);
  267. if (err == Z_STREAM_END) {
  268. free(context->paeth);
  269. free(context->average);
  270. free(context->up);
  271. free(context->prior);
  272. free(context->previous);
  273. deflateEnd(&context->z_stream);
  274. state->errcode = IMAGING_CODEC_END;
  275. break;
  276. }
  277. if (context->z_stream.avail_out == 0)
  278. break; /* Buffer full */
  279. }
  280. }
  281. ImagingSectionLeave(&cookie);
  282. return bytes - context->z_stream.avail_out;
  283. }
  284. /* Should never ever arrive here... */
  285. state->errcode = IMAGING_CODEC_CONFIG;
  286. ImagingSectionLeave(&cookie);
  287. return -1;
  288. }
  289. /* -------------------------------------------------------------------- */
  290. /* Cleanup */
  291. /* -------------------------------------------------------------------- */
  292. int
  293. ImagingZipEncodeCleanup(ImagingCodecState state) {
  294. ZIPSTATE* context = (ZIPSTATE*) state->context;
  295. if (context->dictionary) {
  296. free (context->dictionary);
  297. context->dictionary = NULL;
  298. }
  299. return -1;
  300. }
  301. const char*
  302. ImagingZipVersion(void)
  303. {
  304. return ZLIB_VERSION;
  305. }
  306. #endif