Skip to content

Losing transparency for Icon when creating an icon with source and mask #461

@ShahzaibIbrahim

Description

@ShahzaibIbrahim

Issue Description

When creating a custom cursor using source and mask ImageData, the transparency mask does not scale correctly when applying DPIUtil.autoScaleImageData() in a multi-monitor setup with different DPI settings.

Example

The following code creates a blue square cursor with a mask that alternates between transparent and opaque pixels:

PaletteData rgbPalette = new PaletteData(0xFF0000, 0x00FF00, 0x0000FF);
int bluePixel = rgbPalette.getPixel(new RGB(0, 0, 255));

ImageData source = new ImageData(IMAGE_SIZE_IN_POINTS, IMAGE_SIZE_IN_POINTS, 24, rgbPalette);
for (int x = 0; x < IMAGE_SIZE_IN_POINTS; x++) {
    for (int y = 0; y < IMAGE_SIZE_IN_POINTS; y++) {
        source.setPixel(x, y, bluePixel);
    }
}

ImageData mask = new ImageData(IMAGE_SIZE_IN_POINTS, IMAGE_SIZE_IN_POINTS, 1,
        new PaletteData(new RGB[] { new RGB(0, 0, 0), new RGB(255, 255, 255) }));
for (int x = 0; x < IMAGE_SIZE_IN_POINTS; x++) {
    for (int y = 0; y < IMAGE_SIZE_IN_POINTS; y++) {
        mask.setPixel(x, y, x % 2);
    }
}

// Scaled before creating cursor
source = DPIUtil.autoScaleImageData(source);
mask = DPIUtil.autoScaleImageData(mask);

return new Cursor(display, source, mask, IMAGE_SIZE_IN_POINTS / 2, IMAGE_SIZE_IN_POINTS / 2);
  • At 100% scaling: The expected alternating pattern is correct — one transparent pixel after each blue pixel.
    20250925-1408-36 4221048

  • After applying DPIUtil.autoScaleImageData() (e.g. 150%): The mask scaling is inconsistent. Instead of producing a clean 2×2 block pattern (two blue pixels followed by two transparent pixels), transparency is lost or merged, resulting in artifacts.
    Scaled

Problem

DPIUtil.autoScaleImageData() scales the source and mask independently, but the 1-bit mask does not scale in a way that preserves the intended transparency pattern. This causes visual artifacts in the resulting cursor.

Expected Behavior

When scaling, the transparency mask should scale consistently with the source image so that the expected ratio (e.g., 2×2 transparent/blue pattern) is preserved across different DPI settings.

Solution

Use scaleTo method, here the scaling won't be smooth but at least expected.

Metadata

Metadata

Assignees

No one assigned

    Labels

    HiDPIA HiDPI-Related Issue or FeatureSWTIssue for SWT

    Type

    No type

    Projects

    Status

    👀 In Review

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions