ZipEncode.c 13 KB

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