Add addRepresentation buffer support
authorKevin Sawicki <kevinsawicki@gmail.com>
Tue, 7 Mar 2017 21:48:12 +0000 (13:48 -0800)
committerKevin Sawicki <kevinsawicki@gmail.com>
Thu, 9 Mar 2017 17:56:42 +0000 (09:56 -0800)
atom/common/api/atom_api_native_image.cc
atom/common/api/atom_api_native_image.h

index 98eac6b..e30b407 100644 (file)
@@ -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> 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<v8::Value> buffer;
+  if (options.Get("buffer", &buffer) && node::Buffer::HasInstance(buffer)) {
+    gfx::ImageSkia image_skia = image_.AsImageSkia();
+    AddImageSkiaRep(
+        &image_skia,
+        reinterpret_cast<unsigned char*>(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);
index a6614a8..6ff7a0e 100644 (file)
@@ -9,6 +9,7 @@
 #include <string>
 
 #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<NativeImage> {
   bool IsEmpty();
   gfx::Size GetSize();
   float GetAspectRatio();
+  void AddRepresentation(const mate::Dictionary& options);
 
   // Mark the image as template image.
   void SetTemplateImage(bool setAsTemplate);