Support adding representation from data URL
This commit is contained in:
parent
d67863aa9f
commit
7e039d92ec
2 changed files with 48 additions and 6 deletions
|
@ -394,21 +394,38 @@ void NativeImage::AddRepresentation(const mate::Dictionary& options) {
|
||||||
options.Get("height", &height);
|
options.Get("height", &height);
|
||||||
options.Get("scaleFactor", &scale_factor);
|
options.Get("scaleFactor", &scale_factor);
|
||||||
|
|
||||||
v8::Local<v8::Value> buffer;
|
bool skia_rep_added = false;
|
||||||
if (options.Get("buffer", &buffer) && node::Buffer::HasInstance(buffer)) {
|
|
||||||
gfx::ImageSkia image_skia = image_.AsImageSkia();
|
gfx::ImageSkia image_skia = image_.AsImageSkia();
|
||||||
|
|
||||||
|
v8::Local<v8::Value> buffer;
|
||||||
|
GURL url;
|
||||||
|
if (options.Get("buffer", &buffer) && node::Buffer::HasInstance(buffer)) {
|
||||||
AddImageSkiaRep(
|
AddImageSkiaRep(
|
||||||
&image_skia,
|
&image_skia,
|
||||||
reinterpret_cast<unsigned char*>(node::Buffer::Data(buffer)),
|
reinterpret_cast<unsigned char*>(node::Buffer::Data(buffer)),
|
||||||
node::Buffer::Length(buffer),
|
node::Buffer::Length(buffer),
|
||||||
width, height, scale_factor);
|
width, height, scale_factor);
|
||||||
|
skia_rep_added = true;
|
||||||
|
} else if (options.Get("dataURL", &url)) {
|
||||||
|
std::string mime_type, charset, data;
|
||||||
|
if (net::DataURL::Parse(url, &mime_type, &charset, &data)) {
|
||||||
|
if (mime_type == "image/png" || mime_type == "image/jpeg") {
|
||||||
|
AddImageSkiaRep(
|
||||||
|
&image_skia,
|
||||||
|
reinterpret_cast<const unsigned char*>(data.c_str()),
|
||||||
|
data.size(),
|
||||||
|
width, height, scale_factor);
|
||||||
|
skia_rep_added = true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
if (IsEmpty()) {
|
// Re-initialize image when first representation is added to an empty image
|
||||||
|
if (skia_rep_added && IsEmpty()) {
|
||||||
gfx::Image image(image_skia);
|
gfx::Image image(image_skia);
|
||||||
image_.SwapRepresentations(&image);
|
image_.SwapRepresentations(&image);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
#if !defined(OS_MACOSX)
|
#if !defined(OS_MACOSX)
|
||||||
void NativeImage::SetTemplateImage(bool setAsTemplate) {
|
void NativeImage::SetTemplateImage(bool setAsTemplate) {
|
||||||
|
|
|
@ -251,7 +251,7 @@ describe('nativeImage module', () => {
|
||||||
})
|
})
|
||||||
|
|
||||||
describe('addRepresentation()', () => {
|
describe('addRepresentation()', () => {
|
||||||
it('supports adding a representation for a scale factor', () => {
|
it('supports adding a buffer representation for a scale factor', () => {
|
||||||
const image = nativeImage.createEmpty()
|
const image = nativeImage.createEmpty()
|
||||||
image.addRepresentation({
|
image.addRepresentation({
|
||||||
scaleFactor: 1.0,
|
scaleFactor: 1.0,
|
||||||
|
@ -275,5 +275,30 @@ describe('nativeImage module', () => {
|
||||||
assert.equal(image.toDataURL({scaleFactor: 3.0}), 'data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAMAAAADCAYAAABWKLW/AAAADElEQVQYlWNgIAoAAAAnAAGZWEMnAAAAAElFTkSuQmCC')
|
assert.equal(image.toDataURL({scaleFactor: 3.0}), 'data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAMAAAADCAYAAABWKLW/AAAADElEQVQYlWNgIAoAAAAnAAGZWEMnAAAAAElFTkSuQmCC')
|
||||||
assert.equal(image.toDataURL({scaleFactor: 4.0}), 'data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAMAAAADCAYAAABWKLW/AAAADElEQVQYlWNgIAoAAAAnAAGZWEMnAAAAAElFTkSuQmCC')
|
assert.equal(image.toDataURL({scaleFactor: 4.0}), 'data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAMAAAADCAYAAABWKLW/AAAADElEQVQYlWNgIAoAAAAnAAGZWEMnAAAAAElFTkSuQmCC')
|
||||||
})
|
})
|
||||||
|
|
||||||
|
it('supports adding a data URL representation for a scale factor', () => {
|
||||||
|
const image = nativeImage.createEmpty()
|
||||||
|
image.addRepresentation({
|
||||||
|
scaleFactor: 1.0,
|
||||||
|
dataURL: nativeImage.createFromPath(path.join(__dirname, 'fixtures', 'assets', '1x1.png')).toDataURL()
|
||||||
|
})
|
||||||
|
image.addRepresentation({
|
||||||
|
scaleFactor: 2.0,
|
||||||
|
dataURL: nativeImage.createFromPath(path.join(__dirname, 'fixtures', 'assets', '2x2.jpg')).toDataURL()
|
||||||
|
})
|
||||||
|
image.addRepresentation({
|
||||||
|
scaleFactor: 3.0,
|
||||||
|
dataURL: nativeImage.createFromPath(path.join(__dirname, 'fixtures', 'assets', '3x3.png')).toDataURL()
|
||||||
|
})
|
||||||
|
image.addRepresentation({
|
||||||
|
scaleFactor: 4.0,
|
||||||
|
dataURL: 'invalid'
|
||||||
|
})
|
||||||
|
|
||||||
|
assert.equal(image.toDataURL({scaleFactor: 1.0}), 'data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAAC0lEQVQYlWNgAAIAAAUAAdafFs0AAAAASUVORK5CYII=')
|
||||||
|
assert.equal(image.toDataURL({scaleFactor: 2.0}), 'data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAIAAAACCAYAAABytg0kAAAAFUlEQVQYlWP8////fwYGBgYmBigAAD34BABBrq9BAAAAAElFTkSuQmCC')
|
||||||
|
assert.equal(image.toDataURL({scaleFactor: 3.0}), 'data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAMAAAADCAYAAABWKLW/AAAADElEQVQYlWNgIAoAAAAnAAGZWEMnAAAAAElFTkSuQmCC')
|
||||||
|
assert.equal(image.toDataURL({scaleFactor: 4.0}), 'data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAMAAAADCAYAAABWKLW/AAAADElEQVQYlWNgIAoAAAAnAAGZWEMnAAAAAElFTkSuQmCC')
|
||||||
|
})
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue