I have a tiled imaged. I’m able to read metadata, so I know tile dimensions; but I don’t know how to read a specific tile. I have these questions :
-
Suppose my tiff is composed of 128×128 tile, how can I read the tile positioned at 0,0 (x,y)?
-
How many tiles are present in my tiff file?
I try to develop a code to manage a single tile but I don’t know how to identify a specific tile position.
IImageMetadata metadata = Sanselan.getMetadata(imageFile);
TiffDirectory tiffDirectory = ((TiffImageMetadata) metadata).findDirectory(TiffDirectoryConstants.DIRECTORY_TYPE_ROOT);
ByteSourceFile byteSource = new ByteSourceFile(imageFile);
ArrayList<?> elements = tiffDirectory.getTiffRawImageDataElements();
TiffImageData.Data data[] = new TiffImageData.Data[elements.size()];
for (int i = 0; i < elements.size(); i++) {
TiffDirectory.ImageDataElement element = (TiffDirectory.ImageDataElement) elements.get(i);
byte bytes[] = byteSource.getBlock(element.offset, element.length);
data[i] = new TiffImageData.Data(element.offset, element.length, bytes);
}
TiffField tileWidthField = tiffDirectory.findField(TiffTagConstants.TIFF_TAG_TILE_WIDTH);
if (null == tileWidthField)
throw new ImageReadException("Can't find tile width field.");
int tileWidth = tileWidthField.getIntValue();
TiffField tileLengthField = tiffDirectory.findField(TiffTagConstants.TIFF_TAG_TILE_LENGTH);
if (null == tileLengthField)
throw new ImageReadException("Can't find tile length field.");
int tileLength = tileLengthField.getIntValue();
TiffImageData.Tiles tile = new TiffImageData.Tiles(data, tileWidth, tileLength);
I’m using Sanselan from Apache incubator to make these operations.
The TIFF tiles are laid out in row major order so it is relatively simple to figure out which tile you need to read. For your specific question, the tile at (0,0) is tile #0. The number of tiles is calculated from: