height = (int) dir.getFieldAsLong(XTIFF.TIFFTAG_IMAGE_LENGTH);
photometric_interp = (int) dir.getFieldAsLong(XTIFF.TIFFTAG_PHOTOMETRIC_INTERPRETATION);
// Read the TIFFTAG_BITS_PER_SAMPLE field
XTIFFField bitsField = dir.getField(XTIFF.TIFFTAG_BITS_PER_SAMPLE);
if (bitsField == null) {
// Default
bitsPerSample = new char[1];
bitsPerSample[0] = 1;
} else {
bitsPerSample = bitsField.getAsChars();
}
for (int i = 1; i < bitsPerSample.length; i++) {
if (bitsPerSample[i] != bitsPerSample[1]) {
throw new RuntimeException(JaiI18N.getString("XTIFFImageDecoder19"));
}
}
// Get the number of samples per pixel
XTIFFField sfield = dir.getField(XTIFF.TIFFTAG_SAMPLES_PER_PIXEL);
if (sfield == null) {
samplesPerPixel = 1;
} else {
samplesPerPixel = (int) sfield.getAsLong(0);
}
// Figure out if any extra samples are present.
XTIFFField efield = dir.getField(XTIFF.TIFFTAG_EXTRA_SAMPLES);
if (efield == null) {
extraSamples = 0;
} else {
extraSamples = (int) efield.getAsLong(0);
}
// Read the TIFFTAG_SAMPLE_FORMAT tag to see whether the data might be
// signed or floating point
XTIFFField sampleFormatField = dir.getField(XTIFF.TIFFTAG_SAMPLE_FORMAT);
if (sampleFormatField != null) {
sampleFormat = sampleFormatField.getAsChars();
// Check that all the samples have the same format
for (int l = 1; l < sampleFormat.length; l++) {
if (sampleFormat[l] != sampleFormat[0]) {
throw new RuntimeException(JaiI18N.getString("XTIFFImageDecoder20"));
}
}
} else {
sampleFormat = new char[] { 1 };
}
if (sampleFormat[0] == 1 || sampleFormat[0] == 4) {
// Unsigned or unknown
if (bitsPerSample[0] == 8) {
dataType = DataBuffer.TYPE_BYTE;
} else if (bitsPerSample[0] == 16) {
dataType = DataBuffer.TYPE_USHORT;
} else if (bitsPerSample[0] == 32) {
dataType = DataBuffer.TYPE_INT;
}
} else if (sampleFormat[0] == 2) {
// Signed
if (bitsPerSample[0] == 1 || bitsPerSample[0] == 4
|| bitsPerSample[0] == 8) {
throw new RuntimeException(JaiI18N.getString("XTIFFImageDecoder21"));
} else if (bitsPerSample[0] == 16) {
dataType = DataBuffer.TYPE_SHORT;
} else if (bitsPerSample[0] == 32) {
dataType = DataBuffer.TYPE_INT;
}
} else if (sampleFormat[0] == 3) {
// Floating point
// dataType = DataBuffer.TYPE_FLOAT;
throw new RuntimeException(JaiI18N.getString("XTIFFImageDecoder22"));
}
if (dir.getField(XTIFF.TIFFTAG_TILE_WIDTH) != null) {
// Image is in tiled format
tileWidth = (int) dir.getFieldAsLong(XTIFF.TIFFTAG_TILE_WIDTH);
tileHeight = (int) dir.getFieldAsLong(XTIFF.TIFFTAG_TILE_LENGTH);
tileOffsets = (dir.getField(XTIFF.TIFFTAG_TILE_OFFSETS)).getAsLongs();
tileByteCounts = dir.getField(XTIFF.TIFFTAG_TILE_BYTE_COUNTS)
.getAsLongs();
} else {
// Image is in stripped format, looks like tiles to us
tileWidth = width;
XTIFFField field = dir.getField(XTIFF.TIFFTAG_ROWS_PER_STRIP);
if (field == null) {
// Default is infinity (2^32 -1), basically the entire image
// TODO: Can do a better job of tiling here
tileHeight = height;
} else {
long l = field.getAsLong(0);
long infinity = 1;
infinity = (infinity << 32) - 1;
if (l == infinity) {
// 2^32 - 1 (effectively infinity, entire image is 1 strip)
tileHeight = height;
} else {
tileHeight = (int) l;
}
}
XTIFFField tileOffsetsField = dir.getField(XTIFF.TIFFTAG_STRIP_OFFSETS);
if (tileOffsetsField == null) {
throw new RuntimeException(JaiI18N.getString("XTIFFImageDecoder11"));
} else {
tileOffsets = tileOffsetsField.getAsLongs();
}
XTIFFField tileByteCountsField = dir.getField(XTIFF.TIFFTAG_STRIP_BYTE_COUNTS);
if (tileByteCountsField == null) {
throw new RuntimeException(JaiI18N.getString("XTIFFImageDecoder12"));
} else {
tileByteCounts = tileByteCountsField.getAsLongs();
}
}
}