final class ConstantOpImage extends PatternOpImage {
/** Creates a Raster defining tile (0, 0) of the master pattern. */
private static Raster makePattern(SampleModel sampleModel,
Number[] bandValues) {
WritableRaster pattern = RasterFactory.createWritableRaster(
sampleModel, new Point(0, 0));
int width = sampleModel.getWidth();
int height = sampleModel.getHeight();
int dataType = sampleModel.getTransferType();
int numBands = sampleModel.getNumBands();
switch (dataType) {
case DataBuffer.TYPE_BYTE:
int[] bvalues = new int[numBands];
for (int i = 0; i < numBands; i++) {
bvalues[i] = bandValues[i].intValue() & ImageUtil.BYTE_MASK;
}
/* Put the first scanline in with setPixels. */
for (int x = 0; x < width; x++) {
pattern.setPixel(x, 0, bvalues);
}
break;
case DataBuffer.TYPE_USHORT: // USHORT is less than 127
case DataBuffer.TYPE_SHORT:
case DataBuffer.TYPE_INT:
int[] ivalues = new int[numBands];
for (int i = 0; i < numBands; i++) {
ivalues[i] = bandValues[i].intValue();
}
/* Put the first scanline in with setPixels. */
for (int x = 0; x < width; x++) {
pattern.setPixel(x, 0, ivalues);
}
break;
case DataBuffer.TYPE_FLOAT:
float[] fvalues = new float[numBands];
for (int i = 0; i < numBands; i++) {
fvalues[i] = bandValues[i].floatValue();
}
/* Put the first scanline in with setPixels. */
for (int x = 0; x < width; x++) {
pattern.setPixel(x, 0, fvalues);
}
break;
case DataBuffer.TYPE_DOUBLE:
double[] dvalues = new double[numBands];
for (int i = 0; i < numBands; i++) {
dvalues[i] = bandValues[i].doubleValue();
}
/* Put the first scanline in with setPixels. */
for (int x = 0; x < width; x++) {
pattern.setPixel(x, 0, dvalues);
}
break;
}
/* Copy the first line out. */
Object odata = pattern.getDataElements(0, 0, width, 1, null);
/* Use the first line to copy other rows. */
for (int y = 1; y < height; y++) {
pattern.setDataElements(0, y, width, 1, odata);
}
return pattern;
}