false, // Not premultiplied
Transparency.OPAQUE, // No transparency
DataBuffer.TYPE_BYTE); // 8 Bits
// Build a raster with bands 0, 1 and 2 of img's raster
DataBufferByte data = (DataBufferByte)srcWR.getDataBuffer();
srcWR = Raster.createBandedRaster
(data, argbWR.getWidth(), argbWR.getHeight(),
argbWR.getWidth(), new int[]{0, 1, 2},
new int[]{0, 0, 0}, new Point(0, 0));
BufferedImage newImg = new BufferedImage
(newCM, srcWR, newCM.isAlphaPremultiplied(), null);
/**
* Now, convert the image to sRGB
*/
ComponentColorModel sRGBCompCM = new ComponentColorModel
(ColorSpace.getInstance(ColorSpace.CS_sRGB),
new int[]{8, 8, 8},
false,
false,
Transparency.OPAQUE,
DataBuffer.TYPE_BYTE);
WritableRaster wr = Raster.createBandedRaster
(DataBuffer.TYPE_BYTE, argbWR.getWidth(), argbWR.getHeight(),
sRGBCompCM.getNumComponents(), new Point(0, 0));
BufferedImage sRGBImage = new BufferedImage
(sRGBCompCM, wr, false, null);
ColorConvertOp colorConvertOp = new ColorConvertOp(null);
colorConvertOp.filter(newImg, sRGBImage);
/**
* Integrate alpha back into the image if there is any
*/
if (imgCM.hasAlpha()){
DataBufferByte rgbData = (DataBufferByte)wr.getDataBuffer();
byte[][] imgBanks = data.getBankData();
byte[][] rgbBanks = rgbData.getBankData();
byte[][] argbBanks = {rgbBanks[0], rgbBanks[1],
rgbBanks[2], imgBanks[3]};
DataBufferByte argbData = new DataBufferByte(argbBanks, imgBanks[0].length);
srcWR = Raster.createBandedRaster
(argbData, argbWR.getWidth(), argbWR.getHeight(),
argbWR.getWidth(), new int[]{0, 1, 2, 3},
new int[]{0, 0, 0, 0}, new Point(0, 0));
sRGBCompCM = new ComponentColorModel