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,20 +394,37 @@ void NativeImage::AddRepresentation(const mate::Dictionary& options) {
|
|||
options.Get("height", &height);
|
||||
options.Get("scaleFactor", &scale_factor);
|
||||
|
||||
bool skia_rep_added = false;
|
||||
gfx::ImageSkia image_skia = image_.AsImageSkia();
|
||||
|
||||
v8::Local<v8::Value> buffer;
|
||||
GURL url;
|
||||
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);
|
||||
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;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Re-initialize image when first representation is added to an empty image
|
||||
if (skia_rep_added && IsEmpty()) {
|
||||
gfx::Image image(image_skia);
|
||||
image_.SwapRepresentations(&image);
|
||||
}
|
||||
}
|
||||
|
||||
#if !defined(OS_MACOSX)
|
||||
|
|
|
@ -251,7 +251,7 @@ describe('nativeImage module', () => {
|
|||
})
|
||||
|
||||
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()
|
||||
image.addRepresentation({
|
||||
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: 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…
Reference in a new issue