public WritableRaster copyData(WritableRaster wr) {
// Get my source.
CachableRed src = (CachableRed)getSources().get(0);
SampleModel sm = src.getSampleModel();
ColorModel srcCM = src.getColorModel();
if (srcCM == null) {
// We don't really know much about this source.
float [][] matrix = null;
if (sm.getNumBands() == 2) {
matrix = new float[2][2];
matrix[0][0] = 1;
matrix[1][1] = 1;
} else {
matrix = new float[sm.getNumBands()][1];
matrix[0][0] = 1;
}
Raster srcRas = src.getData(wr.getBounds());
BandCombineOp op = new BandCombineOp(matrix, null);
op.filter(srcRas, wr);
} else {
Raster srcRas = src.getData(wr.getBounds());
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, dstBI;
srcBI = new BufferedImage(srcCM,
srcWr.createWritableTranslatedChild(0,0),
false,
null);
ColorModel dstCM = getColorModel();
if (!dstCM.hasAlpha()) {
// No alpha ao we don't have to work around the bug
// in the color convert op.
dstBI = new BufferedImage
(dstCM, wr.createWritableTranslatedChild(0,0),
dstCM.isAlphaPremultiplied(), null);
} else {
// All this nonsense is to work around the fact that the
// Color convert op doesn't properly copy the Alpha from
// src to dst.
PixelInterleavedSampleModel dstSM;
dstSM = (PixelInterleavedSampleModel)wr.getSampleModel();
SampleModel smna = new PixelInterleavedSampleModel
(dstSM.getDataType(),
dstSM.getWidth(), dstSM.getHeight(),
dstSM.getPixelStride(), dstSM.getScanlineStride(),
new int [] { 0 });
WritableRaster dstWr;
dstWr = Raster.createWritableRaster(smna,
wr.getDataBuffer(),
new Point(0,0));
dstWr = dstWr.createWritableChild
(wr.getMinX()-wr.getSampleModelTranslateX(),
wr.getMinY()-wr.getSampleModelTranslateY(),
wr.getWidth(), wr.getHeight(),
0, 0, null);
ColorModel cmna = new ComponentColorModel
(ColorSpace.getInstance(ColorSpace.CS_GRAY),
new int [] {8}, false, false,
Transparency.OPAQUE,
DataBuffer.TYPE_BYTE);