2015-02-11 10:41:06 +00:00
|
|
|
// Copyright (c) 2015 GitHub, Inc.
|
|
|
|
// Use of this source code is governed by the MIT license that can be
|
|
|
|
// found in the LICENSE file.
|
|
|
|
|
2020-02-04 20:19:40 +00:00
|
|
|
#include "shell/common/api/electron_api_native_image.h"
|
2015-02-11 10:41:06 +00:00
|
|
|
|
2019-05-02 12:05:37 +00:00
|
|
|
#include <string>
|
2020-08-24 16:36:13 +00:00
|
|
|
#include <utility>
|
2019-05-02 12:05:37 +00:00
|
|
|
#include <vector>
|
|
|
|
|
2015-02-11 10:41:06 +00:00
|
|
|
#import <Cocoa/Cocoa.h>
|
2020-08-24 16:36:13 +00:00
|
|
|
#import <QuickLook/QuickLook.h>
|
2015-02-11 10:41:06 +00:00
|
|
|
|
2020-08-24 16:36:13 +00:00
|
|
|
#include "base/mac/foundation_util.h"
|
2017-10-09 16:13:40 +00:00
|
|
|
#include "base/strings/sys_string_conversions.h"
|
2020-07-13 21:44:12 +00:00
|
|
|
#include "gin/arguments.h"
|
2020-08-24 16:36:13 +00:00
|
|
|
#include "shell/common/gin_converters/image_converter.h"
|
|
|
|
#include "shell/common/gin_helper/promise.h"
|
2017-10-10 06:05:13 +00:00
|
|
|
#include "ui/gfx/color_utils.h"
|
|
|
|
#include "ui/gfx/image/image.h"
|
|
|
|
#include "ui/gfx/image/image_skia.h"
|
|
|
|
#include "ui/gfx/image/image_skia_operations.h"
|
2017-10-09 16:13:40 +00:00
|
|
|
|
2019-06-19 21:23:04 +00:00
|
|
|
namespace electron {
|
2015-02-11 10:41:06 +00:00
|
|
|
|
|
|
|
namespace api {
|
|
|
|
|
2017-10-10 06:05:13 +00:00
|
|
|
NSData* bufferFromNSImage(NSImage* image) {
|
|
|
|
CGImageRef ref = [image CGImageForProposedRect:nil context:nil hints:nil];
|
|
|
|
NSBitmapImageRep* rep = [[NSBitmapImageRep alloc] initWithCGImage:ref];
|
|
|
|
[rep setSize:[image size]];
|
2018-04-20 18:47:04 +00:00
|
|
|
return [rep representationUsingType:NSPNGFileType
|
|
|
|
properties:[[NSDictionary alloc] init]];
|
2017-10-10 06:05:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
double safeShift(double in, double def) {
|
2018-04-20 18:47:04 +00:00
|
|
|
if (in >= 0 || in <= 1 || in == def)
|
|
|
|
return in;
|
2017-10-10 06:05:13 +00:00
|
|
|
return def;
|
|
|
|
}
|
|
|
|
|
2020-08-24 16:36:13 +00:00
|
|
|
// static
|
|
|
|
v8::Local<v8::Promise> NativeImage::CreateThumbnailFromPath(
|
|
|
|
v8::Isolate* isolate,
|
|
|
|
const base::FilePath& path,
|
|
|
|
const gfx::Size& size) {
|
|
|
|
gin_helper::Promise<gfx::Image> promise(isolate);
|
|
|
|
v8::Local<v8::Promise> handle = promise.GetHandle();
|
|
|
|
|
|
|
|
if (size.IsEmpty()) {
|
|
|
|
promise.RejectWithErrorMessage("size must not be empty");
|
|
|
|
return handle;
|
|
|
|
}
|
|
|
|
|
|
|
|
CGSize cg_size = size.ToCGSize();
|
|
|
|
base::ScopedCFTypeRef<CFURLRef> cfurl = base::mac::FilePathToCFURL(path);
|
|
|
|
base::ScopedCFTypeRef<QLThumbnailRef> ql_thumbnail(
|
|
|
|
QLThumbnailCreate(kCFAllocatorDefault, cfurl, cg_size, NULL));
|
|
|
|
__block gin_helper::Promise<gfx::Image> p = std::move(promise);
|
|
|
|
// we do not want to blocking the main thread while waiting for quicklook to
|
|
|
|
// generate the thumbnail
|
|
|
|
QLThumbnailDispatchAsync(
|
|
|
|
ql_thumbnail,
|
|
|
|
dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_LOW, /*flags*/ 0), ^{
|
|
|
|
base::ScopedCFTypeRef<CGImageRef> cg_thumbnail(
|
|
|
|
QLThumbnailCopyImage(ql_thumbnail));
|
|
|
|
if (cg_thumbnail) {
|
|
|
|
NSImage* result =
|
|
|
|
[[[NSImage alloc] initWithCGImage:cg_thumbnail
|
|
|
|
size:cg_size] autorelease];
|
|
|
|
gfx::Image thumbnail(result);
|
|
|
|
dispatch_async(dispatch_get_main_queue(), ^{
|
|
|
|
p.Resolve(thumbnail);
|
|
|
|
});
|
|
|
|
} else {
|
|
|
|
dispatch_async(dispatch_get_main_queue(), ^{
|
|
|
|
p.RejectWithErrorMessage("unable to retrieve thumbnail preview "
|
|
|
|
"image for the given path");
|
|
|
|
});
|
|
|
|
}
|
|
|
|
});
|
|
|
|
return handle;
|
|
|
|
}
|
|
|
|
|
2020-05-11 01:24:45 +00:00
|
|
|
gin::Handle<NativeImage> NativeImage::CreateFromNamedImage(gin::Arguments* args,
|
|
|
|
std::string name) {
|
2017-10-09 16:13:40 +00:00
|
|
|
@autoreleasepool {
|
2017-10-10 06:05:13 +00:00
|
|
|
std::vector<double> hsl_shift;
|
2020-05-11 01:24:45 +00:00
|
|
|
|
|
|
|
// The string representations of NSImageNames don't match the strings
|
|
|
|
// themselves; they instead follow the following pattern:
|
|
|
|
// * NSImageNameActionTemplate -> "NSActionTemplate"
|
|
|
|
// * NSImageNameMultipleDocuments -> "NSMultipleDocuments"
|
|
|
|
// To account for this, we strip out "ImageName" from the passed string.
|
|
|
|
std::string to_remove("ImageName");
|
|
|
|
size_t pos = name.find(to_remove);
|
|
|
|
if (pos != std::string::npos) {
|
|
|
|
name.erase(pos, to_remove.length());
|
|
|
|
}
|
|
|
|
|
2017-10-09 16:13:40 +00:00
|
|
|
NSImage* image = [NSImage imageNamed:base::SysUTF8ToNSString(name)];
|
2020-05-11 01:24:45 +00:00
|
|
|
|
2017-10-09 16:13:40 +00:00
|
|
|
if (!image.valid) {
|
|
|
|
return CreateEmpty(args->isolate());
|
|
|
|
}
|
|
|
|
|
2017-10-10 06:20:55 +00:00
|
|
|
NSData* png_data = bufferFromNSImage(image);
|
2017-10-10 06:05:13 +00:00
|
|
|
|
|
|
|
if (args->GetNext(&hsl_shift) && hsl_shift.size() == 3) {
|
2017-10-10 06:20:55 +00:00
|
|
|
gfx::Image gfx_image = gfx::Image::CreateFrom1xPNGBytes(
|
2018-04-20 18:47:04 +00:00
|
|
|
reinterpret_cast<const unsigned char*>((char*)[png_data bytes]),
|
|
|
|
[png_data length]);
|
|
|
|
color_utils::HSL shift = {safeShift(hsl_shift[0], -1),
|
|
|
|
safeShift(hsl_shift[1], 0.5),
|
|
|
|
safeShift(hsl_shift[2], 0.5)};
|
|
|
|
png_data = bufferFromNSImage(
|
|
|
|
gfx::Image(gfx::ImageSkiaOperations::CreateHSLShiftedImage(
|
|
|
|
gfx_image.AsImageSkia(), shift))
|
2019-01-09 22:57:32 +00:00
|
|
|
.AsNSImage());
|
2017-10-10 06:05:13 +00:00
|
|
|
}
|
2017-10-09 16:13:40 +00:00
|
|
|
|
2018-04-20 18:47:04 +00:00
|
|
|
return CreateFromPNG(args->isolate(), (char*)[png_data bytes],
|
|
|
|
[png_data length]);
|
2017-10-09 16:13:40 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-04-08 08:20:03 +00:00
|
|
|
void NativeImage::SetTemplateImage(bool setAsTemplate) {
|
|
|
|
[image_.AsNSImage() setTemplate:setAsTemplate];
|
|
|
|
}
|
|
|
|
|
2015-07-27 04:58:48 +00:00
|
|
|
bool NativeImage::IsTemplateImage() {
|
2015-07-29 03:48:40 +00:00
|
|
|
return [image_.AsNSImage() isTemplate];
|
2015-07-27 04:58:48 +00:00
|
|
|
}
|
|
|
|
|
2015-02-11 10:41:06 +00:00
|
|
|
} // namespace api
|
|
|
|
|
2019-06-19 21:23:04 +00:00
|
|
|
} // namespace electron
|