From d77b877cc9a46645abc46fcb6b0ee8de777a1c23 Mon Sep 17 00:00:00 2001 From: Pascal Massimino Date: Wed, 1 Jun 2016 20:55:29 +0200 Subject: [PATCH] introduce a common signature for all image reader function -> WebPImageReader Introduce a variant of image-guessing function that returns a reader directly: WebPGuessImageReader() Change-Id: I5ddc53024fcf941e33d997b2be6aa1a963d939ab --- examples/cwebp.c | 15 ++------------- examples/image_dec.c | 11 +++++++++++ examples/image_dec.h | 10 ++++++++++ examples/jpegdec.c | 7 +++++-- examples/jpegdec.h | 5 ++++- 5 files changed, 32 insertions(+), 16 deletions(-) diff --git a/examples/cwebp.c b/examples/cwebp.c index 80924072..b538ae21 100644 --- a/examples/cwebp.c +++ b/examples/cwebp.c @@ -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); diff --git a/examples/image_dec.c b/examples/image_dec.c index 0b3ea160..ac056440 100644 --- a/examples/image_dec.c +++ b/examples/image_dec.c @@ -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; + } +} \ No newline at end of file diff --git a/examples/image_dec.h b/examples/image_dec.h index b12b3a85..de25e14f 100644 --- a/examples/image_dec.h +++ b/examples/image_dec.h @@ -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 diff --git a/examples/jpegdec.c b/examples/jpegdec.c index 244502da..94d95d4a 100644 --- a/examples/jpegdec.c +++ b/examples/jpegdec.c @@ -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"); diff --git a/examples/jpegdec.h b/examples/jpegdec.h index 68c042aa..9be463b1 100644 --- a/examples/jpegdec.h +++ b/examples/jpegdec.h @@ -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"