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.
4
5#include "ui/gfx/image/image_skia_util_mac.h"
6
7#include <cmath>
8#include <limits>
9
10#import <AppKit/AppKit.h>
11
12#include "base/mac/mac_util.h"
13#include "base/mac/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"
18
19namespace {
20
21// Returns NSImageRep whose pixel size most closely matches |desired_size|.
22NSImageRep* 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;
35}
36
37// Returns true if NSImage has no representations
38bool IsNSImageEmpty(NSImage* image) {
39  return ([image representations].count == 0);
40}
41
42}  // namespace
43
44namespace gfx {
45
46gfx::ImageSkia ImageSkiaFromNSImage(NSImage* image) {
47  return ImageSkiaFromResizedNSImage(image, [image size]);
48}
49
50gfx::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.
56
57  if (IsNSImageEmpty(image))
58    return gfx::ImageSkia();
59
60  std::vector<float> supported_scales = ImageSkia::GetSupportedScales();
61
62  gfx::ImageSkia image_skia;
63  for (size_t i = 0; i < supported_scales.size(); ++i) {
64    float scale = supported_scales[i];
65    NSSize desired_size_for_scale = NSMakeSize(desired_size.width * scale,
66                                               desired_size.height * scale);
67    NSImageRep* ns_image_rep = GetNSImageRepWithPixelSize(image,
68        desired_size_for_scale);
69
70    // TODO(dcheng): Should this function take a color space argument?
71    SkBitmap bitmap(gfx::NSImageRepToSkBitmapWithColorSpace(ns_image_rep,
72        desired_size_for_scale, false, base::mac::GetGenericRGBColorSpace()));
73    if (bitmap.isNull())
74      continue;
75
76    image_skia.AddRepresentation(gfx::ImageSkiaRep(bitmap, scale));
77  }
78  return image_skia;
79}
80
81NSImage* NSImageFromImageSkia(const gfx::ImageSkia& image_skia) {
82  if (image_skia.isNull())
83    return nil;
84
85  base::scoped_nsobject<NSImage> image([[NSImage alloc] init]);
86  image_skia.EnsureRepsForSupportedScales();
87  std::vector<gfx::ImageSkiaRep> image_reps = image_skia.image_reps();
88  for (std::vector<gfx::ImageSkiaRep>::const_iterator it = image_reps.begin();
89       it != image_reps.end(); ++it) {
90    [image addRepresentation:
91        gfx::SkBitmapToNSBitmapImageRep(it->sk_bitmap())];
92  }
93
94  [image setSize:NSMakeSize(image_skia.width(), image_skia.height())];
95  return [image.release() autorelease];
96}
97
98NSImage* NSImageFromImageSkiaWithColorSpace(const gfx::ImageSkia& image_skia,
99                                            CGColorSpaceRef color_space) {
100  if (image_skia.isNull())
101    return nil;
102
103  base::scoped_nsobject<NSImage> image([[NSImage alloc] init]);
104  image_skia.EnsureRepsForSupportedScales();
105  std::vector<gfx::ImageSkiaRep> image_reps = image_skia.image_reps();
106  for (std::vector<gfx::ImageSkiaRep>::const_iterator it = image_reps.begin();
107       it != image_reps.end(); ++it) {
108    [image addRepresentation:
109        gfx::SkBitmapToNSBitmapImageRepWithColorSpace(it->sk_bitmap(),
110                                                      color_space)];
111  }
112
113  [image setSize:NSMakeSize(image_skia.width(), image_skia.height())];
114  return [image.release() autorelease];
115}
116
117}  // namespace gfx
118