private int[] bandIndices;
private static ImageLayout layoutHelper(ImageLayout layout,
RenderedImage source,
int[] bandIndices) {
ImageLayout il = layout == null ?
new ImageLayout() : (ImageLayout)layout.clone();
// Create a sub-banded SampleModel.
SampleModel sourceSM = source.getSampleModel();
int numBands = bandIndices.length;
// The only ColorModel compatible with a SinglePixelPackedSampleModel
// in the J2SE is a DirectColorModel which is by definition of
// ColorSpace.TYPE_RGB. Therefore if there are fewer than 3 bands
// a data copy is obligatory if a ColorModel will be possible.
SampleModel sm = null;
if(sourceSM instanceof SinglePixelPackedSampleModel && numBands < 3) {
sm = new PixelInterleavedSampleModel(
DataBuffer.TYPE_BYTE,
sourceSM.getWidth(), sourceSM.getHeight(),
numBands, sourceSM.getWidth()*numBands,
numBands == 1 ? new int[] {0} : new int[] {0, 1});
} else {
sm = sourceSM.createSubsetSampleModel(bandIndices);
}
il.setSampleModel(sm);
// Clear the ColorModel mask if needed.
ColorModel cm = il.getColorModel(null);
if(cm != null &&
!JDKWorkarounds.areCompatibleDataModels(sm, cm)) {
// Clear the mask bit if incompatible.
il.unsetValid(ImageLayout.COLOR_MODEL_MASK);
}
// Force the tile grid to be identical to that of the source.
il.setTileGridXOffset(source.getTileGridXOffset());
il.setTileGridYOffset(source.getTileGridYOffset());
il.setTileWidth(source.getTileWidth());
il.setTileHeight(source.getTileHeight());
return il;
}