80#define LSTATE_INIT_DECODE 0x01
81#define LSTATE_INIT_ENCODE 0x02
89#define GetZSTDState(tif) ((ZSTDState *)(tif)->tif_data)
90#define ZSTDDecoderState(tif) GetZSTDState(tif)
91#define ZSTDEncoderState(tif) GetZSTDState(tif)
160 static const char module[] = "ZSTDPreDecode";
170 if (sp->dstream == NULL)
172 sp->dstream = ZSTD_createDStream();
173 if (sp->dstream == NULL)
175 TIFFErrorExtR(tif, module,
"Cannot allocate decompression stream");
180 zstd_ret = ZSTD_initDStream(sp->dstream);
181 if (ZSTD_isError(zstd_ret))
183 TIFFErrorExtR(tif, module,
"Error in ZSTD_initDStream(): %s",
184 ZSTD_getErrorName(zstd_ret));
214 static const char module[] = "ZSTDDecode";
216 ZSTD_inBuffer in_buffer;
217 ZSTD_outBuffer out_buffer;
229 out_buffer.size = (size_t)occ;
234 zstd_ret = ZSTD_decompressStream(sp->dstream, &out_buffer, &in_buffer);
235 if (ZSTD_isError(zstd_ret))
237 memset(op + out_buffer.pos, 0, out_buffer.size - out_buffer.pos);
238 TIFFErrorExtR(tif, module,
"Error in ZSTD_decompressStream(): %s",
239 ZSTD_getErrorName(zstd_ret));
242 }
while (zstd_ret != 0 && in_buffer.pos < in_buffer.size &&
243 out_buffer.pos < out_buffer.size);
245 if (out_buffer.pos < (
size_t)occ)
247 memset(op + out_buffer.pos, 0, out_buffer.size - out_buffer.pos);
249 "Not enough data at scanline %lu (short %lu bytes)",
251 (
unsigned long)((
size_t)occ - out_buffer.pos));
310 static const char module[] = "ZSTDPreEncode";
319 if (sp->cstream == NULL)
321 sp->cstream = ZSTD_createCStream();
322 if (sp->cstream == NULL)
324 TIFFErrorExtR(tif, module,
"Cannot allocate compression stream");
329 zstd_ret = ZSTD_initCStream(sp->cstream, sp->compression_level);
330 if (ZSTD_isError(zstd_ret))
332 TIFFErrorExtR(tif, module,
"Error in ZSTD_initCStream(): %s",
333 ZSTD_getErrorName(zstd_ret));
339 sp->out_buffer.pos = 0;
368 static const char module[] = "ZSTDEncode";
370 ZSTD_inBuffer in_buffer;
377 "ZSTD codec state is not initialized for encoding");
384 in_buffer.size = (size_t)cc;
390 ZSTD_compressStream(sp->cstream, &sp->out_buffer, &in_buffer);
391 if (ZSTD_isError(zstd_ret))
393 TIFFErrorExtR(tif, module,
"Error in ZSTD_compressStream(): %s",
394 ZSTD_getErrorName(zstd_ret));
397 if (sp->out_buffer.pos == sp->out_buffer.size)
403 sp->out_buffer.pos = 0;
405 }
while (in_buffer.pos < in_buffer.size);
430 static const char module[] = "ZSTDPostEncode";
437 "ZSTD codec state is not initialized for encoding");
443 zstd_ret = ZSTD_endStream(sp->cstream, &sp->out_buffer);
444 if (ZSTD_isError(zstd_ret))
447 ZSTD_getErrorName(zstd_ret));
450 if (sp->out_buffer.pos > 0)
456 sp->out_buffer.pos = 0;
458 }
while (zstd_ret != 0);
522 static const char module[] = "ZSTDVSetField";
528 sp->compression_level = (int)va_arg(ap,
int);
529 if (sp->compression_level <= 0 ||
530 sp->compression_level > ZSTD_maxCLevel())
533 "ZSTD_LEVEL should be between 1 and %d",
538 return (*sp->vsetparent)(tif, tag, ap);
582 FALSE,
"ZSTD compression_level", NULL},
605 static const char module[] = "TIFFInitZSTD";
616 TIFFErrorExtR(tif, module,
"Merging ZSTD codec-specific tags failed");
637 sp->compression_level = 9;
641 sp->out_buffer.dst = NULL;
642 sp->out_buffer.size = 0;
643 sp->out_buffer.pos = 0;
void _TIFFSetDefaultCompressionState(TIFF *tif)
Install fallback codec callbacks and reset raw-access flags.
int _TIFFMergeFields(TIFF *tif, const TIFFField info[], uint32_t n)
Merge borrowed field definitions into the lookup table.
#define FIELD_PSEUDO
Share bit zero with FIELD_IGNORE for pseudo-tags.
@ TIFF_SETGET_INT
Promoted int scalar representation.
int TIFFFlushData1(TIFF *tif)
Append buffered encoded bytes without invoking postencode recursively.
void _TIFFfreeExt(TIFF *tif, void *p)
Release a handle-aware allocation and its payload charge.
void * _TIFFmallocExt(TIFF *tif, tmsize_t s)
Allocate uninitialised bytes under a handle's allocation policy.
int TIFFPredictorCleanup(TIFF *tif)
Restore saved Predictor tag print and setup hooks.
int TIFFPredictorInit(TIFF *tif)
Install Predictor tag and setup hooks around a codec.
#define TRUE
Represent the legacy internal true value.
#define FALSE
Represent the legacy internal false value.
#define TIFFArrayCount(a)
Compute the element count of an actual array.
static int ZSTDDecode(TIFF *tif, uint8_t *op, tmsize_t occ, uint16_t s)
Decompress cached ZSTD input into caller output.
static int ZSTDPreEncode(TIFF *tif, uint16_t s)
Create and initialise a ZSTD compression stream for a strile.
static int ZSTDPostEncode(TIFF *tif)
Finish the ZSTD frame and flush its remaining output.
static int ZSTDFixupTags(TIFF *tif)
Accept ZSTD tag fixup without changes.
static int ZSTDSetupDecode(TIFF *tif)
Switch ZSTD state into decode mode.
#define ZSTDDecoderState(tif)
Alias of GetZSTDState(), returning the ZSTDState for decoding.
static int ZSTDEncode(TIFF *tif, uint8_t *bp, tmsize_t cc, uint16_t s)
Compress input through the active ZSTD stream.
int TIFFInitZSTD(TIFF *tif, int scheme)
Allocate ZSTD state and install predictor-aware streaming methods.
static uint64_t ZSTDGetMaxCompressionRatio(TIFF *tif)
Report the ZSTD expansion allocation heuristic.
#define LSTATE_INIT_ENCODE
Bit 0x02 of the codec state field: encoding has been set up.
static int ZSTDVSetField(TIFF *tif, uint32_t tag, va_list ap)
Store ZSTD compression level or delegate another tag assignment.
#define LSTATE_INIT_DECODE
Bit 0x01 of the codec state field: decoding has been set up.
static int ZSTDVGetField(TIFF *tif, uint32_t tag, va_list ap)
Retrieve ZSTD compression level or delegate another tag query.
#define ZSTDEncoderState(tif)
Alias of GetZSTDState(), returning the ZSTDState for encoding.
static const TIFFField ZSTDFields[]
Define the ZSTD compression-level pseudo-tag.
static int ZSTDPreDecode(TIFF *tif, uint16_t s)
Create or reset the ZSTD decompression stream for a strile.
#define GetZSTDState(tif)
Cast tif->tif_data to the ZSTDState.
static int ZSTDSetupEncode(TIFF *tif)
Switch ZSTD state into encode mode.
static void ZSTDCleanup(TIFF *tif)
Release ZSTD streams state and chained codec methods.
#define TIFF_ANY
Match any data type when searching for a field descriptor.
void TIFFWarningExtR(TIFF *tif, const char *module, const char *fmt,...)
Dispatch a warning diagnostic to the configured handlers.
void TIFFErrorExtR(TIFF *tif, const char *module, const char *fmt,...)
Dispatch a error diagnostic to the configured handlers.
int(* TIFFVGetMethod)(TIFF *tif, uint32_t tag, va_list ap)
Implement a handle's variadic tag getter.
int(* TIFFVSetMethod)(TIFF *tif, uint32_t tag, va_list ap)
Implement a handle's variadic tag setter.
TIFF_SSIZE_T tmsize_t
Represent a signed memory or I/O size in bytes.
uint32_t td_row
Current scanline index; UINT32_MAX before traversal starts.
Prefix codec state with Predictor method chains and row geometry.
TIFFVGetMethod vgetfield
Variadic tag-query implementation for the handle.
TIFFVSetMethod vsetfield
Variadic tag-setting implementation for the handle.
Own ZSTD streams predictor prefix and output cursor.
int state
Decode/encode initialisation flags; switching modes releases the old stream.
ZSTD_DStream * dstream
Owned dependency decompression stream; released by ZSTD_freeDStream.
TIFFPredictorState predict
Predictor prefix, first in the codec state.
int compression_level
Cached level applied on the next compression-stream initialisation.
TIFFVGetMethod vgetparent
Borrowed parent tag getter restored during cleanup.
ZSTD_outBuffer out_buffer
Dependency output descriptor borrowing the handle raw buffer.
TIFFVSetMethod vsetparent
Borrowed parent tag setter restored during cleanup.
ZSTD_CStream * cstream
Owned dependency compression stream; released by ZSTD_freeCStream.
Describe a field's file type and internal argument contract.
Hold private state for one open TIFF library instance.
TIFFCodeMethod tif_encodestrip
Codec callback encoding strip data.
TIFFCodeMethod tif_encodetile
Codec callback encoding tile data.
TIFFTagMethods tif_tagmethods
Inline table of directory field and print callbacks.
TIFFPreMethod tif_preencode
Codec callback preparing a row, strip, or tile encode.
TIFFBoolMethod tif_fixuptags
Codec callback for directory-dependent tag corrections.
tmsize_t tif_rawcc
Raw-buffer byte count interpreted according to read/write state.
TIFFPreMethod tif_predecode
Codec callback preparing a row, strip, or tile decode.
TIFFCodeMethod tif_decodestrip
Codec callback decoding strip data.
TIFFGetMaxCompressionRatioMethod tif_getmaxcompressionratio
Codec callback returning its maximum compression ratio.
TIFFCodeMethod tif_decoderow
Codec callback decoding a scanline.
TIFFBoolMethod tif_setupencode
Codec setup callback run before preencode.
TIFFDirectory tif_dir
Inline representation of the current image directory.
TIFFBoolMethod tif_postencode
Codec callback completing buffered encoding.
TIFFCodeMethod tif_encoderow
Codec callback encoding a scanline.
TIFFVoidMethod tif_cleanup
Codec callback releasing codec-private state.
uint8_t * tif_data
Codec-private storage; allocation and cleanup follow the selected codec.
uint8_t * tif_rawcp
Borrowed current-position pointer into the raw buffer.
TIFFBoolMethod tif_setupdecode
Codec setup callback run before predecode.
uint8_t * tif_rawdata
Raw buffer; ownership/mapping follow TIFF_MYBUFFER and TIFF_BUFFERMMAP.
tmsize_t tif_rawdatasize
Raw-buffer capacity in bytes; kept in step with tif_rawdata, including zero whenever tif_rawdata is N...
TIFFCodeMethod tif_decodetile
Codec callback decoding tile data.
Define the predictor prefix and method-chain contracts.
Declare private library state, callbacks, and implementation helpers.