Index: services/image_decoder/services/image_decoder_impl.cc |
diff --git a/services/image_decoder/services/image_decoder_impl.cc b/services/image_decoder/services/image_decoder_impl.cc |
new file mode 100644 |
index 0000000000000000000000000000000000000000..f7d09547836dadd67502f421a6c207b13f82eaa5 |
--- /dev/null |
+++ b/services/image_decoder/services/image_decoder_impl.cc |
@@ -0,0 +1,95 @@ |
+// 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 "services/image_decoder/services/image_decoder_impl.h" |
+ |
+#include <string.h> |
+ |
+#include "content/public/child/image_decoder_utils.h" |
+#include "ipc/ipc_channel.h" |
+#include "services/image_decoder/public/type_converters.h" |
+#include "skia/ext/image_operations.h" |
+#include "third_party/skia/include/core/SkBitmap.h" |
+#include "ui/gfx/codec/jpeg_codec.h" |
+#include "ui/gfx/geometry/size.h" |
+ |
+ |
+namespace services { |
+namespace image_decoder { |
+ |
+namespace { |
+// TODO(amistry): Replace with more correct value. |
+int64_t kMaxMessageSize = IPC::Channel::kMaximumMessageSize; |
+} |
+ |
+ImageDecoderImpl::ImageDecoderImpl(mojo::InterfaceRequest<ImageDecoder> request) |
+ : 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(bool, ImagePtr)>& callback) { |
+ LOG(INFO) << "ImageDecoderImpl::DecodeImage: " << encoded_data.size() |
+ << ", " << codec << ", " << shrink_to_fit; |
+ |
+ 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 { |
+ LOG(ERROR) << "Rebust JPEG decode failed"; |
+ } |
+ } else { |
+ LOG(ERROR) << "Unable to do robust JPEG decode"; |
+ } |
+ } else { |
+ decoded_image = content::DecodeImage(&encoded_data[0], |
+ gfx::Size(), |
+ encoded_data.size()); |
+ } |
+ |
+ if (!decoded_image.isNull()) { |
+ ImagePtr dummy_image = Image::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) > kMaxMessageSize) |
+ 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 { |
+ LOG(ERROR) << "Image too big and not shrinking"; |
+ decoded_image.reset(); |
+ } |
+ } |
+ } |
+ |
+ if (decoded_image.isNull()) { |
+ LOG(ERROR) << "No decoded image"; |
+ callback.Run(false, nullptr); |
+ return; |
+ } |
+ |
+ callback.Run(true, Image::From(decoded_image)); |
+ LOG(INFO) << "............Done decoding image"; |
+} |
+ |
+} // namespace image_decoder |
+} // namespace services |