Separate Simple Backend creation from initialization.
[chromium-blink-merge.git] / ui / gfx / image / image_skia_util_mac.mm
blob226953bb77266394e73a94584c9ac6151445482f
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
3 // found in the LICENSE file.
5 #include "ui/gfx/image/image_skia_util_mac.h"
7 #include <cmath>
8 #include <limits>
10 #import <AppKit/AppKit.h>
12 #include "base/mac/mac_util.h"
13 #include "base/memory/scoped_nsobject.h"
14 #include "base/memory/scoped_ptr.h"
15 #include "skia/ext/skia_utils_mac.h"
16 #include "third_party/skia/include/core/SkBitmap.h"
17 #include "ui/gfx/image/image_skia.h"
19 namespace {
21 // Returns NSImageRep whose pixel size most closely matches |desired_size|.
22 NSImageRep* GetNSImageRepWithPixelSize(NSImage* image,
23                                        NSSize desired_size) {
24   float smallest_diff = std::numeric_limits<float>::max();
25   NSImageRep* closest_match = nil;
26   for (NSImageRep* image_rep in [image representations]) {
27     float diff = std::abs(desired_size.width - [image_rep pixelsWide]) +
28         std::abs(desired_size.height - [image_rep pixelsHigh]);
29     if (diff < smallest_diff) {
30       smallest_diff = diff;
31       closest_match = image_rep;
32     }
33   }
34   return closest_match;
37 // Returns true if NSImage has no representations
38 bool IsNSImageEmpty(NSImage* image) {
39   return ([image representations].count == 0);
42 }  // namespace
44 namespace gfx {
46 gfx::ImageSkia ImageSkiaFromNSImage(NSImage* image) {
47   return ImageSkiaFromResizedNSImage(image, [image size]);
50 gfx::ImageSkia ImageSkiaFromResizedNSImage(NSImage* image,
51                                            NSSize desired_size) {
52   // Resize and convert to ImageSkia simultaneously to save on computation.
53   // TODO(pkotwicz): Separate resizing NSImage and converting to ImageSkia.
54   // Convert to ImageSkia by finding the most appropriate NSImageRep for
55   // each supported scale factor and resizing if necessary.
57   if (IsNSImageEmpty(image))
58     return gfx::ImageSkia();
60   std::vector<ui::ScaleFactor> supported_scale_factors =
61       ui::GetSupportedScaleFactors();
63   gfx::ImageSkia image_skia;
64   for (size_t i = 0; i < supported_scale_factors.size(); ++i) {
65     float scale = ui::GetScaleFactorScale(supported_scale_factors[i]);
66     NSSize desired_size_for_scale = NSMakeSize(desired_size.width * scale,
67                                                desired_size.height * scale);
68     NSImageRep* ns_image_rep = GetNSImageRepWithPixelSize(image,
69         desired_size_for_scale);
71     SkBitmap bitmap(gfx::NSImageRepToSkBitmap(ns_image_rep,
72         desired_size_for_scale, false));
73     if (bitmap.isNull())
74       continue;
76     image_skia.AddRepresentation(gfx::ImageSkiaRep(bitmap,
77         supported_scale_factors[i]));
78   }
79   return image_skia;
82 gfx::ImageSkia ApplicationIconAtSize(int desired_size) {
83   NSImage* image = [NSImage imageNamed:@"NSApplicationIcon"];
84   return ImageSkiaFromResizedNSImage(image,
85                                      NSMakeSize(desired_size, desired_size));
88 NSImage* NSImageFromImageSkia(const gfx::ImageSkia& image_skia) {
89   if (image_skia.isNull())
90     return nil;
92   scoped_nsobject<NSImage> image([[NSImage alloc] init]);
93   image_skia.EnsureRepsForSupportedScaleFactors();
94   std::vector<gfx::ImageSkiaRep> image_reps = image_skia.image_reps();
95   for (std::vector<gfx::ImageSkiaRep>::const_iterator it = image_reps.begin();
96        it != image_reps.end(); ++it) {
97     [image addRepresentation:
98         gfx::SkBitmapToNSBitmapImageRep(it->sk_bitmap())];
99   }
101   [image setSize:NSMakeSize(image_skia.width(), image_skia.height())];
102   return [image.release() autorelease];
105 NSImage* NSImageFromImageSkiaWithColorSpace(const gfx::ImageSkia& image_skia,
106                                             CGColorSpaceRef color_space) {
107   if (image_skia.isNull())
108     return nil;
110   scoped_nsobject<NSImage> image([[NSImage alloc] init]);
111   image_skia.EnsureRepsForSupportedScaleFactors();
112   std::vector<gfx::ImageSkiaRep> image_reps = image_skia.image_reps();
113   for (std::vector<gfx::ImageSkiaRep>::const_iterator it = image_reps.begin();
114        it != image_reps.end(); ++it) {
115     [image addRepresentation:
116         gfx::SkBitmapToNSBitmapImageRepWithColorSpace(it->sk_bitmap(),
117                                                       color_space)];
118   }
120   [image setSize:NSMakeSize(image_skia.width(), image_skia.height())];
121   return [image.release() autorelease];
124 }  // namespace gfx