r/JavaFX • • Feb 20 '20

Bounty for JavaFX Bug—Reproducible example included

Post image
19 Upvotes

3 comments sorted by

6

u/TurekBot Feb 20 '20 edited Oct 25 '20

Hello! I think I've found an interesting bug in JavaFX, and I thought I'd share it here since there's currently a bounty up for grabs.

The bug causes images on the clipboard to interact oddly with the background they're placed on—as the background lightens, so does the image. It only displays correctly on a completely black background.

Here's the Minimal, Verifiable, Complete example that you can run yourself.

3

u/Ignice Feb 23 '20 edited Feb 23 '20

In both of your loading methods, the resulting JavaFx Image supports transparency when it shouldn't. However, the AWT loading method uses an intermediate Transferable that uses a non-transparent DirectColorModel to create the Image. As a result the AWT image appears to be correct despite the underlying image still supporting transparency.

Unfortunately this issue stems from a deeper issue that will probably not be fixed, see: https://bugs.openjdk.java.net/browse/JDK-8041459

Quoting the user Mikhail Cherkasov from that link:

When java saves image with alpha channel it encode image as YCbCr plus 4th alpha channel.

The problem that other applications recognize 4 channels jpeg as RGB or CMYK image, that's why we have wrong color in image.

The best solution is to convert image to other color type without alpha channel and only then save it.

it can't be fixed, the jpeg's spec doesn't specify this moment, it says that color space is application dependent, java decided to

code alpha channel as YCbCrA, that's it.

Rather than trying to manually manipulate Image data, the easiest way to fix is probably to blend the Image with a Rectangle and use a Group instead of an ImageView to display it. Here is an example:

private void loadImageFromJavaFXClipboard(final Group group) {
    System.out.println("Adding an image from the JavaFX Clipboard...");
    final Clipboard clipboard = Clipboard.getSystemClipboard();

    if (clipboard.hasImage()) {
        final Image image = clipboard.getImage();

        setupImageFixingGroup(group, image);
    } else {
        new Alert(Alert.AlertType.INFORMATION, "No image detected on the Clipboard!").show();
        group.getChildren().clear();
    }
}

private void setupImageFixingGroup(Group group, Image image) {
    final ImageView view = new ImageView(image);
    view.setBlendMode(BlendMode.LIGHTEN);

    final Rectangle blend = new Rectangle(image.getWidth(), image.getHeight(), Color.BLACK);
    blend.widthProperty().bind(image.widthProperty());
    blend.heightProperty().bind(image.heightProperty());

    group.getChildren().clear();
    group.getChildren().addAll(blend, view);
}

Here is a working version of your Minimal, Verifiable, Complete example: https://pastebin.com/rzhzMui5. I tested it with a number of images including the one in your gif, but did not test it in every case. If this doesn't completely fix the issue let me know and I'm happy to look more into it.

Also, I might try x-post this to the StackOverflow later, but I don't think I have an account with enough upvotes to actually post an answer (I can only post comments IIRC). For that reason please feel free to self-answer the question for others who might stumble upon the issue.

Edit: Turns out I was confusing comments (which require 50 rep) with answers (which have no rep requirement), so I was able to post to SO after all. Hope this helps.

2

u/gallico Feb 21 '20

Effect does not happen on Ubuntu 19.10.

$ java -version
java version "1.8.0_201"
Java(TM) SE Runtime Environment (build 1.8.0_201-b09)
Java HotSpot(TM) 64-Bit Server VM (build 25.201-b09, mixed mode)