Add addRepresentation buffer support

This commit is contained in:
Kevin Sawicki 2017-03-07 13:48:12 -08:00
parent 17b70670f8
commit d67863aa9f
2 changed files with 26 additions and 1 deletions

View 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);

View 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);