How to read a TIFF file by tiles with Java?
Asked Answered
J

3

5

Let's say I have a very large TIFF image as input. I am not able to load this image entirely because of memory specification I must comply with. So the following is not an option :

BufferedImage data = ImageIO.read(image);

Is there any Java library allowing to read a specific part of the image without buffering the whole image ? Or some ways to get TIFF tiles from a stream ?

Julee answered 1/10, 2014 at 10:16 Comment(0)
K
7

ImageIO can provide you an ImageReader for Tiff, and then you can use readTile. ImageIO has several getImageReadersBy... methods.

I am not aware whether tiff is supported by ImageIO, but ImageIO uses java SPI so one may plug-in ImageReaders and ImageWriters.

In fact this is a short-cut for read with ImageReadParam configured for tiles.

Never used tiles, but seeing the prior answer, I wanted to point this option out.

Kiernan answered 1/10, 2014 at 10:41 Comment(2)
I have used tiles, but in a different language, and never realized the API accomodated it directly, and I thought I knew the API... hmmm thanks for the lesson.Probability
@Probability hey, I do not know whether there is support for tiff, maybe via JAI, so your links were not without merit.Kiernan
P
1

There is no way in the native Java libraries that's able to read a Tiff file in it's components.... so, you are stuck with either using an external library, or building your own.

@Joop has provided a link in to the native Java library I was not aware of (know your tools!) If you cannot find the full support you need there, you may find the following useful:

The specification for Tiff files is not hugely complicated. I would consider writing my own file reader for it.

The Java JAI looks like it has signifciantly extended support for reading and parsing TIFF files. Consider:

Probability answered 1/10, 2014 at 10:34 Comment(1)
I would guess the OP is already using JAI or similar, but just in case.. Instead of writing your own reader (which is good fun, but takes quite some time), you can use mine. :-)Celestyna
C
1

As a complement to @Joop's answer:

All ImageIO ImageReader implementations support an ImageReadParam with source region specified (ImageReadParam.setSourceRegion(rect)), so that you can extract only a specific region of the entire image. This will work for any reader, even if the underlying format doesn't support tiling, and also regardless of the tile size for a tiled TIFF (or other format supporting tiles).

Example:

ImageReader reader = ImageIO.getImageReaders(input).next(); 
reader.setInput(input);

ImageReadParam param = reader.getDefaultReadParam();
param.setSourceRegion(new Rectangle(x, y, w, h));

BufferedImage aoi = reader.read(0, param);

That said, reading the image tile by tile is probably the most efficient way to achieve what you asked for, using the TIFF format. :-)

Celestyna answered 2/10, 2014 at 12:44 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.