下面列出了怎么用java.awt.image.DataBuffer的API类实例代码及写法,或者点击链接到github查看源代码。
static ColorModel createComponentCM(ColorSpace colorSpace,
int numBands,
int dataType,
boolean hasAlpha,
boolean isAlphaPremultiplied) {
int transparency =
hasAlpha ? Transparency.TRANSLUCENT : Transparency.OPAQUE;
int[] numBits = new int[numBands];
int bits = DataBuffer.getDataTypeSize(dataType);
for (int i = 0; i < numBands; i++) {
numBits[i] = bits;
}
return new ComponentColorModel(colorSpace,
numBits,
hasAlpha,
isAlphaPremultiplied,
transparency,
dataType);
}
public static long getBandSize(SampleModel sm) {
int elementSize = DataBuffer.getDataTypeSize(sm.getDataType());
if (sm instanceof ComponentSampleModel) {
ComponentSampleModel csm = (ComponentSampleModel)sm;
int pixelStride = csm.getPixelStride();
int scanlineStride = csm.getScanlineStride();
long size = Math.min(pixelStride, scanlineStride);
if (pixelStride > 0)
size += pixelStride * (sm.getWidth() - 1);
if (scanlineStride > 0)
size += scanlineStride * (sm.getHeight() - 1);
return size * ((elementSize + 7) / 8);
} else
return getTileSize(sm);
}
protected ImageTypeSpecifier produce() {
switch (csCode) {
case JPEG.JCS_GRAYSCALE:
return ImageTypeSpecifier.createFromBufferedImageType
(BufferedImage.TYPE_BYTE_GRAY);
case JPEG.JCS_YCbCr:
//there is no YCbCr raw type so by default we assume it as RGB
case JPEG.JCS_RGB:
return ImageTypeSpecifier.createInterleaved(JPEG.JCS.sRGB,
JPEG.bOffsRGB,
DataBuffer.TYPE_BYTE,
false,
false);
default:
return null;
}
}
@Override
public ColorModel getColorModel(int transparency) {
switch (transparency) {
case Transparency.OPAQUE:
// REMIND: once the ColorModel spec is changed, this should be
// an opaque premultiplied DCM...
return new DirectColorModel(24, 0xff0000, 0xff00, 0xff);
case Transparency.BITMASK:
return new DirectColorModel(25, 0xff0000, 0xff00, 0xff, 0x1000000);
case Transparency.TRANSLUCENT:
ColorSpace cs = ColorSpace.getInstance(ColorSpace.CS_sRGB);
return new DirectColorModel(cs, 32,
0xff0000, 0xff00, 0xff, 0xff000000,
true, DataBuffer.TYPE_INT);
default:
return null;
}
}
@Override
public ColorModel getColorModel(int transparency) {
switch (transparency) {
case Transparency.OPAQUE:
// REMIND: once the ColorModel spec is changed, this should be
// an opaque premultiplied DCM...
return new DirectColorModel(24, 0xff0000, 0xff00, 0xff);
case Transparency.BITMASK:
return new DirectColorModel(25, 0xff0000, 0xff00, 0xff, 0x1000000);
case Transparency.TRANSLUCENT:
ColorSpace cs = ColorSpace.getInstance(ColorSpace.CS_sRGB);
return new DirectColorModel(cs, 32,
0xff0000, 0xff00, 0xff, 0xff000000,
true, DataBuffer.TYPE_INT);
default:
return null;
}
}
/**
* Returns the custom buffered image, which mostly identical to
* BufferedImage.(w,h,TYPE_3BYTE_BGR), but uses the bigger scanlineStride.
* This means that the raster will have gaps, between the rows.
*/
private static BufferedImage makeCustomManagedBI() {
int w = 511, h = 255;
ColorSpace cs = ColorSpace.getInstance(ColorSpace.CS_sRGB);
int[] nBits = {8, 8, 8};
int[] bOffs = {2, 1, 0};
ColorModel colorModel = new ComponentColorModel(cs, nBits, false, false,
Transparency.OPAQUE,
DataBuffer.TYPE_BYTE);
WritableRaster raster =
Raster.createInterleavedRaster(DataBuffer.TYPE_BYTE, w, h,
w * 3 + 2, 3, bOffs, null);
BufferedImage bi = new BufferedImage(colorModel, raster, true, null);
SunWritableRaster.makeTrackable(raster.getDataBuffer());
SunWritableRaster.markDirty(bi);
return bi;
}
public Image makeImage(TestEnvironment env, int w, int h) {
BufferedImage img = new BufferedImage(w, h, type);
if (unmanaged) {
DataBuffer db = img.getRaster().getDataBuffer();
if (db instanceof DataBufferInt) {
((DataBufferInt)db).getData();
} else if (db instanceof DataBufferShort) {
((DataBufferShort)db).getData();
} else if (db instanceof DataBufferByte) {
((DataBufferByte)db).getData();
} else {
try {
img.setAccelerationPriority(0.0f);
} catch (Throwable e) {}
}
}
return img;
}
private static BufferedImage makeUnmanagedBI() {
final BufferedImage bi = new BufferedImage(500, 200, TYPE_INT_ARGB);
final DataBuffer db = bi.getRaster().getDataBuffer();
if (db instanceof DataBufferInt) {
((DataBufferInt) db).getData();
} else if (db instanceof DataBufferShort) {
((DataBufferShort) db).getData();
} else if (db instanceof DataBufferByte) {
((DataBufferByte) db).getData();
} else {
try {
bi.setAccelerationPriority(0.0f);
} catch (final Throwable ignored) {
}
}
return bi;
}
public static int getElementSize(SampleModel sm) {
int elementSize = DataBuffer.getDataTypeSize(sm.getDataType());
if (sm instanceof MultiPixelPackedSampleModel) {
MultiPixelPackedSampleModel mppsm =
(MultiPixelPackedSampleModel)sm;
return mppsm.getSampleSize(0) * mppsm.getNumBands();
} else if (sm instanceof ComponentSampleModel) {
return sm.getNumBands() * elementSize;
} else if (sm instanceof SinglePixelPackedSampleModel) {
return elementSize;
}
return elementSize * sm.getNumBands();
}
private static BufferedImage makeUnmanagedBI(GraphicsConfiguration gc,
int type) {
BufferedImage img = gc.createCompatibleImage(SIZE, SIZE, type);
Graphics2D g2d = img.createGraphics();
g2d.setColor(RGB);
g2d.fillRect(0, 0, SIZE, SIZE);
g2d.dispose();
final DataBuffer db = img.getRaster().getDataBuffer();
if (db instanceof DataBufferInt) {
((DataBufferInt) db).getData();
} else if (db instanceof DataBufferShort) {
((DataBufferShort) db).getData();
} else if (db instanceof DataBufferByte) {
((DataBufferByte) db).getData();
} else {
try {
img.setAccelerationPriority(0.0f);
} catch (final Throwable ignored) {
}
}
return img;
}
public static long getBandSize(SampleModel sm) {
int elementSize = DataBuffer.getDataTypeSize(sm.getDataType());
if (sm instanceof ComponentSampleModel) {
ComponentSampleModel csm = (ComponentSampleModel)sm;
int pixelStride = csm.getPixelStride();
int scanlineStride = csm.getScanlineStride();
long size = Math.min(pixelStride, scanlineStride);
if (pixelStride > 0)
size += pixelStride * (sm.getWidth() - 1);
if (scanlineStride > 0)
size += scanlineStride * (sm.getHeight() - 1);
return size * ((elementSize + 7) / 8);
} else
return getTileSize(sm);
}
@Override
public ColorModel getColorModel(int transparency) {
switch (transparency) {
case Transparency.OPAQUE:
// REMIND: once the ColorModel spec is changed, this should be
// an opaque premultiplied DCM...
return new DirectColorModel(24, 0xff0000, 0xff00, 0xff);
case Transparency.BITMASK:
return new DirectColorModel(25, 0xff0000, 0xff00, 0xff, 0x1000000);
case Transparency.TRANSLUCENT:
ColorSpace cs = ColorSpace.getInstance(ColorSpace.CS_sRGB);
return new DirectColorModel(cs, 32,
0xff0000, 0xff00, 0xff, 0xff000000,
true, DataBuffer.TYPE_INT);
default:
return null;
}
}
public static int getElementSize(SampleModel sm) {
int elementSize = DataBuffer.getDataTypeSize(sm.getDataType());
if (sm instanceof MultiPixelPackedSampleModel) {
MultiPixelPackedSampleModel mppsm =
(MultiPixelPackedSampleModel)sm;
return mppsm.getSampleSize(0) * mppsm.getNumBands();
} else if (sm instanceof ComponentSampleModel) {
return sm.getNumBands() * elementSize;
} else if (sm instanceof SinglePixelPackedSampleModel) {
return elementSize;
}
return elementSize * sm.getNumBands();
}
/**
* Returns true if the data read was originally signed in the specified
* component, false if not. This method always returns false since PPM
* data is always unsigned.
*
* @param c The index of the component, from 0 to N-1.
*
* @return always false, since PPM data is always unsigned.
* */
public boolean isOrigSigned(int c) {
if (isBinary) return true;
// Check component index
SampleModel sm = null;
if (inputIsRaster)
sm = raster.getSampleModel();
else
sm = src.getSampleModel();
if (sm.getDataType() == DataBuffer.TYPE_USHORT ||
sm.getDataType() == DataBuffer.TYPE_BYTE)
return false;
return true;
}
public static void main(String args[]) {
BufferedImage src
= new BufferedImage(1, 10, BufferedImage.TYPE_INT_ARGB);
// Set src pixel values
Color pelColor = new Color(100, 100, 100, 128);
for (int i = 0; i < 10; i++) {
src.setRGB(0, i, pelColor.getRGB());
}
ColorModel cm = new ComponentColorModel
(ColorSpace.getInstance(ColorSpace.CS_GRAY),
new int [] {8,8}, true,
src.getColorModel().isAlphaPremultiplied(),
Transparency.TRANSLUCENT,
DataBuffer.TYPE_BYTE);
SampleModel sm = new PixelInterleavedSampleModel
(DataBuffer.TYPE_BYTE, 100, 100, 2, 200,
new int [] { 0, 1 });
WritableRaster wr = Raster.createWritableRaster(sm, new Point(0,0));
BufferedImage dst =
new BufferedImage(cm, wr, cm.isAlphaPremultiplied(), null);
dst = dst.getSubimage(0, 0, 1, 10);
ColorConvertOp op = new ColorConvertOp(null);
op.filter(src, dst);
for (int i = 0; i < 10; i++) {
if (((dst.getRGB(0, i) >> 24) & 0xff) != 128) {
throw new RuntimeException(
"Incorrect destination alpha value.");
}
}
}
/** Compute the output image */
public void computeImage(Raster[] sources,
WritableRaster dest,
Rectangle destRect)
{
RasterFormatTag[] formatTags = getFormatTags();
Rectangle srcRect = mapDestRect(destRect, 0);
RasterAccessor srcAccessor =
new RasterAccessor(sources[0], srcRect, formatTags[0],
getSourceImage(0).getColorModel());
RasterAccessor dstAccessor =
new RasterAccessor(dest, destRect, formatTags[1],
getColorModel());
switch (srcAccessor.getDataType()) {
case DataBuffer.TYPE_BYTE:
byteLoop(srcAccessor,dstAccessor);
break;
default:
throw new RuntimeException(getClass().getName() +
" does not implement computeRect" +
" for short/int/float/double data");
}
if (dstAccessor.isDataCopy()) {
dstAccessor.clampDataArrays();
dstAccessor.copyDataToRaster();
}
}
/**
* Constructs a IntegerInterleavedRaster with the given SampleModel,
* DataBuffer, and parent. DataBuffer must be a DataBufferInt and
* SampleModel must be of type SinglePixelPackedSampleModel.
* When translated into the base Raster's
* coordinate system, aRegion must be contained by the base Raster.
* Origin is the coodinate in the new Raster's coordinate system of
* the origin of the base Raster. (The base Raster is the Raster's
* ancestor which has no parent.)
*
* Note that this constructor should generally be called by other
* constructors or create methods, it should not be used directly.
* @param sampleModel The SampleModel that specifies the layout.
* @param dataBuffer The DataBufferInt that contains the image data.
* @param aRegion The Rectangle that specifies the image area.
* @param origin The Point that specifies the origin.
* @param parent The parent (if any) of this raster.
*/
public IntegerInterleavedRaster(SampleModel sampleModel,
DataBuffer dataBuffer,
Rectangle aRegion,
Point origin,
IntegerInterleavedRaster parent){
super(sampleModel,dataBuffer,aRegion,origin,parent);
this.maxX = minX + width;
this.maxY = minY + height;
if (!(dataBuffer instanceof DataBufferInt)) {
throw new RasterFormatException("IntegerInterleavedRasters must have" +
"integer DataBuffers");
}
DataBufferInt dbi = (DataBufferInt)dataBuffer;
this.data = stealData(dbi, 0);
if (sampleModel instanceof SinglePixelPackedSampleModel) {
SinglePixelPackedSampleModel sppsm =
(SinglePixelPackedSampleModel)sampleModel;
this.scanlineStride = sppsm.getScanlineStride();
this.pixelStride = 1;
this.dataOffsets = new int[1];
this.dataOffsets[0] = dbi.getOffset();
this.bandOffset = this.dataOffsets[0];
int xOffset = aRegion.x - origin.x;
int yOffset = aRegion.y - origin.y;
dataOffsets[0] += xOffset+yOffset*scanlineStride;
this.numDataElems = sppsm.getNumDataElements();
} else {
throw new RasterFormatException("IntegerInterleavedRasters must have"+
" SinglePixelPackedSampleModel");
}
verify();
}
@Test
@Category(UnitTest.class)
public void toRasterShort() throws IOException
{
MrGeoRaster numberedShort = TestUtils.createNumberedRaster(width, height, DataBuffer.TYPE_SHORT);
Raster raster = numberedShort.toRaster();
compareRaster(numberedShort, raster);
}
/**
* Constructs a ShortComponentRaster with the given SampleModel
* and DataBuffer. The Raster's upper left corner is origin and
* it is the same sizes the SampleModel. The DataBuffer is not
* initialized and must be a DataBufferUShort compatible with SampleModel.
* SampleModel must be of type ComponentSampleModel or
* SinglePixelPackedSampleModel.
* @param sampleModel The SampleModel that specifies the layout.
* @param dataBuffer The DataBufferUShort that contains the image data.
* @param origin The Point that specifies the origin.
*/
public ShortComponentRaster(SampleModel sampleModel,
DataBuffer dataBuffer,
Point origin) {
this(sampleModel,
dataBuffer,
new Rectangle(origin.x,
origin.y,
sampleModel.getWidth(),
sampleModel.getHeight()),
origin,
null);
}
private void compareRaster(MrGeoRaster mrgeoraster, Raster raster)
{
boolean intish = mrgeoraster.datatype() == DataBuffer.TYPE_BYTE || mrgeoraster.datatype() == DataBuffer.TYPE_INT
|| mrgeoraster.datatype() == DataBuffer.TYPE_SHORT || mrgeoraster.datatype() == DataBuffer.TYPE_USHORT;
for (int b = 0; b < mrgeoraster.bands(); b++)
{
for (int y = 0; y < mrgeoraster.height(); y++)
{
for (int x = 0; x < mrgeoraster.width(); x++)
{
float v1 = mrgeoraster.getPixelFloat(x, y, b);
float v2 = raster.getSampleFloat(x, y, b);
if (Float.isNaN(v1) != Float.isNaN(v2))
{
org.junit.Assert.assertEquals("Pixel NaN mismatch: px: " + x + " py: " + y
+ " b: " + b + " v1: " + v1 + " v2: " + v2, v1, v2, 0);
}
// make delta something reasonable relative to the data
//NOTE: this formula is not very reliable. An error of 2e-3f for pixel v1=1 fails, but passes for v1=2.
float delta = intish ? 1.0001f : Math.max(Math.abs(v1 * 1e-3f), 1e-3f);
org.junit.Assert.assertEquals("Pixel value mismatch: px: " + x + " py: " + y
+ " b: " + b + " v1: " + v1 + " v2: " + v2, v1, v2, delta);
}
}
}
}
private static void doTest(int dataType) {
int maxSize = DataBuffer.getDataTypeSize(dataType);
System.out.println("Type size: " + maxSize);
int theMask = (int)(1L << (maxSize + 2)) - 1;
System.out.printf("theMask=%x\n", theMask);
SinglePixelPackedSampleModel sm =
new SinglePixelPackedSampleModel(dataType, w, h,
new int[] { theMask });
int[] sampleSize = sm.getSampleSize();
for (int s : sampleSize) {
if (s > maxSize) {
throw new RuntimeException("Test failed: sample size is too big:" + s);
}
}
System.out.println("Test medialib...");
DataBuffer buf = createDataBuffer(dataType);
WritableRaster wr = Raster.createWritableRaster(sm, buf, null);
op.filter(wr, null);
System.out.println("Test PASSED.");
}
public static void fillDCM(DataBuffer data, SampleModel sm, int csType,
int c1Bits, int c2Bits, int c3Bits)
{
int [] pixel;
pixel = new int[4];
for (int i = 0; i < WIDTH; i++) {
for (int j = 0; j < HEIGHT; j++) {
pixel[0] = i >> (8 - c1Bits);
pixel[1] = j >> (8 - c2Bits);
pixel[2] = ((i + j)>>1) >> (8 - c3Bits);
pixel[3] = 0xFF;
sm.setPixel(i, j, pixel, data);
}
}
}
/**
* Constructs a IntegerInterleavedRaster with the given SampleModel,
* DataBuffer, and parent. DataBuffer must be a DataBufferInt and
* SampleModel must be of type SinglePixelPackedSampleModel.
* When translated into the base Raster's
* coordinate system, aRegion must be contained by the base Raster.
* Origin is the coodinate in the new Raster's coordinate system of
* the origin of the base Raster. (The base Raster is the Raster's
* ancestor which has no parent.)
*
* Note that this constructor should generally be called by other
* constructors or create methods, it should not be used directly.
* @param sampleModel The SampleModel that specifies the layout.
* @param dataBuffer The DataBufferInt that contains the image data.
* @param aRegion The Rectangle that specifies the image area.
* @param origin The Point that specifies the origin.
* @param parent The parent (if any) of this raster.
*/
public IntegerInterleavedRaster(SampleModel sampleModel,
DataBuffer dataBuffer,
Rectangle aRegion,
Point origin,
IntegerInterleavedRaster parent){
super(sampleModel,dataBuffer,aRegion,origin,parent);
this.maxX = minX + width;
this.maxY = minY + height;
if (!(dataBuffer instanceof DataBufferInt)) {
throw new RasterFormatException("IntegerInterleavedRasters must have" +
"integer DataBuffers");
}
DataBufferInt dbi = (DataBufferInt)dataBuffer;
this.data = stealData(dbi, 0);
if (sampleModel instanceof SinglePixelPackedSampleModel) {
SinglePixelPackedSampleModel sppsm =
(SinglePixelPackedSampleModel)sampleModel;
this.scanlineStride = sppsm.getScanlineStride();
this.pixelStride = 1;
this.dataOffsets = new int[1];
this.dataOffsets[0] = dbi.getOffset();
this.bandOffset = this.dataOffsets[0];
int xOffset = aRegion.x - origin.x;
int yOffset = aRegion.y - origin.y;
dataOffsets[0] += xOffset+yOffset*scanlineStride;
this.numDataElems = sppsm.getNumDataElements();
} else {
throw new RasterFormatException("IntegerInterleavedRasters must have"+
" SinglePixelPackedSampleModel");
}
verify();
}
private static void doTest(int dataType) {
int maxSize = DataBuffer.getDataTypeSize(dataType);
System.out.println("Type size: " + maxSize);
int theMask = (int)(1L << (maxSize + 2)) - 1;
System.out.printf("theMask=%x\n", theMask);
SinglePixelPackedSampleModel sm =
new SinglePixelPackedSampleModel(dataType, w, h,
new int[] { theMask });
int[] sampleSize = sm.getSampleSize();
for (int s : sampleSize) {
if (s > maxSize) {
throw new RuntimeException("Test failed: sample size is too big:" + s);
}
}
System.out.println("Test medialib...");
DataBuffer buf = createDataBuffer(dataType);
WritableRaster wr = Raster.createWritableRaster(sm, buf, null);
op.filter(wr, null);
System.out.println("Test PASSED.");
}
protected static ComponentColorModel createBitmaskColorModel() {
ComponentColorModel cm =
new ComponentColorModel(ColorSpace.getInstance(ColorSpace.CS_sRGB),
true, false, Transparency.BITMASK,
DataBuffer.TYPE_BYTE);
return cm;
}
@Override
public DataBuffer convert(FieldAccessor fa, Instance instance) throws FieldAccessor.InvalidFieldException {
int size = fa.getInt(instance, "size"); // NOI18N
int[] offsets = fa.getIntArray(instance, "offsets", false); // NOI18N
byte[][] bankdata = fa.getByteArray2(instance, "bankdata", false); // NOI18N
return new DataBufferByte(bankdata, size, offsets);
}
/**
* Constructs a IntegerInterleavedRaster with the given SampleModel
* and DataBuffer. The Raster's upper left corner is origin and
* it is the same sizes the SampleModel. The DataBuffer is not
* initialized and must be a DataBufferInt compatible with SampleModel.
* SampleModel must be of type SinglePixelPackedSampleModel.
* @param sampleModel The SampleModel that specifies the layout.
* @param dataBuffer The DataBufferInt that contains the image data.
* @param origin The Point that specifies the origin.
*/
public IntegerInterleavedRaster(SampleModel sampleModel,
DataBuffer dataBuffer,
Point origin) {
this(sampleModel,
dataBuffer,
new Rectangle(origin.x,
origin.y,
sampleModel.getWidth(),
sampleModel.getHeight()),
origin,
null);
}
/**
* Constructs a IntegerInterleavedRaster with the given SampleModel,
* DataBuffer, and parent. DataBuffer must be a DataBufferInt and
* SampleModel must be of type SinglePixelPackedSampleModel.
* When translated into the base Raster's
* coordinate system, aRegion must be contained by the base Raster.
* Origin is the coodinate in the new Raster's coordinate system of
* the origin of the base Raster. (The base Raster is the Raster's
* ancestor which has no parent.)
*
* Note that this constructor should generally be called by other
* constructors or create methods, it should not be used directly.
* @param sampleModel The SampleModel that specifies the layout.
* @param dataBuffer The DataBufferInt that contains the image data.
* @param aRegion The Rectangle that specifies the image area.
* @param origin The Point that specifies the origin.
* @param parent The parent (if any) of this raster.
*/
public IntegerInterleavedRaster(SampleModel sampleModel,
DataBuffer dataBuffer,
Rectangle aRegion,
Point origin,
IntegerInterleavedRaster parent){
super(sampleModel,dataBuffer,aRegion,origin,parent);
this.maxX = minX + width;
this.maxY = minY + height;
if (!(dataBuffer instanceof DataBufferInt)) {
throw new RasterFormatException("IntegerInterleavedRasters must have" +
"integer DataBuffers");
}
DataBufferInt dbi = (DataBufferInt)dataBuffer;
this.data = stealData(dbi, 0);
if (sampleModel instanceof SinglePixelPackedSampleModel) {
SinglePixelPackedSampleModel sppsm =
(SinglePixelPackedSampleModel)sampleModel;
this.scanlineStride = sppsm.getScanlineStride();
this.pixelStride = 1;
this.dataOffsets = new int[1];
this.dataOffsets[0] = dbi.getOffset();
this.bandOffset = this.dataOffsets[0];
int xOffset = aRegion.x - origin.x;
int yOffset = aRegion.y - origin.y;
dataOffsets[0] += xOffset+yOffset*scanlineStride;
this.numDataElems = sppsm.getNumDataElements();
} else {
throw new RasterFormatException("IntegerInterleavedRasters must have"+
" SinglePixelPackedSampleModel");
}
verify();
}
/**
* Constructs a ByteBandedRaster with the given sampleModel,
* DataBuffer, and parent. DataBuffer must be a DataBufferShort and
* SampleModel must be of type BandedSampleModel.
* When translated into the base Raster's
* coordinate system, aRegion must be contained by the base Raster.
* Origin is the coordinate in the new Raster's coordinate system of
* the origin of the base Raster. (The base Raster is the Raster's
* ancestor which has no parent.)
*
* Note that this constructor should generally be called by other
* constructors or create methods, it should not be used directly.
* @param sampleModel The SampleModel that specifies the layout.
* @param dataBuffer The DataBufferShort that contains the image data.
* @param aRegion The Rectangle that specifies the image area.
* @param origin The Point that specifies the origin.
* @param parent The parent (if any) of this raster.
*/
public ByteBandedRaster(SampleModel sampleModel,
DataBuffer dataBuffer,
Rectangle aRegion,
Point origin,
ByteBandedRaster parent) {
super(sampleModel, dataBuffer, aRegion, origin, parent);
this.maxX = minX + width;
this.maxY = minY + height;
if (!(dataBuffer instanceof DataBufferByte)) {
throw new RasterFormatException("ByteBandedRaster must have" +
"byte DataBuffers");
}
DataBufferByte dbb = (DataBufferByte)dataBuffer;
if (sampleModel instanceof BandedSampleModel) {
BandedSampleModel bsm = (BandedSampleModel)sampleModel;
this.scanlineStride = bsm.getScanlineStride();
int bankIndices[] = bsm.getBankIndices();
int bandOffsets[] = bsm.getBandOffsets();
int dOffsets[] = dbb.getOffsets();
dataOffsets = new int[bankIndices.length];
data = new byte[bankIndices.length][];
int xOffset = aRegion.x - origin.x;
int yOffset = aRegion.y - origin.y;
for (int i = 0; i < bankIndices.length; i++) {
data[i] = stealData(dbb, bankIndices[i]);
dataOffsets[i] = dOffsets[bankIndices[i]] +
xOffset + yOffset*scanlineStride + bandOffsets[i];
}
} else {
throw new RasterFormatException("ByteBandedRasters must have"+
"BandedSampleModels");
}
verify();
}
private static BufferedImage createCustomBuffer() {
ColorSpace cs = ColorSpace.getInstance(ColorSpace.CS_sRGB);
ColorModel cm = new ComponentColorModel(cs, false, false,
Transparency.OPAQUE, DataBuffer.TYPE_FLOAT);
WritableRaster wr = cm.createCompatibleWritableRaster(width, height);
return new BufferedImage(cm, wr, false, null);
}