Shenal
Shenal

Reputation: 202

Mutipage TIFF creation with different compressions

I want to encode 2 jpeg images to a TIFF file with 3 pages. The specification is as follow.

  1. JPEG compressed with quality of 35 of image 1
  2. CCITT compressed image of image 1
  3. CCITT compressed image of image 2

I am able to generate separate tiff files for above 3 types. But when I try to combine them I have to give a single compression

param(`params.setCompression(TIFFEncodeParam.COMPRESSION_PACKBITS);`) 

which expands the size.

How do I set different compression to different pages?

Upvotes: 2

Views: 1915

Answers (2)

Harald K
Harald K

Reputation: 27064

Using the standard ImageIO API (with JAI ImageIO or other TIFF plugin), you should be able to do it like this:

public static void main(String[] args) throws IOException {
    List<BufferedImage> images = Arrays.asList(
            new BufferedImage(100, 100, BufferedImage.TYPE_INT_ARGB),
            new BufferedImage(100, 100, BufferedImage.TYPE_BYTE_BINARY),
            new BufferedImage(100, 100, BufferedImage.TYPE_BYTE_BINARY)
    );

    List<String> compression = Arrays.asList("JPEG", "CCITT T.4", "CCITT T.4");

    try (ImageOutputStream outputStream = ImageIO.createImageOutputStream(new File(args[0]))) {
        ImageWriter tiffWriter = ImageIO.getImageWritersByFormatName("TIFF").next(); // Assumes TIFF plugin installed
        tiffWriter.setOutput(outputStream);

        if (!images.isEmpty()) {
            tiffWriter.prepareWriteSequence(null); // Use default stream metadata

            for (int i = 0; i < images.size(); i++) {
                // Set up explicit compression for each image
                ImageWriteParam param = tiffWriter.getDefaultWriteParam();
                param.setCompressionMode(ImageWriteParam.MODE_EXPLICIT);
                String compressionType = compression.get(i);
                param.setCompressionType(compressionType);

                if ("JPEG".equals(compressionType)) {
                    param.setCompressionQuality(.35f);
                }

                tiffWriter.writeToSequence(new IIOImage(images.get(i), null, null), param); // Ignoring thumbnail and metadata for now
            }

            tiffWriter.endWriteSequence();
        }
    }
}

The above code is tested using the TwelveMonkeys TIFFImageWriter, but it should work equally well with the JAI ImageIO plugin.

Upvotes: 2

Shenal
Shenal

Reputation: 202

After trying with JAI. I switched to icafe

    BufferedImage[] images = new BufferedImage[3];
    images[0]=compressedFront;
    images[1]=frontBinaryImage;
    images[2]=backBinaryImage;


    ImageParam.ImageParamBuilder builder = ImageParam.getBuilder();

    TIFFOptions tiffOptions = new TIFFOptions();
    tiffOptions.setTiffCompression(Compression.JPG);
    tiffOptions.setJPEGQuality(35);


    ImageParam[] param = new ImageParam[3];
    param[0] =  builder.colorType(ImageColorType.GRAY_SCALE).imageOptions(tiffOptions).build();

    tiffOptions = new TIFFOptions(tiffOptions); // Copy constructor
    tiffOptions.setTiffCompression(Compression.CCITTFAX4);


    param[1] =  builder.colorType(ImageColorType.BILEVEL).imageOptions(tiffOptions).build();

    tiffOptions = new TIFFOptions(tiffOptions);
    tiffOptions.setTiffCompression(Compression.CCITTFAX4);

    param[2] = builder.colorType(ImageColorType.BILEVEL).imageOptions(tiffOptions).build();

    TIFFTweaker.writeMultipageTIFF(rout, param, images);

    rout.close();
    fout.close(); 

Upvotes: 1

Related Questions