Skip to content

Re-evaluate/Understand Image#setBackground #2982

@ShahzaibIbrahim

Description

@ShahzaibIbrahim

Describe the bug
Currently setBackground documentation states the following: Sets the color to which to map the transparent pixel. and this method has no effect if the receiver does not have a transparent pixel value. So I tried applying the background to an image with transparent pixel in the following snippet.

To Reproduce

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

public class PixelTransparencyDemo {
    public static void main(String[] args) {
        Display display = new Display();
        Shell shell = new Shell(display);
        shell.setSize(800, 220);

        // 1) Palette: 0=Red, 1=Green, 2=Black (transparent)
        RGB[] rgbs = {
            new RGB(255, 0, 0),
            new RGB(0, 255, 0),
            new RGB(0, 0, 0)
        };
        PaletteData palette = new PaletteData(rgbs);

        // 2) Create 50x50 indexed image
        ImageData data = new ImageData(50, 50, 8, palette);

        // Fill GREEN
        for (int y = 0; y < 50; y++)
            for (int x = 0; x < 50; x++)
                data.setPixel(x, y, 1);

        // RED square
        for (int y = 10; y < 40; y++)
            for (int x = 10; x < 40; x++)
                data.setPixel(x, y, 0);

        // TRANSPARENT cross (palette index 2)
        for (int i = 0; i < 50; i++) {
            data.setPixel(i, 25, 2);
            data.setPixel(25, i, 2);
        }

        Image originalImage = new Image(display, data);

        // 3) Set transparent pixel + force mask creation
        data.transparentPixel = 2;

        // 4) Create SWT image
        Image imageWithBlackAsTransparentPixel = new Image(display, data);
        System.out.println("Image Type = " + imageWithBlackAsTransparentPixel.type);

        System.out.println("TransparencyType = " + data.getTransparencyType());

        // 5) Paint 2 versions
        shell.addListener(SWT.Paint, e -> {
            GC gc = e.gc;

            gc.drawText("Original Image:", 10, 10);
            gc.drawImage(originalImage, 10, 60);

            gc.drawText("Black As Transparent Pixel:", 200, 10);
            gc.drawImage(imageWithBlackAsTransparentPixel, 200, 60);

            imageWithBlackAsTransparentPixel.setBackground(display.getSystemColor(SWT.COLOR_BLUE));
            gc.drawText("After setBackground(BLUE):", 500, 10);
            gc.drawImage(imageWithBlackAsTransparentPixel, 500, 60);
        });

        shell.open();
        while (!shell.isDisposed())
            if (!display.readAndDispatch()) display.sleep();

        display.dispose();
    }
}

This snippet draws a 50x50 green square image and inside of it a 40x40 red square is drawn. Then two more lines are drawn in the middle horizontally and vertically as black. Then we set black color as transparent.

Upon drawing the first image is drawn as it was mentioned above. For the second image I tried setting the background for this image to blue. So instead of two transparent lines, I should see a blue lines which I don't. This implementation of setBackground is apparently broken. Tested this snippet on R_34 without any monitor specific scaling.

Image

Expected behavior
From my expectation the last image should have blue cross instead of transparent.

Screenshots
If applicable, add screenshots to help explain your problem.

Environment:

    • Windows
    • Linux
    • macOS

Version since
Checked on 4.34 or later, not sure about previous versions.

Workaround (or) Additional context
I am not sure how originally was it meant to be. But from the contract of the method, I would expect setBackground to fill the color on a transparentpixel.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions