Skip to content

Commit 91b51cb

Browse files
committed
[Win32] Extract alpha values from icons if available #715 #1130
Under certain conditions, program icons loaded on Windows via GDI+ have empty mask data, even though the original icon has proper mask data. As a result, these icons are printed with a black instead of a transparent background. Still these icons can contain valid alpha data in their usual 32-bit data. With this change, alpha data is extracted for icons which are loaded without proper mask data to ensure that they have proper transparency information. Fixes #715 Fixes #1130
1 parent af9f10c commit 91b51cb

File tree

1 file changed

+19
-0
lines changed
  • bundles/org.eclipse.swt/Eclipse SWT/win32/org/eclipse/swt/graphics

1 file changed

+19
-0
lines changed

bundles/org.eclipse.swt/Eclipse SWT/win32/org/eclipse/swt/graphics/Image.java

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1430,6 +1430,7 @@ public ImageData getImageDataAtCurrentZoom() {
14301430

14311431
/* Do the mask */
14321432
byte [] maskData = null;
1433+
byte [] alphaData = null;
14331434
if (info.hbmColor == 0) {
14341435
/* Do the bottom half of the mask */
14351436
maskData = new byte[imageSize];
@@ -1459,7 +1460,9 @@ public ImageData getImageDataAtCurrentZoom() {
14591460
maskData = new byte[imageSize];
14601461
OS.GetDIBits(hBitmapDC, info.hbmMask, 0, height, maskData, bmi, OS.DIB_RGB_COLORS);
14611462
/* Loop to invert the mask */
1463+
boolean hasMaskData = false;
14621464
for (int i = 0; i < maskData.length; i++) {
1465+
hasMaskData |= maskData[i] != 0;
14631466
maskData[i] ^= -1;
14641467
}
14651468
/* Make sure mask scanlinePad is 2 */
@@ -1470,6 +1473,21 @@ public ImageData getImageDataAtCurrentZoom() {
14701473
if (calcBpl == bpl) break;
14711474
}
14721475
maskData = ImageData.convertPad(maskData, width, height, 1, maskPad, 2);
1476+
// For missing mask data, see https://github.com/eclipse-platform/eclipse.platform.swt/issues/715
1477+
if (!hasMaskData && depth == 32) {
1478+
alphaData = new byte[width * height];
1479+
boolean hasAlphaData = false;
1480+
for (int pixelIndex = 0; pixelIndex < alphaData.length; pixelIndex++) {
1481+
alphaData[pixelIndex] = data[pixelIndex * 4 + 3];
1482+
hasAlphaData |= alphaData[pixelIndex] != -1;
1483+
}
1484+
// In case there is alpha data, replace the empty mask data with proper alpha data
1485+
if (hasAlphaData) {
1486+
maskData = null;
1487+
} else {
1488+
alphaData = null;
1489+
}
1490+
}
14731491
}
14741492
/* Clean up */
14751493
OS.SelectObject(hBitmapDC, hOldBitmap);
@@ -1482,6 +1500,7 @@ public ImageData getImageDataAtCurrentZoom() {
14821500
if (info.hbmMask != 0) OS.DeleteObject(info.hbmMask);
14831501
/* Construct and return the ImageData */
14841502
ImageData imageData = new ImageData(width, height, depth, palette, 4, data);
1503+
imageData.alphaData = alphaData;
14851504
imageData.maskData = maskData;
14861505
imageData.maskPad = 2;
14871506
return imageData;

0 commit comments

Comments
 (0)