Browse Source

Explicitly set alpha type of bitmaps with JPEG data to opaque

Actually fixes #11245.
Aleksei Kuzmin 7 years ago
parent
commit
b9ed7380aa
2 changed files with 15 additions and 2 deletions
  1. 14 1
      atom/common/api/atom_api_native_image.cc
  2. 1 1
      spec/api-native-image-spec.js

+ 14 - 1
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<SkBitmap> 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

+ 1 - 1
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,