Chromium Code Reviews| Index: components/image_decoder/image_decoder_impl.cc |
| diff --git a/components/image_decoder/image_decoder_impl.cc b/components/image_decoder/image_decoder_impl.cc |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..8864b91c22addfaf9b893d3ee83119359da8a9f4 |
| --- /dev/null |
| +++ b/components/image_decoder/image_decoder_impl.cc |
| @@ -0,0 +1,88 @@ |
| +// Copyright 2015 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 "components/image_decoder/image_decoder_impl.h" |
| + |
| +#include <string.h> |
| + |
| +#include "base/logging.h" |
| +#include "content/public/child/image_decoder_utils.h" |
| +#include "ipc/ipc_channel.h" |
| +#include "skia/ext/image_operations.h" |
| +#include "skia/public/type_converters.h" |
| +#include "third_party/skia/include/core/SkBitmap.h" |
| +#include "ui/gfx/codec/jpeg_codec.h" |
| +#include "ui/gfx/geometry/size.h" |
| + |
| +namespace image_decoder { |
| + |
| +namespace { |
| +// TODO(amistry): Replace with more correct value. |
|
Sam McNally
2015/05/06 06:41:39
According to https://code.google.com/p/chromium/co
Anand Mistry (off Chromium)
2015/05/06 07:14:23
Thanks.
|
| +int64_t kMaxMessageSize = IPC::Channel::kMaximumMessageSize; |
| +} |
| + |
| +ImageDecoderImpl::ImageDecoderImpl(int64_t max_message_size) |
| + : max_message_size_(max_message_size), binding_(this) { |
| +} |
| + |
| +ImageDecoderImpl::ImageDecoderImpl(mojo::InterfaceRequest<ImageDecoder> request) |
| + : max_message_size_(kMaxMessageSize), binding_(this, request.Pass()) { |
| +} |
| + |
| +ImageDecoderImpl::~ImageDecoderImpl() { |
| +} |
| + |
| +void ImageDecoderImpl::DecodeImage( |
| + mojo::Array<uint8_t> encoded_data, |
| + ImageCodec codec, |
| + bool shrink_to_fit, |
| + const mojo::Callback<void(skia::BitmapPtr)>& callback) { |
| + if (encoded_data.size() == 0) { |
| + callback.Run(nullptr); |
| + return; |
| + } |
| + |
| + SkBitmap decoded_image; |
| + if (codec == IMAGE_CODEC_ROBUST_JPEG) { |
| + // Our robust jpeg decoding is using IJG libjpeg. |
| + if (gfx::JPEGCodec::JpegLibraryVariant() == gfx::JPEGCodec::IJG_LIBJPEG && |
| + encoded_data.size()) { |
| + scoped_ptr<SkBitmap> decoded_jpeg( |
| + gfx::JPEGCodec::Decode(&encoded_data[0], encoded_data.size())); |
| + if (decoded_jpeg.get() && !decoded_jpeg->empty()) |
| + decoded_image = *decoded_jpeg; |
| + } |
| + } else { |
| + decoded_image = content::DecodeImage(&encoded_data[0], gfx::Size(), |
| + encoded_data.size()); |
| + } |
| + |
| + if (!decoded_image.isNull()) { |
| + skia::BitmapPtr dummy_image = skia::Bitmap::New(); |
| + int64_t struct_size = GetSerializedSize_(dummy_image); |
| + int64_t image_size = decoded_image.computeSize64(); |
| + int halves = 0; |
| + while (struct_size + (image_size >> 2 * halves) > max_message_size_) |
| + halves++; |
| + if (halves) { |
| + if (shrink_to_fit) { |
| + // If decoded image is too large for IPC message, shrink it by halves. |
| + // This prevents quality loss, and should never overshrink on displays |
| + // smaller than 3600x2400. |
| + // TODO (Issue 416916): Instead of shrinking, return via shared memory |
| + decoded_image = skia::ImageOperations::Resize( |
| + decoded_image, skia::ImageOperations::RESIZE_LANCZOS3, |
| + decoded_image.width() >> halves, decoded_image.height() >> halves); |
| + } else { |
| + decoded_image.reset(); |
| + } |
| + } |
| + } |
| + |
| + if (decoded_image.isNull()) |
| + callback.Run(nullptr); |
| + else |
| + callback.Run(skia::Bitmap::From(decoded_image)); |
| +} |
| + |
| +} // namespace image_decoder |