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.
5 #include "ui/gfx/image/image.h"
9 #import <UIKit/UIKit.h>
11 #include "base/logging.h"
12 #include "base/mac/scoped_cftyperef.h"
13 #include "base/memory/scoped_nsobject.h"
14 #include "ui/base/layout.h"
15 #include "ui/gfx/image/image_png_rep.h"
16 #include "ui/gfx/image/image_skia.h"
17 #include "ui/gfx/image/image_skia_util_ios.h"
18 #include "ui/gfx/size.h"
25 // Returns a 16x16 red UIImage to visually show when a UIImage cannot be
26 // created from PNG data. Logs error as well.
27 // Caller takes ownership of returned UIImage.
28 UIImage* CreateErrorUIImage(float scale) {
29 LOG(ERROR) << "Unable to decode PNG into UIImage.";
30 base::mac::ScopedCFTypeRef<CGColorSpaceRef> color_space(
31 CGColorSpaceCreateDeviceRGB());
32 base::mac::ScopedCFTypeRef<CGContextRef> context(
33 CGBitmapContextCreate(NULL, // Allow CG to allocate memory.
36 8, // bitsPerComponent
37 0, // CG will calculate by default.
39 kCGImageAlphaPremultipliedFirst |
40 kCGBitmapByteOrder32Host));
41 CGContextSetRGBFillColor(context, 1.0, 0.0, 0.0, 1.0);
42 CGContextFillRect(context, CGRectMake(0.0, 0.0, 16, 16));
43 base::mac::ScopedCFTypeRef<CGImageRef> cg_image(
44 CGBitmapContextCreateImage(context));
45 return [[UIImage imageWithCGImage:cg_image.get()
47 orientation:UIImageOrientationUp] retain];
50 // Converts from ImagePNGRep to UIImage.
51 UIImage* CreateUIImageFromImagePNGRep(const gfx::ImagePNGRep& image_png_rep) {
52 float scale = ui::GetScaleFactorScale(image_png_rep.scale_factor);
53 scoped_refptr<base::RefCountedMemory> png = image_png_rep.raw_data;
55 NSData* data = [NSData dataWithBytes:png->front() length:png->size()];
56 UIImage* image = [[UIImage alloc] initWithData:data scale:scale];
57 return image ? image : CreateErrorUIImage(scale);
62 scoped_refptr<base::RefCountedMemory> Get1xPNGBytesFromUIImage(
64 NSData* data = UIImagePNGRepresentation(uiimage);
66 if ([data length] == 0)
69 scoped_refptr<base::RefCountedBytes> png_bytes(
70 new base::RefCountedBytes());
71 png_bytes->data().resize([data length]);
72 [data getBytes:&png_bytes->data().at(0) length:[data length]];
76 UIImage* CreateUIImageFromPNG(
77 const std::vector<gfx::ImagePNGRep>& image_png_reps) {
78 ui::ScaleFactor ideal_scale_factor = ui::GetMaxScaleFactor();
79 float ideal_scale = ui::GetScaleFactorScale(ideal_scale_factor);
81 if (image_png_reps.empty())
82 return CreateErrorUIImage(ideal_scale);
84 // Find best match for |ideal_scale_factor|.
85 float smallest_diff = std::numeric_limits<float>::max();
86 size_t closest_index = 0u;
87 for (size_t i = 0; i < image_png_reps.size(); ++i) {
88 float scale = ui::GetScaleFactorScale(image_png_reps[i].scale_factor);
89 float diff = std::abs(ideal_scale - scale);
90 if (diff < smallest_diff) {
96 return CreateUIImageFromImagePNGRep(image_png_reps[closest_index]);
99 scoped_refptr<base::RefCountedMemory> Get1xPNGBytesFromImageSkia(
100 const ImageSkia* skia) {
101 // iOS does not expose libpng, so conversion from ImageSkia to PNG must go
103 // TODO(rohitrao): Rewrite the callers of this function to save the UIImage
104 // representation in the gfx::Image. If we're generating it, we might as well
106 const gfx::ImageSkiaRep& image_skia_rep = skia->GetRepresentation(
107 ui::SCALE_FACTOR_100P);
108 if (image_skia_rep.scale_factor() != ui::SCALE_FACTOR_100P)
111 UIImage* image = UIImageFromImageSkiaRep(image_skia_rep);
112 return Get1xPNGBytesFromUIImage(image);
115 ImageSkia* ImageSkiaFromPNG(
116 const std::vector<gfx::ImagePNGRep>& image_png_reps) {
117 // iOS does not expose libpng, so conversion from PNG to ImageSkia must go
119 gfx::ImageSkia* image_skia = new gfx::ImageSkia();
120 for (size_t i = 0; i < image_png_reps.size(); ++i) {
121 scoped_nsobject<UIImage> uiimage(CreateUIImageFromImagePNGRep(
123 gfx::ImageSkiaRep image_skia_rep = ImageSkiaRepOfScaleFactorFromUIImage(
124 uiimage, image_png_reps[i].scale_factor);
125 if (!image_skia_rep.is_null())
126 image_skia->AddRepresentation(image_skia_rep);
131 gfx::Size UIImageSize(UIImage* image) {
132 int width = static_cast<int>(image.size.width);
133 int height = static_cast<int>(image.size.height);
134 return gfx::Size(width, height);
137 } // namespace internal