Merge "introduce a common signature for all image reader function"

This commit is contained in:
Pascal Massimino 2016-06-02 05:26:23 +00:00 committed by Gerrit Code Review
commit eee788e26a
5 changed files with 32 additions and 16 deletions

View File

@ -105,19 +105,8 @@ static int ReadPicture(const char* const filename, WebPPicture* const pic,
if (!ok) goto End;
if (pic->width == 0 || pic->height == 0) {
ok = 0;
if (data_size >= 12) {
const WebPInputFileFormat format = WebPGuessImageType(data, data_size);
if (format == WEBP_PNG_FORMAT) {
ok = ReadPNG(data, data_size, pic, keep_alpha, metadata);
} else if (format == WEBP_JPEG_FORMAT) {
ok = ReadJPEG(data, data_size, pic, metadata);
} else if (format == WEBP_TIFF_FORMAT) {
ok = ReadTIFF(data, data_size, pic, keep_alpha, metadata);
} else if (format == WEBP_WEBP_FORMAT) {
ok = ReadWebP(data, data_size, pic, keep_alpha, metadata);
}
}
WebPImageReader reader = WebPGuessImageReader(data, data_size);
ok = (reader != NULL) && reader(data, data_size, pic, keep_alpha, metadata);
} else {
// If image size is specified, infer it as YUV format.
ok = ReadYUV(data, data_size, pic);

View File

@ -33,3 +33,14 @@ WebPInputFileFormat WebPGuessImageType(const uint8_t* const data,
}
return format;
}
WebPImageReader WebPGuessImageReader(const uint8_t* const data,
size_t data_size) {
switch (WebPGuessImageType(data, data_size)) {
case WEBP_PNG_FORMAT: return ReadPNG;
case WEBP_JPEG_FORMAT: return ReadJPEG;
case WEBP_TIFF_FORMAT: return ReadTIFF;
case WEBP_WEBP_FORMAT: return ReadWebP;
default: return NULL;
}
}

View File

@ -44,6 +44,16 @@ typedef enum {
WebPInputFileFormat WebPGuessImageType(const uint8_t* const data,
size_t data_size);
// Signature for common image-reading functions (ReadPNG, ReadJPEG, ...)
typedef int (*WebPImageReader)(const uint8_t* const data, size_t data_size,
struct WebPPicture* const pic,
int keep_alpha, struct Metadata* const metadata);
// This function is similar to WebPGuessImageType(), but returns a
// suitable reader function. Or NULL if the image can't be guessed.
WebPImageReader WebPGuessImageReader(const uint8_t* const data,
size_t data_size);
#ifdef __cplusplus
} // extern "C"
#endif

View File

@ -255,7 +255,8 @@ static void ContextSetup(volatile struct jpeg_decompress_struct* const cinfo,
}
int ReadJPEG(const uint8_t* const data, size_t data_size,
WebPPicture* const pic, Metadata* const metadata) {
WebPPicture* const pic, int keep_alpha,
Metadata* const metadata) {
volatile int ok = 0;
int stride, width, height;
volatile struct jpeg_decompress_struct dinfo;
@ -264,6 +265,7 @@ int ReadJPEG(const uint8_t* const data, size_t data_size,
JSAMPROW buffer[1];
JPEGReadContext ctx;
(void)keep_alpha;
memset(&ctx, 0, sizeof(ctx));
ctx.data = data;
ctx.data_size = data_size;
@ -333,11 +335,12 @@ int ReadJPEG(const uint8_t* const data, size_t data_size,
}
#else // !WEBP_HAVE_JPEG
int ReadJPEG(const uint8_t* const data, size_t data_size,
struct WebPPicture* const pic,
struct WebPPicture* const pic, int keep_alpha,
struct Metadata* const metadata) {
(void)data;
(void)data_size;
(void)pic;
(void)keep_alpha;
(void)metadata;
fprintf(stderr, "JPEG support not compiled. Please install the libjpeg "
"development package before building.\n");

View File

@ -25,8 +25,11 @@ struct WebPPicture;
// Reads a JPEG from 'data', returning the decoded output in 'pic'.
// The output is RGB or YUV depending on pic->use_argb value.
// Returns true on success.
// 'keep_alpha' has no effect, but is kept for coherence with other signatures
// for image readers.
int ReadJPEG(const uint8_t* const data, size_t data_size,
struct WebPPicture* const pic, struct Metadata* const metadata);
struct WebPPicture* const pic, int keep_alpha,
struct Metadata* const metadata);
#ifdef __cplusplus
} // extern "C"