34#include "webp/decode.h"
35#include "webp/encode.h"
39#define LSTATE_INIT_DECODE 0x01
40#define LSTATE_INIT_ENCODE 0x02
51 WebPConfig sEncoderConfig;
53 unsigned int buffer_offset;
56 WebPIDecoder* psDecoder;
57 WebPDecBuffer sDecBuffer;
66#define LState(tif) ((WebPState*) (tif)->tif_data)
67#define DecoderState(tif) LState(tif)
68#define EncoderState(tif) LState(tif)
74int TWebPDatasetWriter(
const uint8_t*
data,
size_t data_size,
75 const WebPPicture*
const picture)
77 static const char module[] =
"TWebPDatasetWriter";
99 static const char module[] =
"TWebPEncode";
100 WebPState *
sp = EncoderState(tif);
104 assert(
sp->state == LSTATE_INIT_ENCODE);
110 "Too many bytes to be written");
125 static const char module[] =
"WebPDecode";
126 VP8StatusCode
status = VP8_STATUS_OK;
127 WebPState *
sp = DecoderState(tif);
131 assert(
sp->state == LSTATE_INIT_DECODE);
133 if (occ %
sp->sDecBuffer.u.RGBA.stride)
136 "Fractional scanlines cannot be read");
142 if (
status != VP8_STATUS_OK &&
status != VP8_STATUS_SUSPENDED) {
143 if (
status == VP8_STATUS_INVALID_PARAM) {
145 "Invalid parameter used.");
146 }
else if (
status == VP8_STATUS_OUT_OF_MEMORY) {
151 "Unrecognized error.");
162 (occ <=
stride * (current_y -
sp->last_y))) {
169 sp->last_y += occ /
sp->sDecBuffer.u.RGBA.stride;
179TWebPFixupTags(
TIFF* tif)
183 static const char module[] =
"TWebPFixupTags";
185 "TIFF WEBP requires data to be stored contiguously in RGB e.g. RGBRGBRGB "
186#
if WEBP_ENCODER_ABI_VERSION >= 0x0100
196TWebPSetupDecode(
TIFF* tif)
198 static const char module[] =
"WebPSetupDecode";
202 WebPState*
sp = DecoderState(tif);
208 if (
sp->nSamples != 3
209#
if WEBP_ENCODER_ABI_VERSION >= 0x0100
215 "WEBP driver doesn't support %d bands. Must be 3 (RGB) "
216 #
if WEBP_ENCODER_ABI_VERSION >= 0x0100
225 if ((nBitsPerSample != 8) && (sampleFormat != 1)) {
227 "WEBP driver requires 8 bit unsigned data");
232 if (
sp->state & LSTATE_INIT_ENCODE) {
233 WebPPictureFree(&
sp->sPicture);
234 if (
sp->pBuffer !=
NULL) {
238 sp->buffer_offset = 0;
242 sp->state |= LSTATE_INIT_DECODE;
253 static const char module[] =
"TWebPPreDecode";
254 uint32 segment_width, segment_height;
255 WebPState*
sp = DecoderState(tif);
270 if( (
sp->state & LSTATE_INIT_DECODE) == 0 )
273 if (
sp->psDecoder !=
NULL) {
274 WebPIDelete(
sp->psDecoder);
275 WebPFreeDecBuffer(&
sp->sDecBuffer);
281 WebPInitDecBuffer(&
sp->sDecBuffer);
283 sp->sDecBuffer.is_external_memory = 0;
284 sp->sDecBuffer.width = segment_width;
285 sp->sDecBuffer.height = segment_height;
286 sp->sDecBuffer.u.RGBA.stride = segment_width *
sp->nSamples;
287 sp->sDecBuffer.u.RGBA.size = segment_width *
sp->nSamples * segment_height;
289 if (
sp->nSamples > 3) {
290 sp->sDecBuffer.colorspace = MODE_RGBA;
292 sp->sDecBuffer.colorspace = MODE_RGB;
295 sp->psDecoder = WebPINewDecoder(&
sp->sDecBuffer);
297 if (
sp->psDecoder ==
NULL) {
299 "Unable to allocate WebP decoder.");
307TWebPSetupEncode(
TIFF* tif)
309 static const char module[] =
"WebPSetupEncode";
313 WebPState*
sp = EncoderState(tif);
319 if (
sp->nSamples != 3
320#
if WEBP_ENCODER_ABI_VERSION >= 0x0100
326 "WEBP driver doesn't support %d bands. Must be 3 (RGB) "
327#
if WEBP_ENCODER_ABI_VERSION >= 0x0100
336 if ((nBitsPerSample != 8) && (sampleFormat != 1)) {
338 "WEBP driver requires 8 bit unsigned data");
342 if (
sp->state & LSTATE_INIT_DECODE) {
343 WebPIDelete(
sp->psDecoder);
344 WebPFreeDecBuffer(&
sp->sDecBuffer);
350 sp->state |= LSTATE_INIT_ENCODE;
352 if (!WebPPictureInit(&
sp->sPicture)) {
354 "Error initializing WebP picture.");
358 if (!WebPConfigInitInternal(&
sp->sEncoderConfig, WEBP_PRESET_DEFAULT,
360 WEBP_ENCODER_ABI_VERSION)) {
362 "Error creating WebP encoder configuration.");
367 #if WEBP_ENCODER_ABI_VERSION >= 0x0100
368 sp->sEncoderConfig.lossless =
sp->lossless;
370 sp->sPicture.use_argb = 1;
374 if (!WebPValidateConfig(&
sp->sEncoderConfig)) {
376 "Error with WebP encoder configuration.");
389 static const char module[] =
"TWebPPreEncode";
390 uint32 segment_width, segment_height;
391 WebPState *
sp = EncoderState(tif);
397 if(
sp->state != LSTATE_INIT_ENCODE )
413 if( segment_width > 16383 || segment_height > 16383 ) {
415 "WEBP maximum image dimensions are 16383 x 16383.");
421 sp->buffer_size = segment_width * segment_height *
sp->nSamples;
423 if (
sp->pBuffer !=
NULL) {
433 sp->buffer_offset = 0;
435 sp->sPicture.width = segment_width;
436 sp->sPicture.height = segment_height;
437 sp->sPicture.writer = TWebPDatasetWriter;
438 sp->sPicture.custom_ptr = tif;
447TWebPPostEncode(
TIFF* tif)
449 static const char module[] =
"WebPPostEncode";
451 WebPState *
sp = EncoderState(tif);
454 assert(
sp->state == LSTATE_INIT_ENCODE);
458#
if WEBP_ENCODER_ABI_VERSION >= 0x0100
459 if (
sp->nSamples == 4) {
460 if (!WebPPictureImportRGBA(&
sp->sPicture,
sp->pBuffer, (
int)
stride)) {
462 "WebPPictureImportRGBA() failed" );
468 if (!WebPPictureImportRGB(&
sp->sPicture,
sp->pBuffer, (
int)
stride)) {
470 "WebPPictureImportRGB() failed");
474 if (!WebPEncode(&
sp->sEncoderConfig, &
sp->sPicture)) {
476#if WEBP_ENCODER_ABI_VERSION >= 0x0100
477 const char* pszErrorMsg =
NULL;
478 switch(
sp->sPicture.error_code) {
479 case VP8_ENC_ERROR_OUT_OF_MEMORY:
480 pszErrorMsg =
"Out of memory";
break;
481 case VP8_ENC_ERROR_BITSTREAM_OUT_OF_MEMORY:
482 pszErrorMsg =
"Out of memory while flushing bits";
break;
483 case VP8_ENC_ERROR_NULL_PARAMETER:
484 pszErrorMsg =
"A pointer parameter is NULL";
break;
485 case VP8_ENC_ERROR_INVALID_CONFIGURATION:
486 pszErrorMsg =
"Configuration is invalid";
break;
487 case VP8_ENC_ERROR_BAD_DIMENSION:
488 pszErrorMsg =
"Picture has invalid width/height";
break;
489 case VP8_ENC_ERROR_PARTITION0_OVERFLOW:
490 pszErrorMsg =
"Partition is bigger than 512k. Try using less "
491 "SEGMENTS, or increase PARTITION_LIMIT value";
493 case VP8_ENC_ERROR_PARTITION_OVERFLOW:
494 pszErrorMsg =
"Partition is bigger than 16M";
496 case VP8_ENC_ERROR_BAD_WRITE:
497 pszErrorMsg =
"Error while fludshing bytes";
break;
498 case VP8_ENC_ERROR_FILE_TOO_BIG:
499 pszErrorMsg =
"File is bigger than 4G";
break;
500 case VP8_ENC_ERROR_USER_ABORT:
501 pszErrorMsg =
"User interrupted";
505 "WebPEncode returned an unknown error code: %d",
506 sp->sPicture.error_code);
507 pszErrorMsg =
"Unknown WebP error type.";
511 "WebPEncode() failed : %s", pszErrorMsg);
514 "Error in WebPEncode()");
519 sp->sPicture.custom_ptr =
NULL;
524 "Error flushing TIFF WebP encoder.");
532TWebPCleanup(
TIFF* tif)
534 WebPState*
sp = LState(tif);
541 if (
sp->state & LSTATE_INIT_ENCODE) {
542 WebPPictureFree(&
sp->sPicture);
545 if (
sp->psDecoder !=
NULL) {
546 WebPIDelete(
sp->psDecoder);
547 WebPFreeDecBuffer(&
sp->sDecBuffer);
552 if (
sp->pBuffer !=
NULL) {
566 static const char module[] =
"WebPVSetField";
567 WebPState*
sp = LState(tif);
572 if(
sp->quality_level <= 0 ||
573 sp->quality_level > 100.0f ) {
575 "WEBP_LEVEL should be between 1 and 100");
579 #if WEBP_ENCODER_ABI_VERSION >= 0x0100
582 sp->quality_level = 100.0f;
587 "Need to upgrade WEBP driver, this version doesn't support "
588 "lossless compression.");
592 return (*
sp->vsetparent)(tif,
tag,
ap);
600 WebPState*
sp = LState(tif);
610 return (*
sp->vgetparent)(tif,
tag,
ap);
628 static const char module[] =
"TIFFInitWebP";
638 "Merging WebP codec-specific tags failed");
659 sp->quality_level = 75.0f;
666 sp->buffer_offset = 0;
691 "No space for WebP state block");
unsigned int(__cdecl typeof(jpeg_read_scanlines))(struct jpeg_decompress_struct *
GLint GLenum GLsizei GLsizei GLsizei GLint GLsizei const GLvoid * data
GLenum GLuint GLenum GLsizei const GLchar * buf
#define memcpy(s1, s2, n)
static unsigned(__cdecl *hash_bstr)(bstr_t s)
uint16 td_samplesperpixel
TIFFCodeMethod tif_encodestrip
TIFFCodeMethod tif_encodetile
TIFFTagMethods tif_tagmethods
TIFFPreMethod tif_preencode
TIFFBoolMethod tif_fixuptags
TIFFPreMethod tif_predecode
TIFFCodeMethod tif_decodestrip
TIFFCodeMethod tif_decoderow
TIFFBoolMethod tif_setupencode
TIFFBoolMethod tif_postencode
TIFFCodeMethod tif_encoderow
TIFFVoidMethod tif_cleanup
TIFFBoolMethod tif_setupdecode
TIFFCodeMethod tif_decodetile
static void buffer_size(GLcontext *ctx, GLuint *width, GLuint *height)
void _TIFFSetDefaultCompressionState(TIFF *tif)
int _TIFFMergeFields(TIFF *tif, const TIFFField info[], uint32 n)
void TIFFErrorExt(thandle_t fd, const char *module, const char *fmt,...)
void * _TIFFmalloc(tmsize_t s)
void _TIFFmemcpy(void *d, const void *s, tmsize_t c)
void TIFFWarningExt(thandle_t fd, const char *module, const char *fmt,...)
int TIFFFlushData1(TIFF *tif)
#define TIFFTAG_WEBP_LEVEL
#define TIFFTAG_WEBP_LOSSLESS
#define PLANARCONFIG_CONTIG
int(* TIFFVGetMethod)(TIFF *, uint32, va_list)
int(* TIFFVSetMethod)(TIFF *, uint32, va_list)
#define TIFFArrayCount(a)
void int int ULONGLONG int va_list * ap