Support toDataURL without 1x representation
This commit is contained in:
parent
8eaf48e552
commit
0bbbeb307e
3 changed files with 14 additions and 6 deletions
|
@ -21,6 +21,7 @@
|
||||||
#include "net/base/data_url.h"
|
#include "net/base/data_url.h"
|
||||||
#include "third_party/skia/include/core/SkPixelRef.h"
|
#include "third_party/skia/include/core/SkPixelRef.h"
|
||||||
#include "ui/base/layout.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/jpeg_codec.h"
|
||||||
#include "ui/gfx/codec/png_codec.h"
|
#include "ui/gfx/codec/png_codec.h"
|
||||||
#include "ui/gfx/geometry/size.h"
|
#include "ui/gfx/geometry/size.h"
|
||||||
|
@ -257,12 +258,12 @@ v8::Local<v8::Value> NativeImage::ToJPEG(v8::Isolate* isolate, int quality) {
|
||||||
}
|
}
|
||||||
|
|
||||||
std::string NativeImage::ToDataURL() {
|
std::string NativeImage::ToDataURL() {
|
||||||
scoped_refptr<base::RefCountedMemory> png = image_.As1xPNGBytes();
|
if (HasRepresentation(1.0)) {
|
||||||
std::string data_url;
|
scoped_refptr<base::RefCountedMemory> png = image_.As1xPNGBytes();
|
||||||
data_url.insert(data_url.end(), png->front(), png->front() + png->size());
|
return webui::GetPngDataUrl(png->front(), png->size());
|
||||||
base::Base64Encode(data_url, &data_url);
|
} else {
|
||||||
data_url.insert(0, "data:image/png;base64,");
|
return webui::GetBitmapDataUrl(image_.AsBitmap());
|
||||||
return data_url;
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
v8::Local<v8::Value> NativeImage::GetBitmap(v8::Isolate* isolate) {
|
v8::Local<v8::Value> NativeImage::GetBitmap(v8::Isolate* isolate) {
|
||||||
|
@ -297,6 +298,10 @@ bool NativeImage::IsEmpty() {
|
||||||
return image_.IsEmpty();
|
return image_.IsEmpty();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
bool NativeImage::HasRepresentation(float scale_factor) {
|
||||||
|
return image_.AsImageSkia().HasRepresentation(scale_factor);
|
||||||
|
}
|
||||||
|
|
||||||
gfx::Size NativeImage::GetSize() {
|
gfx::Size NativeImage::GetSize() {
|
||||||
return image_.Size();
|
return image_.Size();
|
||||||
}
|
}
|
||||||
|
@ -468,6 +473,7 @@ void NativeImage::BuildPrototype(
|
||||||
.SetMethod("resize", &NativeImage::Resize)
|
.SetMethod("resize", &NativeImage::Resize)
|
||||||
.SetMethod("crop", &NativeImage::Crop)
|
.SetMethod("crop", &NativeImage::Crop)
|
||||||
.SetMethod("getAspectRatio", &NativeImage::GetAspectRatio)
|
.SetMethod("getAspectRatio", &NativeImage::GetAspectRatio)
|
||||||
|
.SetMethod("hasRepresentation", &NativeImage::HasRepresentation)
|
||||||
// TODO(kevinsawicki): Remove in 2.0, deprecate before then with warnings
|
// TODO(kevinsawicki): Remove in 2.0, deprecate before then with warnings
|
||||||
.SetMethod("toPng", &NativeImage::ToPNG)
|
.SetMethod("toPng", &NativeImage::ToPNG)
|
||||||
.SetMethod("toJpeg", &NativeImage::ToJPEG);
|
.SetMethod("toJpeg", &NativeImage::ToJPEG);
|
||||||
|
|
|
@ -83,6 +83,7 @@ class NativeImage : public mate::Wrappable<NativeImage> {
|
||||||
const gfx::Rect& rect);
|
const gfx::Rect& rect);
|
||||||
std::string ToDataURL();
|
std::string ToDataURL();
|
||||||
bool IsEmpty();
|
bool IsEmpty();
|
||||||
|
bool HasRepresentation(float scale_factor);
|
||||||
gfx::Size GetSize();
|
gfx::Size GetSize();
|
||||||
float GetAspectRatio();
|
float GetAspectRatio();
|
||||||
|
|
||||||
|
|
|
@ -9,6 +9,7 @@ describe('nativeImage module', () => {
|
||||||
it('returns an empty image', () => {
|
it('returns an empty image', () => {
|
||||||
const empty = nativeImage.createEmpty()
|
const empty = nativeImage.createEmpty()
|
||||||
assert.equal(empty.isEmpty(), true)
|
assert.equal(empty.isEmpty(), true)
|
||||||
|
assert.equal(empty.hasRepresentation(1.0), false)
|
||||||
assert.equal(empty.getAspectRatio(), 1)
|
assert.equal(empty.getAspectRatio(), 1)
|
||||||
assert.equal(empty.toDataURL(), 'data:image/png;base64,')
|
assert.equal(empty.toDataURL(), 'data:image/png;base64,')
|
||||||
assert.deepEqual(empty.getSize(), {width: 0, height: 0})
|
assert.deepEqual(empty.getSize(), {width: 0, height: 0})
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue