1
/* $Id: tif_lzw.c,v 1.45 2011-04-02 20:54:09 bfriesen Exp $ */
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"
44
* NB: The 5.0 spec describes a different algorithm than Aldus
45
* implements. Specifically, Aldus does code length transitions
46
* one code earlier than should be done (for real LZW).
47
* Earlier versions of this library implemented the correct
48
* LZW algorithm, but emitted codes in a bit order opposite
49
* to the TIFF spec. Thus, to maintain compatibility w/ Aldus
50
* we interpret MSB-LSB ordered codes to be images written w/
51
* old versions of this library, but otherwise adhere to the
52
* Aldus "off by one" algorithm.
54
* Future revisions to the TIFF spec are expected to "clarify this issue".
56
#define LZW_COMPAT /* include backwards compatibility code */
58
* Each strip of data is supposed to be terminated by a CODE_EOI.
59
* If the following #define is included, the decoder will also
60
* check for end-of-strip w/o seeing this code. This makes the
61
* library more robust, but also slower.
63
#define LZW_CHECKEOS /* include checks for strips w/o EOI code */
65
#define MAXCODE(n) ((1L<<(n))-1)
67
* The TIFF spec specifies that encoded bit
68
* strings range from 9 to 12 bits.
70
#define BITS_MIN 9 /* start with 9 bits */
71
#define BITS_MAX 12 /* max of 12 bit strings */
72
/* predefined codes */
73
#define CODE_CLEAR 256 /* code to clear string table */
74
#define CODE_EOI 257 /* end-of-information code */
75
#define CODE_FIRST 258 /* first free code entry */
76
#define CODE_MAX MAXCODE(BITS_MAX)
77
#define HSIZE 9001L /* 91% occupancy */
80
/* NB: +1024 is for compatibility with old files */
81
#define CSIZE (MAXCODE(BITS_MAX)+1024L)
83
#define CSIZE (MAXCODE(BITS_MAX)+1L)
87
* State block for each open TIFF file using LZW
88
* compression/decompression. Note that the predictor
89
* state block must be first in this data structure.
92
TIFFPredictorState predict; /* predictor super class */
94
unsigned short nbits; /* # of bits/code */
95
unsigned short maxcode; /* maximum code for lzw_nbits */
96
unsigned short free_ent; /* next free entry in hash table */
97
long nextdata; /* next bits of i/o */
98
long nextbits; /* # of valid bits in lzw_nextdata */
100
int rw_mode; /* preserve rw_mode from init */
103
#define lzw_nbits base.nbits
104
#define lzw_maxcode base.maxcode
105
#define lzw_free_ent base.free_ent
106
#define lzw_nextdata base.nextdata
107
#define lzw_nextbits base.nextbits
110
* Encoding-specific state.
112
typedef uint16 hcode_t; /* codes fit in 16 bits */
119
* Decoding-specific state.
121
typedef struct code_ent {
122
struct code_ent *next;
123
unsigned short length; /* string len, including this token */
124
unsigned char value; /* data value */
125
unsigned char firstchar; /* first token of string */
128
typedef int (*decodeFunc)(TIFF*, uint8*, tmsize_t, uint16);
133
/* Decoding specific data */
134
long dec_nbitsmask; /* lzw_nbits 1 bits, right adjusted */
135
long dec_restart; /* restart count */
137
uint64 dec_bitsleft; /* available bits in raw data */
139
decodeFunc dec_decode; /* regular or backwards compatible */
140
code_t* dec_codep; /* current recognized code */
141
code_t* dec_oldcodep; /* previously recognized code */
142
code_t* dec_free_entp; /* next free entry */
143
code_t* dec_maxcodep; /* max available entry */
144
code_t* dec_codetab; /* kept separate for small machines */
146
/* Encoding specific data */
147
int enc_oldcode; /* last code encountered */
148
long enc_checkpoint; /* point at which to clear table */
149
#define CHECK_GAP 10000 /* enc_ratio check interval */
150
long enc_ratio; /* current compression ratio */
151
long enc_incount; /* (input) data bytes encoded */
152
long enc_outcount; /* encoded (output) bytes */
153
uint8* enc_rawlimit; /* bound on tif_rawdata buffer */
154
hash_t* enc_hashtab; /* kept separate for small machines */
157
#define LZWState(tif) ((LZWBaseState*) (tif)->tif_data)
158
#define DecoderState(tif) ((LZWCodecState*) LZWState(tif))
159
#define EncoderState(tif) ((LZWCodecState*) LZWState(tif))
161
static int LZWDecode(TIFF* tif, uint8* op0, tmsize_t occ0, uint16 s);
163
static int LZWDecodeCompat(TIFF* tif, uint8* op0, tmsize_t occ0, uint16 s);
165
static void cl_hash(LZWCodecState*);
173
* This check shouldn't be necessary because each
174
* strip is suppose to be terminated with CODE_EOI.
176
#define NextCode(_tif, _sp, _bp, _code, _get) { \
177
if ((_sp)->dec_bitsleft < (uint64)nbits) { \
178
TIFFWarningExt(_tif->tif_clientdata, module, \
179
"LZWDecode: Strip %d not terminated with EOI code", \
180
_tif->tif_curstrip); \
183
_get(_sp,_bp,_code); \
184
(_sp)->dec_bitsleft -= nbits; \
188
#define NextCode(tif, sp, bp, code, get) get(sp, bp, code)
192
LZWFixupTags(TIFF* tif)
199
LZWSetupDecode(TIFF* tif)
201
static const char module[] = "LZWSetupDecode";
202
LZWCodecState* sp = DecoderState(tif);
208
* Allocate state block so tag methods have storage to record
211
tif->tif_data = (uint8*) _TIFFmalloc(sizeof(LZWCodecState));
212
if (tif->tif_data == NULL)
214
TIFFErrorExt(tif->tif_clientdata, module, "No space for LZW state block");
218
DecoderState(tif)->dec_codetab = NULL;
219
DecoderState(tif)->dec_decode = NULL;
222
* Setup predictor setup.
224
(void) TIFFPredictorInit(tif);
226
sp = DecoderState(tif);
231
if (sp->dec_codetab == NULL) {
232
sp->dec_codetab = (code_t*)_TIFFmalloc(CSIZE*sizeof (code_t));
233
if (sp->dec_codetab == NULL) {
234
TIFFErrorExt(tif->tif_clientdata, module,
235
"No space for LZW code table");
239
* Pre-load the table.
243
sp->dec_codetab[code].value = code;
244
sp->dec_codetab[code].firstchar = code;
245
sp->dec_codetab[code].length = 1;
246
sp->dec_codetab[code].next = NULL;
249
* Zero-out the unused entries
251
_TIFFmemset(&sp->dec_codetab[CODE_CLEAR], 0,
252
(CODE_FIRST - CODE_CLEAR) * sizeof (code_t));
258
* Setup state for decoding a strip.
261
LZWPreDecode(TIFF* tif, uint16 s)
263
static const char module[] = "LZWPreDecode";
264
LZWCodecState *sp = DecoderState(tif);
268
if( sp->dec_codetab == NULL )
270
tif->tif_setupdecode( tif );
274
* Check for old bit-reversed codes.
276
if (tif->tif_rawdata[0] == 0 && (tif->tif_rawdata[1] & 0x1)) {
278
if (!sp->dec_decode) {
279
TIFFWarningExt(tif->tif_clientdata, module,
280
"Old-style LZW codes, convert file");
282
* Override default decoding methods with
283
* ones that deal with the old coding.
284
* Otherwise the predictor versions set
285
* above will call the compatibility routines
286
* through the dec_decode method.
288
tif->tif_decoderow = LZWDecodeCompat;
289
tif->tif_decodestrip = LZWDecodeCompat;
290
tif->tif_decodetile = LZWDecodeCompat;
292
* If doing horizontal differencing, must
293
* re-setup the predictor logic since we
294
* switched the basic decoder methods...
296
(*tif->tif_setupdecode)(tif);
297
sp->dec_decode = LZWDecodeCompat;
299
sp->lzw_maxcode = MAXCODE(BITS_MIN);
300
#else /* !LZW_COMPAT */
301
if (!sp->dec_decode) {
302
TIFFErrorExt(tif->tif_clientdata, module,
303
"Old-style LZW codes not supported");
304
sp->dec_decode = LZWDecode;
307
#endif/* !LZW_COMPAT */
309
sp->lzw_maxcode = MAXCODE(BITS_MIN)-1;
310
sp->dec_decode = LZWDecode;
312
sp->lzw_nbits = BITS_MIN;
313
sp->lzw_nextbits = 0;
314
sp->lzw_nextdata = 0;
317
sp->dec_nbitsmask = MAXCODE(BITS_MIN);
319
sp->dec_bitsleft = ((uint64)tif->tif_rawcc) << 3;
321
sp->dec_free_entp = sp->dec_codetab + CODE_FIRST;
323
* Zero entries that are not yet filled in. We do
324
* this to guard against bogus input data that causes
325
* us to index into undefined entries. If you can
326
* come up with a way to safely bounds-check input codes
327
* while decoding then you can remove this operation.
329
_TIFFmemset(sp->dec_free_entp, 0, (CSIZE-CODE_FIRST)*sizeof (code_t));
330
sp->dec_oldcodep = &sp->dec_codetab[-1];
331
sp->dec_maxcodep = &sp->dec_codetab[sp->dec_nbitsmask-1];
336
* Decode a "hunk of data".
338
#define GetNextCode(sp, bp, code) { \
339
nextdata = (nextdata<<8) | *(bp)++; \
341
if (nextbits < nbits) { \
342
nextdata = (nextdata<<8) | *(bp)++; \
345
code = (hcode_t)((nextdata >> (nextbits-nbits)) & nbitsmask); \
350
codeLoop(TIFF* tif, const char* module)
352
TIFFErrorExt(tif->tif_clientdata, module,
353
"Bogus encoding, loop in the code table; scanline %d",
358
LZWDecode(TIFF* tif, uint8* op0, tmsize_t occ0, uint16 s)
360
static const char module[] = "LZWDecode";
361
LZWCodecState *sp = DecoderState(tif);
362
char *op = (char*) op0;
363
long occ = (long) occ0;
368
long nbits, nextbits, nextdata, nbitsmask;
369
code_t *codep, *free_entp, *maxcodep, *oldcodep;
373
assert(sp->dec_codetab != NULL);
376
Fail if value does not fit in long.
378
if ((tmsize_t) occ != occ0)
381
* Restart interrupted output operation.
383
if (sp->dec_restart) {
386
codep = sp->dec_codep;
387
residue = codep->length - sp->dec_restart;
390
* Residue from previous decode is sufficient
391
* to satisfy decode request. Skip to the
392
* start of the decoded string, place decoded
393
* values in the output buffer, and return.
395
sp->dec_restart += occ;
398
} while (--residue > occ && codep);
402
*--tp = codep->value;
404
} while (--occ && codep);
409
* Residue satisfies only part of the decode request.
411
op += residue, occ -= residue;
419
} while (--residue && codep);
423
bp = (unsigned char *)tif->tif_rawcp;
424
nbits = sp->lzw_nbits;
425
nextdata = sp->lzw_nextdata;
426
nextbits = sp->lzw_nextbits;
427
nbitsmask = sp->dec_nbitsmask;
428
oldcodep = sp->dec_oldcodep;
429
free_entp = sp->dec_free_entp;
430
maxcodep = sp->dec_maxcodep;
433
NextCode(tif, sp, bp, code, GetNextCode);
434
if (code == CODE_EOI)
436
if (code == CODE_CLEAR) {
437
free_entp = sp->dec_codetab + CODE_FIRST;
438
_TIFFmemset(free_entp, 0,
439
(CSIZE - CODE_FIRST) * sizeof (code_t));
441
nbitsmask = MAXCODE(BITS_MIN);
442
maxcodep = sp->dec_codetab + nbitsmask-1;
443
NextCode(tif, sp, bp, code, GetNextCode);
444
if (code == CODE_EOI)
446
if (code >= CODE_CLEAR) {
447
TIFFErrorExt(tif->tif_clientdata, tif->tif_name,
448
"LZWDecode: Corrupted LZW table at scanline %d",
452
*op++ = (char)code, occ--;
453
oldcodep = sp->dec_codetab + code;
456
codep = sp->dec_codetab + code;
459
* Add the new entry to the code table.
461
if (free_entp < &sp->dec_codetab[0] ||
462
free_entp >= &sp->dec_codetab[CSIZE]) {
463
TIFFErrorExt(tif->tif_clientdata, module,
464
"Corrupted LZW table at scanline %d",
469
free_entp->next = oldcodep;
470
if (free_entp->next < &sp->dec_codetab[0] ||
471
free_entp->next >= &sp->dec_codetab[CSIZE]) {
472
TIFFErrorExt(tif->tif_clientdata, module,
473
"Corrupted LZW table at scanline %d",
477
free_entp->firstchar = free_entp->next->firstchar;
478
free_entp->length = free_entp->next->length+1;
479
free_entp->value = (codep < free_entp) ?
480
codep->firstchar : free_entp->firstchar;
481
if (++free_entp > maxcodep) {
482
if (++nbits > BITS_MAX) /* should not happen */
484
nbitsmask = MAXCODE(nbits);
485
maxcodep = sp->dec_codetab + nbitsmask-1;
490
* Code maps to a string, copy string
491
* value to output (written in reverse).
493
if(codep->length == 0) {
494
TIFFErrorExt(tif->tif_clientdata, module,
495
"Wrong length of decoded string: "
496
"data probably corrupted at scanline %d",
500
if (codep->length > occ) {
502
* String is too long for decode buffer,
503
* locate portion that will fit, copy to
504
* the decode buffer, and setup restart
505
* logic for the next decoding call.
507
sp->dec_codep = codep;
510
} while (codep && codep->length > occ);
512
sp->dec_restart = (long)occ;
515
*--tp = codep->value;
517
} while (--occ && codep);
519
codeLoop(tif, module);
531
} while (codep && tp > op);
533
codeLoop(tif, module);
537
op += len, occ -= len;
539
*op++ = (char)code, occ--;
542
tif->tif_rawcp = (uint8*) bp;
543
sp->lzw_nbits = (unsigned short) nbits;
544
sp->lzw_nextdata = nextdata;
545
sp->lzw_nextbits = nextbits;
546
sp->dec_nbitsmask = nbitsmask;
547
sp->dec_oldcodep = oldcodep;
548
sp->dec_free_entp = free_entp;
549
sp->dec_maxcodep = maxcodep;
552
#if defined(__WIN32__) && (defined(_MSC_VER) || defined(__MINGW32__))
553
TIFFErrorExt(tif->tif_clientdata, module,
554
"Not enough data at scanline %d (short %I64d bytes)",
555
tif->tif_row, (unsigned __int64) occ);
557
TIFFErrorExt(tif->tif_clientdata, module,
558
"Not enough data at scanline %d (short %llu bytes)",
559
tif->tif_row, (unsigned long long) occ);
568
* Decode a "hunk of data" for old images.
570
#define GetNextCodeCompat(sp, bp, code) { \
571
nextdata |= (unsigned long) *(bp)++ << nextbits; \
573
if (nextbits < nbits) { \
574
nextdata |= (unsigned long) *(bp)++ << nextbits;\
577
code = (hcode_t)(nextdata & nbitsmask); \
578
nextdata >>= nbits; \
583
LZWDecodeCompat(TIFF* tif, uint8* op0, tmsize_t occ0, uint16 s)
585
static const char module[] = "LZWDecodeCompat";
586
LZWCodecState *sp = DecoderState(tif);
587
char *op = (char*) op0;
588
long occ = (long) occ0;
592
long nextbits, nextdata, nbitsmask;
593
code_t *codep, *free_entp, *maxcodep, *oldcodep;
599
Fail if value does not fit in long.
601
if ((tmsize_t) occ != occ0)
605
* Restart interrupted output operation.
607
if (sp->dec_restart) {
610
codep = sp->dec_codep;
611
residue = codep->length - sp->dec_restart;
614
* Residue from previous decode is sufficient
615
* to satisfy decode request. Skip to the
616
* start of the decoded string, place decoded
617
* values in the output buffer, and return.
619
sp->dec_restart += occ;
622
} while (--residue > occ);
625
*--tp = codep->value;
631
* Residue satisfies only part of the decode request.
633
op += residue, occ -= residue;
636
*--tp = codep->value;
642
bp = (unsigned char *)tif->tif_rawcp;
643
nbits = sp->lzw_nbits;
644
nextdata = sp->lzw_nextdata;
645
nextbits = sp->lzw_nextbits;
646
nbitsmask = sp->dec_nbitsmask;
647
oldcodep = sp->dec_oldcodep;
648
free_entp = sp->dec_free_entp;
649
maxcodep = sp->dec_maxcodep;
652
NextCode(tif, sp, bp, code, GetNextCodeCompat);
653
if (code == CODE_EOI)
655
if (code == CODE_CLEAR) {
656
free_entp = sp->dec_codetab + CODE_FIRST;
657
_TIFFmemset(free_entp, 0,
658
(CSIZE - CODE_FIRST) * sizeof (code_t));
660
nbitsmask = MAXCODE(BITS_MIN);
661
maxcodep = sp->dec_codetab + nbitsmask;
662
NextCode(tif, sp, bp, code, GetNextCodeCompat);
663
if (code == CODE_EOI)
665
if (code >= CODE_CLEAR) {
666
TIFFErrorExt(tif->tif_clientdata, tif->tif_name,
667
"LZWDecode: Corrupted LZW table at scanline %d",
672
oldcodep = sp->dec_codetab + code;
675
codep = sp->dec_codetab + code;
678
* Add the new entry to the code table.
680
if (free_entp < &sp->dec_codetab[0] ||
681
free_entp >= &sp->dec_codetab[CSIZE]) {
682
TIFFErrorExt(tif->tif_clientdata, module,
683
"Corrupted LZW table at scanline %d", tif->tif_row);
687
free_entp->next = oldcodep;
688
if (free_entp->next < &sp->dec_codetab[0] ||
689
free_entp->next >= &sp->dec_codetab[CSIZE]) {
690
TIFFErrorExt(tif->tif_clientdata, module,
691
"Corrupted LZW table at scanline %d", tif->tif_row);
694
free_entp->firstchar = free_entp->next->firstchar;
695
free_entp->length = free_entp->next->length+1;
696
free_entp->value = (codep < free_entp) ?
697
codep->firstchar : free_entp->firstchar;
698
if (++free_entp > maxcodep) {
699
if (++nbits > BITS_MAX) /* should not happen */
701
nbitsmask = MAXCODE(nbits);
702
maxcodep = sp->dec_codetab + nbitsmask;
707
* Code maps to a string, copy string
708
* value to output (written in reverse).
710
if(codep->length == 0) {
711
TIFFErrorExt(tif->tif_clientdata, module,
712
"Wrong length of decoded "
713
"string: data probably corrupted at scanline %d",
717
if (codep->length > occ) {
719
* String is too long for decode buffer,
720
* locate portion that will fit, copy to
721
* the decode buffer, and setup restart
722
* logic for the next decoding call.
724
sp->dec_codep = codep;
727
} while (codep->length > occ);
728
sp->dec_restart = occ;
731
*--tp = codep->value;
736
assert(occ >= codep->length);
737
op += codep->length, occ -= codep->length;
740
*--tp = codep->value;
741
} while( (codep = codep->next) != NULL );
746
tif->tif_rawcp = (uint8*) bp;
747
sp->lzw_nbits = nbits;
748
sp->lzw_nextdata = nextdata;
749
sp->lzw_nextbits = nextbits;
750
sp->dec_nbitsmask = nbitsmask;
751
sp->dec_oldcodep = oldcodep;
752
sp->dec_free_entp = free_entp;
753
sp->dec_maxcodep = maxcodep;
756
#if defined(__WIN32__) && (defined(_MSC_VER) || defined(__MINGW32__))
757
TIFFErrorExt(tif->tif_clientdata, module,
758
"Not enough data at scanline %d (short %I64d bytes)",
759
tif->tif_row, (unsigned __int64) occ);
761
TIFFErrorExt(tif->tif_clientdata, module,
762
"Not enough data at scanline %d (short %llu bytes)",
763
tif->tif_row, (unsigned long long) occ);
769
#endif /* LZW_COMPAT */
776
LZWSetupEncode(TIFF* tif)
778
static const char module[] = "LZWSetupEncode";
779
LZWCodecState* sp = EncoderState(tif);
782
sp->enc_hashtab = (hash_t*) _TIFFmalloc(HSIZE*sizeof (hash_t));
783
if (sp->enc_hashtab == NULL) {
784
TIFFErrorExt(tif->tif_clientdata, module,
785
"No space for LZW hash table");
792
* Reset encoding state at the start of a strip.
795
LZWPreEncode(TIFF* tif, uint16 s)
797
LZWCodecState *sp = EncoderState(tif);
802
if( sp->enc_hashtab == NULL )
804
tif->tif_setupencode( tif );
807
sp->lzw_nbits = BITS_MIN;
808
sp->lzw_maxcode = MAXCODE(BITS_MIN);
809
sp->lzw_free_ent = CODE_FIRST;
810
sp->lzw_nextbits = 0;
811
sp->lzw_nextdata = 0;
812
sp->enc_checkpoint = CHECK_GAP;
815
sp->enc_outcount = 0;
817
* The 4 here insures there is space for 2 max-sized
818
* codes in LZWEncode and LZWPostDecode.
820
sp->enc_rawlimit = tif->tif_rawdata + tif->tif_rawdatasize-1 - 4;
821
cl_hash(sp); /* clear hash table */
822
sp->enc_oldcode = (hcode_t) -1; /* generates CODE_CLEAR in LZWEncode */
826
#define CALCRATIO(sp, rat) { \
827
if (incount > 0x007fffff) { /* NB: shift will overflow */\
828
rat = outcount >> 8; \
829
rat = (rat == 0 ? 0x7fffffff : incount/rat); \
831
rat = (incount<<8) / outcount; \
833
#define PutNextCode(op, c) { \
834
nextdata = (nextdata << nbits) | c; \
836
*op++ = (unsigned char)(nextdata >> (nextbits-8)); \
838
if (nextbits >= 8) { \
839
*op++ = (unsigned char)(nextdata >> (nextbits-8)); \
846
* Encode a chunk of pixels.
848
* Uses an open addressing double hashing (no chaining) on the
849
* prefix code/next character combination. We do a variant of
850
* Knuth's algorithm D (vol. 3, sec. 6.4) along with G. Knott's
851
* relatively-prime secondary probe. Here, the modular division
852
* first probe is gives way to a faster exclusive-or manipulation.
853
* Also do block compression with an adaptive reset, whereby the
854
* code table is cleared when the compression ratio decreases,
855
* but after the table fills. The variable-length output codes
856
* are re-sized at this point, and a CODE_CLEAR is generated
860
LZWEncode(TIFF* tif, uint8* bp, tmsize_t cc, uint16 s)
862
register LZWCodecState *sp = EncoderState(tif);
868
long incount, outcount, checkpoint;
869
long nextdata, nextbits;
870
int free_ent, maxcode, nbits;
878
assert(sp->enc_hashtab != NULL);
883
incount = sp->enc_incount;
884
outcount = sp->enc_outcount;
885
checkpoint = sp->enc_checkpoint;
886
nextdata = sp->lzw_nextdata;
887
nextbits = sp->lzw_nextbits;
888
free_ent = sp->lzw_free_ent;
889
maxcode = sp->lzw_maxcode;
890
nbits = sp->lzw_nbits;
892
limit = sp->enc_rawlimit;
893
ent = sp->enc_oldcode;
895
if (ent == (hcode_t) -1 && cc > 0) {
897
* NB: This is safe because it can only happen
898
* at the start of a strip where we know there
899
* is space in the data buffer.
901
PutNextCode(op, CODE_CLEAR);
902
ent = *bp++; cc--; incount++;
905
c = *bp++; cc--; incount++;
906
fcode = ((long)c << BITS_MAX) + ent;
907
h = (c << HSHIFT) ^ ent; /* xor hashing */
910
* Check hash index for an overflow.
915
hp = &sp->enc_hashtab[h];
916
if (hp->hash == fcode) {
922
* Primary hash failed, check secondary hash.
929
* Avoid pointer arithmetic 'cuz of
930
* wraparound problems with segments.
934
hp = &sp->enc_hashtab[h];
935
if (hp->hash == fcode) {
939
} while (hp->hash >= 0);
942
* New entry, emit code and add to table.
945
* Verify there is space in the buffer for the code
946
* and any potential Clear code that might be emitted
947
* below. The value of limit is setup so that there
948
* are at least 4 bytes free--room for 2 codes.
951
tif->tif_rawcc = (tmsize_t)(op - tif->tif_rawdata);
953
op = tif->tif_rawdata;
955
PutNextCode(op, ent);
957
hp->code = free_ent++;
959
if (free_ent == CODE_MAX-1) {
960
/* table is full, emit clear code and reset */
965
free_ent = CODE_FIRST;
966
PutNextCode(op, CODE_CLEAR);
968
maxcode = MAXCODE(BITS_MIN);
971
* If the next entry is going to be too big for
972
* the code size, then increase it, if possible.
974
if (free_ent > maxcode) {
976
assert(nbits <= BITS_MAX);
977
maxcode = (int) MAXCODE(nbits);
978
} else if (incount >= checkpoint) {
981
* Check compression ratio and, if things seem
982
* to be slipping, clear the hash table and
983
* reset state. The compression ratio is a
984
* 24+8-bit fractional number.
986
checkpoint = incount+CHECK_GAP;
988
if (rat <= sp->enc_ratio) {
993
free_ent = CODE_FIRST;
994
PutNextCode(op, CODE_CLEAR);
996
maxcode = MAXCODE(BITS_MIN);
1006
* Restore global state.
1008
sp->enc_incount = incount;
1009
sp->enc_outcount = outcount;
1010
sp->enc_checkpoint = checkpoint;
1011
sp->enc_oldcode = ent;
1012
sp->lzw_nextdata = nextdata;
1013
sp->lzw_nextbits = nextbits;
1014
sp->lzw_free_ent = free_ent;
1015
sp->lzw_maxcode = maxcode;
1016
sp->lzw_nbits = nbits;
1017
tif->tif_rawcp = op;
1022
* Finish off an encoded strip by flushing the last
1023
* string and tacking on an End Of Information code.
1026
LZWPostEncode(TIFF* tif)
1028
register LZWCodecState *sp = EncoderState(tif);
1029
uint8* op = tif->tif_rawcp;
1030
long nextbits = sp->lzw_nextbits;
1031
long nextdata = sp->lzw_nextdata;
1032
long outcount = sp->enc_outcount;
1033
int nbits = sp->lzw_nbits;
1035
if (op > sp->enc_rawlimit) {
1036
tif->tif_rawcc = (tmsize_t)(op - tif->tif_rawdata);
1037
TIFFFlushData1(tif);
1038
op = tif->tif_rawdata;
1040
if (sp->enc_oldcode != (hcode_t) -1) {
1041
PutNextCode(op, sp->enc_oldcode);
1042
sp->enc_oldcode = (hcode_t) -1;
1044
PutNextCode(op, CODE_EOI);
1046
*op++ = (unsigned char)(nextdata << (8-nextbits));
1047
tif->tif_rawcc = (tmsize_t)(op - tif->tif_rawdata);
1052
* Reset encoding hash table.
1055
cl_hash(LZWCodecState* sp)
1057
register hash_t *hp = &sp->enc_hashtab[HSIZE-1];
1058
register long i = HSIZE-8;
1072
for (i += 8; i > 0; i--, hp--)
1077
LZWCleanup(TIFF* tif)
1079
(void)TIFFPredictorCleanup(tif);
1081
assert(tif->tif_data != 0);
1083
if (DecoderState(tif)->dec_codetab)
1084
_TIFFfree(DecoderState(tif)->dec_codetab);
1086
if (EncoderState(tif)->enc_hashtab)
1087
_TIFFfree(EncoderState(tif)->enc_hashtab);
1089
_TIFFfree(tif->tif_data);
1090
tif->tif_data = NULL;
1092
_TIFFSetDefaultCompressionState(tif);
1096
TIFFInitLZW(TIFF* tif, int scheme)
1098
static const char module[] = "TIFFInitLZW";
1099
assert(scheme == COMPRESSION_LZW);
1101
* Allocate state block so tag methods have storage to record values.
1103
tif->tif_data = (uint8*) _TIFFmalloc(sizeof (LZWCodecState));
1104
if (tif->tif_data == NULL)
1106
DecoderState(tif)->dec_codetab = NULL;
1107
DecoderState(tif)->dec_decode = NULL;
1108
EncoderState(tif)->enc_hashtab = NULL;
1109
LZWState(tif)->rw_mode = tif->tif_mode;
1112
* Install codec methods.
1114
tif->tif_fixuptags = LZWFixupTags;
1115
tif->tif_setupdecode = LZWSetupDecode;
1116
tif->tif_predecode = LZWPreDecode;
1117
tif->tif_decoderow = LZWDecode;
1118
tif->tif_decodestrip = LZWDecode;
1119
tif->tif_decodetile = LZWDecode;
1120
tif->tif_setupencode = LZWSetupEncode;
1121
tif->tif_preencode = LZWPreEncode;
1122
tif->tif_postencode = LZWPostEncode;
1123
tif->tif_encoderow = LZWEncode;
1124
tif->tif_encodestrip = LZWEncode;
1125
tif->tif_encodetile = LZWEncode;
1126
tif->tif_cleanup = LZWCleanup;
1128
* Setup predictor setup.
1130
(void) TIFFPredictorInit(tif);
1133
TIFFErrorExt(tif->tif_clientdata, module,
1134
"No space for LZW state block");
1139
* Copyright (c) 1985, 1986 The Regents of the University of California.
1140
* All rights reserved.
1142
* This code is derived from software contributed to Berkeley by
1143
* James A. Woods, derived from original work by Spencer Thomas
1146
* Redistribution and use in source and binary forms are permitted
1147
* provided that the above copyright notice and this paragraph are
1148
* duplicated in all such forms and that any documentation,
1149
* advertising materials, and other materials related to such
1150
* distribution and use acknowledge that the software was developed
1151
* by the University of California, Berkeley. The name of the
1152
* University may not be used to endorse or promote products derived
1153
* from this software without specific prior written permission.
1154
* THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
1155
* IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
1156
* WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
1158
#endif /* LZW_SUPPORT */
1160
/* vim: set ts=8 sts=8 sw=8 noet: */