"Subimage {0} does not exist.", new Object[] {new Integer(pageIndex)});
throw new SubImageNotFoundException(errorMessage);
}
int width = (int)dir.getFieldAsLong(TIFFImageDecoder.TIFF_IMAGE_WIDTH);
int height = (int)dir.getFieldAsLong(TIFFImageDecoder.TIFF_IMAGE_LENGTH);
ImageSize size = new ImageSize();
size.setSizeInPixels(width, height);
int unit = 2; //inch is default
if (dir.isTagPresent(TIFFImageDecoder.TIFF_RESOLUTION_UNIT)) {
unit = (int)dir.getFieldAsLong(TIFFImageDecoder.TIFF_RESOLUTION_UNIT);
}
if (unit == 2 || unit == 3) {
float xRes;
float yRes;
TIFFField fldx = dir.getField(TIFFImageDecoder.TIFF_X_RESOLUTION);
TIFFField fldy = dir.getField(TIFFImageDecoder.TIFF_Y_RESOLUTION);
if (fldx == null || fldy == null) {
unit = 2;
xRes = context.getSourceResolution();
yRes = xRes;
} else {
xRes = fldx.getAsFloat(0);
yRes = fldy.getAsFloat(0);
}
if (xRes == 0 || yRes == 0) {
//Some TIFFs may report 0 here which would lead to problems
size.setResolution(context.getSourceResolution());
} else if (unit == 2) {
size.setResolution(xRes, yRes); //Inch
} else {
size.setResolution(
UnitConv.in2mm(xRes) / 10,
UnitConv.in2mm(yRes) / 10); //Centimeters
}
} else {
size.setResolution(context.getSourceResolution());
}
size.calcSizeFromPixels();
if (log.isTraceEnabled()) {
log.trace("TIFF image detected: " + size);
}
info = new ImageInfo(uri, MimeConstants.MIME_TIFF);
info.setSize(size);
TIFFField fld;
fld = dir.getField(TIFFImageDecoder.TIFF_COMPRESSION);
if (fld != null) {
int compression = fld.getAsInt(0);
if (log.isTraceEnabled()) {
log.trace("TIFF compression: " + compression);
}
info.getCustomObjects().put("TIFF_COMPRESSION", new Integer(compression));
}
fld = dir.getField(TIFFImageDecoder.TIFF_TILE_WIDTH);
if (fld != null) {
if (log.isTraceEnabled()) {
log.trace("TIFF is tiled");
}
info.getCustomObjects().put("TIFF_TILED", Boolean.TRUE);
}
int stripCount;
fld = dir.getField(TIFFImageDecoder.TIFF_ROWS_PER_STRIP);
if (fld == null) {
stripCount = 1;
} else {
stripCount = (int)Math.ceil(size.getHeightPx() / (double)fld.getAsLong(0));
}
if (log.isTraceEnabled()) {
log.trace("TIFF has " + stripCount + " strips.");
}
info.getCustomObjects().put("TIFF_STRIP_COUNT", new Integer(stripCount));