From d67863aa9f5e82b495318bb1eb068f7dad2027c9 Mon Sep 17 00:00:00 2001 From: Kevin Sawicki Date: Tue, 7 Mar 2017 13:48:12 -0800 Subject: [PATCH] Add addRepresentation buffer support --- atom/common/api/atom_api_native_image.cc | 25 +++++++++++++++++++++++- atom/common/api/atom_api_native_image.h | 2 ++ 2 files changed, 26 insertions(+), 1 deletion(-) diff --git a/atom/common/api/atom_api_native_image.cc b/atom/common/api/atom_api_native_image.cc index 98eac6b2ff68..e30b407e9ef0 100644 --- a/atom/common/api/atom_api_native_image.cc +++ b/atom/common/api/atom_api_native_image.cc @@ -15,7 +15,6 @@ #include "base/files/file_util.h" #include "base/strings/pattern.h" #include "base/strings/string_util.h" -#include "native_mate/dictionary.h" #include "native_mate/object_template_builder.h" #include "net/base/data_url.h" #include "third_party/skia/include/core/SkPixelRef.h" @@ -387,6 +386,29 @@ mate::Handle NativeImage::Crop(v8::Isolate* isolate, new NativeImage(isolate, gfx::Image(cropped))); } +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); + + v8::Local buffer; + if (options.Get("buffer", &buffer) && node::Buffer::HasInstance(buffer)) { + gfx::ImageSkia image_skia = image_.AsImageSkia(); + AddImageSkiaRep( + &image_skia, + reinterpret_cast(node::Buffer::Data(buffer)), + node::Buffer::Length(buffer), + width, height, scale_factor); + + if (IsEmpty()) { + gfx::Image image(image_skia); + image_.SwapRepresentations(&image); + } + } +} #if !defined(OS_MACOSX) void NativeImage::SetTemplateImage(bool setAsTemplate) { @@ -504,6 +526,7 @@ void NativeImage::BuildPrototype( .SetMethod("resize", &NativeImage::Resize) .SetMethod("crop", &NativeImage::Crop) .SetMethod("getAspectRatio", &NativeImage::GetAspectRatio) + .SetMethod("addRepresentation", &NativeImage::AddRepresentation) // TODO(kevinsawicki): Remove in 2.0, deprecate before then with warnings .SetMethod("toPng", &NativeImage::ToPNG) .SetMethod("toJpeg", &NativeImage::ToJPEG); diff --git a/atom/common/api/atom_api_native_image.h b/atom/common/api/atom_api_native_image.h index a6614a81b0ec..6ff7a0e29f37 100644 --- a/atom/common/api/atom_api_native_image.h +++ b/atom/common/api/atom_api_native_image.h @@ -9,6 +9,7 @@ #include #include "base/values.h" +#include "native_mate/dictionary.h" #include "native_mate/handle.h" #include "native_mate/wrappable.h" #include "ui/gfx/geometry/rect.h" @@ -85,6 +86,7 @@ class NativeImage : public mate::Wrappable { bool IsEmpty(); gfx::Size GetSize(); float GetAspectRatio(); + void AddRepresentation(const mate::Dictionary& options); // Mark the image as template image. void SetTemplateImage(bool setAsTemplate);