From 7e039d92ec4fb02804f4fc3afed8c1c80e99ab9f Mon Sep 17 00:00:00 2001 From: Kevin Sawicki Date: Tue, 7 Mar 2017 14:24:37 -0800 Subject: [PATCH] Support adding representation from data URL --- atom/common/api/atom_api_native_image.cc | 27 +++++++++++++++++++----- spec/api-native-image-spec.js | 27 +++++++++++++++++++++++- 2 files changed, 48 insertions(+), 6 deletions(-) diff --git a/atom/common/api/atom_api_native_image.cc b/atom/common/api/atom_api_native_image.cc index e30b407e9ef..6811ab91930 100644 --- a/atom/common/api/atom_api_native_image.cc +++ b/atom/common/api/atom_api_native_image.cc @@ -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 buffer; + GURL url; if (options.Get("buffer", &buffer) && node::Buffer::HasInstance(buffer)) { - gfx::ImageSkia image_skia = image_.AsImageSkia(); AddImageSkiaRep( &image_skia, reinterpret_cast(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(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) diff --git a/spec/api-native-image-spec.js b/spec/api-native-image-spec.js index d9fb31a81ec..af90332e5ac 100644 --- a/spec/api-native-image-spec.js +++ b/spec/api-native-image-spec.js @@ -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') + }) }) })