From 0bbbeb307e1b756ff86f5ba5d2bdfe263c721f5e Mon Sep 17 00:00:00 2001 From: Kevin Sawicki Date: Mon, 6 Mar 2017 13:24:50 -0800 Subject: [PATCH] Support toDataURL without 1x representation --- atom/common/api/atom_api_native_image.cc | 18 ++++++++++++------ atom/common/api/atom_api_native_image.h | 1 + spec/api-native-image-spec.js | 1 + 3 files changed, 14 insertions(+), 6 deletions(-) diff --git a/atom/common/api/atom_api_native_image.cc b/atom/common/api/atom_api_native_image.cc index 0c174941dbf5..95531b1ddf10 100644 --- a/atom/common/api/atom_api_native_image.cc +++ b/atom/common/api/atom_api_native_image.cc @@ -21,6 +21,7 @@ #include "net/base/data_url.h" #include "third_party/skia/include/core/SkPixelRef.h" #include "ui/base/layout.h" +#include "ui/base/webui/web_ui_util.h" #include "ui/gfx/codec/jpeg_codec.h" #include "ui/gfx/codec/png_codec.h" #include "ui/gfx/geometry/size.h" @@ -257,12 +258,12 @@ v8::Local NativeImage::ToJPEG(v8::Isolate* isolate, int quality) { } std::string NativeImage::ToDataURL() { - scoped_refptr png = image_.As1xPNGBytes(); - std::string data_url; - data_url.insert(data_url.end(), png->front(), png->front() + png->size()); - base::Base64Encode(data_url, &data_url); - data_url.insert(0, "data:image/png;base64,"); - return data_url; + if (HasRepresentation(1.0)) { + scoped_refptr png = image_.As1xPNGBytes(); + return webui::GetPngDataUrl(png->front(), png->size()); + } else { + return webui::GetBitmapDataUrl(image_.AsBitmap()); + } } v8::Local NativeImage::GetBitmap(v8::Isolate* isolate) { @@ -297,6 +298,10 @@ bool NativeImage::IsEmpty() { return image_.IsEmpty(); } +bool NativeImage::HasRepresentation(float scale_factor) { + return image_.AsImageSkia().HasRepresentation(scale_factor); +} + gfx::Size NativeImage::GetSize() { return image_.Size(); } @@ -468,6 +473,7 @@ void NativeImage::BuildPrototype( .SetMethod("resize", &NativeImage::Resize) .SetMethod("crop", &NativeImage::Crop) .SetMethod("getAspectRatio", &NativeImage::GetAspectRatio) + .SetMethod("hasRepresentation", &NativeImage::HasRepresentation) // 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 ee1b5f5d4b81..a653f15e0a71 100644 --- a/atom/common/api/atom_api_native_image.h +++ b/atom/common/api/atom_api_native_image.h @@ -83,6 +83,7 @@ class NativeImage : public mate::Wrappable { const gfx::Rect& rect); std::string ToDataURL(); bool IsEmpty(); + bool HasRepresentation(float scale_factor); gfx::Size GetSize(); float GetAspectRatio(); diff --git a/spec/api-native-image-spec.js b/spec/api-native-image-spec.js index 94492ca40084..9aa0ced3ef6d 100644 --- a/spec/api-native-image-spec.js +++ b/spec/api-native-image-spec.js @@ -9,6 +9,7 @@ describe('nativeImage module', () => { it('returns an empty image', () => { const empty = nativeImage.createEmpty() assert.equal(empty.isEmpty(), true) + assert.equal(empty.hasRepresentation(1.0), false) assert.equal(empty.getAspectRatio(), 1) assert.equal(empty.toDataURL(), 'data:image/png;base64,') assert.deepEqual(empty.getSize(), {width: 0, height: 0})