1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
|
// Copyright (c) 2012 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "ui/gfx/image/image_skia_util_mac.h"
#include <cmath>
#include <limits>
#import <AppKit/AppKit.h>
#include "base/mac/mac_util.h"
#include "base/mac/scoped_nsobject.h"
#include "base/memory/scoped_ptr.h"
#include "skia/ext/skia_utils_mac.h"
#include "third_party/skia/include/core/SkBitmap.h"
#include "ui/gfx/image/image_skia.h"
namespace {
// Returns NSImageRep whose pixel size most closely matches |desired_size|.
NSImageRep* GetNSImageRepWithPixelSize(NSImage* image,
NSSize desired_size) {
float smallest_diff = std::numeric_limits<float>::max();
NSImageRep* closest_match = nil;
for (NSImageRep* image_rep in [image representations]) {
float diff = std::abs(desired_size.width - [image_rep pixelsWide]) +
std::abs(desired_size.height - [image_rep pixelsHigh]);
if (diff < smallest_diff) {
smallest_diff = diff;
closest_match = image_rep;
}
}
return closest_match;
}
// Returns true if NSImage has no representations
bool IsNSImageEmpty(NSImage* image) {
return ([image representations].count == 0);
}
} // namespace
namespace gfx {
gfx::ImageSkia ImageSkiaFromNSImage(NSImage* image) {
return ImageSkiaFromResizedNSImage(image, [image size]);
}
gfx::ImageSkia ImageSkiaFromResizedNSImage(NSImage* image,
NSSize desired_size) {
// Resize and convert to ImageSkia simultaneously to save on computation.
// TODO(pkotwicz): Separate resizing NSImage and converting to ImageSkia.
// Convert to ImageSkia by finding the most appropriate NSImageRep for
// each supported scale factor and resizing if necessary.
if (IsNSImageEmpty(image))
return gfx::ImageSkia();
std::vector<ui::ScaleFactor> supported_scale_factors =
ui::GetSupportedScaleFactors();
gfx::ImageSkia image_skia;
for (size_t i = 0; i < supported_scale_factors.size(); ++i) {
float scale = ui::GetScaleFactorScale(supported_scale_factors[i]);
NSSize desired_size_for_scale = NSMakeSize(desired_size.width * scale,
desired_size.height * scale);
NSImageRep* ns_image_rep = GetNSImageRepWithPixelSize(image,
desired_size_for_scale);
SkBitmap bitmap(gfx::NSImageRepToSkBitmap(ns_image_rep,
desired_size_for_scale, false));
if (bitmap.isNull())
continue;
image_skia.AddRepresentation(gfx::ImageSkiaRep(bitmap,
supported_scale_factors[i]));
}
return image_skia;
}
gfx::ImageSkia ApplicationIconAtSize(int desired_size) {
NSImage* image = [NSImage imageNamed:@"NSApplicationIcon"];
return ImageSkiaFromResizedNSImage(image,
NSMakeSize(desired_size, desired_size));
}
NSImage* NSImageFromImageSkia(const gfx::ImageSkia& image_skia) {
if (image_skia.isNull())
return nil;
base::scoped_nsobject<NSImage> image([[NSImage alloc] init]);
image_skia.EnsureRepsForSupportedScaleFactors();
std::vector<gfx::ImageSkiaRep> image_reps = image_skia.image_reps();
for (std::vector<gfx::ImageSkiaRep>::const_iterator it = image_reps.begin();
it != image_reps.end(); ++it) {
[image addRepresentation:
gfx::SkBitmapToNSBitmapImageRep(it->sk_bitmap())];
}
[image setSize:NSMakeSize(image_skia.width(), image_skia.height())];
return [image.release() autorelease];
}
NSImage* NSImageFromImageSkiaWithColorSpace(const gfx::ImageSkia& image_skia,
CGColorSpaceRef color_space) {
if (image_skia.isNull())
return nil;
base::scoped_nsobject<NSImage> image([[NSImage alloc] init]);
image_skia.EnsureRepsForSupportedScaleFactors();
std::vector<gfx::ImageSkiaRep> image_reps = image_skia.image_reps();
for (std::vector<gfx::ImageSkiaRep>::const_iterator it = image_reps.begin();
it != image_reps.end(); ++it) {
[image addRepresentation:
gfx::SkBitmapToNSBitmapImageRepWithColorSpace(it->sk_bitmap(),
color_space)];
}
[image setSize:NSMakeSize(image_skia.width(), image_skia.height())];
return [image.release() autorelease];
}
} // namespace gfx
|