3 * Copyright (c) 1988-1997 Sam Leffler
4 * Copyright (c) 1991-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
30 * Rev 5.0 Lempel-Ziv & Welch Compression Support
32 * This code is derived from the compress program whose code is
33 * derived from software contributed to Berkeley by James A. Woods,
34 * derived from original work by Spencer Thomas and Joseph Orost.
36 * The original Berkeley copyright notice appears below in its entirety.
38 #include "tif_predict.h"
43 * NB: The 5.0 spec describes a different algorithm than Aldus
44 * implements. Specifically, Aldus does code length transitions
45 * one code earlier than should be done (for real LZW).
46 * Earlier versions of this library implemented the correct
47 * LZW algorithm, but emitted codes in a bit order opposite
48 * to the TIFF spec. Thus, to maintain compatibility w/ Aldus
49 * we interpret MSB-LSB ordered codes to be images written w/
50 * old versions of this library, but otherwise adhere to the
51 * Aldus "off by one" algorithm.
53 * Future revisions to the TIFF spec are expected to "clarify this issue".
55 #define LZW_COMPAT /* include backwards compatibility code */
57 * Each strip of data is supposed to be terminated by a CODE_EOI.
58 * If the following #define is included, the decoder will also
59 * check for end-of-strip w/o seeing this code. This makes the
60 * library more robust, but also slower.
62 #define LZW_CHECKEOS /* include checks for strips w/o EOI code */
64 #define MAXCODE(n) ((1L<<(n))-1)
66 * The TIFF spec specifies that encoded bit
67 * strings range from 9 to 12 bits.
69 #define BITS_MIN 9 /* start with 9 bits */
70 #define BITS_MAX 12 /* max of 12 bit strings */
71 /* predefined codes */
72 #define CODE_CLEAR 256 /* code to clear string table */
73 #define CODE_EOI 257 /* end-of-information code */
74 #define CODE_FIRST 258 /* first free code entry */
75 #define CODE_MAX MAXCODE(BITS_MAX)
76 #define HSIZE 9001L /* 91% occupancy */
79 /* NB: +1024 is for compatibility with old files */
80 #define CSIZE (MAXCODE(BITS_MAX)+1024L)
82 #define CSIZE (MAXCODE(BITS_MAX)+1L)
86 * State block for each open TIFF file using LZW
87 * compression/decompression. Note that the predictor
88 * state block must be first in this data structure.
91 TIFFPredictorState predict
; /* predictor super class */
93 unsigned short nbits
; /* # of bits/code */
94 unsigned short maxcode
; /* maximum code for lzw_nbits */
95 unsigned short free_ent
; /* next free entry in hash table */
96 long nextdata
; /* next bits of i/o */
97 long nextbits
; /* # of valid bits in lzw_nextdata */
99 int rw_mode
; /* preserve rw_mode from init */
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 */
118 * Decoding-specific state.
120 typedef struct code_ent
{
121 struct code_ent
*next
;
122 unsigned short length
; /* string len, including this token */
123 unsigned char value
; /* data value */
124 unsigned char firstchar
; /* first token of string */
127 typedef int (*decodeFunc
)(TIFF
*, uint8
*, tmsize_t
, uint16
);
132 /* Decoding specific data */
133 long dec_nbitsmask
; /* lzw_nbits 1 bits, right adjusted */
134 long dec_restart
; /* restart count */
136 uint64 dec_bitsleft
; /* available bits in raw data */
138 decodeFunc dec_decode
; /* regular or backwards compatible */
139 code_t
* dec_codep
; /* current recognized code */
140 code_t
* dec_oldcodep
; /* previously recognized code */
141 code_t
* dec_free_entp
; /* next free entry */
142 code_t
* dec_maxcodep
; /* max available entry */
143 code_t
* dec_codetab
; /* kept separate for small machines */
145 /* Encoding specific data */
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 uint8
* 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) ((LZWCodecState*) LZWState(tif))
158 #define EncoderState(tif) ((LZWCodecState*) LZWState(tif))
160 static int LZWDecode(TIFF
* tif
, uint8
* op0
, tmsize_t occ0
, uint16 s
);
162 static int LZWDecodeCompat(TIFF
* tif
, uint8
* op0
, tmsize_t occ0
, uint16 s
);
164 static void cl_hash(LZWCodecState
*);
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 < (uint64)nbits) { \
177 TIFFWarningExt(_tif->tif_clientdata, module, \
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 LZWFixupTags(TIFF
* tif
)
198 LZWSetupDecode(TIFF
* tif
)
200 static const char module[] = "LZWSetupDecode";
201 LZWCodecState
* sp
= DecoderState(tif
);
207 * Allocate state block so tag methods have storage to record
210 tif
->tif_data
= (uint8
*) _TIFFmalloc(sizeof(LZWCodecState
));
211 if (tif
->tif_data
== NULL
)
213 TIFFErrorExt(tif
->tif_clientdata
, module, "No space for LZW state block");
217 DecoderState(tif
)->dec_codetab
= NULL
;
218 DecoderState(tif
)->dec_decode
= NULL
;
221 * Setup predictor setup.
223 (void) TIFFPredictorInit(tif
);
225 sp
= DecoderState(tif
);
230 if (sp
->dec_codetab
== NULL
) {
231 sp
->dec_codetab
= (code_t
*)_TIFFmalloc(CSIZE
*sizeof (code_t
));
232 if (sp
->dec_codetab
== NULL
) {
233 TIFFErrorExt(tif
->tif_clientdata
, module,
234 "No space for LZW code table");
238 * Pre-load the table.
242 sp
->dec_codetab
[code
].value
= code
;
243 sp
->dec_codetab
[code
].firstchar
= code
;
244 sp
->dec_codetab
[code
].length
= 1;
245 sp
->dec_codetab
[code
].next
= NULL
;
248 * Zero-out the unused entries
250 _TIFFmemset(&sp
->dec_codetab
[CODE_CLEAR
], 0,
251 (CODE_FIRST
- CODE_CLEAR
) * sizeof (code_t
));
257 * Setup state for decoding a strip.
260 LZWPreDecode(TIFF
* tif
, uint16 s
)
262 static const char module[] = "LZWPreDecode";
263 LZWCodecState
*sp
= DecoderState(tif
);
267 if( sp
->dec_codetab
== NULL
)
269 tif
->tif_setupdecode( tif
);
273 * Check for old bit-reversed codes.
275 if (tif
->tif_rawdata
[0] == 0 && (tif
->tif_rawdata
[1] & 0x1)) {
277 if (!sp
->dec_decode
) {
278 TIFFWarningExt(tif
->tif_clientdata
, module,
279 "Old-style LZW codes, convert file");
281 * Override default decoding methods with
282 * ones that deal with the old coding.
283 * Otherwise the predictor versions set
284 * above will call the compatibility routines
285 * through the dec_decode method.
287 tif
->tif_decoderow
= LZWDecodeCompat
;
288 tif
->tif_decodestrip
= LZWDecodeCompat
;
289 tif
->tif_decodetile
= LZWDecodeCompat
;
291 * If doing horizontal differencing, must
292 * re-setup the predictor logic since we
293 * switched the basic decoder methods...
295 (*tif
->tif_setupdecode
)(tif
);
296 sp
->dec_decode
= LZWDecodeCompat
;
298 sp
->lzw_maxcode
= MAXCODE(BITS_MIN
);
299 #else /* !LZW_COMPAT */
300 if (!sp
->dec_decode
) {
301 TIFFErrorExt(tif
->tif_clientdata
, module,
302 "Old-style LZW codes not supported");
303 sp
->dec_decode
= LZWDecode
;
306 #endif/* !LZW_COMPAT */
308 sp
->lzw_maxcode
= MAXCODE(BITS_MIN
)-1;
309 sp
->dec_decode
= LZWDecode
;
311 sp
->lzw_nbits
= BITS_MIN
;
312 sp
->lzw_nextbits
= 0;
313 sp
->lzw_nextdata
= 0;
316 sp
->dec_nbitsmask
= MAXCODE(BITS_MIN
);
318 sp
->dec_bitsleft
= ((uint64
)tif
->tif_rawcc
) << 3;
320 sp
->dec_free_entp
= sp
->dec_codetab
+ CODE_FIRST
;
322 * Zero entries that are not yet filled in. We do
323 * this to guard against bogus input data that causes
324 * us to index into undefined entries. If you can
325 * come up with a way to safely bounds-check input codes
326 * while decoding then you can remove this operation.
328 _TIFFmemset(sp
->dec_free_entp
, 0, (CSIZE
-CODE_FIRST
)*sizeof (code_t
));
329 sp
->dec_oldcodep
= &sp
->dec_codetab
[-1];
330 sp
->dec_maxcodep
= &sp
->dec_codetab
[sp
->dec_nbitsmask
-1];
335 * Decode a "hunk of data".
337 #define GetNextCode(sp, bp, code) { \
338 nextdata = (nextdata<<8) | *(bp)++; \
340 if (nextbits < nbits) { \
341 nextdata = (nextdata<<8) | *(bp)++; \
344 code = (hcode_t)((nextdata >> (nextbits-nbits)) & nbitsmask); \
349 codeLoop(TIFF
* tif
, const char* module)
351 TIFFErrorExt(tif
->tif_clientdata
, module,
352 "Bogus encoding, loop in the code table; scanline %d",
357 LZWDecode(TIFF
* tif
, uint8
* op0
, tmsize_t occ0
, uint16 s
)
359 static const char module[] = "LZWDecode";
360 LZWCodecState
*sp
= DecoderState(tif
);
361 char *op
= (char*) op0
;
362 long occ
= (long) occ0
;
367 long nbits
, nextbits
, nextdata
, nbitsmask
;
368 code_t
*codep
, *free_entp
, *maxcodep
, *oldcodep
;
372 assert(sp
->dec_codetab
!= NULL
);
375 Fail if value does not fit in long.
377 if ((tmsize_t
) occ
!= occ0
)
380 * Restart interrupted output operation.
382 if (sp
->dec_restart
) {
385 codep
= sp
->dec_codep
;
386 residue
= codep
->length
- sp
->dec_restart
;
389 * Residue from previous decode is sufficient
390 * to satisfy decode request. Skip to the
391 * start of the decoded string, place decoded
392 * values in the output buffer, and return.
394 sp
->dec_restart
+= occ
;
397 } while (--residue
> occ
&& codep
);
401 *--tp
= codep
->value
;
403 } while (--occ
&& codep
);
408 * Residue satisfies only part of the decode request.
410 op
+= residue
, occ
-= residue
;
418 } while (--residue
&& codep
);
422 bp
= (unsigned char *)tif
->tif_rawcp
;
423 nbits
= sp
->lzw_nbits
;
424 nextdata
= sp
->lzw_nextdata
;
425 nextbits
= sp
->lzw_nextbits
;
426 nbitsmask
= sp
->dec_nbitsmask
;
427 oldcodep
= sp
->dec_oldcodep
;
428 free_entp
= sp
->dec_free_entp
;
429 maxcodep
= sp
->dec_maxcodep
;
432 NextCode(tif
, sp
, bp
, code
, GetNextCode
);
433 if (code
== CODE_EOI
)
435 if (code
== CODE_CLEAR
) {
436 free_entp
= sp
->dec_codetab
+ CODE_FIRST
;
437 _TIFFmemset(free_entp
, 0,
438 (CSIZE
- CODE_FIRST
) * sizeof (code_t
));
440 nbitsmask
= MAXCODE(BITS_MIN
);
441 maxcodep
= sp
->dec_codetab
+ nbitsmask
-1;
442 NextCode(tif
, sp
, bp
, code
, GetNextCode
);
443 if (code
== CODE_EOI
)
445 if (code
>= CODE_CLEAR
) {
446 TIFFErrorExt(tif
->tif_clientdata
, tif
->tif_name
,
447 "LZWDecode: Corrupted LZW table at scanline %d",
451 *op
++ = (char)code
, occ
--;
452 oldcodep
= sp
->dec_codetab
+ code
;
455 codep
= sp
->dec_codetab
+ code
;
458 * Add the new entry to the code table.
460 if (free_entp
< &sp
->dec_codetab
[0] ||
461 free_entp
>= &sp
->dec_codetab
[CSIZE
]) {
462 TIFFErrorExt(tif
->tif_clientdata
, module,
463 "Corrupted LZW table at scanline %d",
468 free_entp
->next
= oldcodep
;
469 if (free_entp
->next
< &sp
->dec_codetab
[0] ||
470 free_entp
->next
>= &sp
->dec_codetab
[CSIZE
]) {
471 TIFFErrorExt(tif
->tif_clientdata
, module,
472 "Corrupted LZW table at scanline %d",
476 free_entp
->firstchar
= free_entp
->next
->firstchar
;
477 free_entp
->length
= free_entp
->next
->length
+1;
478 free_entp
->value
= (codep
< free_entp
) ?
479 codep
->firstchar
: free_entp
->firstchar
;
480 if (++free_entp
> maxcodep
) {
481 if (++nbits
> BITS_MAX
) /* should not happen */
483 nbitsmask
= MAXCODE(nbits
);
484 maxcodep
= sp
->dec_codetab
+ nbitsmask
-1;
489 * Code maps to a string, copy string
490 * value to output (written in reverse).
492 if(codep
->length
== 0) {
493 TIFFErrorExt(tif
->tif_clientdata
, module,
494 "Wrong length of decoded string: "
495 "data probably corrupted at scanline %d",
499 if (codep
->length
> occ
) {
501 * String is too long for decode buffer,
502 * locate portion that will fit, copy to
503 * the decode buffer, and setup restart
504 * logic for the next decoding call.
506 sp
->dec_codep
= codep
;
509 } while (codep
&& codep
->length
> occ
);
511 sp
->dec_restart
= (long)occ
;
514 *--tp
= codep
->value
;
516 } while (--occ
&& codep
);
518 codeLoop(tif
, module);
530 } while (codep
&& tp
> op
);
532 codeLoop(tif
, module);
536 op
+= len
, occ
-= len
;
538 *op
++ = (char)code
, occ
--;
541 tif
->tif_rawcp
= (uint8
*) bp
;
542 sp
->lzw_nbits
= (unsigned short) nbits
;
543 sp
->lzw_nextdata
= nextdata
;
544 sp
->lzw_nextbits
= nextbits
;
545 sp
->dec_nbitsmask
= nbitsmask
;
546 sp
->dec_oldcodep
= oldcodep
;
547 sp
->dec_free_entp
= free_entp
;
548 sp
->dec_maxcodep
= maxcodep
;
551 #if defined(__WIN32__) && (defined(_MSC_VER) || defined(__MINGW32__))
552 TIFFErrorExt(tif
->tif_clientdata
, module,
553 "Not enough data at scanline %d (short %I64d bytes)",
554 tif
->tif_row
, (unsigned __int64
) occ
);
556 TIFFErrorExt(tif
->tif_clientdata
, module,
557 "Not enough data at scanline %d (short %llu bytes)",
558 tif
->tif_row
, (unsigned long long) occ
);
567 * Decode a "hunk of data" for old images.
569 #define GetNextCodeCompat(sp, bp, code) { \
570 nextdata |= (unsigned long) *(bp)++ << nextbits; \
572 if (nextbits < nbits) { \
573 nextdata |= (unsigned long) *(bp)++ << nextbits;\
576 code = (hcode_t)(nextdata & nbitsmask); \
577 nextdata >>= nbits; \
582 LZWDecodeCompat(TIFF
* tif
, uint8
* op0
, tmsize_t occ0
, uint16 s
)
584 static const char module[] = "LZWDecodeCompat";
585 LZWCodecState
*sp
= DecoderState(tif
);
586 char *op
= (char*) op0
;
587 long occ
= (long) occ0
;
591 long nextbits
, nextdata
, nbitsmask
;
592 code_t
*codep
, *free_entp
, *maxcodep
, *oldcodep
;
598 Fail if value does not fit in long.
600 if ((tmsize_t
) occ
!= occ0
)
604 * Restart interrupted output operation.
606 if (sp
->dec_restart
) {
609 codep
= sp
->dec_codep
;
610 residue
= codep
->length
- sp
->dec_restart
;
613 * Residue from previous decode is sufficient
614 * to satisfy decode request. Skip to the
615 * start of the decoded string, place decoded
616 * values in the output buffer, and return.
618 sp
->dec_restart
+= occ
;
621 } while (--residue
> occ
);
624 *--tp
= codep
->value
;
630 * Residue satisfies only part of the decode request.
632 op
+= residue
, occ
-= residue
;
635 *--tp
= codep
->value
;
641 bp
= (unsigned char *)tif
->tif_rawcp
;
642 nbits
= sp
->lzw_nbits
;
643 nextdata
= sp
->lzw_nextdata
;
644 nextbits
= sp
->lzw_nextbits
;
645 nbitsmask
= sp
->dec_nbitsmask
;
646 oldcodep
= sp
->dec_oldcodep
;
647 free_entp
= sp
->dec_free_entp
;
648 maxcodep
= sp
->dec_maxcodep
;
651 NextCode(tif
, sp
, bp
, code
, GetNextCodeCompat
);
652 if (code
== CODE_EOI
)
654 if (code
== CODE_CLEAR
) {
655 free_entp
= sp
->dec_codetab
+ CODE_FIRST
;
656 _TIFFmemset(free_entp
, 0,
657 (CSIZE
- CODE_FIRST
) * sizeof (code_t
));
659 nbitsmask
= MAXCODE(BITS_MIN
);
660 maxcodep
= sp
->dec_codetab
+ nbitsmask
;
661 NextCode(tif
, sp
, bp
, code
, GetNextCodeCompat
);
662 if (code
== CODE_EOI
)
664 if (code
>= CODE_CLEAR
) {
665 TIFFErrorExt(tif
->tif_clientdata
, tif
->tif_name
,
666 "LZWDecode: Corrupted LZW table at scanline %d",
671 oldcodep
= sp
->dec_codetab
+ code
;
674 codep
= sp
->dec_codetab
+ code
;
677 * Add the new entry to the code table.
679 if (free_entp
< &sp
->dec_codetab
[0] ||
680 free_entp
>= &sp
->dec_codetab
[CSIZE
]) {
681 TIFFErrorExt(tif
->tif_clientdata
, module,
682 "Corrupted LZW table at scanline %d", tif
->tif_row
);
686 free_entp
->next
= oldcodep
;
687 if (free_entp
->next
< &sp
->dec_codetab
[0] ||
688 free_entp
->next
>= &sp
->dec_codetab
[CSIZE
]) {
689 TIFFErrorExt(tif
->tif_clientdata
, module,
690 "Corrupted LZW table at scanline %d", tif
->tif_row
);
693 free_entp
->firstchar
= free_entp
->next
->firstchar
;
694 free_entp
->length
= free_entp
->next
->length
+1;
695 free_entp
->value
= (codep
< free_entp
) ?
696 codep
->firstchar
: free_entp
->firstchar
;
697 if (++free_entp
> maxcodep
) {
698 if (++nbits
> BITS_MAX
) /* should not happen */
700 nbitsmask
= MAXCODE(nbits
);
701 maxcodep
= sp
->dec_codetab
+ nbitsmask
;
706 * Code maps to a string, copy string
707 * value to output (written in reverse).
709 if(codep
->length
== 0) {
710 TIFFErrorExt(tif
->tif_clientdata
, module,
711 "Wrong length of decoded "
712 "string: data probably corrupted at scanline %d",
716 if (codep
->length
> occ
) {
718 * String is too long for decode buffer,
719 * locate portion that will fit, copy to
720 * the decode buffer, and setup restart
721 * logic for the next decoding call.
723 sp
->dec_codep
= codep
;
726 } while (codep
->length
> occ
);
727 sp
->dec_restart
= occ
;
730 *--tp
= codep
->value
;
735 assert(occ
>= codep
->length
);
736 op
+= codep
->length
, occ
-= codep
->length
;
739 *--tp
= codep
->value
;
740 } while( (codep
= codep
->next
) != NULL
);
745 tif
->tif_rawcp
= (uint8
*) bp
;
746 sp
->lzw_nbits
= nbits
;
747 sp
->lzw_nextdata
= nextdata
;
748 sp
->lzw_nextbits
= nextbits
;
749 sp
->dec_nbitsmask
= nbitsmask
;
750 sp
->dec_oldcodep
= oldcodep
;
751 sp
->dec_free_entp
= free_entp
;
752 sp
->dec_maxcodep
= maxcodep
;
755 #if defined(__WIN32__) && (defined(_MSC_VER) || defined(__MINGW32__))
756 TIFFErrorExt(tif
->tif_clientdata
, module,
757 "Not enough data at scanline %d (short %I64d bytes)",
758 tif
->tif_row
, (unsigned __int64
) occ
);
760 TIFFErrorExt(tif
->tif_clientdata
, module,
761 "Not enough data at scanline %d (short %llu bytes)",
762 tif
->tif_row
, (unsigned long long) occ
);
768 #endif /* LZW_COMPAT */
775 LZWSetupEncode(TIFF
* tif
)
777 static const char module[] = "LZWSetupEncode";
778 LZWCodecState
* sp
= EncoderState(tif
);
781 sp
->enc_hashtab
= (hash_t
*) _TIFFmalloc(HSIZE
*sizeof (hash_t
));
782 if (sp
->enc_hashtab
== NULL
) {
783 TIFFErrorExt(tif
->tif_clientdata
, module,
784 "No space for LZW hash table");
791 * Reset encoding state at the start of a strip.
794 LZWPreEncode(TIFF
* tif
, uint16 s
)
796 LZWCodecState
*sp
= EncoderState(tif
);
801 if( sp
->enc_hashtab
== NULL
)
803 tif
->tif_setupencode( tif
);
806 sp
->lzw_nbits
= BITS_MIN
;
807 sp
->lzw_maxcode
= MAXCODE(BITS_MIN
);
808 sp
->lzw_free_ent
= CODE_FIRST
;
809 sp
->lzw_nextbits
= 0;
810 sp
->lzw_nextdata
= 0;
811 sp
->enc_checkpoint
= CHECK_GAP
;
814 sp
->enc_outcount
= 0;
816 * The 4 here insures there is space for 2 max-sized
817 * codes in LZWEncode and LZWPostDecode.
819 sp
->enc_rawlimit
= tif
->tif_rawdata
+ tif
->tif_rawdatasize
-1 - 4;
820 cl_hash(sp
); /* clear hash table */
821 sp
->enc_oldcode
= (hcode_t
) -1; /* generates CODE_CLEAR in LZWEncode */
825 #define CALCRATIO(sp, rat) { \
826 if (incount > 0x007fffff) { /* NB: shift will overflow */\
827 rat = outcount >> 8; \
828 rat = (rat == 0 ? 0x7fffffff : incount/rat); \
830 rat = (incount<<8) / outcount; \
832 #define PutNextCode(op, c) { \
833 nextdata = (nextdata << nbits) | c; \
835 *op++ = (unsigned char)(nextdata >> (nextbits-8)); \
837 if (nextbits >= 8) { \
838 *op++ = (unsigned char)(nextdata >> (nextbits-8)); \
845 * Encode a chunk of pixels.
847 * Uses an open addressing double hashing (no chaining) on the
848 * prefix code/next character combination. We do a variant of
849 * Knuth's algorithm D (vol. 3, sec. 6.4) along with G. Knott's
850 * relatively-prime secondary probe. Here, the modular division
851 * first probe is gives way to a faster exclusive-or manipulation.
852 * Also do block compression with an adaptive reset, whereby the
853 * code table is cleared when the compression ratio decreases,
854 * but after the table fills. The variable-length output codes
855 * are re-sized at this point, and a CODE_CLEAR is generated
859 LZWEncode(TIFF
* tif
, uint8
* bp
, tmsize_t cc
, uint16 s
)
861 register LZWCodecState
*sp
= EncoderState(tif
);
867 long incount
, outcount
, checkpoint
;
868 long nextdata
, nextbits
;
869 int free_ent
, maxcode
, nbits
;
877 assert(sp
->enc_hashtab
!= NULL
);
882 incount
= sp
->enc_incount
;
883 outcount
= sp
->enc_outcount
;
884 checkpoint
= sp
->enc_checkpoint
;
885 nextdata
= sp
->lzw_nextdata
;
886 nextbits
= sp
->lzw_nextbits
;
887 free_ent
= sp
->lzw_free_ent
;
888 maxcode
= sp
->lzw_maxcode
;
889 nbits
= sp
->lzw_nbits
;
891 limit
= sp
->enc_rawlimit
;
892 ent
= sp
->enc_oldcode
;
894 if (ent
== (hcode_t
) -1 && cc
> 0) {
896 * NB: This is safe because it can only happen
897 * at the start of a strip where we know there
898 * is space in the data buffer.
900 PutNextCode(op
, CODE_CLEAR
);
901 ent
= *bp
++; cc
--; incount
++;
904 c
= *bp
++; cc
--; incount
++;
905 fcode
= ((long)c
<< BITS_MAX
) + ent
;
906 h
= (c
<< HSHIFT
) ^ ent
; /* xor hashing */
909 * Check hash index for an overflow.
914 hp
= &sp
->enc_hashtab
[h
];
915 if (hp
->hash
== fcode
) {
921 * Primary hash failed, check secondary hash.
928 * Avoid pointer arithmetic 'cuz of
929 * wraparound problems with segments.
933 hp
= &sp
->enc_hashtab
[h
];
934 if (hp
->hash
== fcode
) {
938 } while (hp
->hash
>= 0);
941 * New entry, emit code and add to table.
944 * Verify there is space in the buffer for the code
945 * and any potential Clear code that might be emitted
946 * below. The value of limit is setup so that there
947 * are at least 4 bytes free--room for 2 codes.
950 tif
->tif_rawcc
= (tmsize_t
)(op
- tif
->tif_rawdata
);
952 op
= tif
->tif_rawdata
;
954 PutNextCode(op
, ent
);
956 hp
->code
= free_ent
++;
958 if (free_ent
== CODE_MAX
-1) {
959 /* table is full, emit clear code and reset */
964 free_ent
= CODE_FIRST
;
965 PutNextCode(op
, CODE_CLEAR
);
967 maxcode
= MAXCODE(BITS_MIN
);
970 * If the next entry is going to be too big for
971 * the code size, then increase it, if possible.
973 if (free_ent
> maxcode
) {
975 assert(nbits
<= BITS_MAX
);
976 maxcode
= (int) MAXCODE(nbits
);
977 } else if (incount
>= checkpoint
) {
980 * Check compression ratio and, if things seem
981 * to be slipping, clear the hash table and
982 * reset state. The compression ratio is a
983 * 24+8-bit fractional number.
985 checkpoint
= incount
+CHECK_GAP
;
987 if (rat
<= sp
->enc_ratio
) {
992 free_ent
= CODE_FIRST
;
993 PutNextCode(op
, CODE_CLEAR
);
995 maxcode
= MAXCODE(BITS_MIN
);
1005 * Restore global state.
1007 sp
->enc_incount
= incount
;
1008 sp
->enc_outcount
= outcount
;
1009 sp
->enc_checkpoint
= checkpoint
;
1010 sp
->enc_oldcode
= ent
;
1011 sp
->lzw_nextdata
= nextdata
;
1012 sp
->lzw_nextbits
= nextbits
;
1013 sp
->lzw_free_ent
= free_ent
;
1014 sp
->lzw_maxcode
= maxcode
;
1015 sp
->lzw_nbits
= nbits
;
1016 tif
->tif_rawcp
= op
;
1021 * Finish off an encoded strip by flushing the last
1022 * string and tacking on an End Of Information code.
1025 LZWPostEncode(TIFF
* tif
)
1027 register LZWCodecState
*sp
= EncoderState(tif
);
1028 uint8
* op
= tif
->tif_rawcp
;
1029 long nextbits
= sp
->lzw_nextbits
;
1030 long nextdata
= sp
->lzw_nextdata
;
1031 long outcount
= sp
->enc_outcount
;
1032 int nbits
= sp
->lzw_nbits
;
1034 if (op
> sp
->enc_rawlimit
) {
1035 tif
->tif_rawcc
= (tmsize_t
)(op
- tif
->tif_rawdata
);
1036 TIFFFlushData1(tif
);
1037 op
= tif
->tif_rawdata
;
1039 if (sp
->enc_oldcode
!= (hcode_t
) -1) {
1040 PutNextCode(op
, sp
->enc_oldcode
);
1041 sp
->enc_oldcode
= (hcode_t
) -1;
1043 PutNextCode(op
, CODE_EOI
);
1045 *op
++ = (unsigned char)(nextdata
<< (8-nextbits
));
1046 tif
->tif_rawcc
= (tmsize_t
)(op
- tif
->tif_rawdata
);
1051 * Reset encoding hash table.
1054 cl_hash(LZWCodecState
* sp
)
1056 register hash_t
*hp
= &sp
->enc_hashtab
[HSIZE
-1];
1057 register long i
= HSIZE
-8;
1071 for (i
+= 8; i
> 0; i
--, hp
--)
1076 LZWCleanup(TIFF
* tif
)
1078 (void)TIFFPredictorCleanup(tif
);
1080 assert(tif
->tif_data
!= 0);
1082 if (DecoderState(tif
)->dec_codetab
)
1083 _TIFFfree(DecoderState(tif
)->dec_codetab
);
1085 if (EncoderState(tif
)->enc_hashtab
)
1086 _TIFFfree(EncoderState(tif
)->enc_hashtab
);
1088 _TIFFfree(tif
->tif_data
);
1089 tif
->tif_data
= NULL
;
1091 _TIFFSetDefaultCompressionState(tif
);
1095 TIFFInitLZW(TIFF
* tif
, int scheme
)
1097 static const char module[] = "TIFFInitLZW";
1098 assert(scheme
== COMPRESSION_LZW
);
1100 * Allocate state block so tag methods have storage to record values.
1102 tif
->tif_data
= (uint8
*) _TIFFmalloc(sizeof (LZWCodecState
));
1103 if (tif
->tif_data
== NULL
)
1105 DecoderState(tif
)->dec_codetab
= NULL
;
1106 DecoderState(tif
)->dec_decode
= NULL
;
1107 EncoderState(tif
)->enc_hashtab
= NULL
;
1108 LZWState(tif
)->rw_mode
= tif
->tif_mode
;
1111 * Install codec methods.
1113 tif
->tif_fixuptags
= LZWFixupTags
;
1114 tif
->tif_setupdecode
= LZWSetupDecode
;
1115 tif
->tif_predecode
= LZWPreDecode
;
1116 tif
->tif_decoderow
= LZWDecode
;
1117 tif
->tif_decodestrip
= LZWDecode
;
1118 tif
->tif_decodetile
= LZWDecode
;
1119 tif
->tif_setupencode
= LZWSetupEncode
;
1120 tif
->tif_preencode
= LZWPreEncode
;
1121 tif
->tif_postencode
= LZWPostEncode
;
1122 tif
->tif_encoderow
= LZWEncode
;
1123 tif
->tif_encodestrip
= LZWEncode
;
1124 tif
->tif_encodetile
= LZWEncode
;
1125 tif
->tif_cleanup
= LZWCleanup
;
1127 * Setup predictor setup.
1129 (void) TIFFPredictorInit(tif
);
1132 TIFFErrorExt(tif
->tif_clientdata
, module,
1133 "No space for LZW state block");
1138 * Copyright (c) 1985, 1986 The Regents of the University of California.
1139 * All rights reserved.
1141 * This code is derived from software contributed to Berkeley by
1142 * James A. Woods, derived from original work by Spencer Thomas
1145 * Redistribution and use in source and binary forms are permitted
1146 * provided that the above copyright notice and this paragraph are
1147 * duplicated in all such forms and that any documentation,
1148 * advertising materials, and other materials related to such
1149 * distribution and use acknowledge that the software was developed
1150 * by the University of California, Berkeley. The name of the
1151 * University may not be used to endorse or promote products derived
1152 * from this software without specific prior written permission.
1153 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
1154 * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
1155 * WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
1157 #endif /* LZW_SUPPORT */
1159 /* vim: set ts=8 sts=8 sw=8 noet: */