/* * Copyright (c) 1988-1997 Sam Leffler * Copyright (c) 1991-1997 Silicon Graphics, Inc. * Copyright (c) 2022 Even Rouault * * Permission to use, copy, modify, distribute, and sell this software and * its documentation for any purpose is hereby granted without fee, provided * that (i) the above copyright notices and this permission notice appear in * all copies of the software and related documentation, and (ii) the names of * Sam Leffler and Silicon Graphics may not be used in any advertising or * publicity relating to the software without the specific, prior written * permission of Sam Leffler and Silicon Graphics. * * THE SOFTWARE IS PROVIDED "AS-IS" AND WITHOUT WARRANTY OF ANY KIND, * EXPRESS, IMPLIED OR OTHERWISE, INCLUDING WITHOUT LIMITATION, ANY * WARRANTY OF MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE. * * IN NO EVENT SHALL SAM LEFFLER OR SILICON GRAPHICS BE LIABLE FOR * ANY SPECIAL, INCIDENTAL, INDIRECT OR CONSEQUENTIAL DAMAGES OF ANY KIND, * OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, * WHETHER OR NOT ADVISED OF THE POSSIBILITY OF DAMAGE, AND ON ANY THEORY OF * LIABILITY, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE * OF THIS SOFTWARE. */ #include "tiffiop.h" #ifdef LZW_SUPPORT /* * TIFF Library. * Rev 5.0 Lempel-Ziv & Welch Compression Support * * This code is derived from the compress program whose code is * derived from software contributed to Berkeley by James A. Woods, * derived from original work by Spencer Thomas and Joseph Orost. * * The original Berkeley copyright notice appears below in its entirety. */ #include "tif_predict.h" #include <stdbool.h> #include <stdio.h> #include <stdlib.h> /* Select the plausible largest natural integer type for the architecture */ #define SIZEOF_WORDTYPE … WordType; /* * NB: The 5.0 spec describes a different algorithm than Aldus * implements. Specifically, Aldus does code length transitions * one code earlier than should be done (for real LZW). * Earlier versions of this library implemented the correct * LZW algorithm, but emitted codes in a bit order opposite * to the TIFF spec. Thus, to maintain compatibility w/ Aldus * we interpret MSB-LSB ordered codes to be images written w/ * old versions of this library, but otherwise adhere to the * Aldus "off by one" algorithm. * * Future revisions to the TIFF spec are expected to "clarify this issue". */ #define LZW_COMPAT … #define MAXCODE(n) … /* * The TIFF spec specifies that encoded bit * strings range from 9 to 12 bits. */ #define BITS_MIN … #define BITS_MAX … /* predefined codes */ #define CODE_CLEAR … #define CODE_EOI … #define CODE_FIRST … #define CODE_MAX … #define HSIZE … #define HSHIFT … #ifdef LZW_COMPAT /* NB: +1024 is for compatibility with old files */ #define CSIZE … #else #define CSIZE … #endif /* * State block for each open TIFF file using LZW * compression/decompression. Note that the predictor * state block must be first in this data structure. */ LZWBaseState; #define lzw_nbits … #define lzw_maxcode … #define lzw_free_ent … #define lzw_nextdata … #define lzw_nextbits … /* * Encoding-specific state. */ hcode_t; /* codes fit in 16 bits */ hash_t; /* * Decoding-specific state. */ code_t; decodeFunc; LZWCodecState; #define LZWState(tif) … #define DecoderState(tif) … #define EncoderState(tif) … static int LZWDecode(TIFF *tif, uint8_t *op0, tmsize_t occ0, uint16_t s); #ifdef LZW_COMPAT static int LZWDecodeCompat(TIFF *tif, uint8_t *op0, tmsize_t occ0, uint16_t s); #endif static void cl_hash(LZWCodecState *); /* * LZW Decoder. */ static int LZWFixupTags(TIFF *tif) { … } static int LZWSetupDecode(TIFF *tif) { … } /* * Setup state for decoding a strip. */ static int LZWPreDecode(TIFF *tif, uint16_t s) { … } /* * Decode a "hunk of data". */ /* Get the next 32 or 64-bit from the input data */ #ifdef WORDS_BIGENDIAN #define GetNextData … #elif SIZEOF_WORDTYPE == 8 #if defined(__GNUC__) && defined(__x86_64__) #define GetNextData(nextdata, bp) … #elif defined(_M_X64) #define GetNextData … #elif defined(__GNUC__) #define GetNextData … #else #define GetNextData … #endif #elif SIZEOF_WORDTYPE == 4 #if defined(__GNUC__) && defined(__i386__) #define GetNextData … #elif defined(_M_X86) #define GetNextData … #elif defined(__GNUC__) #define GetNextData … #else #define GetNextData … #endif #else #error "Unhandled SIZEOF_WORDTYPE" #endif #define GetNextCodeLZW() … static int LZWDecode(TIFF *tif, uint8_t *op0, tmsize_t occ0, uint16_t s) { … } #ifdef LZW_COMPAT /* * This check shouldn't be necessary because each * strip is suppose to be terminated with CODE_EOI. */ #define NextCode(_tif, _sp, _bp, _code, _get, dec_bitsleft) … /* * Decode a "hunk of data" for old images. */ #define GetNextCodeCompat(sp, bp, code) … static int LZWDecodeCompat(TIFF *tif, uint8_t *op0, tmsize_t occ0, uint16_t s) { … } #endif /* LZW_COMPAT */ /* * LZW Encoding. */ static int LZWSetupEncode(TIFF *tif) { … } /* * Reset encoding state at the start of a strip. */ static int LZWPreEncode(TIFF *tif, uint16_t s) { … } #define CALCRATIO(sp, rat) … /* Explicit 0xff masking to make icc -check=conversions happy */ #define PutNextCode(op, c) … /* * Encode a chunk of pixels. * * Uses an open addressing double hashing (no chaining) on the * prefix code/next character combination. We do a variant of * Knuth's algorithm D (vol. 3, sec. 6.4) along with G. Knott's * relatively-prime secondary probe. Here, the modular division * first probe is gives way to a faster exclusive-or manipulation. * Also do block compression with an adaptive reset, whereby the * code table is cleared when the compression ratio decreases, * but after the table fills. The variable-length output codes * are re-sized at this point, and a CODE_CLEAR is generated * for the decoder. */ static int LZWEncode(TIFF *tif, uint8_t *bp, tmsize_t cc, uint16_t s) { … } /* * Finish off an encoded strip by flushing the last * string and tacking on an End Of Information code. */ static int LZWPostEncode(TIFF *tif) { … } /* * Reset encoding hash table. */ static void cl_hash(LZWCodecState *sp) { … } static void LZWCleanup(TIFF *tif) { … } int TIFFInitLZW(TIFF *tif, int scheme) { … } /* * Copyright (c) 1985, 1986 The Regents of the University of California. * All rights reserved. * * This code is derived from software contributed to Berkeley by * James A. Woods, derived from original work by Spencer Thomas * and Joseph Orost. * * Redistribution and use in source and binary forms are permitted * provided that the above copyright notice and this paragraph are * duplicated in all such forms and that any documentation, * advertising materials, and other materials related to such * distribution and use acknowledge that the software was developed * by the University of California, Berkeley. The name of the * University may not be used to endorse or promote products derived * from this software without specific prior written permission. * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. */ #endif /* LZW_SUPPORT */