Home | History | Annotate | Download | only in image
      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 
     19 namespace {
     20 
     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;
     35 }
     36 
     37 // Returns true if NSImage has no representations
     38 bool IsNSImageEmpty(NSImage* image) {
     39   return ([image representations].count == 0);
     40 }
     41 
     42 }  // namespace
     43 
     44 namespace gfx {
     45 
     46 gfx::ImageSkia ImageSkiaFromNSImage(NSImage* image) {
     47   return ImageSkiaFromResizedNSImage(image, [image size]);
     48 }
     49 
     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.
     56 
     57   if (IsNSImageEmpty(image))
     58     return gfx::ImageSkia();
     59 
     60   std::vector<ui::ScaleFactor> supported_scale_factors =
     61       ui::GetSupportedScaleFactors();
     62 
     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);
     70 
     71     SkBitmap bitmap(gfx::NSImageRepToSkBitmap(ns_image_rep,
     72         desired_size_for_scale, false));
     73     if (bitmap.isNull())
     74       continue;
     75 
     76     image_skia.AddRepresentation(gfx::ImageSkiaRep(bitmap,
     77         supported_scale_factors[i]));
     78   }
     79   return image_skia;
     80 }
     81 
     82 gfx::ImageSkia ApplicationIconAtSize(int desired_size) {
     83   NSImage* image = [NSImage imageNamed:@"NSApplicationIcon"];
     84   return ImageSkiaFromResizedNSImage(image,
     85                                      NSMakeSize(desired_size, desired_size));
     86 }
     87 
     88 NSImage* NSImageFromImageSkia(const gfx::ImageSkia& image_skia) {
     89   if (image_skia.isNull())
     90     return nil;
     91 
     92   base::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   }
    100 
    101   [image setSize:NSMakeSize(image_skia.width(), image_skia.height())];
    102   return [image.release() autorelease];
    103 }
    104 
    105 NSImage* NSImageFromImageSkiaWithColorSpace(const gfx::ImageSkia& image_skia,
    106                                             CGColorSpaceRef color_space) {
    107   if (image_skia.isNull())
    108     return nil;
    109 
    110   base::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   }
    119 
    120   [image setSize:NSMakeSize(image_skia.width(), image_skia.height())];
    121   return [image.release() autorelease];
    122 }
    123 
    124 }  // namespace gfx
    125