OLD | NEW |
1 // Copyright 2012 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "ui/gfx/image/image.h" | 5 #include "ui/gfx/image/image.h" |
6 | 6 |
7 #import <UIKit/UIKit.h> | 7 #import <UIKit/UIKit.h> |
8 #include <cmath> | 8 #include <cmath> |
9 #include <limits> | 9 #include <limits> |
10 | 10 |
(...skipping 30 matching lines...) Expand all Loading... |
41 CGContextFillRect(context, CGRectMake(0.0, 0.0, 16, 16)); | 41 CGContextFillRect(context, CGRectMake(0.0, 0.0, 16, 16)); |
42 base::ScopedCFTypeRef<CGImageRef> cg_image( | 42 base::ScopedCFTypeRef<CGImageRef> cg_image( |
43 CGBitmapContextCreateImage(context)); | 43 CGBitmapContextCreateImage(context)); |
44 return [[UIImage imageWithCGImage:cg_image.get() | 44 return [[UIImage imageWithCGImage:cg_image.get() |
45 scale:scale | 45 scale:scale |
46 orientation:UIImageOrientationUp] retain]; | 46 orientation:UIImageOrientationUp] retain]; |
47 } | 47 } |
48 | 48 |
49 // Converts from ImagePNGRep to UIImage. | 49 // Converts from ImagePNGRep to UIImage. |
50 UIImage* CreateUIImageFromImagePNGRep(const gfx::ImagePNGRep& image_png_rep) { | 50 UIImage* CreateUIImageFromImagePNGRep(const gfx::ImagePNGRep& image_png_rep) { |
51 float scale = ui::GetScaleFactorScale(image_png_rep.scale_factor); | 51 float scale = image_png_rep.scale; |
52 scoped_refptr<base::RefCountedMemory> png = image_png_rep.raw_data; | 52 scoped_refptr<base::RefCountedMemory> png = image_png_rep.raw_data; |
53 CHECK(png.get()); | 53 CHECK(png.get()); |
54 NSData* data = [NSData dataWithBytes:png->front() length:png->size()]; | 54 NSData* data = [NSData dataWithBytes:png->front() length:png->size()]; |
55 UIImage* image = [[UIImage alloc] initWithData:data scale:scale]; | 55 UIImage* image = [[UIImage alloc] initWithData:data scale:scale]; |
56 return image ? image : CreateErrorUIImage(scale); | 56 return image ? image : CreateErrorUIImage(scale); |
57 } | 57 } |
58 | 58 |
59 } // namespace | 59 } // namespace |
60 | 60 |
61 scoped_refptr<base::RefCountedMemory> Get1xPNGBytesFromUIImage( | 61 scoped_refptr<base::RefCountedMemory> Get1xPNGBytesFromUIImage( |
62 UIImage* uiimage) { | 62 UIImage* uiimage) { |
63 NSData* data = UIImagePNGRepresentation(uiimage); | 63 NSData* data = UIImagePNGRepresentation(uiimage); |
64 | 64 |
65 if ([data length] == 0) | 65 if ([data length] == 0) |
66 return NULL; | 66 return NULL; |
67 | 67 |
68 scoped_refptr<base::RefCountedBytes> png_bytes( | 68 scoped_refptr<base::RefCountedBytes> png_bytes( |
69 new base::RefCountedBytes()); | 69 new base::RefCountedBytes()); |
70 png_bytes->data().resize([data length]); | 70 png_bytes->data().resize([data length]); |
71 [data getBytes:&png_bytes->data().at(0) length:[data length]]; | 71 [data getBytes:&png_bytes->data().at(0) length:[data length]]; |
72 return png_bytes; | 72 return png_bytes; |
73 } | 73 } |
74 | 74 |
75 UIImage* CreateUIImageFromPNG( | 75 UIImage* CreateUIImageFromPNG( |
76 const std::vector<gfx::ImagePNGRep>& image_png_reps) { | 76 const std::vector<gfx::ImagePNGRep>& image_png_reps) { |
77 ui::ScaleFactor ideal_scale_factor = ui::GetMaxScaleFactor(); | 77 float ideal_scale = ImageSkia::GetMaxSupportedScale(); |
78 float ideal_scale = ui::GetScaleFactorScale(ideal_scale_factor); | |
79 | 78 |
80 if (image_png_reps.empty()) | 79 if (image_png_reps.empty()) |
81 return CreateErrorUIImage(ideal_scale); | 80 return CreateErrorUIImage(ideal_scale); |
82 | 81 |
83 // Find best match for |ideal_scale_factor|. | 82 // Find best match for |ideal_scale|. |
84 float smallest_diff = std::numeric_limits<float>::max(); | 83 float smallest_diff = std::numeric_limits<float>::max(); |
85 size_t closest_index = 0u; | 84 size_t closest_index = 0u; |
86 for (size_t i = 0; i < image_png_reps.size(); ++i) { | 85 for (size_t i = 0; i < image_png_reps.size(); ++i) { |
87 float scale = ui::GetScaleFactorScale(image_png_reps[i].scale_factor); | 86 float scale = image_png_reps[i].scale; |
88 float diff = std::abs(ideal_scale - scale); | 87 float diff = std::abs(ideal_scale - scale); |
89 if (diff < smallest_diff) { | 88 if (diff < smallest_diff) { |
90 smallest_diff = diff; | 89 smallest_diff = diff; |
91 closest_index = i; | 90 closest_index = i; |
92 } | 91 } |
93 } | 92 } |
94 | 93 |
95 return CreateUIImageFromImagePNGRep(image_png_reps[closest_index]); | 94 return CreateUIImageFromImagePNGRep(image_png_reps[closest_index]); |
96 } | 95 } |
97 | 96 |
98 scoped_refptr<base::RefCountedMemory> Get1xPNGBytesFromImageSkia( | 97 scoped_refptr<base::RefCountedMemory> Get1xPNGBytesFromImageSkia( |
99 const ImageSkia* skia) { | 98 const ImageSkia* skia) { |
100 // iOS does not expose libpng, so conversion from ImageSkia to PNG must go | 99 // iOS does not expose libpng, so conversion from ImageSkia to PNG must go |
101 // through UIImage. | 100 // through UIImage. |
102 // TODO(rohitrao): Rewrite the callers of this function to save the UIImage | 101 // TODO(rohitrao): Rewrite the callers of this function to save the UIImage |
103 // representation in the gfx::Image. If we're generating it, we might as well | 102 // representation in the gfx::Image. If we're generating it, we might as well |
104 // hold on to it. | 103 // hold on to it. |
105 const gfx::ImageSkiaRep& image_skia_rep = skia->GetRepresentation( | 104 const gfx::ImageSkiaRep& image_skia_rep = skia->GetRepresentation(1.0f); |
106 ui::SCALE_FACTOR_100P); | 105 if (image_skia_rep.scale() != 1.0f) |
107 if (image_skia_rep.scale_factor() != ui::SCALE_FACTOR_100P) | |
108 return NULL; | 106 return NULL; |
109 | 107 |
110 UIImage* image = UIImageFromImageSkiaRep(image_skia_rep); | 108 UIImage* image = UIImageFromImageSkiaRep(image_skia_rep); |
111 return Get1xPNGBytesFromUIImage(image); | 109 return Get1xPNGBytesFromUIImage(image); |
112 } | 110 } |
113 | 111 |
114 ImageSkia* ImageSkiaFromPNG( | 112 ImageSkia* ImageSkiaFromPNG( |
115 const std::vector<gfx::ImagePNGRep>& image_png_reps) { | 113 const std::vector<gfx::ImagePNGRep>& image_png_reps) { |
116 // iOS does not expose libpng, so conversion from PNG to ImageSkia must go | 114 // iOS does not expose libpng, so conversion from PNG to ImageSkia must go |
117 // through UIImage. | 115 // through UIImage. |
118 gfx::ImageSkia* image_skia = new gfx::ImageSkia(); | 116 gfx::ImageSkia* image_skia = new gfx::ImageSkia(); |
119 for (size_t i = 0; i < image_png_reps.size(); ++i) { | 117 for (size_t i = 0; i < image_png_reps.size(); ++i) { |
120 base::scoped_nsobject<UIImage> uiimage( | 118 base::scoped_nsobject<UIImage> uiimage( |
121 CreateUIImageFromImagePNGRep(image_png_reps[i])); | 119 CreateUIImageFromImagePNGRep(image_png_reps[i])); |
122 gfx::ImageSkiaRep image_skia_rep = ImageSkiaRepOfScaleFactorFromUIImage( | 120 gfx::ImageSkiaRep image_skia_rep = ImageSkiaRepOfScaleFromUIImage( |
123 uiimage, image_png_reps[i].scale_factor); | 121 uiimage, image_png_reps[i].scale); |
124 if (!image_skia_rep.is_null()) | 122 if (!image_skia_rep.is_null()) |
125 image_skia->AddRepresentation(image_skia_rep); | 123 image_skia->AddRepresentation(image_skia_rep); |
126 } | 124 } |
127 return image_skia; | 125 return image_skia; |
128 } | 126 } |
129 | 127 |
130 gfx::Size UIImageSize(UIImage* image) { | 128 gfx::Size UIImageSize(UIImage* image) { |
131 int width = static_cast<int>(image.size.width); | 129 int width = static_cast<int>(image.size.width); |
132 int height = static_cast<int>(image.size.height); | 130 int height = static_cast<int>(image.size.height); |
133 return gfx::Size(width, height); | 131 return gfx::Size(width, height); |
134 } | 132 } |
135 | 133 |
136 } // namespace internal | 134 } // namespace internal |
137 } // namespace gfx | 135 } // namespace gfx |
OLD | NEW |