WritableRaster dest,
Rectangle destRect) {
Raster source = sources[0];
PixelAccessor pa = new PixelAccessor(source.getSampleModel(), null);
PackedImageData srcIm =
pa.getPackedPixels(source, source.getBounds(), false, false);
pa = new PixelAccessor(dest.getSampleModel(), null);
PackedImageData dstIm =
pa.getPackedPixels(dest, destRect, true, false);
// src data under kernel, packed in int.
int[] srcUK = new int [kwPack * kh];
// sliding the kernel row by row
// general the packed matrix under the row
int dheight = destRect.height;
int dwidth = destRect.width;
int sOffset = srcIm.offset;
int dOffset = dstIm.offset;
for (int j = 0; j < dheight; j++) {
int selement, val, dindex, delement;
// reset srcUK for each row beginning
// src[sOffset +[-kx:kw-kx, -ky:kh-ky]] placed in srcUK
//
for (int m = 0; m < srcUK.length; m++){
srcUK[m] = 0;
}
// initial srcUK
// first shift left the packed bits under the sliding kernel by 1 bit
// then fill (compute) in the last bit of each row
for(int i = 0; i < kw -1; i++){
bitShiftMatrixLeft(srcUK, kh, kwPack); // expand for speedup?
int lastCol = kwPack - 1;
int bitLoc = srcIm.bitOffset + i;
int byteLoc = bitLoc >> 3;
bitLoc = 7 - (bitLoc & 7);
for(int m=0, sOffsetB = sOffset;
m < kh;
m++, sOffsetB += srcIm.lineStride){
selement = (int)srcIm.data[sOffsetB + byteLoc];
val = (selement >> bitLoc) & 0x1;
srcUK[lastCol] |= val;
lastCol += kwPack;
}
}
// same as above
// also setting dest
for (int i = 0; i < dwidth; i++){
bitShiftMatrixLeft(srcUK, kh, kwPack); // expand for speedup?
int lastCol = kwPack - 1;
int bitLoc = srcIm.bitOffset + i + kw -1;
int byteLoc = bitLoc >> 3;
bitLoc = 7 - (bitLoc & 7);
for(int m=0, sOffsetB = sOffset;
m < kh;
m++, sOffsetB += srcIm.lineStride){
selement = (int)srcIm.data[sOffsetB + byteLoc];
val = (selement >> bitLoc) & 0x1;
srcUK[lastCol] |= val;
lastCol += kwPack;
}
int dBitLoc = dstIm.bitOffset + i;
int dshift = 7 - (dBitLoc & 7);
int dByteLoc= (dBitLoc >> 3) + dOffset;
delement = (int)dstIm.data[dByteLoc];
delement |= (0x1) << dshift;
for (int m = 0; m < srcUK.length; m++){
if ((srcUK[m] & kdataPack[m]) != kdataPack[m]){
delement &= ~((0x1) << dshift);
break;
}
}
dstIm.data[dByteLoc] = (byte)delement;
}
sOffset += srcIm.lineStride;
dOffset += dstIm.lineStride;
}
pa.setPackedPixels(dstIm);
}