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/browser/bookmarks/enhanced/image_store_ios.h" |
| 5 |
| 6 #import <UIKit/UIKit.h> |
| 7 |
| 8 #include "base/mac/scoped_cftyperef.h" |
| 9 #include "base/mac/scoped_nsobject.h" |
| 10 |
| 11 namespace { |
| 12 // An implementation of RefCountedMemory, where the bytes are stored in a |
| 13 // NSData. This class assumes the NSData is non mutable to avoid a copy. |
| 14 class RefCountedNSDataMemory : public base::RefCountedMemory { |
| 15 public: |
| 16 explicit RefCountedNSDataMemory(NSData* memory) : data_([memory retain]) {} |
| 17 |
| 18 virtual const unsigned char* front() const OVERRIDE { |
| 19 return reinterpret_cast<const unsigned char*>([data_ bytes]); |
| 20 } |
| 21 |
| 22 virtual size_t size() const OVERRIDE { |
| 23 return [data_ length]; |
| 24 } |
| 25 |
| 26 private: |
| 27 virtual ~RefCountedNSDataMemory() {} |
| 28 |
| 29 base::scoped_nsobject<NSData> data_; |
| 30 DISALLOW_COPY_AND_ASSIGN(RefCountedNSDataMemory); |
| 31 }; |
| 32 } // namespace |
| 33 |
| 34 namespace image_store_ios { |
| 35 |
| 36 // Encodes the UIImage representation of a gfx::Image. |
| 37 const scoped_refptr<base::RefCountedMemory> bytesForImage(gfx::Image image) { |
| 38 DCHECK(image.HasRepresentation(gfx::Image::kImageRepCocoaTouch)); |
| 39 return scoped_refptr<RefCountedNSDataMemory>(new RefCountedNSDataMemory( |
| 40 [NSKeyedArchiver archivedDataWithRootObject:image.ToUIImage()])); |
| 41 } |
| 42 |
| 43 // Decodes the UIImage in the bytes and returns a gfx::Image. |
| 44 gfx::Image imageForBytes(const scoped_refptr<base::RefCountedMemory>& data) { |
| 45 return gfx::Image([[NSKeyedUnarchiver unarchiveObjectWithData: |
| 46 [NSData dataWithBytes:data->front() length:data->size()]] retain]); |
| 47 } |
| 48 |
| 49 // Generates a gfx::Image with a random UIImage representation. Used for tests. |
| 50 gfx::Image generateRandomUIImage(gfx::Size& size, float scale) { |
| 51 UIGraphicsBeginImageContextWithOptions(CGSizeMake(size.width(), |
| 52 size.height()), |
| 53 YES, // opaque. |
| 54 scale); |
| 55 |
| 56 // Create the gradient's colors. |
| 57 CGFloat locations[] = { 0.0, 1.0 }; |
| 58 CGFloat components[] = { rand()/CGFloat(RAND_MAX), // Start color r |
| 59 rand()/CGFloat(RAND_MAX), // g |
| 60 rand()/CGFloat(RAND_MAX), // b |
| 61 1.0, // Alpha |
| 62 rand()/CGFloat(RAND_MAX), // End color r |
| 63 rand()/CGFloat(RAND_MAX), // g |
| 64 rand()/CGFloat(RAND_MAX), // b |
| 65 1.0 }; // Alpha |
| 66 CGPoint center = CGPointMake(size.width() / 2, size.height() / 2); |
| 67 CGFloat radius = MIN(size.width(), size.height()); |
| 68 |
| 69 base::ScopedCFTypeRef<CGColorSpaceRef> |
| 70 colorspace(CGColorSpaceCreateDeviceRGB()); |
| 71 base::ScopedCFTypeRef<CGGradientRef> |
| 72 gradient(CGGradientCreateWithColorComponents(colorspace, |
| 73 components, |
| 74 locations, |
| 75 arraysize(locations))); |
| 76 CGContextDrawRadialGradient(UIGraphicsGetCurrentContext(), |
| 77 gradient, |
| 78 center, |
| 79 0, |
| 80 center, |
| 81 radius, |
| 82 kCGGradientDrawsAfterEndLocation); |
| 83 UIImage* image = UIGraphicsGetImageFromCurrentImageContext(); |
| 84 UIGraphicsEndImageContext(); |
| 85 return gfx::Image([image retain]); |
| 86 } |
| 87 } // namespace image_store_ios |
OLD | NEW |