From 7861578bd6dc7008c3aedc5479ad662fb6050ed2 Mon Sep 17 00:00:00 2001 From: Pascal Massimino Date: Tue, 27 Oct 2015 22:54:11 +0100 Subject: [PATCH] for ReadXXXX() image-readers, use the value of pic->use_argb This is to infer the needed conversion to YUV(A) or RGB(A). This is useful to avoid some conversion steps between ARGB and YUVA. For instance, if the input file is a JPEG, we decode to RGB and convert to YUV right away, without the intermediate step to ARGB. The only caveat is that cropping/scaling might give slightly different result, because of YUV420 downsampling. Therefore, we omit this feature at cwebp level, when -crop or -rescale is used. Change-Id: I5a3abe5108982f2a4570e841e3d9baffc73f5bee --- examples/cwebp.c | 6 +++++- examples/jpegdec.c | 1 - examples/jpegdec.h | 2 +- examples/pngdec.c | 1 - examples/pngdec.h | 3 ++- examples/tiffdec.c | 1 - examples/tiffdec.h | 3 ++- examples/webpdec.c | 3 ++- examples/webpdec.h | 5 +++-- examples/wicdec.c | 2 +- examples/wicdec.h | 2 +- 11 files changed, 17 insertions(+), 12 deletions(-) diff --git a/examples/cwebp.c b/examples/cwebp.c index 183307ac..f5c01f11 100644 --- a/examples/cwebp.c +++ b/examples/cwebp.c @@ -967,7 +967,11 @@ int main(int argc, const char *argv[]) { goto Error; } - // Read the input + // Read the input. We need to decide if we prefer ARGB or YUVA + // samples, depending on the expected compression mode (this saves + // some conversion steps). + picture.use_argb = (config.lossless || config.preprocessing > 0 || + crop || (resize_w | resize_h) > 0); if (verbose) { StopwatchReset(&stop_watch); } diff --git a/examples/jpegdec.c b/examples/jpegdec.c index 5411e6e0..c06da8dc 100644 --- a/examples/jpegdec.c +++ b/examples/jpegdec.c @@ -272,7 +272,6 @@ int ReadJPEG(FILE* in_file, WebPPicture* const pic, Metadata* const metadata) { // WebP conversion. pic->width = width; pic->height = height; - pic->use_argb = 1; // store raw RGB samples ok = WebPPictureImportRGB(pic, rgb, stride); if (!ok) goto Error; diff --git a/examples/jpegdec.h b/examples/jpegdec.h index cc6e3a19..799164cd 100644 --- a/examples/jpegdec.h +++ b/examples/jpegdec.h @@ -23,7 +23,7 @@ struct Metadata; struct WebPPicture; // Reads a JPEG from 'in_file', returning the decoded output in 'pic'. -// The output is RGB. +// The output is RGB or YUV depending on pic->use_argb value. // Returns true on success. int ReadJPEG(FILE* in_file, struct WebPPicture* const pic, struct Metadata* const metadata); diff --git a/examples/pngdec.c b/examples/pngdec.c index c3072ba5..cb8b389a 100644 --- a/examples/pngdec.c +++ b/examples/pngdec.c @@ -270,7 +270,6 @@ int ReadPNG(FILE* in_file, WebPPicture* const pic, int keep_alpha, pic->width = (int)width; pic->height = (int)height; - pic->use_argb = 1; ok = has_alpha ? WebPPictureImportRGBA(pic, rgb, (int)stride) : WebPPictureImportRGB(pic, rgb, (int)stride); diff --git a/examples/pngdec.h b/examples/pngdec.h index aeeb02ad..3fb16830 100644 --- a/examples/pngdec.h +++ b/examples/pngdec.h @@ -22,8 +22,9 @@ struct Metadata; struct WebPPicture; // Reads a PNG from 'in_file', returning the decoded output in 'pic'. +// Output is RGBA or YUVA, depending on pic->use_argb value. // If 'keep_alpha' is true and the PNG has an alpha channel, the output is RGBA -// otherwise it will be RGB. +// or YUVA. Otherwise, alpha channel is dropped and output is RGB or YUV. // Returns true on success. int ReadPNG(FILE* in_file, struct WebPPicture* const pic, int keep_alpha, struct Metadata* const metadata); diff --git a/examples/tiffdec.c b/examples/tiffdec.c index 88e19fdb..4af9b9ee 100644 --- a/examples/tiffdec.c +++ b/examples/tiffdec.c @@ -100,7 +100,6 @@ int ReadTIFF(const char* const filename, #ifdef WORDS_BIGENDIAN TIFFSwabArrayOfLong(raster, width * height); #endif - pic->use_argb = 1; ok = keep_alpha ? WebPPictureImportRGBA(pic, (const uint8_t*)raster, stride) : WebPPictureImportRGBX(pic, (const uint8_t*)raster, stride); diff --git a/examples/tiffdec.h b/examples/tiffdec.h index d6f90958..1e7d50a4 100644 --- a/examples/tiffdec.h +++ b/examples/tiffdec.h @@ -20,8 +20,9 @@ struct Metadata; struct WebPPicture; // Reads a TIFF from 'filename', returning the decoded output in 'pic'. +// Output is RGBA or YUVA, depending on pic->use_argb value. // If 'keep_alpha' is true and the TIFF has an alpha channel, the output is RGBA -// otherwise it will be RGB. +// or YUVA. Otherwise, alpha channel is dropped and output is RGB or YUV. // Returns true on success. int ReadTIFF(const char* const filename, struct WebPPicture* const pic, int keep_alpha, diff --git a/examples/webpdec.c b/examples/webpdec.c index b527a167..93a26b1b 100644 --- a/examples/webpdec.c +++ b/examples/webpdec.c @@ -41,6 +41,8 @@ int ReadWebP(const char* const in_file, WebPPicture* const pic, if (ExUtilLoadWebP(in_file, &data, &data_size, bitstream)) { const int has_alpha = keep_alpha && bitstream->has_alpha; + // TODO(skal): use MODE_YUV(A), depending on the expected + // input pic->use_argb. This would save some conversion steps. output_buffer->colorspace = has_alpha ? MODE_RGBA : MODE_RGB; status = ExUtilDecodeWebP(data, data_size, 0, &config); @@ -49,7 +51,6 @@ int ReadWebP(const char* const in_file, WebPPicture* const pic, const int stride = output_buffer->u.RGBA.stride; pic->width = output_buffer->width; pic->height = output_buffer->height; - pic->use_argb = 1; ok = has_alpha ? WebPPictureImportRGBA(pic, rgba, stride) : WebPPictureImportRGB(pic, rgba, stride); } diff --git a/examples/webpdec.h b/examples/webpdec.h index 27f40315..2eb6f5af 100644 --- a/examples/webpdec.h +++ b/examples/webpdec.h @@ -20,8 +20,9 @@ struct Metadata; struct WebPPicture; // Reads a WebP from 'in_file', returning the decoded output in 'pic'. -// If 'keep_alpha' is true and the WebP has an alpha channel, the output is -// RGBA otherwise it will be RGB. +// Output is RGBA or YUVA, depending on pic->use_argb value. +// If 'keep_alpha' is true and the TIFF has an alpha channel, the output is RGBA +// or YUVA. Otherwise, alpha channel is dropped and output is RGB or YUV. // Returns true on success. int ReadWebP(const char* const in_file, struct WebPPicture* const pic, int keep_alpha, struct Metadata* const metadata); diff --git a/examples/wicdec.c b/examples/wicdec.c index 37988cb2..f2319524 100644 --- a/examples/wicdec.c +++ b/examples/wicdec.c @@ -320,7 +320,7 @@ int ReadPictureWithWIC(const char* const filename, int ok; pic->width = width; pic->height = height; - pic->use_argb = 1; + pic->use_argb = 1; // For WIC, we always force to argb ok = importer->import(pic, rgb, stride); if (!ok) hr = E_FAIL; } diff --git a/examples/wicdec.h b/examples/wicdec.h index 94d44b36..e17ea7f5 100644 --- a/examples/wicdec.h +++ b/examples/wicdec.h @@ -21,7 +21,7 @@ struct WebPPicture; // Reads an image from 'filename', returning the decoded output in 'pic'. // If 'keep_alpha' is true and the image has an alpha channel, the output is -// RGBA otherwise it will be RGB. +// RGBA otherwise it will be RGB. pic->use_argb is always forced to true. // Returns true on success. int ReadPictureWithWIC(const char* const filename, struct WebPPicture* const pic, int keep_alpha,