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.
|
|
|
|
|
|
|
|
|
|
#include "atom/common/api/atom_api_native_image.h"
|
|
|
|
|
|
2015-02-11 13:59:08 +00:00
|
|
|
|
#include <string>
|
|
|
|
|
#include <vector>
|
|
|
|
|
|
2015-02-12 11:34:21 +00:00
|
|
|
|
#include "atom/common/asar/asar_util.h"
|
2015-02-11 10:41:06 +00:00
|
|
|
|
#include "atom/common/native_mate_converters/file_path_converter.h"
|
2015-02-11 08:49:43 +00:00
|
|
|
|
#include "atom/common/native_mate_converters/gfx_converter.h"
|
2015-02-12 06:27:53 +00:00
|
|
|
|
#include "atom/common/native_mate_converters/gurl_converter.h"
|
2016-10-04 17:26:01 +00:00
|
|
|
|
#include "atom/common/native_mate_converters/value_converter.h"
|
2016-03-04 20:05:36 +00:00
|
|
|
|
#include "base/files/file_util.h"
|
2015-09-02 07:16:49 +00:00
|
|
|
|
#include "base/strings/pattern.h"
|
2016-08-26 22:30:02 +00:00
|
|
|
|
#include "base/strings/string_util.h"
|
2015-02-11 08:03:19 +00:00
|
|
|
|
#include "native_mate/object_template_builder.h"
|
2015-02-12 06:27:53 +00:00
|
|
|
|
#include "net/base/data_url.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"
|
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
|
|
|
|
|
2015-08-08 14:58:05 +00:00
|
|
|
|
#if defined(OS_WIN)
|
2015-08-08 18:57:29 +00:00
|
|
|
|
#include "atom/common/asar/archive.h"
|
2015-08-20 16:26:20 +00:00
|
|
|
|
#include "base/win/scoped_gdi_object.h"
|
2015-08-08 14:58:05 +00:00
|
|
|
|
#include "ui/gfx/icon_util.h"
|
|
|
|
|
#endif
|
2015-02-11 08:03:19 +00:00
|
|
|
|
|
2016-09-06 08:24:37 +00:00
|
|
|
|
#include "atom/common/node_includes.h"
|
|
|
|
|
|
2015-02-11 08:03:19 +00:00
|
|
|
|
namespace atom {
|
|
|
|
|
|
|
|
|
|
namespace api {
|
|
|
|
|
|
|
|
|
|
namespace {
|
|
|
|
|
|
2015-02-11 10:41:06 +00:00
|
|
|
|
struct ScaleFactorPair {
|
|
|
|
|
const char* name;
|
|
|
|
|
float scale;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
ScaleFactorPair kScaleFactorPairs[] = {
|
|
|
|
|
// The "@2x" is put as first one to make scale matching faster.
|
|
|
|
|
{ "@2x" , 2.0f },
|
|
|
|
|
{ "@3x" , 3.0f },
|
|
|
|
|
{ "@1x" , 1.0f },
|
|
|
|
|
{ "@4x" , 4.0f },
|
|
|
|
|
{ "@5x" , 5.0f },
|
|
|
|
|
{ "@1.25x" , 1.25f },
|
|
|
|
|
{ "@1.33x" , 1.33f },
|
|
|
|
|
{ "@1.4x" , 1.4f },
|
|
|
|
|
{ "@1.5x" , 1.5f },
|
|
|
|
|
{ "@1.8x" , 1.8f },
|
|
|
|
|
{ "@2.5x" , 2.5f },
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
float GetScaleFactorFromPath(const base::FilePath& path) {
|
|
|
|
|
std::string filename(path.BaseName().RemoveExtension().AsUTF8Unsafe());
|
|
|
|
|
|
|
|
|
|
// We don't try to convert string to float here because it is very very
|
|
|
|
|
// expensive.
|
2016-07-10 13:56:42 +00:00
|
|
|
|
for (const auto& kScaleFactorPair : kScaleFactorPairs) {
|
2016-07-10 11:32:40 +00:00
|
|
|
|
if (base::EndsWith(filename, kScaleFactorPair.name,
|
2015-12-07 11:56:23 +00:00
|
|
|
|
base::CompareCase::INSENSITIVE_ASCII))
|
2016-07-10 11:32:40 +00:00
|
|
|
|
return kScaleFactorPair.scale;
|
2015-02-11 10:41:06 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return 1.0f;
|
|
|
|
|
}
|
|
|
|
|
|
2017-03-07 00:29:39 +00:00
|
|
|
|
// Get the scale factor from options object at the first argument
|
|
|
|
|
float GetScaleFactorFromOptions(mate::Arguments* args) {
|
|
|
|
|
float scale_factor = 1.0f;
|
|
|
|
|
mate::Dictionary options;
|
|
|
|
|
if (args->GetNext(&options))
|
|
|
|
|
options.Get("scaleFactor", &scale_factor);
|
|
|
|
|
return scale_factor;
|
|
|
|
|
}
|
|
|
|
|
|
2015-02-11 10:41:06 +00:00
|
|
|
|
bool AddImageSkiaRep(gfx::ImageSkia* image,
|
2015-02-12 07:19:05 +00:00
|
|
|
|
const unsigned char* data,
|
|
|
|
|
size_t size,
|
2016-12-09 23:12:36 +00:00
|
|
|
|
int width,
|
|
|
|
|
int height,
|
2015-02-11 10:41:06 +00:00
|
|
|
|
double scale_factor) {
|
2016-05-23 01:59:39 +00:00
|
|
|
|
std::unique_ptr<SkBitmap> decoded(new SkBitmap());
|
2015-02-11 10:41:06 +00:00
|
|
|
|
|
|
|
|
|
// Try PNG first.
|
2017-11-30 16:56:32 +00:00
|
|
|
|
if (!gfx::PNGCodec::Decode(data, size, decoded.get())) {
|
2015-02-11 10:41:06 +00:00
|
|
|
|
// Try JPEG.
|
2016-07-04 06:08:55 +00:00
|
|
|
|
decoded = gfx::JPEGCodec::Decode(data, size);
|
2017-11-30 16:56:32 +00:00
|
|
|
|
if (decoded) {
|
|
|
|
|
// `JPEGCodec::Decode()` doesn't tell `SkBitmap` instance it creates
|
|
|
|
|
// that all of its pixels are opaque, that's why the bitmap gets
|
|
|
|
|
// an alpha type `kPremul_SkAlphaType` instead of `kOpaque_SkAlphaType`.
|
|
|
|
|
// Let's fix it here.
|
|
|
|
|
// TODO(alexeykuzmin): This workaround should be removed
|
|
|
|
|
// when the `JPEGCodec::Decode()` code is fixed.
|
|
|
|
|
// See https://github.com/electron/electron/issues/11294.
|
|
|
|
|
decoded->setAlphaType(SkAlphaType::kOpaque_SkAlphaType);
|
|
|
|
|
}
|
|
|
|
|
}
|
2015-02-11 10:41:06 +00:00
|
|
|
|
|
2016-12-09 23:12:36 +00:00
|
|
|
|
if (!decoded) {
|
|
|
|
|
// Try Bitmap
|
|
|
|
|
if (width > 0 && height > 0) {
|
|
|
|
|
decoded.reset(new SkBitmap);
|
|
|
|
|
decoded->allocN32Pixels(width, height, false);
|
|
|
|
|
decoded->setPixels(
|
|
|
|
|
const_cast<void*>(reinterpret_cast<const void*>(data)));
|
|
|
|
|
} else {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
}
|
2015-02-11 10:41:06 +00:00
|
|
|
|
|
2015-08-05 07:49:37 +00:00
|
|
|
|
image->AddRepresentation(gfx::ImageSkiaRep(*decoded, scale_factor));
|
2015-02-11 10:41:06 +00:00
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
2015-02-12 07:19:05 +00:00
|
|
|
|
bool AddImageSkiaRep(gfx::ImageSkia* image,
|
|
|
|
|
const base::FilePath& path,
|
|
|
|
|
double scale_factor) {
|
|
|
|
|
std::string file_contents;
|
2015-02-12 11:34:21 +00:00
|
|
|
|
if (!asar::ReadFileToString(path, &file_contents))
|
2015-02-12 07:19:05 +00:00
|
|
|
|
return false;
|
|
|
|
|
|
|
|
|
|
const unsigned char* data =
|
|
|
|
|
reinterpret_cast<const unsigned char*>(file_contents.data());
|
|
|
|
|
size_t size = file_contents.size();
|
2016-12-09 23:12:36 +00:00
|
|
|
|
return AddImageSkiaRep(image, data, size, 0, 0, scale_factor);
|
2015-02-12 07:19:05 +00:00
|
|
|
|
}
|
|
|
|
|
|
2015-02-11 10:41:06 +00:00
|
|
|
|
bool PopulateImageSkiaRepsFromPath(gfx::ImageSkia* image,
|
|
|
|
|
const base::FilePath& path) {
|
|
|
|
|
bool succeed = false;
|
|
|
|
|
std::string filename(path.BaseName().RemoveExtension().AsUTF8Unsafe());
|
2015-09-02 07:16:49 +00:00
|
|
|
|
if (base::MatchPattern(filename, "*@*x"))
|
2015-02-11 10:41:06 +00:00
|
|
|
|
// Don't search for other representations if the DPI has been specified.
|
|
|
|
|
return AddImageSkiaRep(image, path, GetScaleFactorFromPath(path));
|
|
|
|
|
else
|
|
|
|
|
succeed |= AddImageSkiaRep(image, path, 1.0f);
|
|
|
|
|
|
|
|
|
|
for (const ScaleFactorPair& pair : kScaleFactorPairs)
|
|
|
|
|
succeed |= AddImageSkiaRep(image,
|
|
|
|
|
path.InsertBeforeExtensionASCII(pair.name),
|
|
|
|
|
pair.scale);
|
|
|
|
|
return succeed;
|
|
|
|
|
}
|
2015-02-12 04:24:18 +00:00
|
|
|
|
|
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;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
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;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2015-02-12 04:24:18 +00:00
|
|
|
|
#if defined(OS_MACOSX)
|
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
|
|
|
|
|
2015-08-21 04:06:38 +00:00
|
|
|
|
#if defined(OS_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.
|
2016-05-20 08:51:05 +00:00
|
|
|
|
return base::win::ScopedHICON(static_cast<HICON>(
|
|
|
|
|
LoadImage(NULL, 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
|
|
|
|
|
2016-06-27 12:38:12 +00:00
|
|
|
|
bool ReadImageSkiaFromICO(gfx::ImageSkia* image, HICON icon) {
|
2015-08-21 04:06:38 +00:00
|
|
|
|
// Convert the icon from the Windows specific HICON to gfx::ImageSkia.
|
2016-05-23 01:59:39 +00:00
|
|
|
|
std::unique_ptr<SkBitmap> bitmap(IconUtil::CreateSkBitmapFromHICON(icon));
|
2016-06-27 12:38:12 +00:00
|
|
|
|
if (!bitmap)
|
|
|
|
|
return false;
|
|
|
|
|
|
2015-08-21 04:06:38 +00:00
|
|
|
|
image->AddRepresentation(gfx::ImageSkiaRep(*bitmap, 1.0f));
|
2016-06-27 12:38:12 +00:00
|
|
|
|
return true;
|
2015-08-21 04:06:38 +00:00
|
|
|
|
}
|
|
|
|
|
#endif
|
|
|
|
|
|
2016-08-05 08:55:57 +00:00
|
|
|
|
void Noop(char*, void*) {
|
|
|
|
|
}
|
|
|
|
|
|
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)
|
2017-05-02 02:29:16 +00:00
|
|
|
|
: image_(image) {
|
|
|
|
|
Init(isolate);
|
|
|
|
|
if (image_.HasRepresentation(gfx::Image::kImageRepSkia)) {
|
|
|
|
|
isolate->AdjustAmountOfExternalAllocatedMemory(
|
2017-05-02 04:08:33 +00:00
|
|
|
|
image_.ToImageSkia()->bitmap()->computeSize64());
|
2017-05-02 02:29:16 +00:00
|
|
|
|
}
|
2017-05-02 02:28:32 +00:00
|
|
|
|
MarkHighMemoryUsage();
|
2016-04-25 01:17:54 +00:00
|
|
|
|
}
|
2015-02-11 08:03:19 +00:00
|
|
|
|
|
2016-05-20 02:31:02 +00:00
|
|
|
|
#if defined(OS_WIN)
|
2016-05-20 08:51:05 +00:00
|
|
|
|
NativeImage::NativeImage(v8::Isolate* isolate, const base::FilePath& hicon_path)
|
2017-05-02 02:29:16 +00:00
|
|
|
|
: hicon_path_(hicon_path) {
|
2016-05-20 08:51:05 +00:00
|
|
|
|
// Use the 256x256 icon as fallback icon.
|
|
|
|
|
gfx::ImageSkia image_skia;
|
|
|
|
|
ReadImageSkiaFromICO(&image_skia, GetHICON(256));
|
|
|
|
|
image_ = gfx::Image(image_skia);
|
2016-05-20 02:31:02 +00:00
|
|
|
|
Init(isolate);
|
2017-05-11 09:38:34 +00:00
|
|
|
|
if (image_.HasRepresentation(gfx::Image::kImageRepSkia)) {
|
|
|
|
|
isolate->AdjustAmountOfExternalAllocatedMemory(
|
|
|
|
|
image_.ToImageSkia()->bitmap()->computeSize64());
|
|
|
|
|
}
|
2017-05-02 02:28:32 +00:00
|
|
|
|
MarkHighMemoryUsage();
|
2016-05-20 02:31:02 +00:00
|
|
|
|
}
|
|
|
|
|
#endif
|
|
|
|
|
|
2017-04-12 14:17:07 +00:00
|
|
|
|
NativeImage::~NativeImage() {
|
2017-05-02 02:29:16 +00:00
|
|
|
|
if (image_.HasRepresentation(gfx::Image::kImageRepSkia)) {
|
|
|
|
|
isolate()->AdjustAmountOfExternalAllocatedMemory(
|
2017-05-02 04:08:33 +00:00
|
|
|
|
- image_.ToImageSkia()->bitmap()->computeSize64());
|
2017-05-02 02:29:16 +00:00
|
|
|
|
}
|
2017-04-12 14:17:07 +00:00
|
|
|
|
}
|
2015-02-11 08:03:19 +00:00
|
|
|
|
|
2016-05-20 07:55:22 +00:00
|
|
|
|
#if defined(OS_WIN)
|
2016-05-20 08:51:05 +00:00
|
|
|
|
HICON NativeImage::GetHICON(int size) {
|
|
|
|
|
auto iter = hicons_.find(size);
|
|
|
|
|
if (iter != hicons_.end())
|
|
|
|
|
return iter->second.get();
|
|
|
|
|
|
|
|
|
|
// First try loading the icon with specified size.
|
|
|
|
|
if (!hicon_path_.empty()) {
|
|
|
|
|
hicons_[size] = std::move(ReadICOFromPath(size, hicon_path_));
|
|
|
|
|
return hicons_[size].get();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Then convert the image to ICO.
|
2016-05-20 07:55:22 +00:00
|
|
|
|
if (image_.IsEmpty())
|
|
|
|
|
return NULL;
|
2016-05-20 08:51:05 +00:00
|
|
|
|
hicons_[size] = std::move(
|
|
|
|
|
IconUtil::CreateHICONFromSkBitmap(image_.AsBitmap()));
|
|
|
|
|
return hicons_[size].get();
|
2016-05-20 07:55:22 +00:00
|
|
|
|
}
|
|
|
|
|
#endif
|
|
|
|
|
|
2017-03-07 00:19:16 +00:00
|
|
|
|
v8::Local<v8::Value> NativeImage::ToPNG(mate::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 =
|
|
|
|
|
image_.AsImageSkia().GetRepresentation(scale_factor).sk_bitmap();
|
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
|
|
|
|
|
2017-03-07 00:19:16 +00:00
|
|
|
|
v8::Local<v8::Value> NativeImage::ToBitmap(mate::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 =
|
|
|
|
|
image_.AsImageSkia().GetRepresentation(scale_factor).sk_bitmap();
|
|
|
|
|
SkPixelRef* ref = bitmap.pixelRef();
|
|
|
|
|
if (!ref)
|
|
|
|
|
return node::Buffer::New(args->isolate(), 0).ToLocalChecked();
|
|
|
|
|
return node::Buffer::Copy(args->isolate(),
|
2016-07-31 03:11:18 +00:00
|
|
|
|
reinterpret_cast<const char*>(ref->pixels()),
|
2017-03-07 00:19:16 +00:00
|
|
|
|
bitmap.getSafeSize()).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();
|
2015-08-04 08:50:33 +00:00
|
|
|
|
return node::Buffer::Copy(
|
|
|
|
|
isolate,
|
|
|
|
|
reinterpret_cast<const char*>(&output.front()),
|
2017-03-07 00:19:16 +00:00
|
|
|
|
output.size()).ToLocalChecked();
|
2015-02-11 09:47:54 +00:00
|
|
|
|
}
|
|
|
|
|
|
2017-03-07 00:19:16 +00:00
|
|
|
|
std::string NativeImage::ToDataURL(mate::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)
|
|
|
|
|
return webui::GetPngDataUrl(png->front(), png->size());
|
|
|
|
|
}
|
2015-02-12 06:32:51 +00:00
|
|
|
|
|
2017-03-07 00:19:16 +00:00
|
|
|
|
return webui::GetBitmapDataUrl(
|
|
|
|
|
image_.AsImageSkia().GetRepresentation(scale_factor).sk_bitmap());
|
|
|
|
|
}
|
2017-01-25 21:22:44 +00:00
|
|
|
|
|
2017-03-07 00:19:16 +00:00
|
|
|
|
v8::Local<v8::Value> NativeImage::GetBitmap(mate::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 =
|
|
|
|
|
image_.AsImageSkia().GetRepresentation(scale_factor).sk_bitmap();
|
|
|
|
|
SkPixelRef* ref = bitmap.pixelRef();
|
|
|
|
|
if (!ref)
|
|
|
|
|
return node::Buffer::New(args->isolate(), 0).ToLocalChecked();
|
|
|
|
|
return node::Buffer::New(args->isolate(),
|
2016-08-05 08:55:57 +00:00
|
|
|
|
reinterpret_cast<char*>(ref->pixels()),
|
2017-03-07 00:19:16 +00:00
|
|
|
|
bitmap.getSafeSize(),
|
2016-08-05 08:55:57 +00:00
|
|
|
|
&Noop,
|
|
|
|
|
nullptr).ToLocalChecked();
|
|
|
|
|
}
|
|
|
|
|
|
2016-03-17 13:01:31 +00:00
|
|
|
|
v8::Local<v8::Value> NativeImage::GetNativeHandle(v8::Isolate* isolate,
|
|
|
|
|
mate::Arguments* args) {
|
2016-03-14 03:08:53 +00:00
|
|
|
|
#if defined(OS_MACOSX)
|
2017-01-25 21:22:44 +00:00
|
|
|
|
if (IsEmpty()) return node::Buffer::New(isolate, 0).ToLocalChecked();
|
|
|
|
|
|
2016-03-17 13:01:31 +00:00
|
|
|
|
NSImage* ptr = image_.AsNSImage();
|
|
|
|
|
return node::Buffer::Copy(
|
|
|
|
|
isolate,
|
|
|
|
|
reinterpret_cast<char*>(ptr),
|
|
|
|
|
sizeof(void*)).ToLocalChecked();
|
2016-03-15 02:48:40 +00:00
|
|
|
|
#else
|
2016-03-17 13:01:31 +00:00
|
|
|
|
args->ThrowError("Not implemented");
|
2016-03-15 02:48:40 +00:00
|
|
|
|
return v8::Undefined(isolate);
|
2016-03-14 03:08:53 +00:00
|
|
|
|
#endif
|
|
|
|
|
}
|
|
|
|
|
|
2015-02-11 08:49:43 +00:00
|
|
|
|
bool NativeImage::IsEmpty() {
|
|
|
|
|
return image_.IsEmpty();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
gfx::Size NativeImage::GetSize() {
|
|
|
|
|
return image_.Size();
|
|
|
|
|
}
|
|
|
|
|
|
2016-10-05 16:15:06 +00:00
|
|
|
|
float NativeImage::GetAspectRatio() {
|
|
|
|
|
gfx::Size size = GetSize();
|
|
|
|
|
if (size.IsEmpty())
|
|
|
|
|
return 1.f;
|
|
|
|
|
else
|
|
|
|
|
return static_cast<float>(size.width()) / static_cast<float>(size.height());
|
|
|
|
|
}
|
|
|
|
|
|
2016-10-04 17:26:01 +00:00
|
|
|
|
mate::Handle<NativeImage> NativeImage::Resize(
|
|
|
|
|
v8::Isolate* isolate, const base::DictionaryValue& options) {
|
|
|
|
|
gfx::Size size = GetSize();
|
|
|
|
|
int width = size.width();
|
|
|
|
|
int height = size.height();
|
2016-10-05 16:15:06 +00:00
|
|
|
|
bool width_set = options.GetInteger("width", &width);
|
|
|
|
|
bool height_set = options.GetInteger("height", &height);
|
|
|
|
|
size.SetSize(width, height);
|
2017-04-12 18:54:03 +00:00
|
|
|
|
|
2016-10-05 16:15:06 +00:00
|
|
|
|
if (width_set && !height_set) {
|
|
|
|
|
// Scale height to preserve original aspect ratio
|
|
|
|
|
size.set_height(width);
|
|
|
|
|
size = gfx::ScaleToRoundedSize(size, 1.f, 1.f / GetAspectRatio());
|
|
|
|
|
} else if (height_set && !width_set) {
|
|
|
|
|
// Scale width to preserve original aspect ratio
|
|
|
|
|
size.set_width(height);
|
|
|
|
|
size = gfx::ScaleToRoundedSize(size, GetAspectRatio(), 1.f);
|
|
|
|
|
}
|
2016-10-04 17:26:01 +00:00
|
|
|
|
|
|
|
|
|
skia::ImageOperations::ResizeMethod method =
|
|
|
|
|
skia::ImageOperations::ResizeMethod::RESIZE_BEST;
|
|
|
|
|
std::string quality;
|
|
|
|
|
options.GetString("quality", &quality);
|
|
|
|
|
if (quality == "good")
|
|
|
|
|
method = skia::ImageOperations::ResizeMethod::RESIZE_GOOD;
|
|
|
|
|
else if (quality == "better")
|
|
|
|
|
method = skia::ImageOperations::ResizeMethod::RESIZE_BETTER;
|
|
|
|
|
|
|
|
|
|
gfx::ImageSkia resized = gfx::ImageSkiaOperations::CreateResizedImage(
|
|
|
|
|
image_.AsImageSkia(), method, size);
|
|
|
|
|
return mate::CreateHandle(isolate,
|
|
|
|
|
new NativeImage(isolate, gfx::Image(resized)));
|
|
|
|
|
}
|
|
|
|
|
|
2016-10-04 18:23:18 +00:00
|
|
|
|
mate::Handle<NativeImage> NativeImage::Crop(v8::Isolate* isolate,
|
2016-10-05 15:02:04 +00:00
|
|
|
|
const gfx::Rect& rect) {
|
2016-10-04 18:23:18 +00:00
|
|
|
|
gfx::ImageSkia cropped = gfx::ImageSkiaOperations::ExtractSubset(
|
2016-10-05 15:02:04 +00:00
|
|
|
|
image_.AsImageSkia(), rect);
|
2016-10-04 18:23:18 +00:00
|
|
|
|
return mate::CreateHandle(isolate,
|
|
|
|
|
new NativeImage(isolate, gfx::Image(cropped)));
|
|
|
|
|
}
|
|
|
|
|
|
2017-03-07 21:48:12 +00:00
|
|
|
|
void NativeImage::AddRepresentation(const mate::Dictionary& options) {
|
|
|
|
|
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)) {
|
|
|
|
|
AddImageSkiaRep(
|
|
|
|
|
&image_skia,
|
|
|
|
|
reinterpret_cast<unsigned char*>(node::Buffer::Data(buffer)),
|
|
|
|
|
node::Buffer::Length(buffer),
|
|
|
|
|
width, height, scale_factor);
|
2017-03-07 22:24:37 +00:00
|
|
|
|
skia_rep_added = true;
|
|
|
|
|
} else if (options.Get("dataURL", &url)) {
|
|
|
|
|
std::string mime_type, charset, data;
|
|
|
|
|
if (net::DataURL::Parse(url, &mime_type, &charset, &data)) {
|
|
|
|
|
if (mime_type == "image/png" || mime_type == "image/jpeg") {
|
|
|
|
|
AddImageSkiaRep(
|
|
|
|
|
&image_skia,
|
|
|
|
|
reinterpret_cast<const unsigned char*>(data.c_str()),
|
|
|
|
|
data.size(),
|
|
|
|
|
width, height, scale_factor);
|
|
|
|
|
skia_rep_added = true;
|
|
|
|
|
}
|
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);
|
|
|
|
|
image_.SwapRepresentations(&image);
|
|
|
|
|
}
|
2017-03-07 21:48:12 +00:00
|
|
|
|
}
|
2016-10-04 18:23:18 +00:00
|
|
|
|
|
2015-04-13 03:53:24 +00:00
|
|
|
|
#if !defined(OS_MACOSX)
|
|
|
|
|
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
|
|
|
|
|
mate::Handle<NativeImage> NativeImage::CreateEmpty(v8::Isolate* isolate) {
|
2016-04-25 01:17:54 +00:00
|
|
|
|
return mate::CreateHandle(isolate, new NativeImage(isolate, gfx::Image()));
|
2015-02-12 05:55:45 +00:00
|
|
|
|
}
|
|
|
|
|
|
2015-02-11 08:03:19 +00:00
|
|
|
|
// static
|
2015-02-11 10:02:59 +00:00
|
|
|
|
mate::Handle<NativeImage> NativeImage::Create(
|
|
|
|
|
v8::Isolate* isolate, const gfx::Image& image) {
|
2016-04-25 01:17:54 +00:00
|
|
|
|
return mate::CreateHandle(isolate, new NativeImage(isolate, image));
|
2015-02-11 08:03:19 +00:00
|
|
|
|
}
|
|
|
|
|
|
2015-02-11 10:02:59 +00:00
|
|
|
|
// static
|
|
|
|
|
mate::Handle<NativeImage> NativeImage::CreateFromPNG(
|
2015-02-12 06:27:53 +00:00
|
|
|
|
v8::Isolate* isolate, const char* buffer, size_t length) {
|
2015-02-11 10:02:59 +00:00
|
|
|
|
gfx::Image image = gfx::Image::CreateFrom1xPNGBytes(
|
2015-02-12 06:27:53 +00:00
|
|
|
|
reinterpret_cast<const unsigned char*>(buffer), length);
|
2015-02-11 10:02:59 +00:00
|
|
|
|
return Create(isolate, image);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// static
|
|
|
|
|
mate::Handle<NativeImage> NativeImage::CreateFromJPEG(
|
2015-02-12 06:27:53 +00:00
|
|
|
|
v8::Isolate* isolate, const char* buffer, size_t length) {
|
2015-02-11 10:02:59 +00:00
|
|
|
|
gfx::Image image = gfx::ImageFrom1xJPEGEncodedData(
|
2015-02-12 06:27:53 +00:00
|
|
|
|
reinterpret_cast<const unsigned char*>(buffer), length);
|
2015-02-11 10:02:59 +00:00
|
|
|
|
return Create(isolate, image);
|
|
|
|
|
}
|
|
|
|
|
|
2015-02-11 10:41:06 +00:00
|
|
|
|
// static
|
|
|
|
|
mate::Handle<NativeImage> NativeImage::CreateFromPath(
|
|
|
|
|
v8::Isolate* isolate, const base::FilePath& path) {
|
2016-03-07 22:22:16 +00:00
|
|
|
|
base::FilePath image_path = NormalizePath(path);
|
2015-08-08 14:58:05 +00:00
|
|
|
|
#if defined(OS_WIN)
|
2016-05-20 13:22:15 +00:00
|
|
|
|
if (image_path.MatchesExtension(FILE_PATH_LITERAL(".ico"))) {
|
2016-05-20 02:31:02 +00:00
|
|
|
|
return mate::CreateHandle(isolate,
|
2016-05-20 08:51:05 +00:00
|
|
|
|
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;
|
|
|
|
|
PopulateImageSkiaRepsFromPath(&image_skia, image_path);
|
|
|
|
|
gfx::Image image(image_skia);
|
|
|
|
|
mate::Handle<NativeImage> handle = Create(isolate, image);
|
2015-02-12 04:24:18 +00:00
|
|
|
|
#if defined(OS_MACOSX)
|
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
|
|
|
|
}
|
|
|
|
|
|
2015-02-12 07:19:05 +00:00
|
|
|
|
// static
|
|
|
|
|
mate::Handle<NativeImage> NativeImage::CreateFromBuffer(
|
2015-05-22 11:11:22 +00:00
|
|
|
|
mate::Arguments* args, v8::Local<v8::Value> buffer) {
|
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
|
|
|
|
|
|
|
|
|
mate::Dictionary options;
|
|
|
|
|
if (args->GetNext(&options)) {
|
|
|
|
|
options.Get("width", &width);
|
|
|
|
|
options.Get("height", &height);
|
|
|
|
|
options.Get("scaleFactor", &scale_factor);
|
2016-12-14 18:18:01 +00:00
|
|
|
|
} else {
|
|
|
|
|
// TODO(kevinsawicki): Remove in 2.0, deprecate before then with warnings
|
|
|
|
|
args->GetNext(&scale_factor);
|
2016-12-13 20:30:45 +00:00
|
|
|
|
}
|
|
|
|
|
|
2015-02-12 07:19:05 +00:00
|
|
|
|
gfx::ImageSkia image_skia;
|
|
|
|
|
AddImageSkiaRep(&image_skia,
|
|
|
|
|
reinterpret_cast<unsigned char*>(node::Buffer::Data(buffer)),
|
|
|
|
|
node::Buffer::Length(buffer),
|
2016-12-09 23:12:36 +00:00
|
|
|
|
width,
|
|
|
|
|
height,
|
2015-02-12 07:19:05 +00:00
|
|
|
|
scale_factor);
|
|
|
|
|
return Create(args->isolate(), gfx::Image(image_skia));
|
|
|
|
|
}
|
|
|
|
|
|
2015-02-12 06:27:53 +00:00
|
|
|
|
// static
|
|
|
|
|
mate::Handle<NativeImage> NativeImage::CreateFromDataURL(
|
|
|
|
|
v8::Isolate* isolate, const GURL& url) {
|
|
|
|
|
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);
|
|
|
|
|
}
|
|
|
|
|
|
2017-10-09 16:13:40 +00:00
|
|
|
|
#if !defined(OS_MACOSX)
|
|
|
|
|
mate::Handle<NativeImage> NativeImage::CreateFromNamedImage(
|
|
|
|
|
mate::Arguments* args, const std::string& name) {
|
|
|
|
|
return CreateEmpty(args->isolate());
|
|
|
|
|
}
|
|
|
|
|
#endif
|
|
|
|
|
|
2016-04-25 01:17:54 +00:00
|
|
|
|
// static
|
|
|
|
|
void NativeImage::BuildPrototype(
|
2016-08-02 09:08:12 +00:00
|
|
|
|
v8::Isolate* isolate, v8::Local<v8::FunctionTemplate> prototype) {
|
2016-08-02 10:28:12 +00:00
|
|
|
|
prototype->SetClassName(mate::StringToV8(isolate, "NativeImage"));
|
2016-08-02 09:08:12 +00:00
|
|
|
|
mate::ObjectTemplateBuilder(isolate, prototype->PrototypeTemplate())
|
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)
|
2016-04-25 01:17:54 +00:00
|
|
|
|
.SetMethod("getNativeHandle", &NativeImage::GetNativeHandle)
|
|
|
|
|
.SetMethod("toDataURL", &NativeImage::ToDataURL)
|
|
|
|
|
.SetMethod("isEmpty", &NativeImage::IsEmpty)
|
|
|
|
|
.SetMethod("getSize", &NativeImage::GetSize)
|
|
|
|
|
.SetMethod("setTemplateImage", &NativeImage::SetTemplateImage)
|
2016-06-22 22:46:22 +00:00
|
|
|
|
.SetMethod("isTemplateImage", &NativeImage::IsTemplateImage)
|
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)
|
2017-03-07 21:48:12 +00:00
|
|
|
|
.SetMethod("addRepresentation", &NativeImage::AddRepresentation)
|
2016-06-22 22:46:22 +00:00
|
|
|
|
// TODO(kevinsawicki): Remove in 2.0, deprecate before then with warnings
|
|
|
|
|
.SetMethod("toPng", &NativeImage::ToPNG)
|
|
|
|
|
.SetMethod("toJpeg", &NativeImage::ToJPEG);
|
2016-04-25 01:17:54 +00:00
|
|
|
|
}
|
|
|
|
|
|
2015-02-11 08:03:19 +00:00
|
|
|
|
} // namespace api
|
|
|
|
|
|
|
|
|
|
} // namespace atom
|
|
|
|
|
|
2016-05-20 07:14:40 +00:00
|
|
|
|
namespace mate {
|
|
|
|
|
|
|
|
|
|
v8::Local<v8::Value> Converter<mate::Handle<atom::api::NativeImage>>::ToV8(
|
|
|
|
|
v8::Isolate* isolate,
|
|
|
|
|
const mate::Handle<atom::api::NativeImage>& val) {
|
|
|
|
|
return val.ToV8();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool Converter<mate::Handle<atom::api::NativeImage>>::FromV8(
|
|
|
|
|
v8::Isolate* isolate, v8::Local<v8::Value> val,
|
|
|
|
|
mate::Handle<atom::api::NativeImage>* out) {
|
|
|
|
|
// Try converting from file path.
|
|
|
|
|
base::FilePath path;
|
|
|
|
|
if (ConvertFromV8(isolate, val, &path)) {
|
|
|
|
|
*out = atom::api::NativeImage::CreateFromPath(isolate, path);
|
|
|
|
|
// Should throw when failed to initialize from path.
|
|
|
|
|
return !(*out)->image().IsEmpty();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
WrappableBase* wrapper = static_cast<WrappableBase*>(internal::FromV8Impl(
|
|
|
|
|
isolate, val));
|
|
|
|
|
if (!wrapper)
|
|
|
|
|
return false;
|
|
|
|
|
|
|
|
|
|
*out = CreateHandle(isolate, static_cast<atom::api::NativeImage*>(wrapper));
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
} // namespace mate
|
2015-02-11 08:03:19 +00:00
|
|
|
|
|
|
|
|
|
namespace {
|
|
|
|
|
|
2015-05-22 11:11:22 +00:00
|
|
|
|
void Initialize(v8::Local<v8::Object> exports, v8::Local<v8::Value> unused,
|
|
|
|
|
v8::Local<v8::Context> context, void* priv) {
|
2015-02-11 10:09:51 +00:00
|
|
|
|
mate::Dictionary dict(context->GetIsolate(), exports);
|
2015-02-12 05:55:45 +00:00
|
|
|
|
dict.SetMethod("createEmpty", &atom::api::NativeImage::CreateEmpty);
|
2015-02-11 10:41:06 +00:00
|
|
|
|
dict.SetMethod("createFromPath", &atom::api::NativeImage::CreateFromPath);
|
2015-02-12 07:19:05 +00:00
|
|
|
|
dict.SetMethod("createFromBuffer", &atom::api::NativeImage::CreateFromBuffer);
|
2015-11-13 08:03:40 +00:00
|
|
|
|
dict.SetMethod("createFromDataURL",
|
2015-02-12 06:27:53 +00:00
|
|
|
|
&atom::api::NativeImage::CreateFromDataURL);
|
2017-10-10 06:20:55 +00:00
|
|
|
|
dict.SetMethod("createFromNamedImage",
|
2017-10-10 06:17:51 +00:00
|
|
|
|
&atom::api::NativeImage::CreateFromNamedImage);
|
2015-02-11 08:03:19 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
} // namespace
|
|
|
|
|
|
|
|
|
|
NODE_MODULE_CONTEXT_AWARE_BUILTIN(atom_common_native_image, Initialize)
|