Chromium Code Reviews| Index: chrome/browser/bookmarks/enhanced/image_store_ios.mm |
| diff --git a/chrome/browser/bookmarks/enhanced/image_store_ios.mm b/chrome/browser/bookmarks/enhanced/image_store_ios.mm |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..3c0d669296fa7fbfe82d318f8e6ca06e9b56d6aa |
| --- /dev/null |
| +++ b/chrome/browser/bookmarks/enhanced/image_store_ios.mm |
| @@ -0,0 +1,87 @@ |
| +// 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 "chrome/browser/bookmarks/enhanced/image_store_ios.h" |
| + |
| +#import <UIKit/UIKit.h> |
| + |
| +#include "base/mac/scoped_cftyperef.h" |
| +#include "base/mac/scoped_nsobject.h" |
| + |
| +namespace { |
| +// An implementation of RefCountedMemory, where the bytes are stored in a |
| +// NSData. This class assumes the NSData is non mutable to avoid a copy. |
| +class RefCountedNSDataMemory : public base::RefCountedMemory { |
| + public: |
| + explicit RefCountedNSDataMemory(NSData* memory) : data_([memory retain]) {} |
| + |
| + virtual const unsigned char* front() const OVERRIDE { |
| + return reinterpret_cast<const unsigned char*>([data_ bytes]); |
| + } |
| + |
| + virtual size_t size() const OVERRIDE { |
| + return [data_ length]; |
| + } |
| + |
| +private: |
| + virtual ~RefCountedNSDataMemory() {} |
| + |
| +base::scoped_nsobject<NSData> data_; |
|
noyau (Ping after 24h)
2014/04/29 08:17:39
indent.
Kibeom Kim (inactive)
2014/04/29 08:22:48
Done.
|
| + DISALLOW_COPY_AND_ASSIGN(RefCountedNSDataMemory); |
| +}; |
| +} // namespace |
| + |
| +namespace image_store_ios { |
| + |
| +// Encodes the UIImage representation of a gfx::Image. |
| +const scoped_refptr<base::RefCountedMemory> bytesForImage(gfx::Image image) { |
| + DCHECK(image.HasRepresentation(gfx::Image::kImageRepCocoaTouch)); |
| + return scoped_refptr<RefCountedNSDataMemory>(new RefCountedNSDataMemory( |
| + [NSKeyedArchiver archivedDataWithRootObject:image.ToUIImage()])); |
| +} |
| + |
| +// Decodes the UIImage in the bytes and returns a gfx::Image. |
| +gfx::Image imageForBytes(const scoped_refptr<base::RefCountedMemory>& data) { |
| + return gfx::Image([[NSKeyedUnarchiver unarchiveObjectWithData: |
| + [NSData dataWithBytes:data->front() length:data->size()]] retain]); |
| +} |
| + |
| +// Generates a gfx::Image with a random UIImage representation. Used for tests. |
| +gfx::Image generateRandomUIImage(gfx::Size& size, float scale) { |
| + UIGraphicsBeginImageContextWithOptions(CGSizeMake(size.width(), |
| + size.height()), |
| + YES, // opaque. |
| + scale); |
| + |
| + // Create the gradient's colors. |
| + CGFloat locations[] = { 0.0, 1.0 }; |
| + CGFloat components[] = { rand()/CGFloat(RAND_MAX), // Start color r |
| + rand()/CGFloat(RAND_MAX), // g |
| + rand()/CGFloat(RAND_MAX), // b |
| + 1.0, // Alpha |
| + rand()/CGFloat(RAND_MAX), // End color r |
| + rand()/CGFloat(RAND_MAX), // g |
| + rand()/CGFloat(RAND_MAX), // b |
| + 1.0 }; // Alpha |
| + CGPoint center = CGPointMake(size.width() / 2, size.height() / 2); |
| + CGFloat radius = MIN(size.width(), size.height()); |
| + |
| + base::ScopedCFTypeRef<CGColorSpaceRef> |
| + colorspace(CGColorSpaceCreateDeviceRGB()); |
| + base::ScopedCFTypeRef<CGGradientRef> |
| + gradient(CGGradientCreateWithColorComponents(colorspace, |
| + components, |
| + locations, |
| + arraysize(locations))); |
| + CGContextDrawRadialGradient(UIGraphicsGetCurrentContext(), |
| + gradient, |
| + center, |
| + 0, |
| + center, |
| + radius, |
| + kCGGradientDrawsAfterEndLocation); |
| + UIImage* image = UIGraphicsGetImageFromCurrentImageContext(); |
| + UIGraphicsEndImageContext(); |
| + return gfx::Image([image retain]); |
| +} |
| +} // namespace image_store_ios |