3 * Copyright (c) 1995-1997 Sam Leffler
4 * Copyright (c) 1995-1997 Silicon Graphics, Inc.
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.
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.
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
31 * ZIP (aka Deflate) Compression Support
33 * This file is simply an interface to the zlib library written by
34 * Jean-loup Gailly and Mark Adler. You must use version 1.0 or later
35 * of the library: this code assumes the 1.0 API and also depends on
36 * the ability to write the zlib header multiple times (one per strip)
37 * which was not possible with versions prior to 0.95. Note also that
38 * older versions of this codec avoided this bug by supressing the header
39 * entirely. This means that files written with the old library cannot
40 * be read; they should be converted to a different compression scheme
41 * and then reconverted.
43 * The data format used by the zlib library is described in the files
44 * zlib-3.1.doc, deflate-1.1.doc and gzip-4.1.doc, available in the
45 * directory ftp://ftp.uu.net/pub/archiving/zip/doc. The library was
46 * last found at ftp://ftp.uu.net/pub/archiving/zip/zlib/zlib-0.99.tar.gz.
48 #include "tif_predict.h"
54 * Sigh, ZLIB_VERSION is defined as a string so there's no
55 * way to do a proper check here. Instead we guess based
56 * on the presence of #defines that were added between the
57 * 0.95 and 1.0 distributions.
59 #if !defined(Z_NO_COMPRESSION) || !defined(Z_DEFLATED)
60 #error "Antiquated ZLIB software; you must use version 1.0 or later"
64 * State block for each open TIFF
65 * file using ZIP compression/decompression.
68 TIFFPredictorState predict
;
70 int zipquality
; /* compression level */
71 int state
; /* state flags */
72 #define ZSTATE_INIT_DECODE 0x01
73 #define ZSTATE_INIT_ENCODE 0x02
75 TIFFVGetMethod vgetparent
; /* super-class method */
76 TIFFVSetMethod vsetparent
; /* super-class method */
79 #define ZState(tif) ((ZIPState*) (tif)->tif_data)
80 #define DecoderState(tif) ZState(tif)
81 #define EncoderState(tif) ZState(tif)
83 static int ZIPEncode(TIFF
* tif
, uint8
* bp
, tmsize_t cc
, uint16 s
);
84 static int ZIPDecode(TIFF
* tif
, uint8
* op
, tmsize_t occ
, uint16 s
);
87 ZIPFixupTags(TIFF
* tif
)
94 ZIPSetupDecode(TIFF
* tif
)
96 static const char module[] = "ZIPSetupDecode";
97 ZIPState
* sp
= DecoderState(tif
);
101 /* if we were last encoding, terminate this mode */
102 if (sp
->state
& ZSTATE_INIT_ENCODE
) {
103 deflateEnd(&sp
->stream
);
107 if (inflateInit(&sp
->stream
) != Z_OK
) {
108 TIFFErrorExt(tif
->tif_clientdata
, module, "%s", sp
->stream
.msg
);
111 sp
->state
|= ZSTATE_INIT_DECODE
;
117 * Setup state for decoding a strip.
120 ZIPPreDecode(TIFF
* tif
, uint16 s
)
122 static const char module[] = "ZIPPreDecode";
123 ZIPState
* sp
= DecoderState(tif
);
128 if( (sp
->state
& ZSTATE_INIT_DECODE
) == 0 )
129 tif
->tif_setupdecode( tif
);
131 sp
->stream
.next_in
= tif
->tif_rawdata
;
132 assert(sizeof(sp
->stream
.avail_in
)==4); /* if this assert gets raised,
133 we need to simplify this code to reflect a ZLib that is likely updated
134 to deal with 8byte memory sizes, though this code will respond
135 apropriately even before we simplify it */
136 sp
->stream
.avail_in
= (uInt
) tif
->tif_rawcc
;
137 if ((tmsize_t
)sp
->stream
.avail_in
!= tif
->tif_rawcc
)
139 TIFFErrorExt(tif
->tif_clientdata
, module, "ZLib cannot deal with buffers this size");
142 return (inflateReset(&sp
->stream
) == Z_OK
);
146 ZIPDecode(TIFF
* tif
, uint8
* op
, tmsize_t occ
, uint16 s
)
148 static const char module[] = "ZIPDecode";
149 ZIPState
* sp
= DecoderState(tif
);
153 assert(sp
->state
== ZSTATE_INIT_DECODE
);
155 sp
->stream
.next_in
= tif
->tif_rawcp
;
156 sp
->stream
.avail_in
= (uInt
) tif
->tif_rawcc
;
158 sp
->stream
.next_out
= op
;
159 assert(sizeof(sp
->stream
.avail_out
)==4); /* if this assert gets raised,
160 we need to simplify this code to reflect a ZLib that is likely updated
161 to deal with 8byte memory sizes, though this code will respond
162 apropriately even before we simplify it */
163 sp
->stream
.avail_out
= (uInt
) occ
;
164 if ((tmsize_t
)sp
->stream
.avail_out
!= occ
)
166 TIFFErrorExt(tif
->tif_clientdata
, module, "ZLib cannot deal with buffers this size");
170 int state
= inflate(&sp
->stream
, Z_PARTIAL_FLUSH
);
171 if (state
== Z_STREAM_END
)
173 if (state
== Z_DATA_ERROR
) {
174 TIFFErrorExt(tif
->tif_clientdata
, module,
175 "Decoding error at scanline %lu, %s",
176 (unsigned long) tif
->tif_row
, sp
->stream
.msg
);
177 if (inflateSync(&sp
->stream
) != Z_OK
)
182 TIFFErrorExt(tif
->tif_clientdata
, module, "ZLib error: %s",
186 } while (sp
->stream
.avail_out
> 0);
187 if (sp
->stream
.avail_out
!= 0) {
188 TIFFErrorExt(tif
->tif_clientdata
, module,
189 "Not enough data at scanline %lu (short " TIFF_UINT64_FORMAT
" bytes)",
190 (unsigned long) tif
->tif_row
, (TIFF_UINT64_T
) sp
->stream
.avail_out
);
194 tif
->tif_rawcp
= sp
->stream
.next_in
;
195 tif
->tif_rawcc
= sp
->stream
.avail_in
;
201 ZIPSetupEncode(TIFF
* tif
)
203 static const char module[] = "ZIPSetupEncode";
204 ZIPState
* sp
= EncoderState(tif
);
207 if (sp
->state
& ZSTATE_INIT_DECODE
) {
208 inflateEnd(&sp
->stream
);
212 if (deflateInit(&sp
->stream
, sp
->zipquality
) != Z_OK
) {
213 TIFFErrorExt(tif
->tif_clientdata
, module, "%s", sp
->stream
.msg
);
216 sp
->state
|= ZSTATE_INIT_ENCODE
;
222 * Reset encoding state at the start of a strip.
225 ZIPPreEncode(TIFF
* tif
, uint16 s
)
227 static const char module[] = "ZIPPreEncode";
228 ZIPState
*sp
= EncoderState(tif
);
232 if( sp
->state
!= ZSTATE_INIT_ENCODE
)
233 tif
->tif_setupencode( tif
);
235 sp
->stream
.next_out
= tif
->tif_rawdata
;
236 assert(sizeof(sp
->stream
.avail_out
)==4); /* if this assert gets raised,
237 we need to simplify this code to reflect a ZLib that is likely updated
238 to deal with 8byte memory sizes, though this code will respond
239 apropriately even before we simplify it */
240 sp
->stream
.avail_out
= tif
->tif_rawdatasize
;
241 if ((tmsize_t
)sp
->stream
.avail_out
!= tif
->tif_rawdatasize
)
243 TIFFErrorExt(tif
->tif_clientdata
, module, "ZLib cannot deal with buffers this size");
246 return (deflateReset(&sp
->stream
) == Z_OK
);
250 * Encode a chunk of pixels.
253 ZIPEncode(TIFF
* tif
, uint8
* bp
, tmsize_t cc
, uint16 s
)
255 static const char module[] = "ZIPEncode";
256 ZIPState
*sp
= EncoderState(tif
);
259 assert(sp
->state
== ZSTATE_INIT_ENCODE
);
262 sp
->stream
.next_in
= bp
;
263 assert(sizeof(sp
->stream
.avail_in
)==4); /* if this assert gets raised,
264 we need to simplify this code to reflect a ZLib that is likely updated
265 to deal with 8byte memory sizes, though this code will respond
266 apropriately even before we simplify it */
267 sp
->stream
.avail_in
= (uInt
) cc
;
268 if ((tmsize_t
)sp
->stream
.avail_in
!= cc
)
270 TIFFErrorExt(tif
->tif_clientdata
, module, "ZLib cannot deal with buffers this size");
274 if (deflate(&sp
->stream
, Z_NO_FLUSH
) != Z_OK
) {
275 TIFFErrorExt(tif
->tif_clientdata
, module, "Encoder error: %s",
279 if (sp
->stream
.avail_out
== 0) {
280 tif
->tif_rawcc
= tif
->tif_rawdatasize
;
282 sp
->stream
.next_out
= tif
->tif_rawdata
;
283 sp
->stream
.avail_out
= (uInt
) tif
->tif_rawdatasize
; /* this is a safe typecast, as check is made already in ZIPPreEncode */
285 } while (sp
->stream
.avail_in
> 0);
290 * Finish off an encoded strip by flushing the last
291 * string and tacking on an End Of Information code.
294 ZIPPostEncode(TIFF
* tif
)
296 static const char module[] = "ZIPPostEncode";
297 ZIPState
*sp
= EncoderState(tif
);
300 sp
->stream
.avail_in
= 0;
302 state
= deflate(&sp
->stream
, Z_FINISH
);
306 if ((tmsize_t
)sp
->stream
.avail_out
!= tif
->tif_rawdatasize
)
308 tif
->tif_rawcc
= tif
->tif_rawdatasize
- sp
->stream
.avail_out
;
310 sp
->stream
.next_out
= tif
->tif_rawdata
;
311 sp
->stream
.avail_out
= (uInt
) tif
->tif_rawdatasize
; /* this is a safe typecast, as check is made already in ZIPPreEncode */
315 TIFFErrorExt(tif
->tif_clientdata
, module, "ZLib error: %s",
319 } while (state
!= Z_STREAM_END
);
324 ZIPCleanup(TIFF
* tif
)
326 ZIPState
* sp
= ZState(tif
);
330 (void)TIFFPredictorCleanup(tif
);
332 tif
->tif_tagmethods
.vgetfield
= sp
->vgetparent
;
333 tif
->tif_tagmethods
.vsetfield
= sp
->vsetparent
;
335 if (sp
->state
& ZSTATE_INIT_ENCODE
) {
336 deflateEnd(&sp
->stream
);
338 } else if( sp
->state
& ZSTATE_INIT_DECODE
) {
339 inflateEnd(&sp
->stream
);
343 tif
->tif_data
= NULL
;
345 _TIFFSetDefaultCompressionState(tif
);
349 ZIPVSetField(TIFF
* tif
, uint32 tag
, va_list ap
)
351 static const char module[] = "ZIPVSetField";
352 ZIPState
* sp
= ZState(tif
);
355 case TIFFTAG_ZIPQUALITY
:
356 sp
->zipquality
= (int) va_arg(ap
, int);
357 if ( sp
->state
&ZSTATE_INIT_ENCODE
) {
358 if (deflateParams(&sp
->stream
,
359 sp
->zipquality
, Z_DEFAULT_STRATEGY
) != Z_OK
) {
360 TIFFErrorExt(tif
->tif_clientdata
, module, "ZLib error: %s",
367 return (*sp
->vsetparent
)(tif
, tag
, ap
);
373 ZIPVGetField(TIFF
* tif
, uint32 tag
, va_list ap
)
375 ZIPState
* sp
= ZState(tif
);
378 case TIFFTAG_ZIPQUALITY
:
379 *va_arg(ap
, int*) = sp
->zipquality
;
382 return (*sp
->vgetparent
)(tif
, tag
, ap
);
387 static const TIFFField zipFields
[] = {
388 { TIFFTAG_ZIPQUALITY
, 0, 0, TIFF_ANY
, 0, TIFF_SETGET_INT
, TIFF_SETGET_UNDEFINED
, FIELD_PSEUDO
, TRUE
, FALSE
, "", NULL
},
392 TIFFInitZIP(TIFF
* tif
, int scheme
)
394 static const char module[] = "TIFFInitZIP";
397 assert( (scheme
== COMPRESSION_DEFLATE
)
398 || (scheme
== COMPRESSION_ADOBE_DEFLATE
));
401 * Merge codec-specific tag information.
403 if (!_TIFFMergeFields(tif
, zipFields
, TIFFArrayCount(zipFields
))) {
404 TIFFErrorExt(tif
->tif_clientdata
, module,
405 "Merging Deflate codec-specific tags failed");
410 * Allocate state block so tag methods have storage to record values.
412 tif
->tif_data
= (uint8
*) _TIFFmalloc(sizeof (ZIPState
));
413 if (tif
->tif_data
== NULL
)
416 sp
->stream
.zalloc
= NULL
;
417 sp
->stream
.zfree
= NULL
;
418 sp
->stream
.opaque
= NULL
;
419 sp
->stream
.data_type
= Z_BINARY
;
422 * Override parent get/set field methods.
424 sp
->vgetparent
= tif
->tif_tagmethods
.vgetfield
;
425 tif
->tif_tagmethods
.vgetfield
= ZIPVGetField
; /* hook for codec tags */
426 sp
->vsetparent
= tif
->tif_tagmethods
.vsetfield
;
427 tif
->tif_tagmethods
.vsetfield
= ZIPVSetField
; /* hook for codec tags */
429 /* Default values for codec-specific fields */
430 sp
->zipquality
= Z_DEFAULT_COMPRESSION
; /* default comp. level */
434 * Install codec methods.
436 tif
->tif_fixuptags
= ZIPFixupTags
;
437 tif
->tif_setupdecode
= ZIPSetupDecode
;
438 tif
->tif_predecode
= ZIPPreDecode
;
439 tif
->tif_decoderow
= ZIPDecode
;
440 tif
->tif_decodestrip
= ZIPDecode
;
441 tif
->tif_decodetile
= ZIPDecode
;
442 tif
->tif_setupencode
= ZIPSetupEncode
;
443 tif
->tif_preencode
= ZIPPreEncode
;
444 tif
->tif_postencode
= ZIPPostEncode
;
445 tif
->tif_encoderow
= ZIPEncode
;
446 tif
->tif_encodestrip
= ZIPEncode
;
447 tif
->tif_encodetile
= ZIPEncode
;
448 tif
->tif_cleanup
= ZIPCleanup
;
450 * Setup predictor setup.
452 (void) TIFFPredictorInit(tif
);
455 TIFFErrorExt(tif
->tif_clientdata
, module,
456 "No space for ZIP state block");
459 #endif /* ZIP_SUPORT */
461 /* vim: set ts=8 sts=8 sw=8 noet: */