OLD | NEW |
(Empty) | |
| 1 // Copyright 2012 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 |
| 5 #include "ui/gfx/image/image.h" |
| 6 |
| 7 #import <UIKit/UIKit.h> |
| 8 |
| 9 #include "base/logging.h" |
| 10 #include "base/memory/scoped_nsobject.h" |
| 11 #include "ui/gfx/image/image_skia.h" |
| 12 #include "ui/gfx/image/image_skia_util_ios.h" |
| 13 |
| 14 namespace gfx { |
| 15 namespace internal { |
| 16 |
| 17 void PNGFromUIImage(UIImage* uiimage, std::vector<unsigned char>* png) { |
| 18 NSData* data = UIImagePNGRepresentation(uiimage); |
| 19 |
| 20 if ([data length] == 0) |
| 21 return; |
| 22 |
| 23 png->resize([data length]); |
| 24 [data getBytes:&png->at(0) length:[data length]]; |
| 25 } |
| 26 |
| 27 UIImage* CreateUIImageFromPNG(const std::vector<unsigned char>& png) { |
| 28 NSData* data = [NSData dataWithBytes:&png.front() length:png.size()]; |
| 29 scoped_nsobject<UIImage> image ([[UIImage alloc] initWithData:data]); |
| 30 if (!image) { |
| 31 LOG(WARNING) << "Unable to decode PNG into UIImage."; |
| 32 // Return a 16x16 red image to visually show error. |
| 33 UIGraphicsBeginImageContext(CGSizeMake(16, 16)); |
| 34 CGContextRef context = UIGraphicsGetCurrentContext(); |
| 35 CGContextSetRGBFillColor(context, 1.0, 0.0, 0.0, 1.0); |
| 36 CGContextFillRect(context, CGRectMake(0.0, 0.0, 16, 16)); |
| 37 image.reset([UIGraphicsGetImageFromCurrentImageContext() retain]); |
| 38 UIGraphicsEndImageContext(); |
| 39 } |
| 40 return image.release(); |
| 41 } |
| 42 |
| 43 void PNGFromImageSkia(const ImageSkia* skia, std::vector<unsigned char>* png) { |
| 44 // iOS does not expose libpng, so conversion from ImageSkia to PNG must go |
| 45 // through UIImage. |
| 46 // TODO(rohitrao): Rewrite the callers of this function to save the UIImage |
| 47 // representation in the gfx::Image. If we're generating it, we might as well |
| 48 // hold on to it. |
| 49 UIImage* image = UIImageFromImageSkia(*skia); |
| 50 PNGFromUIImage(image, png); |
| 51 } |
| 52 |
| 53 ImageSkia* ImageSkiaFromPNG(const std::vector<unsigned char>& png) { |
| 54 // iOS does not expose libpng, so conversion from PNG to ImageSkia must go |
| 55 // through UIImage. |
| 56 scoped_nsobject<UIImage> uiimage(CreateUIImageFromPNG(png)); |
| 57 if (!uiimage) { |
| 58 LOG(WARNING) << "Unable to decode PNG into ImageSkia."; |
| 59 // Return a 16x16 red image to visually show error. |
| 60 SkBitmap bitmap; |
| 61 bitmap.setConfig(SkBitmap::kARGB_8888_Config, 16, 16); |
| 62 bitmap.allocPixels(); |
| 63 bitmap.eraseRGB(0xff, 0, 0); |
| 64 return new ImageSkia(bitmap); |
| 65 } |
| 66 |
| 67 return new ImageSkia(ImageSkiaFromUIImage(uiimage)); |
| 68 } |
| 69 |
| 70 } // namespace internal |
| 71 } // namespace gfx |
OLD | NEW |