OLD | NEW |
1 /* $Id: tif_lzw.c,v 1.45 2011-04-02 20:54:09 bfriesen Exp $ */ | 1 /* $Id: tif_lzw.c,v 1.49 2015-08-30 21:07:44 erouault Exp $ */ |
2 | 2 |
3 /* | 3 /* |
4 * Copyright (c) 1988-1997 Sam Leffler | 4 * Copyright (c) 1988-1997 Sam Leffler |
5 * Copyright (c) 1991-1997 Silicon Graphics, Inc. | 5 * Copyright (c) 1991-1997 Silicon Graphics, Inc. |
6 * | 6 * |
7 * Permission to use, copy, modify, distribute, and sell this software and | 7 * Permission to use, copy, modify, distribute, and sell this software and |
8 * its documentation for any purpose is hereby granted without fee, provided | 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 | 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 | 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 | 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 | 12 * publicity relating to the software without the specific, prior written |
13 * permission of Sam Leffler and Silicon Graphics. | 13 * permission of Sam Leffler and Silicon Graphics. |
14 * | 14 * |
15 * THE SOFTWARE IS PROVIDED "AS-IS" AND WITHOUT WARRANTY OF ANY KIND, | 15 * THE SOFTWARE IS PROVIDED "AS-IS" AND WITHOUT WARRANTY OF ANY KIND, |
16 * EXPRESS, IMPLIED OR OTHERWISE, INCLUDING WITHOUT LIMITATION, ANY | 16 * EXPRESS, IMPLIED OR OTHERWISE, INCLUDING WITHOUT LIMITATION, ANY |
17 * WARRANTY OF MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE. | 17 * WARRANTY OF MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE. |
18 * | 18 * |
19 * IN NO EVENT SHALL SAM LEFFLER OR SILICON GRAPHICS BE LIABLE FOR | 19 * IN NO EVENT SHALL SAM LEFFLER OR SILICON GRAPHICS BE LIABLE FOR |
20 * ANY SPECIAL, INCIDENTAL, INDIRECT OR CONSEQUENTIAL DAMAGES OF ANY KIND, | 20 * ANY SPECIAL, INCIDENTAL, INDIRECT OR CONSEQUENTIAL DAMAGES OF ANY KIND, |
21 * OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, | 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 | 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 | 23 * LIABILITY, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE |
24 * OF THIS SOFTWARE. | 24 * OF THIS SOFTWARE. |
25 */ | 25 */ |
| 26 |
26 #include "tiffiop.h" | 27 #include "tiffiop.h" |
27 #ifdef LZW_SUPPORT | 28 #ifdef LZW_SUPPORT |
28 /* | 29 /* |
29 * TIFF Library. | 30 * TIFF Library. |
30 * Rev 5.0 Lempel-Ziv & Welch Compression Support | 31 * Rev 5.0 Lempel-Ziv & Welch Compression Support |
31 * | 32 * |
32 * This code is derived from the compress program whose code is | 33 * 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 software contributed to Berkeley by James A. Woods, |
34 * derived from original work by Spencer Thomas and Joseph Orost. | 35 * derived from original work by Spencer Thomas and Joseph Orost. |
35 * | 36 * |
(...skipping 50 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
86 * State block for each open TIFF file using LZW | 87 * State block for each open TIFF file using LZW |
87 * compression/decompression. Note that the predictor | 88 * compression/decompression. Note that the predictor |
88 * state block must be first in this data structure. | 89 * state block must be first in this data structure. |
89 */ | 90 */ |
90 typedef struct { | 91 typedef struct { |
91 TIFFPredictorState predict; /* predictor super class */ | 92 TIFFPredictorState predict; /* predictor super class */ |
92 | 93 |
93 unsigned short nbits; /* # of bits/code */ | 94 unsigned short nbits; /* # of bits/code */ |
94 unsigned short maxcode; /* maximum code for lzw_nbits */ | 95 unsigned short maxcode; /* maximum code for lzw_nbits */ |
95 unsigned short free_ent; /* next free entry in hash table */ | 96 unsigned short free_ent; /* next free entry in hash table */ |
96 » long nextdata; /* next bits of i/o */ | 97 » unsigned long nextdata; /* next bits of i/o */ |
97 long nextbits; /* # of valid bits in lzw_nextdata */ | 98 long nextbits; /* # of valid bits in lzw_nextdata */ |
98 | 99 |
99 int rw_mode; /* preserve rw_mode from init */ | 100 int rw_mode; /* preserve rw_mode from init */ |
100 } LZWBaseState; | 101 } LZWBaseState; |
101 | 102 |
102 #define lzw_nbits base.nbits | 103 #define lzw_nbits base.nbits |
103 #define lzw_maxcode base.maxcode | 104 #define lzw_maxcode base.maxcode |
104 #define lzw_free_ent base.free_ent | 105 #define lzw_free_ent base.free_ent |
105 #define lzw_nextdata base.nextdata | 106 #define lzw_nextdata base.nextdata |
106 #define lzw_nextbits base.nextbits | 107 #define lzw_nextbits base.nextbits |
(...skipping 153 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
260 LZWPreDecode(TIFF* tif, uint16 s) | 261 LZWPreDecode(TIFF* tif, uint16 s) |
261 { | 262 { |
262 static const char module[] = "LZWPreDecode"; | 263 static const char module[] = "LZWPreDecode"; |
263 LZWCodecState *sp = DecoderState(tif); | 264 LZWCodecState *sp = DecoderState(tif); |
264 | 265 |
265 (void) s; | 266 (void) s; |
266 assert(sp != NULL); | 267 assert(sp != NULL); |
267 if( sp->dec_codetab == NULL ) | 268 if( sp->dec_codetab == NULL ) |
268 { | 269 { |
269 tif->tif_setupdecode( tif ); | 270 tif->tif_setupdecode( tif ); |
| 271 if( sp->dec_codetab == NULL ) |
| 272 return (0); |
270 } | 273 } |
271 | 274 |
272 /* | 275 /* |
273 * Check for old bit-reversed codes. | 276 * Check for old bit-reversed codes. |
274 */ | 277 */ |
275 if (tif->tif_rawdata[0] == 0 && (tif->tif_rawdata[1] & 0x1)) { | 278 if (tif->tif_rawdata[0] == 0 && (tif->tif_rawdata[1] & 0x1)) { |
276 #ifdef LZW_COMPAT | 279 #ifdef LZW_COMPAT |
277 if (!sp->dec_decode) { | 280 if (!sp->dec_decode) { |
278 TIFFWarningExt(tif->tif_clientdata, module, | 281 TIFFWarningExt(tif->tif_clientdata, module, |
279 "Old-style LZW codes, convert file"); | 282 "Old-style LZW codes, convert file"); |
(...skipping 77 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
357 LZWDecode(TIFF* tif, uint8* op0, tmsize_t occ0, uint16 s) | 360 LZWDecode(TIFF* tif, uint8* op0, tmsize_t occ0, uint16 s) |
358 { | 361 { |
359 static const char module[] = "LZWDecode"; | 362 static const char module[] = "LZWDecode"; |
360 LZWCodecState *sp = DecoderState(tif); | 363 LZWCodecState *sp = DecoderState(tif); |
361 char *op = (char*) op0; | 364 char *op = (char*) op0; |
362 long occ = (long) occ0; | 365 long occ = (long) occ0; |
363 char *tp; | 366 char *tp; |
364 unsigned char *bp; | 367 unsigned char *bp; |
365 hcode_t code; | 368 hcode_t code; |
366 int len; | 369 int len; |
367 » long nbits, nextbits, nextdata, nbitsmask; | 370 » long nbits, nextbits, nbitsmask; |
| 371 unsigned long nextdata; |
368 code_t *codep, *free_entp, *maxcodep, *oldcodep; | 372 code_t *codep, *free_entp, *maxcodep, *oldcodep; |
369 | 373 |
370 (void) s; | 374 (void) s; |
371 assert(sp != NULL); | 375 assert(sp != NULL); |
372 assert(sp->dec_codetab != NULL); | 376 assert(sp->dec_codetab != NULL); |
373 | 377 |
374 /* | 378 /* |
375 Fail if value does not fit in long. | 379 Fail if value does not fit in long. |
376 */ | 380 */ |
377 if ((tmsize_t) occ != occ0) | 381 if ((tmsize_t) occ != occ0) |
(...skipping 48 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
426 nbitsmask = sp->dec_nbitsmask; | 430 nbitsmask = sp->dec_nbitsmask; |
427 oldcodep = sp->dec_oldcodep; | 431 oldcodep = sp->dec_oldcodep; |
428 free_entp = sp->dec_free_entp; | 432 free_entp = sp->dec_free_entp; |
429 maxcodep = sp->dec_maxcodep; | 433 maxcodep = sp->dec_maxcodep; |
430 | 434 |
431 while (occ > 0) { | 435 while (occ > 0) { |
432 NextCode(tif, sp, bp, code, GetNextCode); | 436 NextCode(tif, sp, bp, code, GetNextCode); |
433 if (code == CODE_EOI) | 437 if (code == CODE_EOI) |
434 break; | 438 break; |
435 if (code == CODE_CLEAR) { | 439 if (code == CODE_CLEAR) { |
436 » » » free_entp = sp->dec_codetab + CODE_FIRST; | 440 » » » do { |
437 » » » _TIFFmemset(free_entp, 0, | 441 » » » » free_entp = sp->dec_codetab + CODE_FIRST; |
438 » » » » (CSIZE - CODE_FIRST) * sizeof (code_t)); | 442 » » » » _TIFFmemset(free_entp, 0, |
439 » » » nbits = BITS_MIN; | 443 » » » » » (CSIZE - CODE_FIRST) * sizeof (code_
t)); |
440 » » » nbitsmask = MAXCODE(BITS_MIN); | 444 » » » » nbits = BITS_MIN; |
441 » » » maxcodep = sp->dec_codetab + nbitsmask-1; | 445 » » » » nbitsmask = MAXCODE(BITS_MIN); |
442 » » » NextCode(tif, sp, bp, code, GetNextCode); | 446 » » » » maxcodep = sp->dec_codetab + nbitsmask-1; |
| 447 » » » » NextCode(tif, sp, bp, code, GetNextCode); |
| 448 » » » } while (code == CODE_CLEAR);» /* consecutive CODE_CLEA
R codes */ |
443 if (code == CODE_EOI) | 449 if (code == CODE_EOI) |
444 break; | 450 break; |
445 » » » if (code >= CODE_CLEAR) { | 451 » » » if (code > CODE_CLEAR) { |
446 TIFFErrorExt(tif->tif_clientdata, tif->tif_name, | 452 TIFFErrorExt(tif->tif_clientdata, tif->tif_name, |
447 "LZWDecode: Corrupted LZW table at scanline %d", | 453 "LZWDecode: Corrupted LZW table at scanline %d", |
448 tif->tif_row); | 454 tif->tif_row); |
449 return (0); | 455 return (0); |
450 } | 456 } |
451 *op++ = (char)code, occ--; | 457 *op++ = (char)code, occ--; |
452 oldcodep = sp->dec_codetab + code; | 458 oldcodep = sp->dec_codetab + code; |
453 continue; | 459 continue; |
454 } | 460 } |
455 codep = sp->dec_codetab + code; | 461 codep = sp->dec_codetab + code; |
(...skipping 189 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
645 nbitsmask = sp->dec_nbitsmask; | 651 nbitsmask = sp->dec_nbitsmask; |
646 oldcodep = sp->dec_oldcodep; | 652 oldcodep = sp->dec_oldcodep; |
647 free_entp = sp->dec_free_entp; | 653 free_entp = sp->dec_free_entp; |
648 maxcodep = sp->dec_maxcodep; | 654 maxcodep = sp->dec_maxcodep; |
649 | 655 |
650 while (occ > 0) { | 656 while (occ > 0) { |
651 NextCode(tif, sp, bp, code, GetNextCodeCompat); | 657 NextCode(tif, sp, bp, code, GetNextCodeCompat); |
652 if (code == CODE_EOI) | 658 if (code == CODE_EOI) |
653 break; | 659 break; |
654 if (code == CODE_CLEAR) { | 660 if (code == CODE_CLEAR) { |
655 » » » free_entp = sp->dec_codetab + CODE_FIRST; | 661 » » » do { |
656 » » » _TIFFmemset(free_entp, 0, | 662 » » » » free_entp = sp->dec_codetab + CODE_FIRST; |
657 » » » » (CSIZE - CODE_FIRST) * sizeof (code_t)); | 663 » » » » _TIFFmemset(free_entp, 0, |
658 » » » nbits = BITS_MIN; | 664 » » » » » (CSIZE - CODE_FIRST) * sizeof (code_
t)); |
659 » » » nbitsmask = MAXCODE(BITS_MIN); | 665 » » » » nbits = BITS_MIN; |
660 » » » maxcodep = sp->dec_codetab + nbitsmask; | 666 » » » » nbitsmask = MAXCODE(BITS_MIN); |
661 » » » NextCode(tif, sp, bp, code, GetNextCodeCompat); | 667 » » » » maxcodep = sp->dec_codetab + nbitsmask; |
| 668 » » » » NextCode(tif, sp, bp, code, GetNextCodeCompat); |
| 669 » » » } while (code == CODE_CLEAR);» /* consecutive CODE_CLEA
R codes */ |
662 if (code == CODE_EOI) | 670 if (code == CODE_EOI) |
663 break; | 671 break; |
664 » » » if (code >= CODE_CLEAR) { | 672 » » » if (code > CODE_CLEAR) { |
665 TIFFErrorExt(tif->tif_clientdata, tif->tif_name, | 673 TIFFErrorExt(tif->tif_clientdata, tif->tif_name, |
666 "LZWDecode: Corrupted LZW table at scanline %d", | 674 "LZWDecode: Corrupted LZW table at scanline %d", |
667 tif->tif_row); | 675 tif->tif_row); |
668 return (0); | 676 return (0); |
669 } | 677 } |
670 *op++ = code, occ--; | 678 *op++ = code, occ--; |
671 oldcodep = sp->dec_codetab + code; | 679 oldcodep = sp->dec_codetab + code; |
672 continue; | 680 continue; |
673 } | 681 } |
674 codep = sp->dec_codetab + code; | 682 codep = sp->dec_codetab + code; |
(...skipping 147 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
822 return (1); | 830 return (1); |
823 } | 831 } |
824 | 832 |
825 #define CALCRATIO(sp, rat) { \ | 833 #define CALCRATIO(sp, rat) { \ |
826 if (incount > 0x007fffff) { /* NB: shift will overflow */\ | 834 if (incount > 0x007fffff) { /* NB: shift will overflow */\ |
827 rat = outcount >> 8; \ | 835 rat = outcount >> 8; \ |
828 rat = (rat == 0 ? 0x7fffffff : incount/rat); \ | 836 rat = (rat == 0 ? 0x7fffffff : incount/rat); \ |
829 } else \ | 837 } else \ |
830 rat = (incount<<8) / outcount; \ | 838 rat = (incount<<8) / outcount; \ |
831 } | 839 } |
| 840 |
| 841 /* Explicit 0xff masking to make icc -check=conversions happy */ |
832 #define PutNextCode(op, c) { \ | 842 #define PutNextCode(op, c) { \ |
833 nextdata = (nextdata << nbits) | c; \ | 843 nextdata = (nextdata << nbits) | c; \ |
834 nextbits += nbits; \ | 844 nextbits += nbits; \ |
835 » *op++ = (unsigned char)(nextdata >> (nextbits-8));» » \ | 845 » *op++ = (unsigned char)((nextdata >> (nextbits-8))&0xff);» »
\ |
836 nextbits -= 8; \ | 846 nextbits -= 8; \ |
837 if (nextbits >= 8) { \ | 847 if (nextbits >= 8) { \ |
838 » » *op++ = (unsigned char)(nextdata >> (nextbits-8));» \ | 848 » » *op++ = (unsigned char)((nextdata >> (nextbits-8))&0xff);»
\ |
839 nextbits -= 8; \ | 849 nextbits -= 8; \ |
840 } \ | 850 } \ |
841 outcount += nbits; \ | 851 outcount += nbits; \ |
842 } | 852 } |
843 | 853 |
844 /* | 854 /* |
845 * Encode a chunk of pixels. | 855 * Encode a chunk of pixels. |
846 * | 856 * |
847 * Uses an open addressing double hashing (no chaining) on the | 857 * Uses an open addressing double hashing (no chaining) on the |
848 * prefix code/next character combination. We do a variant of | 858 * 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 | 859 * Knuth's algorithm D (vol. 3, sec. 6.4) along with G. Knott's |
850 * relatively-prime secondary probe. Here, the modular division | 860 * relatively-prime secondary probe. Here, the modular division |
851 * first probe is gives way to a faster exclusive-or manipulation. | 861 * first probe is gives way to a faster exclusive-or manipulation. |
852 * Also do block compression with an adaptive reset, whereby the | 862 * Also do block compression with an adaptive reset, whereby the |
853 * code table is cleared when the compression ratio decreases, | 863 * code table is cleared when the compression ratio decreases, |
854 * but after the table fills. The variable-length output codes | 864 * but after the table fills. The variable-length output codes |
855 * are re-sized at this point, and a CODE_CLEAR is generated | 865 * are re-sized at this point, and a CODE_CLEAR is generated |
856 * for the decoder. | 866 * for the decoder. |
857 */ | 867 */ |
858 static int | 868 static int |
859 LZWEncode(TIFF* tif, uint8* bp, tmsize_t cc, uint16 s) | 869 LZWEncode(TIFF* tif, uint8* bp, tmsize_t cc, uint16 s) |
860 { | 870 { |
861 register LZWCodecState *sp = EncoderState(tif); | 871 register LZWCodecState *sp = EncoderState(tif); |
862 register long fcode; | 872 register long fcode; |
863 register hash_t *hp; | 873 register hash_t *hp; |
864 register int h, c; | 874 register int h, c; |
865 hcode_t ent; | 875 hcode_t ent; |
866 long disp; | 876 long disp; |
867 long incount, outcount, checkpoint; | 877 long incount, outcount, checkpoint; |
868 » long nextdata, nextbits; | 878 » unsigned long nextdata; |
| 879 long nextbits; |
869 int free_ent, maxcode, nbits; | 880 int free_ent, maxcode, nbits; |
870 uint8* op; | 881 uint8* op; |
871 uint8* limit; | 882 uint8* limit; |
872 | 883 |
873 (void) s; | 884 (void) s; |
874 if (sp == NULL) | 885 if (sp == NULL) |
875 return (0); | 886 return (0); |
876 | 887 |
877 assert(sp->enc_hashtab != NULL); | 888 assert(sp->enc_hashtab != NULL); |
878 | 889 |
(...skipping 141 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1020 /* | 1031 /* |
1021 * Finish off an encoded strip by flushing the last | 1032 * Finish off an encoded strip by flushing the last |
1022 * string and tacking on an End Of Information code. | 1033 * string and tacking on an End Of Information code. |
1023 */ | 1034 */ |
1024 static int | 1035 static int |
1025 LZWPostEncode(TIFF* tif) | 1036 LZWPostEncode(TIFF* tif) |
1026 { | 1037 { |
1027 register LZWCodecState *sp = EncoderState(tif); | 1038 register LZWCodecState *sp = EncoderState(tif); |
1028 uint8* op = tif->tif_rawcp; | 1039 uint8* op = tif->tif_rawcp; |
1029 long nextbits = sp->lzw_nextbits; | 1040 long nextbits = sp->lzw_nextbits; |
1030 » long nextdata = sp->lzw_nextdata; | 1041 » unsigned long nextdata = sp->lzw_nextdata; |
1031 long outcount = sp->enc_outcount; | 1042 long outcount = sp->enc_outcount; |
1032 int nbits = sp->lzw_nbits; | 1043 int nbits = sp->lzw_nbits; |
1033 | 1044 |
1034 if (op > sp->enc_rawlimit) { | 1045 if (op > sp->enc_rawlimit) { |
1035 tif->tif_rawcc = (tmsize_t)(op - tif->tif_rawdata); | 1046 tif->tif_rawcc = (tmsize_t)(op - tif->tif_rawdata); |
1036 TIFFFlushData1(tif); | 1047 TIFFFlushData1(tif); |
1037 op = tif->tif_rawdata; | 1048 op = tif->tif_rawdata; |
1038 } | 1049 } |
1039 if (sp->enc_oldcode != (hcode_t) -1) { | 1050 if (sp->enc_oldcode != (hcode_t) -1) { |
1040 PutNextCode(op, sp->enc_oldcode); | 1051 PutNextCode(op, sp->enc_oldcode); |
1041 sp->enc_oldcode = (hcode_t) -1; | 1052 sp->enc_oldcode = (hcode_t) -1; |
1042 } | 1053 } |
1043 PutNextCode(op, CODE_EOI); | 1054 PutNextCode(op, CODE_EOI); |
| 1055 /* Explicit 0xff masking to make icc -check=conversions happy */ |
1044 if (nextbits > 0) | 1056 if (nextbits > 0) |
1045 » » *op++ = (unsigned char)(nextdata << (8-nextbits)); | 1057 » » *op++ = (unsigned char)((nextdata << (8-nextbits))&0xff); |
1046 tif->tif_rawcc = (tmsize_t)(op - tif->tif_rawdata); | 1058 tif->tif_rawcc = (tmsize_t)(op - tif->tif_rawdata); |
1047 return (1); | 1059 return (1); |
1048 } | 1060 } |
1049 | 1061 |
1050 /* | 1062 /* |
1051 * Reset encoding hash table. | 1063 * Reset encoding hash table. |
1052 */ | 1064 */ |
1053 static void | 1065 static void |
1054 cl_hash(LZWCodecState* sp) | 1066 cl_hash(LZWCodecState* sp) |
1055 { | 1067 { |
(...skipping 101 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1157 #endif /* LZW_SUPPORT */ | 1169 #endif /* LZW_SUPPORT */ |
1158 | 1170 |
1159 /* vim: set ts=8 sts=8 sw=8 noet: */ | 1171 /* vim: set ts=8 sts=8 sw=8 noet: */ |
1160 /* | 1172 /* |
1161 * Local Variables: | 1173 * Local Variables: |
1162 * mode: c | 1174 * mode: c |
1163 * c-basic-offset: 8 | 1175 * c-basic-offset: 8 |
1164 * fill-column: 78 | 1176 * fill-column: 78 |
1165 * End: | 1177 * End: |
1166 */ | 1178 */ |
1167 | |
OLD | NEW |