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 ?

rolfl
  • 17,539
  • 7
  • 42
  • 76

3 Answers3

6

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.

Joop Eggen
  • 107,315
  • 7
  • 83
  • 138
  • 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. – rolfl Oct 01 '14 at 10:49
  • @rolfl hey, I do not know whether there is support for tiff, maybe via [JAI](http://www.oracle.com/technetwork/java/jaifaq-138288.html), so your links were not without merit. – Joop Eggen Oct 01 '14 at 11:08
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:

rolfl
  • 17,539
  • 7
  • 42
  • 76
  • 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](https://github.com/haraldk/TwelveMonkeys#aldusadobe-tagged-image-file-format-tiff). :-) – Harald K Oct 02 '14 at 12:49
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. :-)

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