Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(774)

Unified Diff: blimp/common/compositor/webp_decoder.cc

Issue 1680333004: Add support for encoding/decoding WebP images. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@serialize-deserialize-images-framework
Patch Set: Simplify WebPDecoder and assume premultiplied alpha channel Created 4 years, 10 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
Index: blimp/common/compositor/webp_decoder.cc
diff --git a/blimp/common/compositor/webp_decoder.cc b/blimp/common/compositor/webp_decoder.cc
new file mode 100644
index 0000000000000000000000000000000000000000..88737d8712cb6be39b5305a5ec0647d8fec6dd20
--- /dev/null
+++ b/blimp/common/compositor/webp_decoder.cc
@@ -0,0 +1,85 @@
+// Copyright 2016 The Chromium Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+#include "blimp/common/compositor/webp_decoder.h"
+
+#include "base/logging.h"
+#include "third_party/libwebp/webp/decode.h"
+#include "third_party/libwebp/webp/demux.h"
+#include "third_party/skia/include/core/SkBitmap.h"
+
+namespace {
+#if SK_B32_SHIFT // Output little-endian RGBA pixels (Android).
+inline WEBP_CSP_MODE outputMode(bool hasAlpha) {
+ 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.
+}
+#else // Output little-endian BGRA pixels.
+inline WEBP_CSP_MODE outputMode(bool hasAlpha) {
+ return hasAlpha ? MODE_bgrA : MODE_BGRA;
+}
+#endif
+
+} // namespace
+
+namespace blimp {
+
+bool WebPDecoder(const void* input, size_t input_size, SkBitmap* bitmap) {
+ DCHECK(bitmap);
+
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
+ // Initialize an empty WebPDecoderConfig.
+ WebPDecoderConfig config;
+ if (!WebPInitDecoderConfig(&config)) {
+ LOG(WARNING) << "Failed to initialize WebP config.";
+ return false;
+ }
+
+ // Treat the input as uint8_t.
+ WebPData data = {reinterpret_cast<const uint8_t*>(input), input_size};
+
+ // Read WebP feature information into |config.input|, which is a
+ // WebPBitstreamFeatures. It contains information such as width, height and
+ // whether the WebP image has an alpha channel or not.
+ if (WebPGetFeatures(data.bytes, data.size, &config.input) != VP8_STATUS_OK) {
+ LOG(WARNING) << "Failed to get WebP features.";
+ return false;
+ }
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.
+
+ // Allocate correct size for the bitmap based on the WebPBitstreamFeatures.
+ bitmap->allocN32Pixels(config.input.width, config.input.height);
+
+ // Setup the decoder buffer based on the WebPBitstreamFeatures.
+ WebPDecBuffer decoderBuffer;
+ decoderBuffer.colorspace = outputMode(config.input.has_alpha);
+ decoderBuffer.u.RGBA.stride = config.input.width * 4;
+ decoderBuffer.u.RGBA.size = decoderBuffer.u.RGBA.stride * config.input.height;
+
+ // Instead of using the default WebPDecBuffer output, make WebPDecode directly
+ // write into the SkBitmap.
+ decoderBuffer.is_external_memory = 1;
+ decoderBuffer.u.RGBA.rgba =
+ reinterpret_cast<uint8_t*>(bitmap->getAddr32(0, 0));
+
+ // Set the config up to use the decoding buffer we created.
+ config.output = decoderBuffer;
+
+ // Decode the input data into the bitmap buffer.
+ bool success = WebPDecode(data.bytes, data.size, &config) == VP8_STATUS_OK;
+
+ // Now free the buffer. It is safe to call this even when the buffer is
+ // external and not allocated by WebPDecode.
+ WebPFreeDecBuffer(&config.output);
+
+ if (!success) {
+ LOG(WARNING) << "Failed to decode WebP data.";
+ return false;
+ }
+
+ // Set the bitmap's opaqueness based on what we saw.
+ 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.
+ : kOpaque_SkAlphaType);
+
+ return true;
+}
+
+} // namespace blimp

Powered by Google App Engine
This is Rietveld 408576698