Mark Stephens

Mark Stephens has been working with Java and PDF since 1999 and has diversified into HTML5, SVG and JavaFX.

He also enjoys speaking at conferences and has been a Speaker at user groups, Business of Software, Seybold and JavaOne conferences. He has a very dry sense of humor and an MA in Medieval History for which he has not yet found a practical use.

Saving Java images as Tifs wth JAI

1 min read

Java makes it very easy to create images as BufferedImages which  can then be saved out in standard image file formats. Here is the code to save an image as a Tif image using the JAI image (a free library from Sun).

com.sun.media.jai.codec.TIFFEncodeParam params = new com.sun.media.jai.codec.TIFFEncodeParam();
FileOutputStream os = new FileOutputStream(outputDir + imageName+”.tif”);

javax.media.jai.JAI.create(“encode”, image, os, “TIFF”, params);

This works very nicely but there are a number of extra tricks worth knowing. 

Firstly, there is a compression option available to compress the image – use the modified code as shown below. There are several types of compression but several of them produce Tif files which will not display under Windows or Mac – COMPRESSION_PACKBITS works well.

 com.sun.media.jai.codec.TIFFEncodeParam params = new com.sun.media.jai.codec.TIFFEncodeParam();

params.setCompression(com.sun.media.jai.codec.TIFFEncodeParam.COMPRESSION_PACKBITS);
 

Secondly, the type of image you save out can have a huge effect on the size of the Tif image. If you save a grayscale image, it produces a much smaller file and also compresses much better than an RGB or ARGB image. You can find out the image type by using image.getType() – the int values returned are all static Constants in the BufferedImage class.

You can convert the image to another format by creating a second BufferedImage in that format and drawing the original image onto it. Here is the code to make any image grayscale.

BufferedImage image_to_save2=new BufferedImage(image_to_save.getWidth(),image_to_save.getHeight(), BufferedImage.TYPE_BYTE_GRAY);
                                image_to_save2.getGraphics().drawImage(image_to_save,0,0,null);
image_to_save = image_to_save2; 

For our PDF library, we always generate images in ARGB (we need to because PDF files can have transparency which only works in ARGB). But sometimes the image is only grayscale. Using both these tricks of converting to grayscale and compressing allowed us to reduce the size of the Tif files created from a sample PDF file from 1.4 Meg to 52K – pretty impressive!

Mark Stephens

Mark Stephens has been working with Java and PDF since 1999 and has diversified into HTML5, SVG and JavaFX.

He also enjoys speaking at conferences and has been a Speaker at user groups, Business of Software, Seybold and JavaOne conferences. He has a very dry sense of humor and an MA in Medieval History for which he has not yet found a practical use.

Leave a Reply

Your email address will not be published. Required fields are marked *