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"
56 * Sigh, ZLIB_VERSION is defined as a string so there's no
57 * way to do a proper check here. Instead we guess based
58 * on the presence of #defines that were added between the
59 * 0.95 and 1.0 distributions.
61 #if !defined(Z_NO_COMPRESSION) || !defined(Z_DEFLATED)
62 #error "Antiquated ZLIB software; you must use version 1.0 or later"
66 * State block for each open TIFF
67 * file using ZIP compression/decompression.
70 TIFFPredictorState predict
;
72 int zipquality
; /* compression level */
73 int state
; /* state flags */
74 #define ZSTATE_INIT 0x1 /* zlib setup successfully */
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
*, tidata_t
, tsize_t
, tsample_t
);
85 static int ZIPDecode(TIFF
*, tidata_t
, tsize_t
, tsample_t
);
88 ZIPSetupDecode(TIFF
* tif
)
90 ZIPState
* sp
= DecoderState(tif
);
91 static const char module[] = "ZIPSetupDecode";
94 if (inflateInit(&sp
->stream
) != Z_OK
) {
95 TIFFError(module, "%s: %s", tif
->tif_name
, sp
->stream
.msg
);
98 sp
->state
|= ZSTATE_INIT
;
104 * Setup state for decoding a strip.
107 ZIPPreDecode(TIFF
* tif
, tsample_t s
)
109 ZIPState
* sp
= DecoderState(tif
);
113 sp
->stream
.next_in
= tif
->tif_rawdata
;
114 sp
->stream
.avail_in
= tif
->tif_rawcc
;
115 return (inflateReset(&sp
->stream
) == Z_OK
);
119 ZIPDecode(TIFF
* tif
, tidata_t op
, tsize_t occ
, tsample_t s
)
121 ZIPState
* sp
= DecoderState(tif
);
122 static const char module[] = "ZIPDecode";
126 sp
->stream
.next_out
= op
;
127 sp
->stream
.avail_out
= occ
;
129 int state
= inflate(&sp
->stream
, Z_PARTIAL_FLUSH
);
130 if (state
== Z_STREAM_END
)
132 if (state
== Z_DATA_ERROR
) {
134 "%s: Decoding error at scanline %d, %s",
135 tif
->tif_name
, tif
->tif_row
, sp
->stream
.msg
);
136 if (inflateSync(&sp
->stream
) != Z_OK
)
141 TIFFError(module, "%s: zlib error: %s",
142 tif
->tif_name
, sp
->stream
.msg
);
145 } while (sp
->stream
.avail_out
> 0);
146 if (sp
->stream
.avail_out
!= 0) {
148 "%s: Not enough data at scanline %d (short %d bytes)",
149 tif
->tif_name
, tif
->tif_row
, sp
->stream
.avail_out
);
156 ZIPSetupEncode(TIFF
* tif
)
158 ZIPState
* sp
= EncoderState(tif
);
159 static const char module[] = "ZIPSetupEncode";
162 if (deflateInit(&sp
->stream
, sp
->zipquality
) != Z_OK
) {
163 TIFFError(module, "%s: %s", tif
->tif_name
, sp
->stream
.msg
);
166 sp
->state
|= ZSTATE_INIT
;
172 * Reset encoding state at the start of a strip.
175 ZIPPreEncode(TIFF
* tif
, tsample_t s
)
177 ZIPState
*sp
= EncoderState(tif
);
181 sp
->stream
.next_out
= tif
->tif_rawdata
;
182 sp
->stream
.avail_out
= tif
->tif_rawdatasize
;
183 return (deflateReset(&sp
->stream
) == Z_OK
);
187 * Encode a chunk of pixels.
190 ZIPEncode(TIFF
* tif
, tidata_t bp
, tsize_t cc
, tsample_t s
)
192 ZIPState
*sp
= EncoderState(tif
);
193 static const char module[] = "ZIPEncode";
196 sp
->stream
.next_in
= bp
;
197 sp
->stream
.avail_in
= cc
;
199 if (deflate(&sp
->stream
, Z_NO_FLUSH
) != Z_OK
) {
200 TIFFError(module, "%s: Encoder error: %s",
201 tif
->tif_name
, sp
->stream
.msg
);
204 if (sp
->stream
.avail_out
== 0) {
205 tif
->tif_rawcc
= tif
->tif_rawdatasize
;
207 sp
->stream
.next_out
= tif
->tif_rawdata
;
208 sp
->stream
.avail_out
= tif
->tif_rawdatasize
;
210 } while (sp
->stream
.avail_in
> 0);
215 * Finish off an encoded strip by flushing the last
216 * string and tacking on an End Of Information code.
219 ZIPPostEncode(TIFF
* tif
)
221 ZIPState
*sp
= EncoderState(tif
);
222 static const char module[] = "ZIPPostEncode";
225 sp
->stream
.avail_in
= 0;
227 state
= deflate(&sp
->stream
, Z_FINISH
);
231 if ((int)sp
->stream
.avail_out
!= (int)tif
->tif_rawdatasize
)
234 tif
->tif_rawdatasize
- sp
->stream
.avail_out
;
236 sp
->stream
.next_out
= tif
->tif_rawdata
;
237 sp
->stream
.avail_out
= tif
->tif_rawdatasize
;
241 TIFFError(module, "%s: zlib error: %s",
242 tif
->tif_name
, sp
->stream
.msg
);
245 } while (state
!= Z_STREAM_END
);
250 ZIPCleanup(TIFF
* tif
)
252 ZIPState
* sp
= ZState(tif
);
254 if (sp
->state
&ZSTATE_INIT
) {
255 /* NB: avoid problems in the library */
256 if (tif
->tif_mode
== O_RDONLY
)
257 inflateEnd(&sp
->stream
);
259 deflateEnd(&sp
->stream
);
262 tif
->tif_data
= NULL
;
267 ZIPVSetField(TIFF
* tif
, ttag_t tag
, va_list ap
)
269 ZIPState
* sp
= ZState(tif
);
270 static const char module[] = "ZIPVSetField";
273 case TIFFTAG_ZIPQUALITY
:
274 sp
->zipquality
= va_arg(ap
, int);
275 if (tif
->tif_mode
!= O_RDONLY
&& (sp
->state
&ZSTATE_INIT
)) {
276 if (deflateParams(&sp
->stream
,
277 sp
->zipquality
, Z_DEFAULT_STRATEGY
) != Z_OK
) {
278 TIFFError(module, "%s: zlib error: %s",
279 tif
->tif_name
, sp
->stream
.msg
);
285 return (*sp
->vsetparent
)(tif
, tag
, ap
);
291 ZIPVGetField(TIFF
* tif
, ttag_t tag
, va_list ap
)
293 ZIPState
* sp
= ZState(tif
);
296 case TIFFTAG_ZIPQUALITY
:
297 *va_arg(ap
, int*) = sp
->zipquality
;
300 return (*sp
->vgetparent
)(tif
, tag
, ap
);
305 static const TIFFFieldInfo zipFieldInfo
[] = {
306 { TIFFTAG_ZIPQUALITY
, 0, 0, TIFF_ANY
, FIELD_PSEUDO
,
309 #define N(a) (sizeof (a) / sizeof (a[0]))
312 TIFFInitZIP(TIFF
* tif
, int scheme
)
316 assert( (scheme
== COMPRESSION_DEFLATE
) || (scheme
== COMPRESSION_ADOBE_DEFLATE
));
319 * Allocate state block so tag methods have storage to record values.
321 tif
->tif_data
= (tidata_t
) _TIFFmalloc(sizeof (ZIPState
));
322 if (tif
->tif_data
== NULL
)
325 sp
->stream
.zalloc
= NULL
;
326 sp
->stream
.zfree
= NULL
;
327 sp
->stream
.opaque
= NULL
;
328 sp
->stream
.data_type
= Z_BINARY
;
331 * Merge codec-specific tag information and
332 * override parent get/set field methods.
334 _TIFFMergeFieldInfo(tif
, zipFieldInfo
, N(zipFieldInfo
));
335 sp
->vgetparent
= tif
->tif_tagmethods
.vgetfield
;
336 tif
->tif_tagmethods
.vgetfield
= ZIPVGetField
; /* hook for codec tags */
337 sp
->vsetparent
= tif
->tif_tagmethods
.vsetfield
;
338 tif
->tif_tagmethods
.vsetfield
= ZIPVSetField
; /* hook for codec tags */
340 /* Default values for codec-specific fields */
341 sp
->zipquality
= Z_DEFAULT_COMPRESSION
; /* default comp. level */
345 * Install codec methods.
347 tif
->tif_setupdecode
= ZIPSetupDecode
;
348 tif
->tif_predecode
= ZIPPreDecode
;
349 tif
->tif_decoderow
= ZIPDecode
;
350 tif
->tif_decodestrip
= ZIPDecode
;
351 tif
->tif_decodetile
= ZIPDecode
;
352 tif
->tif_setupencode
= ZIPSetupEncode
;
353 tif
->tif_preencode
= ZIPPreEncode
;
354 tif
->tif_postencode
= ZIPPostEncode
;
355 tif
->tif_encoderow
= ZIPEncode
;
356 tif
->tif_encodestrip
= ZIPEncode
;
357 tif
->tif_encodetile
= ZIPEncode
;
358 tif
->tif_cleanup
= ZIPCleanup
;
360 * Setup predictor setup.
362 (void) TIFFPredictorInit(tif
);
365 TIFFError("TIFFInitZIP", "No space for ZIP state block");
368 #endif /* ZIP_SUPORT */