I am trying to create a huge TIFF image dynamically without holding the whole image in memory. I've chosen the TIFF format because it seems to be the only one among Java ImageWriters which supports this feature.
However whatever I tried I get either a multi-paged file (when I use writeToSequence
method) or an image containing just the last tile (when I use write()
method).
My basic code looks like that:
final ImageWriter tiffImageWriter = ImageIO.getImageWritersByFormatName("TIFF").next();
final ImageWriteParam param = tiffImageWriter.getDefaultWriteParam();
param.setTilingMode(MODE_EXPLICIT);
param.setTiling(TILE_SIZE, TILE_SIZE, 0, 0);
try (ImageOutputStream imageOutputStream = ImageIO.createImageOutputStream(response.getOutputStream())) {
tiffImageWriter.setOutput(imageOutputStream);
for (int tileX = 0; tileX < 10; tileX++) {
for (int tileY = 0; tileY < 10; tileY++) {
final BufferedImage bufferedImage = ... // some method producing a BufferedImage of size TILE_SIZExTILE_SIZE
tiffImageWriter.write(null, new IIOImage(bufferedImage, null, null), param);
imageOutputStream.flush();
}
}
}
tiffImageWriter.dispose();
Here response.getOutputStream()
is an output stream from my service.
The outcome of running this code is an image of TILE_SIZE x TILE_SIZE
size with the last tile as a content.
I wonder if it even possible to do this trick or this feature is designed for something else?