diff --git a/atom/common/api/atom_api_native_image.cc b/atom/common/api/atom_api_native_image.cc index 469a76fd7f3..3939a0afe56 100644 --- a/atom/common/api/atom_api_native_image.cc +++ b/atom/common/api/atom_api_native_image.cc @@ -17,6 +17,8 @@ #include "base/strings/string_util.h" #include "native_mate/object_template_builder.h" #include "net/base/data_url.h" +#include "third_party/skia/include/core/SkBitmap.h" +#include "third_party/skia/include/core/SkImageInfo.h" #include "third_party/skia/include/core/SkPixelRef.h" #include "ui/base/layout.h" #include "ui/base/webui/web_ui_util.h" @@ -93,9 +95,20 @@ bool AddImageSkiaRep(gfx::ImageSkia* image, std::unique_ptr decoded(new SkBitmap()); // Try PNG first. - if (!gfx::PNGCodec::Decode(data, size, decoded.get())) + if (!gfx::PNGCodec::Decode(data, size, decoded.get())) { // Try JPEG. decoded = gfx::JPEGCodec::Decode(data, size); + if (decoded) { + // `JPEGCodec::Decode()` doesn't tell `SkBitmap` instance it creates + // that all of its pixels are opaque, that's why the bitmap gets + // an alpha type `kPremul_SkAlphaType` instead of `kOpaque_SkAlphaType`. + // Let's fix it here. + // TODO(alexeykuzmin): This workaround should be removed + // when the `JPEGCodec::Decode()` code is fixed. + // See https://github.com/electron/electron/issues/11294. + decoded->setAlphaType(SkAlphaType::kOpaque_SkAlphaType); + } + } if (!decoded) { // Try Bitmap diff --git a/spec/api-native-image-spec.js b/spec/api-native-image-spec.js index 2e066190306..1610b51de80 100644 --- a/spec/api-native-image-spec.js +++ b/spec/api-native-image-spec.js @@ -29,7 +29,7 @@ describe('nativeImage module', () => { width: 1 }, { - dataUrl: 'data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAIAAAACCAYAAABytg0kAAAAFUlEQVQYlWP8////fwYGBgYmBigAAD34BABBrq9BAAAAAElFTkSuQmCC', + dataUrl: 'data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAIAAAACCAIAAAD91JpzAAAAFklEQVQYlWP8//8/AwMDEwMDAwMDAwAkBgMBBMzldwAAAABJRU5ErkJggg==', filename: '2x2.jpg', format: ImageFormat.JPEG, hasAlphaChannel: false,