2

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?

dragon66
  • 2,645
  • 2
  • 21
  • 43
Shenal
  • 202
  • 5
  • 21
  • Can you show some more code, please? If you are using the ImageIO part of JAI, you should be able to write multiple pages as a sequence, and pass a separate param/compression to each `writeToSequence(img, param)` invocation. – Harald K Sep 08 '16 at 12:16
  • 1
    Yes I was using JAI. But now I changed to [icafe](https://github.com/dragon66/icafe/) its way easier. – Shenal Sep 08 '16 at 14:38
  • Thank you for the suggestion. Will try it – Shenal Sep 08 '16 at 14:38

2 Answers2

2

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.

Harald K
  • 26,314
  • 7
  • 65
  • 111
1

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(); 
Shenal
  • 202
  • 5
  • 21
  • You don't even need to create the third tiffOptions as it is the same as the second one. The library will fill the missing one with the last one you provided. – dragon66 Oct 30 '16 at 23:44