OLD | NEW |
(Empty) | |
| 1 // Copyright 2014 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 "chrome/utility/image_decoder_impl.h" |
| 5 |
| 6 #include <string.h> |
| 7 |
| 8 #include "base/base64.h" |
| 9 #include "base/memory/ref_counted.h" |
| 10 #include "base/time/time.h" |
| 11 #include "chrome/common/chrome_utility_messages.h" |
| 12 #include "content/child/child_process.h" |
| 13 #include "content/public/child/image_decoder_utils.h" |
| 14 #include "content/public/common/content_switches.h" |
| 15 #include "skia/ext/image_operations.h" |
| 16 #include "third_party/skia/include/core/SkBitmap.h" |
| 17 #include "ui/gfx/codec/jpeg_codec.h" |
| 18 #include "ui/gfx/geometry/size.h" |
| 19 |
| 20 namespace mojo { |
| 21 |
| 22 // TODO(amistry): Move somewhere else. |
| 23 template <> |
| 24 struct TypeConverter<content::ImageDataPtr, SkBitmap> { |
| 25 static content::ImageDataPtr Convert(const SkBitmap& bitmap) { |
| 26 content::ImageDataPtr result = content::ImageData::New(); |
| 27 const SkImageInfo& info = bitmap.info(); |
| 28 result->color_type = info.fColorType; |
| 29 result->alpha_type = info.fAlphaType; |
| 30 result->width = info.fWidth; |
| 31 result->height = info.fHeight; |
| 32 SkAutoLockPixels lock(bitmap); |
| 33 size_t size = bitmap.getSize(); |
| 34 result->pixels = mojo::Array<uint8_t>::New(size); |
| 35 memcpy(&result->pixels[0], bitmap.getPixels(), size); |
| 36 return result.Pass(); |
| 37 } |
| 38 }; |
| 39 |
| 40 } // namespace mojo |
| 41 |
| 42 namespace content { |
| 43 |
| 44 ImageDecoderImpl::ImageDecoderImpl() { |
| 45 } |
| 46 |
| 47 ImageDecoderImpl::~ImageDecoderImpl() { |
| 48 // TODO(amistry): Single process mode. |
| 49 // TODO(amistry): This is the wrong place to do this. |
| 50 LOG(INFO) << "Possibly destroying utility process"; |
| 51 content::ChildProcess::current()->ReleaseProcess(); |
| 52 } |
| 53 |
| 54 void ImageDecoderImpl::DecodeImage( |
| 55 mojo::Array<uint8_t> encoded_data, |
| 56 bool use_robust_jpeg, |
| 57 bool shrink_to_fit, |
| 58 const mojo::Callback<void(bool, ImageDataPtr)>& callback) { |
| 59 LOG(INFO) << "ImageDecoderImpl::DecodeImage: " << encoded_data.size() |
| 60 << ", " << use_robust_jpeg << ", " << shrink_to_fit; |
| 61 if (use_robust_jpeg) { |
| 62 // Our robust jpeg decoding is using IJG libjpeg. |
| 63 if (gfx::JPEGCodec::JpegLibraryVariant() == gfx::JPEGCodec::IJG_LIBJPEG && |
| 64 encoded_data.size()) { |
| 65 scoped_ptr<SkBitmap> decoded_image(gfx::JPEGCodec::Decode( |
| 66 &encoded_data[0], encoded_data.size())); |
| 67 if (decoded_image.get() && !decoded_image->empty()) { |
| 68 ImageDataPtr image = ImageData::From(*decoded_image); |
| 69 LOG(INFO) << "............Done decoding image"; |
| 70 callback.Run(true, image.Pass()); |
| 71 return; |
| 72 } |
| 73 } |
| 74 LOG(ERROR) << "Unable to do robust JPEG decode"; |
| 75 callback.Run(false, ImageDataPtr()); |
| 76 } else { |
| 77 SkBitmap decoded_image = content::DecodeImage(&encoded_data[0], |
| 78 gfx::Size(), |
| 79 encoded_data.size()); |
| 80 |
| 81 ImageDataPtr image = ImageData::From(decoded_image); |
| 82 // TODO(amistry): Do this! |
| 83 // TODO(amistry): Resizing is (should be) orthogonal to decoding. |
| 84 if (GetSerializedSize_(image) > 32*1024*1024) { |
| 85 if (shrink_to_fit) { |
| 86 LOG(ERROR) << "Resize currently unsupported"; |
| 87 } else { |
| 88 // Image too big for IPC message, but caller didn't request resize; |
| 89 LOG(ERROR) << "Decoded image too large for IPC message"; |
| 90 } |
| 91 callback.Run(false, ImageDataPtr()); |
| 92 return; |
| 93 } |
| 94 |
| 95 callback.Run(true, image.Pass()); |
| 96 } |
| 97 LOG(INFO) << "............Done decoding image"; |
| 98 /* |
| 99 int64_t struct_size = sizeof(ChromeUtilityHostMsg_DecodeImage_Succeeded); |
| 100 int64_t image_size = decoded_image.computeSize64(); |
| 101 int halves = 0; |
| 102 while (struct_size + (image_size >> 2*halves) > max_ipc_message_size_) |
| 103 halves++; |
| 104 if (halves) { |
| 105 if (shrink_to_fit) { |
| 106 // If decoded image is too large for IPC message, shrink it by halves. |
| 107 // This prevents quality loss, and should never overshrink on displays |
| 108 // smaller than 3600x2400. |
| 109 // TODO (Issue 416916): Instead of shrinking, return via shared memory |
| 110 decoded_image = skia::ImageOperations::Resize( |
| 111 decoded_image, skia::ImageOperations::RESIZE_LANCZOS3, |
| 112 decoded_image.width() >> halves, decoded_image.height() >> halves); |
| 113 } else { |
| 114 decoded_image.reset(); |
| 115 } |
| 116 } |
| 117 return decoded_image; |
| 118 */ |
| 119 } |
| 120 |
| 121 void ImageDecoderImpl::DecodeImageBase64( |
| 122 const mojo::String& encoded_data, |
| 123 const mojo::Callback<void(bool, ImageDataPtr)>& callback) { |
| 124 std::string decoded_data; |
| 125 if (!base::Base64Decode(encoded_data.get(), &decoded_data)) { |
| 126 LOG(ERROR) << "Unable to base64 decode image"; |
| 127 callback.Run(false, ImageDataPtr()); |
| 128 return; |
| 129 } |
| 130 |
| 131 mojo::Array<uint8_t> decoded_array(decoded_data.size()); |
| 132 // TODO(amistry): This seems wrong. |
| 133 memcpy(&decoded_array[0], decoded_data.data(), decoded_data.size()); |
| 134 DecodeImage(decoded_array.Pass(), false, false, callback); |
| 135 } |
| 136 |
| 137 } // namespace content |
OLD | NEW |