OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 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_skia_util_mac.h" | 5 #include "ui/gfx/image/image_skia_util_mac.h" |
6 | 6 |
7 #include <cmath> | 7 #include <cmath> |
8 #include <limits> | 8 #include <limits> |
9 | 9 |
10 #import <AppKit/AppKit.h> | 10 #import <AppKit/AppKit.h> |
(...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
50 gfx::ImageSkia ImageSkiaFromResizedNSImage(NSImage* image, | 50 gfx::ImageSkia ImageSkiaFromResizedNSImage(NSImage* image, |
51 NSSize desired_size) { | 51 NSSize desired_size) { |
52 // Resize and convert to ImageSkia simultaneously to save on computation. | 52 // Resize and convert to ImageSkia simultaneously to save on computation. |
53 // TODO(pkotwicz): Separate resizing NSImage and converting to ImageSkia. | 53 // TODO(pkotwicz): Separate resizing NSImage and converting to ImageSkia. |
54 // Convert to ImageSkia by finding the most appropriate NSImageRep for | 54 // Convert to ImageSkia by finding the most appropriate NSImageRep for |
55 // each supported scale factor and resizing if necessary. | 55 // each supported scale factor and resizing if necessary. |
56 | 56 |
57 if (IsNSImageEmpty(image)) | 57 if (IsNSImageEmpty(image)) |
58 return gfx::ImageSkia(); | 58 return gfx::ImageSkia(); |
59 | 59 |
60 std::vector<ui::ScaleFactor> supported_scale_factors = | 60 std::vector<float> supported_scales = ImageSkia::GetSupportedScales(); |
61 ui::GetSupportedScaleFactors(); | |
62 | 61 |
63 gfx::ImageSkia image_skia; | 62 gfx::ImageSkia image_skia; |
64 for (size_t i = 0; i < supported_scale_factors.size(); ++i) { | 63 for (size_t i = 0; i < supported_scales.size(); ++i) { |
65 float scale = ui::GetScaleFactorScale(supported_scale_factors[i]); | 64 float scale = supported_scales[i]; |
66 NSSize desired_size_for_scale = NSMakeSize(desired_size.width * scale, | 65 NSSize desired_size_for_scale = NSMakeSize(desired_size.width * scale, |
67 desired_size.height * scale); | 66 desired_size.height * scale); |
68 NSImageRep* ns_image_rep = GetNSImageRepWithPixelSize(image, | 67 NSImageRep* ns_image_rep = GetNSImageRepWithPixelSize(image, |
69 desired_size_for_scale); | 68 desired_size_for_scale); |
70 | 69 |
71 SkBitmap bitmap(gfx::NSImageRepToSkBitmap(ns_image_rep, | 70 SkBitmap bitmap(gfx::NSImageRepToSkBitmap(ns_image_rep, |
72 desired_size_for_scale, false)); | 71 desired_size_for_scale, false)); |
73 if (bitmap.isNull()) | 72 if (bitmap.isNull()) |
74 continue; | 73 continue; |
75 | 74 |
76 image_skia.AddRepresentation(gfx::ImageSkiaRep(bitmap, | 75 image_skia.AddRepresentation(gfx::ImageSkiaRep(bitmap, scale)); |
77 supported_scale_factors[i])); | |
78 } | 76 } |
79 return image_skia; | 77 return image_skia; |
80 } | 78 } |
81 | 79 |
82 gfx::ImageSkia ApplicationIconAtSize(int desired_size) { | 80 gfx::ImageSkia ApplicationIconAtSize(int desired_size) { |
83 NSImage* image = [NSImage imageNamed:@"NSApplicationIcon"]; | 81 NSImage* image = [NSImage imageNamed:@"NSApplicationIcon"]; |
84 return ImageSkiaFromResizedNSImage(image, | 82 return ImageSkiaFromResizedNSImage(image, |
85 NSMakeSize(desired_size, desired_size)); | 83 NSMakeSize(desired_size, desired_size)); |
86 } | 84 } |
87 | 85 |
88 NSImage* NSImageFromImageSkia(const gfx::ImageSkia& image_skia) { | 86 NSImage* NSImageFromImageSkia(const gfx::ImageSkia& image_skia) { |
89 if (image_skia.isNull()) | 87 if (image_skia.isNull()) |
90 return nil; | 88 return nil; |
91 | 89 |
92 base::scoped_nsobject<NSImage> image([[NSImage alloc] init]); | 90 base::scoped_nsobject<NSImage> image([[NSImage alloc] init]); |
93 image_skia.EnsureRepsForSupportedScaleFactors(); | 91 image_skia.EnsureRepsForSupportedScales(); |
94 std::vector<gfx::ImageSkiaRep> image_reps = image_skia.image_reps(); | 92 std::vector<gfx::ImageSkiaRep> image_reps = image_skia.image_reps(); |
95 for (std::vector<gfx::ImageSkiaRep>::const_iterator it = image_reps.begin(); | 93 for (std::vector<gfx::ImageSkiaRep>::const_iterator it = image_reps.begin(); |
96 it != image_reps.end(); ++it) { | 94 it != image_reps.end(); ++it) { |
97 [image addRepresentation: | 95 [image addRepresentation: |
98 gfx::SkBitmapToNSBitmapImageRep(it->sk_bitmap())]; | 96 gfx::SkBitmapToNSBitmapImageRep(it->sk_bitmap())]; |
99 } | 97 } |
100 | 98 |
101 [image setSize:NSMakeSize(image_skia.width(), image_skia.height())]; | 99 [image setSize:NSMakeSize(image_skia.width(), image_skia.height())]; |
102 return [image.release() autorelease]; | 100 return [image.release() autorelease]; |
103 } | 101 } |
104 | 102 |
105 NSImage* NSImageFromImageSkiaWithColorSpace(const gfx::ImageSkia& image_skia, | 103 NSImage* NSImageFromImageSkiaWithColorSpace(const gfx::ImageSkia& image_skia, |
106 CGColorSpaceRef color_space) { | 104 CGColorSpaceRef color_space) { |
107 if (image_skia.isNull()) | 105 if (image_skia.isNull()) |
108 return nil; | 106 return nil; |
109 | 107 |
110 base::scoped_nsobject<NSImage> image([[NSImage alloc] init]); | 108 base::scoped_nsobject<NSImage> image([[NSImage alloc] init]); |
111 image_skia.EnsureRepsForSupportedScaleFactors(); | 109 image_skia.EnsureRepsForSupportedScales(); |
112 std::vector<gfx::ImageSkiaRep> image_reps = image_skia.image_reps(); | 110 std::vector<gfx::ImageSkiaRep> image_reps = image_skia.image_reps(); |
113 for (std::vector<gfx::ImageSkiaRep>::const_iterator it = image_reps.begin(); | 111 for (std::vector<gfx::ImageSkiaRep>::const_iterator it = image_reps.begin(); |
114 it != image_reps.end(); ++it) { | 112 it != image_reps.end(); ++it) { |
115 [image addRepresentation: | 113 [image addRepresentation: |
116 gfx::SkBitmapToNSBitmapImageRepWithColorSpace(it->sk_bitmap(), | 114 gfx::SkBitmapToNSBitmapImageRepWithColorSpace(it->sk_bitmap(), |
117 color_space)]; | 115 color_space)]; |
118 } | 116 } |
119 | 117 |
120 [image setSize:NSMakeSize(image_skia.width(), image_skia.height())]; | 118 [image setSize:NSMakeSize(image_skia.width(), image_skia.height())]; |
121 return [image.release() autorelease]; | 119 return [image.release() autorelease]; |
122 } | 120 } |
123 | 121 |
124 } // namespace gfx | 122 } // namespace gfx |
OLD | NEW |