From ce8733209d18369d74b86d1e3e77b334bf0443c4 Mon Sep 17 00:00:00 2001 From: Pascal Massimino Date: Sat, 8 Oct 2016 18:57:13 +0000 Subject: [PATCH] Introduce a generic WebPGetImageReader(type) function Also introduce an always-failing 'reader' for unknown formats. So we don't have to check reader==NULL, code is more regular. -> We can get read of specific ReadPNG(), ReadJPEG(), ... declaration and use. Change-Id: I290759705420878f00c7223c726d4ad404afd9c4 --- examples/cwebp.c | 2 +- imageio/image_dec.c | 25 ++++++++++++++++++++----- imageio/image_dec.h | 6 +++++- 3 files changed, 26 insertions(+), 7 deletions(-) diff --git a/examples/cwebp.c b/examples/cwebp.c index aede1321..446576ea 100644 --- a/examples/cwebp.c +++ b/examples/cwebp.c @@ -107,7 +107,7 @@ static int ReadPicture(const char* const filename, WebPPicture* const pic, if (pic->width == 0 || pic->height == 0) { WebPImageReader reader = WebPGuessImageReader(data, data_size); - ok = (reader != NULL) && reader(data, data_size, pic, keep_alpha, metadata); + ok = 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/imageio/image_dec.c b/imageio/image_dec.c index ac056440..67d47d5f 100644 --- a/imageio/image_dec.c +++ b/imageio/image_dec.c @@ -34,13 +34,28 @@ 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)) { +static int FailReader(const uint8_t* const data, size_t data_size, + struct WebPPicture* const pic, + int keep_alpha, struct Metadata* const metadata) { + (void)data; + (void)data_size; + (void)pic; + (void)keep_alpha; + (void)metadata; + return 0; +} + +WebPImageReader WebPGetImageReader(WebPInputFileFormat format) { + switch (format) { 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; + default: return FailReader; } -} \ No newline at end of file +} + +WebPImageReader WebPGuessImageReader(const uint8_t* const data, + size_t data_size) { + return WebPGetImageReader(WebPGuessImageType(data, data_size)); +} diff --git a/imageio/image_dec.h b/imageio/image_dec.h index 54dcda3f..28a24ebe 100644 --- a/imageio/image_dec.h +++ b/imageio/image_dec.h @@ -49,8 +49,12 @@ typedef int (*WebPImageReader)(const uint8_t* const data, size_t data_size, struct WebPPicture* const pic, int keep_alpha, struct Metadata* const metadata); +// Return the reader associated to a given file format. +WebPImageReader WebPGetImageReader(WebPInputFileFormat format); + // This function is similar to WebPGuessImageType(), but returns a -// suitable reader function. Or NULL if the image can't be guessed. +// suitable reader function. The returned reader is never NULL, but +// unknown formats will return an always-failing valid reader. WebPImageReader WebPGuessImageReader(const uint8_t* const data, size_t data_size);