4 * Copyright (c) 1988-1997 Sam Leffler
5 * Copyright (c) 1991-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
31 * Rev 5.0 Lempel-Ziv & Welch Compression Support
33 * This code is derived from the compress program whose code is
34 * derived from software contributed to Berkeley by James A. Woods,
35 * derived from original work by Spencer Thomas and Joseph Orost.
37 * The original Berkeley copyright notice appears below in its entirety.
39 /* Watcom C++ (or its make utility) doesn't like long filenames */
43 #include "tif_predict.h"
50 * NB: The 5.0 spec describes a different algorithm than Aldus
51 * implements. Specifically, Aldus does code length transitions
52 * one code earlier than should be done (for real LZW).
53 * Earlier versions of this library implemented the correct
54 * LZW algorithm, but emitted codes in a bit order opposite
55 * to the TIFF spec. Thus, to maintain compatibility w/ Aldus
56 * we interpret MSB-LSB ordered codes to be images written w/
57 * old versions of this library, but otherwise adhere to the
58 * Aldus "off by one" algorithm.
60 * Future revisions to the TIFF spec are expected to "clarify this issue".
62 #define LZW_COMPAT /* include backwards compatibility code */
64 * Each strip of data is supposed to be terminated by a CODE_EOI.
65 * If the following #define is included, the decoder will also
66 * check for end-of-strip w/o seeing this code. This makes the
67 * library more robust, but also slower.
69 #define LZW_CHECKEOS /* include checks for strips w/o EOI code */
71 #define MAXCODE(n) ((1L<<(n))-1)
73 * The TIFF spec specifies that encoded bit
74 * strings range from 9 to 12 bits.
76 #define BITS_MIN 9 /* start with 9 bits */
77 #define BITS_MAX 12 /* max of 12 bit strings */
78 /* predefined codes */
79 #define CODE_CLEAR 256 /* code to clear string table */
80 #define CODE_EOI 257 /* end-of-information code */
81 #define CODE_FIRST 258 /* first free code entry */
82 #define CODE_MAX MAXCODE(BITS_MAX)
83 #define HSIZE 9001L /* 91% occupancy */
86 /* NB: +1024 is for compatibility with old files */
87 #define CSIZE (MAXCODE(BITS_MAX)+1024L)
89 #define CSIZE (MAXCODE(BITS_MAX)+1L)
93 * State block for each open TIFF file using LZW
94 * compression/decompression. Note that the predictor
95 * state block must be first in this data structure.
98 TIFFPredictorState predict
; /* predictor super class */
100 u_short nbits
; /* # of bits/code */
101 u_short maxcode
; /* maximum code for lzw_nbits */
102 u_short free_ent
; /* next free entry in hash table */
103 long nextdata
; /* next bits of i/o */
104 long nextbits
; /* # of valid bits in lzw_nextdata */
107 #define lzw_nbits base.nbits
108 #define lzw_maxcode base.maxcode
109 #define lzw_free_ent base.free_ent
110 #define lzw_nextdata base.nextdata
111 #define lzw_nextbits base.nextbits
114 * Decoding-specific state.
116 typedef struct code_ent
{
117 struct code_ent
*next
;
118 u_short length
; /* string len, including this token */
119 u_char value
; /* data value */
120 u_char firstchar
; /* first token of string */
123 typedef int (LINKAGEMODE
*decodeFunc
)(TIFF
*, tidata_t
, tsize_t
, tsample_t
);
127 long dec_nbitsmask
; /* lzw_nbits 1 bits, right adjusted */
128 long dec_restart
; /* restart count */
130 long dec_bitsleft
; /* available bits in raw data */
132 decodeFunc dec_decode
; /* regular or backwards compatible */
133 code_t
* dec_codep
; /* current recognized code */
134 code_t
* dec_oldcodep
; /* previously recognized code */
135 code_t
* dec_free_entp
; /* next free entry */
136 code_t
* dec_maxcodep
; /* max available entry */
137 code_t
* dec_codetab
; /* kept separate for small machines */
141 * Encoding-specific state.
143 typedef uint16 hcode_t
; /* codes fit in 16 bits */
151 int enc_oldcode
; /* last code encountered */
152 long enc_checkpoint
; /* point at which to clear table */
153 #define CHECK_GAP 10000 /* enc_ratio check interval */
154 long enc_ratio
; /* current compression ratio */
155 long enc_incount
; /* (input) data bytes encoded */
156 long enc_outcount
; /* encoded (output) bytes */
157 tidata_t enc_rawlimit
; /* bound on tif_rawdata buffer */
158 hash_t
* enc_hashtab
; /* kept separate for small machines */
161 #define LZWState(tif) ((LZWBaseState*) (tif)->tif_data)
162 #define DecoderState(tif) ((LZWDecodeState*) LZWState(tif))
163 #define EncoderState(tif) ((LZWEncodeState*) LZWState(tif))
165 static int LINKAGEMODE
LZWDecode(TIFF
*, tidata_t
, tsize_t
, tsample_t
);
167 static int LINKAGEMODE
LZWDecodeCompat(TIFF
*, tidata_t
, tsize_t
, tsample_t
);
169 static void cl_hash(LZWEncodeState
*);
177 * This check shouldn't be necessary because each
178 * strip is suppose to be terminated with CODE_EOI.
180 #define NextCode(_tif, _sp, _bp, _code, _get) { \
181 if ((_sp)->dec_bitsleft < nbits) { \
182 TIFFWarning(_tif->tif_name, \
183 "LZWDecode: Strip %d not terminated with EOI code", \
184 _tif->tif_curstrip); \
187 _get(_sp,_bp,_code); \
188 (_sp)->dec_bitsleft -= nbits; \
192 #define NextCode(tif, sp, bp, code, get) get(sp, bp, code)
196 LZWSetupDecode(TIFF
* tif
)
198 LZWDecodeState
* sp
= DecoderState(tif
);
199 static const char module[] = " LZWSetupDecode";
203 if (sp
->dec_codetab
== NULL
) {
204 sp
->dec_codetab
= (code_t
*)_TIFFmalloc(CSIZE
*sizeof (code_t
));
205 if (sp
->dec_codetab
== NULL
) {
206 TIFFError(module, "No space for LZW code table");
210 * Pre-load the table.
212 for (code
= 255; code
>= 0; code
--) {
213 sp
->dec_codetab
[code
].value
= code
;
214 sp
->dec_codetab
[code
].firstchar
= code
;
215 sp
->dec_codetab
[code
].length
= 1;
216 sp
->dec_codetab
[code
].next
= NULL
;
223 * Setup state for decoding a strip.
226 LZWPreDecode(TIFF
* tif
, tsample_t s
)
228 LZWDecodeState
*sp
= DecoderState(tif
);
233 * Check for old bit-reversed codes.
235 if (tif
->tif_rawdata
[0] == 0 && (tif
->tif_rawdata
[1] & 0x1)) {
237 if (!sp
->dec_decode
) {
238 TIFFWarning(tif
->tif_name
,
239 "Old-style LZW codes, convert file");
241 * Override default decoding methods with
242 * ones that deal with the old coding.
243 * Otherwise the predictor versions set
244 * above will call the compatibility routines
245 * through the dec_decode method.
247 tif
->tif_decoderow
= LZWDecodeCompat
;
248 tif
->tif_decodestrip
= LZWDecodeCompat
;
249 tif
->tif_decodetile
= LZWDecodeCompat
;
251 * If doing horizontal differencing, must
252 * re-setup the predictor logic since we
253 * switched the basic decoder methods...
255 (*tif
->tif_setupdecode
)(tif
);
256 sp
->dec_decode
= LZWDecodeCompat
;
258 sp
->lzw_maxcode
= MAXCODE(BITS_MIN
);
259 #else /* !LZW_COMPAT */
260 if (!sp
->dec_decode
) {
261 TIFFError(tif
->tif_name
,
262 "Old-style LZW codes not supported");
263 sp
->dec_decode
= LZWDecode
;
266 #endif/* !LZW_COMPAT */
268 sp
->lzw_maxcode
= MAXCODE(BITS_MIN
)-1;
269 sp
->dec_decode
= LZWDecode
;
271 sp
->lzw_nbits
= BITS_MIN
;
272 sp
->lzw_nextbits
= 0;
273 sp
->lzw_nextdata
= 0;
276 sp
->dec_nbitsmask
= MAXCODE(BITS_MIN
);
278 sp
->dec_bitsleft
= tif
->tif_rawcc
<< 3;
280 sp
->dec_free_entp
= sp
->dec_codetab
+ CODE_FIRST
;
282 * Zero entries that are not yet filled in. We do
283 * this to guard against bogus input data that causes
284 * us to index into undefined entries. If you can
285 * come up with a way to safely bounds-check input codes
286 * while decoding then you can remove this operation.
288 _TIFFmemset(sp
->dec_free_entp
, 0, (CSIZE
-CODE_FIRST
)*sizeof (code_t
));
289 sp
->dec_oldcodep
= &sp
->dec_codetab
[-1];
290 sp
->dec_maxcodep
= &sp
->dec_codetab
[sp
->dec_nbitsmask
-1];
295 * Decode a "hunk of data".
297 #define GetNextCode(sp, bp, code) { \
298 nextdata = (nextdata<<8) | *(bp)++; \
300 if (nextbits < nbits) { \
301 nextdata = (nextdata<<8) | *(bp)++; \
304 code = (hcode_t)((nextdata >> (nextbits-nbits)) & nbitsmask); \
311 TIFFError(tif
->tif_name
,
312 "LZWDecode: Bogus encoding, loop in the code table; scanline %d",
317 LZWDecode(TIFF
* tif
, tidata_t op0
, tsize_t occ0
, tsample_t s
)
319 LZWDecodeState
*sp
= DecoderState(tif
);
320 char *op
= (char*) op0
;
321 long occ
= (long) occ0
;
326 long nbits
, nextbits
, nextdata
, nbitsmask
;
327 code_t
*codep
, *free_entp
, *maxcodep
, *oldcodep
;
332 * Restart interrupted output operation.
334 if (sp
->dec_restart
) {
337 codep
= sp
->dec_codep
;
338 residue
= codep
->length
- sp
->dec_restart
;
341 * Residue from previous decode is sufficient
342 * to satisfy decode request. Skip to the
343 * start of the decoded string, place decoded
344 * values in the output buffer, and return.
346 sp
->dec_restart
+= occ
;
349 } while (--residue
> occ
&& codep
);
353 *--tp
= codep
->value
;
355 } while (--occ
&& codep
);
360 * Residue satisfies only part of the decode request.
362 op
+= residue
, occ
-= residue
;
370 } while (--residue
&& codep
);
374 bp
= (u_char
*)tif
->tif_rawcp
;
375 nbits
= sp
->lzw_nbits
;
376 nextdata
= sp
->lzw_nextdata
;
377 nextbits
= sp
->lzw_nextbits
;
378 nbitsmask
= sp
->dec_nbitsmask
;
379 oldcodep
= sp
->dec_oldcodep
;
380 free_entp
= sp
->dec_free_entp
;
381 maxcodep
= sp
->dec_maxcodep
;
384 NextCode(tif
, sp
, bp
, code
, GetNextCode
);
385 if (code
== CODE_EOI
)
387 if (code
== CODE_CLEAR
) {
388 free_entp
= sp
->dec_codetab
+ CODE_FIRST
;
390 nbitsmask
= MAXCODE(BITS_MIN
);
391 maxcodep
= sp
->dec_codetab
+ nbitsmask
-1;
392 NextCode(tif
, sp
, bp
, code
, GetNextCode
);
393 if (code
== CODE_EOI
)
396 oldcodep
= sp
->dec_codetab
+ code
;
399 codep
= sp
->dec_codetab
+ code
;
402 * Add the new entry to the code table.
404 assert(&sp
->dec_codetab
[0] <= free_entp
&& free_entp
< &sp
->dec_codetab
[CSIZE
]);
405 free_entp
->next
= oldcodep
;
406 free_entp
->firstchar
= free_entp
->next
->firstchar
;
407 free_entp
->length
= free_entp
->next
->length
+1;
408 free_entp
->value
= (codep
< free_entp
) ?
409 codep
->firstchar
: free_entp
->firstchar
;
410 if (++free_entp
> maxcodep
) {
411 if (++nbits
> BITS_MAX
) /* should not happen */
413 nbitsmask
= MAXCODE(nbits
);
414 maxcodep
= sp
->dec_codetab
+ nbitsmask
-1;
419 * Code maps to a string, copy string
420 * value to output (written in reverse).
422 if (codep
->length
> occ
) {
424 * String is too long for decode buffer,
425 * locate portion that will fit, copy to
426 * the decode buffer, and setup restart
427 * logic for the next decoding call.
429 sp
->dec_codep
= codep
;
432 } while (codep
&& codep
->length
> occ
);
434 sp
->dec_restart
= occ
;
437 *--tp
= codep
->value
;
439 } while (--occ
&& codep
);
453 } while (codep
&& tp
> op
);
458 op
+= len
, occ
-= len
;
463 tif
->tif_rawcp
= (tidata_t
) bp
;
464 sp
->lzw_nbits
= (u_short
) nbits
;
465 sp
->lzw_nextdata
= nextdata
;
466 sp
->lzw_nextbits
= nextbits
;
467 sp
->dec_nbitsmask
= nbitsmask
;
468 sp
->dec_oldcodep
= oldcodep
;
469 sp
->dec_free_entp
= free_entp
;
470 sp
->dec_maxcodep
= maxcodep
;
473 TIFFError(tif
->tif_name
,
474 "LZWDecode: Not enough data at scanline %d (short %d bytes)",
483 * Decode a "hunk of data" for old images.
485 #define GetNextCodeCompat(sp, bp, code) { \
486 nextdata |= (u_long) *(bp)++ << nextbits; \
488 if (nextbits < nbits) { \
489 nextdata |= (u_long) *(bp)++ << nextbits; \
492 code = (hcode_t)(nextdata & nbitsmask); \
493 nextdata >>= nbits; \
497 static int LINKAGEMODE
498 LZWDecodeCompat(TIFF
* tif
, tidata_t op0
, tsize_t occ0
, tsample_t s
)
500 LZWDecodeState
*sp
= DecoderState(tif
);
501 char *op
= (char*) op0
;
502 long occ
= (long) occ0
;
506 long nextbits
, nextdata
, nbitsmask
;
507 code_t
*codep
, *free_entp
, *maxcodep
, *oldcodep
;
512 * Restart interrupted output operation.
514 if (sp
->dec_restart
) {
517 codep
= sp
->dec_codep
;
518 residue
= codep
->length
- sp
->dec_restart
;
521 * Residue from previous decode is sufficient
522 * to satisfy decode request. Skip to the
523 * start of the decoded string, place decoded
524 * values in the output buffer, and return.
526 sp
->dec_restart
+= occ
;
529 } while (--residue
> occ
);
532 *--tp
= codep
->value
;
538 * Residue satisfies only part of the decode request.
540 op
+= residue
, occ
-= residue
;
543 *--tp
= codep
->value
;
549 bp
= (u_char
*)tif
->tif_rawcp
;
550 nbits
= sp
->lzw_nbits
;
551 nextdata
= sp
->lzw_nextdata
;
552 nextbits
= sp
->lzw_nextbits
;
553 nbitsmask
= sp
->dec_nbitsmask
;
554 oldcodep
= sp
->dec_oldcodep
;
555 free_entp
= sp
->dec_free_entp
;
556 maxcodep
= sp
->dec_maxcodep
;
559 NextCode(tif
, sp
, bp
, code
, GetNextCodeCompat
);
560 if (code
== CODE_EOI
)
562 if (code
== CODE_CLEAR
) {
563 free_entp
= sp
->dec_codetab
+ CODE_FIRST
;
565 nbitsmask
= MAXCODE(BITS_MIN
);
566 maxcodep
= sp
->dec_codetab
+ nbitsmask
;
567 NextCode(tif
, sp
, bp
, code
, GetNextCodeCompat
);
568 if (code
== CODE_EOI
)
571 oldcodep
= sp
->dec_codetab
+ code
;
574 codep
= sp
->dec_codetab
+ code
;
577 * Add the new entry to the code table.
579 assert(&sp
->dec_codetab
[0] <= free_entp
&& free_entp
< &sp
->dec_codetab
[CSIZE
]);
580 free_entp
->next
= oldcodep
;
581 free_entp
->firstchar
= free_entp
->next
->firstchar
;
582 free_entp
->length
= free_entp
->next
->length
+1;
583 free_entp
->value
= (codep
< free_entp
) ?
584 codep
->firstchar
: free_entp
->firstchar
;
585 if (++free_entp
> maxcodep
) {
586 if (++nbits
> BITS_MAX
) /* should not happen */
588 nbitsmask
= MAXCODE(nbits
);
589 maxcodep
= sp
->dec_codetab
+ nbitsmask
;
594 * Code maps to a string, copy string
595 * value to output (written in reverse).
597 if (codep
->length
> occ
) {
599 * String is too long for decode buffer,
600 * locate portion that will fit, copy to
601 * the decode buffer, and setup restart
602 * logic for the next decoding call.
604 sp
->dec_codep
= codep
;
607 } while (codep
->length
> occ
);
608 sp
->dec_restart
= occ
;
611 *--tp
= codep
->value
;
616 op
+= codep
->length
, occ
-= codep
->length
;
619 *--tp
= codep
->value
;
620 } while( (codep
= codep
->next
) != NULL
);
625 tif
->tif_rawcp
= (tidata_t
) bp
;
626 sp
->lzw_nbits
= nbits
;
627 sp
->lzw_nextdata
= nextdata
;
628 sp
->lzw_nextbits
= nextbits
;
629 sp
->dec_nbitsmask
= nbitsmask
;
630 sp
->dec_oldcodep
= oldcodep
;
631 sp
->dec_free_entp
= free_entp
;
632 sp
->dec_maxcodep
= maxcodep
;
635 TIFFError(tif
->tif_name
,
636 "LZWDecodeCompat: Not enough data at scanline %d (short %d bytes)",
642 #endif /* LZW_COMPAT */
649 LZWSetupEncode(TIFF
* tif
)
651 LZWEncodeState
* sp
= EncoderState(tif
);
652 static const char module[] = "LZWSetupEncode";
655 sp
->enc_hashtab
= (hash_t
*) _TIFFmalloc(HSIZE
*sizeof (hash_t
));
656 if (sp
->enc_hashtab
== NULL
) {
657 TIFFError(module, "No space for LZW hash table");
664 * Reset encoding state at the start of a strip.
667 LZWPreEncode(TIFF
* tif
, tsample_t s
)
669 LZWEncodeState
*sp
= EncoderState(tif
);
673 sp
->lzw_nbits
= BITS_MIN
;
674 sp
->lzw_maxcode
= MAXCODE(BITS_MIN
);
675 sp
->lzw_free_ent
= CODE_FIRST
;
676 sp
->lzw_nextbits
= 0;
677 sp
->lzw_nextdata
= 0;
678 sp
->enc_checkpoint
= CHECK_GAP
;
681 sp
->enc_outcount
= 0;
683 * The 4 here insures there is space for 2 max-sized
684 * codes in LZWEncode and LZWPostDecode.
686 sp
->enc_rawlimit
= tif
->tif_rawdata
+ tif
->tif_rawdatasize
-1 - 4;
687 cl_hash(sp
); /* clear hash table */
688 sp
->enc_oldcode
= (hcode_t
) -1; /* generates CODE_CLEAR in LZWEncode */
692 #define CALCRATIO(sp, rat) { \
693 if (incount > 0x007fffff) { /* NB: shift will overflow */\
694 rat = outcount >> 8; \
695 rat = (rat == 0 ? 0x7fffffff : incount/rat); \
697 rat = (incount<<8) / outcount; \
699 #define PutNextCode(op, c) { \
700 nextdata = (nextdata << nbits) | c; \
702 *op++ = (u_char)(nextdata >> (nextbits-8)); \
704 if (nextbits >= 8) { \
705 *op++ = (u_char)(nextdata >> (nextbits-8)); \
712 * Encode a chunk of pixels.
714 * Uses an open addressing double hashing (no chaining) on the
715 * prefix code/next character combination. We do a variant of
716 * Knuth's algorithm D (vol. 3, sec. 6.4) along with G. Knott's
717 * relatively-prime secondary probe. Here, the modular division
718 * first probe is gives way to a faster exclusive-or manipulation.
719 * Also do block compression with an adaptive reset, whereby the
720 * code table is cleared when the compression ratio decreases,
721 * but after the table fills. The variable-length output codes
722 * are re-sized at this point, and a CODE_CLEAR is generated
725 static int LINKAGEMODE
726 LZWEncode(TIFF
* tif
, tidata_t bp
, tsize_t cc
, tsample_t s
)
728 register LZWEncodeState
*sp
= EncoderState(tif
);
734 long incount
, outcount
, checkpoint
;
735 long nextdata
, nextbits
;
736 int free_ent
, maxcode
, nbits
;
745 incount
= sp
->enc_incount
;
746 outcount
= sp
->enc_outcount
;
747 checkpoint
= sp
->enc_checkpoint
;
748 nextdata
= sp
->lzw_nextdata
;
749 nextbits
= sp
->lzw_nextbits
;
750 free_ent
= sp
->lzw_free_ent
;
751 maxcode
= sp
->lzw_maxcode
;
752 nbits
= sp
->lzw_nbits
;
754 limit
= sp
->enc_rawlimit
;
755 ent
= sp
->enc_oldcode
;
757 if (ent
== (hcode_t
) -1 && cc
> 0) {
759 * NB: This is safe because it can only happen
760 * at the start of a strip where we know there
761 * is space in the data buffer.
763 PutNextCode(op
, CODE_CLEAR
);
764 ent
= *bp
++; cc
--; incount
++;
767 c
= *bp
++; cc
--; incount
++;
768 fcode
= ((long)c
<< BITS_MAX
) + ent
;
769 h
= (c
<< HSHIFT
) ^ ent
; /* xor hashing */
772 * Check hash index for an overflow.
777 hp
= &sp
->enc_hashtab
[h
];
778 if (hp
->hash
== fcode
) {
784 * Primary hash failed, check secondary hash.
791 * Avoid pointer arithmetic 'cuz of
792 * wraparound problems with segments.
796 hp
= &sp
->enc_hashtab
[h
];
797 if (hp
->hash
== fcode
) {
801 } while (hp
->hash
>= 0);
804 * New entry, emit code and add to table.
807 * Verify there is space in the buffer for the code
808 * and any potential Clear code that might be emitted
809 * below. The value of limit is setup so that there
810 * are at least 4 bytes free--room for 2 codes.
813 tif
->tif_rawcc
= (tsize_t
)(op
- tif
->tif_rawdata
);
815 op
= tif
->tif_rawdata
;
817 PutNextCode(op
, ent
);
819 hp
->code
= free_ent
++;
821 if (free_ent
== CODE_MAX
-1) {
822 /* table is full, emit clear code and reset */
827 free_ent
= CODE_FIRST
;
828 PutNextCode(op
, CODE_CLEAR
);
830 maxcode
= MAXCODE(BITS_MIN
);
833 * If the next entry is going to be too big for
834 * the code size, then increase it, if possible.
836 if (free_ent
> maxcode
) {
838 assert(nbits
<= BITS_MAX
);
839 maxcode
= (int) MAXCODE(nbits
);
840 } else if (incount
>= checkpoint
) {
843 * Check compression ratio and, if things seem
844 * to be slipping, clear the hash table and
845 * reset state. The compression ratio is a
846 * 24+8-bit fractional number.
848 checkpoint
= incount
+CHECK_GAP
;
850 if (rat
<= sp
->enc_ratio
) {
855 free_ent
= CODE_FIRST
;
856 PutNextCode(op
, CODE_CLEAR
);
858 maxcode
= MAXCODE(BITS_MIN
);
868 * Restore global state.
870 sp
->enc_incount
= incount
;
871 sp
->enc_outcount
= outcount
;
872 sp
->enc_checkpoint
= checkpoint
;
873 sp
->enc_oldcode
= ent
;
874 sp
->lzw_nextdata
= nextdata
;
875 sp
->lzw_nextbits
= nextbits
;
876 sp
->lzw_free_ent
= free_ent
;
877 sp
->lzw_maxcode
= maxcode
;
878 sp
->lzw_nbits
= nbits
;
884 * Finish off an encoded strip by flushing the last
885 * string and tacking on an End Of Information code.
888 LZWPostEncode(TIFF
* tif
)
890 register LZWEncodeState
*sp
= EncoderState(tif
);
891 tidata_t op
= tif
->tif_rawcp
;
892 long nextbits
= sp
->lzw_nextbits
;
893 long nextdata
= sp
->lzw_nextdata
;
894 long outcount
= sp
->enc_outcount
;
895 int nbits
= sp
->lzw_nbits
;
897 if (op
> sp
->enc_rawlimit
) {
898 tif
->tif_rawcc
= (tsize_t
)(op
- tif
->tif_rawdata
);
900 op
= tif
->tif_rawdata
;
902 if (sp
->enc_oldcode
!= (hcode_t
) -1) {
903 PutNextCode(op
, sp
->enc_oldcode
);
904 sp
->enc_oldcode
= (hcode_t
) -1;
906 PutNextCode(op
, CODE_EOI
);
908 *op
++ = (u_char
)(nextdata
<< (8-nextbits
));
909 tif
->tif_rawcc
= (tsize_t
)(op
- tif
->tif_rawdata
);
914 * Reset encoding hash table.
917 cl_hash(LZWEncodeState
* sp
)
919 register hash_t
*hp
= &sp
->enc_hashtab
[HSIZE
-1];
920 register long i
= HSIZE
-8;
934 for (i
+= 8; i
> 0; i
--, hp
--)
939 LZWCleanup(TIFF
* tif
)
942 if (tif
->tif_mode
== O_RDONLY
) {
943 if (DecoderState(tif
)->dec_codetab
)
944 _TIFFfree(DecoderState(tif
)->dec_codetab
);
946 if (EncoderState(tif
)->enc_hashtab
)
947 _TIFFfree(EncoderState(tif
)->enc_hashtab
);
949 _TIFFfree(tif
->tif_data
);
950 tif
->tif_data
= NULL
;
955 TIFFInitLZW(TIFF
* tif
, int scheme
)
957 assert(scheme
== COMPRESSION_LZW
);
959 * Allocate state block so tag methods have storage to record values.
961 if (tif
->tif_mode
== O_RDONLY
) {
962 tif
->tif_data
= (tidata_t
) _TIFFmalloc(sizeof (LZWDecodeState
));
963 if (tif
->tif_data
== NULL
)
965 DecoderState(tif
)->dec_codetab
= NULL
;
966 DecoderState(tif
)->dec_decode
= NULL
;
968 tif
->tif_data
= (tidata_t
) _TIFFmalloc(sizeof (LZWEncodeState
));
969 if (tif
->tif_data
== NULL
)
971 EncoderState(tif
)->enc_hashtab
= NULL
;
974 * Install codec methods.
976 tif
->tif_setupdecode
= LZWSetupDecode
;
977 tif
->tif_predecode
= LZWPreDecode
;
978 tif
->tif_decoderow
= LZWDecode
;
979 tif
->tif_decodestrip
= LZWDecode
;
980 tif
->tif_decodetile
= LZWDecode
;
981 tif
->tif_setupencode
= LZWSetupEncode
;
982 tif
->tif_preencode
= LZWPreEncode
;
983 tif
->tif_postencode
= LZWPostEncode
;
984 tif
->tif_encoderow
= LZWEncode
;
985 tif
->tif_encodestrip
= LZWEncode
;
986 tif
->tif_encodetile
= LZWEncode
;
987 tif
->tif_cleanup
= LZWCleanup
;
989 * Setup predictor setup.
991 (void) TIFFPredictorInit(tif
);
994 TIFFError("TIFFInitLZW", "No space for LZW state block");
999 * Copyright (c) 1985, 1986 The Regents of the University of California.
1000 * All rights reserved.
1002 * This code is derived from software contributed to Berkeley by
1003 * James A. Woods, derived from original work by Spencer Thomas
1006 * Redistribution and use in source and binary forms are permitted
1007 * provided that the above copyright notice and this paragraph are
1008 * duplicated in all such forms and that any documentation,
1009 * advertising materials, and other materials related to such
1010 * distribution and use acknowledge that the software was developed
1011 * by the University of California, Berkeley. The name of the
1012 * University may not be used to endorse or promote products derived
1013 * from this software without specific prior written permission.
1014 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
1015 * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
1016 * WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
1018 #endif /* LZW_SUPPORT */