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 | |
40 // Setup the decoder buffer based on the WebPBitstreamFeatures. | |
41 WebPDecBuffer decoderBuffer; | |
42 | |
43 DCHECK(config.input.has_alpha); | |
44 #if SK_B32_SHIFT // Output little-endian RGBA pixels (Android). | |
45 decoderBuffer.colorspace = MODE_rgbA; | |
46 #else // Output little-endian BGRA pixels. | |
47 decoderBuffer.colorspace = MODE_bgrA; | |
48 #endif | |
49 decoderBuffer.u.RGBA.stride = config.input.width * 4; | |
50 decoderBuffer.u.RGBA.size = decoderBuffer.u.RGBA.stride * config.input.height; | |
51 | |
52 // Instead of using the default WebPDecBuffer output, make WebPDecode directly | |
53 // write into the SkBitmap. | |
54 decoderBuffer.is_external_memory = 1; | |
55 decoderBuffer.u.RGBA.rgba = | |
56 reinterpret_cast<uint8_t*>(bitmap->getAddr32(0, 0)); | |
57 | |
58 // Set the config up to use the decoding buffer we created. | |
59 config.output = decoderBuffer; | |
60 | |
61 // Decode the input data into the bitmap buffer. | |
62 bool success = WebPDecode(data.bytes, data.size, &config) == VP8_STATUS_OK; | |
63 | |
64 // Now free the buffer. It is safe to call this even when the buffer is | |
65 // external and not allocated by WebPDecode. | |
66 WebPFreeDecBuffer(&config.output); | |
67 | |
68 if (!success) { | |
69 LOG(WARNING) << "Failed to decode WebP data."; | |
70 return false; | |
71 } | |
72 | |
73 // The input always has an alpha-channel. | |
74 bitmap->setAlphaType(kUnpremul_SkAlphaType); | |
urvang
2016/02/17 01:49:08
Given that you are assuming pre-multiplied always,
nyquist
2016/02/17 16:56:44
Hmm... Maybe I'm not understanding correctly: We g
urvang
2016/02/17 18:48:34
From what I understand, the only important part is
| |
75 | |
76 return true; | |
77 } | |
78 | |
79 } // namespace blimp | |
OLD | NEW |