// The output image itself, directly allocated in the file cache
CachedImage image = new CachedImage(layout, JAIContext.fileCache);
final int maxTileX = image.getNumXTiles();
ProgressIndicator indicator = null;
if ( thread != null )
indicator = thread.getProgressIndicator();
if ( indicator != null )
indicator.setMaximum( tf.tiles );
int bandList[] = new int[tf.samplesPerPixel];
for (int i = 0; i < tf.samplesPerPixel; i++)
bandList[i] = i;
Rectangle imageBounds = new Rectangle(0, 0, tf.imageWidth, tf.imageHeight);
for ( int tile = 0; tile < tf.tiles; tile++ ) {
if ( thread != null && thread.isCanceled() )
throw new UserCanceledException();
final int tileX = tf.tiled ? tile % maxTileX : 0;
final int tileY = tf.tiled ? tile / maxTileX : tile;
// The actual tile bounds, clipping on the image bounds
Rectangle tileBounds = new Rectangle(tileX * tf.tiffTileWidth,
tileY * tf.tiffTileHeight,
tf.tiffTileWidth,
tf.tiffTileHeight).intersection(imageBounds);
// the corresponding tile data
int tileData = (tf.samplesPerPixel / tf.planes) * tileBounds.width * tileBounds.height * (tf.bitsPerSample == 8 ? 1 : 2);
/* If the TIFF image is tiled and it doesn't have an alpha channel
then we can read directly into the destination image buffer,
don't allocate an intermediate raster */
WritableRaster raster =
!tf.tiled || tf.hasAlpha
? Raster.createWritableRaster(tf.tiffTsm, db, new Point(tileBounds.x, tileBounds.y))
: null;
for ( int plane = 0; plane < tf.planes; plane++ ) {
if ( tf.tiled ) {
if (!tf.hasAlpha)
raster = image.getWritableTile( tileX, tileY );
if ( tf.bitsPerSample == 8 ) {
final byte[] buffer =
((DataBufferByte)raster.getDataBuffer()).getData(
plane );
int read = readTileByte( tile + plane * tf.tiles, buffer, 0, tileData );
if (read != tileData)
throw new LCImageLibException("Broken TIFF File");
} else {
final short[] buffer =
((DataBufferUShort)raster.getDataBuffer()).getData(
plane );
int read = readTileShort( tile + plane * tf.tiles, buffer, 0, tileData );
if (read != tileData)
throw new LCImageLibException("Broken TIFF File");
}
} else {
if ( tf.bitsPerSample == 8 ) {
final byte[] buffer =
((DataBufferByte)db).getData( plane );
int read = readStripByte( tileY + plane * tf.tiles, buffer, 0, tileData );
if (read != tileData)
throw new LCImageLibException("Broken TIFF File");
} else {
final short[] buffer =
((DataBufferUShort)db).getData( plane );
int read = readStripShort( tileY + plane * tf.tiles, buffer, 0, tileData );
if (read != tileData)
throw new LCImageLibException("Broken TIFF File");
}
}
}
if (!tf.tiled || tf.hasAlpha)
image.setData(raster.createChild(raster.getMinX(), raster.getMinY(),
raster.getWidth(), raster.getHeight(),
raster.getMinX(), raster.getMinY(),
bandList));
if ( indicator != null )
indicator.incrementBy( 1 );
}
if ( indicator != null )
indicator.setIndeterminate( true );
m_image = image;
}