From 2ca642e02a2f38dd64ac0204f3b8cc8d01944e74 Mon Sep 17 00:00:00 2001 From: Urvang Joshi Date: Fri, 9 Nov 2012 14:37:20 -0800 Subject: [PATCH] Rectify WebPMuxGetFeatures: It should return ALPHA_FLAG for lossless bit-stream Change-Id: I900bd5b58bf75bc25fca1abf4ecc12aea26eac1c --- src/mux/muxinternal.c | 12 +++++++----- src/mux/muxread.c | 29 +++++++++++++++++------------ 2 files changed, 24 insertions(+), 17 deletions(-) diff --git a/src/mux/muxinternal.c b/src/mux/muxinternal.c index 06882bbf..ebeb651e 100644 --- a/src/mux/muxinternal.c +++ b/src/mux/muxinternal.c @@ -578,12 +578,14 @@ WebPMuxError MuxValidate(const WebPMux* const mux) { if (num_vp8x == 0 && num_images != 1) return WEBP_MUX_INVALID_ARGUMENT; // ALPHA_FLAG & alpha chunk(s) are consistent. - if (num_vp8x > 0 && MuxHasLosslessImages(mux->images_)) { - // Special case: we have a VP8X chunk as well as some lossless images. - if (!(flags & ALPHA_FLAG)) return WEBP_MUX_INVALID_ARGUMENT; + if (MuxHasLosslessImages(mux->images_)) { + if (num_vp8x > 0) { + // Special case: we have a VP8X chunk as well as some lossless images. + if (!(flags & ALPHA_FLAG)) return WEBP_MUX_INVALID_ARGUMENT; + } } else { - err = ValidateChunk(mux, IDX_ALPHA, ALPHA_FLAG, flags, -1, &num_alpha); - if (err != WEBP_MUX_OK) return err; + err = ValidateChunk(mux, IDX_ALPHA, ALPHA_FLAG, flags, -1, &num_alpha); + if (err != WEBP_MUX_OK) return err; } // num_fragments & num_images are consistent. diff --git a/src/mux/muxread.c b/src/mux/muxread.c index 239a36e0..e1862e4e 100644 --- a/src/mux/muxread.c +++ b/src/mux/muxread.c @@ -254,27 +254,32 @@ WebPMux* WebPMuxCreateInternal(const WebPData* bitstream, int copy_data, //------------------------------------------------------------------------------ // Get API(s). -// TODO(urvang): Change the behavior of this to return ALPHA_FLAG when the mux -// doesn't contain a VP8X chunk, but does contain a VP8L chunk with real alpha. WebPMuxError WebPMuxGetFeatures(const WebPMux* mux, uint32_t* flags) { WebPData data; - WebPMuxError err; if (mux == NULL || flags == NULL) return WEBP_MUX_INVALID_ARGUMENT; *flags = 0; // Check if VP8X chunk is present. - err = MuxGet(mux, IDX_VP8X, 1, &data); - if (err == WEBP_MUX_NOT_FOUND) { - return MuxValidateForImage(mux); // Check if a single image is present. - } else if (err != WEBP_MUX_OK) { - return err; + if (MuxGet(mux, IDX_VP8X, 1, &data) == WEBP_MUX_OK) { + if (data.size < CHUNK_SIZE_BYTES) return WEBP_MUX_BAD_DATA; + *flags = GetLE32(data.bytes); // All OK. Fill up flags. + } else { + WebPMuxError err = MuxValidateForImage(mux); // Check for single image. + if (err != WEBP_MUX_OK) return err; + if (MuxHasLosslessImages(mux->images_)) { + const WebPData* const vp8l_data = &mux->images_->img_->data_; + int has_alpha = 0; + if (!VP8LGetInfo(vp8l_data->bytes, vp8l_data->size, NULL, NULL, + &has_alpha)) { + return WEBP_MUX_BAD_DATA; + } + if (has_alpha) { + *flags = ALPHA_FLAG; + } + } } - if (data.size < CHUNK_SIZE_BYTES) return WEBP_MUX_BAD_DATA; - - // All OK. Fill up flags. - *flags = GetLE32(data.bytes); return WEBP_MUX_OK; }