On 4/16/25 3:04 AM, Glavo wrote:
* Different image APIs have to repeatedly implement support for
reading the same image format (such as JPEG).
In fact, AWT, JavaFX, and Android now each implement reading JPEG
images.
This is a waste.
I was initially frustrated by the split between Java (AWT) and JavaFX in
their image support and would have welcomed a common library, but I came
to appreciate the flexibility of having both available in a JavaFX
application. Below are some thoughts.
The challenge is not just in reading various external image formats.
There are three parts:
1. Efficiently loading and converting an image from an external format
(GIF, PNG, JPG).
2. Efficiently storing images internally with a minimal number of bits
per pixel.
3. Efficiently converting the internal format to that required by the
client library (AWT, JavaFX, Android).
The first item is determined by the external image format standards, but
the second and third items involve a trade-off. Optimizing for memory
usage might results in a huge waste of CPU time doing conversions, while
optimizing for zero conversion time might results in a huge waste of
RAM. A JavaFX application can pick and choose between this trade-off by
using both the AWT and JavaFX image libraries.
For example, I had to load and display hundreds of binary images (pure
black and white) on a constrained device. The AWT can store images in
memory as one bit per pixel, but JavaFX supports only 32-bit images. So
I needed to use the AWT image format in memory, but the JavaFX image
format for display, converting them on-the-fly one-by-one to 32 bits per
pixel.
JavaFX provides an AWT-to-JavaFX image conversion utility, called
SwingFXUtils::toFxImage
<https://github.com/openjdk/jfx/blob/master/modules/javafx.swing/src/main/java/javafx/embed/swing/SwingFXUtils.java>,
but I couldn't use it because it was far too slow. It's slow because
it's generic and has to work for all image types. I came up with 14
different ways to convert <https://github.com/jgneff/tofximage> an AWT
image to a JavaFX image: 9 that work for images with transparency and
another 5 that work only for images with no alpha. In my case, one such
conversion was much faster than the others
<https://jgneff.github.io/tofximage/2020-06/>.
A common library might end up too generic, too slow, or require too much
memory to be useful.
John