2015-02-11 08:03:19 +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.
|
|
|
|
|
|
2019-06-19 20:46:59 +00:00
|
|
|
|
#include "shell/common/api/electron_api_native_image.h"
|
2015-02-11 08:03:19 +00:00
|
|
|
|
|
2018-09-13 00:25:56 +00:00
|
|
|
|
#include <memory>
|
2015-02-11 13:59:08 +00:00
|
|
|
|
#include <string>
|
2018-09-13 00:25:56 +00:00
|
|
|
|
#include <utility>
|
2015-02-11 13:59:08 +00:00
|
|
|
|
#include <vector>
|
|
|
|
|
|
2016-03-04 20:05:36 +00:00
|
|
|
|
#include "base/files/file_util.h"
|
2021-01-25 01:24:10 +00:00
|
|
|
|
#include "base/logging.h"
|
2023-01-06 02:35:34 +00:00
|
|
|
|
#include "base/memory/ref_counted_memory.h"
|
2015-09-02 07:16:49 +00:00
|
|
|
|
#include "base/strings/pattern.h"
|
2019-11-23 03:16:43 +00:00
|
|
|
|
#include "base/strings/utf_string_conversions.h"
|
2020-07-13 21:44:12 +00:00
|
|
|
|
#include "gin/arguments.h"
|
2024-07-29 17:42:57 +00:00
|
|
|
|
#include "gin/handle.h"
|
2020-07-13 21:44:12 +00:00
|
|
|
|
#include "gin/object_template_builder.h"
|
|
|
|
|
#include "gin/per_isolate_data.h"
|
2015-02-12 06:27:53 +00:00
|
|
|
|
#include "net/base/data_url.h"
|
2023-06-20 16:24:03 +00:00
|
|
|
|
#include "shell/browser/browser.h"
|
2019-06-19 20:46:59 +00:00
|
|
|
|
#include "shell/common/asar/asar_util.h"
|
2019-10-18 00:31:29 +00:00
|
|
|
|
#include "shell/common/gin_converters/file_path_converter.h"
|
|
|
|
|
#include "shell/common/gin_converters/gfx_converter.h"
|
|
|
|
|
#include "shell/common/gin_converters/gurl_converter.h"
|
2019-10-31 07:56:00 +00:00
|
|
|
|
#include "shell/common/gin_converters/value_converter.h"
|
2019-10-18 00:31:29 +00:00
|
|
|
|
#include "shell/common/gin_helper/dictionary.h"
|
2024-07-29 17:42:57 +00:00
|
|
|
|
#include "shell/common/gin_helper/error_thrower.h"
|
2020-07-13 21:44:12 +00:00
|
|
|
|
#include "shell/common/gin_helper/function_template_extensions.h"
|
2019-10-18 00:31:29 +00:00
|
|
|
|
#include "shell/common/gin_helper/object_template_builder.h"
|
2019-06-19 20:46:59 +00:00
|
|
|
|
#include "shell/common/node_includes.h"
|
2023-06-20 16:24:03 +00:00
|
|
|
|
#include "shell/common/process_util.h"
|
2019-07-30 20:49:25 +00:00
|
|
|
|
#include "shell/common/skia_util.h"
|
2022-11-17 19:59:23 +00:00
|
|
|
|
#include "shell/common/thread_restrictions.h"
|
2017-11-30 16:56:32 +00:00
|
|
|
|
#include "third_party/skia/include/core/SkBitmap.h"
|
|
|
|
|
#include "third_party/skia/include/core/SkImageInfo.h"
|
2016-08-26 22:30:02 +00:00
|
|
|
|
#include "third_party/skia/include/core/SkPixelRef.h"
|
2015-02-11 10:41:06 +00:00
|
|
|
|
#include "ui/base/layout.h"
|
2023-06-20 16:24:03 +00:00
|
|
|
|
#include "ui/base/resource/resource_scale_factor.h"
|
2017-03-06 21:24:50 +00:00
|
|
|
|
#include "ui/base/webui/web_ui_util.h"
|
2015-02-11 10:41:06 +00:00
|
|
|
|
#include "ui/gfx/codec/jpeg_codec.h"
|
|
|
|
|
#include "ui/gfx/codec/png_codec.h"
|
2015-02-11 08:49:43 +00:00
|
|
|
|
#include "ui/gfx/geometry/size.h"
|
2015-02-11 10:41:06 +00:00
|
|
|
|
#include "ui/gfx/image/image_skia.h"
|
2016-10-04 17:26:01 +00:00
|
|
|
|
#include "ui/gfx/image/image_skia_operations.h"
|
2015-02-11 09:47:54 +00:00
|
|
|
|
#include "ui/gfx/image/image_util.h"
|
2015-02-11 08:03:19 +00:00
|
|
|
|
|
2022-02-10 02:58:52 +00:00
|
|
|
|
#if BUILDFLAG(IS_WIN)
|
2015-08-20 16:26:20 +00:00
|
|
|
|
#include "base/win/scoped_gdi_object.h"
|
2019-06-19 20:46:59 +00:00
|
|
|
|
#include "shell/common/asar/archive.h"
|
2015-08-08 14:58:05 +00:00
|
|
|
|
#include "ui/gfx/icon_util.h"
|
|
|
|
|
#endif
|
2015-02-11 08:03:19 +00:00
|
|
|
|
|
2022-06-29 19:55:47 +00:00
|
|
|
|
namespace electron::api {
|
2015-02-11 08:03:19 +00:00
|
|
|
|
|
|
|
|
|
namespace {
|
|
|
|
|
|
2023-06-20 16:24:03 +00:00
|
|
|
|
// This is needed to avoid a hard CHECK when certain aspects of
|
|
|
|
|
// ImageSkia are invoked before the browser process is ready,
|
|
|
|
|
// since supported scales are normally set by
|
|
|
|
|
// ui::ResourceBundle::InitSharedInstance during browser process startup.
|
|
|
|
|
void EnsureSupportedScaleFactors() {
|
|
|
|
|
if (!electron::IsBrowserProcess())
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
|
|
if (!Browser::Get()->is_ready())
|
|
|
|
|
ui::SetSupportedResourceScaleFactors({ui::k100Percent});
|
|
|
|
|
}
|
|
|
|
|
|
2017-03-07 00:29:39 +00:00
|
|
|
|
// Get the scale factor from options object at the first argument
|
2019-10-18 00:31:29 +00:00
|
|
|
|
float GetScaleFactorFromOptions(gin::Arguments* args) {
|
2017-03-07 00:29:39 +00:00
|
|
|
|
float scale_factor = 1.0f;
|
2019-10-18 00:31:29 +00:00
|
|
|
|
gin_helper::Dictionary options;
|
2017-03-07 00:29:39 +00:00
|
|
|
|
if (args->GetNext(&options))
|
|
|
|
|
options.Get("scaleFactor", &scale_factor);
|
|
|
|
|
return scale_factor;
|
|
|
|
|
}
|
|
|
|
|
|
2016-03-07 22:21:24 +00:00
|
|
|
|
base::FilePath NormalizePath(const base::FilePath& path) {
|
2016-03-07 17:35:35 +00:00
|
|
|
|
if (!path.ReferencesParent()) {
|
|
|
|
|
return path;
|
|
|
|
|
}
|
|
|
|
|
|
2022-11-17 19:59:23 +00:00
|
|
|
|
ScopedAllowBlockingForElectron allow_blocking;
|
2016-03-07 17:35:35 +00:00
|
|
|
|
base::FilePath absolute_path = MakeAbsoluteFilePath(path);
|
|
|
|
|
// MakeAbsoluteFilePath returns an empty path on failures so use original path
|
|
|
|
|
if (absolute_path.empty()) {
|
|
|
|
|
return path;
|
|
|
|
|
} else {
|
|
|
|
|
return absolute_path;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2022-02-10 02:58:52 +00:00
|
|
|
|
#if BUILDFLAG(IS_MAC)
|
2015-07-27 04:58:48 +00:00
|
|
|
|
bool IsTemplateFilename(const base::FilePath& path) {
|
2015-09-02 07:16:49 +00:00
|
|
|
|
return (base::MatchPattern(path.value(), "*Template.*") ||
|
|
|
|
|
base::MatchPattern(path.value(), "*Template@*x.*"));
|
2015-02-12 04:24:18 +00:00
|
|
|
|
}
|
|
|
|
|
#endif
|
2015-02-11 10:41:06 +00:00
|
|
|
|
|
2022-02-10 02:58:52 +00:00
|
|
|
|
#if BUILDFLAG(IS_WIN)
|
2016-05-20 08:51:05 +00:00
|
|
|
|
base::win::ScopedHICON ReadICOFromPath(int size, const base::FilePath& path) {
|
2015-08-21 04:06:38 +00:00
|
|
|
|
// If file is in asar archive, we extract it to a temp file so LoadImage can
|
|
|
|
|
// load it.
|
|
|
|
|
base::FilePath asar_path, relative_path;
|
|
|
|
|
base::FilePath image_path(path);
|
|
|
|
|
if (asar::GetAsarArchivePath(image_path, &asar_path, &relative_path)) {
|
|
|
|
|
std::shared_ptr<asar::Archive> archive =
|
|
|
|
|
asar::GetOrCreateAsarArchive(asar_path);
|
|
|
|
|
if (archive)
|
|
|
|
|
archive->CopyFileOut(relative_path, &image_path);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Load the icon from file.
|
2018-04-18 01:55:30 +00:00
|
|
|
|
return base::win::ScopedHICON(
|
2023-10-03 19:26:35 +00:00
|
|
|
|
static_cast<HICON>(LoadImage(nullptr, image_path.value().c_str(),
|
|
|
|
|
IMAGE_ICON, size, size, LR_LOADFROMFILE)));
|
2016-05-20 02:31:02 +00:00
|
|
|
|
}
|
2015-08-21 04:06:38 +00:00
|
|
|
|
#endif
|
|
|
|
|
|
2015-02-11 08:03:19 +00:00
|
|
|
|
} // namespace
|
|
|
|
|
|
2016-04-25 01:17:54 +00:00
|
|
|
|
NativeImage::NativeImage(v8::Isolate* isolate, const gfx::Image& image)
|
2020-07-13 21:44:12 +00:00
|
|
|
|
: image_(image), isolate_(isolate) {
|
2023-06-20 16:24:03 +00:00
|
|
|
|
EnsureSupportedScaleFactors();
|
2022-03-16 17:54:30 +00:00
|
|
|
|
UpdateExternalAllocatedMemoryUsage();
|
2016-04-25 01:17:54 +00:00
|
|
|
|
}
|
2015-02-11 08:03:19 +00:00
|
|
|
|
|
2022-02-10 02:58:52 +00:00
|
|
|
|
#if BUILDFLAG(IS_WIN)
|
2016-05-20 08:51:05 +00:00
|
|
|
|
NativeImage::NativeImage(v8::Isolate* isolate, const base::FilePath& hicon_path)
|
2020-07-13 21:44:12 +00:00
|
|
|
|
: hicon_path_(hicon_path), isolate_(isolate) {
|
2023-06-20 16:24:03 +00:00
|
|
|
|
EnsureSupportedScaleFactors();
|
|
|
|
|
|
2016-05-20 08:51:05 +00:00
|
|
|
|
// Use the 256x256 icon as fallback icon.
|
|
|
|
|
gfx::ImageSkia image_skia;
|
2019-07-30 20:49:25 +00:00
|
|
|
|
electron::util::ReadImageSkiaFromICO(&image_skia, GetHICON(256));
|
2016-05-20 08:51:05 +00:00
|
|
|
|
image_ = gfx::Image(image_skia);
|
2021-05-24 02:32:55 +00:00
|
|
|
|
|
2022-03-16 17:54:30 +00:00
|
|
|
|
UpdateExternalAllocatedMemoryUsage();
|
2016-05-20 02:31:02 +00:00
|
|
|
|
}
|
|
|
|
|
#endif
|
|
|
|
|
|
2017-04-12 14:17:07 +00:00
|
|
|
|
NativeImage::~NativeImage() {
|
2022-03-16 17:54:30 +00:00
|
|
|
|
isolate_->AdjustAmountOfExternalAllocatedMemory(-memory_usage_);
|
2021-05-24 02:32:55 +00:00
|
|
|
|
}
|
|
|
|
|
|
2022-03-16 17:54:30 +00:00
|
|
|
|
void NativeImage::UpdateExternalAllocatedMemoryUsage() {
|
|
|
|
|
int32_t new_memory_usage = 0;
|
|
|
|
|
|
2017-05-02 02:29:16 +00:00
|
|
|
|
if (image_.HasRepresentation(gfx::Image::kImageRepSkia)) {
|
2021-01-12 23:31:23 +00:00
|
|
|
|
auto* const image_skia = image_.ToImageSkia();
|
|
|
|
|
if (!image_skia->isNull()) {
|
2022-03-16 17:54:30 +00:00
|
|
|
|
new_memory_usage = image_skia->bitmap()->computeByteSize();
|
2021-01-12 23:31:23 +00:00
|
|
|
|
}
|
2017-05-02 02:29:16 +00:00
|
|
|
|
}
|
2022-03-16 17:54:30 +00:00
|
|
|
|
|
|
|
|
|
isolate_->AdjustAmountOfExternalAllocatedMemory(new_memory_usage -
|
|
|
|
|
memory_usage_);
|
|
|
|
|
memory_usage_ = new_memory_usage;
|
2017-04-12 14:17:07 +00:00
|
|
|
|
}
|
2015-02-11 08:03:19 +00:00
|
|
|
|
|
2021-01-04 20:58:31 +00:00
|
|
|
|
// static
|
|
|
|
|
bool NativeImage::TryConvertNativeImage(v8::Isolate* isolate,
|
|
|
|
|
v8::Local<v8::Value> image,
|
2021-01-25 01:24:10 +00:00
|
|
|
|
NativeImage** native_image,
|
|
|
|
|
OnConvertError on_error) {
|
|
|
|
|
std::string error_message;
|
|
|
|
|
|
2021-01-04 20:58:31 +00:00
|
|
|
|
base::FilePath icon_path;
|
|
|
|
|
if (gin::ConvertFromV8(isolate, image, &icon_path)) {
|
|
|
|
|
*native_image = NativeImage::CreateFromPath(isolate, icon_path).get();
|
|
|
|
|
if ((*native_image)->image().IsEmpty()) {
|
2022-02-10 02:58:52 +00:00
|
|
|
|
#if BUILDFLAG(IS_WIN)
|
2021-03-18 19:55:51 +00:00
|
|
|
|
const auto img_path = base::WideToUTF8(icon_path.value());
|
2021-01-04 20:58:31 +00:00
|
|
|
|
#else
|
|
|
|
|
const auto img_path = icon_path.value();
|
|
|
|
|
#endif
|
2021-01-25 01:24:10 +00:00
|
|
|
|
error_message = "Failed to load image from path '" + img_path + "'";
|
2021-01-04 20:58:31 +00:00
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
if (!gin::ConvertFromV8(isolate, image, native_image)) {
|
2021-01-25 01:24:10 +00:00
|
|
|
|
error_message = "Argument must be a file path or a NativeImage";
|
2021-01-04 20:58:31 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
2021-01-25 01:24:10 +00:00
|
|
|
|
|
|
|
|
|
if (!error_message.empty()) {
|
|
|
|
|
switch (on_error) {
|
|
|
|
|
case OnConvertError::kThrow:
|
|
|
|
|
isolate->ThrowException(
|
|
|
|
|
v8::Exception::Error(gin::StringToV8(isolate, error_message)));
|
|
|
|
|
break;
|
|
|
|
|
case OnConvertError::kWarn:
|
|
|
|
|
LOG(WARNING) << error_message;
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
2021-01-04 20:58:31 +00:00
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
2022-02-10 02:58:52 +00:00
|
|
|
|
#if BUILDFLAG(IS_WIN)
|
2016-05-20 08:51:05 +00:00
|
|
|
|
HICON NativeImage::GetHICON(int size) {
|
2023-07-13 06:37:43 +00:00
|
|
|
|
if (auto iter = hicons_.find(size); iter != hicons_.end())
|
2016-05-20 08:51:05 +00:00
|
|
|
|
return iter->second.get();
|
|
|
|
|
|
|
|
|
|
// First try loading the icon with specified size.
|
|
|
|
|
if (!hicon_path_.empty()) {
|
2023-07-13 06:37:43 +00:00
|
|
|
|
auto& hicon = hicons_[size];
|
|
|
|
|
hicon = ReadICOFromPath(size, hicon_path_);
|
|
|
|
|
return hicon.get();
|
2016-05-20 08:51:05 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Then convert the image to ICO.
|
2016-05-20 07:55:22 +00:00
|
|
|
|
if (image_.IsEmpty())
|
2023-10-03 19:26:35 +00:00
|
|
|
|
return nullptr;
|
2023-07-13 06:37:43 +00:00
|
|
|
|
|
|
|
|
|
auto& hicon = hicons_[size];
|
|
|
|
|
hicon = IconUtil::CreateHICONFromSkBitmap(image_.AsBitmap());
|
|
|
|
|
return hicon.get();
|
2016-05-20 07:55:22 +00:00
|
|
|
|
}
|
|
|
|
|
#endif
|
|
|
|
|
|
2019-10-18 00:31:29 +00:00
|
|
|
|
v8::Local<v8::Value> NativeImage::ToPNG(gin::Arguments* args) {
|
2017-03-07 00:29:39 +00:00
|
|
|
|
float scale_factor = GetScaleFactorFromOptions(args);
|
2017-03-07 00:19:16 +00:00
|
|
|
|
|
|
|
|
|
if (scale_factor == 1.0f) {
|
|
|
|
|
// Use raw 1x PNG bytes when available
|
|
|
|
|
scoped_refptr<base::RefCountedMemory> png = image_.As1xPNGBytes();
|
|
|
|
|
if (png->size() > 0) {
|
|
|
|
|
const char* data = reinterpret_cast<const char*>(png->front());
|
|
|
|
|
size_t size = png->size();
|
|
|
|
|
return node::Buffer::Copy(args->isolate(), data, size).ToLocalChecked();
|
|
|
|
|
}
|
2017-03-06 21:47:37 +00:00
|
|
|
|
}
|
2015-02-11 08:03:19 +00:00
|
|
|
|
|
2017-03-07 00:19:16 +00:00
|
|
|
|
const SkBitmap bitmap =
|
2019-01-12 01:00:43 +00:00
|
|
|
|
image_.AsImageSkia().GetRepresentation(scale_factor).GetBitmap();
|
2017-07-31 06:45:59 +00:00
|
|
|
|
std::vector<unsigned char> encoded;
|
|
|
|
|
gfx::PNGCodec::EncodeBGRASkBitmap(bitmap, false, &encoded);
|
|
|
|
|
const char* data = reinterpret_cast<char*>(encoded.data());
|
|
|
|
|
size_t size = encoded.size();
|
2017-03-07 00:19:16 +00:00
|
|
|
|
return node::Buffer::Copy(args->isolate(), data, size).ToLocalChecked();
|
|
|
|
|
}
|
2017-01-25 21:22:44 +00:00
|
|
|
|
|
2019-10-18 00:31:29 +00:00
|
|
|
|
v8::Local<v8::Value> NativeImage::ToBitmap(gin::Arguments* args) {
|
2017-03-07 00:29:39 +00:00
|
|
|
|
float scale_factor = GetScaleFactorFromOptions(args);
|
2017-03-07 00:19:16 +00:00
|
|
|
|
|
|
|
|
|
const SkBitmap bitmap =
|
2019-01-12 01:00:43 +00:00
|
|
|
|
image_.AsImageSkia().GetRepresentation(scale_factor).GetBitmap();
|
2020-10-05 01:15:16 +00:00
|
|
|
|
|
|
|
|
|
SkImageInfo info =
|
|
|
|
|
SkImageInfo::MakeN32Premul(bitmap.width(), bitmap.height());
|
|
|
|
|
|
|
|
|
|
auto array_buffer =
|
|
|
|
|
v8::ArrayBuffer::New(args->isolate(), info.computeMinByteSize());
|
|
|
|
|
auto backing_store = array_buffer->GetBackingStore();
|
|
|
|
|
if (bitmap.readPixels(info, backing_store->Data(), info.minRowBytes(), 0,
|
|
|
|
|
0)) {
|
|
|
|
|
return node::Buffer::New(args->isolate(), array_buffer, 0,
|
|
|
|
|
info.computeMinByteSize())
|
|
|
|
|
.ToLocalChecked();
|
|
|
|
|
}
|
|
|
|
|
return node::Buffer::New(args->isolate(), 0).ToLocalChecked();
|
2016-07-31 03:11:18 +00:00
|
|
|
|
}
|
|
|
|
|
|
2015-05-22 11:11:22 +00:00
|
|
|
|
v8::Local<v8::Value> NativeImage::ToJPEG(v8::Isolate* isolate, int quality) {
|
2015-02-11 09:47:54 +00:00
|
|
|
|
std::vector<unsigned char> output;
|
|
|
|
|
gfx::JPEG1xEncodedDataFromImage(image_, quality, &output);
|
2017-07-31 06:48:54 +00:00
|
|
|
|
if (output.empty())
|
|
|
|
|
return node::Buffer::New(isolate, 0).ToLocalChecked();
|
2018-04-18 01:55:30 +00:00
|
|
|
|
return node::Buffer::Copy(isolate,
|
|
|
|
|
reinterpret_cast<const char*>(&output.front()),
|
|
|
|
|
output.size())
|
|
|
|
|
.ToLocalChecked();
|
2015-02-11 09:47:54 +00:00
|
|
|
|
}
|
|
|
|
|
|
2019-10-18 00:31:29 +00:00
|
|
|
|
std::string NativeImage::ToDataURL(gin::Arguments* args) {
|
2017-03-07 00:29:39 +00:00
|
|
|
|
float scale_factor = GetScaleFactorFromOptions(args);
|
2017-03-07 00:19:16 +00:00
|
|
|
|
|
|
|
|
|
return webui::GetBitmapDataUrl(
|
2019-01-12 01:00:43 +00:00
|
|
|
|
image_.AsImageSkia().GetRepresentation(scale_factor).GetBitmap());
|
2017-03-07 00:19:16 +00:00
|
|
|
|
}
|
2017-01-25 21:22:44 +00:00
|
|
|
|
|
2019-10-18 00:31:29 +00:00
|
|
|
|
v8::Local<v8::Value> NativeImage::GetBitmap(gin::Arguments* args) {
|
2017-03-07 00:29:39 +00:00
|
|
|
|
float scale_factor = GetScaleFactorFromOptions(args);
|
2017-03-07 00:19:16 +00:00
|
|
|
|
|
|
|
|
|
const SkBitmap bitmap =
|
2019-01-12 01:00:43 +00:00
|
|
|
|
image_.AsImageSkia().GetRepresentation(scale_factor).GetBitmap();
|
2017-03-07 00:19:16 +00:00
|
|
|
|
SkPixelRef* ref = bitmap.pixelRef();
|
|
|
|
|
if (!ref)
|
|
|
|
|
return node::Buffer::New(args->isolate(), 0).ToLocalChecked();
|
2022-06-01 06:12:47 +00:00
|
|
|
|
return node::Buffer::Copy(args->isolate(),
|
|
|
|
|
reinterpret_cast<char*>(ref->pixels()),
|
|
|
|
|
bitmap.computeByteSize())
|
|
|
|
|
.ToLocalChecked();
|
2016-08-05 08:55:57 +00:00
|
|
|
|
}
|
|
|
|
|
|
2019-10-18 00:31:29 +00:00
|
|
|
|
v8::Local<v8::Value> NativeImage::GetNativeHandle(
|
|
|
|
|
gin_helper::ErrorThrower thrower) {
|
2022-02-10 02:58:52 +00:00
|
|
|
|
#if BUILDFLAG(IS_MAC)
|
2018-04-18 01:55:30 +00:00
|
|
|
|
if (IsEmpty())
|
2019-10-18 00:31:29 +00:00
|
|
|
|
return node::Buffer::New(thrower.isolate(), 0).ToLocalChecked();
|
2017-01-25 21:22:44 +00:00
|
|
|
|
|
2016-03-17 13:01:31 +00:00
|
|
|
|
NSImage* ptr = image_.AsNSImage();
|
2019-10-18 00:31:29 +00:00
|
|
|
|
return node::Buffer::Copy(thrower.isolate(), reinterpret_cast<char*>(ptr),
|
2018-04-18 01:55:30 +00:00
|
|
|
|
sizeof(void*))
|
|
|
|
|
.ToLocalChecked();
|
2016-03-15 02:48:40 +00:00
|
|
|
|
#else
|
2019-10-18 00:31:29 +00:00
|
|
|
|
thrower.ThrowError("Not implemented");
|
|
|
|
|
return v8::Undefined(thrower.isolate());
|
2016-03-14 03:08:53 +00:00
|
|
|
|
#endif
|
|
|
|
|
}
|
|
|
|
|
|
2015-02-11 08:49:43 +00:00
|
|
|
|
bool NativeImage::IsEmpty() {
|
|
|
|
|
return image_.IsEmpty();
|
|
|
|
|
}
|
|
|
|
|
|
2024-01-10 22:23:35 +00:00
|
|
|
|
gfx::Size NativeImage::GetSize(const std::optional<float> scale_factor) {
|
2020-05-18 16:29:24 +00:00
|
|
|
|
float sf = scale_factor.value_or(1.0f);
|
|
|
|
|
gfx::ImageSkiaRep image_rep = image_.AsImageSkia().GetRepresentation(sf);
|
|
|
|
|
|
|
|
|
|
return gfx::Size(image_rep.GetWidth(), image_rep.GetHeight());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
std::vector<float> NativeImage::GetScaleFactors() {
|
|
|
|
|
gfx::ImageSkia image_skia = image_.AsImageSkia();
|
2020-10-13 01:59:18 +00:00
|
|
|
|
std::vector<float> scale_factors;
|
|
|
|
|
for (const auto& rep : image_skia.image_reps()) {
|
|
|
|
|
scale_factors.push_back(rep.scale());
|
|
|
|
|
}
|
|
|
|
|
return scale_factors;
|
2015-02-11 08:49:43 +00:00
|
|
|
|
}
|
|
|
|
|
|
2024-01-10 22:23:35 +00:00
|
|
|
|
float NativeImage::GetAspectRatio(const std::optional<float> scale_factor) {
|
2020-05-18 16:29:24 +00:00
|
|
|
|
float sf = scale_factor.value_or(1.0f);
|
|
|
|
|
gfx::Size size = GetSize(sf);
|
2016-10-05 16:15:06 +00:00
|
|
|
|
if (size.IsEmpty())
|
|
|
|
|
return 1.f;
|
|
|
|
|
else
|
|
|
|
|
return static_cast<float>(size.width()) / static_cast<float>(size.height());
|
|
|
|
|
}
|
|
|
|
|
|
2020-05-18 16:29:24 +00:00
|
|
|
|
gin::Handle<NativeImage> NativeImage::Resize(gin::Arguments* args,
|
2022-07-05 15:25:18 +00:00
|
|
|
|
base::Value::Dict options) {
|
2020-05-18 16:29:24 +00:00
|
|
|
|
float scale_factor = GetScaleFactorFromOptions(args);
|
|
|
|
|
|
|
|
|
|
gfx::Size size = GetSize(scale_factor);
|
2024-01-10 22:23:35 +00:00
|
|
|
|
std::optional<int> new_width = options.FindInt("width");
|
|
|
|
|
std::optional<int> new_height = options.FindInt("height");
|
2022-07-05 15:25:18 +00:00
|
|
|
|
int width = new_width.value_or(size.width());
|
|
|
|
|
int height = new_height.value_or(size.height());
|
2016-10-05 16:15:06 +00:00
|
|
|
|
size.SetSize(width, height);
|
2017-04-12 18:54:03 +00:00
|
|
|
|
|
2021-01-12 23:31:23 +00:00
|
|
|
|
if (width <= 0 && height <= 0) {
|
|
|
|
|
return CreateEmpty(args->isolate());
|
2022-07-05 15:25:18 +00:00
|
|
|
|
} else if (new_width && !new_height) {
|
2016-10-05 16:15:06 +00:00
|
|
|
|
// Scale height to preserve original aspect ratio
|
|
|
|
|
size.set_height(width);
|
2020-05-18 16:29:24 +00:00
|
|
|
|
size =
|
|
|
|
|
gfx::ScaleToRoundedSize(size, 1.f, 1.f / GetAspectRatio(scale_factor));
|
2022-07-05 15:25:18 +00:00
|
|
|
|
} else if (new_height && !new_width) {
|
2016-10-05 16:15:06 +00:00
|
|
|
|
// Scale width to preserve original aspect ratio
|
|
|
|
|
size.set_width(height);
|
2020-05-18 16:29:24 +00:00
|
|
|
|
size = gfx::ScaleToRoundedSize(size, GetAspectRatio(scale_factor), 1.f);
|
2016-10-05 16:15:06 +00:00
|
|
|
|
}
|
2016-10-04 17:26:01 +00:00
|
|
|
|
|
|
|
|
|
skia::ImageOperations::ResizeMethod method =
|
|
|
|
|
skia::ImageOperations::ResizeMethod::RESIZE_BEST;
|
2022-07-05 15:25:18 +00:00
|
|
|
|
std::string* quality = options.FindString("quality");
|
|
|
|
|
if (quality && *quality == "good")
|
2016-10-04 17:26:01 +00:00
|
|
|
|
method = skia::ImageOperations::ResizeMethod::RESIZE_GOOD;
|
2022-07-05 15:25:18 +00:00
|
|
|
|
else if (quality && *quality == "better")
|
2016-10-04 17:26:01 +00:00
|
|
|
|
method = skia::ImageOperations::ResizeMethod::RESIZE_BETTER;
|
|
|
|
|
|
|
|
|
|
gfx::ImageSkia resized = gfx::ImageSkiaOperations::CreateResizedImage(
|
|
|
|
|
image_.AsImageSkia(), method, size);
|
2020-05-18 16:29:24 +00:00
|
|
|
|
return gin::CreateHandle(
|
|
|
|
|
args->isolate(), new NativeImage(args->isolate(), gfx::Image(resized)));
|
2016-10-04 17:26:01 +00:00
|
|
|
|
}
|
|
|
|
|
|
2019-10-25 13:03:28 +00:00
|
|
|
|
gin::Handle<NativeImage> NativeImage::Crop(v8::Isolate* isolate,
|
|
|
|
|
const gfx::Rect& rect) {
|
2018-04-18 01:55:30 +00:00
|
|
|
|
gfx::ImageSkia cropped =
|
|
|
|
|
gfx::ImageSkiaOperations::ExtractSubset(image_.AsImageSkia(), rect);
|
2019-10-25 13:03:28 +00:00
|
|
|
|
return gin::CreateHandle(isolate,
|
|
|
|
|
new NativeImage(isolate, gfx::Image(cropped)));
|
2016-10-04 18:23:18 +00:00
|
|
|
|
}
|
|
|
|
|
|
2019-10-18 00:31:29 +00:00
|
|
|
|
void NativeImage::AddRepresentation(const gin_helper::Dictionary& options) {
|
2017-03-07 21:48:12 +00:00
|
|
|
|
int width = 0;
|
|
|
|
|
int height = 0;
|
|
|
|
|
float scale_factor = 1.0f;
|
|
|
|
|
options.Get("width", &width);
|
|
|
|
|
options.Get("height", &height);
|
|
|
|
|
options.Get("scaleFactor", &scale_factor);
|
|
|
|
|
|
2017-03-07 22:24:37 +00:00
|
|
|
|
bool skia_rep_added = false;
|
|
|
|
|
gfx::ImageSkia image_skia = image_.AsImageSkia();
|
|
|
|
|
|
2017-03-07 21:48:12 +00:00
|
|
|
|
v8::Local<v8::Value> buffer;
|
2017-03-07 22:24:37 +00:00
|
|
|
|
GURL url;
|
2017-03-07 21:48:12 +00:00
|
|
|
|
if (options.Get("buffer", &buffer) && node::Buffer::HasInstance(buffer)) {
|
2019-03-26 01:13:39 +00:00
|
|
|
|
auto* data = reinterpret_cast<unsigned char*>(node::Buffer::Data(buffer));
|
|
|
|
|
auto size = node::Buffer::Length(buffer);
|
2019-07-30 20:49:25 +00:00
|
|
|
|
skia_rep_added = electron::util::AddImageSkiaRepFromBuffer(
|
|
|
|
|
&image_skia, data, size, width, height, scale_factor);
|
2017-03-07 22:24:37 +00:00
|
|
|
|
} else if (options.Get("dataURL", &url)) {
|
|
|
|
|
std::string mime_type, charset, data;
|
|
|
|
|
if (net::DataURL::Parse(url, &mime_type, &charset, &data)) {
|
2019-03-26 01:13:39 +00:00
|
|
|
|
auto* data_ptr = reinterpret_cast<const unsigned char*>(data.c_str());
|
|
|
|
|
if (mime_type == "image/png") {
|
2019-07-30 20:49:25 +00:00
|
|
|
|
skia_rep_added = electron::util::AddImageSkiaRepFromPNG(
|
|
|
|
|
&image_skia, data_ptr, data.size(), scale_factor);
|
2019-03-26 01:13:39 +00:00
|
|
|
|
} else if (mime_type == "image/jpeg") {
|
2019-07-30 20:49:25 +00:00
|
|
|
|
skia_rep_added = electron::util::AddImageSkiaRepFromJPEG(
|
|
|
|
|
&image_skia, data_ptr, data.size(), scale_factor);
|
2017-03-07 22:24:37 +00:00
|
|
|
|
}
|
2017-03-07 21:48:12 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
2017-03-07 22:24:37 +00:00
|
|
|
|
|
|
|
|
|
// Re-initialize image when first representation is added to an empty image
|
|
|
|
|
if (skia_rep_added && IsEmpty()) {
|
|
|
|
|
gfx::Image image(image_skia);
|
2017-11-27 06:50:42 +00:00
|
|
|
|
image_ = std::move(image);
|
2017-03-07 22:24:37 +00:00
|
|
|
|
}
|
2017-03-07 21:48:12 +00:00
|
|
|
|
}
|
2016-10-04 18:23:18 +00:00
|
|
|
|
|
2022-02-10 02:58:52 +00:00
|
|
|
|
#if !BUILDFLAG(IS_MAC)
|
2018-04-18 01:55:30 +00:00
|
|
|
|
void NativeImage::SetTemplateImage(bool setAsTemplate) {}
|
2015-07-29 03:48:40 +00:00
|
|
|
|
|
2015-07-27 04:58:48 +00:00
|
|
|
|
bool NativeImage::IsTemplateImage() {
|
2015-07-29 04:36:01 +00:00
|
|
|
|
return false;
|
2015-07-27 04:58:48 +00:00
|
|
|
|
}
|
2015-04-13 03:53:24 +00:00
|
|
|
|
#endif
|
|
|
|
|
|
2015-02-12 05:55:45 +00:00
|
|
|
|
// static
|
2019-10-25 13:03:28 +00:00
|
|
|
|
gin::Handle<NativeImage> NativeImage::CreateEmpty(v8::Isolate* isolate) {
|
|
|
|
|
return gin::CreateHandle(isolate, new NativeImage(isolate, gfx::Image()));
|
2015-02-12 05:55:45 +00:00
|
|
|
|
}
|
|
|
|
|
|
2015-02-11 08:03:19 +00:00
|
|
|
|
// static
|
2019-10-25 13:03:28 +00:00
|
|
|
|
gin::Handle<NativeImage> NativeImage::Create(v8::Isolate* isolate,
|
|
|
|
|
const gfx::Image& image) {
|
|
|
|
|
return gin::CreateHandle(isolate, new NativeImage(isolate, image));
|
2015-02-11 08:03:19 +00:00
|
|
|
|
}
|
|
|
|
|
|
2015-02-11 10:02:59 +00:00
|
|
|
|
// static
|
2019-10-25 13:03:28 +00:00
|
|
|
|
gin::Handle<NativeImage> NativeImage::CreateFromPNG(v8::Isolate* isolate,
|
|
|
|
|
const char* buffer,
|
|
|
|
|
size_t length) {
|
2020-10-13 01:59:18 +00:00
|
|
|
|
gfx::ImageSkia image_skia;
|
|
|
|
|
electron::util::AddImageSkiaRepFromPNG(
|
|
|
|
|
&image_skia, reinterpret_cast<const unsigned char*>(buffer), length, 1.0);
|
|
|
|
|
return Create(isolate, gfx::Image(image_skia));
|
2015-02-11 10:02:59 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// static
|
2019-10-25 13:03:28 +00:00
|
|
|
|
gin::Handle<NativeImage> NativeImage::CreateFromJPEG(v8::Isolate* isolate,
|
|
|
|
|
const char* buffer,
|
|
|
|
|
size_t length) {
|
2020-10-13 01:59:18 +00:00
|
|
|
|
gfx::ImageSkia image_skia;
|
|
|
|
|
electron::util::AddImageSkiaRepFromJPEG(
|
|
|
|
|
&image_skia, reinterpret_cast<const unsigned char*>(buffer), length, 1.0);
|
|
|
|
|
return Create(isolate, gfx::Image(image_skia));
|
2015-02-11 10:02:59 +00:00
|
|
|
|
}
|
|
|
|
|
|
2015-02-11 10:41:06 +00:00
|
|
|
|
// static
|
2019-10-25 13:03:28 +00:00
|
|
|
|
gin::Handle<NativeImage> NativeImage::CreateFromPath(
|
2018-04-18 01:55:30 +00:00
|
|
|
|
v8::Isolate* isolate,
|
|
|
|
|
const base::FilePath& path) {
|
2016-03-07 22:22:16 +00:00
|
|
|
|
base::FilePath image_path = NormalizePath(path);
|
2022-02-10 02:58:52 +00:00
|
|
|
|
#if BUILDFLAG(IS_WIN)
|
2016-05-20 13:22:15 +00:00
|
|
|
|
if (image_path.MatchesExtension(FILE_PATH_LITERAL(".ico"))) {
|
2019-10-25 13:03:28 +00:00
|
|
|
|
return gin::CreateHandle(isolate, new NativeImage(isolate, image_path));
|
2016-05-20 13:22:15 +00:00
|
|
|
|
}
|
2015-08-08 14:58:05 +00:00
|
|
|
|
#endif
|
2016-05-20 13:22:15 +00:00
|
|
|
|
gfx::ImageSkia image_skia;
|
2019-07-30 20:49:25 +00:00
|
|
|
|
electron::util::PopulateImageSkiaRepsFromPath(&image_skia, image_path);
|
2016-05-20 13:22:15 +00:00
|
|
|
|
gfx::Image image(image_skia);
|
2019-10-25 13:03:28 +00:00
|
|
|
|
gin::Handle<NativeImage> handle = Create(isolate, image);
|
2022-02-10 02:58:52 +00:00
|
|
|
|
#if BUILDFLAG(IS_MAC)
|
2016-05-20 13:22:15 +00:00
|
|
|
|
if (IsTemplateFilename(image_path))
|
|
|
|
|
handle->SetTemplateImage(true);
|
2015-02-12 04:24:18 +00:00
|
|
|
|
#endif
|
2016-05-20 13:22:15 +00:00
|
|
|
|
return handle;
|
2015-02-11 10:41:06 +00:00
|
|
|
|
}
|
|
|
|
|
|
2019-03-14 18:00:38 +00:00
|
|
|
|
// static
|
2019-10-25 13:03:28 +00:00
|
|
|
|
gin::Handle<NativeImage> NativeImage::CreateFromBitmap(
|
2019-10-18 00:31:29 +00:00
|
|
|
|
gin_helper::ErrorThrower thrower,
|
2019-03-14 18:00:38 +00:00
|
|
|
|
v8::Local<v8::Value> buffer,
|
2019-10-18 00:31:29 +00:00
|
|
|
|
const gin_helper::Dictionary& options) {
|
2019-03-14 18:00:38 +00:00
|
|
|
|
if (!node::Buffer::HasInstance(buffer)) {
|
2019-10-18 00:31:29 +00:00
|
|
|
|
thrower.ThrowError("buffer must be a node Buffer");
|
2019-10-25 13:03:28 +00:00
|
|
|
|
return gin::Handle<NativeImage>();
|
2019-03-14 18:00:38 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
unsigned int width = 0;
|
|
|
|
|
unsigned int height = 0;
|
|
|
|
|
double scale_factor = 1.;
|
|
|
|
|
|
|
|
|
|
if (!options.Get("width", &width)) {
|
2019-10-18 00:31:29 +00:00
|
|
|
|
thrower.ThrowError("width is required");
|
2019-10-25 13:03:28 +00:00
|
|
|
|
return gin::Handle<NativeImage>();
|
2019-03-14 18:00:38 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (!options.Get("height", &height)) {
|
2019-10-18 00:31:29 +00:00
|
|
|
|
thrower.ThrowError("height is required");
|
2019-10-25 13:03:28 +00:00
|
|
|
|
return gin::Handle<NativeImage>();
|
2019-03-14 18:00:38 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
auto info = SkImageInfo::MakeN32(width, height, kPremul_SkAlphaType);
|
|
|
|
|
auto size_bytes = info.computeMinByteSize();
|
|
|
|
|
|
|
|
|
|
if (size_bytes != node::Buffer::Length(buffer)) {
|
2019-10-18 00:31:29 +00:00
|
|
|
|
thrower.ThrowError("invalid buffer size");
|
2019-10-25 13:03:28 +00:00
|
|
|
|
return gin::Handle<NativeImage>();
|
2019-03-14 18:00:38 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
options.Get("scaleFactor", &scale_factor);
|
|
|
|
|
|
|
|
|
|
if (width == 0 || height == 0) {
|
2019-10-18 00:31:29 +00:00
|
|
|
|
return CreateEmpty(thrower.isolate());
|
2019-03-14 18:00:38 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
SkBitmap bitmap;
|
|
|
|
|
bitmap.allocN32Pixels(width, height, false);
|
2019-04-18 00:16:06 +00:00
|
|
|
|
bitmap.writePixels({info, node::Buffer::Data(buffer), bitmap.rowBytes()});
|
2019-03-14 18:00:38 +00:00
|
|
|
|
|
2021-01-12 23:31:23 +00:00
|
|
|
|
gfx::ImageSkia image_skia =
|
|
|
|
|
gfx::ImageSkia::CreateFromBitmap(bitmap, scale_factor);
|
2019-03-14 18:00:38 +00:00
|
|
|
|
|
2019-10-18 00:31:29 +00:00
|
|
|
|
return Create(thrower.isolate(), gfx::Image(image_skia));
|
2019-03-14 18:00:38 +00:00
|
|
|
|
}
|
|
|
|
|
|
2015-02-12 07:19:05 +00:00
|
|
|
|
// static
|
2019-10-25 13:03:28 +00:00
|
|
|
|
gin::Handle<NativeImage> NativeImage::CreateFromBuffer(
|
2019-10-18 00:31:29 +00:00
|
|
|
|
gin_helper::ErrorThrower thrower,
|
|
|
|
|
v8::Local<v8::Value> buffer,
|
|
|
|
|
gin::Arguments* args) {
|
2019-03-13 15:26:11 +00:00
|
|
|
|
if (!node::Buffer::HasInstance(buffer)) {
|
2019-10-18 00:31:29 +00:00
|
|
|
|
thrower.ThrowError("buffer must be a node Buffer");
|
2019-10-25 13:03:28 +00:00
|
|
|
|
return gin::Handle<NativeImage>();
|
2019-03-13 15:26:11 +00:00
|
|
|
|
}
|
|
|
|
|
|
2016-12-09 23:12:36 +00:00
|
|
|
|
int width = 0;
|
|
|
|
|
int height = 0;
|
2015-02-12 07:19:05 +00:00
|
|
|
|
double scale_factor = 1.;
|
2016-12-13 20:30:45 +00:00
|
|
|
|
|
2019-10-18 00:31:29 +00:00
|
|
|
|
gin_helper::Dictionary options;
|
2016-12-13 20:30:45 +00:00
|
|
|
|
if (args->GetNext(&options)) {
|
|
|
|
|
options.Get("width", &width);
|
|
|
|
|
options.Get("height", &height);
|
|
|
|
|
options.Get("scaleFactor", &scale_factor);
|
|
|
|
|
}
|
|
|
|
|
|
2015-02-12 07:19:05 +00:00
|
|
|
|
gfx::ImageSkia image_skia;
|
2019-07-30 20:49:25 +00:00
|
|
|
|
electron::util::AddImageSkiaRepFromBuffer(
|
2019-03-26 01:13:39 +00:00
|
|
|
|
&image_skia, reinterpret_cast<unsigned char*>(node::Buffer::Data(buffer)),
|
|
|
|
|
node::Buffer::Length(buffer), width, height, scale_factor);
|
2015-02-12 07:19:05 +00:00
|
|
|
|
return Create(args->isolate(), gfx::Image(image_skia));
|
|
|
|
|
}
|
|
|
|
|
|
2015-02-12 06:27:53 +00:00
|
|
|
|
// static
|
2019-10-25 13:03:28 +00:00
|
|
|
|
gin::Handle<NativeImage> NativeImage::CreateFromDataURL(v8::Isolate* isolate,
|
|
|
|
|
const GURL& url) {
|
2015-02-12 06:27:53 +00:00
|
|
|
|
std::string mime_type, charset, data;
|
|
|
|
|
if (net::DataURL::Parse(url, &mime_type, &charset, &data)) {
|
|
|
|
|
if (mime_type == "image/png")
|
|
|
|
|
return CreateFromPNG(isolate, data.c_str(), data.size());
|
|
|
|
|
else if (mime_type == "image/jpeg")
|
|
|
|
|
return CreateFromJPEG(isolate, data.c_str(), data.size());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return CreateEmpty(isolate);
|
|
|
|
|
}
|
|
|
|
|
|
2022-02-10 02:58:52 +00:00
|
|
|
|
#if !BUILDFLAG(IS_MAC)
|
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
|
|
|
|
return CreateEmpty(args->isolate());
|
|
|
|
|
}
|
|
|
|
|
#endif
|
|
|
|
|
|
2016-04-25 01:17:54 +00:00
|
|
|
|
// static
|
2020-07-13 21:44:12 +00:00
|
|
|
|
gin::ObjectTemplateBuilder NativeImage::GetObjectTemplateBuilder(
|
|
|
|
|
v8::Isolate* isolate) {
|
|
|
|
|
gin::PerIsolateData* data = gin::PerIsolateData::From(isolate);
|
|
|
|
|
auto* wrapper_info = &kWrapperInfo;
|
|
|
|
|
v8::Local<v8::FunctionTemplate> constructor =
|
|
|
|
|
data->GetFunctionTemplate(wrapper_info);
|
|
|
|
|
if (constructor.IsEmpty()) {
|
|
|
|
|
constructor = v8::FunctionTemplate::New(isolate);
|
|
|
|
|
constructor->SetClassName(gin::StringToV8(isolate, GetTypeName()));
|
|
|
|
|
data->SetFunctionTemplate(wrapper_info, constructor);
|
|
|
|
|
}
|
|
|
|
|
return gin::ObjectTemplateBuilder(isolate, GetTypeName(),
|
|
|
|
|
constructor->InstanceTemplate())
|
2016-06-22 22:46:22 +00:00
|
|
|
|
.SetMethod("toPNG", &NativeImage::ToPNG)
|
|
|
|
|
.SetMethod("toJPEG", &NativeImage::ToJPEG)
|
2016-08-01 00:13:31 +00:00
|
|
|
|
.SetMethod("toBitmap", &NativeImage::ToBitmap)
|
2016-08-04 17:36:01 +00:00
|
|
|
|
.SetMethod("getBitmap", &NativeImage::GetBitmap)
|
2020-05-18 16:29:24 +00:00
|
|
|
|
.SetMethod("getScaleFactors", &NativeImage::GetScaleFactors)
|
2016-04-25 01:17:54 +00:00
|
|
|
|
.SetMethod("getNativeHandle", &NativeImage::GetNativeHandle)
|
|
|
|
|
.SetMethod("toDataURL", &NativeImage::ToDataURL)
|
|
|
|
|
.SetMethod("isEmpty", &NativeImage::IsEmpty)
|
|
|
|
|
.SetMethod("getSize", &NativeImage::GetSize)
|
2020-03-18 01:06:52 +00:00
|
|
|
|
.SetMethod("setTemplateImage", &NativeImage::SetTemplateImage)
|
|
|
|
|
.SetMethod("isTemplateImage", &NativeImage::IsTemplateImage)
|
2019-05-07 13:52:07 +00:00
|
|
|
|
.SetProperty("isMacTemplateImage", &NativeImage::IsTemplateImage,
|
|
|
|
|
&NativeImage::SetTemplateImage)
|
2016-10-04 17:26:01 +00:00
|
|
|
|
.SetMethod("resize", &NativeImage::Resize)
|
2016-10-04 18:23:18 +00:00
|
|
|
|
.SetMethod("crop", &NativeImage::Crop)
|
2016-10-05 16:15:06 +00:00
|
|
|
|
.SetMethod("getAspectRatio", &NativeImage::GetAspectRatio)
|
2018-02-20 15:15:27 +00:00
|
|
|
|
.SetMethod("addRepresentation", &NativeImage::AddRepresentation);
|
2016-04-25 01:17:54 +00:00
|
|
|
|
}
|
|
|
|
|
|
2020-07-13 21:44:12 +00:00
|
|
|
|
const char* NativeImage::GetTypeName() {
|
|
|
|
|
return "NativeImage";
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// static
|
|
|
|
|
gin::WrapperInfo NativeImage::kWrapperInfo = {gin::kEmbedderNativeGin};
|
|
|
|
|
|
2022-06-29 19:55:47 +00:00
|
|
|
|
} // namespace electron::api
|
2015-02-11 08:03:19 +00:00
|
|
|
|
|
|
|
|
|
namespace {
|
|
|
|
|
|
2019-03-16 00:32:04 +00:00
|
|
|
|
using electron::api::NativeImage;
|
|
|
|
|
|
2018-04-18 01:55:30 +00:00
|
|
|
|
void Initialize(v8::Local<v8::Object> exports,
|
|
|
|
|
v8::Local<v8::Value> unused,
|
|
|
|
|
v8::Local<v8::Context> context,
|
|
|
|
|
void* priv) {
|
2019-03-16 00:32:04 +00:00
|
|
|
|
v8::Isolate* isolate = context->GetIsolate();
|
2019-10-18 00:31:29 +00:00
|
|
|
|
gin_helper::Dictionary dict(isolate, exports);
|
2023-08-21 01:43:41 +00:00
|
|
|
|
auto native_image = gin_helper::Dictionary::CreateEmpty(isolate);
|
2019-03-16 00:32:04 +00:00
|
|
|
|
dict.Set("nativeImage", native_image);
|
|
|
|
|
|
|
|
|
|
native_image.SetMethod("createEmpty", &NativeImage::CreateEmpty);
|
|
|
|
|
native_image.SetMethod("createFromPath", &NativeImage::CreateFromPath);
|
|
|
|
|
native_image.SetMethod("createFromBitmap", &NativeImage::CreateFromBitmap);
|
|
|
|
|
native_image.SetMethod("createFromBuffer", &NativeImage::CreateFromBuffer);
|
|
|
|
|
native_image.SetMethod("createFromDataURL", &NativeImage::CreateFromDataURL);
|
|
|
|
|
native_image.SetMethod("createFromNamedImage",
|
|
|
|
|
&NativeImage::CreateFromNamedImage);
|
2022-02-10 02:58:52 +00:00
|
|
|
|
#if !BUILDFLAG(IS_LINUX)
|
2020-08-24 16:36:13 +00:00
|
|
|
|
native_image.SetMethod("createThumbnailFromPath",
|
|
|
|
|
&NativeImage::CreateThumbnailFromPath);
|
|
|
|
|
#endif
|
2015-02-11 08:03:19 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
} // namespace
|
|
|
|
|
|
2023-02-09 01:31:38 +00:00
|
|
|
|
NODE_LINKED_BINDING_CONTEXT_AWARE(electron_common_native_image, Initialize)
|