// Create an interleaved raster for copying for 8-bit case.
// This ensures that for RGB data the band offsets are {0,1,2}.
// If the JPEG encoder encounters data with BGR offsets as
// {2,1,0} then it will make yet another copy of the data
// which might as well be averted here.
WritableRaster target = sampleModel.getSampleSize(0) == 8 ?
Raster.createInterleavedRaster(DataBuffer.TYPE_BYTE,
im.getWidth(),
im.getHeight(),
sampleModel.getNumBands(),
new Point(im.getMinX(),
im.getMinY())) :
null;
// Copy the data.
ras = im.copyData(target);
}
// Convert the Raster to a WritableRaster.
WritableRaster wRas;
if (ras instanceof WritableRaster) {
wRas = (WritableRaster)ras;
} else {
wRas = Raster.createWritableRaster(ras.getSampleModel(),
ras.getDataBuffer(),
new Point(ras.getSampleModelTranslateX(),
ras.getSampleModelTranslateY()));
}
// Ensure that the WritableRaster has origin (0,0) and the
// same dimensions as the image (if derived from a single
// image tile, the tile dimensions might differ from the
// image dimensions.
if (wRas.getMinX() != 0 || wRas.getMinY() != 0 ||
wRas.getWidth() != im.getWidth() ||
wRas.getHeight() != im.getHeight())
wRas = wRas.createWritableChild(wRas.getMinX(),
wRas.getMinY(),
im.getWidth(),
im.getHeight(),
0, 0,
null);
bi = new BufferedImage(colorModel, wRas, false, null);
}
if (colorModel instanceof IndexColorModel) {
//
// Need to expand the indexed data to components.
// The convertToIntDiscrete method is used to perform this.
//
IndexColorModel icm = (IndexColorModel)colorModel;
bi = icm.convertToIntDiscrete(bi.getRaster(), false);
if(bi.getSampleModel().getNumBands() == 4) {
//
// Without copying data create a BufferedImage which has
// only the RGB bands, not the alpha band.
//
WritableRaster rgbaRas = bi.getRaster();
WritableRaster rgbRas =
rgbaRas.createWritableChild(0, 0,
bi.getWidth(), bi.getHeight(),
0, 0,
new int[] {0, 1, 2});
//