Problems writing TIFF files with ImageIO

Viewed 1525

I'm using Java 11, attempting to write compressed TIFF images with ImageIO. Most images write correctly, but some images throw an IIOException from within the javax.imageio code. My application is using Graphics 2D to resize images, then writing them to file. The Exception is:

javax.imageio.IIOException: I/O error writing TIFF file!

Here is the code snippet where I create the ImageWriter and write the image. The Exception is thrown from the last line in this snippet, tiffWriter.write().

//Create the TiffWriter, tell it where to write
ImageWriter tiffWriter = ImageIO.getImageWritersByFormatName("tiff").next();
tiffWriter.setOutput(imageOut);

//Wrap up the BufferedImage and Metadata in the IIOImage
IIOImage iioImg = new IIOImage(bi, null, meta);

//Set some writing parameters, so we get the G4 compression
ImageWriteParam writeParam = tiffWriter.getDefaultWriteParam();
writeParam.setCompressionMode(ImageWriteParam.MODE_EXPLICIT);
writeParam.setCompressionType(COMPRESSION_TYPE_GROUP4FAX);

//Write the image
tiffWriter.write(null, iioImg, writeParam);

The relevant stack trace entries are:

File=TIFFImageWriter.java,Class=com.sun.imageio.plugins.tiff.TIFFImageWriter,Method=write,Line=2612 File=TIFFImageWriter.java,Class=com.sun.imageio.plugins.tiff.TIFFImageWriter,Method=write,Line=2315

There is no further information given. So I'm not really sure what steps to take next. I know that I have write permission in the destination location. I can't seem to find any pattern with the images that fail the write operation. It seems like a roughly 20% failure rate in my sample.

Has anyone else experienced this error when writing TIFF files?

EDIT: Just to clarify, I am setting the compression type to "CCITT T.6". The compression definitely has something to do with this error. If I omit the lines:

writeParam.setCompressionMode(ImageWriteParam.MODE_EXPLICIT);
writeParam.setCompressionType(COMPRESSION_TYPE_GROUP4FAX);

Then the IIOException is not thrown! In my batch of test images, the same images fail no matter what the compression type is. But if there is no compression, all images are written successfully. So I guess the question is, why does writing a compressed image fail here?

0 Answers
Related