Rectangle rect,
double[] backgroundValues) {
rect = rect.intersection(raster.getBounds());
int numBands = raster.getSampleModel().getNumBands();
SampleModel sm = raster.getSampleModel();
PixelAccessor accessor = new PixelAccessor(sm, null);
if (isBinary(sm)) {
//fill binary data
byte value = (byte)(((int)backgroundValues[0]) & 1);
if (value == 0)
return;
int rectX = rect.x;
int rectY = rect.y;
int rectWidth = rect.width;
int rectHeight = rect.height;
int dx = rectX - raster.getSampleModelTranslateX();
int dy = rectY - raster.getSampleModelTranslateY();
DataBuffer dataBuffer = raster.getDataBuffer();
MultiPixelPackedSampleModel mpp = (MultiPixelPackedSampleModel)sm;
int lineStride = mpp.getScanlineStride();
int eltOffset = dataBuffer.getOffset() + mpp.getOffset(dx, dy);
int bitOffset = mpp.getBitOffset(dx);
switch(sm.getDataType()) {
case DataBuffer.TYPE_BYTE:
{
byte[] data = ((DataBufferByte)dataBuffer).getData();
int bits = bitOffset & 7;
int otherBits = (bits == 0) ? 0: 8 - bits;
byte mask = (byte)(255 >> bits);
int lineLength = (rectWidth - otherBits) / 8;
int bits1 = (rectWidth - otherBits) & 7;
byte mask1 = (byte)(255 << (8 - bits1));
// If operating within a single byte, merge masks into one
// and don't apply second mask after while loop
if (lineLength == 0) {
mask &= mask1;
bits1 = 0;
}
for (int y = 0; y < rectHeight; y++) {
int start = eltOffset;
int end = start + lineLength;
if (bits != 0)
data[start++] |= mask;
while (start < end)
data[start++] = (byte)255;
if (bits1 != 0)
data[start] |= mask1;
eltOffset += lineStride;
}
break;
}
case DataBuffer.TYPE_USHORT:
{
short[] data = ((DataBufferUShort)dataBuffer).getData();
int bits = bitOffset & 15;
int otherBits = (bits == 0) ? 0: 16 - bits;
short mask = (short)(65535 >> bits);
int lineLength = (rectWidth - otherBits) / 16;
int bits1 = (rectWidth - otherBits) & 15;
short mask1 = (short)(65535 << (16 - bits1));
// If operating within a single byte, merge masks into one
// and don't apply second mask after while loop
if (lineLength == 0) {
mask &= mask1;
bits1 = 0;
}
for (int y = 0; y < rectHeight; y++) {
int start = eltOffset;
int end = start + lineLength;
if (bits != 0)
data[start++] |= mask;
while (start < end)
data[start++] = (short)0xFFFF;
if (bits1 != 0)
data[start++] |= mask1;
eltOffset += lineStride;
}
break;
}
case DataBuffer.TYPE_INT:
{
int[] data = ((DataBufferInt)dataBuffer).getData();
int bits = bitOffset & 31;
int otherBits = (bits == 0) ? 0: 32 - bits;
int mask = 0xFFFFFFFF >> bits;
int lineLength = (rectWidth - otherBits) / 32;
int bits1 = (rectWidth - otherBits) & 31;
int mask1 = 0xFFFFFFFF << (32 - bits1);
// If operating within a single byte, merge masks into one
// and don't apply second mask after while loop
if (lineLength == 0) {
mask &= mask1;
bits1 = 0;
}
for (int y = 0; y < rectHeight; y++) {
int start = eltOffset;
int end = start + lineLength;
if (bits != 0)
data[start++] |= mask;
while (start < end)
data[start++] = 0xFFFFFFFF;
if (bits1 != 0)
data[start++] |= mask1;
eltOffset += lineStride;
}
break;
}
}
} else {
int srcSampleType = accessor.sampleType == PixelAccessor.TYPE_BIT ?
DataBuffer.TYPE_BYTE : accessor.sampleType;
UnpackedImageData uid = accessor.getPixels(raster, rect,
srcSampleType, false);
rect = uid.rect;
int lineStride = uid.lineStride;
int pixelStride = uid.pixelStride;