if (srcCM instanceof IndexColorModel) {
src = ((IndexColorModel) srcCM).convertToIntDiscrete(src.getRaster(), false);
}
ColorSpace srcCS = srcCM.getColorSpace();
BufferedImage res;
boolean isDstIndex = false;
if (dst != null) {
if (src.getWidth() != dst.getWidth() ||
src.getHeight() != dst.getHeight()) {
throw new IllegalArgumentException(Messages.getString("awt.263")); //$NON-NLS-1$
}
if (dst.getColorModel() instanceof IndexColorModel) {
isDstIndex = true;
res = createCompatibleDestImage(src, null);
} else {
res = dst;
}
} else {
res = createCompatibleDestImage(src, null);
}
ColorModel dstCM = res.getColorModel();
ColorSpace dstCS = dstCM.getColorSpace();
ICC_Profile srcPf = null, dstPf = null;
if (srcCS instanceof ICC_ColorSpace) {
srcPf = ((ICC_ColorSpace)srcCS).getProfile();
}
if (dstCS instanceof ICC_ColorSpace) {
dstPf = ((ICC_ColorSpace)dstCS).getProfile();
}
boolean isFullICC = isICC && srcPf != null && dstPf != null;
if (isFullICC) {
ICC_Transform t =
tCreator.getTransform(srcPf, dstPf, (ICC_Profile[]) conversionSequence);
cc.translateColor(t, src, res);
} else { // Perform non-ICC transform
Object sequence[] = tCreator.getSequence(
srcPf == null ? (Object) srcCS : srcPf,
dstPf == null ? (Object) dstCS : dstPf);
int srcW = src.getWidth();
int srcH = src.getHeight();
int numPixels = srcW*srcH;
// Load all pixel data into array tmpData
float tmpData[][] =
new float[numPixels][tCreator.maxComponents];
for (int row=0, dataPos=0; row<srcW; row++) {
for (int col=0; col<srcH; col++) {
tmpData[dataPos] =
srcCM.getNormalizedComponents(
src.getRaster().getDataElements(row, col, null),
tmpData[dataPos], 0);
dataPos++;
}
}
// Copy alpha channel if needed
float alpha[] = null;
int alphaIdx = srcCM.numComponents - 1;
if (srcCM.hasAlpha() && dstCM.hasAlpha()) {
alpha = new float[numPixels];
for (int i=0; i<numPixels; i++) {
alpha[i] = tmpData[i][alphaIdx];
}
}
// Translate colors
applySequence(sequence, tmpData, srcCS, dstCS);
// Copy alpha if needed
if (dstCM.hasAlpha()) {
alphaIdx = dstCM.numComponents - 1;
if (alpha != null) {
for (int i=0; i<numPixels; i++) {
tmpData[i][alphaIdx] = alpha[i];
}
} else {
for (int i=0; i<numPixels; i++) {
tmpData[i][alphaIdx] = 1f;
}
}
}
// Store data back to the image
for (int row=0, dataPos=0; row<srcW; row++) {
for (int col=0; col<srcH; col++) {
res.getRaster().setDataElements(row, col,
dstCM.getDataElements(tmpData[dataPos++], 0 , null));
}
}
}