This discussion is archived
2 Replies Latest reply: Jul 10, 2013 8:38 PM by c06e753f-5e07-4e13-bc17-3f293bd84afd RSS

How to create multipage tiff from BufferedImage's

843802 Newbie
Currently Being Moderated
Hi

I can't figure out how to create a multipage TIFF from BufferedImage's using JAI. So far I can read a multipage TIFF without any problems:
SeekableStream s = new FileSeekableStream(file);

TIFFDecodeParam param = null;

dec = ImageCodec.createImageDecoder("tiff", s, param);

RenderedImage op = new NullOpImage(dec.decodeAsRenderedImage(page),
                null,
                OpImage.OP_IO_BOUND,
                null);
where file is the TIFF-file to read and page is an int describing page number in the TIFF-file starting from 0.

First question: Is there a similar way to create a multipage TIFF and add/append BufferedImage as a page? It would be great to do this directly to the newly created TIFF-file to prevent memory problems. I have searched google and several forums and I haven't found a solution yet.

Second question: Is there any way to get a BufferedImage instead og RenderedImage when reading from a TIFF-file? I want to use getRGB but with RenderedImage I can only use getData().getPixel and at the moment I don't know how to get normal RGB data from that function.

Thanks a lot for any response :-)
  • 1. Re: How to create multipage tiff from BufferedImage's
    843802 Newbie
    Currently Being Moderated
    Question 1
    To create a multipage tiff you use the method TIFFEncodeParam#setExtraImages. The method takes an iterator. I'm not sure if there is a built in way to append pages to an already existing file, but in theory you can read from the already existing file as needed (when iterator#next is called) and write to a temporary file, before replacing the existing file with the temporary one. So you'll still have to read the existing file, but you don't have to keep all the images in memory.

    If you switch to core java's ImageWriter/ImageReader api and get [JAI-Imageio|https://jai-imageio.dev.java.net/binary-builds.html] (which has a TIFF ImageWriter plugin) then you can insert images into an already existing file. Presumably the TIFF ImageWriter appends directly to the file.

    Question 2
    The only thing special about a BufferedImage is that it has exactly one raster at (0,0) and it's guaranteed to have a color model. RenderedImages may have more than one raster and are not guaranteed to have a color model. But if your RenderedImage does have a color model, then you get rgb data from it the same way a BufferedImage does,
    colorModel.getRGB(raster.getDataElements(x, y, null));
    There are other ways, but this is the most generic.
  • 2. Re: How to create multipage tiff from BufferedImage's
    c06e753f-5e07-4e13-bc17-3f293bd84afd Newbie
    Currently Being Moderated

    i can't tell you how to create multipage tiff by using the software you are talking about. but i do know how to make to make the tiff out by using other software. if you are interested in , please let me know.