Skip to content

Conversation

@ShahzaibIbrahim
Copy link
Contributor

I have a png image (icon), which has no maskData and no alpha pixels (that means it is fully opaque):
image

On Windows, GetDIBits() may return undefined alpha bytes for 32-bit BI_RGB bitmaps, commonly resulting in all-zero alpha even for fully opaque images.

When icon mask data is missing, this caused opaque icons to be incorrectly treated as fully transparent.

Reject all-zero alpha data and only replace empty mask data when the alpha channel contains meaningful (non-opaque, non-zero) values.

How to test

  • Run the following snippet on 100% (only reproducible on 100%, otherwise image will be scaled, image semantics will change and we will never run into this issue):

Note: Add the attached image on the same folder as snippet

import org.eclipse.swt.*;
import org.eclipse.swt.graphics.*;
import org.eclipse.swt.layout.*;
import org.eclipse.swt.widgets.*;

public class ImageTransparencyFix {
    public static void main(String[] args) {
    	System.setProperty("swt.autoScale.updateOnRuntime", "true");
        Display display = new Display();
        Shell shell = new Shell(display);
        shell.setText("Image Transparency Toolbar Demo");
        shell.setSize(300, 200);
        shell.setLayout(new FillLayout());

        Image original = new Image(display, ImageTransparencyFix.class.getResourceAsStream("png-image.png"));

        ImageData data = original.getImageData();
        Image transparentImage = new Image(display, data, data.getTransparencyMask());

        ToolBar imageToolBar = new ToolBar(shell, SWT.FLAT);
        for (int i = 0; i < 3; i++) {
            ToolItem item = new ToolItem(imageToolBar, SWT.PUSH);
            item.setImage(transparentImage);
            item.setToolTipText("Image ToolItem ToolTip " + i);
        }

        // 4) Open UI
        shell.open();
        while (!shell.isDisposed()) {
            if (!display.readAndDispatch()) display.sleep();
        }

        // 5) Dispose resources
        transparentImage.dispose();
        original.dispose();
        display.dispose();
    }
}
  • You will see the images on toolbar will be completely transparent.
image

After fix

image

On Windows, GetDIBits() may return undefined alpha bytes for 32-bit
BI_RGB bitmaps, commonly resulting in all-zero alpha even for fully
opaque images.

When icon mask data is missing, this caused opaque icons to be
incorrectly treated as fully transparent.

Reject all-zero alpha data and only replace empty mask data when the
alpha channel contains meaningful (non-opaque, non-zero) values.
@ShahzaibIbrahim ShahzaibIbrahim marked this pull request as draft January 20, 2026 10:55
@ShahzaibIbrahim
Copy link
Contributor Author

ShahzaibIbrahim commented Jan 20, 2026

Drafted because I need suggestion here in the case where, what if the image was made completely transparent by choice? Meaning user define the image to be completely transparent (all 0 alpha pixels). Is this a viable/possible case here? Or that this fix is enough? as we can't know for sure if GetDIBits() is returning 0 as un-defined alpha-byte or was it really 0.

Documentation for GetDIBits -> https://learn.microsoft.com/en-us/windows/win32/api/wingdi/nf-wingdi-getdibits#return-value

@github-actions
Copy link
Contributor

Test Results (win32)

   34 files  ±0     34 suites  ±0   4m 39s ⏱️ +2s
4 636 tests ±0  4 563 ✅ ±0  73 💤 ±0  0 ❌ ±0 
  170 runs  ±0    167 ✅ ±0   3 💤 ±0  0 ❌ ±0 

Results for commit c663e76. ± Comparison against base commit 97a79df.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Discuss Images with no image transparency

1 participant