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 = image_png_rep.scale; | 51 float scale = ui::GetScaleFactorScale(image_png_rep.scale_factor); |
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 float ideal_scale = ImageSkia::GetMaxSupportedScale(); | 77 ui::ScaleFactor ideal_scale_factor = ui::GetMaxScaleFactor(); |
| 78 float ideal_scale = ui::GetScaleFactorScale(ideal_scale_factor); |
78 | 79 |
79 if (image_png_reps.empty()) | 80 if (image_png_reps.empty()) |
80 return CreateErrorUIImage(ideal_scale); | 81 return CreateErrorUIImage(ideal_scale); |
81 | 82 |
82 // Find best match for |ideal_scale|. | 83 // Find best match for |ideal_scale_factor|. |
83 float smallest_diff = std::numeric_limits<float>::max(); | 84 float smallest_diff = std::numeric_limits<float>::max(); |
84 size_t closest_index = 0u; | 85 size_t closest_index = 0u; |
85 for (size_t i = 0; i < image_png_reps.size(); ++i) { | 86 for (size_t i = 0; i < image_png_reps.size(); ++i) { |
86 float scale = image_png_reps[i].scale; | 87 float scale = ui::GetScaleFactorScale(image_png_reps[i].scale_factor); |
87 float diff = std::abs(ideal_scale - scale); | 88 float diff = std::abs(ideal_scale - scale); |
88 if (diff < smallest_diff) { | 89 if (diff < smallest_diff) { |
89 smallest_diff = diff; | 90 smallest_diff = diff; |
90 closest_index = i; | 91 closest_index = i; |
91 } | 92 } |
92 } | 93 } |
93 | 94 |
94 return CreateUIImageFromImagePNGRep(image_png_reps[closest_index]); | 95 return CreateUIImageFromImagePNGRep(image_png_reps[closest_index]); |
95 } | 96 } |
96 | 97 |
97 scoped_refptr<base::RefCountedMemory> Get1xPNGBytesFromImageSkia( | 98 scoped_refptr<base::RefCountedMemory> Get1xPNGBytesFromImageSkia( |
98 const ImageSkia* skia) { | 99 const ImageSkia* skia) { |
99 // iOS does not expose libpng, so conversion from ImageSkia to PNG must go | 100 // iOS does not expose libpng, so conversion from ImageSkia to PNG must go |
100 // through UIImage. | 101 // through UIImage. |
101 // TODO(rohitrao): Rewrite the callers of this function to save the UIImage | 102 // TODO(rohitrao): Rewrite the callers of this function to save the UIImage |
102 // representation in the gfx::Image. If we're generating it, we might as well | 103 // representation in the gfx::Image. If we're generating it, we might as well |
103 // hold on to it. | 104 // hold on to it. |
104 const gfx::ImageSkiaRep& image_skia_rep = skia->GetRepresentation(1.0f); | 105 const gfx::ImageSkiaRep& image_skia_rep = skia->GetRepresentation( |
105 if (image_skia_rep.scale() != 1.0f) | 106 ui::SCALE_FACTOR_100P); |
| 107 if (image_skia_rep.scale_factor() != ui::SCALE_FACTOR_100P) |
106 return NULL; | 108 return NULL; |
107 | 109 |
108 UIImage* image = UIImageFromImageSkiaRep(image_skia_rep); | 110 UIImage* image = UIImageFromImageSkiaRep(image_skia_rep); |
109 return Get1xPNGBytesFromUIImage(image); | 111 return Get1xPNGBytesFromUIImage(image); |
110 } | 112 } |
111 | 113 |
112 ImageSkia* ImageSkiaFromPNG( | 114 ImageSkia* ImageSkiaFromPNG( |
113 const std::vector<gfx::ImagePNGRep>& image_png_reps) { | 115 const std::vector<gfx::ImagePNGRep>& image_png_reps) { |
114 // iOS does not expose libpng, so conversion from PNG to ImageSkia must go | 116 // iOS does not expose libpng, so conversion from PNG to ImageSkia must go |
115 // through UIImage. | 117 // through UIImage. |
116 gfx::ImageSkia* image_skia = new gfx::ImageSkia(); | 118 gfx::ImageSkia* image_skia = new gfx::ImageSkia(); |
117 for (size_t i = 0; i < image_png_reps.size(); ++i) { | 119 for (size_t i = 0; i < image_png_reps.size(); ++i) { |
118 base::scoped_nsobject<UIImage> uiimage( | 120 base::scoped_nsobject<UIImage> uiimage( |
119 CreateUIImageFromImagePNGRep(image_png_reps[i])); | 121 CreateUIImageFromImagePNGRep(image_png_reps[i])); |
120 gfx::ImageSkiaRep image_skia_rep = ImageSkiaRepOfScaleFromUIImage( | 122 gfx::ImageSkiaRep image_skia_rep = ImageSkiaRepOfScaleFactorFromUIImage( |
121 uiimage, image_png_reps[i].scale); | 123 uiimage, image_png_reps[i].scale_factor); |
122 if (!image_skia_rep.is_null()) | 124 if (!image_skia_rep.is_null()) |
123 image_skia->AddRepresentation(image_skia_rep); | 125 image_skia->AddRepresentation(image_skia_rep); |
124 } | 126 } |
125 return image_skia; | 127 return image_skia; |
126 } | 128 } |
127 | 129 |
128 gfx::Size UIImageSize(UIImage* image) { | 130 gfx::Size UIImageSize(UIImage* image) { |
129 int width = static_cast<int>(image.size.width); | 131 int width = static_cast<int>(image.size.width); |
130 int height = static_cast<int>(image.size.height); | 132 int height = static_cast<int>(image.size.height); |
131 return gfx::Size(width, height); | 133 return gfx::Size(width, height); |
132 } | 134 } |
133 | 135 |
134 } // namespace internal | 136 } // namespace internal |
135 } // namespace gfx | 137 } // namespace gfx |
OLD | NEW |