4 * Copyright (c) 1995-1997 Sam Leffler
5 * Copyright (c) 1995-1997 Silicon Graphics, Inc.
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.
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.
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
32 * ZIP (aka Deflate) Compression Support
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.
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.
49 #include "tif_predict.h"
55 * Sigh, ZLIB_VERSION is defined as a string so there's no
56 * way to do a proper check here. Instead we guess based
57 * on the presence of #defines that were added between the
58 * 0.95 and 1.0 distributions.
60 #if !defined(Z_NO_COMPRESSION) || !defined(Z_DEFLATED)
61 #error "Antiquated ZLIB software; you must use version 1.0 or later"
65 * State block for each open TIFF
66 * file using ZIP compression/decompression.
69 TIFFPredictorState predict
;
71 int zipquality
; /* compression level */
72 int state
; /* state flags */
73 #define ZSTATE_INIT_DECODE 0x01
74 #define ZSTATE_INIT_ENCODE 0x02
76 TIFFVGetMethod vgetparent
; /* super-class method */
77 TIFFVSetMethod vsetparent
; /* super-class method */
80 #define ZState(tif) ((ZIPState*) (tif)->tif_data)
81 #define DecoderState(tif) ZState(tif)
82 #define EncoderState(tif) ZState(tif)
84 static int ZIPEncode(TIFF
* tif
, uint8
* bp
, tmsize_t cc
, uint16 s
);
85 static int ZIPDecode(TIFF
* tif
, uint8
* op
, tmsize_t occ
, uint16 s
);
88 ZIPFixupTags(TIFF
* tif
)
95 ZIPSetupDecode(TIFF
* tif
)
97 static const char module[] = "ZIPSetupDecode";
98 ZIPState
* sp
= DecoderState(tif
);
102 /* if we were last encoding, terminate this mode */
103 if (sp
->state
& ZSTATE_INIT_ENCODE
) {
104 deflateEnd(&sp
->stream
);
108 if (inflateInit(&sp
->stream
) != Z_OK
) {
109 TIFFErrorExt(tif
->tif_clientdata
, module, "%s", sp
->stream
.msg
);
112 sp
->state
|= ZSTATE_INIT_DECODE
;
118 * Setup state for decoding a strip.
121 ZIPPreDecode(TIFF
* tif
, uint16 s
)
123 static const char module[] = "ZIPPreDecode";
124 ZIPState
* sp
= DecoderState(tif
);
129 if( (sp
->state
& ZSTATE_INIT_DECODE
) == 0 )
130 tif
->tif_setupdecode( tif
);
132 sp
->stream
.next_in
= tif
->tif_rawdata
;
133 assert(sizeof(sp
->stream
.avail_in
)==4); /* if this assert gets raised,
134 we need to simplify this code to reflect a ZLib that is likely updated
135 to deal with 8byte memory sizes, though this code will respond
136 apropriately even before we simplify it */
137 sp
->stream
.avail_in
= (uInt
) tif
->tif_rawcc
;
138 if ((tmsize_t
)sp
->stream
.avail_in
!= tif
->tif_rawcc
)
140 TIFFErrorExt(tif
->tif_clientdata
, module, "ZLib cannot deal with buffers this size");
143 return (inflateReset(&sp
->stream
) == Z_OK
);
147 ZIPDecode(TIFF
* tif
, uint8
* op
, tmsize_t occ
, uint16 s
)
149 static const char module[] = "ZIPDecode";
150 ZIPState
* sp
= DecoderState(tif
);
154 assert(sp
->state
== ZSTATE_INIT_DECODE
);
156 sp
->stream
.next_in
= tif
->tif_rawcp
;
157 sp
->stream
.avail_in
= (uInt
) tif
->tif_rawcc
;
159 sp
->stream
.next_out
= op
;
160 assert(sizeof(sp
->stream
.avail_out
)==4); /* if this assert gets raised,
161 we need to simplify this code to reflect a ZLib that is likely updated
162 to deal with 8byte memory sizes, though this code will respond
163 apropriately even before we simplify it */
164 sp
->stream
.avail_out
= (uInt
) occ
;
165 if ((tmsize_t
)sp
->stream
.avail_out
!= occ
)
167 TIFFErrorExt(tif
->tif_clientdata
, module, "ZLib cannot deal with buffers this size");
171 int state
= inflate(&sp
->stream
, Z_PARTIAL_FLUSH
);
172 if (state
== Z_STREAM_END
)
174 if (state
== Z_DATA_ERROR
) {
175 TIFFErrorExt(tif
->tif_clientdata
, module,
176 "Decoding error at scanline %lu, %s",
177 (unsigned long) tif
->tif_row
, sp
->stream
.msg
);
178 if (inflateSync(&sp
->stream
) != Z_OK
)
183 TIFFErrorExt(tif
->tif_clientdata
, module, "ZLib error: %s",
187 } while (sp
->stream
.avail_out
> 0);
188 if (sp
->stream
.avail_out
!= 0) {
189 TIFFErrorExt(tif
->tif_clientdata
, module,
190 "Not enough data at scanline %lu (short " TIFF_UINT64_FORMAT
" bytes)",
191 (unsigned long) tif
->tif_row
, (TIFF_UINT64_T
) sp
->stream
.avail_out
);
195 tif
->tif_rawcp
= sp
->stream
.next_in
;
196 tif
->tif_rawcc
= sp
->stream
.avail_in
;
202 ZIPSetupEncode(TIFF
* tif
)
204 static const char module[] = "ZIPSetupEncode";
205 ZIPState
* sp
= EncoderState(tif
);
208 if (sp
->state
& ZSTATE_INIT_DECODE
) {
209 inflateEnd(&sp
->stream
);
213 if (deflateInit(&sp
->stream
, sp
->zipquality
) != Z_OK
) {
214 TIFFErrorExt(tif
->tif_clientdata
, module, "%s", sp
->stream
.msg
);
217 sp
->state
|= ZSTATE_INIT_ENCODE
;
223 * Reset encoding state at the start of a strip.
226 ZIPPreEncode(TIFF
* tif
, uint16 s
)
228 static const char module[] = "ZIPPreEncode";
229 ZIPState
*sp
= EncoderState(tif
);
233 if( sp
->state
!= ZSTATE_INIT_ENCODE
)
234 tif
->tif_setupencode( tif
);
236 sp
->stream
.next_out
= tif
->tif_rawdata
;
237 assert(sizeof(sp
->stream
.avail_out
)==4); /* if this assert gets raised,
238 we need to simplify this code to reflect a ZLib that is likely updated
239 to deal with 8byte memory sizes, though this code will respond
240 apropriately even before we simplify it */
241 sp
->stream
.avail_out
= tif
->tif_rawdatasize
;
242 if ((tmsize_t
)sp
->stream
.avail_out
!= tif
->tif_rawdatasize
)
244 TIFFErrorExt(tif
->tif_clientdata
, module, "ZLib cannot deal with buffers this size");
247 return (deflateReset(&sp
->stream
) == Z_OK
);
251 * Encode a chunk of pixels.
254 ZIPEncode(TIFF
* tif
, uint8
* bp
, tmsize_t cc
, uint16 s
)
256 static const char module[] = "ZIPEncode";
257 ZIPState
*sp
= EncoderState(tif
);
260 assert(sp
->state
== ZSTATE_INIT_ENCODE
);
263 sp
->stream
.next_in
= bp
;
264 assert(sizeof(sp
->stream
.avail_in
)==4); /* if this assert gets raised,
265 we need to simplify this code to reflect a ZLib that is likely updated
266 to deal with 8byte memory sizes, though this code will respond
267 apropriately even before we simplify it */
268 sp
->stream
.avail_in
= (uInt
) cc
;
269 if ((tmsize_t
)sp
->stream
.avail_in
!= cc
)
271 TIFFErrorExt(tif
->tif_clientdata
, module, "ZLib cannot deal with buffers this size");
275 if (deflate(&sp
->stream
, Z_NO_FLUSH
) != Z_OK
) {
276 TIFFErrorExt(tif
->tif_clientdata
, module, "Encoder error: %s",
280 if (sp
->stream
.avail_out
== 0) {
281 tif
->tif_rawcc
= tif
->tif_rawdatasize
;
283 sp
->stream
.next_out
= tif
->tif_rawdata
;
284 sp
->stream
.avail_out
= (uInt
) tif
->tif_rawdatasize
; /* this is a safe typecast, as check is made already in ZIPPreEncode */
286 } while (sp
->stream
.avail_in
> 0);
291 * Finish off an encoded strip by flushing the last
292 * string and tacking on an End Of Information code.
295 ZIPPostEncode(TIFF
* tif
)
297 static const char module[] = "ZIPPostEncode";
298 ZIPState
*sp
= EncoderState(tif
);
301 sp
->stream
.avail_in
= 0;
303 state
= deflate(&sp
->stream
, Z_FINISH
);
307 if ((tmsize_t
)sp
->stream
.avail_out
!= tif
->tif_rawdatasize
)
309 tif
->tif_rawcc
= tif
->tif_rawdatasize
- sp
->stream
.avail_out
;
311 sp
->stream
.next_out
= tif
->tif_rawdata
;
312 sp
->stream
.avail_out
= (uInt
) tif
->tif_rawdatasize
; /* this is a safe typecast, as check is made already in ZIPPreEncode */
316 TIFFErrorExt(tif
->tif_clientdata
, module, "ZLib error: %s",
320 } while (state
!= Z_STREAM_END
);
325 ZIPCleanup(TIFF
* tif
)
327 ZIPState
* sp
= ZState(tif
);
331 (void)TIFFPredictorCleanup(tif
);
333 tif
->tif_tagmethods
.vgetfield
= sp
->vgetparent
;
334 tif
->tif_tagmethods
.vsetfield
= sp
->vsetparent
;
336 if (sp
->state
& ZSTATE_INIT_ENCODE
) {
337 deflateEnd(&sp
->stream
);
339 } else if( sp
->state
& ZSTATE_INIT_DECODE
) {
340 inflateEnd(&sp
->stream
);
344 tif
->tif_data
= NULL
;
346 _TIFFSetDefaultCompressionState(tif
);
350 ZIPVSetField(TIFF
* tif
, uint32 tag
, va_list ap
)
352 static const char module[] = "ZIPVSetField";
353 ZIPState
* sp
= ZState(tif
);
356 case TIFFTAG_ZIPQUALITY
:
357 sp
->zipquality
= (int) va_arg(ap
, int);
358 if ( sp
->state
&ZSTATE_INIT_ENCODE
) {
359 if (deflateParams(&sp
->stream
,
360 sp
->zipquality
, Z_DEFAULT_STRATEGY
) != Z_OK
) {
361 TIFFErrorExt(tif
->tif_clientdata
, module, "ZLib error: %s",
368 return (*sp
->vsetparent
)(tif
, tag
, ap
);
374 ZIPVGetField(TIFF
* tif
, uint32 tag
, va_list ap
)
376 ZIPState
* sp
= ZState(tif
);
379 case TIFFTAG_ZIPQUALITY
:
380 *va_arg(ap
, int*) = sp
->zipquality
;
383 return (*sp
->vgetparent
)(tif
, tag
, ap
);
388 static const TIFFField zipFields
[] = {
389 { TIFFTAG_ZIPQUALITY
, 0, 0, TIFF_ANY
, 0, TIFF_SETGET_INT
, TIFF_SETGET_UNDEFINED
, FIELD_PSEUDO
, TRUE
, FALSE
, "", NULL
},
393 TIFFInitZIP(TIFF
* tif
, int scheme
)
395 static const char module[] = "TIFFInitZIP";
398 assert( (scheme
== COMPRESSION_DEFLATE
)
399 || (scheme
== COMPRESSION_ADOBE_DEFLATE
));
402 * Merge codec-specific tag information.
404 if (!_TIFFMergeFields(tif
, zipFields
, TIFFArrayCount(zipFields
))) {
405 TIFFErrorExt(tif
->tif_clientdata
, module,
406 "Merging Deflate codec-specific tags failed");
411 * Allocate state block so tag methods have storage to record values.
413 tif
->tif_data
= (uint8
*) _TIFFmalloc(sizeof (ZIPState
));
414 if (tif
->tif_data
== NULL
)
417 sp
->stream
.zalloc
= NULL
;
418 sp
->stream
.zfree
= NULL
;
419 sp
->stream
.opaque
= NULL
;
420 sp
->stream
.data_type
= Z_BINARY
;
423 * Override parent get/set field methods.
425 sp
->vgetparent
= tif
->tif_tagmethods
.vgetfield
;
426 tif
->tif_tagmethods
.vgetfield
= ZIPVGetField
; /* hook for codec tags */
427 sp
->vsetparent
= tif
->tif_tagmethods
.vsetfield
;
428 tif
->tif_tagmethods
.vsetfield
= ZIPVSetField
; /* hook for codec tags */
430 /* Default values for codec-specific fields */
431 sp
->zipquality
= Z_DEFAULT_COMPRESSION
; /* default comp. level */
435 * Install codec methods.
437 tif
->tif_fixuptags
= ZIPFixupTags
;
438 tif
->tif_setupdecode
= ZIPSetupDecode
;
439 tif
->tif_predecode
= ZIPPreDecode
;
440 tif
->tif_decoderow
= ZIPDecode
;
441 tif
->tif_decodestrip
= ZIPDecode
;
442 tif
->tif_decodetile
= ZIPDecode
;
443 tif
->tif_setupencode
= ZIPSetupEncode
;
444 tif
->tif_preencode
= ZIPPreEncode
;
445 tif
->tif_postencode
= ZIPPostEncode
;
446 tif
->tif_encoderow
= ZIPEncode
;
447 tif
->tif_encodestrip
= ZIPEncode
;
448 tif
->tif_encodetile
= ZIPEncode
;
449 tif
->tif_cleanup
= ZIPCleanup
;
451 * Setup predictor setup.
453 (void) TIFFPredictorInit(tif
);
456 TIFFErrorExt(tif
->tif_clientdata
, module,
457 "No space for ZIP state block");
460 #endif /* ZIP_SUPORT */
462 /* vim: set ts=8 sts=8 sw=8 noet: */