[SHIMGVW] Image Viewer transparent image fix (#2165)
authorKatayama Hirofumi MZ <katayama.hirofumi.mz@gmail.com>
Mon, 23 Dec 2019 10:43:20 +0000 (19:43 +0900)
committerGitHub <noreply@github.com>
Mon, 23 Dec 2019 10:43:20 +0000 (19:43 +0900)
If the pixel is transparent or translucent, fill the background with a checkered pattern. CORE-15287

dll/win32/shimgvw/shimgvw.c

index 46553b3..7f2d039 100644 (file)
@@ -580,6 +580,26 @@ ImageView_UpdateWindow(HWND hwnd)
     UpdateWindow(hwnd);
 }
 
+static HBRUSH CreateCheckerBoardBrush(HDC hdc)
+{
+    static const CHAR pattern[] =
+        "\x28\x00\x00\x00\x10\x00\x00\x00\x10\x00\x00\x00\x01\x00\x04\x00\x00\x00"
+        "\x00\x00\x80\x00\x00\x00\x23\x2E\x00\x00\x23\x2E\x00\x00\x10\x00\x00\x00"
+        "\x00\x00\x00\x00\x99\x99\x99\x00\xCC\xCC\xCC\x00\x00\x00\x00\x00\x00\x00"
+        "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00"
+        "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00"
+        "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x11\x11\x11\x11"
+        "\x00\x00\x00\x00\x11\x11\x11\x11\x00\x00\x00\x00\x11\x11\x11\x11\x00\x00"
+        "\x00\x00\x11\x11\x11\x11\x00\x00\x00\x00\x11\x11\x11\x11\x00\x00\x00\x00"
+        "\x11\x11\x11\x11\x00\x00\x00\x00\x11\x11\x11\x11\x00\x00\x00\x00\x11\x11"
+        "\x11\x11\x00\x00\x00\x00\x00\x00\x00\x00\x11\x11\x11\x11\x00\x00\x00\x00"
+        "\x11\x11\x11\x11\x00\x00\x00\x00\x11\x11\x11\x11\x00\x00\x00\x00\x11\x11"
+        "\x11\x11\x00\x00\x00\x00\x11\x11\x11\x11\x00\x00\x00\x00\x11\x11\x11\x11"
+        "\x00\x00\x00\x00\x11\x11\x11\x11\x00\x00\x00\x00\x11\x11\x11\x11";
+
+    return CreateDIBPatternBrushPt(pattern, DIB_RGB_COLORS);
+}
+
 static VOID
 ImageView_DrawImage(HWND hwnd)
 {
@@ -591,6 +611,7 @@ ImageView_DrawImage(HWND hwnd)
     HDC hdc;
     HBRUSH white;
     HGDIOBJ hbrOld;
+    UINT uFlags;
 
     hdc = BeginPaint(hwnd, &ps);
     if (!hdc)
@@ -652,9 +673,24 @@ ImageView_DrawImage(HWND hwnd)
             GdipSetSmoothingMode(graphics, SmoothingModeHighQuality);
         }
 
-        hbrOld = SelectObject(hdc, GetStockObject(NULL_BRUSH));
-        Rectangle(hdc, x - 1, y - 1, x + ZoomedWidth + 1, y + ZoomedHeight + 1);
-        SelectObject(hdc, hbrOld);
+        uFlags = 0;
+        GdipGetImageFlags(image, &uFlags);
+
+        if (uFlags & (ImageFlagsHasAlpha | ImageFlagsHasTranslucent))
+        {
+            HBRUSH hbr = CreateCheckerBoardBrush(hdc);
+            hbrOld = SelectObject(hdc, hbr);
+            Rectangle(hdc, x - 1, y - 1, x + ZoomedWidth + 1, y + ZoomedHeight + 1);
+            SelectObject(hdc, hbrOld);
+            DeleteObject(hbr);
+        }
+        else
+        {
+            hbrOld = SelectObject(hdc, GetStockObject(NULL_BRUSH));
+            Rectangle(hdc, x - 1, y - 1, x + ZoomedWidth + 1, y + ZoomedHeight + 1);
+            SelectObject(hdc, hbrOld);
+        }
+
         GdipDrawImageRectI(graphics, image, x, y, ZoomedWidth, ZoomedHeight);
     }
     GdipDeleteGraphics(graphics);