/src/FreeImage/Source/LibTIFF/tif_zip.c

https://bitbucket.org/cabalistic/ogredeps/ · C · 419 lines · 277 code · 49 blank · 93 comment · 59 complexity · a3cf712089ffdaf629f1f1ed4c1f8494 MD5 · raw file

  1. /* $Id: tif_zip.c,v 1.37 2011/04/10 17:14:09 drolon Exp $ */
  2. /*
  3. * Copyright (c) 1995-1997 Sam Leffler
  4. * Copyright (c) 1995-1997 Silicon Graphics, Inc.
  5. *
  6. * Permission to use, copy, modify, distribute, and sell this software and
  7. * its documentation for any purpose is hereby granted without fee, provided
  8. * that (i) the above copyright notices and this permission notice appear in
  9. * all copies of the software and related documentation, and (ii) the names of
  10. * Sam Leffler and Silicon Graphics may not be used in any advertising or
  11. * publicity relating to the software without the specific, prior written
  12. * permission of Sam Leffler and Silicon Graphics.
  13. *
  14. * THE SOFTWARE IS PROVIDED "AS-IS" AND WITHOUT WARRANTY OF ANY KIND,
  15. * EXPRESS, IMPLIED OR OTHERWISE, INCLUDING WITHOUT LIMITATION, ANY
  16. * WARRANTY OF MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE.
  17. *
  18. * IN NO EVENT SHALL SAM LEFFLER OR SILICON GRAPHICS BE LIABLE FOR
  19. * ANY SPECIAL, INCIDENTAL, INDIRECT OR CONSEQUENTIAL DAMAGES OF ANY KIND,
  20. * OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS,
  21. * WHETHER OR NOT ADVISED OF THE POSSIBILITY OF DAMAGE, AND ON ANY THEORY OF
  22. * LIABILITY, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE
  23. * OF THIS SOFTWARE.
  24. */
  25. #include "tiffiop.h"
  26. #ifdef ZIP_SUPPORT
  27. /*
  28. * TIFF Library.
  29. *
  30. * ZIP (aka Deflate) Compression Support
  31. *
  32. * This file is simply an interface to the zlib library written by
  33. * Jean-loup Gailly and Mark Adler. You must use version 1.0 or later
  34. * of the library: this code assumes the 1.0 API and also depends on
  35. * the ability to write the zlib header multiple times (one per strip)
  36. * which was not possible with versions prior to 0.95. Note also that
  37. * older versions of this codec avoided this bug by supressing the header
  38. * entirely. This means that files written with the old library cannot
  39. * be read; they should be converted to a different compression scheme
  40. * and then reconverted.
  41. *
  42. * The data format used by the zlib library is described in the files
  43. * zlib-3.1.doc, deflate-1.1.doc and gzip-4.1.doc, available in the
  44. * directory ftp://ftp.uu.net/pub/archiving/zip/doc. The library was
  45. * last found at ftp://ftp.uu.net/pub/archiving/zip/zlib/zlib-0.99.tar.gz.
  46. */
  47. #include "tif_predict.h"
  48. #include "../ZLib/zlib.h"
  49. #include <stdio.h>
  50. /*
  51. * Sigh, ZLIB_VERSION is defined as a string so there's no
  52. * way to do a proper check here. Instead we guess based
  53. * on the presence of #defines that were added between the
  54. * 0.95 and 1.0 distributions.
  55. */
  56. #if !defined(Z_NO_COMPRESSION) || !defined(Z_DEFLATED)
  57. #error "Antiquated ZLIB software; you must use version 1.0 or later"
  58. #endif
  59. /*
  60. * State block for each open TIFF
  61. * file using ZIP compression/decompression.
  62. */
  63. typedef struct {
  64. TIFFPredictorState predict;
  65. z_stream stream;
  66. int zipquality; /* compression level */
  67. int state; /* state flags */
  68. #define ZSTATE_INIT_DECODE 0x01
  69. #define ZSTATE_INIT_ENCODE 0x02
  70. TIFFVGetMethod vgetparent; /* super-class method */
  71. TIFFVSetMethod vsetparent; /* super-class method */
  72. } ZIPState;
  73. #define ZState(tif) ((ZIPState*) (tif)->tif_data)
  74. #define DecoderState(tif) ZState(tif)
  75. #define EncoderState(tif) ZState(tif)
  76. static int ZIPEncode(TIFF*, tidata_t, tsize_t, tsample_t);
  77. static int ZIPDecode(TIFF*, tidata_t, tsize_t, tsample_t);
  78. static int
  79. ZIPSetupDecode(TIFF* tif)
  80. {
  81. ZIPState* sp = DecoderState(tif);
  82. static const char module[] = "ZIPSetupDecode";
  83. assert(sp != NULL);
  84. /* if we were last encoding, terminate this mode */
  85. if (sp->state & ZSTATE_INIT_ENCODE) {
  86. deflateEnd(&sp->stream);
  87. sp->state = 0;
  88. }
  89. if (inflateInit(&sp->stream) != Z_OK) {
  90. TIFFErrorExt(tif->tif_clientdata, module, "%s: %s", tif->tif_name, sp->stream.msg);
  91. return (0);
  92. } else {
  93. sp->state |= ZSTATE_INIT_DECODE;
  94. return (1);
  95. }
  96. }
  97. /*
  98. * Setup state for decoding a strip.
  99. */
  100. static int
  101. ZIPPreDecode(TIFF* tif, tsample_t s)
  102. {
  103. ZIPState* sp = DecoderState(tif);
  104. (void) s;
  105. assert(sp != NULL);
  106. if( (sp->state & ZSTATE_INIT_DECODE) == 0 )
  107. tif->tif_setupdecode( tif );
  108. sp->stream.next_in = tif->tif_rawdata;
  109. sp->stream.avail_in = tif->tif_rawcc;
  110. return (inflateReset(&sp->stream) == Z_OK);
  111. }
  112. static int
  113. ZIPDecode(TIFF* tif, tidata_t op, tsize_t occ, tsample_t s)
  114. {
  115. ZIPState* sp = DecoderState(tif);
  116. static const char module[] = "ZIPDecode";
  117. (void) s;
  118. assert(sp != NULL);
  119. assert(sp->state == ZSTATE_INIT_DECODE);
  120. sp->stream.next_out = op;
  121. sp->stream.avail_out = occ;
  122. do {
  123. int state = inflate(&sp->stream, Z_PARTIAL_FLUSH);
  124. if (state == Z_STREAM_END)
  125. break;
  126. if (state == Z_DATA_ERROR) {
  127. TIFFErrorExt(tif->tif_clientdata, module,
  128. "%s: Decoding error at scanline %d, %s",
  129. tif->tif_name, tif->tif_row, sp->stream.msg);
  130. if (inflateSync(&sp->stream) != Z_OK)
  131. return (0);
  132. continue;
  133. }
  134. if (state != Z_OK) {
  135. TIFFErrorExt(tif->tif_clientdata, module, "%s: zlib error: %s",
  136. tif->tif_name, sp->stream.msg);
  137. return (0);
  138. }
  139. } while (sp->stream.avail_out > 0);
  140. if (sp->stream.avail_out != 0) {
  141. TIFFErrorExt(tif->tif_clientdata, module,
  142. "%s: Not enough data at scanline %d (short %d bytes)",
  143. tif->tif_name, tif->tif_row, sp->stream.avail_out);
  144. return (0);
  145. }
  146. return (1);
  147. }
  148. static int
  149. ZIPSetupEncode(TIFF* tif)
  150. {
  151. ZIPState* sp = EncoderState(tif);
  152. static const char module[] = "ZIPSetupEncode";
  153. assert(sp != NULL);
  154. if (sp->state & ZSTATE_INIT_DECODE) {
  155. inflateEnd(&sp->stream);
  156. sp->state = 0;
  157. }
  158. if (deflateInit(&sp->stream, sp->zipquality) != Z_OK) {
  159. TIFFErrorExt(tif->tif_clientdata, module, "%s: %s", tif->tif_name, sp->stream.msg);
  160. return (0);
  161. } else {
  162. sp->state |= ZSTATE_INIT_ENCODE;
  163. return (1);
  164. }
  165. }
  166. /*
  167. * Reset encoding state at the start of a strip.
  168. */
  169. static int
  170. ZIPPreEncode(TIFF* tif, tsample_t s)
  171. {
  172. ZIPState *sp = EncoderState(tif);
  173. (void) s;
  174. assert(sp != NULL);
  175. if( sp->state != ZSTATE_INIT_ENCODE )
  176. tif->tif_setupencode( tif );
  177. sp->stream.next_out = tif->tif_rawdata;
  178. sp->stream.avail_out = tif->tif_rawdatasize;
  179. return (deflateReset(&sp->stream) == Z_OK);
  180. }
  181. /*
  182. * Encode a chunk of pixels.
  183. */
  184. static int
  185. ZIPEncode(TIFF* tif, tidata_t bp, tsize_t cc, tsample_t s)
  186. {
  187. ZIPState *sp = EncoderState(tif);
  188. static const char module[] = "ZIPEncode";
  189. assert(sp != NULL);
  190. assert(sp->state == ZSTATE_INIT_ENCODE);
  191. (void) s;
  192. sp->stream.next_in = bp;
  193. sp->stream.avail_in = cc;
  194. do {
  195. if (deflate(&sp->stream, Z_NO_FLUSH) != Z_OK) {
  196. TIFFErrorExt(tif->tif_clientdata, module, "%s: Encoder error: %s",
  197. tif->tif_name, sp->stream.msg);
  198. return (0);
  199. }
  200. if (sp->stream.avail_out == 0) {
  201. tif->tif_rawcc = tif->tif_rawdatasize;
  202. TIFFFlushData1(tif);
  203. sp->stream.next_out = tif->tif_rawdata;
  204. sp->stream.avail_out = tif->tif_rawdatasize;
  205. }
  206. } while (sp->stream.avail_in > 0);
  207. return (1);
  208. }
  209. /*
  210. * Finish off an encoded strip by flushing the last
  211. * string and tacking on an End Of Information code.
  212. */
  213. static int
  214. ZIPPostEncode(TIFF* tif)
  215. {
  216. ZIPState *sp = EncoderState(tif);
  217. static const char module[] = "ZIPPostEncode";
  218. int state;
  219. sp->stream.avail_in = 0;
  220. do {
  221. state = deflate(&sp->stream, Z_FINISH);
  222. switch (state) {
  223. case Z_STREAM_END:
  224. case Z_OK:
  225. if ((int)sp->stream.avail_out != (int)tif->tif_rawdatasize)
  226. {
  227. tif->tif_rawcc =
  228. tif->tif_rawdatasize - sp->stream.avail_out;
  229. TIFFFlushData1(tif);
  230. sp->stream.next_out = tif->tif_rawdata;
  231. sp->stream.avail_out = tif->tif_rawdatasize;
  232. }
  233. break;
  234. default:
  235. TIFFErrorExt(tif->tif_clientdata, module, "%s: zlib error: %s",
  236. tif->tif_name, sp->stream.msg);
  237. return (0);
  238. }
  239. } while (state != Z_STREAM_END);
  240. return (1);
  241. }
  242. static void
  243. ZIPCleanup(TIFF* tif)
  244. {
  245. ZIPState* sp = ZState(tif);
  246. assert(sp != 0);
  247. (void)TIFFPredictorCleanup(tif);
  248. tif->tif_tagmethods.vgetfield = sp->vgetparent;
  249. tif->tif_tagmethods.vsetfield = sp->vsetparent;
  250. if (sp->state & ZSTATE_INIT_ENCODE) {
  251. deflateEnd(&sp->stream);
  252. sp->state = 0;
  253. } else if( sp->state & ZSTATE_INIT_DECODE) {
  254. inflateEnd(&sp->stream);
  255. sp->state = 0;
  256. }
  257. _TIFFfree(sp);
  258. tif->tif_data = NULL;
  259. _TIFFSetDefaultCompressionState(tif);
  260. }
  261. static int
  262. ZIPVSetField(TIFF* tif, ttag_t tag, va_list ap)
  263. {
  264. ZIPState* sp = ZState(tif);
  265. static const char module[] = "ZIPVSetField";
  266. switch (tag) {
  267. case TIFFTAG_ZIPQUALITY:
  268. sp->zipquality = va_arg(ap, int);
  269. if ( sp->state&ZSTATE_INIT_ENCODE ) {
  270. if (deflateParams(&sp->stream,
  271. sp->zipquality, Z_DEFAULT_STRATEGY) != Z_OK) {
  272. TIFFErrorExt(tif->tif_clientdata, module, "%s: zlib error: %s",
  273. tif->tif_name, sp->stream.msg);
  274. return (0);
  275. }
  276. }
  277. return (1);
  278. default:
  279. return (*sp->vsetparent)(tif, tag, ap);
  280. }
  281. /*NOTREACHED*/
  282. }
  283. static int
  284. ZIPVGetField(TIFF* tif, ttag_t tag, va_list ap)
  285. {
  286. ZIPState* sp = ZState(tif);
  287. switch (tag) {
  288. case TIFFTAG_ZIPQUALITY:
  289. *va_arg(ap, int*) = sp->zipquality;
  290. break;
  291. default:
  292. return (*sp->vgetparent)(tif, tag, ap);
  293. }
  294. return (1);
  295. }
  296. static const TIFFFieldInfo zipFieldInfo[] = {
  297. { TIFFTAG_ZIPQUALITY, 0, 0, TIFF_ANY, FIELD_PSEUDO,
  298. TRUE, FALSE, "" },
  299. };
  300. int
  301. TIFFInitZIP(TIFF* tif, int scheme)
  302. {
  303. static const char module[] = "TIFFInitZIP";
  304. ZIPState* sp;
  305. assert( (scheme == COMPRESSION_DEFLATE)
  306. || (scheme == COMPRESSION_ADOBE_DEFLATE));
  307. /*
  308. * Merge codec-specific tag information.
  309. */
  310. if (!_TIFFMergeFieldInfo(tif, zipFieldInfo,
  311. TIFFArrayCount(zipFieldInfo))) {
  312. TIFFErrorExt(tif->tif_clientdata, module,
  313. "Merging Deflate codec-specific tags failed");
  314. return 0;
  315. }
  316. /*
  317. * Allocate state block so tag methods have storage to record values.
  318. */
  319. tif->tif_data = (tidata_t) _TIFFmalloc(sizeof (ZIPState));
  320. if (tif->tif_data == NULL)
  321. goto bad;
  322. sp = ZState(tif);
  323. sp->stream.zalloc = NULL;
  324. sp->stream.zfree = NULL;
  325. sp->stream.opaque = NULL;
  326. sp->stream.data_type = Z_BINARY;
  327. /*
  328. * Override parent get/set field methods.
  329. */
  330. sp->vgetparent = tif->tif_tagmethods.vgetfield;
  331. tif->tif_tagmethods.vgetfield = ZIPVGetField; /* hook for codec tags */
  332. sp->vsetparent = tif->tif_tagmethods.vsetfield;
  333. tif->tif_tagmethods.vsetfield = ZIPVSetField; /* hook for codec tags */
  334. /* Default values for codec-specific fields */
  335. sp->zipquality = Z_DEFAULT_COMPRESSION; /* default comp. level */
  336. sp->state = 0;
  337. /*
  338. * Install codec methods.
  339. */
  340. tif->tif_setupdecode = ZIPSetupDecode;
  341. tif->tif_predecode = ZIPPreDecode;
  342. tif->tif_decoderow = ZIPDecode;
  343. tif->tif_decodestrip = ZIPDecode;
  344. tif->tif_decodetile = ZIPDecode;
  345. tif->tif_setupencode = ZIPSetupEncode;
  346. tif->tif_preencode = ZIPPreEncode;
  347. tif->tif_postencode = ZIPPostEncode;
  348. tif->tif_encoderow = ZIPEncode;
  349. tif->tif_encodestrip = ZIPEncode;
  350. tif->tif_encodetile = ZIPEncode;
  351. tif->tif_cleanup = ZIPCleanup;
  352. /*
  353. * Setup predictor setup.
  354. */
  355. (void) TIFFPredictorInit(tif);
  356. return (1);
  357. bad:
  358. TIFFErrorExt(tif->tif_clientdata, module,
  359. "No space for ZIP state block");
  360. return (0);
  361. }
  362. #endif /* ZIP_SUPORT */
  363. /* vim: set ts=8 sts=8 sw=8 noet: */
  364. /*
  365. * Local Variables:
  366. * mode: c
  367. * c-basic-offset: 8
  368. * fill-column: 78
  369. * End:
  370. */