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 blimp { |
| 13 |
| 14 bool WebPDecoder(const void* input, size_t input_size, SkBitmap* bitmap) { |
| 15 DCHECK(bitmap); |
| 16 |
| 17 // Initialize an empty WebPDecoderConfig. |
| 18 WebPDecoderConfig config; |
| 19 if (!WebPInitDecoderConfig(&config)) { |
| 20 LOG(WARNING) << "Failed to initialize WebP config."; |
| 21 return false; |
| 22 } |
| 23 |
| 24 // Treat the input as uint8_t. |
| 25 WebPData data = {reinterpret_cast<const uint8_t*>(input), input_size}; |
| 26 |
| 27 // Read WebP feature information into |config.input|, which is a |
| 28 // WebPBitstreamFeatures. It contains information such as width, height and |
| 29 // whether the WebP image has an alpha channel or not. |
| 30 if (WebPGetFeatures(data.bytes, data.size, &config.input) != VP8_STATUS_OK) { |
| 31 LOG(WARNING) << "Failed to get WebP features."; |
| 32 return false; |
| 33 } |
| 34 // Animations are not supported. |
| 35 DCHECK_EQ(config.input.has_animation, 0); |
| 36 |
| 37 // Allocate correct size for the bitmap based on the WebPBitstreamFeatures. |
| 38 bitmap->allocN32Pixels(config.input.width, config.input.height); |
| 39 DCHECK_EQ(kPremul_SkAlphaType, bitmap->alphaType()); |
| 40 |
| 41 // Setup the decoder buffer based on the WebPBitstreamFeatures. |
| 42 WebPDecBuffer decoderBuffer; |
| 43 |
| 44 DCHECK(config.input.has_alpha); |
| 45 #if SK_B32_SHIFT // Output little-endian RGBA pixels (Android). |
| 46 decoderBuffer.colorspace = MODE_rgbA; |
| 47 #else // Output little-endian BGRA pixels. |
| 48 decoderBuffer.colorspace = MODE_bgrA; |
| 49 #endif |
| 50 decoderBuffer.u.RGBA.stride = config.input.width * 4; |
| 51 decoderBuffer.u.RGBA.size = decoderBuffer.u.RGBA.stride * config.input.height; |
| 52 |
| 53 // Instead of using the default WebPDecBuffer output, make WebPDecode directly |
| 54 // write into the SkBitmap. |
| 55 decoderBuffer.is_external_memory = 1; |
| 56 decoderBuffer.u.RGBA.rgba = |
| 57 reinterpret_cast<uint8_t*>(bitmap->getAddr32(0, 0)); |
| 58 |
| 59 // Set the config up to use the decoding buffer we created. |
| 60 config.output = decoderBuffer; |
| 61 |
| 62 // Decode the input data into the bitmap buffer. |
| 63 bool success = WebPDecode(data.bytes, data.size, &config) == VP8_STATUS_OK; |
| 64 |
| 65 // Now free the buffer. It is safe to call this even when the buffer is |
| 66 // external and not allocated by WebPDecode. |
| 67 WebPFreeDecBuffer(&config.output); |
| 68 |
| 69 if (!success) { |
| 70 LOG(WARNING) << "Failed to decode WebP data."; |
| 71 return false; |
| 72 } |
| 73 return true; |
| 74 } |
| 75 |
| 76 } // namespace blimp |
OLD | NEW |