Site iconJava PDF Blog

Java images – 5 tips for Java ImageIO image performance

We have been doing a lot of work recently on images in Java and will be writing a series of articles on JPEG2000 support, access, bugs and other issues. First off, I want to take a look at performance.

When you are generating a PDF image a lot of time is spent in ImageIO, either decoding the DCTDecode image (which is a JPEG) and writing out the final image as a PNG. Here is what I have found out for trying to speed this up.

1. Doing the conversion in memory does not speed things up.

2. Using a BufferedOutputStream for ImageIO.write images seems to have a marginal improvement on some systems.

3. The ImageIO.write is optimised for Indexed Colorspaces so it is often quicker to convert the image to an Index Colorspace and then write it out. Here is some sample code.

BufferedImage indexedImage = new BufferedImage(image.getWidth(),
image.getHeight(), BufferedImage.TYPE_BYTE_INDEXED);
Graphics2D g = indexedImage.createGraphics();
g.drawImage(image, 0,0,null);
image=indexedImage;

4. JAI JPEG decoding seems to be much slower than ImageIO (certainly on my Mac).

5. ImageIO.setUseCache(true) is slower

Comparing the performance we get in Java compared to non-Java solutions also seems to be disappointing at the moment. So I hope it will be something Sun looks at for Java 8.

Do you have any tips for improving Java ImageIO performance?