Debian Codegraph @

pTk/mTk/additions/imgTIFFzip.c

raw
1 /* $Header: /usr/people/sam/tiff/libtiff/RCS/tif_zip.c,v 1.8 1996/03/07 17:00:23 sam Rel $ */
2
3 /*
4 * Copyright (c) 1995-1996 Sam Leffler
5 * Copyright (c) 1995-1996 Silicon Graphics, Inc.
6 *
7 * Permission to use, copy, modify, distribute, and sell this software and
8 * its documentation for any purpose is hereby granted without fee, provided
9 * that (i) the above copyright notices and this permission notice appear in
10 * all copies of the software and related documentation, and (ii) the names of
11 * Sam Leffler and Silicon Graphics may not be used in any advertising or
12 * publicity relating to the software without the specific, prior written
13 * permission of Sam Leffler and Silicon Graphics.
14 *
15 * THE SOFTWARE IS PROVIDED "AS-IS" AND WITHOUT WARRANTY OF ANY KIND,
16 * EXPRESS, IMPLIED OR OTHERWISE, INCLUDING WITHOUT LIMITATION, ANY
17 * WARRANTY OF MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE.
18 *
19 * IN NO EVENT SHALL SAM LEFFLER OR SILICON GRAPHICS BE LIABLE FOR
20 * ANY SPECIAL, INCIDENTAL, INDIRECT OR CONSEQUENTIAL DAMAGES OF ANY KIND,
21 * OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS,
22 * WHETHER OR NOT ADVISED OF THE POSSIBILITY OF DAMAGE, AND ON ANY THEORY OF
23 * LIABILITY, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE
24 * OF THIS SOFTWARE.
25 */
26
27 #include "tiffiop.h"
28
29 /*
30 * TIFF Library.
31 *
32 * ZIP (aka Deflate) Compression Support
33 *
34 * This file is simply an interface to the zlib library written by
35 * Jean-loup Gailly and Mark Adler. You must use version 1.0 or later
36 * of the library: this code assumes the 1.0 API and also depends on
37 * the ability to write the zlib header multiple times (one per strip)
38 * which was not possible with versions prior to 0.95. Note also that
39 * older versions of this codec avoided this bug by supressing the header
40 * entirely. This means that files written with the old library cannot
41 * be read; they should be converted to a different compression scheme
42 * and then reconverted.
43 *
44 * The data format used by the zlib library is described in the files
45 * zlib-3.1.doc, deflate-1.1.doc and gzip-4.1.doc, available in the
46 * directory ftp://ftp.uu.net/pub/archiving/zip/doc. The library was
47 * last found at ftp://ftp.uu.net/pub/archiving/zip/zlib/zlib-0.99.tar.gz.
48 */
49 #include "tif_predict.h"
50 #undef EXPORT
51 #include "zlib.h"
52
53 #include <assert.h>
54 #include "imgInt.h"
55
56 #ifdef __WIN32__
57 #define Z_LIB_NAME "zlib.dll"
58 #else
59 #define WINAPI /**/
60 #endif
61
62 #ifndef Z_LIB_NAME
63 #define Z_LIB_NAME "libz.so"
64 #endif
65
66 /*
67 * Sigh, ZLIB_VERSION is defined as a string so there's no
68 * way to do a proper check here. Instead we guess based
69 * on the presence of defines that were added between the
70 * 0.95 and 1.0 distributions.
71 */
72 #if !defined(Z_NO_COMPRESSION) || !defined(Z_DEFLATED)
73 #error "Antiquated ZLIB software; you must use version 1.0 or later"
74 #endif
75
76 /*
77 * State block for each open TIFF
78 * file using ZIP compression/decompression.
79 */
80 typedef struct {
81 TIFFPredictorState predict;
82 z_stream stream;
83 int zipquality; /* compression level */
84 int state; /* state flags */
85 #define ZSTATE_INIT 0x1 /* zlib setup successfully */
86
87 TIFFVGetMethod vgetparent; /* super-class method */
88 TIFFVSetMethod vsetparent; /* super-class method */
89 } ZIPState;
90
91 #define ZState(tif) ((ZIPState*) (tif)->tif_data)
92 #define DecoderState(tif) ZState(tif)
93 #define EncoderState(tif) ZState(tif)
94
95 /*
96 * Stuff to support dynamical loading of libz.
97 */
98
99 static struct LibzFunctions {
100 VOID *handle;
101 int (WINAPI * deflate) _ANSI_ARGS_((z_streamp, int));
102 int (WINAPI * dInit) _ANSI_ARGS_((z_streamp, int, CONST char *, int));
103 int (WINAPI * deflateReset) _ANSI_ARGS_((z_streamp));
104 int (WINAPI * deflateParams) _ANSI_ARGS_((z_streamp, int, int));
105 int (WINAPI * deflateEnd) _ANSI_ARGS_((z_streamp));
106 int (WINAPI * inflate) _ANSI_ARGS_((z_streamp, int));
107 int (WINAPI * iInit) _ANSI_ARGS_((z_streamp, CONST char *, int));
108 int (WINAPI * inflateReset) _ANSI_ARGS_((z_streamp));
109 int (WINAPI * inflateSync) _ANSI_ARGS_((z_streamp));
110 int (WINAPI * inflateEnd) _ANSI_ARGS_((z_streamp));
111 } zlib = {0};
112
113 static char *symbols[] = {
114 "deflate",
115 "deflateInit_",
116 "deflateReset",
117 "deflateParams",
118 "deflateEnd",
119 "inflate",
120 "inflateInit_",
121 "inflateReset",
122 "inflateSync",
123 "inflateEnd",
124 (char *) NULL
125 };
126
127 extern void ImgTIFFfree _ANSI_ARGS_((tdata_t data));
128 extern tdata_t ImgTIFFmalloc _ANSI_ARGS_((tsize_t size));
129 extern tdata_t ImgTIFFrealloc _ANSI_ARGS_((tdata_t data, tsize_t size));
130 extern void ImgTIFFError _ANSI_ARGS_(TCL_VARARGS(CONST char *, module));
131 extern int ImgTIFFPredictorInit _ANSI_ARGS_((TIFF *tif));
132 extern void ImgTIFFMergeFieldInfo _ANSI_ARGS_((TIFF* tif, CONST VOID *voidp, int i));
133 extern int ImgTIFFFlushData1 _ANSI_ARGS_((TIFF *tif));
134
135 static int ZIPSetupDecode _ANSI_ARGS_((TIFF* tif));
136
137 static int
138 ZIPSetupDecode(tif)
139 TIFF* tif;
140 {
141 ZIPState* sp = DecoderState(tif);
142 static char module[] = "ZIPSetupDecode";
143
144 assert(sp != NULL);
145 if (zlib.iInit(&sp->stream, ZLIB_VERSION, sizeof(z_stream)) != Z_OK) {
146 ImgTIFFError(module, "%s: %s", tif->tif_name, sp->stream.msg);
147 return (0);
148 } else {
149 sp->state |= ZSTATE_INIT;
150 return (1);
151 }
152 }
153
154 /*
155 * Setup state for decoding a strip.
156 */
157
158 static int
159 #ifdef _USING_PROTOTYPES_
160 ZIPPreDecode (
161 TIFF* tif,
162 tsample_t s)
163 #else
164 ZIPPreDecode(tif, s)
165 TIFF* tif;
166 tsample_t s;
167 #endif
168 {
169 ZIPState* sp = DecoderState(tif);
170
171 (void) s;
172 assert(sp != NULL);
173 sp->stream.next_in = tif->tif_rawdata;
174 sp->stream.avail_in = tif->tif_rawcc;
175 return (zlib.inflateReset(&sp->stream) == Z_OK);
176 }
177
178 static int
179 #ifdef _USING_PROTOTYPES_
180 ZIPDecode (
181 TIFF* tif,
182 tidata_t op,
183 tsize_t occ,
184 tsample_t s)
185 #else
186 ZIPDecode(tif, op, occ, s)
187 TIFF* tif;
188 tidata_t op;
189 tsize_t occ;
190 tsample_t s;
191 #endif
192 {
193 ZIPState* sp = DecoderState(tif);
194 static char module[] = "ZIPDecode";
195
196 (void) s;
197 assert(sp != NULL);
198 sp->stream.next_out = op;
199 sp->stream.avail_out = occ;
200 do {
201 int state = zlib.inflate(&sp->stream, Z_PARTIAL_FLUSH);
202 if (state == Z_STREAM_END)
203 break;
204 if (state == Z_DATA_ERROR) {
205 ImgTIFFError(module,
206 "%s: Decoding error at scanline %d, %s",
207 tif->tif_name, tif->tif_row, sp->stream.msg);
208 if (zlib.inflateSync(&sp->stream) != Z_OK)
209 return (0);
210 continue;
211 }
212 if (state != Z_OK) {
213 ImgTIFFError(module, "%s: zlib error: %s",
214 tif->tif_name, sp->stream.msg);
215 return (0);
216 }
217 } while (sp->stream.avail_out > 0);
218 if (sp->stream.avail_out != 0) {
219 ImgTIFFError(module,
220 "%s: Not enough data at scanline %d (short %d bytes)",
221 tif->tif_name, tif->tif_row, sp->stream.avail_out);
222 return (0);
223 }
224 return (1);
225 }
226
227 static int
228 #ifdef _USING_PROTOTYPES_
229 ZIPSetupEncode (
230 TIFF* tif)
231 #else
232 ZIPSetupEncode(tif)
233 TIFF* tif;
234 #endif
235 {
236 ZIPState* sp = EncoderState(tif);
237 static char module[] = "ZIPSetupEncode";
238
239 assert(sp != NULL);
240 if (zlib.dInit(&sp->stream, sp->zipquality, ZLIB_VERSION, sizeof(z_stream)) != Z_OK) {
241 ImgTIFFError(module, "%s: %s", tif->tif_name, sp->stream.msg);
242 return (0);
243 } else {
244 sp->state |= ZSTATE_INIT;
245 return (1);
246 }
247 }
248
249 /*
250 * Reset encoding state at the start of a strip.
251 */
252
253 static int
254 #ifdef _USING_PROTOTYPES_
255 ZIPPreEncode (
256 TIFF* tif,
257 tsample_t s)
258 #else
259 ZIPPreEncode(tif, s)
260 TIFF* tif;
261 tsample_t s;
262 #endif
263 {
264 ZIPState *sp = EncoderState(tif);
265
266 (void) s;
267 assert(sp != NULL);
268 sp->stream.next_out = tif->tif_rawdata;
269 sp->stream.avail_out = tif->tif_rawdatasize;
270 return (zlib.deflateReset(&sp->stream) == Z_OK);
271 }
272
273 /*
274 * Encode a chunk of pixels.
275 */
276
277 static int
278 #ifdef _USING_PROTOTYPES_
279 ZIPEncode (TIFF* tif,
280 tidata_t bp,
281 tsize_t cc,
282 tsample_t s)
283 #else
284 ZIPEncode(tif, bp, cc, s)
285 TIFF* tif;
286 tidata_t bp;
287 tsize_t cc;
288 tsample_t s;
289 #endif
290 {
291 ZIPState *sp = EncoderState(tif);
292 static char module[] = "ZIPEncode";
293
294 (void) s;
295 sp->stream.next_in = bp;
296 sp->stream.avail_in = cc;
297 do {
298 if (zlib.deflate(&sp->stream, Z_NO_FLUSH) != Z_OK) {
299 ImgTIFFError(module, "%s: Encoder error: %s",
300 tif->tif_name, sp->stream.msg);
301 return (0);
302 }
303 if (sp->stream.avail_out == 0) {
304 tif->tif_rawcc = tif->tif_rawdatasize;
305 ImgTIFFFlushData1(tif);
306 sp->stream.next_out = tif->tif_rawdata;
307 sp->stream.avail_out = tif->tif_rawdatasize;
308 }
309 } while (sp->stream.avail_in > 0);
310 return (1);
311 }
312
313 /*
314 * Finish off an encoded strip by flushing the last
315 * string and tacking on an End Of Information code.
316 */
317
318 static int ZIPPostEncode _ANSI_ARGS_((TIFF* tif));
319
320 static int
321 ZIPPostEncode(tif)
322 TIFF* tif;
323 {
324 ZIPState *sp = EncoderState(tif);
325 static char module[] = "ZIPPostEncode";
326 int state;
327
328 sp->stream.avail_in = 0;
329 do {
330 state = zlib.deflate(&sp->stream, Z_FINISH);
331 switch (state) {
332 case Z_STREAM_END:
333 case Z_OK:
334 if ((int) sp->stream.avail_out != tif->tif_rawdatasize) {
335 tif->tif_rawcc =
336 tif->tif_rawdatasize - sp->stream.avail_out;
337 ImgTIFFFlushData1(tif);
338 sp->stream.next_out = tif->tif_rawdata;
339 sp->stream.avail_out = tif->tif_rawdatasize;
340 }
341 break;
342 default:
343 ImgTIFFError(module, "%s: zlib error: %s",
344 tif->tif_name, sp->stream.msg);
345 return (0);
346 }
347 } while (state != Z_STREAM_END);
348 return (1);
349 }
350
351 static void ZIPCleanup _ANSI_ARGS_((TIFF* tif));
352
353 static void
354 ZIPCleanup(tif)
355 TIFF* tif;
356 {
357 ZIPState* sp = ZState(tif);
358 if (sp) {
359 if (tif->tif_mode == O_RDONLY)
360 zlib.inflateEnd(&sp->stream);
361 else
362 zlib.deflateEnd(&sp->stream);
363 ImgTIFFfree(sp);
364 tif->tif_data = NULL;
365 }
366 }
367
368 static int ZIPVSetField _ANSI_ARGS_((TIFF* tif, ttag_t tag, va_list ap));
369
370 static int
371 ZIPVSetField(tif, tag, ap)
372 TIFF* tif;
373 ttag_t tag;
374 va_list ap;
375 {
376 ZIPState* sp = ZState(tif);
377 static char module[] = "ZIPVSetField";
378
379 switch (tag) {
380 case TIFFTAG_ZIPQUALITY:
381 sp->zipquality = va_arg(ap, int);
382 if (tif->tif_mode != O_RDONLY && (sp->state&ZSTATE_INIT)) {
383 if (zlib.deflateParams(&sp->stream,
384 sp->zipquality, Z_DEFAULT_STRATEGY) != Z_OK) {
385 ImgTIFFError(module, "%s: zlib error: %s",
386 tif->tif_name, sp->stream.msg);
387 return (0);
388 }
389 }
390 return (1);
391 default:
392 return (*sp->vsetparent)(tif, tag, ap);
393 }
394 /*NOTREACHED*/
395 }
396
397 static int ZIPVGetField _ANSI_ARGS_((TIFF* tif, ttag_t tag, va_list ap));
398
399 static int
400 ZIPVGetField(tif, tag, ap)
401 TIFF* tif;
402 ttag_t tag;
403 va_list ap;
404 {
405 ZIPState* sp = ZState(tif);
406
407 switch (tag) {
408 case TIFFTAG_ZIPQUALITY:
409 *va_arg(ap, int*) = sp->zipquality;
410 break;
411 default:
412 return (*sp->vgetparent)(tif, tag, ap);
413 }
414 return (1);
415 }
416
417 static CONST TIFFFieldInfo zipFieldInfo[] = {
418 { TIFFTAG_ZIPQUALITY, 0, 0, TIFF_ANY, FIELD_PSEUDO,
419 TRUE, FALSE, "" },
420 };
421 #define N(a) (sizeof (a) / sizeof (a[0]))
422
423 static voidpf
424 ZipAlloc(opaque, items, size)
425 voidpf opaque;
426 uInt items;
427 uInt size;
428 {
429 return (voidpf) ImgTIFFmalloc((tsize_t)(items * size));
430 }
431
432 static void
433 ZipFree(opaque, address)
434 voidpf opaque;
435 voidpf address;
436 {
437 ImgTIFFfree((tdata_t) address);
438 }
439
440 int
441 ImgInitTIFFzip(handle, scheme)
442 VOID * handle;
443 int scheme;
444 {
445 TIFF* tif = (TIFF *) handle;
446 ZIPState* sp;
447
448 assert(scheme == COMPRESSION_DEFLATE);
449
450 if (ImgLoadLib(NULL, Z_LIB_NAME, &zlib.handle, symbols, 10)
451 != TCL_OK) {
452 ImgTIFFError("", "%s: Cannot load %s", tif->tif_name, Z_LIB_NAME);
453 return (0);
454 }
455 /*
456 * Allocate state block so tag methods have storage to record values.
457 */
458 tif->tif_data = (tidata_t) ImgTIFFmalloc(sizeof (ZIPState));
459 if (tif->tif_data == NULL)
460 goto bad;
461 sp = ZState(tif);
462 sp->stream.zalloc = ZipAlloc;
463 sp->stream.zfree = ZipFree;
464 sp->stream.opaque = NULL;
465 sp->stream.data_type = Z_BINARY;
466
467 /*
468 * Merge codec-specific tag information and
469 * override parent get/set field methods.
470 */
471 ImgTIFFMergeFieldInfo(tif, (CONST VOID *) zipFieldInfo, N(zipFieldInfo));
472 sp->vgetparent = tif->tif_vgetfield;
473 tif->tif_vgetfield = ZIPVGetField; /* hook for codec tags */
474 sp->vsetparent = tif->tif_vsetfield;
475 tif->tif_vsetfield = ZIPVSetField; /* hook for codec tags */
476
477 /* Default values for codec-specific fields */
478 sp->zipquality = Z_DEFAULT_COMPRESSION; /* default comp. level */
479 sp->state = 0;
480
481 /*
482 * Install codec methods.
483 */
484 tif->tif_setupdecode = ZIPSetupDecode;
485 tif->tif_predecode = ZIPPreDecode;
486 tif->tif_decoderow = ZIPDecode;
487 tif->tif_decodestrip = ZIPDecode;
488 tif->tif_decodetile = ZIPDecode;
489 tif->tif_setupencode = ZIPSetupEncode;
490 tif->tif_preencode = ZIPPreEncode;
491 tif->tif_postencode = ZIPPostEncode;
492 tif->tif_encoderow = ZIPEncode;
493 tif->tif_encodestrip = ZIPEncode;
494 tif->tif_encodetile = ZIPEncode;
495 tif->tif_cleanup = ZIPCleanup;
496 /*
497 * Setup predictor setup.
498 */
499 (void) ImgTIFFPredictorInit(tif);
500 return (1);
501 bad:
502 ImgTIFFError("TIFFInitZIP", "No space for ZIP state block");
503 return (0);
504 }