39#include "webp/decode.h"
40#include "webp/encode.h"
45#define LSTATE_INIT_DECODE 0x01
46#define LSTATE_INIT_ENCODE 0x02
116#define LState(tif) ((WebPState *)(tif)->tif_data)
117#define DecoderState(tif) LState(tif)
118#define EncoderState(tif) LState(tif)
173 const WebPPicture *
const picture)
175 static const char module[] = "TWebPDatasetWriter";
176 TIFF *tif = (
TIFF *)(picture->custom_ptr);
178 if ((tif->tif_rawcc + (
tmsize_t)data_size) > tif->tif_rawdatasize)
182 (
size_t)((uint64_t)tif->tif_rawcc + (uint64_t)data_size -
183 (uint64_t)tif->tif_rawdatasize));
189 tif->tif_rawcc += (
tmsize_t)data_size;
190 tif->tif_rawcp += data_size;
223 static const char module[] = "TWebPEncode";
231 "WebP codec state is not initialized for encoding");
235 if ((uint64_t)sp->buffer_offset + (uint64_t)cc > sp->buffer_size)
241 memcpy(sp->pBuffer + sp->buffer_offset, bp, (
size_t)cc);
242 sp->buffer_offset += (unsigned)cc;
277 static const char module[] = "TWebPDecode";
278 VP8StatusCode status = VP8_STATUS_OK;
280 uint32_t segment_width, segment_height;
281 bool decode_whole_strile =
false;
290 memset(op, 0, (
size_t)occ);
292 "ZIPDecode: Scanline %" PRIu32
" cannot be read due to "
300 uint32_t buffer_size;
304 int webp_width, webp_height;
309 &webp_width, &webp_height))
311 memset(op, 0, (
size_t)occ);
316 if ((uint32_t)webp_width != segment_width ||
317 (uint32_t)webp_height != segment_height)
319 memset(op, 0, (
size_t)occ);
322 tif, module,
"WebP blob dimension is %dx%d. Expected %ux%u",
323 webp_width, webp_height, segment_width, segment_height);
327#if WEBP_DECODER_ABI_VERSION >= 0x0002
328 WebPDecoderConfig config;
329 if (!WebPInitDecoderConfig(&config))
331 memset(op, 0, (
size_t)occ);
333 TIFFErrorExtR(tif, module,
"WebPInitDecoderConfig() failed");
337 const bool bWebPGetFeaturesOK =
342 &config.input) == VP8_STATUS_OK;
344 WebPFreeDecBuffer(&config.output);
346 if (!bWebPGetFeaturesOK)
348 memset(op, 0, (
size_t)occ);
350 TIFFErrorExtR(tif, module,
"WebPInitDecoderConfig() failed");
354 const int webp_bands = config.input.has_alpha ? 4 : 3;
361 !(webp_bands == 3 && sp->
nSamples == 4))
363 memset(op, 0, (
size_t)occ);
366 "WebP blob band count is %d. Expected %d", webp_bands,
372 buffer_size = segment_width * segment_height * sp->
nSamples;
377 decode_whole_strile =
true;
391 memset(op, 0, (
size_t)occ);
422 memset(op, 0, (
size_t)occ);
424 TIFFErrorExtR(tif, module,
"Unable to allocate WebP decoder.");
433 memset(op, 0, (
size_t)occ);
435 TIFFErrorExtR(tif, module,
"Fractional scanlines cannot be read");
441 if (status != VP8_STATUS_OK && status != VP8_STATUS_SUSPENDED)
443 if (status == VP8_STATUS_INVALID_PARAM)
447 else if (status == VP8_STATUS_OUT_OF_MEMORY)
455 memset(op, 0, (
size_t)occ);
461 int current_y, stride;
465 buf = WebPIDecGetRGB(sp->
psDecoder, ¤t_y, NULL, NULL, &stride);
470 const int numberOfExpectedLines =
472 if (decode_whole_strile)
474 if (current_y != numberOfExpectedLines)
476 memset(op, 0, (
size_t)occ);
479 "Unable to decode WebP data: less lines than "
486 memcpy(op, buf + (sp->
last_y * stride), (
size_t)occ);
491 sp->
last_y += numberOfExpectedLines;
493 if (decode_whole_strile)
507 memset(op, 0, (
size_t)occ);
531 static const char module[] = "TWebPFixupTags";
533 "TIFF WEBP requires data to be stored contiguously in "
534 "RGB e.g. RGBRGBRGB "
535#
if WEBP_ENCODER_ABI_VERSION >= 0x0100
562 static const char module[] = "TWebPSetupDecode";
573#
if WEBP_ENCODER_ABI_VERSION >= 0x0100
579 "WEBP driver doesn't support %d bands. Must be 3 (RGB) "
580#
if WEBP_ENCODER_ABI_VERSION >= 0x0100
589 if ((nBitsPerSample != 8) && (sampleFormat != 1))
591 TIFFErrorExtR(tif, module,
"WEBP driver requires 8 bit unsigned data");
635 static const char module[] = "TWebPPreDecode";
636 uint32_t segment_width, segment_height;
643 if (segment_width > 16383 || segment_height > 16383)
646 "WEBP maximum image dimensions are 16383 x 16383.");
686 static const char module[] = "TWebPSetupEncode";
698 "TIFF WEBP requires data to be stored contiguously in "
699 "RGB e.g. RGBRGBRGB "
700#
if WEBP_ENCODER_ABI_VERSION >= 0x0100
709#
if WEBP_ENCODER_ABI_VERSION >= 0x0100
715 "WEBP driver doesn't support %d bands. Must be 3 (RGB) "
716#
if WEBP_ENCODER_ABI_VERSION >= 0x0100
727 TIFFErrorExtR(tif, module,
"WEBP driver requires 8 bit unsigned data");
742 if (!WebPPictureInit(&sp->
sPicture))
744 TIFFErrorExtR(tif, module,
"Error initializing WebP picture.");
748 if (!WebPConfigInitInternal(&sp->
sEncoderConfig, WEBP_PRESET_DEFAULT,
750 WEBP_ENCODER_ABI_VERSION))
753 "Error creating WebP encoder configuration.");
758#if WEBP_ENCODER_ABI_VERSION >= 0x0100
763#if WEBP_ENCODER_ABI_VERSION >= 0x0209
771 TIFFErrorExtR(tif, module,
"Error with WebP encoder configuration.");
803 static const char module[] = "TWebPPreEncode";
804 uint32_t segment_width, segment_height;
815 if (segment_width > 16383 || segment_height > 16383)
818 "WEBP maximum image dimensions are 16383 x 16383.");
840 sp->
sPicture.width = (int)segment_width;
841 sp->
sPicture.height = (int)segment_height;
872 static const char module[] = "TWebPPostEncode";
880 "WebP codec state is not initialized for encoding");
895#if WEBP_ENCODER_ABI_VERSION >= 0x0100
900 TIFFErrorExtR(tif, module,
"WebPPictureImportRGBA() failed");
915#if WEBP_ENCODER_ABI_VERSION >= 0x0100
916 const char *pszErrorMsg = NULL;
919 case VP8_ENC_ERROR_OUT_OF_MEMORY:
920 pszErrorMsg =
"Out of memory";
922 case VP8_ENC_ERROR_BITSTREAM_OUT_OF_MEMORY:
923 pszErrorMsg =
"Out of memory while flushing bits";
925 case VP8_ENC_ERROR_NULL_PARAMETER:
926 pszErrorMsg =
"A pointer parameter is NULL";
928 case VP8_ENC_ERROR_INVALID_CONFIGURATION:
929 pszErrorMsg =
"Configuration is invalid";
931 case VP8_ENC_ERROR_BAD_DIMENSION:
932 pszErrorMsg =
"Picture has invalid width/height";
934 case VP8_ENC_ERROR_PARTITION0_OVERFLOW:
935 pszErrorMsg =
"Partition is bigger than 512k. Try using less "
936 "SEGMENTS, or increase PARTITION_LIMIT value";
938 case VP8_ENC_ERROR_PARTITION_OVERFLOW:
939 pszErrorMsg =
"Partition is bigger than 16M";
941 case VP8_ENC_ERROR_BAD_WRITE:
942 pszErrorMsg =
"Error while fludshing bytes";
944 case VP8_ENC_ERROR_FILE_TOO_BIG:
945 pszErrorMsg =
"File is bigger than 4G";
947 case VP8_ENC_ERROR_USER_ABORT:
948 pszErrorMsg =
"User interrupted";
951 case VP8_ENC_ERROR_LAST:
954 "WebPEncode returned an unknown error code: %u",
956 pszErrorMsg =
"Unknown WebP error type.";
959 TIFFErrorExtR(tif, module,
"WebPEncode() failed : %s", pszErrorMsg);
970 TIFFErrorExtR(tif, module,
"Error flushing TIFF WebP encoder.");
1047 static const char module[] = "TWebPVSetField";
1053 sp->quality_level = (int)va_arg(ap,
int);
1054 if (sp->quality_level <= 0 || sp->quality_level > 100)
1057 "WEBP_LEVEL should be between 1 and 100");
1061#if WEBP_ENCODER_ABI_VERSION >= 0x0100
1062 sp->lossless = va_arg(ap,
int);
1065 sp->quality_level = 100;
1071 "Need to upgrade WEBP driver, this version doesn't support "
1072 "lossless compression.");
1076#if WEBP_ENCODER_ABI_VERSION >= 0x0209
1077 sp->lossless_exact = va_arg(ap,
int);
1082 "Need to upgrade WEBP driver, this version doesn't support "
1083 "lossless compression.");
1087 return (*sp->vsetparent)(tif, tag, ap);
1138 FALSE,
"WEBP quality", NULL},
1140 TRUE,
FALSE,
"WEBP lossless/lossy", NULL},
1169 static const char module[] = "TIFFInitWebP";
1180 TIFFErrorExtR(tif, module,
"Merging WebP codec-specific tags failed");
1201 sp->quality_level = 75;
1203 sp->lossless_exact = 1;
1206 sp->psDecoder = NULL;
1209 sp->buffer_offset = 0;
1234 TIFFErrorExtR(tif, module,
"No space for WebP state block");
void _TIFFSetDefaultCompressionState(TIFF *tif)
Install fallback codec callbacks and reset raw-access flags.
#define TIFF_SIZE_FORMAT
Provide the printf conversion fragment for size_t, without a percent sign.
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.
#define isTiled(tif)
Test whether a handle uses tiled storage.
#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 TWebPPreDecode(TIFF *tif, uint16_t s)
Reset WebP per-strile decode state and error latch.
static int TWebPPreEncode(TIFF *tif, uint16_t s)
Allocate full-strile WebP input staging and attach the output writer.
static int TWebPDatasetWriter(const uint8_t *data, size_t data_size, const WebPPicture *const picture)
Append one dependency output fragment to the raw TIFF buffer.
#define EncoderState(tif)
Alias of LState(), returning the WebPState for encoding.
static int TWebPSetupEncode(TIFF *tif)
Validate WebP input and initialise picture and encoder configuration.
static int TWebPSetupDecode(TIFF *tif)
Validate WebP channel support and enter decode mode.
static int TWebPVGetField(TIFF *tif, uint32_t tag, va_list ap)
Retrieve WebP quality or lossless settings or delegate a tag.
#define DecoderState(tif)
Alias of LState(), returning the WebPState for decoding.
#define LState(tif)
Cast tif->tif_data to the WebPState.
static int TWebPVSetField(TIFF *tif, uint32_t tag, va_list ap)
Store WebP quality lossless settings or delegate another tag.
static int TWebPPostEncode(TIFF *tif)
Import staged RGB or RGBA encode it and flush the WebP frame.
#define LSTATE_INIT_ENCODE
Bit 0x02 of the codec state field: encoding has been set up.
static int TWebPDecode(TIFF *tif, uint8_t *op, tmsize_t occ, uint16_t s)
Decode a WebP strile or retrieve its next decoded rows.
static uint64_t TWebPGetMaxCompressionRatio(TIFF *tif)
Report the channel-dependent WebP expansion heuristic.
static const TIFFField TWebPFields[]
Define WebP quality lossless and exact pseudo-tags.
static void TWebPGetStrileDimensions(const TIFF *tif, uint32_t *width, uint32_t *height)
Compute the pixel extent of the current strip or tile.
#define LSTATE_INIT_DECODE
Bit 0x01 of the codec state field: decoding has been set up.
static int TWebPEncode(TIFF *tif, uint8_t *bp, tmsize_t cc, uint16_t s)
Accumulate decoded WebP input in the strile staging buffer.
static int TWebPFixupTags(TIFF *tif)
Reject noncontiguous WebP sample organisation.
static void TWebPCleanup(TIFF *tif)
Release WebP picture decoder staging and outer codec state.
int TIFFInitWebP(TIFF *tif, int scheme)
Allocate WebP codec state and install buffered frame callbacks.
#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.
void _TIFFmemcpy(void *d, const void *s, tmsize_t c)
Copy a non-overlapping byte range.
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.
Store the current directory values and image traversal state.
uint32_t td_rowsperstrip
Rows per strip; UINT32_MAX represents one whole-image strip.
uint32_t td_imagewidth
Image width in pixels.
uint32_t td_tilewidth
Tile width in pixels.
uint16_t td_bitspersample
Bits in each sample.
uint32_t td_row
Current scanline index; UINT32_MAX before traversal starts.
uint16_t td_planarconfig
PLANARCONFIG_* sample-plane arrangement.
uint16_t td_sampleformat
SAMPLEFORMAT_* interpretation of samples.
uint16_t td_samplesperpixel
Samples per pixel, including extra samples.
uint32_t td_imagelength
Image length in rows.
uint32_t td_tilelength
Tile length in rows.
TIFFVGetMethod vgetfield
Variadic tag-query implementation for the handle.
TIFFVSetMethod vsetfield
Variadic tag-setting implementation for the handle.
Own WebP frame staging dependency objects and saved tag methods.
WebPPicture sPicture
Embedded dependency picture owning imported samples until WebPPictureFree.
unsigned int buffer_size
Staging capacity in bytes, bounded by validated frame dimensions.
TIFFVGetMethod vgetparent
Borrowed parent getter restored on cleanup.
WebPDecBuffer sDecBuffer
External-memory descriptor borrowing pBuffer or caller whole-strile output.
int quality_level
Cached compression quality, including warned out-of-range assignments.
TIFFVSetMethod vsetparent
Borrowed parent setter restored on cleanup.
uint16_t nSamples
Cached RGB/RGBA channel count.
int read_error
Same-strile decode failure latch reset by predecode.
WebPConfig sEncoderConfig
Embedded dependency encoder configuration.
int lossless
Cached lossless option applied at encoder setup.
WebPIDecoder * psDecoder
Owned incremental dependency decoder, released by WebPIDelete.
uint8_t * pBuffer
Owned Ext full-strile input/output staging, released on replacement/cleanup.
unsigned int buffer_offset
Number of staged input bytes.
int state
Decode/encode initialisation flags.
int lossless_exact
Cached preservation of RGB under zero alpha in supported lossless ABI.
int last_y
Number of decoded rows already returned.
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.
TIFFCodeMethod tif_decodetile
Codec callback decoding tile data.
Declare private library state, callbacks, and implementation helpers.