Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 1 // Copyright 2015 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 #include "components/image_decoder/image_decoder_impl.h" | |
| 5 | |
| 6 #include <string.h> | |
| 7 | |
| 8 #include "base/logging.h" | |
| 9 #include "content/public/child/image_decoder_utils.h" | |
| 10 #include "ipc/ipc_channel.h" | |
| 11 #include "skia/ext/image_operations.h" | |
| 12 #include "skia/public/type_converters.h" | |
| 13 #include "third_party/skia/include/core/SkBitmap.h" | |
| 14 #include "ui/gfx/codec/jpeg_codec.h" | |
| 15 #include "ui/gfx/geometry/size.h" | |
| 16 | |
| 17 namespace image_decoder { | |
| 18 | |
| 19 namespace { | |
| 20 // 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.
| |
| 21 int64_t kMaxMessageSize = IPC::Channel::kMaximumMessageSize; | |
| 22 } | |
| 23 | |
| 24 ImageDecoderImpl::ImageDecoderImpl(int64_t max_message_size) | |
| 25 : max_message_size_(max_message_size), binding_(this) { | |
| 26 } | |
| 27 | |
| 28 ImageDecoderImpl::ImageDecoderImpl(mojo::InterfaceRequest<ImageDecoder> request) | |
| 29 : max_message_size_(kMaxMessageSize), binding_(this, request.Pass()) { | |
| 30 } | |
| 31 | |
| 32 ImageDecoderImpl::~ImageDecoderImpl() { | |
| 33 } | |
| 34 | |
| 35 void ImageDecoderImpl::DecodeImage( | |
| 36 mojo::Array<uint8_t> encoded_data, | |
| 37 ImageCodec codec, | |
| 38 bool shrink_to_fit, | |
| 39 const mojo::Callback<void(skia::BitmapPtr)>& callback) { | |
| 40 if (encoded_data.size() == 0) { | |
| 41 callback.Run(nullptr); | |
| 42 return; | |
| 43 } | |
| 44 | |
| 45 SkBitmap decoded_image; | |
| 46 if (codec == IMAGE_CODEC_ROBUST_JPEG) { | |
| 47 // Our robust jpeg decoding is using IJG libjpeg. | |
| 48 if (gfx::JPEGCodec::JpegLibraryVariant() == gfx::JPEGCodec::IJG_LIBJPEG && | |
| 49 encoded_data.size()) { | |
| 50 scoped_ptr<SkBitmap> decoded_jpeg( | |
| 51 gfx::JPEGCodec::Decode(&encoded_data[0], encoded_data.size())); | |
| 52 if (decoded_jpeg.get() && !decoded_jpeg->empty()) | |
| 53 decoded_image = *decoded_jpeg; | |
| 54 } | |
| 55 } else { | |
| 56 decoded_image = content::DecodeImage(&encoded_data[0], gfx::Size(), | |
| 57 encoded_data.size()); | |
| 58 } | |
| 59 | |
| 60 if (!decoded_image.isNull()) { | |
| 61 skia::BitmapPtr dummy_image = skia::Bitmap::New(); | |
| 62 int64_t struct_size = GetSerializedSize_(dummy_image); | |
| 63 int64_t image_size = decoded_image.computeSize64(); | |
| 64 int halves = 0; | |
| 65 while (struct_size + (image_size >> 2 * halves) > max_message_size_) | |
| 66 halves++; | |
| 67 if (halves) { | |
| 68 if (shrink_to_fit) { | |
| 69 // If decoded image is too large for IPC message, shrink it by halves. | |
| 70 // This prevents quality loss, and should never overshrink on displays | |
| 71 // smaller than 3600x2400. | |
| 72 // TODO (Issue 416916): Instead of shrinking, return via shared memory | |
| 73 decoded_image = skia::ImageOperations::Resize( | |
| 74 decoded_image, skia::ImageOperations::RESIZE_LANCZOS3, | |
| 75 decoded_image.width() >> halves, decoded_image.height() >> halves); | |
| 76 } else { | |
| 77 decoded_image.reset(); | |
| 78 } | |
| 79 } | |
| 80 } | |
| 81 | |
| 82 if (decoded_image.isNull()) | |
| 83 callback.Run(nullptr); | |
| 84 else | |
| 85 callback.Run(skia::Bitmap::From(decoded_image)); | |
| 86 } | |
| 87 | |
| 88 } // namespace image_decoder | |
| OLD | NEW |