LibTIFF 4.7.2
TIFF library public API and implementation reference
Loading...
Searching...
No Matches
Data Structures | Macros | Typedefs | Functions

Manage TIFF LZW dictionaries bit streams and predictor-aware transfers. More...

Collaboration diagram for LZW codec:

Data Structures

struct  LZWBaseState
 Prefix LZW state with Predictor hooks and shared bit-code state. More...
 
struct  hash_t
 Pair an encoder hash key with its dictionary code. More...
 
struct  code_ent
 Link one decoder string to its preceding dictionary string. More...
 
struct  LZWCodecState
 Own LZW dictionaries hash storage and transfer restart state. More...
 

Macros

#define SIZEOF_WORDTYPE   SIZEOF_SIZE_T
 Size in bytes of WordType, the decoder bit-buffer word.
 
#define LZW_COMPAT   /* include backwards compatibility code */
 Include decoding of historical reversed-bit-order LZW data.
 
#define MAXCODE(n)   ((1 << (n)) - 1)
 Compute the inclusive maximum code for a valid bit width.
 
#define BITS_MIN   9 /* start with 9 bits */
 Initial and post-clear code width: 9 bits.
 
#define BITS_MAX   12 /* max of 12 bit strings */
 Maximum code width allowed by TIFF LZW: 12 bits.
 
#define CODE_CLEAR   256 /* code to clear string table */
 Reserved code 256 that resets the string table.
 
#define CODE_EOI   257 /* end-of-information code */
 Reserved code 257 that ends the strip or tile.
 
#define CODE_FIRST   258 /* first free code entry */
 First dictionary code assigned to a new string: 258.
 
#define CODE_MAX   MAXCODE(BITS_MAX)
 Largest 12-bit code: MAXCODE(BITS_MAX), i.e. 4095.
 
#define HSIZE   9001 /* 91% occupancy */
 Number of entries in the encoder hash table: 9001.
 
#define HSHIFT   (13 - 8)
 Left shift applied to the input byte when hashing encoder strings.
 
#define CSIZE   (MAXCODE(BITS_MAX) + 1024L)
 Number of entries allocated for the decoder string table.
 
#define lzw_nbits   base.nbits
 Member alias for base.nbits, the current code width.
 
#define lzw_maxcode   base.maxcode
 Member alias for base.maxcode, the largest code at this width.
 
#define lzw_free_ent   base.free_ent
 Member alias for base.free_ent, the next free dictionary code.
 
#define lzw_nextdata   base.nextdata
 Member alias for base.nextdata, the buffered code bits.
 
#define lzw_nextbits   base.nextbits
 Member alias for base.nextbits, the number of buffered bits.
 
#define CHECK_GAP   10000
 Encoder input bytes between compression-ratio checks: 10000.
 
#define LZWState(tif)   ((LZWBaseState *)(tif)->tif_data)
 Cast tif->tif_data to the shared LZWBaseState prefix.
 
#define LZWDecoderState(tif)   ((LZWCodecState *)LZWState(tif))
 Cast tif->tif_data to the full LZWCodecState for decoding.
 
#define LZWEncoderState(tif)   ((LZWCodecState *)LZWState(tif))
 Cast tif->tif_data to the full LZWCodecState for encoding.
 
#define GetNextData(nextdata, bp)
 Load SIZEOF_WORDTYPE bytes at bp into nextdata, first byte most significant.
 
#define GetNextCodeLZW()
 Extract the next nbits-wide MSB-first code from the decoder bit cache into code, refilling from bp as needed.
 
#define NextCode(_tif, _sp, _bp, _code, _get, dec_bitsleft)
 Fetch the next compatibility-mode code with _get, or yield CODE_EOI with a warning when fewer than nbits input bits remain.
 
#define GetNextCodeCompat(sp, bp, code)
 Extract the next nbits-wide LSB-first code for old reversed-order LZW data.
 
#define CALCRATIO(sp, rat)
 Compute the encoder compression ratio incount/outcount as a 24.8 fixed-point value into rat.
 
#define PutNextCode(op, c)
 Append the nbits-wide code c MSB-first to the encoder output at op.
 

Typedefs

typedef size_t WordType
 Select the native size_t word for buffered LZW bits.
 
typedef uint16_t hcode_t
 Store an encoder dictionary code in sixteen bits.
 
typedef struct code_ent code_t
 Link one decoder string to its preceding dictionary string.
 
typedef int(* decodeFunc) (TIFF *tif, uint8_t *buf, tmsize_t size, uint16_t sample)
 Signature shared by the LZW decode entry points (recorded in dec_decode).
 

Functions

static int LZWFixupTags (TIFF *tif)
 Accept LZW tag fixup without changes.
 
static int LZWSetupDecode (TIFF *tif)
 Allocate and initialise the LZW decode dictionary.
 
static int LZWPreDecode (TIFF *tif, uint16_t s)
 Reset LZW bit and dictionary state for a strile.
 
static int LZWDecode (TIFF *tif, uint8_t *op0, tmsize_t occ0, uint16_t s)
 Expand standard TIFF LZW codes into caller output.
 
static int LZWDecodeCompat (TIFF *tif, uint8_t *op0, tmsize_t occ0, uint16_t s)
 Expand historical reversed-order LZW codes into caller output.
 
static int LZWSetupEncode (TIFF *tif)
 Allocate the LZW encoder hash table.
 
static int LZWPreEncode (TIFF *tif, uint16_t s)
 Reset the LZW encoder dictionary bit cache and counters.
 
static int LZWEncode (TIFF *tif, uint8_t *bp, tmsize_t cc, uint16_t s)
 Compress borrowed bytes into buffered TIFF LZW codes.
 
static int LZWPostEncode (TIFF *tif)
 Emit the final LZW string end code and padding bits.
 
static void cl_hash (LZWCodecState *sp)
 Mark every LZW encoder hash slot unused.
 
static void LZWCleanup (TIFF *tif)
 Release LZW tables and codec state and reset methods.
 
static uint64_t LZWGetMaxCompressionRatio (TIFF *tif)
 Report the empirical LZW expansion heuristic.
 
int TIFFInitLZW (TIFF *tif, int scheme)
 Allocate LZW codec state and install predictor-aware methods.
 

Detailed Description

Manage TIFF LZW dictionaries bit streams and predictor-aware transfers.

Macro Definition Documentation

◆ BITS_MAX

#define BITS_MAX   12 /* max of 12 bit strings */

Maximum code width allowed by TIFF LZW: 12 bits.

Definition at line 78 of file tif_lzw.c.

◆ BITS_MIN

#define BITS_MIN   9 /* start with 9 bits */

Initial and post-clear code width: 9 bits.

Definition at line 77 of file tif_lzw.c.

◆ CALCRATIO

#define CALCRATIO (   sp,
  rat 
)
Value:
{ \
if (incount > 0x007fffff) \
{ /* NB: shift will overflow */ \
rat = outcount >> 8; \
rat = (rat == 0 ? 0x7fffffff : incount / rat); \
} \
else \
rat = (incount << 8) / outcount; \
}

Compute the encoder compression ratio incount/outcount as a 24.8 fixed-point value into rat.

Reads the LZWEncode() locals incount (input bytes) and outcount (output bits); the sp operand is not used. If incount > 0x007fffff it divides incount by outcount >> 8 to avoid overflowing the shift, giving 0x7fffffff when that is 0; otherwise it computes (incount << 8) / outcount, which requires outcount != 0. rat is assigned up to twice. No hidden control flow or diagnostics.

Parameters
[in]spUnused.
[in,out]ratLvalue that receives the ratio.

Definition at line 1344 of file tif_lzw.c.

◆ CHECK_GAP

#define CHECK_GAP   10000

Encoder input bytes between compression-ratio checks: 10000.

If the ratio has not improved since the previous check, the encoder emits CODE_CLEAR and resets the table.

Definition at line 247 of file tif_lzw.c.

◆ CODE_CLEAR

#define CODE_CLEAR   256 /* code to clear string table */

Reserved code 256 that resets the string table.

Definition at line 80 of file tif_lzw.c.

◆ CODE_EOI

#define CODE_EOI   257 /* end-of-information code */

Reserved code 257 that ends the strip or tile.

Definition at line 81 of file tif_lzw.c.

◆ CODE_FIRST

#define CODE_FIRST   258 /* first free code entry */

First dictionary code assigned to a new string: 258.

Definition at line 82 of file tif_lzw.c.

◆ CODE_MAX

#define CODE_MAX   MAXCODE(BITS_MAX)

Largest 12-bit code: MAXCODE(BITS_MAX), i.e. 4095.

Definition at line 83 of file tif_lzw.c.

◆ CSIZE

#define CSIZE   (MAXCODE(BITS_MAX) + 1024L)

Number of entries allocated for the decoder string table.

MAXCODE(BITS_MAX) + 1024 with LZW_COMPAT (headroom for old files), otherwise MAXCODE(BITS_MAX) + 1.

Definition at line 88 of file tif_lzw.c.

◆ GetNextCodeCompat

#define GetNextCodeCompat (   sp,
  bp,
  code 
)
Value:
{ \
nextdata |= (unsigned long)*(bp)++ << nextbits; \
nextbits += 8; \
if (nextbits < nbits) \
{ \
nextdata |= (unsigned long)*(bp)++ << nextbits; \
nextbits += 8; \
} \
code = (hcode_t)(nextdata & (unsigned long)nbitsmask); \
nextdata >>= nbits; \
nextbits -= nbits; \
}
uint16_t hcode_t
Store an encoder dictionary code in sixteen bits.
Definition tif_lzw.c:138

Extract the next nbits-wide LSB-first code for old reversed-order LZW data.

ORs one byte, or two if nextbits is still below nbits, from *bp into the caller locals nextdata/nextbits, advancing bp each time; then assigns code = nextdata & nbitsmask and shifts nbits out of nextdata. Reads the caller locals nbits and nbitsmask. Performs no bounds check: NextCode() ensures dec_bitsleft >= nbits first. The sp operand is not used.

Parameters
[in]spUnused; present for the NextCode() _get signature.
[in,out]bpInput byte-pointer lvalue, advanced by 1 or 2.
[in,out]codeLvalue that receives the code.

Definition at line 956 of file tif_lzw.c.

◆ GetNextCodeLZW

#define GetNextCodeLZW ( )

Extract the next nbits-wide MSB-first code from the decoder bit cache into code, refilling from bp as needed.

Operates entirely on locals of LZWDecode(): reads nbits and nbitsmask, reads and updates nextbits, nextdata, bp and dec_bitsleft, and assigns code. When the cache runs short it loads a whole word with GetNextData() if dec_bitsleft holds at least one word, otherwise one or two single bytes. dec_bitsleft is reduced by the bits loaded, not by nbits.

Hidden control flow: if fewer than 8 bits remain in dec_bitsleft when a byte is needed, it executes goto no_eoi, so the enclosing function must define a no_eoi label (LZWDecode() zeroes the output, sets read_error, reports a missing EOI with TIFFErrorExtR() and returns 0 there). Its internal break only leaves the macro's own do/while.

Definition at line 506 of file tif_lzw.c.

◆ GetNextData

#define GetNextData (   nextdata,
  bp 
)
Value:
nextdata = (((uint64_t)bp[0]) << 56) | (((uint64_t)bp[1]) << 48) | \
(((uint64_t)bp[2]) << 40) | (((uint64_t)bp[3]) << 32) | \
(((uint64_t)bp[4]) << 24) | (((uint64_t)bp[5]) << 16) | \
(((uint64_t)bp[6]) << 8) | (((uint64_t)bp[7]))

Load SIZEOF_WORDTYPE bytes at bp into nextdata, first byte most significant.

Statement macro used by GetNextCodeLZW() to refill the bit cache with a whole word. Assigns nextdata and reads SIZEOF_WORDTYPE bytes from bp without a bounds check; does not advance bp. The portable variants evaluate bp once per byte, and the memcpy/bswap variants expand to two statements, so bp must be free of side effects and the macro must be used inside braces. The _M_X64/_M_X86 variants dereference bp through a wider pointer type, i.e. an unaligned load.

Parameters
[in,out]nextdataUnsigned WordType lvalue that receives the word.
[in]bpByte pointer to at least SIZEOF_WORDTYPE readable bytes.

Definition at line 483 of file tif_lzw.c.

◆ HSHIFT

#define HSHIFT   (13 - 8)

Left shift applied to the input byte when hashing encoder strings.

Value 5 (13 - 8), so (byte << HSHIFT) ^ code stays within 13 bits.

Definition at line 85 of file tif_lzw.c.

◆ HSIZE

#define HSIZE   9001 /* 91% occupancy */

Number of entries in the encoder hash table: 9001.

Upstream notes this as 91% occupancy. Indexed with HSHIFT-mixed keys and probed with a secondary displacement.

Definition at line 84 of file tif_lzw.c.

◆ LZW_COMPAT

#define LZW_COMPAT   /* include backwards compatibility code */

Include decoding of historical reversed-bit-order LZW data.

Defined unconditionally. Enables LZWDecodeCompat(), its detection in LZWPreDecode() and 1024 extra decode-table entries in CSIZE.

Definition at line 70 of file tif_lzw.c.

◆ lzw_free_ent

#define lzw_free_ent   base.free_ent

Member alias for base.free_ent, the next free dictionary code.

Definition at line 127 of file tif_lzw.c.

◆ lzw_maxcode

#define lzw_maxcode   base.maxcode

Member alias for base.maxcode, the largest code at this width.

Definition at line 126 of file tif_lzw.c.

◆ lzw_nbits

#define lzw_nbits   base.nbits

Member alias for base.nbits, the current code width.

Definition at line 125 of file tif_lzw.c.

◆ lzw_nextbits

#define lzw_nextbits   base.nextbits

Member alias for base.nextbits, the number of buffered bits.

Definition at line 129 of file tif_lzw.c.

◆ lzw_nextdata

#define lzw_nextdata   base.nextdata

Member alias for base.nextdata, the buffered code bits.

Definition at line 128 of file tif_lzw.c.

◆ LZWDecoderState

#define LZWDecoderState (   tif)    ((LZWCodecState *)LZWState(tif))

Cast tif->tif_data to the full LZWCodecState for decoding.

Pure cast via LZWState(); tif is evaluated once. Requires the LZW codec to be initialised.

Parameters
[in]tifHandle whose codec state is returned.

Definition at line 261 of file tif_lzw.c.

◆ LZWEncoderState

#define LZWEncoderState (   tif)    ((LZWCodecState *)LZWState(tif))

Cast tif->tif_data to the full LZWCodecState for encoding.

Pure cast via LZWState(); tif is evaluated once. Requires the LZW codec to be initialised.

Parameters
[in]tifHandle whose codec state is returned.

Definition at line 262 of file tif_lzw.c.

◆ LZWState

#define LZWState (   tif)    ((LZWBaseState *)(tif)->tif_data)

Cast tif->tif_data to the shared LZWBaseState prefix.

Pure cast; tif is evaluated once. Requires the LZW codec to be initialised (tif_data non-NULL).

Parameters
[in]tifHandle whose codec state is returned.

Definition at line 260 of file tif_lzw.c.

◆ MAXCODE

#define MAXCODE (   n)    ((1 << (n)) - 1)

Compute the inclusive maximum code for a valid bit width.

Expands to ((1 << n) - 1); pure, with n evaluated once.

Parameters
[in]nCode width in bits.

Definition at line 72 of file tif_lzw.c.

◆ NextCode

#define NextCode (   _tif,
  _sp,
  _bp,
  _code,
  _get,
  dec_bitsleft 
)
Value:
{ \
if (dec_bitsleft < (uint64_t)nbits) \
{ \
TIFFWarningExtR(_tif, module, \
"LZWDecode: Strip %" PRIu32 \
" not terminated with EOI code", \
_tif->tif_dir.td_curstrip); \
_code = CODE_EOI; \
} \
else \
{ \
_get(_sp, _bp, _code); \
dec_bitsleft -= (uint64_t)nbits; \
} \
}
#define CODE_EOI
Reserved code 257 that ends the strip or tile.
Definition tif_lzw.c:81

Fetch the next compatibility-mode code with _get, or yield CODE_EOI with a warning when fewer than nbits input bits remain.

Defined only with LZW_COMPAT and used by LZWDecodeCompat(). Also reads the caller locals nbits and module. If dec_bitsleft < nbits it emits TIFFWarningExtR() ("not terminated with EOI code") naming _tif->tif_dir.td_curstrip, sets _code to CODE_EOI and consumes no input; this is not treated as an error. Otherwise it expands _get(_sp, _bp, _code) and subtracts nbits from dec_bitsleft. _tif is evaluated twice on the warning path. No hidden return or goto.

Parameters
[in]_tifTIFF handle used for the warning.
[in]_spCodec state passed through to _get.
[in,out]_bpInput byte-pointer lvalue, advanced by _get.
[in,out]_codeLvalue that receives the code.
[in]_getCode-fetch macro name, e.g. GetNextCodeCompat.
[in,out]dec_bitsleftLvalue holding the remaining input bit count.

Definition at line 936 of file tif_lzw.c.

◆ PutNextCode

#define PutNextCode (   op,
  c 
)
Value:
{ \
nextdata = (nextdata << nbits) | c; \
nextbits += nbits; \
*op++ = (unsigned char)((nextdata >> (nextbits - 8)) & 0xff); \
nextbits -= 8; \
if (nextbits >= 8) \
{ \
*op++ = (unsigned char)((nextdata >> (nextbits - 8)) & 0xff); \
nextbits -= 8; \
} \
outcount += nbits; \
}

Append the nbits-wide code c MSB-first to the encoder output at op.

Shifts c into the LZWEncode()/LZWPostEncode() locals nextdata and nextbits, stores one or two complete bytes at op (advancing it each time) and adds nbits to outcount. Writes without a bounds check: callers compare op with enc_rawlimit (which reserves 4 bytes, room for two codes) and flush first. c is evaluated once; op is read and incremented up to twice. No hidden control flow or diagnostics.

Parameters
[in,out]opOutput byte-pointer lvalue, advanced by 1 or 2.
[in]cCode value, below 1 << nbits.

Definition at line 1356 of file tif_lzw.c.

◆ SIZEOF_WORDTYPE

#define SIZEOF_WORDTYPE   SIZEOF_SIZE_T

Size in bytes of WordType, the decoder bit-buffer word.

Equals SIZEOF_SIZE_T; selects the GetNextData() variant and how many bytes GetNextCodeLZW() loads at once (4 or 8).

Definition at line 50 of file tif_lzw.c.

Typedef Documentation

◆ code_t

typedef struct code_ent code_t

Link one decoder string to its preceding dictionary string.

Element of the CSIZE-entry dec_codetab array owned by LZWCodecState; next links point into the same array.

◆ decodeFunc

typedef int(* decodeFunc) (TIFF *tif, uint8_t *buf, tmsize_t size, uint16_t sample)

Signature shared by the LZW decode entry points (recorded in dec_decode).

Matches LZWDecode() and LZWDecodeCompat(). No pointer of this type is ever called: decoding is dispatched through tif_decoderow, tif_decodestrip and tif_decodetile.

Parameters
[in,out]tifLZW handle being decoded.
[out]bufDecoded output of size bytes.
[in]sizeRequested decoded byte count.
[in]sampleSample plane, ignored by LZW.
Returns
One on filling output, zero on decoding failure.

Definition at line 200 of file tif_lzw.c.

◆ hcode_t

typedef uint16_t hcode_t

Store an encoder dictionary code in sixteen bits.

Definition at line 138 of file tif_lzw.c.

◆ WordType

typedef size_t WordType

Select the native size_t word for buffered LZW bits.

Definition at line 55 of file tif_lzw.c.

Function Documentation

◆ cl_hash()

static void cl_hash ( LZWCodecState *  sp)
static

Mark every LZW encoder hash slot unused.

Requires a live HSIZE-entry table. Sets hash fields to minus one; code fields need not be cleared. No allocation, I/O, diagnostics, ownership changes or error status.

Parameters
[in,out]spNon-NULL codec state owning an allocated HSIZE-entry encoder table.

Definition at line 1662 of file tif_lzw.c.

References LZWCodecState::enc_hashtab, hash_t::hash, and HSIZE.

Referenced by LZWEncode(), and LZWPreEncode().

Here is the caller graph for this function:

◆ LZWCleanup()

static void LZWCleanup ( TIFF *  tif)
static

Release LZW tables and codec state and reset methods.

Requires live initialised LZW state. Restores predictor chains, frees dictionary and hash table if present, frees outer state and sets tif_data NULL, then installs default compression methods. Invalidates all state/table views. Does not flush payload or close the borrowed handle; no separate status result.

Parameters
[in,out]tifNon-NULL live LZW handle with valid state, raw-buffer cursors and matching geometry; serialise access.

Definition at line 1699 of file tif_lzw.c.

References _TIFFfreeExt(), _TIFFSetDefaultCompressionState(), LZWDecoderState, LZWEncoderState, tiff::tif_data, and TIFFPredictorCleanup().

Referenced by TIFFInitLZW().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ LZWDecode()

static int LZWDecode ( TIFF *  tif,
uint8_t *  op0,
tmsize_t  occ0,
uint16_t  s 
)
static

Expand standard TIFF LZW codes into caller output.

Requires a nonnegative writable output extent and coherent predecode dictionary/bit state. Supports interrupted-string restart across calls, clear/end codes and TIFF early code-width changes. Updates dictionary, bit cache and raw cursors on normal exits. Detected corrupt/truncated input clears the unfilled tail, latches read_error, diagnoses and returns zero; some error exits do not commit local cursor state. Further calls in the same strile clear requested output and fail until predecode resets the latch. No allocation/client I/O or ownership transfer.

Parameters
[in,out]tifNon-NULL live LZW handle with valid state, raw-buffer cursors and matching geometry; serialise access.
[out]op0Borrowed writable output of occ0 bytes, disjoint from raw input and codec tables.
[in]occ0Nonnegative requested decoded bytes; use a positive extent during an interrupted-string restart.
[in]sUnused sample plane; predictor wrappers manage channel stride.
Returns
One when requested output is filled, zero on detected decoding error.

Definition at line 571 of file tif_lzw.c.

References BITS_MAX, BITS_MIN, CODE_CLEAR, CODE_EOI, CODE_FIRST, CSIZE, code_ent::firstchar, GetNextCodeLZW, code_ent::length, LZWDecoderState, MAXCODE, code_ent::next, code_ent::repeated, TIFFDirectory::td_curstrip, TIFFDirectory::td_row, tiff::tif_dir, tiff::tif_name, tiff::tif_rawcc, tiff::tif_rawcp, TIFFErrorExtR(), and code_ent::value.

Referenced by LZWPreDecode(), and TIFFInitLZW().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ LZWDecodeCompat()

static int LZWDecodeCompat ( TIFF *  tif,
uint8_t *  op0,
tmsize_t  occ0,
uint16_t  s 
)
static

Expand historical reversed-order LZW codes into caller output.

Requires compatibility predecode selection, nonnegative output extent and valid dictionary/raw state. Maintains restart, bit and dictionary progress across calls. Corrupt dictionary codes and zero-length strings clear the entire requested output (including bytes already decoded), latch read_error, diagnose and return zero without committing local cursor state. Running out of input bits is treated as an end code with only a warning; if output is then short, the unfilled tail is cleared, read_error latched and zero returned. While read_error is latched, further calls clear the requested output and fail until predecode resets it. No allocation/client I/O or ownership transfer.

Parameters
[in,out]tifNon-NULL live LZW handle with valid state, raw-buffer cursors and matching geometry; serialise access.
[out]op0Borrowed writable output of occ0 bytes, disjoint from raw input and codec tables.
[in]occ0Nonnegative requested decoded bytes; use a positive extent during an interrupted-string restart.
[in]sUnused sample plane; predictor wrappers manage channel stride.
Returns
One when requested output is filled, zero on detected decoding error.

Definition at line 996 of file tif_lzw.c.

References _TIFFmemset(), BITS_MAX, BITS_MIN, CODE_CLEAR, CODE_EOI, CODE_FIRST, CSIZE, code_ent::firstchar, GetNextCodeCompat, code_ent::length, LZWDecoderState, MAXCODE, code_ent::next, NextCode, TIFFDirectory::td_row, tiff::tif_dir, tiff::tif_name, tiff::tif_rawcc, tiff::tif_rawcp, TIFFErrorExtR(), and code_ent::value.

Referenced by LZWPreDecode().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ LZWEncode()

static int LZWEncode ( TIFF *  tif,
uint8_t *  bp,
tmsize_t  cc,
uint16_t  s 
)
static

Compress borrowed bytes into buffered TIFF LZW codes.

Requires successful encoder setup/preencode, nonnegative input length and usable raw capacity/limit. Updates code/hash state, emits clear codes and adjusts code width; ratio checkpoints can reset the dictionary. Flushes when needed, invoking client I/O and diagnostics. Input is not changed. Early flush failure can leave output bytes, dictionary and local/cached progress inconsistent for retry; no rollback or ownership transfer.

Parameters
[in,out]tifNon-NULL live LZW handle with valid state, raw-buffer cursors and matching geometry; serialise access.
[in]bpBorrowed readable cc-byte input, not retained.
[in]ccNonnegative input byte count.
[in]sUnused sample plane; predictor wrappers manage channel stride.
Returns
One after encoding, zero for missing state or flush failure.

Definition at line 1403 of file tif_lzw.c.

References BITS_MAX, BITS_MIN, CALCRATIO, CHECK_GAP, cl_hash(), hash_t::code, CODE_CLEAR, CODE_FIRST, CODE_MAX, LZWCodecState::enc_checkpoint, LZWCodecState::enc_hashtab, LZWCodecState::enc_incount, LZWCodecState::enc_oldcode, LZWCodecState::enc_outcount, LZWCodecState::enc_ratio, LZWCodecState::enc_rawlimit, hash_t::hash, HSHIFT, HSIZE, LZWEncoderState, MAXCODE, PutNextCode, tiff::tif_rawcc, tiff::tif_rawcp, tiff::tif_rawdata, and TIFFFlushData1().

Referenced by TIFFInitLZW().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ LZWFixupTags()

static int LZWFixupTags ( TIFF *  tif)
static

Accept LZW tag fixup without changes.

No allocation, I/O, diagnostics, mutation or ownership effects.

Parameters
[in]tifUnused handle.
Returns
One unconditionally.

Definition at line 282 of file tif_lzw.c.

Referenced by TIFFInitLZW().

Here is the caller graph for this function:

◆ LZWGetMaxCompressionRatio()

static uint64_t LZWGetMaxCompressionRatio ( TIFF *  tif)
static

Report the empirical LZW expansion heuristic.

Ignores the handle; no allocation, I/O, diagnostics, state or ownership effects. Not a universal compression bound or data-validation guarantee.

Parameters
[in]tifUnused handle.
Returns
One thousand four hundred.

Definition at line 1727 of file tif_lzw.c.

Referenced by TIFFInitLZW().

Here is the caller graph for this function:

◆ LZWPostEncode()

static int LZWPostEncode ( TIFF *  tif)
static

Emit the final LZW string end code and padding bits.

Requires valid encoder state and reserved raw capacity. Can flush first, emit clear/width changes, append EOI and pad the last byte, then set rawcc. Does not persist the IFD or guarantee final buffered bytes were written. Raw/file state can change before failed flush; no ownership transfer.

Parameters
[in,out]tifNon-NULL live LZW handle with valid state, raw-buffer cursors and matching geometry; serialise access.
Returns
One after final codes buffered, zero on flush failure.

Definition at line 1595 of file tif_lzw.c.

References BITS_MAX, BITS_MIN, CODE_CLEAR, CODE_EOI, CODE_MAX, LZWCodecState::enc_oldcode, LZWCodecState::enc_outcount, LZWCodecState::enc_rawlimit, LZWEncoderState, PutNextCode, tiff::tif_rawcc, tiff::tif_rawcp, tiff::tif_rawdata, and TIFFFlushData1().

Referenced by TIFFInitLZW().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ LZWPreDecode()

static int LZWPreDecode ( TIFF *  tif,
uint16_t  s 
)
static

Reset LZW bit and dictionary state for a strile.

Requires live outer state; lazily attempts setup if needed and rejects a missing dictionary. Tests the first two raw bytes for historical reversed code order on every call and, with LZW_COMPAT (always defined), sets the matching lzw_maxcode convention. Only on the first predecode after codec initialisation (dec_decode still NULL) does it act on the result: for old-style data it warns, installs LZWDecodeCompat() as the row/strip/tile decoder and re-runs setup so that predictor wrappers rebind. The decoder choice is not revisited for later striles: a later strile with the other code order keeps the decoder chosen first (and the warning is not repeated). Without LZW_COMPAT, old-style data fails on every strile but is diagnosed only the first time. The setup statuses are ignored. Clears restart/error state and resets code pointers/bit cache. Does not allocate output or read client bytes directly; setup can allocate/diagnose.

Parameters
[in,out]tifNon-NULL live LZW handle with valid state, raw-buffer cursors and matching geometry; serialise access.
[in]sUnused sample plane; predictor wrappers manage channel stride.
Returns
One after reset, zero for missing dictionary or unavailable compatibility support.

Definition at line 389 of file tif_lzw.c.

References BITS_MIN, LZWDecode(), LZWDecodeCompat(), LZWDecoderState, MAXCODE, tiff::tif_decoderow, tiff::tif_decodestrip, tiff::tif_decodetile, tiff::tif_rawcc, tiff::tif_rawdata, tiff::tif_setupdecode, TIFFErrorExtR(), and TIFFWarningExtR().

Referenced by TIFFInitLZW().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ LZWPreEncode()

static int LZWPreEncode ( TIFF *  tif,
uint16_t  s 
)
static

Reset the LZW encoder dictionary bit cache and counters.

Requires valid outer state and an allocated hash table. A raw output buffer smaller than five bytes, the most LZWEncode() and LZWPostEncode() write after their room checks, is diagnosed and rejected. If table is NULL it invokes setup but does not check its return before clearing the table; caller must ensure setup succeeds. Resets code widths, checkpoint, pending code and output limit. Does not emit payload directly; setup can allocate/diagnose.

Parameters
[in,out]tifNon-NULL live LZW handle with valid state, raw-buffer cursors and matching geometry; serialise access.
[in]sUnused sample plane; predictor wrappers manage channel stride.
Returns
One after reset, zero for a raw buffer smaller than five bytes; not independent validation of setup success.

Definition at line 1297 of file tif_lzw.c.

References BITS_MIN, CHECK_GAP, cl_hash(), CODE_FIRST, LZWEncoderState, MAXCODE, tiff::tif_rawdata, tiff::tif_rawdatasize, tiff::tif_setupencode, TIFF_SSIZE_FORMAT, and TIFFErrorExtR().

Referenced by TIFFInitLZW().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ LZWSetupDecode()

static int LZWSetupDecode ( TIFF *  tif)
static

Allocate and initialise the LZW decode dictionary.

Allocates handle-owned codec state when absent, installs predictor hooks on that path, and lazily allocates CSIZE entries. Preloads literal entries and clears reserved entries. Allocation failure diagnoses and can retain outer state for cleanup; no pixel I/O. Predictor initialisation status is not propagated.

Parameters
[in,out]tifNon-NULL live LZW handle; tif_data may still be NULL, in which case the state is allocated here. Serialise access.
Returns
One when dictionary storage is ready, zero on allocation failure.

Definition at line 302 of file tif_lzw.c.

References _TIFFmallocExt(), CODE_CLEAR, CODE_FIRST, CSIZE, LZWDecoderState, tiff::tif_data, TIFFErrorExtR(), and TIFFPredictorInit().

Referenced by TIFFInitLZW().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ LZWSetupEncode()

static int LZWSetupEncode ( TIFF *  tif)
static

Allocate the LZW encoder hash table.

Requires existing codec state and no previous unreleased encoder table; assigns a new handle-owned HSIZE allocation. Diagnoses allocation failure; no pixel I/O. Cleanup releases the table.

Parameters
[in,out]tifNon-NULL live LZW handle with valid state, raw-buffer cursors and matching geometry; serialise access.
Returns
One after allocation, zero on failure.

Definition at line 1259 of file tif_lzw.c.

References _TIFFmallocExt(), HSIZE, LZWEncoderState, and TIFFErrorExtR().

Referenced by TIFFInitLZW().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ TIFFInitLZW()

int TIFFInitLZW ( TIFF *  tif,
int  scheme 
)
extern

Allocate LZW codec state and install predictor-aware methods.

Requires COMPRESSION_LZW and cleaned-up previous codec state. Allocates handle-owned state, initialises table pointers, installs callbacks and attempts predictor hook installation. PredictorInit status is ignored, so success does not guarantee that field merging succeeded. Encoding methods are omitted in LZW_READ_ONLY builds. Allocation failure diagnoses; partial state/callback effects are not rolled back. No pixel I/O.

Parameters
[in,out]tifNon-NULL handle whose previous codec state has been cleaned up; receives the new codec state. Serialise access.
[in]schemeCOMPRESSION_LZW; asserted in debug builds.
Returns
One after state/method installation, zero on outer-state allocation failure.

Definition at line 1742 of file tif_lzw.c.

References _TIFFmallocExt(), COMPRESSION_LZW, LZWCleanup(), LZWDecode(), LZWDecoderState, LZWEncode(), LZWEncoderState, LZWFixupTags(), LZWGetMaxCompressionRatio(), LZWPostEncode(), LZWPreDecode(), LZWPreEncode(), LZWSetupDecode(), LZWSetupEncode(), LZWState, tiff::tif_cleanup, tiff::tif_data, tiff::tif_decoderow, tiff::tif_decodestrip, tiff::tif_decodetile, tiff::tif_encoderow, tiff::tif_encodestrip, tiff::tif_encodetile, tiff::tif_fixuptags, tiff::tif_getmaxcompressionratio, tiff::tif_mode, tiff::tif_postencode, tiff::tif_predecode, tiff::tif_preencode, tiff::tif_setupdecode, tiff::tif_setupencode, TIFFErrorExtR(), and TIFFPredictorInit().

Here is the call graph for this function: