0

I'm facing an I/O bug when using large images as a TIFF file using javax.imageioand JAI.

Here is what I did:

Generate a random BufferedImage of type TYPE_CUSTOM with DataBufferInt encoding
Save the image as a TIFF file using javax.imageio or JAI
Read the image using javax.imageio or JAI
Compare the original image with the image saved/read

It turns out that everything works fine as long as the images dimensions are smaller than 31Kx31K ish, but if they are bigger then the top part of the image becomes different.

I looked online and I found this bug report. If I read it correctly, it should have been fixed since java 11.0.13, see this bug report. This bug appeared while I was using OpenJDF 11.0.2, so I tried using openjdk 11.0.14.1 2022-02-08 and openjdk 17.0.2 2022-01-18, but I still have the same issue. I don't have an exception as this bug report, so it's close but might not be related.

So my questions are:

  • What am I not understanding correctly?
  • Is there another better/safer way to handle TIFF images?
FiReTiTi
  • 5,597
  • 12
  • 30
  • 58
  • 1
    31k * 31k * 4 (integer size) ~= 3844000000. This is close to the TIFF maximum file size of 4GB. My guess is that the file eventually grows larger than 4 GB with increased dimensions and the extra metadata and the TIFF container. If this is the problem, then you need to use a different file format. BigTIFF or PSD are the most common options I think. – Harald K Apr 25 '22 at 08:08
  • Thank you @HaraldK. Do you happen to know which library is best for BigTIFF in Java? – FiReTiTi Apr 25 '22 at 17:18
  • Side note: The above calculations are for uncompressed data. If your images are compressible and you use a good compression, you should be able to have larger images in TIFF, as long as the *file* size is less than 4 GB. – Harald K Apr 25 '22 at 17:29
  • I'm a little biased, so I'd use [TwelveMonkeys ImageIO](https://github.com/haraldk/TwelveMonkeys) obviously. There might be other/better options too, but I have never used them, so I don't know... – Harald K Apr 25 '22 at 17:37
  • @HaraldK my images are uncompressed, so your computation works. I also used to use TwelveMonkeys, I just temporarily removed it when I switched to IntelliJ IDE as I face an issue that I didn't have time to investigate. Is the BigTIFF format automatically used when TwelveMonkeys is installed or do I have to specify something or call a specific command? – FiReTiTi Apr 25 '22 at 20:15
  • You need the TIFF plugin as run-time dependency. I think your existing ImageIO code should work (assuming it’s generic), just change the format to “BigTIFF”. There’s no automatic conversion to BigTIFF when the file grows too large. For reading the BigTIFF format will be recognized automatically. – Harald K Apr 25 '22 at 20:23
  • Thank you, but the BigTIFF format does not seem to work. I used `javax.imageio.ImageIO.write(myBufferedImage, "BigTIFF", new File("Test.tiff"))` and it returns false. I double checked and in my IntelliJ project under "File Structures/Modules/Dependencies/" I can find image-tiff-3.8.1.jar What am I doing wrong? How can I check that the proper tiff jar/class was exported in my project jar? – FiReTiTi Apr 26 '22 at 08:02
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/244213/discussion-between-harald-k-and-firetiti). – Harald K Apr 26 '22 at 08:05

0 Answers0