in src/main/java/org/apache/xmlgraphics/image/rendered/Any2sRGBRed.java [182:326]
public WritableRaster copyData(WritableRaster wr) {
// Get my source.
CachableRed src = (CachableRed)getSources().get(0);
ColorModel srcCM = src.getColorModel();
SampleModel srcSM = src.getSampleModel();
// Fast case, Linear SRGB source, INT Pack writable raster...
if (srcIsLsRGB
&& is_INT_PACK_COMP(wr.getSampleModel())) {
src.copyData(wr);
if (srcCM.hasAlpha()) {
GraphicsUtil.coerceData(wr, srcCM, false);
}
applyLut_INT(wr, linearToSRGBLut);
return wr;
}
if (srcCM == null) {
// We don't really know much about this source, let's
// guess based on the number of bands...
float [][] matrix = null;
switch (srcSM.getNumBands()) {
case 1:
matrix = new float[3][1];
matrix[0][0] = 1; // Red
matrix[1][0] = 1; // Grn
matrix[2][0] = 1; // Blu
break;
case 2:
matrix = new float[4][2];
matrix[0][0] = 1; // Red
matrix[1][0] = 1; // Grn
matrix[3][0] = 1; // Blu
matrix[3][1] = 1; // Alpha
break;
case 3:
matrix = new float[3][3];
matrix[0][0] = 1; // Red
matrix[1][1] = 1; // Grn
matrix[2][2] = 1; // Blu
break;
default:
matrix = new float[4][srcSM.getNumBands()];
matrix[0][0] = 1; // Red
matrix[1][1] = 1; // Grn
matrix[2][2] = 1; // Blu
matrix[3][3] = 1; // Alpha
break;
}
Raster srcRas = src.getData(wr.getBounds());
BandCombineOp op = new BandCombineOp(matrix, null);
op.filter(srcRas, wr);
return wr;
}
if (srcCM.getColorSpace()
== ColorSpace.getInstance(ColorSpace.CS_GRAY)) {
// This is a little bit of a hack. There is only
// a linear grayscale ICC profile in the JDK so
// many things use this when the data _really_
// has sRGB gamma applied.
try {
float [][] matrix = null;
switch (srcSM.getNumBands()) {
case 1:
matrix = new float[3][1];
matrix[0][0] = 1; // Red
matrix[1][0] = 1; // Grn
matrix[2][0] = 1; // Blu
break;
case 2:
default:
matrix = new float[4][2];
matrix[0][0] = 1; // Red
matrix[1][0] = 1; // Grn
matrix[3][0] = 1; // Blu
matrix[4][1] = 1; // Alpha
break;
}
Raster srcRas = src.getData(wr.getBounds());
BandCombineOp op = new BandCombineOp(matrix, null);
op.filter(srcRas, wr);
} catch (Throwable t) {
t.printStackTrace();
}
return wr;
}
ColorModel dstCM = getColorModel();
if (srcCM.getColorSpace() == dstCM.getColorSpace()) {
// No transform needed, just reformat data...
// System.out.println("Bypassing");
if (is_INT_PACK_COMP(srcSM)) {
src.copyData(wr);
} else {
GraphicsUtil.copyData(src.getData(wr.getBounds()), wr);
}
return wr;
}
Raster srcRas = src.getData(wr.getBounds());
assert srcRas instanceof WritableRaster;
WritableRaster srcWr = (WritableRaster)srcRas;
// Divide out alpha if we have it. We need to do this since
// the color convert may not be a linear operation which may
// lead to out of range values.
ColorModel srcBICM = srcCM;
if (srcCM.hasAlpha()) {
srcBICM = GraphicsUtil.coerceData(srcWr, srcCM, false);
}
BufferedImage srcBI;
BufferedImage dstBI;
srcBI = new BufferedImage(srcBICM,
srcWr.createWritableTranslatedChild(0, 0),
false,
null);
// System.out.println("src: " + srcBI.getWidth() + "x" +
// srcBI.getHeight());
ColorConvertOp op = new ColorConvertOp(dstCM.getColorSpace(),
null);
dstBI = op.filter(srcBI, null);
// System.out.println("After filter:");
WritableRaster wr00 = wr.createWritableTranslatedChild(0, 0);
for (int i = 0; i < dstCM.getColorSpace().getNumComponents(); i++) {
copyBand(dstBI.getRaster(), i, wr00, i);
}
if (dstCM.hasAlpha()) {
copyBand(srcWr, srcSM.getNumBands() - 1,
wr, getSampleModel().getNumBands() - 1);
}
return wr;
}