Use NativeImage when converting images from V8
This commit is contained in:
parent
6556602bdc
commit
054b6a9817
6 changed files with 27 additions and 166 deletions
1
atom.gyp
1
atom.gyp
|
@ -246,7 +246,6 @@
|
||||||
'atom/common/native_mate_converters/gurl_converter.h',
|
'atom/common/native_mate_converters/gurl_converter.h',
|
||||||
'atom/common/native_mate_converters/image_converter.cc',
|
'atom/common/native_mate_converters/image_converter.cc',
|
||||||
'atom/common/native_mate_converters/image_converter.h',
|
'atom/common/native_mate_converters/image_converter.h',
|
||||||
'atom/common/native_mate_converters/image_converter_mac.mm',
|
|
||||||
'atom/common/native_mate_converters/string16_converter.h',
|
'atom/common/native_mate_converters/string16_converter.h',
|
||||||
'atom/common/native_mate_converters/v8_value_converter.cc',
|
'atom/common/native_mate_converters/v8_value_converter.cc',
|
||||||
'atom/common/native_mate_converters/v8_value_converter.h',
|
'atom/common/native_mate_converters/v8_value_converter.h',
|
||||||
|
|
|
@ -106,6 +106,8 @@ v8::Persistent<v8::ObjectTemplate> template_;
|
||||||
|
|
||||||
} // namespace
|
} // namespace
|
||||||
|
|
||||||
|
NativeImage::NativeImage() {}
|
||||||
|
|
||||||
NativeImage::NativeImage(const gfx::Image& image) : image_(image) {}
|
NativeImage::NativeImage(const gfx::Image& image) : image_(image) {}
|
||||||
|
|
||||||
NativeImage::~NativeImage() {}
|
NativeImage::~NativeImage() {}
|
||||||
|
|
|
@ -32,6 +32,11 @@ class NativeImage : public mate::Wrappable {
|
||||||
static mate::Handle<NativeImage> CreateFromPath(
|
static mate::Handle<NativeImage> CreateFromPath(
|
||||||
v8::Isolate* isolate, const base::FilePath& path);
|
v8::Isolate* isolate, const base::FilePath& path);
|
||||||
|
|
||||||
|
// The default constructor should only be used by image_converter.cc.
|
||||||
|
NativeImage();
|
||||||
|
|
||||||
|
const gfx::Image& image() const { return image_; }
|
||||||
|
|
||||||
protected:
|
protected:
|
||||||
explicit NativeImage(const gfx::Image& image);
|
explicit NativeImage(const gfx::Image& image);
|
||||||
virtual ~NativeImage();
|
virtual ~NativeImage();
|
||||||
|
|
|
@ -4,129 +4,45 @@
|
||||||
|
|
||||||
#include "atom/common/native_mate_converters/image_converter.h"
|
#include "atom/common/native_mate_converters/image_converter.h"
|
||||||
|
|
||||||
#include <string>
|
|
||||||
#include <vector>
|
|
||||||
|
|
||||||
#include "atom/common/api/atom_api_native_image.h"
|
#include "atom/common/api/atom_api_native_image.h"
|
||||||
#include "atom/common/native_mate_converters/file_path_converter.h"
|
#include "atom/common/native_mate_converters/file_path_converter.h"
|
||||||
#include "base/files/file_util.h"
|
|
||||||
#include "base/strings/string_util.h"
|
|
||||||
#include "ui/gfx/codec/jpeg_codec.h"
|
|
||||||
#include "ui/gfx/codec/png_codec.h"
|
|
||||||
#include "ui/gfx/image/image.h"
|
|
||||||
#include "ui/gfx/image/image_skia.h"
|
#include "ui/gfx/image/image_skia.h"
|
||||||
#include "ui/base/layout.h"
|
|
||||||
|
|
||||||
#include "atom/common/node_includes.h"
|
|
||||||
|
|
||||||
namespace mate {
|
namespace mate {
|
||||||
|
|
||||||
#if !defined(OS_MACOSX)
|
|
||||||
|
|
||||||
namespace {
|
|
||||||
|
|
||||||
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.
|
|
||||||
for (unsigned i = 0; i < arraysize(kScaleFactorPairs); ++i) {
|
|
||||||
if (EndsWith(filename, kScaleFactorPairs[i].name, true))
|
|
||||||
return kScaleFactorPairs[i].scale;
|
|
||||||
}
|
|
||||||
|
|
||||||
return 1.0f;
|
|
||||||
}
|
|
||||||
|
|
||||||
bool AddImageSkiaRep(gfx::ImageSkia* image,
|
|
||||||
const base::FilePath& path,
|
|
||||||
double scale_factor) {
|
|
||||||
std::string file_contents;
|
|
||||||
if (!base::ReadFileToString(path, &file_contents))
|
|
||||||
return false;
|
|
||||||
|
|
||||||
const unsigned char* data =
|
|
||||||
reinterpret_cast<const unsigned char*>(file_contents.data());
|
|
||||||
size_t size = file_contents.size();
|
|
||||||
scoped_ptr<SkBitmap> decoded(new SkBitmap());
|
|
||||||
|
|
||||||
// Try PNG first.
|
|
||||||
if (!gfx::PNGCodec::Decode(data, size, decoded.get()))
|
|
||||||
// Try JPEG.
|
|
||||||
decoded.reset(gfx::JPEGCodec::Decode(data, size));
|
|
||||||
|
|
||||||
if (!decoded)
|
|
||||||
return false;
|
|
||||||
|
|
||||||
image->AddRepresentation(gfx::ImageSkiaRep(*decoded.release(), scale_factor));
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
|
|
||||||
bool PopulateImageSkiaRepsFromPath(gfx::ImageSkia* image,
|
|
||||||
const base::FilePath& path) {
|
|
||||||
bool succeed = false;
|
|
||||||
std::string filename(path.BaseName().RemoveExtension().AsUTF8Unsafe());
|
|
||||||
if (MatchPattern(filename, "*@*x"))
|
|
||||||
// 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;
|
|
||||||
}
|
|
||||||
|
|
||||||
} // namespace
|
|
||||||
|
|
||||||
bool Converter<gfx::ImageSkia>::FromV8(v8::Isolate* isolate,
|
bool Converter<gfx::ImageSkia>::FromV8(v8::Isolate* isolate,
|
||||||
v8::Handle<v8::Value> val,
|
v8::Handle<v8::Value> val,
|
||||||
gfx::ImageSkia* out) {
|
gfx::ImageSkia* out) {
|
||||||
if (val->IsNull())
|
gfx::Image image;
|
||||||
return true;
|
if (!ConvertFromV8(isolate, val, &image))
|
||||||
|
|
||||||
base::FilePath path;
|
|
||||||
if (!Converter<base::FilePath>::FromV8(isolate, val, &path))
|
|
||||||
return false;
|
return false;
|
||||||
|
|
||||||
return PopulateImageSkiaRepsFromPath(out, path);
|
*out = image.AsImageSkia();
|
||||||
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
bool Converter<gfx::Image>::FromV8(v8::Isolate* isolate,
|
bool Converter<gfx::Image>::FromV8(v8::Isolate* isolate,
|
||||||
v8::Handle<v8::Value> val,
|
v8::Handle<v8::Value> val,
|
||||||
gfx::Image* out) {
|
gfx::Image* out) {
|
||||||
gfx::ImageSkia image;
|
if (val->IsNull())
|
||||||
if (!ConvertFromV8(isolate, val, &image))
|
return true;
|
||||||
return false;
|
|
||||||
|
|
||||||
*out = gfx::Image(image);
|
Handle<atom::api::NativeImage> native_image;
|
||||||
|
if (!ConvertFromV8(isolate, val, &native_image)) {
|
||||||
|
// Try converting from file path.
|
||||||
|
base::FilePath path;
|
||||||
|
if (!Converter<base::FilePath>::FromV8(isolate, val, &path))
|
||||||
|
return false;
|
||||||
|
|
||||||
|
native_image = atom::api::NativeImage::CreateFromPath(isolate, path);
|
||||||
|
if (native_image->image().IsEmpty())
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
*out = native_image->image();
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
#endif // !defined(OS_MACOSX)
|
|
||||||
|
|
||||||
v8::Handle<v8::Value> Converter<gfx::Image>::ToV8(v8::Isolate* isolate,
|
v8::Handle<v8::Value> Converter<gfx::Image>::ToV8(v8::Isolate* isolate,
|
||||||
const gfx::Image& val) {
|
const gfx::Image& val) {
|
||||||
return ConvertToV8(isolate, atom::api::NativeImage::Create(isolate, val));
|
return ConvertToV8(isolate, atom::api::NativeImage::Create(isolate, val));
|
||||||
|
|
|
@ -26,8 +26,7 @@ struct Converter<gfx::Image> {
|
||||||
static bool FromV8(v8::Isolate* isolate,
|
static bool FromV8(v8::Isolate* isolate,
|
||||||
v8::Handle<v8::Value> val,
|
v8::Handle<v8::Value> val,
|
||||||
gfx::Image* out);
|
gfx::Image* out);
|
||||||
static v8::Handle<v8::Value> ToV8(v8::Isolate* isolate,
|
static v8::Handle<v8::Value> ToV8(v8::Isolate* isolate, const gfx::Image& val);
|
||||||
const gfx::Image& val);
|
|
||||||
};
|
};
|
||||||
|
|
||||||
} // namespace mate
|
} // namespace mate
|
||||||
|
|
|
@ -1,60 +0,0 @@
|
||||||
// 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/native_mate_converters/image_converter.h"
|
|
||||||
|
|
||||||
#import <Cocoa/Cocoa.h>
|
|
||||||
|
|
||||||
#include "base/mac/foundation_util.h"
|
|
||||||
#include "base/mac/scoped_nsobject.h"
|
|
||||||
#include "base/strings/string_util.h"
|
|
||||||
#include "base/strings/sys_string_conversions.h"
|
|
||||||
#include "ui/gfx/image/image.h"
|
|
||||||
#include "ui/gfx/image/image_skia.h"
|
|
||||||
|
|
||||||
namespace {
|
|
||||||
|
|
||||||
bool IsTemplateImage(const std::string& path) {
|
|
||||||
return (MatchPattern(path, "*Template.*") ||
|
|
||||||
MatchPattern(path, "*Template@*x.*"));
|
|
||||||
}
|
|
||||||
|
|
||||||
} // namespace
|
|
||||||
|
|
||||||
namespace mate {
|
|
||||||
|
|
||||||
bool Converter<gfx::ImageSkia>::FromV8(v8::Isolate* isolate,
|
|
||||||
v8::Handle<v8::Value> val,
|
|
||||||
gfx::ImageSkia* out) {
|
|
||||||
gfx::Image image;
|
|
||||||
if (!ConvertFromV8(isolate, val, &image))
|
|
||||||
return false;
|
|
||||||
|
|
||||||
*out = image.AsImageSkia();
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
|
|
||||||
bool Converter<gfx::Image>::FromV8(v8::Isolate* isolate,
|
|
||||||
v8::Handle<v8::Value> val,
|
|
||||||
gfx::Image* out) {
|
|
||||||
if (val->IsNull())
|
|
||||||
return true;
|
|
||||||
|
|
||||||
std::string path;
|
|
||||||
if (!ConvertFromV8(isolate, val, &path))
|
|
||||||
return false;
|
|
||||||
|
|
||||||
base::scoped_nsobject<NSImage> image([[NSImage alloc]
|
|
||||||
initByReferencingFile:base::SysUTF8ToNSString(path)]);
|
|
||||||
if (![image isValid])
|
|
||||||
return false;
|
|
||||||
|
|
||||||
if (IsTemplateImage(path))
|
|
||||||
[image setTemplate:YES];
|
|
||||||
|
|
||||||
*out = gfx::Image(image.release());
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
|
|
||||||
} // namespace mate
|
|
Loading…
Add table
Add a link
Reference in a new issue