On Wed, 19 Aug 2026 18:10:03 GMT, Andy Goryachev <[email protected]> wrote:

>> Any internal code that needs to read/write javafx images currently goes 
>> through the `ImageIO` (`java.desktop` module) and the `SwingFXUtils` (in 
>> `javafx.swing` module).
>> 
>> To avoid adding dependencies, we should move the bulk of implementation to 
>> the `javafx.graphics` module which in turn can be used by the `SwingFXUtils` 
>> and other internal code without adding additional explicit dependencies.
>> 
>> This PR moves the `SwingFXUtils` implementation to 
>> javafx.graphics/com.sun.javafx.util.ImageUtils and also adds a utility method
>> 
>> public static byte[] writeImage(Image im, String format) throws IOException
>> 
>> 
>> ---------
>> - [x] I confirm that I make this contribution in accordance with the 
>> [OpenJDK Interim AI Policy](https://openjdk.org/legal/ai).
>
> Andy Goryachev has updated the pull request incrementally with one additional 
> commit since the last revision:
> 
>   try with

modules/javafx.graphics/src/main/java/com/sun/javafx/util/ImageUtils.java line 
114:

> 112:         PixelWriter pw = wimg.getPixelWriter();
> 113:         DataBufferInt db = 
> (DataBufferInt)bimg.getRaster().getDataBuffer();
> 114:         int data[] = db.getData();

Again C-style array declaration

modules/javafx.graphics/src/main/java/com/sun/javafx/util/ImageUtils.java line 
163:

> 161:         }
> 162:         switch (fxFormat.getType()) {
> 163:             default:

Minor, also if you like: The `default` branch is the first. Looks very weird, 
should be the last instead

modules/javafx.graphics/src/main/java/com/sun/javafx/util/ImageUtils.java line 
204:

> 202:                 throw new InternalError("Failed to validate 
> BufferedImage type");
> 203:         }
> 204:     }

This could be a switch expression:


    private static WritablePixelFormat<IntBuffer> 
getAssociatedPixelFormat(BufferedImage bimg) {
        return switch (bimg.getType()) {
            // We lie here for xRGB, but we vetted that the src data was opaque
            // so we can ignore the alpha.  We use ArgbPre instead of Argb
            // just to get a loop that does not have divides in it if the
            // PixelReader happens to not know the data is opaque.
            case BufferedImage.TYPE_INT_RGB, BufferedImage.TYPE_INT_ARGB_PRE -> 
PixelFormat.getIntArgbPreInstance();
            case BufferedImage.TYPE_INT_ARGB -> 
PixelFormat.getIntArgbInstance();
            default -> throw new InternalError("Failed to validate 
BufferedImage type");
        };
    }

modules/javafx.graphics/src/main/java/com/sun/javafx/util/ImageUtils.java line 
292:

> 290:         }
> 291:         DataBufferInt db = 
> (DataBufferInt)bimg.getRaster().getDataBuffer();
> 292:         int data[] = db.getData();

minor, only if you like: While here, we could use the correct Java-style array 
declaration `int[] data` instead of the C-style declaration

modules/javafx.graphics/src/main/java/com/sun/javafx/util/ImageUtils.java line 
316:

> 314:      * @throws IOException
> 315:      */
> 316:     public static byte[] writeImage(Image im, String format) throws 
> IOException {

I wonder if there is a way to write bytes from a JavaFX `Image` without using 
the Swing `BufferedImage`

-------------

PR Review Comment: https://git.openjdk.org/jfx/pull/2267#discussion_r3822099682
PR Review Comment: https://git.openjdk.org/jfx/pull/2267#discussion_r3822096691
PR Review Comment: https://git.openjdk.org/jfx/pull/2267#discussion_r3822091753
PR Review Comment: https://git.openjdk.org/jfx/pull/2267#discussion_r3822081035
PR Review Comment: https://git.openjdk.org/jfx/pull/2267#discussion_r3822060677

Reply via email to