///////////////////////////////////////////////////////////////////////
//let's check the number of bands
final SampleModel outputImageSampleModel= outputImage.getSampleModel();
int numBands=outputImageSampleModel.getNumBands();
final int dataType= outputImageSampleModel.getDataType();
GridSampleDimension sd[];
if(numBands>4)
{
//get the visible band
final int visibleBand=CoverageUtilities.getVisibleBand(outputImage);
outputImage=
new ImageWorker(outputImage).setRenderingHints(this.getHints()).retainBands(new int[]{visibleBand}).getRenderedImage();
sd=new GridSampleDimension[]{(GridSampleDimension) output.getSampleDimension(visibleBand)};
}else{
sd=output.getSampleDimensions();
}
//more general case, let's check the data type and let go only USHORT and BYTE
// TODO I am not sure this will work with multipacked types (using INT for an RGB as an instance)
// TODO should we go to component color model also?
// TODO use JAI TOOLS statistics and ignore no data properly.
switch(dataType){
// in case the original image has a USHORT pixel type without being associated
// with an index color model I would still go to 8 bits
case DataBuffer.TYPE_USHORT:
if(outputImage.getColorModel() instanceof IndexColorModel){
break;
}
case DataBuffer.TYPE_DOUBLE:
case DataBuffer.TYPE_FLOAT:
case DataBuffer.TYPE_INT:
case DataBuffer.TYPE_SHORT:
//rescale to byte
outputImage=
new ImageWorker(outputImage).setRenderingHints(this.getHints()).rescaleToBytes().getRenderedImage();
}
// ///////////////////////////////////////////////////////////////////
// Apply opacity if needed
// ///////////////////////////////////////////////////////////////////
final RenderedImage finalImage;
if(opacity < 1) {
ImageWorker ow = new ImageWorker(outputImage);
finalImage = ow.applyOpacity(opacity).getRenderedImage();
numBands=finalImage.getSampleModel().getNumBands();
sd= new GridSampleDimension[numBands];
for(int i=0;i<numBands;i++) {
sd[i]= new GridSampleDimension(TypeMap.getColorInterpretation(finalImage.getColorModel(), i).name());
}
// create a new grid coverage but preserve as much input as possible
return this.getCoverageFactory().create(
output.getName(),