Reading and Writing out TIFF image in Java

The easiest way to read in a TIFF and output a BMP would be to use the ImageIO class:

BufferedImage image = ImageIO.read(inputFile);
ImageIO.write(image, "bmp", new File(outputFile));

The only additional thing you would need to do to get this to work is make sure you’ve added the JAI ImageIO JARs to your classpath, since BMP and TIFF are not handled by the JRE without the plugins from this library.

If you can’t use JAI ImageIO for some reason, you can get it to work with your existing code but you’ll have to do some additional work. The color model that is being created for the TIFF that you are loading is probably an indexed color model which is not supported by a BMP. You can replace it with the JAI.create(“format”,…) operation by providing a rendering hint with a key of JAI.KEY_REPLACE_INDEX_COLOR_MODEL.

You may have some luck writing the image read from file into a temporary image and then writing out the temp image:

BufferedImage image = ImageIO.read(inputFile);
BufferedImage convertedImage = new BufferedImage(image.getWidth(), 
    image.getHeight(), BufferedImage.TYPE_INT_RGB);
convertedImage.createGraphics().drawRenderedImage(image, null);
ImageIO.write(convertedImage, "bmp", new File(outputFile));

I’m wondering if you’re running into the same index color model issue as with the regular JAI. Ideally you should be using the ImageIO class to get ImageReader and ImageWriter instances for all but the simplest cases so that you can tweak the read and write params accordingly, but the ImageIO.read() and .write() can be finessed to give you what you want.

Leave a Comment