Skip to content

Commit

Permalink
[Win32] Extract alpha values from icons if available eclipse-platform…
Browse files Browse the repository at this point in the history
…#715 eclipse-platform#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 eclipse-platform#715
Fixes
eclipse-platform#1130
  • Loading branch information
HeikoKlare committed Apr 3, 2024
1 parent af9f10c commit 91b51cb
Showing 1 changed file with 19 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -1430,6 +1430,7 @@ public ImageData getImageDataAtCurrentZoom() {

/* Do the mask */
byte [] maskData = null;
byte [] alphaData = null;
if (info.hbmColor == 0) {
/* Do the bottom half of the mask */
maskData = new byte[imageSize];
Expand Down Expand Up @@ -1459,7 +1460,9 @@ public ImageData getImageDataAtCurrentZoom() {
maskData = new byte[imageSize];
OS.GetDIBits(hBitmapDC, info.hbmMask, 0, height, maskData, bmi, OS.DIB_RGB_COLORS);
/* Loop to invert the mask */
boolean hasMaskData = false;
for (int i = 0; i < maskData.length; i++) {
hasMaskData |= maskData[i] != 0;
maskData[i] ^= -1;
}
/* Make sure mask scanlinePad is 2 */
Expand All @@ -1470,6 +1473,21 @@ public ImageData getImageDataAtCurrentZoom() {
if (calcBpl == bpl) break;
}
maskData = ImageData.convertPad(maskData, width, height, 1, maskPad, 2);
// For missing mask data, see https://github.com/eclipse-platform/eclipse.platform.swt/issues/715
if (!hasMaskData && depth == 32) {
alphaData = new byte[width * height];
boolean hasAlphaData = false;
for (int pixelIndex = 0; pixelIndex < alphaData.length; pixelIndex++) {
alphaData[pixelIndex] = data[pixelIndex * 4 + 3];
hasAlphaData |= alphaData[pixelIndex] != -1;
}
// In case there is alpha data, replace the empty mask data with proper alpha data
if (hasAlphaData) {
maskData = null;
} else {
alphaData = null;
}
}
}
/* Clean up */
OS.SelectObject(hBitmapDC, hOldBitmap);
Expand All @@ -1482,6 +1500,7 @@ public ImageData getImageDataAtCurrentZoom() {
if (info.hbmMask != 0) OS.DeleteObject(info.hbmMask);
/* Construct and return the ImageData */
ImageData imageData = new ImageData(width, height, depth, palette, 4, data);
imageData.alphaData = alphaData;
imageData.maskData = maskData;
imageData.maskPad = 2;
return imageData;
Expand Down

0 comments on commit 91b51cb

Please sign in to comment.