Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 1 // Copyright 2016 The Chromium Authors. All rights reserved. | |
| 2 // Use of this source code is governed by a BSD-style license that can be | |
| 3 // found in the LICENSE file. | |
| 4 | |
| 5 #include "blimp/common/compositor/webp_decoder.h" | |
| 6 | |
| 7 #include "base/logging.h" | |
| 8 #include "third_party/libwebp/webp/decode.h" | |
| 9 #include "third_party/libwebp/webp/demux.h" | |
| 10 #include "third_party/skia/include/core/SkBitmap.h" | |
| 11 | |
| 12 namespace { | |
| 13 #if SK_B32_SHIFT // Output little-endian RGBA pixels (Android). | |
| 14 inline WEBP_CSP_MODE outputMode(bool hasAlpha) { | |
| 15 return hasAlpha ? MODE_rgbA : MODE_RGBA; | |
|
urvang
2016/02/13 00:24:36
I'm thinking you can always use MODE_RGBA here (an
nyquist
2016/02/16 23:37:10
Done.
| |
| 16 } | |
| 17 #else // Output little-endian BGRA pixels. | |
| 18 inline WEBP_CSP_MODE outputMode(bool hasAlpha) { | |
| 19 return hasAlpha ? MODE_bgrA : MODE_BGRA; | |
| 20 } | |
| 21 #endif | |
| 22 | |
| 23 } // namespace | |
| 24 | |
| 25 namespace blimp { | |
| 26 | |
| 27 bool WebPDecoder(const void* input, size_t input_size, SkBitmap* bitmap) { | |
| 28 DCHECK(bitmap); | |
| 29 | |
|
urvang
2016/02/13 00:24:36
Also DCHECK() that AlphaType is as expected (opaqu
nyquist
2016/02/16 23:37:10
So, I added
DCHECK(config.input.has_alpha);
Is th
| |
| 30 // Initialize an empty WebPDecoderConfig. | |
| 31 WebPDecoderConfig config; | |
| 32 if (!WebPInitDecoderConfig(&config)) { | |
| 33 LOG(WARNING) << "Failed to initialize WebP config."; | |
| 34 return false; | |
| 35 } | |
| 36 | |
| 37 // Treat the input as uint8_t. | |
| 38 WebPData data = {reinterpret_cast<const uint8_t*>(input), input_size}; | |
| 39 | |
| 40 // Read WebP feature information into |config.input|, which is a | |
| 41 // WebPBitstreamFeatures. It contains information such as width, height and | |
| 42 // whether the WebP image has an alpha channel or not. | |
| 43 if (WebPGetFeatures(data.bytes, data.size, &config.input) != VP8_STATUS_OK) { | |
| 44 LOG(WARNING) << "Failed to get WebP features."; | |
| 45 return false; | |
| 46 } | |
|
urvang
2016/02/13 00:24:36
You can ensure ur assumption in code:
DCHECK_EQ(co
nyquist
2016/02/16 23:37:10
Done.
| |
| 47 | |
| 48 // Allocate correct size for the bitmap based on the WebPBitstreamFeatures. | |
| 49 bitmap->allocN32Pixels(config.input.width, config.input.height); | |
| 50 | |
| 51 // Setup the decoder buffer based on the WebPBitstreamFeatures. | |
| 52 WebPDecBuffer decoderBuffer; | |
| 53 decoderBuffer.colorspace = outputMode(config.input.has_alpha); | |
| 54 decoderBuffer.u.RGBA.stride = config.input.width * 4; | |
| 55 decoderBuffer.u.RGBA.size = decoderBuffer.u.RGBA.stride * config.input.height; | |
| 56 | |
| 57 // Instead of using the default WebPDecBuffer output, make WebPDecode directly | |
| 58 // write into the SkBitmap. | |
| 59 decoderBuffer.is_external_memory = 1; | |
| 60 decoderBuffer.u.RGBA.rgba = | |
| 61 reinterpret_cast<uint8_t*>(bitmap->getAddr32(0, 0)); | |
| 62 | |
| 63 // Set the config up to use the decoding buffer we created. | |
| 64 config.output = decoderBuffer; | |
| 65 | |
| 66 // Decode the input data into the bitmap buffer. | |
| 67 bool success = WebPDecode(data.bytes, data.size, &config) == VP8_STATUS_OK; | |
| 68 | |
| 69 // Now free the buffer. It is safe to call this even when the buffer is | |
| 70 // external and not allocated by WebPDecode. | |
| 71 WebPFreeDecBuffer(&config.output); | |
| 72 | |
| 73 if (!success) { | |
| 74 LOG(WARNING) << "Failed to decode WebP data."; | |
| 75 return false; | |
| 76 } | |
| 77 | |
| 78 // Set the bitmap's opaqueness based on what we saw. | |
| 79 bitmap->setAlphaType(config.input.has_alpha ? kPremul_SkAlphaType | |
|
urvang
2016/02/13 00:24:36
As discussed, assume and use non-premultiplied mod
nyquist
2016/02/16 23:37:10
Done.
| |
| 80 : kOpaque_SkAlphaType); | |
| 81 | |
| 82 return true; | |
| 83 } | |
| 84 | |
| 85 } // namespace blimp | |
| OLD | NEW |