[18173] | 1 | /* $Header: /afs/dev.mit.edu/source/repository/third/tiff/libtiff/tif_lzw.c,v 1.1.1.1 2002-12-26 02:37:19 ghudson Exp $ */ |
---|
| 2 | |
---|
| 3 | /* |
---|
| 4 | * Copyright (c) 1988-1997 Sam Leffler |
---|
| 5 | * Copyright (c) 1991-1997 Silicon Graphics, Inc. |
---|
| 6 | * |
---|
| 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. |
---|
| 14 | * |
---|
| 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. |
---|
| 18 | * |
---|
| 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 |
---|
| 24 | * OF THIS SOFTWARE. |
---|
| 25 | */ |
---|
| 26 | |
---|
| 27 | #include "tiffiop.h" |
---|
| 28 | #ifdef LZW_SUPPORT |
---|
| 29 | /* |
---|
| 30 | * TIFF Library. |
---|
| 31 | * Rev 5.0 Lempel-Ziv & Welch Compression Support |
---|
| 32 | * |
---|
| 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. |
---|
| 36 | * |
---|
| 37 | * The original Berkeley copyright notice appears below in its entirety. |
---|
| 38 | */ |
---|
| 39 | #include "tif_predict.h" |
---|
| 40 | |
---|
| 41 | #include <assert.h> |
---|
| 42 | #include <stdio.h> |
---|
| 43 | |
---|
| 44 | /* |
---|
| 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. |
---|
| 54 | * |
---|
| 55 | * Future revisions to the TIFF spec are expected to "clarify this issue". |
---|
| 56 | */ |
---|
| 57 | #define LZW_COMPAT /* include backwards compatibility code */ |
---|
| 58 | /* |
---|
| 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. |
---|
| 63 | */ |
---|
| 64 | #define LZW_CHECKEOS /* include checks for strips w/o EOI code */ |
---|
| 65 | |
---|
| 66 | #define MAXCODE(n) ((1L<<(n))-1) |
---|
| 67 | /* |
---|
| 68 | * The TIFF spec specifies that encoded bit |
---|
| 69 | * strings range from 9 to 12 bits. |
---|
| 70 | */ |
---|
| 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 */ |
---|
| 79 | #define HSHIFT (13-8) |
---|
| 80 | #ifdef LZW_COMPAT |
---|
| 81 | /* NB: +1024 is for compatibility with old files */ |
---|
| 82 | #define CSIZE (MAXCODE(BITS_MAX)+1024L) |
---|
| 83 | #else |
---|
| 84 | #define CSIZE (MAXCODE(BITS_MAX)+1L) |
---|
| 85 | #endif |
---|
| 86 | |
---|
| 87 | /* |
---|
| 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. |
---|
| 91 | */ |
---|
| 92 | typedef struct { |
---|
| 93 | TIFFPredictorState predict; /* predictor super class */ |
---|
| 94 | |
---|
| 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 */ |
---|
| 100 | } LZWBaseState; |
---|
| 101 | |
---|
| 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 |
---|
| 107 | |
---|
| 108 | /* |
---|
| 109 | * Decoding-specific state. |
---|
| 110 | */ |
---|
| 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 */ |
---|
| 116 | } code_t; |
---|
| 117 | |
---|
| 118 | typedef int (*decodeFunc)(TIFF*, tidata_t, tsize_t, tsample_t); |
---|
| 119 | |
---|
| 120 | typedef struct { |
---|
| 121 | LZWBaseState base; |
---|
| 122 | long dec_nbitsmask; /* lzw_nbits 1 bits, right adjusted */ |
---|
| 123 | long dec_restart; /* restart count */ |
---|
| 124 | #ifdef LZW_CHECKEOS |
---|
| 125 | long dec_bitsleft; /* available bits in raw data */ |
---|
| 126 | #endif |
---|
| 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 */ |
---|
| 133 | } LZWDecodeState; |
---|
| 134 | |
---|
| 135 | /* |
---|
| 136 | * Encoding-specific state. |
---|
| 137 | */ |
---|
| 138 | typedef uint16 hcode_t; /* codes fit in 16 bits */ |
---|
| 139 | typedef struct { |
---|
| 140 | long hash; |
---|
| 141 | hcode_t code; |
---|
| 142 | } hash_t; |
---|
| 143 | |
---|
| 144 | typedef struct { |
---|
| 145 | LZWBaseState base; |
---|
| 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 */ |
---|
| 154 | } LZWEncodeState; |
---|
| 155 | |
---|
| 156 | #define LZWState(tif) ((LZWBaseState*) (tif)->tif_data) |
---|
| 157 | #define DecoderState(tif) ((LZWDecodeState*) LZWState(tif)) |
---|
| 158 | #define EncoderState(tif) ((LZWEncodeState*) LZWState(tif)) |
---|
| 159 | |
---|
| 160 | static int LZWDecode(TIFF*, tidata_t, tsize_t, tsample_t); |
---|
| 161 | #ifdef LZW_COMPAT |
---|
| 162 | static int LZWDecodeCompat(TIFF*, tidata_t, tsize_t, tsample_t); |
---|
| 163 | #endif |
---|
| 164 | static void cl_hash(LZWEncodeState*); |
---|
| 165 | |
---|
| 166 | /* |
---|
| 167 | * LZW Decoder. |
---|
| 168 | */ |
---|
| 169 | |
---|
| 170 | #ifdef LZW_CHECKEOS |
---|
| 171 | /* |
---|
| 172 | * This check shouldn't be necessary because each |
---|
| 173 | * strip is suppose to be terminated with CODE_EOI. |
---|
| 174 | */ |
---|
| 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); \ |
---|
| 180 | _code = CODE_EOI; \ |
---|
| 181 | } else { \ |
---|
| 182 | _get(_sp,_bp,_code); \ |
---|
| 183 | (_sp)->dec_bitsleft -= nbits; \ |
---|
| 184 | } \ |
---|
| 185 | } |
---|
| 186 | #else |
---|
| 187 | #define NextCode(tif, sp, bp, code, get) get(sp, bp, code) |
---|
| 188 | #endif |
---|
| 189 | |
---|
| 190 | static int |
---|
| 191 | LZWSetupDecode(TIFF* tif) |
---|
| 192 | { |
---|
| 193 | LZWDecodeState* sp = DecoderState(tif); |
---|
| 194 | static const char module[] = " LZWSetupDecode"; |
---|
| 195 | int code; |
---|
| 196 | |
---|
| 197 | assert(sp != NULL); |
---|
| 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"); |
---|
| 202 | return (0); |
---|
| 203 | } |
---|
| 204 | /* |
---|
| 205 | * Pre-load the table. |
---|
| 206 | */ |
---|
| 207 | code = 255; |
---|
| 208 | do { |
---|
| 209 | sp->dec_codetab[code].value = code; |
---|
| 210 | sp->dec_codetab[code].firstchar = code; |
---|
| 211 | sp->dec_codetab[code].length = 1; |
---|
| 212 | sp->dec_codetab[code].next = NULL; |
---|
| 213 | } while (code--); |
---|
| 214 | } |
---|
| 215 | return (1); |
---|
| 216 | } |
---|
| 217 | |
---|
| 218 | /* |
---|
| 219 | * Setup state for decoding a strip. |
---|
| 220 | */ |
---|
| 221 | static int |
---|
| 222 | LZWPreDecode(TIFF* tif, tsample_t s) |
---|
| 223 | { |
---|
| 224 | LZWDecodeState *sp = DecoderState(tif); |
---|
| 225 | |
---|
| 226 | (void) s; |
---|
| 227 | assert(sp != NULL); |
---|
| 228 | /* |
---|
| 229 | * Check for old bit-reversed codes. |
---|
| 230 | */ |
---|
| 231 | if (tif->tif_rawdata[0] == 0 && (tif->tif_rawdata[1] & 0x1)) { |
---|
| 232 | #ifdef LZW_COMPAT |
---|
| 233 | if (!sp->dec_decode) { |
---|
| 234 | TIFFWarning(tif->tif_name, |
---|
| 235 | "Old-style LZW codes, convert file"); |
---|
| 236 | /* |
---|
| 237 | * Override default decoding methods with |
---|
| 238 | * ones that deal with the old coding. |
---|
| 239 | * Otherwise the predictor versions set |
---|
| 240 | * above will call the compatibility routines |
---|
| 241 | * through the dec_decode method. |
---|
| 242 | */ |
---|
| 243 | tif->tif_decoderow = LZWDecodeCompat; |
---|
| 244 | tif->tif_decodestrip = LZWDecodeCompat; |
---|
| 245 | tif->tif_decodetile = LZWDecodeCompat; |
---|
| 246 | /* |
---|
| 247 | * If doing horizontal differencing, must |
---|
| 248 | * re-setup the predictor logic since we |
---|
| 249 | * switched the basic decoder methods... |
---|
| 250 | */ |
---|
| 251 | (*tif->tif_setupdecode)(tif); |
---|
| 252 | sp->dec_decode = LZWDecodeCompat; |
---|
| 253 | } |
---|
| 254 | sp->lzw_maxcode = MAXCODE(BITS_MIN); |
---|
| 255 | #else /* !LZW_COMPAT */ |
---|
| 256 | if (!sp->dec_decode) { |
---|
| 257 | TIFFError(tif->tif_name, |
---|
| 258 | "Old-style LZW codes not supported"); |
---|
| 259 | sp->dec_decode = LZWDecode; |
---|
| 260 | } |
---|
| 261 | return (0); |
---|
| 262 | #endif/* !LZW_COMPAT */ |
---|
| 263 | } else { |
---|
| 264 | sp->lzw_maxcode = MAXCODE(BITS_MIN)-1; |
---|
| 265 | sp->dec_decode = LZWDecode; |
---|
| 266 | } |
---|
| 267 | sp->lzw_nbits = BITS_MIN; |
---|
| 268 | sp->lzw_nextbits = 0; |
---|
| 269 | sp->lzw_nextdata = 0; |
---|
| 270 | |
---|
| 271 | sp->dec_restart = 0; |
---|
| 272 | sp->dec_nbitsmask = MAXCODE(BITS_MIN); |
---|
| 273 | #ifdef LZW_CHECKEOS |
---|
| 274 | sp->dec_bitsleft = tif->tif_rawcc << 3; |
---|
| 275 | #endif |
---|
| 276 | sp->dec_free_entp = sp->dec_codetab + CODE_FIRST; |
---|
| 277 | /* |
---|
| 278 | * Zero entries that are not yet filled in. We do |
---|
| 279 | * this to guard against bogus input data that causes |
---|
| 280 | * us to index into undefined entries. If you can |
---|
| 281 | * come up with a way to safely bounds-check input codes |
---|
| 282 | * while decoding then you can remove this operation. |
---|
| 283 | */ |
---|
| 284 | _TIFFmemset(sp->dec_free_entp, 0, (CSIZE-CODE_FIRST)*sizeof (code_t)); |
---|
| 285 | sp->dec_oldcodep = &sp->dec_codetab[-1]; |
---|
| 286 | sp->dec_maxcodep = &sp->dec_codetab[sp->dec_nbitsmask-1]; |
---|
| 287 | return (1); |
---|
| 288 | } |
---|
| 289 | |
---|
| 290 | /* |
---|
| 291 | * Decode a "hunk of data". |
---|
| 292 | */ |
---|
| 293 | #define GetNextCode(sp, bp, code) { \ |
---|
| 294 | nextdata = (nextdata<<8) | *(bp)++; \ |
---|
| 295 | nextbits += 8; \ |
---|
| 296 | if (nextbits < nbits) { \ |
---|
| 297 | nextdata = (nextdata<<8) | *(bp)++; \ |
---|
| 298 | nextbits += 8; \ |
---|
| 299 | } \ |
---|
| 300 | code = (hcode_t)((nextdata >> (nextbits-nbits)) & nbitsmask); \ |
---|
| 301 | nextbits -= nbits; \ |
---|
| 302 | } |
---|
| 303 | |
---|
| 304 | static void |
---|
| 305 | codeLoop(TIFF* tif) |
---|
| 306 | { |
---|
| 307 | TIFFError(tif->tif_name, |
---|
| 308 | "LZWDecode: Bogus encoding, loop in the code table; scanline %d", |
---|
| 309 | tif->tif_row); |
---|
| 310 | } |
---|
| 311 | |
---|
| 312 | static int |
---|
| 313 | LZWDecode(TIFF* tif, tidata_t op0, tsize_t occ0, tsample_t s) |
---|
| 314 | { |
---|
| 315 | LZWDecodeState *sp = DecoderState(tif); |
---|
| 316 | char *op = (char*) op0; |
---|
| 317 | long occ = (long) occ0; |
---|
| 318 | char *tp; |
---|
| 319 | u_char *bp; |
---|
| 320 | hcode_t code; |
---|
| 321 | int len; |
---|
| 322 | long nbits, nextbits, nextdata, nbitsmask; |
---|
| 323 | code_t *codep, *free_entp, *maxcodep, *oldcodep; |
---|
| 324 | |
---|
| 325 | (void) s; |
---|
| 326 | assert(sp != NULL); |
---|
| 327 | /* |
---|
| 328 | * Restart interrupted output operation. |
---|
| 329 | */ |
---|
| 330 | if (sp->dec_restart) { |
---|
| 331 | long residue; |
---|
| 332 | |
---|
| 333 | codep = sp->dec_codep; |
---|
| 334 | residue = codep->length - sp->dec_restart; |
---|
| 335 | if (residue > occ) { |
---|
| 336 | /* |
---|
| 337 | * Residue from previous decode is sufficient |
---|
| 338 | * to satisfy decode request. Skip to the |
---|
| 339 | * start of the decoded string, place decoded |
---|
| 340 | * values in the output buffer, and return. |
---|
| 341 | */ |
---|
| 342 | sp->dec_restart += occ; |
---|
| 343 | do { |
---|
| 344 | codep = codep->next; |
---|
| 345 | } while (--residue > occ && codep); |
---|
| 346 | if (codep) { |
---|
| 347 | tp = op + occ; |
---|
| 348 | do { |
---|
| 349 | *--tp = codep->value; |
---|
| 350 | codep = codep->next; |
---|
| 351 | } while (--occ && codep); |
---|
| 352 | } |
---|
| 353 | return (1); |
---|
| 354 | } |
---|
| 355 | /* |
---|
| 356 | * Residue satisfies only part of the decode request. |
---|
| 357 | */ |
---|
| 358 | op += residue, occ -= residue; |
---|
| 359 | tp = op; |
---|
| 360 | do { |
---|
| 361 | int t; |
---|
| 362 | --tp; |
---|
| 363 | t = codep->value; |
---|
| 364 | codep = codep->next; |
---|
| 365 | *tp = t; |
---|
| 366 | } while (--residue && codep); |
---|
| 367 | sp->dec_restart = 0; |
---|
| 368 | } |
---|
| 369 | |
---|
| 370 | bp = (u_char *)tif->tif_rawcp; |
---|
| 371 | nbits = sp->lzw_nbits; |
---|
| 372 | nextdata = sp->lzw_nextdata; |
---|
| 373 | nextbits = sp->lzw_nextbits; |
---|
| 374 | nbitsmask = sp->dec_nbitsmask; |
---|
| 375 | oldcodep = sp->dec_oldcodep; |
---|
| 376 | free_entp = sp->dec_free_entp; |
---|
| 377 | maxcodep = sp->dec_maxcodep; |
---|
| 378 | |
---|
| 379 | while (occ > 0) { |
---|
| 380 | NextCode(tif, sp, bp, code, GetNextCode); |
---|
| 381 | if (code == CODE_EOI) |
---|
| 382 | break; |
---|
| 383 | if (code == CODE_CLEAR) { |
---|
| 384 | free_entp = sp->dec_codetab + CODE_FIRST; |
---|
| 385 | nbits = BITS_MIN; |
---|
| 386 | nbitsmask = MAXCODE(BITS_MIN); |
---|
| 387 | maxcodep = sp->dec_codetab + nbitsmask-1; |
---|
| 388 | NextCode(tif, sp, bp, code, GetNextCode); |
---|
| 389 | if (code == CODE_EOI) |
---|
| 390 | break; |
---|
| 391 | *op++ = (char)code, occ--; |
---|
| 392 | oldcodep = sp->dec_codetab + code; |
---|
| 393 | continue; |
---|
| 394 | } |
---|
| 395 | codep = sp->dec_codetab + code; |
---|
| 396 | |
---|
| 397 | /* |
---|
| 398 | * Add the new entry to the code table. |
---|
| 399 | */ |
---|
| 400 | assert(&sp->dec_codetab[0] <= free_entp && free_entp < &sp->dec_codetab[CSIZE]); |
---|
| 401 | free_entp->next = oldcodep; |
---|
| 402 | free_entp->firstchar = free_entp->next->firstchar; |
---|
| 403 | free_entp->length = free_entp->next->length+1; |
---|
| 404 | free_entp->value = (codep < free_entp) ? |
---|
| 405 | codep->firstchar : free_entp->firstchar; |
---|
| 406 | if (++free_entp > maxcodep) { |
---|
| 407 | if (++nbits > BITS_MAX) /* should not happen */ |
---|
| 408 | nbits = BITS_MAX; |
---|
| 409 | nbitsmask = MAXCODE(nbits); |
---|
| 410 | maxcodep = sp->dec_codetab + nbitsmask-1; |
---|
| 411 | } |
---|
| 412 | oldcodep = codep; |
---|
| 413 | if (code >= 256) { |
---|
| 414 | /* |
---|
| 415 | * Code maps to a string, copy string |
---|
| 416 | * value to output (written in reverse). |
---|
| 417 | */ |
---|
| 418 | if (codep->length > occ) { |
---|
| 419 | /* |
---|
| 420 | * String is too long for decode buffer, |
---|
| 421 | * locate portion that will fit, copy to |
---|
| 422 | * the decode buffer, and setup restart |
---|
| 423 | * logic for the next decoding call. |
---|
| 424 | */ |
---|
| 425 | sp->dec_codep = codep; |
---|
| 426 | do { |
---|
| 427 | codep = codep->next; |
---|
| 428 | } while (codep && codep->length > occ); |
---|
| 429 | if (codep) { |
---|
| 430 | sp->dec_restart = occ; |
---|
| 431 | tp = op + occ; |
---|
| 432 | do { |
---|
| 433 | *--tp = codep->value; |
---|
| 434 | codep = codep->next; |
---|
| 435 | } while (--occ && codep); |
---|
| 436 | if (codep) |
---|
| 437 | codeLoop(tif); |
---|
| 438 | } |
---|
| 439 | break; |
---|
| 440 | } |
---|
| 441 | len = codep->length; |
---|
| 442 | tp = op + len; |
---|
| 443 | do { |
---|
| 444 | int t; |
---|
| 445 | --tp; |
---|
| 446 | t = codep->value; |
---|
| 447 | codep = codep->next; |
---|
| 448 | *tp = t; |
---|
| 449 | } while (codep && tp > op); |
---|
| 450 | if (codep) { |
---|
| 451 | codeLoop(tif); |
---|
| 452 | break; |
---|
| 453 | } |
---|
| 454 | op += len, occ -= len; |
---|
| 455 | } else |
---|
| 456 | *op++ = (char)code, occ--; |
---|
| 457 | } |
---|
| 458 | |
---|
| 459 | tif->tif_rawcp = (tidata_t) bp; |
---|
| 460 | sp->lzw_nbits = (u_short) nbits; |
---|
| 461 | sp->lzw_nextdata = nextdata; |
---|
| 462 | sp->lzw_nextbits = nextbits; |
---|
| 463 | sp->dec_nbitsmask = nbitsmask; |
---|
| 464 | sp->dec_oldcodep = oldcodep; |
---|
| 465 | sp->dec_free_entp = free_entp; |
---|
| 466 | sp->dec_maxcodep = maxcodep; |
---|
| 467 | |
---|
| 468 | if (occ > 0) { |
---|
| 469 | TIFFError(tif->tif_name, |
---|
| 470 | "LZWDecode: Not enough data at scanline %d (short %d bytes)", |
---|
| 471 | tif->tif_row, occ); |
---|
| 472 | return (0); |
---|
| 473 | } |
---|
| 474 | return (1); |
---|
| 475 | } |
---|
| 476 | |
---|
| 477 | #ifdef LZW_COMPAT |
---|
| 478 | /* |
---|
| 479 | * Decode a "hunk of data" for old images. |
---|
| 480 | */ |
---|
| 481 | #define GetNextCodeCompat(sp, bp, code) { \ |
---|
| 482 | nextdata |= (u_long) *(bp)++ << nextbits; \ |
---|
| 483 | nextbits += 8; \ |
---|
| 484 | if (nextbits < nbits) { \ |
---|
| 485 | nextdata |= (u_long) *(bp)++ << nextbits; \ |
---|
| 486 | nextbits += 8; \ |
---|
| 487 | } \ |
---|
| 488 | code = (hcode_t)(nextdata & nbitsmask); \ |
---|
| 489 | nextdata >>= nbits; \ |
---|
| 490 | nextbits -= nbits; \ |
---|
| 491 | } |
---|
| 492 | |
---|
| 493 | static int |
---|
| 494 | LZWDecodeCompat(TIFF* tif, tidata_t op0, tsize_t occ0, tsample_t s) |
---|
| 495 | { |
---|
| 496 | LZWDecodeState *sp = DecoderState(tif); |
---|
| 497 | char *op = (char*) op0; |
---|
| 498 | long occ = (long) occ0; |
---|
| 499 | char *tp; |
---|
| 500 | u_char *bp; |
---|
| 501 | int code, nbits; |
---|
| 502 | long nextbits, nextdata, nbitsmask; |
---|
| 503 | code_t *codep, *free_entp, *maxcodep, *oldcodep; |
---|
| 504 | |
---|
| 505 | (void) s; |
---|
| 506 | assert(sp != NULL); |
---|
| 507 | /* |
---|
| 508 | * Restart interrupted output operation. |
---|
| 509 | */ |
---|
| 510 | if (sp->dec_restart) { |
---|
| 511 | long residue; |
---|
| 512 | |
---|
| 513 | codep = sp->dec_codep; |
---|
| 514 | residue = codep->length - sp->dec_restart; |
---|
| 515 | if (residue > occ) { |
---|
| 516 | /* |
---|
| 517 | * Residue from previous decode is sufficient |
---|
| 518 | * to satisfy decode request. Skip to the |
---|
| 519 | * start of the decoded string, place decoded |
---|
| 520 | * values in the output buffer, and return. |
---|
| 521 | */ |
---|
| 522 | sp->dec_restart += occ; |
---|
| 523 | do { |
---|
| 524 | codep = codep->next; |
---|
| 525 | } while (--residue > occ); |
---|
| 526 | tp = op + occ; |
---|
| 527 | do { |
---|
| 528 | *--tp = codep->value; |
---|
| 529 | codep = codep->next; |
---|
| 530 | } while (--occ); |
---|
| 531 | return (1); |
---|
| 532 | } |
---|
| 533 | /* |
---|
| 534 | * Residue satisfies only part of the decode request. |
---|
| 535 | */ |
---|
| 536 | op += residue, occ -= residue; |
---|
| 537 | tp = op; |
---|
| 538 | do { |
---|
| 539 | *--tp = codep->value; |
---|
| 540 | codep = codep->next; |
---|
| 541 | } while (--residue); |
---|
| 542 | sp->dec_restart = 0; |
---|
| 543 | } |
---|
| 544 | |
---|
| 545 | bp = (u_char *)tif->tif_rawcp; |
---|
| 546 | nbits = sp->lzw_nbits; |
---|
| 547 | nextdata = sp->lzw_nextdata; |
---|
| 548 | nextbits = sp->lzw_nextbits; |
---|
| 549 | nbitsmask = sp->dec_nbitsmask; |
---|
| 550 | oldcodep = sp->dec_oldcodep; |
---|
| 551 | free_entp = sp->dec_free_entp; |
---|
| 552 | maxcodep = sp->dec_maxcodep; |
---|
| 553 | |
---|
| 554 | while (occ > 0) { |
---|
| 555 | NextCode(tif, sp, bp, code, GetNextCodeCompat); |
---|
| 556 | if (code == CODE_EOI) |
---|
| 557 | break; |
---|
| 558 | if (code == CODE_CLEAR) { |
---|
| 559 | free_entp = sp->dec_codetab + CODE_FIRST; |
---|
| 560 | nbits = BITS_MIN; |
---|
| 561 | nbitsmask = MAXCODE(BITS_MIN); |
---|
| 562 | maxcodep = sp->dec_codetab + nbitsmask; |
---|
| 563 | NextCode(tif, sp, bp, code, GetNextCodeCompat); |
---|
| 564 | if (code == CODE_EOI) |
---|
| 565 | break; |
---|
| 566 | *op++ = code, occ--; |
---|
| 567 | oldcodep = sp->dec_codetab + code; |
---|
| 568 | continue; |
---|
| 569 | } |
---|
| 570 | codep = sp->dec_codetab + code; |
---|
| 571 | |
---|
| 572 | /* |
---|
| 573 | * Add the new entry to the code table. |
---|
| 574 | */ |
---|
| 575 | assert(&sp->dec_codetab[0] <= free_entp && free_entp < &sp->dec_codetab[CSIZE]); |
---|
| 576 | free_entp->next = oldcodep; |
---|
| 577 | free_entp->firstchar = free_entp->next->firstchar; |
---|
| 578 | free_entp->length = free_entp->next->length+1; |
---|
| 579 | free_entp->value = (codep < free_entp) ? |
---|
| 580 | codep->firstchar : free_entp->firstchar; |
---|
| 581 | if (++free_entp > maxcodep) { |
---|
| 582 | if (++nbits > BITS_MAX) /* should not happen */ |
---|
| 583 | nbits = BITS_MAX; |
---|
| 584 | nbitsmask = MAXCODE(nbits); |
---|
| 585 | maxcodep = sp->dec_codetab + nbitsmask; |
---|
| 586 | } |
---|
| 587 | oldcodep = codep; |
---|
| 588 | if (code >= 256) { |
---|
| 589 | /* |
---|
| 590 | * Code maps to a string, copy string |
---|
| 591 | * value to output (written in reverse). |
---|
| 592 | */ |
---|
| 593 | if (codep->length > occ) { |
---|
| 594 | /* |
---|
| 595 | * String is too long for decode buffer, |
---|
| 596 | * locate portion that will fit, copy to |
---|
| 597 | * the decode buffer, and setup restart |
---|
| 598 | * logic for the next decoding call. |
---|
| 599 | */ |
---|
| 600 | sp->dec_codep = codep; |
---|
| 601 | do { |
---|
| 602 | codep = codep->next; |
---|
| 603 | } while (codep->length > occ); |
---|
| 604 | sp->dec_restart = occ; |
---|
| 605 | tp = op + occ; |
---|
| 606 | do { |
---|
| 607 | *--tp = codep->value; |
---|
| 608 | codep = codep->next; |
---|
| 609 | } while (--occ); |
---|
| 610 | break; |
---|
| 611 | } |
---|
| 612 | op += codep->length, occ -= codep->length; |
---|
| 613 | tp = op; |
---|
| 614 | do { |
---|
| 615 | *--tp = codep->value; |
---|
| 616 | } while( (codep = codep->next) != NULL); |
---|
| 617 | } else |
---|
| 618 | *op++ = code, occ--; |
---|
| 619 | } |
---|
| 620 | |
---|
| 621 | tif->tif_rawcp = (tidata_t) bp; |
---|
| 622 | sp->lzw_nbits = nbits; |
---|
| 623 | sp->lzw_nextdata = nextdata; |
---|
| 624 | sp->lzw_nextbits = nextbits; |
---|
| 625 | sp->dec_nbitsmask = nbitsmask; |
---|
| 626 | sp->dec_oldcodep = oldcodep; |
---|
| 627 | sp->dec_free_entp = free_entp; |
---|
| 628 | sp->dec_maxcodep = maxcodep; |
---|
| 629 | |
---|
| 630 | if (occ > 0) { |
---|
| 631 | TIFFError(tif->tif_name, |
---|
| 632 | "LZWDecodeCompat: Not enough data at scanline %d (short %d bytes)", |
---|
| 633 | tif->tif_row, occ); |
---|
| 634 | return (0); |
---|
| 635 | } |
---|
| 636 | return (1); |
---|
| 637 | } |
---|
| 638 | #endif /* LZW_COMPAT */ |
---|
| 639 | |
---|
| 640 | |
---|
| 641 | |
---|
| 642 | static void |
---|
| 643 | LZWCleanup(TIFF* tif) |
---|
| 644 | { |
---|
| 645 | if (tif->tif_data) { |
---|
| 646 | if (tif->tif_mode == O_RDONLY) { |
---|
| 647 | if (DecoderState(tif)->dec_codetab) |
---|
| 648 | _TIFFfree(DecoderState(tif)->dec_codetab); |
---|
| 649 | } |
---|
| 650 | _TIFFfree(tif->tif_data); |
---|
| 651 | tif->tif_data = NULL; |
---|
| 652 | } |
---|
| 653 | } |
---|
| 654 | |
---|
| 655 | static int |
---|
| 656 | LZWSetupEncode(TIFF* tif) |
---|
| 657 | { |
---|
| 658 | TIFFError(tif->tif_name, |
---|
| 659 | "LZW compression is not available to due to Unisys patent enforcement"); |
---|
| 660 | return (0); |
---|
| 661 | } |
---|
| 662 | |
---|
| 663 | int |
---|
| 664 | TIFFInitLZW(TIFF* tif, int scheme) |
---|
| 665 | { |
---|
| 666 | assert(scheme == COMPRESSION_LZW); |
---|
| 667 | /* |
---|
| 668 | * Allocate state block so tag methods have storage to record values. |
---|
| 669 | */ |
---|
| 670 | if (tif->tif_mode == O_RDONLY) |
---|
| 671 | { |
---|
| 672 | tif->tif_data = (tidata_t) _TIFFmalloc(sizeof (LZWDecodeState)); |
---|
| 673 | if (tif->tif_data == NULL) |
---|
| 674 | goto bad; |
---|
| 675 | DecoderState(tif)->dec_codetab = NULL; |
---|
| 676 | DecoderState(tif)->dec_decode = NULL; |
---|
| 677 | } |
---|
| 678 | |
---|
| 679 | /* |
---|
| 680 | * Install codec methods. |
---|
| 681 | */ |
---|
| 682 | tif->tif_setupencode = LZWSetupEncode; |
---|
| 683 | tif->tif_setupdecode = LZWSetupDecode; |
---|
| 684 | tif->tif_predecode = LZWPreDecode; |
---|
| 685 | tif->tif_decoderow = LZWDecode; |
---|
| 686 | tif->tif_decodestrip = LZWDecode; |
---|
| 687 | tif->tif_decodetile = LZWDecode; |
---|
| 688 | tif->tif_cleanup = LZWCleanup; |
---|
| 689 | |
---|
| 690 | /* |
---|
| 691 | * Setup predictor setup. |
---|
| 692 | */ |
---|
| 693 | if( tif->tif_mode == O_RDONLY ) |
---|
| 694 | (void) TIFFPredictorInit(tif); |
---|
| 695 | |
---|
| 696 | return (1); |
---|
| 697 | |
---|
| 698 | bad: |
---|
| 699 | TIFFError("TIFFInitLZW", "No space for LZW state block"); |
---|
| 700 | return (0); |
---|
| 701 | } |
---|
| 702 | |
---|
| 703 | /* |
---|
| 704 | * Copyright (c) 1985, 1986 The Regents of the University of California. |
---|
| 705 | * All rights reserved. |
---|
| 706 | * |
---|
| 707 | * This code is derived from software contributed to Berkeley by |
---|
| 708 | * James A. Woods, derived from original work by Spencer Thomas |
---|
| 709 | * and Joseph Orost. |
---|
| 710 | * |
---|
| 711 | * Redistribution and use in source and binary forms are permitted |
---|
| 712 | * provided that the above copyright notice and this paragraph are |
---|
| 713 | * duplicated in all such forms and that any documentation, |
---|
| 714 | * advertising materials, and other materials related to such |
---|
| 715 | * distribution and use acknowledge that the software was developed |
---|
| 716 | * by the University of California, Berkeley. The name of the |
---|
| 717 | * University may not be used to endorse or promote products derived |
---|
| 718 | * from this software without specific prior written permission. |
---|
| 719 | * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR |
---|
| 720 | * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED |
---|
| 721 | * WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE. |
---|
| 722 | */ |
---|
| 723 | #endif /* LZW_SUPPORT */ |
---|