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
|
// Copyright 2012 The Chromium Authors
// 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"
#import <AppKit/AppKit.h>
#include <stddef.h>
#include <cmath>
#include <limits>
#include <memory>
#include "base/mac/mac_util.h"
#include "skia/ext/skia_utils_mac.h"
#include "third_party/skia/include/core/SkBitmap.h"
#include "ui/base/resource/resource_scale_factor.h"
#include "ui/gfx/image/image_skia.h"
#include "ui/gfx/image/image_skia_rep.h"
namespace {
// Returns true if the 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();
gfx::ImageSkia image_skia;
const std::vector<ui::ResourceScaleFactor>& supported_scales =
ui::GetSupportedResourceScaleFactors();
for (const auto resource_scale : supported_scales) {
const float scale = ui::GetScaleForResourceScaleFactor(resource_scale);
NSAffineTransform* transform = [NSAffineTransform transform];
[transform scaleBy:scale];
NSDictionary<NSImageHintKey, id>* hints = @{NSImageHintCTM : transform};
NSImageRep* best_match =
[image bestRepresentationForRect:{NSZeroPoint, desired_size}
context:NULL
hints:hints];
NSSize desired_size_for_scale =
NSMakeSize(desired_size.width * scale, desired_size.height * scale);
SkBitmap bitmap(
skia::NSImageRepToSkBitmap(best_match, desired_size_for_scale, false));
if (bitmap.isNull())
continue;
image_skia.AddRepresentation(gfx::ImageSkiaRep(bitmap, scale));
}
return image_skia;
}
NSImage* NSImageFromImageSkia(const gfx::ImageSkia& image_skia) {
if (image_skia.isNull())
return nil;
NSImage* image = [[NSImage alloc] init];
image_skia.EnsureRepsForSupportedScales();
std::vector<gfx::ImageSkiaRep> image_reps = image_skia.image_reps();
for (const auto& rep : image_reps) {
[image addRepresentation:skia::SkBitmapToNSBitmapImageRep(rep.GetBitmap())];
}
image.size = NSMakeSize(image_skia.width(), image_skia.height());
return image;
}
} // namespace gfx
|