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 * Encoding-specific state.
111 typedef uint16 hcode_t
; /* codes fit in 16 bits */
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 (*decodeFunc
)(TIFF
*, tidata_t
, tsize_t
, tsample_t
);
128 /* Decoding specific data */
129 long dec_nbitsmask
; /* lzw_nbits 1 bits, right adjusted */
130 long dec_restart
; /* restart count */
132 long dec_bitsleft
; /* available bits in raw data */
134 decodeFunc dec_decode
; /* regular or backwards compatible */
135 code_t
* dec_codep
; /* current recognized code */
136 code_t
* dec_oldcodep
; /* previously recognized code */
137 code_t
* dec_free_entp
; /* next free entry */
138 code_t
* dec_maxcodep
; /* max available entry */
139 code_t
* dec_codetab
; /* kept separate for small machines */
142 #define LZWState(tif) ((LZWBaseState*) (tif)->tif_data)
143 #define DecoderState(tif) ((LZWCodecState*) LZWState(tif))
145 static int LZWDecode(TIFF
*, tidata_t
, tsize_t
, tsample_t
);
147 static int LZWDecodeCompat(TIFF
*, tidata_t
, tsize_t
, tsample_t
);
156 * This check shouldn't be necessary because each
157 * strip is suppose to be terminated with CODE_EOI.
159 #define NextCode(_tif, _sp, _bp, _code, _get) { \
160 if ((_sp)->dec_bitsleft < nbits) { \
161 TIFFWarning(_tif->tif_name, \
162 "LZWDecode: Strip %d not terminated with EOI code", \
163 _tif->tif_curstrip); \
166 _get(_sp,_bp,_code); \
167 (_sp)->dec_bitsleft -= nbits; \
171 #define NextCode(tif, sp, bp, code, get) get(sp, bp, code)
175 LZWSetupDecode(TIFF
* tif
)
177 LZWCodecState
* sp
= DecoderState(tif
);
178 static const char module[] = "LZWSetupDecode";
183 if (sp
->dec_codetab
== NULL
) {
184 sp
->dec_codetab
= (code_t
*)_TIFFmalloc(CSIZE
*sizeof (code_t
));
185 if (sp
->dec_codetab
== NULL
) {
186 TIFFError(module, "No space for LZW code table");
190 * Pre-load the table.
194 sp
->dec_codetab
[code
].value
= (u_char
) code
;
195 sp
->dec_codetab
[code
].firstchar
= (u_char
) code
;
196 sp
->dec_codetab
[code
].length
= 1;
197 sp
->dec_codetab
[code
].next
= NULL
;
204 * Setup state for decoding a strip.
207 LZWPreDecode(TIFF
* tif
, tsample_t s
)
209 LZWCodecState
*sp
= DecoderState(tif
);
214 * Check for old bit-reversed codes.
216 if (tif
->tif_rawdata
[0] == 0 && (tif
->tif_rawdata
[1] & 0x1)) {
218 if (!sp
->dec_decode
) {
219 TIFFWarning(tif
->tif_name
,
220 "Old-style LZW codes, convert file");
222 * Override default decoding methods with
223 * ones that deal with the old coding.
224 * Otherwise the predictor versions set
225 * above will call the compatibility routines
226 * through the dec_decode method.
228 tif
->tif_decoderow
= LZWDecodeCompat
;
229 tif
->tif_decodestrip
= LZWDecodeCompat
;
230 tif
->tif_decodetile
= LZWDecodeCompat
;
232 * If doing horizontal differencing, must
233 * re-setup the predictor logic since we
234 * switched the basic decoder methods...
236 (*tif
->tif_setupdecode
)(tif
);
237 sp
->dec_decode
= LZWDecodeCompat
;
239 sp
->lzw_maxcode
= MAXCODE(BITS_MIN
);
240 #else /* !LZW_COMPAT */
241 if (!sp
->dec_decode
) {
242 TIFFError(tif
->tif_name
,
243 "Old-style LZW codes not supported");
244 sp
->dec_decode
= LZWDecode
;
247 #endif/* !LZW_COMPAT */
249 sp
->lzw_maxcode
= MAXCODE(BITS_MIN
)-1;
250 sp
->dec_decode
= LZWDecode
;
252 sp
->lzw_nbits
= BITS_MIN
;
253 sp
->lzw_nextbits
= 0;
254 sp
->lzw_nextdata
= 0;
257 sp
->dec_nbitsmask
= MAXCODE(BITS_MIN
);
259 sp
->dec_bitsleft
= tif
->tif_rawcc
<< 3;
261 sp
->dec_free_entp
= sp
->dec_codetab
+ CODE_FIRST
;
263 * Zero entries that are not yet filled in. We do
264 * this to guard against bogus input data that causes
265 * us to index into undefined entries. If you can
266 * come up with a way to safely bounds-check input codes
267 * while decoding then you can remove this operation.
269 _TIFFmemset(sp
->dec_free_entp
, 0, (CSIZE
-CODE_FIRST
)*sizeof (code_t
));
270 sp
->dec_oldcodep
= &sp
->dec_codetab
[-1];
271 sp
->dec_maxcodep
= &sp
->dec_codetab
[sp
->dec_nbitsmask
-1];
276 * Decode a "hunk of data".
278 #define GetNextCode(sp, bp, code) { \
279 nextdata = (nextdata<<8) | *(bp)++; \
281 if (nextbits < nbits) { \
282 nextdata = (nextdata<<8) | *(bp)++; \
285 code = (hcode_t)((nextdata >> (nextbits-nbits)) & nbitsmask); \
292 TIFFError(tif
->tif_name
,
293 "LZWDecode: Bogus encoding, loop in the code table; scanline %d",
298 LZWDecode(TIFF
* tif
, tidata_t op0
, tsize_t occ0
, tsample_t s
)
300 LZWCodecState
*sp
= DecoderState(tif
);
301 char *op
= (char*) op0
;
302 long occ
= (long) occ0
;
307 long nbits
, nextbits
, nextdata
, nbitsmask
;
308 code_t
*codep
, *free_entp
, *maxcodep
, *oldcodep
;
313 * Restart interrupted output operation.
315 if (sp
->dec_restart
) {
318 codep
= sp
->dec_codep
;
319 residue
= codep
->length
- sp
->dec_restart
;
322 * Residue from previous decode is sufficient
323 * to satisfy decode request. Skip to the
324 * start of the decoded string, place decoded
325 * values in the output buffer, and return.
327 sp
->dec_restart
+= occ
;
330 } while (--residue
> occ
&& codep
);
334 *--tp
= codep
->value
;
336 } while (--occ
&& codep
);
341 * Residue satisfies only part of the decode request.
343 op
+= residue
, occ
-= residue
;
351 } while (--residue
&& codep
);
355 bp
= (u_char
*)tif
->tif_rawcp
;
356 nbits
= sp
->lzw_nbits
;
357 nextdata
= sp
->lzw_nextdata
;
358 nextbits
= sp
->lzw_nextbits
;
359 nbitsmask
= sp
->dec_nbitsmask
;
360 oldcodep
= sp
->dec_oldcodep
;
361 free_entp
= sp
->dec_free_entp
;
362 maxcodep
= sp
->dec_maxcodep
;
365 NextCode(tif
, sp
, bp
, code
, GetNextCode
);
366 if (code
== CODE_EOI
)
368 if (code
== CODE_CLEAR
) {
369 free_entp
= sp
->dec_codetab
+ CODE_FIRST
;
371 nbitsmask
= MAXCODE(BITS_MIN
);
372 maxcodep
= sp
->dec_codetab
+ nbitsmask
-1;
373 NextCode(tif
, sp
, bp
, code
, GetNextCode
);
374 if (code
== CODE_EOI
)
376 *op
++ = (char)code
, occ
--;
377 oldcodep
= sp
->dec_codetab
+ code
;
380 codep
= sp
->dec_codetab
+ code
;
383 * Add the new entry to the code table.
385 if (free_entp
< &sp
->dec_codetab
[0] ||
386 free_entp
>= &sp
->dec_codetab
[CSIZE
]) {
387 TIFFError(tif
->tif_name
,
388 "LZWDecode: Corrupted LZW table at scanline %d",
393 free_entp
->next
= oldcodep
;
394 if (free_entp
->next
< &sp
->dec_codetab
[0] ||
395 free_entp
->next
>= &sp
->dec_codetab
[CSIZE
]) {
396 TIFFError(tif
->tif_name
,
397 "LZWDecode: Corrupted LZW table at scanline %d",
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
== 0) {
418 TIFFError(tif
->tif_name
,
419 "LZWDecode: Wrong length of decoded string: "
420 "data probably corrupted at scanline %d",
424 if (codep
->length
> occ
) {
426 * String is too long for decode buffer,
427 * locate portion that will fit, copy to
428 * the decode buffer, and setup restart
429 * logic for the next decoding call.
431 sp
->dec_codep
= codep
;
434 } while (codep
&& codep
->length
> occ
);
436 sp
->dec_restart
= occ
;
439 *--tp
= codep
->value
;
441 } while (--occ
&& codep
);
455 } while (codep
&& tp
> op
);
460 op
+= len
, occ
-= len
;
462 *op
++ = (char)code
, occ
--;
465 tif
->tif_rawcp
= (tidata_t
) bp
;
466 sp
->lzw_nbits
= (u_short
) nbits
;
467 sp
->lzw_nextdata
= nextdata
;
468 sp
->lzw_nextbits
= nextbits
;
469 sp
->dec_nbitsmask
= nbitsmask
;
470 sp
->dec_oldcodep
= oldcodep
;
471 sp
->dec_free_entp
= free_entp
;
472 sp
->dec_maxcodep
= maxcodep
;
475 TIFFError(tif
->tif_name
,
476 "LZWDecode: Not enough data at scanline %d (short %d bytes)",
485 * Decode a "hunk of data" for old images.
487 #define GetNextCodeCompat(sp, bp, code) { \
488 nextdata |= (u_long) *(bp)++ << nextbits; \
490 if (nextbits < nbits) { \
491 nextdata |= (u_long) *(bp)++ << nextbits; \
494 code = (hcode_t)(nextdata & nbitsmask); \
495 nextdata >>= nbits; \
500 LZWDecodeCompat(TIFF
* tif
, tidata_t op0
, tsize_t occ0
, tsample_t s
)
502 LZWCodecState
*sp
= DecoderState(tif
);
503 char *op
= (char*) op0
;
504 long occ
= (long) occ0
;
508 long nextbits
, nextdata
, nbitsmask
;
509 code_t
*codep
, *free_entp
, *maxcodep
, *oldcodep
;
514 * Restart interrupted output operation.
516 if (sp
->dec_restart
) {
519 codep
= sp
->dec_codep
;
520 residue
= codep
->length
- sp
->dec_restart
;
523 * Residue from previous decode is sufficient
524 * to satisfy decode request. Skip to the
525 * start of the decoded string, place decoded
526 * values in the output buffer, and return.
528 sp
->dec_restart
+= occ
;
531 } while (--residue
> occ
);
534 *--tp
= codep
->value
;
540 * Residue satisfies only part of the decode request.
542 op
+= residue
, occ
-= residue
;
545 *--tp
= codep
->value
;
551 bp
= (u_char
*)tif
->tif_rawcp
;
552 nbits
= sp
->lzw_nbits
;
553 nextdata
= sp
->lzw_nextdata
;
554 nextbits
= sp
->lzw_nextbits
;
555 nbitsmask
= sp
->dec_nbitsmask
;
556 oldcodep
= sp
->dec_oldcodep
;
557 free_entp
= sp
->dec_free_entp
;
558 maxcodep
= sp
->dec_maxcodep
;
561 NextCode(tif
, sp
, bp
, code
, GetNextCodeCompat
);
562 if (code
== CODE_EOI
)
564 if (code
== CODE_CLEAR
) {
565 free_entp
= sp
->dec_codetab
+ CODE_FIRST
;
567 nbitsmask
= MAXCODE(BITS_MIN
);
568 maxcodep
= sp
->dec_codetab
+ nbitsmask
;
569 NextCode(tif
, sp
, bp
, code
, GetNextCodeCompat
);
570 if (code
== CODE_EOI
)
572 *op
++ = (char) code
, occ
--;
573 oldcodep
= sp
->dec_codetab
+ code
;
576 codep
= sp
->dec_codetab
+ code
;
579 * Add the new entry to the code table.
581 if (free_entp
< &sp
->dec_codetab
[0] ||
582 free_entp
>= &sp
->dec_codetab
[CSIZE
]) {
583 TIFFError(tif
->tif_name
,
584 "LZWDecodeCompat: Corrupted LZW table at scanline %d",
589 free_entp
->next
= oldcodep
;
590 if (free_entp
->next
< &sp
->dec_codetab
[0] ||
591 free_entp
->next
>= &sp
->dec_codetab
[CSIZE
]) {
592 TIFFError(tif
->tif_name
,
593 "LZWDecodeCompat: Corrupted LZW table at scanline %d",
597 free_entp
->firstchar
= free_entp
->next
->firstchar
;
598 free_entp
->length
= free_entp
->next
->length
+1;
599 free_entp
->value
= (codep
< free_entp
) ?
600 codep
->firstchar
: free_entp
->firstchar
;
601 if (++free_entp
> maxcodep
) {
602 if (++nbits
> BITS_MAX
) /* should not happen */
604 nbitsmask
= MAXCODE(nbits
);
605 maxcodep
= sp
->dec_codetab
+ nbitsmask
;
610 * Code maps to a string, copy string
611 * value to output (written in reverse).
613 if(codep
->length
== 0) {
614 TIFFError(tif
->tif_name
,
615 "LZWDecodeCompat: Wrong length of decoded "
616 "string: data probably corrupted at scanline %d",
620 if (codep
->length
> occ
) {
622 * String is too long for decode buffer,
623 * locate portion that will fit, copy to
624 * the decode buffer, and setup restart
625 * logic for the next decoding call.
627 sp
->dec_codep
= codep
;
630 } while (codep
->length
> occ
);
631 sp
->dec_restart
= occ
;
634 *--tp
= codep
->value
;
639 op
+= codep
->length
, occ
-= codep
->length
;
642 *--tp
= codep
->value
;
643 } while( (codep
= codep
->next
) != NULL
);
645 *op
++ = (char) code
, occ
--;
648 tif
->tif_rawcp
= (tidata_t
) bp
;
649 sp
->lzw_nbits
= (u_short
) nbits
;
650 sp
->lzw_nextdata
= nextdata
;
651 sp
->lzw_nextbits
= nextbits
;
652 sp
->dec_nbitsmask
= nbitsmask
;
653 sp
->dec_oldcodep
= oldcodep
;
654 sp
->dec_free_entp
= free_entp
;
655 sp
->dec_maxcodep
= maxcodep
;
658 TIFFError(tif
->tif_name
,
659 "LZWDecodeCompat: Not enough data at scanline %d (short %d bytes)",
665 #endif /* LZW_COMPAT */
670 LZWCleanup(TIFF
* tif
)
673 if (DecoderState(tif
)->dec_codetab
)
674 _TIFFfree(DecoderState(tif
)->dec_codetab
);
675 _TIFFfree(tif
->tif_data
);
676 tif
->tif_data
= NULL
;
681 LZWSetupEncode(TIFF
* tif
)
683 TIFFError(tif
->tif_name
,
684 "LZW compression is not available to due to Unisys patent enforcement");
689 TIFFInitLZW(TIFF
* tif
, int scheme
)
691 assert(scheme
== COMPRESSION_LZW
);
694 * Allocate state block so tag methods have storage to record values.
696 tif
->tif_data
= (tidata_t
) _TIFFmalloc(sizeof (LZWCodecState
));
697 if (tif
->tif_data
== NULL
)
699 DecoderState(tif
)->dec_codetab
= NULL
;
700 DecoderState(tif
)->dec_decode
= NULL
;
703 * Install codec methods.
705 tif
->tif_setupencode
= LZWSetupEncode
;
706 tif
->tif_setupdecode
= LZWSetupDecode
;
707 tif
->tif_predecode
= LZWPreDecode
;
708 tif
->tif_decoderow
= LZWDecode
;
709 tif
->tif_decodestrip
= LZWDecode
;
710 tif
->tif_decodetile
= LZWDecode
;
711 tif
->tif_cleanup
= LZWCleanup
;
714 * Setup predictor setup.
716 (void) TIFFPredictorInit(tif
);
721 TIFFError("TIFFInitLZW", "No space for LZW state block");
726 * Copyright (c) 1985, 1986 The Regents of the University of California.
727 * All rights reserved.
729 * This code is derived from software contributed to Berkeley by
730 * James A. Woods, derived from original work by Spencer Thomas
733 * Redistribution and use in source and binary forms are permitted
734 * provided that the above copyright notice and this paragraph are
735 * duplicated in all such forms and that any documentation,
736 * advertising materials, and other materials related to such
737 * distribution and use acknowledge that the software was developed
738 * by the University of California, Berkeley. The name of the
739 * University may not be used to endorse or promote products derived
740 * from this software without specific prior written permission.
741 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
742 * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
743 * WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
745 #endif /* LZW_SUPPORT */