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 #include "tif_predict.h"
45 * NB: The 5.0 spec describes a different algorithm than Aldus
46 * implements. Specifically, Aldus does code length transitions
47 * one code earlier than should be done (for real LZW).
48 * Earlier versions of this library implemented the correct
49 * LZW algorithm, but emitted codes in a bit order opposite
50 * to the TIFF spec. Thus, to maintain compatibility w/ Aldus
51 * we interpret MSB-LSB ordered codes to be images written w/
52 * old versions of this library, but otherwise adhere to the
53 * Aldus "off by one" algorithm.
55 * Future revisions to the TIFF spec are expected to "clarify this issue".
57 #define LZW_COMPAT /* include backwards compatibility code */
59 * Each strip of data is supposed to be terminated by a CODE_EOI.
60 * If the following #define is included, the decoder will also
61 * check for end-of-strip w/o seeing this code. This makes the
62 * library more robust, but also slower.
64 #define LZW_CHECKEOS /* include checks for strips w/o EOI code */
66 #define MAXCODE(n) ((1L<<(n))-1)
68 * The TIFF spec specifies that encoded bit
69 * strings range from 9 to 12 bits.
71 #define BITS_MIN 9 /* start with 9 bits */
72 #define BITS_MAX 12 /* max of 12 bit strings */
73 /* predefined codes */
74 #define CODE_CLEAR 256 /* code to clear string table */
75 #define CODE_EOI 257 /* end-of-information code */
76 #define CODE_FIRST 258 /* first free code entry */
77 #define CODE_MAX MAXCODE(BITS_MAX)
78 #define HSIZE 9001L /* 91% occupancy */
81 /* NB: +1024 is for compatibility with old files */
82 #define CSIZE (MAXCODE(BITS_MAX)+1024L)
84 #define CSIZE (MAXCODE(BITS_MAX)+1L)
88 * State block for each open TIFF file using LZW
89 * compression/decompression. Note that the predictor
90 * state block must be first in this data structure.
93 TIFFPredictorState predict
; /* predictor super class */
95 u_short nbits
; /* # of bits/code */
96 u_short maxcode
; /* maximum code for lzw_nbits */
97 u_short free_ent
; /* next free entry in hash table */
98 long nextdata
; /* next bits of i/o */
99 long nextbits
; /* # of valid bits in lzw_nextdata */
102 #define lzw_nbits base.nbits
103 #define lzw_maxcode base.maxcode
104 #define lzw_free_ent base.free_ent
105 #define lzw_nextdata base.nextdata
106 #define lzw_nextbits base.nextbits
109 * Decoding-specific state.
111 typedef struct code_ent
{
112 struct code_ent
*next
;
113 u_short length
; /* string len, including this token */
114 u_char value
; /* data value */
115 u_char firstchar
; /* first token of string */
118 typedef int (*decodeFunc
)(TIFF
*, tidata_t
, tsize_t
, tsample_t
);
122 long dec_nbitsmask
; /* lzw_nbits 1 bits, right adjusted */
123 long dec_restart
; /* restart count */
125 long dec_bitsleft
; /* available bits in raw data */
127 decodeFunc dec_decode
; /* regular or backwards compatible */
128 code_t
* dec_codep
; /* current recognized code */
129 code_t
* dec_oldcodep
; /* previously recognized code */
130 code_t
* dec_free_entp
; /* next free entry */
131 code_t
* dec_maxcodep
; /* max available entry */
132 code_t
* dec_codetab
; /* kept separate for small machines */
136 * Encoding-specific state.
138 typedef uint16 hcode_t
; /* codes fit in 16 bits */
146 int enc_oldcode
; /* last code encountered */
147 long enc_checkpoint
; /* point at which to clear table */
148 #define CHECK_GAP 10000 /* enc_ratio check interval */
149 long enc_ratio
; /* current compression ratio */
150 long enc_incount
; /* (input) data bytes encoded */
151 long enc_outcount
; /* encoded (output) bytes */
152 tidata_t enc_rawlimit
; /* bound on tif_rawdata buffer */
153 hash_t
* enc_hashtab
; /* kept separate for small machines */
156 #define LZWState(tif) ((LZWBaseState*) (tif)->tif_data)
157 #define DecoderState(tif) ((LZWDecodeState*) LZWState(tif))
158 #define EncoderState(tif) ((LZWEncodeState*) LZWState(tif))
160 static int LZWDecode(TIFF
*, tidata_t
, tsize_t
, tsample_t
);
162 static int LZWDecodeCompat(TIFF
*, tidata_t
, tsize_t
, tsample_t
);
164 static void cl_hash(LZWEncodeState
*);
172 * This check shouldn't be necessary because each
173 * strip is suppose to be terminated with CODE_EOI.
175 #define NextCode(_tif, _sp, _bp, _code, _get) { \
176 if ((_sp)->dec_bitsleft < nbits) { \
177 TIFFWarning(_tif->tif_name, \
178 "LZWDecode: Strip %d not terminated with EOI code", \
179 _tif->tif_curstrip); \
182 _get(_sp,_bp,_code); \
183 (_sp)->dec_bitsleft -= nbits; \
187 #define NextCode(tif, sp, bp, code, get) get(sp, bp, code)
191 LZWSetupDecode(TIFF
* tif
)
193 LZWDecodeState
* sp
= DecoderState(tif
);
194 static const char module[] = " LZWSetupDecode";
198 if (sp
->dec_codetab
== NULL
) {
199 sp
->dec_codetab
= (code_t
*)_TIFFmalloc(CSIZE
*sizeof (code_t
));
200 if (sp
->dec_codetab
== NULL
) {
201 TIFFError(module, "No space for LZW code table");
205 * Pre-load the table.
207 for (code
= 255; code
>= 0; code
--) {
208 sp
->dec_codetab
[code
].value
= code
;
209 sp
->dec_codetab
[code
].firstchar
= code
;
210 sp
->dec_codetab
[code
].length
= 1;
211 sp
->dec_codetab
[code
].next
= NULL
;
218 * Setup state for decoding a strip.
221 LZWPreDecode(TIFF
* tif
, tsample_t s
)
223 LZWDecodeState
*sp
= DecoderState(tif
);
228 * Check for old bit-reversed codes.
230 if (tif
->tif_rawdata
[0] == 0 && (tif
->tif_rawdata
[1] & 0x1)) {
232 if (!sp
->dec_decode
) {
233 TIFFWarning(tif
->tif_name
,
234 "Old-style LZW codes, convert file");
236 * Override default decoding methods with
237 * ones that deal with the old coding.
238 * Otherwise the predictor versions set
239 * above will call the compatibility routines
240 * through the dec_decode method.
242 tif
->tif_decoderow
= LZWDecodeCompat
;
243 tif
->tif_decodestrip
= LZWDecodeCompat
;
244 tif
->tif_decodetile
= LZWDecodeCompat
;
246 * If doing horizontal differencing, must
247 * re-setup the predictor logic since we
248 * switched the basic decoder methods...
250 (*tif
->tif_setupdecode
)(tif
);
251 sp
->dec_decode
= LZWDecodeCompat
;
253 sp
->lzw_maxcode
= MAXCODE(BITS_MIN
);
254 #else /* !LZW_COMPAT */
255 if (!sp
->dec_decode
) {
256 TIFFError(tif
->tif_name
,
257 "Old-style LZW codes not supported");
258 sp
->dec_decode
= LZWDecode
;
261 #endif/* !LZW_COMPAT */
263 sp
->lzw_maxcode
= MAXCODE(BITS_MIN
)-1;
264 sp
->dec_decode
= LZWDecode
;
266 sp
->lzw_nbits
= BITS_MIN
;
267 sp
->lzw_nextbits
= 0;
268 sp
->lzw_nextdata
= 0;
271 sp
->dec_nbitsmask
= MAXCODE(BITS_MIN
);
273 sp
->dec_bitsleft
= tif
->tif_rawcc
<< 3;
275 sp
->dec_free_entp
= sp
->dec_codetab
+ CODE_FIRST
;
277 * Zero entries that are not yet filled in. We do
278 * this to guard against bogus input data that causes
279 * us to index into undefined entries. If you can
280 * come up with a way to safely bounds-check input codes
281 * while decoding then you can remove this operation.
283 _TIFFmemset(sp
->dec_free_entp
, 0, (CSIZE
-CODE_FIRST
)*sizeof (code_t
));
284 sp
->dec_oldcodep
= &sp
->dec_codetab
[-1];
285 sp
->dec_maxcodep
= &sp
->dec_codetab
[sp
->dec_nbitsmask
-1];
290 * Decode a "hunk of data".
292 #define GetNextCode(sp, bp, code) { \
293 nextdata = (nextdata<<8) | *(bp)++; \
295 if (nextbits < nbits) { \
296 nextdata = (nextdata<<8) | *(bp)++; \
299 code = (hcode_t)((nextdata >> (nextbits-nbits)) & nbitsmask); \
306 TIFFError(tif
->tif_name
,
307 "LZWDecode: Bogus encoding, loop in the code table; scanline %d",
312 LZWDecode(TIFF
* tif
, tidata_t op0
, tsize_t occ0
, tsample_t s
)
314 LZWDecodeState
*sp
= DecoderState(tif
);
315 char *op
= (char*) op0
;
316 long occ
= (long) occ0
;
321 long nbits
, nextbits
, nextdata
, nbitsmask
;
322 code_t
*codep
, *free_entp
, *maxcodep
, *oldcodep
;
327 * Restart interrupted output operation.
329 if (sp
->dec_restart
) {
332 codep
= sp
->dec_codep
;
333 residue
= codep
->length
- sp
->dec_restart
;
336 * Residue from previous decode is sufficient
337 * to satisfy decode request. Skip to the
338 * start of the decoded string, place decoded
339 * values in the output buffer, and return.
341 sp
->dec_restart
+= occ
;
344 } while (--residue
> occ
&& codep
);
348 *--tp
= codep
->value
;
350 } while (--occ
&& codep
);
355 * Residue satisfies only part of the decode request.
357 op
+= residue
, occ
-= residue
;
365 } while (--residue
&& codep
);
369 bp
= (u_char
*)tif
->tif_rawcp
;
370 nbits
= sp
->lzw_nbits
;
371 nextdata
= sp
->lzw_nextdata
;
372 nextbits
= sp
->lzw_nextbits
;
373 nbitsmask
= sp
->dec_nbitsmask
;
374 oldcodep
= sp
->dec_oldcodep
;
375 free_entp
= sp
->dec_free_entp
;
376 maxcodep
= sp
->dec_maxcodep
;
379 NextCode(tif
, sp
, bp
, code
, GetNextCode
);
380 if (code
== CODE_EOI
)
382 if (code
== CODE_CLEAR
) {
383 free_entp
= sp
->dec_codetab
+ CODE_FIRST
;
385 nbitsmask
= MAXCODE(BITS_MIN
);
386 maxcodep
= sp
->dec_codetab
+ nbitsmask
-1;
387 NextCode(tif
, sp
, bp
, code
, GetNextCode
);
388 if (code
== CODE_EOI
)
391 oldcodep
= sp
->dec_codetab
+ code
;
394 codep
= sp
->dec_codetab
+ code
;
397 * Add the new entry to the code table.
399 assert(&sp
->dec_codetab
[0] <= free_entp
&& free_entp
< &sp
->dec_codetab
[CSIZE
]);
400 free_entp
->next
= oldcodep
;
401 free_entp
->firstchar
= free_entp
->next
->firstchar
;
402 free_entp
->length
= free_entp
->next
->length
+1;
403 free_entp
->value
= (codep
< free_entp
) ?
404 codep
->firstchar
: free_entp
->firstchar
;
405 if (++free_entp
> maxcodep
) {
406 if (++nbits
> BITS_MAX
) /* should not happen */
408 nbitsmask
= MAXCODE(nbits
);
409 maxcodep
= sp
->dec_codetab
+ nbitsmask
-1;
414 * Code maps to a string, copy string
415 * value to output (written in reverse).
417 if (codep
->length
> occ
) {
419 * String is too long for decode buffer,
420 * locate portion that will fit, copy to
421 * the decode buffer, and setup restart
422 * logic for the next decoding call.
424 sp
->dec_codep
= codep
;
427 } while (codep
&& codep
->length
> occ
);
429 sp
->dec_restart
= occ
;
432 *--tp
= codep
->value
;
434 } while (--occ
&& codep
);
448 } while (codep
&& tp
> op
);
453 op
+= len
, occ
-= len
;
458 tif
->tif_rawcp
= (tidata_t
) bp
;
459 sp
->lzw_nbits
= (u_short
) nbits
;
460 sp
->lzw_nextdata
= nextdata
;
461 sp
->lzw_nextbits
= nextbits
;
462 sp
->dec_nbitsmask
= nbitsmask
;
463 sp
->dec_oldcodep
= oldcodep
;
464 sp
->dec_free_entp
= free_entp
;
465 sp
->dec_maxcodep
= maxcodep
;
468 TIFFError(tif
->tif_name
,
469 "LZWDecode: Not enough data at scanline %d (short %d bytes)",
478 * Decode a "hunk of data" for old images.
480 #define GetNextCodeCompat(sp, bp, code) { \
481 nextdata |= (u_long) *(bp)++ << nextbits; \
483 if (nextbits < nbits) { \
484 nextdata |= (u_long) *(bp)++ << nextbits; \
487 code = (hcode_t)(nextdata & nbitsmask); \
488 nextdata >>= nbits; \
493 LZWDecodeCompat(TIFF
* tif
, tidata_t op0
, tsize_t occ0
, tsample_t s
)
495 LZWDecodeState
*sp
= DecoderState(tif
);
496 char *op
= (char*) op0
;
497 long occ
= (long) occ0
;
501 long nextbits
, nextdata
, nbitsmask
;
502 code_t
*codep
, *free_entp
, *maxcodep
, *oldcodep
;
507 * Restart interrupted output operation.
509 if (sp
->dec_restart
) {
512 codep
= sp
->dec_codep
;
513 residue
= codep
->length
- sp
->dec_restart
;
516 * Residue from previous decode is sufficient
517 * to satisfy decode request. Skip to the
518 * start of the decoded string, place decoded
519 * values in the output buffer, and return.
521 sp
->dec_restart
+= occ
;
524 } while (--residue
> occ
);
527 *--tp
= codep
->value
;
533 * Residue satisfies only part of the decode request.
535 op
+= residue
, occ
-= residue
;
538 *--tp
= codep
->value
;
544 bp
= (u_char
*)tif
->tif_rawcp
;
545 nbits
= sp
->lzw_nbits
;
546 nextdata
= sp
->lzw_nextdata
;
547 nextbits
= sp
->lzw_nextbits
;
548 nbitsmask
= sp
->dec_nbitsmask
;
549 oldcodep
= sp
->dec_oldcodep
;
550 free_entp
= sp
->dec_free_entp
;
551 maxcodep
= sp
->dec_maxcodep
;
554 NextCode(tif
, sp
, bp
, code
, GetNextCodeCompat
);
555 if (code
== CODE_EOI
)
557 if (code
== CODE_CLEAR
) {
558 free_entp
= sp
->dec_codetab
+ CODE_FIRST
;
560 nbitsmask
= MAXCODE(BITS_MIN
);
561 maxcodep
= sp
->dec_codetab
+ nbitsmask
;
562 NextCode(tif
, sp
, bp
, code
, GetNextCodeCompat
);
563 if (code
== CODE_EOI
)
566 oldcodep
= sp
->dec_codetab
+ code
;
569 codep
= sp
->dec_codetab
+ code
;
572 * Add the new entry to the code table.
574 assert(&sp
->dec_codetab
[0] <= free_entp
&& free_entp
< &sp
->dec_codetab
[CSIZE
]);
575 free_entp
->next
= oldcodep
;
576 free_entp
->firstchar
= free_entp
->next
->firstchar
;
577 free_entp
->length
= free_entp
->next
->length
+1;
578 free_entp
->value
= (codep
< free_entp
) ?
579 codep
->firstchar
: free_entp
->firstchar
;
580 if (++free_entp
> maxcodep
) {
581 if (++nbits
> BITS_MAX
) /* should not happen */
583 nbitsmask
= MAXCODE(nbits
);
584 maxcodep
= sp
->dec_codetab
+ nbitsmask
;
589 * Code maps to a string, copy string
590 * value to output (written in reverse).
592 if (codep
->length
> occ
) {
594 * String is too long for decode buffer,
595 * locate portion that will fit, copy to
596 * the decode buffer, and setup restart
597 * logic for the next decoding call.
599 sp
->dec_codep
= codep
;
602 } while (codep
->length
> occ
);
603 sp
->dec_restart
= occ
;
606 *--tp
= codep
->value
;
611 op
+= codep
->length
, occ
-= codep
->length
;
614 *--tp
= codep
->value
;
615 } while( (codep
= codep
->next
) != NULL
);
620 tif
->tif_rawcp
= (tidata_t
) bp
;
621 sp
->lzw_nbits
= nbits
;
622 sp
->lzw_nextdata
= nextdata
;
623 sp
->lzw_nextbits
= nextbits
;
624 sp
->dec_nbitsmask
= nbitsmask
;
625 sp
->dec_oldcodep
= oldcodep
;
626 sp
->dec_free_entp
= free_entp
;
627 sp
->dec_maxcodep
= maxcodep
;
630 TIFFError(tif
->tif_name
,
631 "LZWDecodeCompat: Not enough data at scanline %d (short %d bytes)",
637 #endif /* LZW_COMPAT */
644 LZWSetupEncode(TIFF
* tif
)
646 LZWEncodeState
* sp
= EncoderState(tif
);
647 static const char module[] = "LZWSetupEncode";
650 sp
->enc_hashtab
= (hash_t
*) _TIFFmalloc(HSIZE
*sizeof (hash_t
));
651 if (sp
->enc_hashtab
== NULL
) {
652 TIFFError(module, "No space for LZW hash table");
659 * Reset encoding state at the start of a strip.
662 LZWPreEncode(TIFF
* tif
, tsample_t s
)
664 LZWEncodeState
*sp
= EncoderState(tif
);
668 sp
->lzw_nbits
= BITS_MIN
;
669 sp
->lzw_maxcode
= MAXCODE(BITS_MIN
);
670 sp
->lzw_free_ent
= CODE_FIRST
;
671 sp
->lzw_nextbits
= 0;
672 sp
->lzw_nextdata
= 0;
673 sp
->enc_checkpoint
= CHECK_GAP
;
676 sp
->enc_outcount
= 0;
678 * The 4 here insures there is space for 2 max-sized
679 * codes in LZWEncode and LZWPostDecode.
681 sp
->enc_rawlimit
= tif
->tif_rawdata
+ tif
->tif_rawdatasize
-1 - 4;
682 cl_hash(sp
); /* clear hash table */
683 sp
->enc_oldcode
= (hcode_t
) -1; /* generates CODE_CLEAR in LZWEncode */
687 #define CALCRATIO(sp, rat) { \
688 if (incount > 0x007fffff) { /* NB: shift will overflow */\
689 rat = outcount >> 8; \
690 rat = (rat == 0 ? 0x7fffffff : incount/rat); \
692 rat = (incount<<8) / outcount; \
694 #define PutNextCode(op, c) { \
695 nextdata = (nextdata << nbits) | c; \
697 *op++ = (u_char)(nextdata >> (nextbits-8)); \
699 if (nextbits >= 8) { \
700 *op++ = (u_char)(nextdata >> (nextbits-8)); \
707 * Encode a chunk of pixels.
709 * Uses an open addressing double hashing (no chaining) on the
710 * prefix code/next character combination. We do a variant of
711 * Knuth's algorithm D (vol. 3, sec. 6.4) along with G. Knott's
712 * relatively-prime secondary probe. Here, the modular division
713 * first probe is gives way to a faster exclusive-or manipulation.
714 * Also do block compression with an adaptive reset, whereby the
715 * code table is cleared when the compression ratio decreases,
716 * but after the table fills. The variable-length output codes
717 * are re-sized at this point, and a CODE_CLEAR is generated
721 LZWEncode(TIFF
* tif
, tidata_t bp
, tsize_t cc
, tsample_t s
)
723 register LZWEncodeState
*sp
= EncoderState(tif
);
729 long incount
, outcount
, checkpoint
;
730 long nextdata
, nextbits
;
731 int free_ent
, maxcode
, nbits
;
740 incount
= sp
->enc_incount
;
741 outcount
= sp
->enc_outcount
;
742 checkpoint
= sp
->enc_checkpoint
;
743 nextdata
= sp
->lzw_nextdata
;
744 nextbits
= sp
->lzw_nextbits
;
745 free_ent
= sp
->lzw_free_ent
;
746 maxcode
= sp
->lzw_maxcode
;
747 nbits
= sp
->lzw_nbits
;
749 limit
= sp
->enc_rawlimit
;
750 ent
= sp
->enc_oldcode
;
752 if (ent
== (hcode_t
) -1 && cc
> 0) {
754 * NB: This is safe because it can only happen
755 * at the start of a strip where we know there
756 * is space in the data buffer.
758 PutNextCode(op
, CODE_CLEAR
);
759 ent
= *bp
++; cc
--; incount
++;
762 c
= *bp
++; cc
--; incount
++;
763 fcode
= ((long)c
<< BITS_MAX
) + ent
;
764 h
= (c
<< HSHIFT
) ^ ent
; /* xor hashing */
767 * Check hash index for an overflow.
772 hp
= &sp
->enc_hashtab
[h
];
773 if (hp
->hash
== fcode
) {
779 * Primary hash failed, check secondary hash.
786 * Avoid pointer arithmetic 'cuz of
787 * wraparound problems with segments.
791 hp
= &sp
->enc_hashtab
[h
];
792 if (hp
->hash
== fcode
) {
796 } while (hp
->hash
>= 0);
799 * New entry, emit code and add to table.
802 * Verify there is space in the buffer for the code
803 * and any potential Clear code that might be emitted
804 * below. The value of limit is setup so that there
805 * are at least 4 bytes free--room for 2 codes.
808 tif
->tif_rawcc
= (tsize_t
)(op
- tif
->tif_rawdata
);
810 op
= tif
->tif_rawdata
;
812 PutNextCode(op
, ent
);
814 hp
->code
= free_ent
++;
816 if (free_ent
== CODE_MAX
-1) {
817 /* table is full, emit clear code and reset */
822 free_ent
= CODE_FIRST
;
823 PutNextCode(op
, CODE_CLEAR
);
825 maxcode
= MAXCODE(BITS_MIN
);
828 * If the next entry is going to be too big for
829 * the code size, then increase it, if possible.
831 if (free_ent
> maxcode
) {
833 assert(nbits
<= BITS_MAX
);
834 maxcode
= (int) MAXCODE(nbits
);
835 } else if (incount
>= checkpoint
) {
838 * Check compression ratio and, if things seem
839 * to be slipping, clear the hash table and
840 * reset state. The compression ratio is a
841 * 24+8-bit fractional number.
843 checkpoint
= incount
+CHECK_GAP
;
845 if (rat
<= sp
->enc_ratio
) {
850 free_ent
= CODE_FIRST
;
851 PutNextCode(op
, CODE_CLEAR
);
853 maxcode
= MAXCODE(BITS_MIN
);
863 * Restore global state.
865 sp
->enc_incount
= incount
;
866 sp
->enc_outcount
= outcount
;
867 sp
->enc_checkpoint
= checkpoint
;
868 sp
->enc_oldcode
= ent
;
869 sp
->lzw_nextdata
= nextdata
;
870 sp
->lzw_nextbits
= nextbits
;
871 sp
->lzw_free_ent
= free_ent
;
872 sp
->lzw_maxcode
= maxcode
;
873 sp
->lzw_nbits
= nbits
;
879 * Finish off an encoded strip by flushing the last
880 * string and tacking on an End Of Information code.
883 LZWPostEncode(TIFF
* tif
)
885 register LZWEncodeState
*sp
= EncoderState(tif
);
886 tidata_t op
= tif
->tif_rawcp
;
887 long nextbits
= sp
->lzw_nextbits
;
888 long nextdata
= sp
->lzw_nextdata
;
889 long outcount
= sp
->enc_outcount
;
890 int nbits
= sp
->lzw_nbits
;
892 if (op
> sp
->enc_rawlimit
) {
893 tif
->tif_rawcc
= (tsize_t
)(op
- tif
->tif_rawdata
);
895 op
= tif
->tif_rawdata
;
897 if (sp
->enc_oldcode
!= (hcode_t
) -1) {
898 PutNextCode(op
, sp
->enc_oldcode
);
899 sp
->enc_oldcode
= (hcode_t
) -1;
901 PutNextCode(op
, CODE_EOI
);
903 *op
++ = (u_char
)(nextdata
<< (8-nextbits
));
904 tif
->tif_rawcc
= (tsize_t
)(op
- tif
->tif_rawdata
);
909 * Reset encoding hash table.
912 cl_hash(LZWEncodeState
* sp
)
914 register hash_t
*hp
= &sp
->enc_hashtab
[HSIZE
-1];
915 register long i
= HSIZE
-8;
929 for (i
+= 8; i
> 0; i
--, hp
--)
934 LZWCleanup(TIFF
* tif
)
937 if (tif
->tif_mode
== O_RDONLY
) {
938 if (DecoderState(tif
)->dec_codetab
)
939 _TIFFfree(DecoderState(tif
)->dec_codetab
);
941 if (EncoderState(tif
)->enc_hashtab
)
942 _TIFFfree(EncoderState(tif
)->enc_hashtab
);
944 _TIFFfree(tif
->tif_data
);
945 tif
->tif_data
= NULL
;
950 TIFFInitLZW(TIFF
* tif
, int scheme
)
952 assert(scheme
== COMPRESSION_LZW
);
954 * Allocate state block so tag methods have storage to record values.
956 if (tif
->tif_mode
== O_RDONLY
) {
957 tif
->tif_data
= (tidata_t
) _TIFFmalloc(sizeof (LZWDecodeState
));
958 if (tif
->tif_data
== NULL
)
960 DecoderState(tif
)->dec_codetab
= NULL
;
961 DecoderState(tif
)->dec_decode
= NULL
;
963 tif
->tif_data
= (tidata_t
) _TIFFmalloc(sizeof (LZWEncodeState
));
964 if (tif
->tif_data
== NULL
)
966 EncoderState(tif
)->enc_hashtab
= NULL
;
969 * Install codec methods.
971 tif
->tif_setupdecode
= LZWSetupDecode
;
972 tif
->tif_predecode
= LZWPreDecode
;
973 tif
->tif_decoderow
= LZWDecode
;
974 tif
->tif_decodestrip
= LZWDecode
;
975 tif
->tif_decodetile
= LZWDecode
;
976 tif
->tif_setupencode
= LZWSetupEncode
;
977 tif
->tif_preencode
= LZWPreEncode
;
978 tif
->tif_postencode
= LZWPostEncode
;
979 tif
->tif_encoderow
= LZWEncode
;
980 tif
->tif_encodestrip
= LZWEncode
;
981 tif
->tif_encodetile
= LZWEncode
;
982 tif
->tif_cleanup
= LZWCleanup
;
984 * Setup predictor setup.
986 (void) TIFFPredictorInit(tif
);
989 TIFFError("TIFFInitLZW", "No space for LZW state block");
994 * Copyright (c) 1985, 1986 The Regents of the University of California.
995 * All rights reserved.
997 * This code is derived from software contributed to Berkeley by
998 * James A. Woods, derived from original work by Spencer Thomas
1001 * Redistribution and use in source and binary forms are permitted
1002 * provided that the above copyright notice and this paragraph are
1003 * duplicated in all such forms and that any documentation,
1004 * advertising materials, and other materials related to such
1005 * distribution and use acknowledge that the software was developed
1006 * by the University of California, Berkeley. The name of the
1007 * University may not be used to endorse or promote products derived
1008 * from this software without specific prior written permission.
1009 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
1010 * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
1011 * WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
1013 #endif /* LZW_SUPPORT */