下面列出了怎么用java.awt.image.DataBufferUShort的API类实例代码及写法,或者点击链接到github查看源代码。
public static Object getData(final DataBuffer db) {
if (db instanceof DataBufferByte) {
return ((DataBufferByte) db).getData();
} else if (db instanceof DataBufferUShort) {
return ((DataBufferUShort) db).getData();
} else if (db instanceof DataBufferShort) {
return ((DataBufferShort) db).getData();
} else if (db instanceof DataBufferInt) {
return ((DataBufferInt) db).getData();
} else if (db instanceof DataBufferFloat) {
return ((DataBufferFloat) db).getData();
} else if (db instanceof DataBufferDouble) {
return ((DataBufferDouble) db).getData();
} else {
throw new RuntimeException("Not found DataBuffer class !");
}
}
/** Extracts pixel data as arrays of unsigned shorts, one per channel. */
public static short[][] getShorts(final WritableRaster r, final int x,
final int y, final int w, final int h)
{
if (canUseBankDataDirectly(r, DataBuffer.TYPE_USHORT,
DataBufferUShort.class) && x == 0 && y == 0 && w == r.getWidth() && h == r
.getHeight())
{
return ((DataBufferUShort) r.getDataBuffer()).getBankData();
}
final int c = r.getNumBands();
final short[][] samples = new short[c][w * h];
final int[] buf = new int[w * h];
for (int i = 0; i < c; i++) {
r.getSamples(x, y, w, h, i, buf);
for (int j = 0; j < buf.length; j++)
samples[i][j] = (short) buf[j];
}
return samples;
}
public static DataBuffer clone(DataBuffer dataBuffer) {
if (dataBuffer instanceof DataBufferByte) {
return clone((DataBufferByte) dataBuffer);
} else if (dataBuffer instanceof DataBufferDouble) {
return clone((DataBufferDouble) dataBuffer);
} else if (dataBuffer instanceof DataBufferFloat) {
return clone((DataBufferFloat) dataBuffer);
} else if (dataBuffer instanceof DataBufferInt) {
return clone((DataBufferInt) dataBuffer);
} else if (dataBuffer instanceof DataBufferShort) {
return clone((DataBufferShort) dataBuffer);
} else if (dataBuffer instanceof DataBufferUShort) {
return clone((DataBufferUShort) dataBuffer);
} else {
throw new UnsupportedOperationException("Don't know how to clone " + dataBuffer.getClass().getName());
}
}
/**
* Constructs a ShortBandedRaster with the given SampleModel,
* DataBuffer, and parent. DataBuffer must be a DataBufferUShort 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 DataBufferUShort 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 ShortBandedRaster(SampleModel sampleModel,
DataBuffer dataBuffer,
Rectangle aRegion,
Point origin,
ShortBandedRaster parent) {
super(sampleModel, dataBuffer, aRegion, origin, parent);
this.maxX = minX + width;
this.maxY = minY + height;
if (!(dataBuffer instanceof DataBufferUShort)) {
throw new RasterFormatException("ShortBandedRaster must have " +
"ushort DataBuffers");
}
DataBufferUShort dbus = (DataBufferUShort)dataBuffer;
if (sampleModel instanceof BandedSampleModel) {
BandedSampleModel bsm = (BandedSampleModel)sampleModel;
this.scanlineStride = bsm.getScanlineStride();
int bankIndices[] = bsm.getBankIndices();
int bandOffsets[] = bsm.getBandOffsets();
int dOffsets[] = dbus.getOffsets();
dataOffsets = new int[bankIndices.length];
data = new short[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(dbus, bankIndices[i]);
dataOffsets[i] = dOffsets[bankIndices[i]] +
xOffset + yOffset*scanlineStride + bandOffsets[i];
}
} else {
throw new RasterFormatException("ShortBandedRasters must have "+
"BandedSampleModels");
}
verify();
}
/**
* Constructs a ShortBandedRaster with the given SampleModel,
* DataBuffer, and parent. DataBuffer must be a DataBufferUShort 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 DataBufferUShort 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 ShortBandedRaster(SampleModel sampleModel,
DataBuffer dataBuffer,
Rectangle aRegion,
Point origin,
ShortBandedRaster parent) {
super(sampleModel, dataBuffer, aRegion, origin, parent);
this.maxX = minX + width;
this.maxY = minY + height;
if (!(dataBuffer instanceof DataBufferUShort)) {
throw new RasterFormatException("ShortBandedRaster must have " +
"ushort DataBuffers");
}
DataBufferUShort dbus = (DataBufferUShort)dataBuffer;
if (sampleModel instanceof BandedSampleModel) {
BandedSampleModel bsm = (BandedSampleModel)sampleModel;
this.scanlineStride = bsm.getScanlineStride();
int bankIndices[] = bsm.getBankIndices();
int bandOffsets[] = bsm.getBandOffsets();
int dOffsets[] = dbus.getOffsets();
dataOffsets = new int[bankIndices.length];
data = new short[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(dbus, bankIndices[i]);
dataOffsets[i] = dOffsets[bankIndices[i]] +
xOffset + yOffset*scanlineStride + bandOffsets[i];
}
} else {
throw new RasterFormatException("ShortBandedRasters must have "+
"BandedSampleModels");
}
verify();
}
/**
* Constructs a ShortBandedRaster with the given SampleModel,
* DataBuffer, and parent. DataBuffer must be a DataBufferUShort 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 DataBufferUShort 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 ShortBandedRaster(SampleModel sampleModel,
DataBuffer dataBuffer,
Rectangle aRegion,
Point origin,
ShortBandedRaster parent) {
super(sampleModel, dataBuffer, aRegion, origin, parent);
this.maxX = minX + width;
this.maxY = minY + height;
if (!(dataBuffer instanceof DataBufferUShort)) {
throw new RasterFormatException("ShortBandedRaster must have " +
"ushort DataBuffers");
}
DataBufferUShort dbus = (DataBufferUShort)dataBuffer;
if (sampleModel instanceof BandedSampleModel) {
BandedSampleModel bsm = (BandedSampleModel)sampleModel;
this.scanlineStride = bsm.getScanlineStride();
int bankIndices[] = bsm.getBankIndices();
int bandOffsets[] = bsm.getBandOffsets();
int dOffsets[] = dbus.getOffsets();
dataOffsets = new int[bankIndices.length];
data = new short[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(dbus, bankIndices[i]);
dataOffsets[i] = dOffsets[bankIndices[i]] +
xOffset + yOffset*scanlineStride + bandOffsets[i];
}
} else {
throw new RasterFormatException("ShortBandedRasters must have "+
"BandedSampleModels");
}
verify();
}
/**
* Constructs a ShortBandedRaster with the given SampleModel,
* DataBuffer, and parent. DataBuffer must be a DataBufferUShort 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 DataBufferUShort 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 ShortBandedRaster(SampleModel sampleModel,
DataBuffer dataBuffer,
Rectangle aRegion,
Point origin,
ShortBandedRaster parent) {
super(sampleModel, dataBuffer, aRegion, origin, parent);
this.maxX = minX + width;
this.maxY = minY + height;
if (!(dataBuffer instanceof DataBufferUShort)) {
throw new RasterFormatException("ShortBandedRaster must have " +
"ushort DataBuffers");
}
DataBufferUShort dbus = (DataBufferUShort)dataBuffer;
if (sampleModel instanceof BandedSampleModel) {
BandedSampleModel bsm = (BandedSampleModel)sampleModel;
this.scanlineStride = bsm.getScanlineStride();
int bankIndices[] = bsm.getBankIndices();
int bandOffsets[] = bsm.getBandOffsets();
int dOffsets[] = dbus.getOffsets();
dataOffsets = new int[bankIndices.length];
data = new short[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(dbus, bankIndices[i]);
dataOffsets[i] = dOffsets[bankIndices[i]] +
xOffset + yOffset*scanlineStride + bandOffsets[i];
}
} else {
throw new RasterFormatException("ShortBandedRasters must have "+
"BandedSampleModels");
}
verify();
}
private static void testShortRaster() {
WritableRaster srcRaster, dstRaster;
short[] pixels =
{ 11, 12, 13, 14,
21, 22, 23, 24,
31, 32, 33, 34,
41, 42, 43, 44 };
DataBuffer db = new DataBufferUShort(pixels, pixels.length);
srcRaster =
Raster.createInterleavedRaster(db, 4, 4, 4, 1, offsets, null);
srcRaster = srcRaster.createWritableChild(1, 1, 3, 3, 0, 0, null);
dstRaster = rop.filter(srcRaster, null);
}
@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
short[][] bankdata = fa.getShortArray2(instance, "bankdata", false); // NOI18N
return new DataBufferUShort(bankdata, size, offsets);
}
/**
* Constructs a ShortBandedRaster with the given SampleModel,
* DataBuffer, and parent. DataBuffer must be a DataBufferUShort 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 DataBufferUShort 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 ShortBandedRaster(SampleModel sampleModel,
DataBuffer dataBuffer,
Rectangle aRegion,
Point origin,
ShortBandedRaster parent) {
super(sampleModel, dataBuffer, aRegion, origin, parent);
this.maxX = minX + width;
this.maxY = minY + height;
if (!(dataBuffer instanceof DataBufferUShort)) {
throw new RasterFormatException("ShortBandedRaster must have " +
"ushort DataBuffers");
}
DataBufferUShort dbus = (DataBufferUShort)dataBuffer;
if (sampleModel instanceof BandedSampleModel) {
BandedSampleModel bsm = (BandedSampleModel)sampleModel;
this.scanlineStride = bsm.getScanlineStride();
int bankIndices[] = bsm.getBankIndices();
int bandOffsets[] = bsm.getBandOffsets();
int dOffsets[] = dbus.getOffsets();
dataOffsets = new int[bankIndices.length];
data = new short[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(dbus, bankIndices[i]);
dataOffsets[i] = dOffsets[bankIndices[i]] +
xOffset + yOffset*scanlineStride + bandOffsets[i];
}
} else {
throw new RasterFormatException("ShortBandedRasters must have "+
"BandedSampleModels");
}
verify();
}
/**
* Constructs a ShortComponentRaster with the given SampleModel.
* The Raster's upper left corner is origin and it is the same
* size as the SampleModel. A DataBuffer large enough to describe the
* Raster is automatically created. SampleModel must be of type
* ComponentSampleModel or SinglePixelPackedSampleModel.
* @param sampleModel The SampleModel that specifies the layout.
* @param origin The Point that specified the origin.
*/
public ShortComponentRaster(SampleModel sampleModel, Point origin) {
this(sampleModel,
(DataBufferUShort) sampleModel.createDataBuffer(),
new Rectangle(origin.x,
origin.y,
sampleModel.getWidth(),
sampleModel.getHeight()),
origin,
null);
}
/**
* 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,
DataBufferUShort dataBuffer,
Point origin)
{
this(sampleModel,
dataBuffer,
new Rectangle(origin.x,
origin.y,
sampleModel.getWidth(),
sampleModel.getHeight()),
origin,
null);
}
/**
* Creates a Writable subRaster given a region of the Raster. The x and y
* coordinates specify the horizontal and vertical offsets
* from the upper-left corner of this Raster to the upper-left corner
* of the subRaster. A subset of the bands of the parent Raster may
* be specified. If this is null, then all the bands are present in the
* subRaster. A translation to the subRaster may also be specified.
* Note that the subRaster will reference the same
* DataBuffers as the parent Raster, but using different offsets.
* @param x X offset.
* @param y Y offset.
* @param width Width (in pixels) of the subraster.
* @param height Height (in pixels) of the subraster.
* @param x0 Translated X origin of the subraster.
* @param y0 Translated Y origin of the subraster.
* @param bandList Array of band indices.
* @exception RasterFormatException
* if the specified bounding box is outside of the parent Raster.
*/
public WritableRaster createWritableChild(int x, int y,
int width, int height,
int x0, int y0,
int[] bandList) {
if (x < this.minX) {
throw new RasterFormatException("x lies outside the raster");
}
if (y < this.minY) {
throw new RasterFormatException("y lies outside the raster");
}
if ((x+width < x) || (x+width > this.minX + this.width)) {
throw new RasterFormatException("(x + width) is outside of Raster");
}
if ((y+height < y) || (y+height > this.minY + this.height)) {
throw new RasterFormatException("(y + height) is outside of Raster");
}
SampleModel sm;
if (bandList != null)
sm = sampleModel.createSubsetSampleModel(bandList);
else
sm = sampleModel;
int deltaX = x0 - x;
int deltaY = y0 - y;
return new ShortComponentRaster(sm,
(DataBufferUShort) dataBuffer,
new Rectangle(x0, y0, width, height),
new Point(sampleModelTranslateX+deltaX,
sampleModelTranslateY+deltaY),
this);
}
/**
* Constructs a ShortInterleavedRaster with the given SampleModel.
* The Raster's upper left corner is origin and it is the same
* size as the SampleModel. A DataBuffer large enough to describe the
* Raster is automatically created. SampleModel must be of type
* PixelInterleavedSampleModel or SinglePixelPackedSampleModel.
* @param sampleModel The SampleModel that specifies the layout.
* @param origin The Point that specified the origin.
*/
public ShortInterleavedRaster(SampleModel sampleModel, Point origin) {
this(sampleModel,
(DataBufferUShort) sampleModel.createDataBuffer(),
new Rectangle(origin.x,
origin.y,
sampleModel.getWidth(),
sampleModel.getHeight()),
origin,
null);
}
/**
* Constructs a ShortInterleavedRaster 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 PixelInterleavedSampleModel 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 ShortInterleavedRaster(SampleModel sampleModel,
DataBufferUShort dataBuffer,
Point origin)
{
this(sampleModel,
dataBuffer,
new Rectangle(origin.x,
origin.y,
sampleModel.getWidth(),
sampleModel.getHeight()),
origin,
null);
}
/**
* Creates a Writable subRaster given a region of the Raster. The x and y
* coordinates specify the horizontal and vertical offsets
* from the upper-left corner of this Raster to the upper-left corner
* of the subRaster. A subset of the bands of the parent Raster may
* be specified. If this is null, then all the bands are present in the
* subRaster. A translation to the subRaster may also be specified.
* Note that the subRaster will reference the same
* DataBuffers as the parent Raster, but using different offsets.
* @param x X offset.
* @param y Y offset.
* @param width Width (in pixels) of the subraster.
* @param height Height (in pixels) of the subraster.
* @param x0 Translated X origin of the subraster.
* @param y0 Translated Y origin of the subraster.
* @param bandList Array of band indices.
* @exception RasterFormatException
* if the specified bounding box is outside of the parent Raster.
*/
public WritableRaster createWritableChild(int x, int y,
int width, int height,
int x0, int y0,
int[] bandList) {
if (x < this.minX) {
throw new RasterFormatException("x lies outside the raster");
}
if (y < this.minY) {
throw new RasterFormatException("y lies outside the raster");
}
if ((x+width < x) || (x+width > this.minX + this.width)) {
throw new RasterFormatException("(x + width) is outside of Raster");
}
if ((y+height < y) || (y+height > this.minY + this.height)) {
throw new RasterFormatException("(y + height) is outside of Raster");
}
SampleModel sm;
if (bandList != null)
sm = sampleModel.createSubsetSampleModel(bandList);
else
sm = sampleModel;
int deltaX = x0 - x;
int deltaY = y0 - y;
return new ShortInterleavedRaster(sm,
(DataBufferUShort) dataBuffer,
new Rectangle(x0, y0, width, height),
new Point(sampleModelTranslateX+deltaX,
sampleModelTranslateY+deltaY),
this);
}
/**
* Constructs a ShortBandedRaster with the given SampleModel.
* The Raster's upper left corner is origin and it is the same
* size as the SampleModel. A DataBuffer large enough to describe the
* Raster is automatically created. SampleModel must be of type
* BandedSampleModel.
* @param sampleModel The SampleModel that specifies the layout.
* @param origin The Point that specified the origin.
*/
public ShortBandedRaster(SampleModel sampleModel, Point origin) {
this(sampleModel,
(DataBufferUShort) sampleModel.createDataBuffer(),
new Rectangle(origin.x,
origin.y,
sampleModel.getWidth(),
sampleModel.getHeight()),
origin,
null);
}
/**
* Constructs a ShortBandedRaster with the given SampleModel
* and DataBuffer. The Raster's upper left corner is origin and
* it is the same size as the SampleModel. The DataBuffer is not
* initialized and must be a DataBufferUShort compatible with SampleModel.
* SampleModel must be of type BandedSampleModel.
* @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 ShortBandedRaster(SampleModel sampleModel,
DataBufferUShort dataBuffer,
Point origin)
{
this(sampleModel, dataBuffer,
new Rectangle(origin.x, origin.y,
sampleModel.getWidth(),
sampleModel.getHeight()),
origin, null);
}
/**
* Constructs a ShortBandedRaster with the given SampleModel,
* DataBuffer, and parent. DataBuffer must be a DataBufferUShort 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 DataBufferUShort 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 ShortBandedRaster(SampleModel sampleModel,
DataBufferUShort dataBuffer,
Rectangle aRegion,
Point origin,
ShortBandedRaster parent)
{
super(sampleModel, dataBuffer, aRegion, origin, parent);
this.maxX = minX + width;
this.maxY = minY + height;
if (sampleModel instanceof BandedSampleModel) {
BandedSampleModel bsm = (BandedSampleModel)sampleModel;
this.scanlineStride = bsm.getScanlineStride();
int[] bankIndices = bsm.getBankIndices();
int[] bandOffsets = bsm.getBandOffsets();
int[] dOffsets = dataBuffer.getOffsets();
dataOffsets = new int[bankIndices.length];
data = new short[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(dataBuffer, bankIndices[i]);
dataOffsets[i] = dOffsets[bankIndices[i]] +
xOffset + yOffset*scanlineStride + bandOffsets[i];
}
} else {
throw new RasterFormatException("ShortBandedRasters must have "+
"BandedSampleModels");
}
verify();
}
/**
* Creates a Writable subRaster given a region of the Raster. The x and y
* coordinates specify the horizontal and vertical offsets
* from the upper-left corner of this Raster to the upper-left corner
* of the subRaster. A subset of the bands of the parent Raster may
* be specified. If this is null, then all the bands are present in the
* subRaster. A translation to the subRaster may also be specified.
* Note that the subRaster will reference the same
* DataBuffers as the parent Raster, but using different offsets.
* @param x X offset.
* @param y Y offset.
* @param width Width (in pixels) of the subraster.
* @param height Height (in pixels) of the subraster.
* @param x0 Translated X origin of the subraster.
* @param y0 Translated Y origin of the subraster.
* @param bandList Array of band indices.
* @exception RasterFormatException
* if the specified bounding box is outside of the parent Raster.
*/
public WritableRaster createWritableChild(int x, int y,
int width, int height,
int x0, int y0,
int[] bandList) {
if (x < this.minX) {
throw new RasterFormatException("x lies outside raster");
}
if (y < this.minY) {
throw new RasterFormatException("y lies outside raster");
}
if ((x+width < x) || (x+width > this.minX + this.width)) {
throw new RasterFormatException("(x + width) is outside of Raster");
}
if ((y+height < y) || (y+height > this.minY + this.height)) {
throw new RasterFormatException("(y + height) is outside of Raster");
}
SampleModel sm;
if (bandList != null)
sm = sampleModel.createSubsetSampleModel(bandList);
else
sm = sampleModel;
int deltaX = x0 - x;
int deltaY = y0 - y;
return new ShortBandedRaster(sm,
(DataBufferUShort) dataBuffer,
new Rectangle(x0, y0, width, height),
new Point(sampleModelTranslateX+deltaX,
sampleModelTranslateY+deltaY),
this);
}
/**
* Constructs a ShortComponentRaster with the given SampleModel.
* The Raster's upper left corner is origin and it is the same
* size as the SampleModel. A DataBuffer large enough to describe the
* Raster is automatically created. SampleModel must be of type
* ComponentSampleModel or SinglePixelPackedSampleModel.
* @param sampleModel The SampleModel that specifies the layout.
* @param origin The Point that specified the origin.
*/
public ShortComponentRaster(SampleModel sampleModel, Point origin) {
this(sampleModel,
(DataBufferUShort) sampleModel.createDataBuffer(),
new Rectangle(origin.x,
origin.y,
sampleModel.getWidth(),
sampleModel.getHeight()),
origin,
null);
}
/**
* 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,
DataBufferUShort dataBuffer,
Point origin)
{
this(sampleModel,
dataBuffer,
new Rectangle(origin.x,
origin.y,
sampleModel.getWidth(),
sampleModel.getHeight()),
origin,
null);
}
/**
* Creates a Writable subRaster given a region of the Raster. The x and y
* coordinates specify the horizontal and vertical offsets
* from the upper-left corner of this Raster to the upper-left corner
* of the subRaster. A subset of the bands of the parent Raster may
* be specified. If this is null, then all the bands are present in the
* subRaster. A translation to the subRaster may also be specified.
* Note that the subRaster will reference the same
* DataBuffers as the parent Raster, but using different offsets.
* @param x X offset.
* @param y Y offset.
* @param width Width (in pixels) of the subraster.
* @param height Height (in pixels) of the subraster.
* @param x0 Translated X origin of the subraster.
* @param y0 Translated Y origin of the subraster.
* @param bandList Array of band indices.
* @exception RasterFormatException
* if the specified bounding box is outside of the parent Raster.
*/
public WritableRaster createWritableChild(int x, int y,
int width, int height,
int x0, int y0,
int[] bandList) {
if (x < this.minX) {
throw new RasterFormatException("x lies outside the raster");
}
if (y < this.minY) {
throw new RasterFormatException("y lies outside the raster");
}
if ((x+width < x) || (x+width > this.minX + this.width)) {
throw new RasterFormatException("(x + width) is outside of Raster");
}
if ((y+height < y) || (y+height > this.minY + this.height)) {
throw new RasterFormatException("(y + height) is outside of Raster");
}
SampleModel sm;
if (bandList != null)
sm = sampleModel.createSubsetSampleModel(bandList);
else
sm = sampleModel;
int deltaX = x0 - x;
int deltaY = y0 - y;
return new ShortComponentRaster(sm,
(DataBufferUShort) dataBuffer,
new Rectangle(x0, y0, width, height),
new Point(sampleModelTranslateX+deltaX,
sampleModelTranslateY+deltaY),
this);
}
/**
* Constructs a ShortInterleavedRaster with the given SampleModel.
* The Raster's upper left corner is origin and it is the same
* size as the SampleModel. A DataBuffer large enough to describe the
* Raster is automatically created. SampleModel must be of type
* PixelInterleavedSampleModel or SinglePixelPackedSampleModel.
* @param sampleModel The SampleModel that specifies the layout.
* @param origin The Point that specified the origin.
*/
public ShortInterleavedRaster(SampleModel sampleModel, Point origin) {
this(sampleModel,
(DataBufferUShort) sampleModel.createDataBuffer(),
new Rectangle(origin.x,
origin.y,
sampleModel.getWidth(),
sampleModel.getHeight()),
origin,
null);
}
/**
* Constructs a ShortInterleavedRaster 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 PixelInterleavedSampleModel 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 ShortInterleavedRaster(SampleModel sampleModel,
DataBufferUShort dataBuffer,
Point origin)
{
this(sampleModel,
dataBuffer,
new Rectangle(origin.x,
origin.y,
sampleModel.getWidth(),
sampleModel.getHeight()),
origin,
null);
}
/**
* Creates a Writable subRaster given a region of the Raster. The x and y
* coordinates specify the horizontal and vertical offsets
* from the upper-left corner of this Raster to the upper-left corner
* of the subRaster. A subset of the bands of the parent Raster may
* be specified. If this is null, then all the bands are present in the
* subRaster. A translation to the subRaster may also be specified.
* Note that the subRaster will reference the same
* DataBuffers as the parent Raster, but using different offsets.
* @param x X offset.
* @param y Y offset.
* @param width Width (in pixels) of the subraster.
* @param height Height (in pixels) of the subraster.
* @param x0 Translated X origin of the subraster.
* @param y0 Translated Y origin of the subraster.
* @param bandList Array of band indices.
* @exception RasterFormatException
* if the specified bounding box is outside of the parent Raster.
*/
public WritableRaster createWritableChild(int x, int y,
int width, int height,
int x0, int y0,
int[] bandList) {
if (x < this.minX) {
throw new RasterFormatException("x lies outside the raster");
}
if (y < this.minY) {
throw new RasterFormatException("y lies outside the raster");
}
if ((x+width < x) || (x+width > this.minX + this.width)) {
throw new RasterFormatException("(x + width) is outside of Raster");
}
if ((y+height < y) || (y+height > this.minY + this.height)) {
throw new RasterFormatException("(y + height) is outside of Raster");
}
SampleModel sm;
if (bandList != null)
sm = sampleModel.createSubsetSampleModel(bandList);
else
sm = sampleModel;
int deltaX = x0 - x;
int deltaY = y0 - y;
return new ShortInterleavedRaster(sm,
(DataBufferUShort) dataBuffer,
new Rectangle(x0, y0, width, height),
new Point(sampleModelTranslateX+deltaX,
sampleModelTranslateY+deltaY),
this);
}
/**
* Constructs a ShortBandedRaster with the given SampleModel.
* The Raster's upper left corner is origin and it is the same
* size as the SampleModel. A DataBuffer large enough to describe the
* Raster is automatically created. SampleModel must be of type
* BandedSampleModel.
* @param sampleModel The SampleModel that specifies the layout.
* @param origin The Point that specified the origin.
*/
public ShortBandedRaster(SampleModel sampleModel, Point origin) {
this(sampleModel,
(DataBufferUShort) sampleModel.createDataBuffer(),
new Rectangle(origin.x,
origin.y,
sampleModel.getWidth(),
sampleModel.getHeight()),
origin,
null);
}
/**
* Constructs a ShortBandedRaster with the given SampleModel
* and DataBuffer. The Raster's upper left corner is origin and
* it is the same size as the SampleModel. The DataBuffer is not
* initialized and must be a DataBufferUShort compatible with SampleModel.
* SampleModel must be of type BandedSampleModel.
* @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 ShortBandedRaster(SampleModel sampleModel,
DataBufferUShort dataBuffer,
Point origin)
{
this(sampleModel, dataBuffer,
new Rectangle(origin.x, origin.y,
sampleModel.getWidth(),
sampleModel.getHeight()),
origin, null);
}
/**
* Constructs a ShortBandedRaster with the given SampleModel,
* DataBuffer, and parent. DataBuffer must be a DataBufferUShort 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 DataBufferUShort 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 ShortBandedRaster(SampleModel sampleModel,
DataBufferUShort dataBuffer,
Rectangle aRegion,
Point origin,
ShortBandedRaster parent)
{
super(sampleModel, dataBuffer, aRegion, origin, parent);
this.maxX = minX + width;
this.maxY = minY + height;
if (sampleModel instanceof BandedSampleModel) {
BandedSampleModel bsm = (BandedSampleModel)sampleModel;
this.scanlineStride = bsm.getScanlineStride();
int bankIndices[] = bsm.getBankIndices();
int bandOffsets[] = bsm.getBandOffsets();
int dOffsets[] = dataBuffer.getOffsets();
dataOffsets = new int[bankIndices.length];
data = new short[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(dataBuffer, bankIndices[i]);
dataOffsets[i] = dOffsets[bankIndices[i]] +
xOffset + yOffset*scanlineStride + bandOffsets[i];
}
} else {
throw new RasterFormatException("ShortBandedRasters must have "+
"BandedSampleModels");
}
verify();
}
/**
* Creates a Writable subRaster given a region of the Raster. The x and y
* coordinates specify the horizontal and vertical offsets
* from the upper-left corner of this Raster to the upper-left corner
* of the subRaster. A subset of the bands of the parent Raster may
* be specified. If this is null, then all the bands are present in the
* subRaster. A translation to the subRaster may also be specified.
* Note that the subRaster will reference the same
* DataBuffers as the parent Raster, but using different offsets.
* @param x X offset.
* @param y Y offset.
* @param width Width (in pixels) of the subraster.
* @param height Height (in pixels) of the subraster.
* @param x0 Translated X origin of the subraster.
* @param y0 Translated Y origin of the subraster.
* @param bandList Array of band indices.
* @exception RasterFormatException
* if the specified bounding box is outside of the parent Raster.
*/
public WritableRaster createWritableChild(int x, int y,
int width, int height,
int x0, int y0,
int bandList[]) {
if (x < this.minX) {
throw new RasterFormatException("x lies outside raster");
}
if (y < this.minY) {
throw new RasterFormatException("y lies outside raster");
}
if ((x+width < x) || (x+width > this.minX + this.width)) {
throw new RasterFormatException("(x + width) is outside of Raster");
}
if ((y+height < y) || (y+height > this.minY + this.height)) {
throw new RasterFormatException("(y + height) is outside of Raster");
}
SampleModel sm;
if (bandList != null)
sm = sampleModel.createSubsetSampleModel(bandList);
else
sm = sampleModel;
int deltaX = x0 - x;
int deltaY = y0 - y;
return new ShortBandedRaster(sm,
(DataBufferUShort) dataBuffer,
new Rectangle(x0, y0, width, height),
new Point(sampleModelTranslateX+deltaX,
sampleModelTranslateY+deltaY),
this);
}