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; | |
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 // Assuming only one frame. | |
urvang
2016/02/10 23:54:42
A few choices here, based on the expected behavior
nyquist
2016/02/11 05:19:23
Going for (2) for now, using WebPDecode. I'll defi
| |
29 WebPData inputData = {reinterpret_cast<const uint8_t*>(input), input_size}; | |
30 WebPDemuxState demuxState(WEBP_DEMUX_PARSING_HEADER); | |
31 WebPDemuxer* demux = WebPDemuxPartial(&inputData, &demuxState); | |
32 | |
33 uint32_t width = WebPDemuxGetI(demux, WEBP_FF_CANVAS_WIDTH); | |
34 uint32_t height = WebPDemuxGetI(demux, WEBP_FF_CANVAS_HEIGHT); | |
35 // ... (Get information about the features present in the WebP file). | |
36 uint32_t flags = WebPDemuxGetI(demux, WEBP_FF_FORMAT_FLAGS); | |
37 | |
38 DCHECK(bitmap); | |
39 bitmap->allocN32Pixels(width, height); | |
40 | |
41 // TODO(nyquist): Iterate over all frames. | |
42 WebPIterator iter; | |
43 bool success = false; | |
44 if (WebPDemuxGetFrame(demux, 1, &iter)) { | |
45 // do { | |
46 // // ... (Consume 'iter'; e.g. Decode 'iter.fragment' with WebPDecode(), | |
47 // // ... and get other frame properties like width, height, offsets etc. | |
48 // // ... see 'struct WebPIterator' below for more info). | |
49 // } while (WebPDemuxNextFrame(&iter)); | |
50 WebPDecBuffer decoderBuffer; | |
51 WebPIDecoder* decoder = WebPINewDecoder(&decoderBuffer); | |
urvang
2016/02/10 23:54:43
Note that WebPIDecoder is used for incremental dec
nyquist
2016/02/11 05:19:23
Thanks! That was for sure simpler.
Updated to use
| |
52 WebPInitDecBuffer(&decoderBuffer); | |
53 WEBP_CSP_MODE mode = outputMode((flags & ALPHA_FLAG) != 0); | |
54 decoderBuffer.colorspace = mode; | |
55 decoderBuffer.u.RGBA.stride = width * 4; | |
56 decoderBuffer.u.RGBA.size = decoderBuffer.u.RGBA.stride * height; | |
57 decoderBuffer.is_external_memory = 1; | |
58 decoderBuffer.u.RGBA.rgba = | |
59 reinterpret_cast<uint8_t*>(bitmap->getAddr32(0, 0)); | |
60 | |
61 success = | |
62 VP8_STATUS_OK == | |
63 WebPIUpdate(decoder, static_cast<const uint8_t*>(input), input_size); | |
64 // applyPostProcessing(1); | |
65 WebPIDelete(decoder); | |
66 WebPDemuxReleaseIterator(&iter); | |
67 } | |
68 WebPDemuxDelete(demux); | |
69 | |
70 // Set the bitmap's opaqueness based on what we saw. | |
71 bitmap->setAlphaType(flags & ALPHA_FLAG ? kPremul_SkAlphaType | |
72 : kOpaque_SkAlphaType); | |
73 | |
74 return success; | |
75 } | |
76 | |
77 } // namespace blimp | |
OLD | NEW |