diff options
Diffstat (limited to 'components/suggestions')
-rw-r--r-- | components/suggestions/BUILD.gn | 12 | ||||
-rw-r--r-- | components/suggestions/DEPS | 1 | ||||
-rw-r--r-- | components/suggestions/image_encoder.cc | 28 | ||||
-rw-r--r-- | components/suggestions/image_encoder.h | 24 | ||||
-rw-r--r-- | components/suggestions/image_encoder_ios.h | 24 | ||||
-rw-r--r-- | components/suggestions/image_encoder_ios.mm | 34 | ||||
-rw-r--r-- | components/suggestions/image_manager.cc | 33 | ||||
-rw-r--r-- | components/suggestions/image_manager.h | 5 | ||||
-rw-r--r-- | components/suggestions/image_manager_unittest.cc | 8 |
9 files changed, 138 insertions, 31 deletions
diff --git a/components/suggestions/BUILD.gn b/components/suggestions/BUILD.gn index 12c9576..b540faf 100644 --- a/components/suggestions/BUILD.gn +++ b/components/suggestions/BUILD.gn @@ -30,4 +30,16 @@ static_library("suggestions") { "//ui/gfx", "//url", ] + + if (is_ios) { + sources += [ + "image_encoder_ios.h", + "image_encoder_ios.mm", + ] + } else { + sources += [ + "image_encoder.cc", + "image_encoder.h", + ] + } } diff --git a/components/suggestions/DEPS b/components/suggestions/DEPS index 557fff6..cf3bb09 100644 --- a/components/suggestions/DEPS +++ b/components/suggestions/DEPS @@ -4,6 +4,7 @@ include_rules = [ "+components/pref_registry", "+components/variations", "+net", + "+skia/ext", "+ui", "+url", ] diff --git a/components/suggestions/image_encoder.cc b/components/suggestions/image_encoder.cc new file mode 100644 index 0000000..bcceaae --- /dev/null +++ b/components/suggestions/image_encoder.cc @@ -0,0 +1,28 @@ +// Copyright 2014 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/suggestions/image_encoder.h" + +#include "ui/gfx/codec/jpeg_codec.h" +#include "ui/gfx/image/image_skia.h" + +namespace suggestions { + +SkBitmap* DecodeJPEGToSkBitmap(const std::vector<unsigned char>& encoded_data) { + return gfx::JPEGCodec::Decode(&encoded_data[0], encoded_data.size()); +} + +bool EncodeSkBitmapToJPEG(const SkBitmap& bitmap, + std::vector<unsigned char>* dest) { + SkAutoLockPixels bitmap_lock(bitmap); + if (!bitmap.readyToDraw() || bitmap.isNull()) { + return false; + } + return gfx::JPEGCodec::Encode( + reinterpret_cast<unsigned char*>(bitmap.getAddr32(0, 0)), + gfx::JPEGCodec::FORMAT_SkBitmap, bitmap.width(), bitmap.height(), + bitmap.rowBytes(), 100, dest); +} + +} // namespace suggestions diff --git a/components/suggestions/image_encoder.h b/components/suggestions/image_encoder.h new file mode 100644 index 0000000..f32314a --- /dev/null +++ b/components/suggestions/image_encoder.h @@ -0,0 +1,24 @@ +// Copyright 2014 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. + +#ifndef COMPONENTS_SUGGESTIONS_IMAGE_ENCODER_H_ +#define COMPONENTS_SUGGESTIONS_IMAGE_ENCODER_H_ + +#include <vector> + +class SkBitmap; + +namespace suggestions { + +// From encoded bytes to SkBitmap. It's the caller's responsibility to delete +// the bitmap. +SkBitmap* DecodeJPEGToSkBitmap(const std::vector<unsigned char>& encoded_data); + +// From SkBitmap to a vector of JPEG-encoded bytes, |dst|. +bool EncodeSkBitmapToJPEG(const SkBitmap& bitmap, + std::vector<unsigned char>* dest); + +} // namespace suggestions + +#endif // COMPONENTS_SUGGESTIONS_IMAGE_ENCODER_H_ diff --git a/components/suggestions/image_encoder_ios.h b/components/suggestions/image_encoder_ios.h new file mode 100644 index 0000000..50741ac --- /dev/null +++ b/components/suggestions/image_encoder_ios.h @@ -0,0 +1,24 @@ +// Copyright 2014 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. + +#ifndef COMPONENTS_SUGGESTIONS_IMAGE_ENCODER_IOS_H_ +#define COMPONENTS_SUGGESTIONS_IMAGE_ENCODER_IOS_H_ + +#include <vector> + +class SkBitmap; + +namespace suggestions { + +// From encoded bytes to SkBitmap. It's the caller's responsibility to delete +// the bitmap. +SkBitmap* DecodeJPEGToSkBitmap(const std::vector<unsigned char>& encoded_data); + +// From SkBitmap to a vector of JPEG-encoded bytes, |dst|. +bool EncodeSkBitmapToJPEG(const SkBitmap& bitmap, + std::vector<unsigned char>* dest); + +} // namespace suggestions + +#endif // COMPONENTS_SUGGESTIONS_IMAGE_ENCODER_IOS_H_ diff --git a/components/suggestions/image_encoder_ios.mm b/components/suggestions/image_encoder_ios.mm new file mode 100644 index 0000000..5e8733b --- /dev/null +++ b/components/suggestions/image_encoder_ios.mm @@ -0,0 +1,34 @@ +// Copyright 2014 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/suggestions/image_encoder_ios.h" + +#import <UIKit/UIKit.h> + +#include "base/mac/scoped_cftyperef.h" +#include "skia/ext/skia_utils_ios.h" + +namespace suggestions { + +SkBitmap* DecodeJPEGToSkBitmap(const std::vector<unsigned char>& encoded_data) { + NSData* data = + [NSData dataWithBytes:encoded_data.data() length:encoded_data.size()]; + UIImage* image = + [UIImage imageWithData:data scale:1.0]; + return new SkBitmap(gfx::CGImageToSkBitmap(image.CGImage, [image size], YES)); +} + +bool EncodeSkBitmapToJPEG(const SkBitmap& bitmap, + std::vector<unsigned char>* dest) { + base::ScopedCFTypeRef<CGColorSpaceRef> color_space( + CGColorSpaceCreateDeviceRGB()); + UIImage* image = + gfx::SkBitmapToUIImageWithColorSpace(bitmap, 1 /* scale */, color_space); + NSData* data = UIImageJPEGRepresentation(image, 1.0); + const char* bytes = reinterpret_cast<const char*>([data bytes]); + dest->assign(bytes, bytes + [data length]); + return true; +} + +} // namespace suggestions diff --git a/components/suggestions/image_manager.cc b/components/suggestions/image_manager.cc index 9e43381..5309e0b 100644 --- a/components/suggestions/image_manager.cc +++ b/components/suggestions/image_manager.cc @@ -6,18 +6,14 @@ #include "base/bind.h" #include "components/suggestions/image_fetcher.h" -#include "ui/gfx/codec/jpeg_codec.h" -using leveldb_proto::ProtoDatabase; - -namespace { - -// From JPEG-encoded bytes to SkBitmap. -SkBitmap* DecodeImage(const std::vector<unsigned char>& encoded_data) { - return gfx::JPEGCodec::Decode(&encoded_data[0], encoded_data.size()); -} +#if defined(OS_IOS) +#include "components/suggestions/image_encoder_ios.h" +#else +#include "components/suggestions/image_encoder.h" +#endif -} // namespace +using leveldb_proto::ProtoDatabase; namespace suggestions { @@ -139,7 +135,7 @@ void ImageManager::SaveImage(const GURL& url, const SkBitmap& bitmap) { // Attempt to save a JPEG representation to the database. If not successful, // the fetched bitmap will still be inserted in the cache, above. std::vector<unsigned char> encoded_data; - if (EncodeImage(bitmap, &encoded_data)) { + if (EncodeSkBitmapToJPEG(bitmap, &encoded_data)) { // Save the resulting bitmap to the database. ImageData data; data.set_url(url.spec()); @@ -194,7 +190,7 @@ void ImageManager::LoadEntriesInCache(scoped_ptr<ImageDataVector> entries) { std::vector<unsigned char> encoded_data(it->data().begin(), it->data().end()); - scoped_ptr<SkBitmap> bitmap(DecodeImage(encoded_data)); + scoped_ptr<SkBitmap> bitmap(DecodeJPEGToSkBitmap(encoded_data)); if (bitmap.get()) { image_map_.insert(std::make_pair(it->url(), *bitmap)); } @@ -212,17 +208,4 @@ void ImageManager::ServePendingCacheRequests() { } } -// static -bool ImageManager::EncodeImage(const SkBitmap& bitmap, - std::vector<unsigned char>* dest) { - SkAutoLockPixels bitmap_lock(bitmap); - if (!bitmap.readyToDraw() || bitmap.isNull()) { - return false; - } - return gfx::JPEGCodec::Encode( - reinterpret_cast<unsigned char*>(bitmap.getAddr32(0, 0)), - gfx::JPEGCodec::FORMAT_SkBitmap, bitmap.width(), bitmap.height(), - bitmap.rowBytes(), 100, dest); -} - } // namespace suggestions diff --git a/components/suggestions/image_manager.h b/components/suggestions/image_manager.h index 02ec268..57ead85 100644 --- a/components/suggestions/image_manager.h +++ b/components/suggestions/image_manager.h @@ -119,11 +119,6 @@ class ImageManager : public ImageFetcherDelegate { void ServePendingCacheRequests(); - // From SkBitmap to the vector of JPEG-encoded bytes, |dst|. Visible only for - // testing. - static bool EncodeImage(const SkBitmap& bitmap, - std::vector<unsigned char>* dest); - // Map from URL to image URL. Should be kept up to date when a new // SuggestionsProfile is available. std::map<GURL, GURL> image_url_map_; diff --git a/components/suggestions/image_manager_unittest.cc b/components/suggestions/image_manager_unittest.cc index 57dd8bf..e0f67a2 100644 --- a/components/suggestions/image_manager_unittest.cc +++ b/components/suggestions/image_manager_unittest.cc @@ -17,6 +17,12 @@ #include "ui/gfx/image/image_skia.h" #include "url/gurl.h" +#if defined(OS_IOS) +#include "components/suggestions/image_encoder_ios.h" +#else +#include "components/suggestions/image_encoder.h" +#endif + using ::testing::Return; using ::testing::StrictMock; using ::testing::_; @@ -92,7 +98,7 @@ class ImageManagerTest : public testing::Test { ImageData data; data.set_url(url); std::vector<unsigned char> encoded; - EXPECT_TRUE(ImageManager::EncodeImage(bm, &encoded)); + EXPECT_TRUE(EncodeSkBitmapToJPEG(bm, &encoded)); data.set_data(std::string(encoded.begin(), encoded.end())); return data; } |