[...]
@Override
public Array
readData(Variable variable, Section section)
throws
IOException, InvalidRangeException {
int image =
Integer.MIN_VALUE;
int band =
Integer.MIN_VALUE;
for(Attribute attribute : variable.getAttributes()) {
if
(ATTRIBUTE_IMAGE.equals(attribute.getName())) {
image = attribute.getNumericValue().intValue();
} else
if (ATTRIBUTE_BAND.equals(attribute.getName())) {
band
= attribute.getNumericValue().intValue();
}
}
if (image
< 0 || band < 0) {
throw
new IllegalArgumentException("ERROR: Can't extract image "
+ "or band number from variable.");
}
ImageReader
imageReader = null;
ImageInputStream imageInputStream = null;
try {
Range
yRange = section.getRange(0);
Range
xRange = section.getRange(1);
imageReader = createImageReader();
if
(section.getStride(1) > 1 &&
variable.getDataType().isFloatingPoint() &&
IMAGEIO_JAI_READER.equals(imageReader.getClass().getName())) {
throw new IllegalArgumentException(
"ERROR: Data corruption bug in Java JAI ImageIO TIFF "
+ " ImageReader with floating point data and X strides > "
+ " 1. To utilize this functionality please obatain another"
+ "JAI TIFF implementation such as ImageIO-Ext and place"
+ "in the application classpath");
}
imageInputStream = ImageIO.createImageInputStream(new File(location));
imageReader.setInput(imageInputStream, true, true);
ImageReadParam imageReadParam = imageReader.getDefaultReadParam();
imageReadParam.setSourceRegion(new Rectangle(
xRange.first(),
yRange.first(),
xRange.last() - xRange.first() + 1,
yRange.last() - yRange.first() + 1));
imageReadParam.setSourceSubsampling(
xRange.stride(),
yRange.stride(),
0,
0);
imageReadParam.setSourceBands(new int[] { band } );
BufferedImage bufferedImage = imageReader.read(image, imageReadParam);
Raster
raster = bufferedImage.getData();
Object
arrayAsObject = createJavaArray(
variable.getDataType(), (int)section.computeSize());
Array
array = Array.factory(
variable.getDataType(),
section.getShape(),
arrayAsObject);
raster.getDataElements(
0, 0,
bufferedImage.getWidth(), bufferedImage.getHeight(),
arrayAsObject);
return
array;
} finally {
if
(imageReader != null) {
imageReader.dispose();
}
if
(imageInputStream != null) {
try
{ imageInputStream.close(); } catch (IOException e) { }
}
}
}
[...]