/*
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation. Oracle designates this
* particular file as subject to the "Classpath" exception as provided
* by Oracle in the LICENSE file that accompanied this code.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
/**
* This class defines a Raster with pixels consisting of multiple
* 8-bit samples stored in possibly separate arrays for each band.
* Operations on sets of pixels are performed on a given band in the
* Raster before moving on to the next band. The arrays used for
* storage may be distinct or shared between some or all of the bands.
* Each band additionally has an offset that is added to determine the
* DataBuffer location of each pixel.
*
* There is only one scanline stride for all bands. The pixel stride
* is always equal to one. This type of raster can be used with a
* ComponentColorModel. This class requires a BandedSampleModel.
*
*/
/** Data offsets for each band of image data. */
int[] dataOffsets;
/** Scanline stride of the image data contained in this Raster. */
int scanlineStride;
/** The image data array. */
byte[][] data;
/** A cached copy of minX + width for use in bounds checks. */
private int maxX;
/** A cached copy of minY + height for use in bounds checks. */
private int maxY;
/**
* Constructs a ByteBandedRaster 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 specifies the origin.
*/
this(sampleModel,
origin.y,
sampleModel.getHeight()),
null);
}
/**
* Constructs a ByteBanded Raster 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 DataBufferShort compatible with SampleModel.
* SampleModel must be of type BandedSampleModel.
* @param sampleModel The SampleModel that specifies the layout.
* @param dataBuffer The DataBufferShort that contains the image data.
* @param origin The Point that specifies the origin.
*/
this(sampleModel, dataBuffer,
sampleModel.getHeight()),
}
/**
* 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.
*/
if (!(dataBuffer instanceof DataBufferByte)) {
throw new RasterFormatException("ByteBandedRaster must have" +
"byte DataBuffers");
}
if (sampleModel instanceof BandedSampleModel) {
}
} else {
throw new RasterFormatException("ByteBandedRasters must have"+
"BandedSampleModels");
}
verify();
}
/**
* Returns a copy of the data offsets array. For each band the data
* offset is the index into the band's data array, of the first sample
* of the band.
*/
public int[] getDataOffsets() {
return (int[])dataOffsets.clone();
}
/**
* Returns data offset for the specified band. The data offset
* is the index into the band's data array
* in which the first sample of the first scanline is stored.
* @param The band whose offset is returned.
*/
return dataOffsets[band];
}
/**
* Returns the scanline stride -- the number of data array elements
* between a given sample and the sample in the same column
* of the next row in the same band.
*/
public int getScanlineStride() {
return scanlineStride;
}
/**
* Returns the pixel stride, which is always equal to one for
* a Raster with a BandedSampleModel.
*/
public int getPixelStride() {
return 1;
}
/**
* Returns a reference to the entire data array.
*/
public byte[][] getDataStorage() {
return data;
}
/**
* Returns a reference to the specific band data array.
*/
}
/**
* Returns the data elements for all bands at the specified
* location.
* An ArrayIndexOutOfBounds exception will be thrown at runtime
* if the pixel coordinate is out of bounds.
* A ClassCastException will be thrown if the input object is non null
* and references anything other than an array of transferType.
* @param x The X coordinate of the pixel location.
* @param y The Y coordinate of the pixel location.
* @param outData An object reference to an array of type defined by
* getTransferType() and length getNumDataElements().
* If null an array of appropriate type and size will be
* allocated.
* @return An object reference to an array of type defined by
* getTransferType() with the request pixel data.
*/
throw new ArrayIndexOutOfBoundsException
("Coordinate out of bounds!");
}
byte outData[];
outData = new byte[numDataElements];
} else {
}
}
return outData;
}
/**
* Returns an array of data elements from the specified
* rectangular region.
* An ArrayIndexOutOfBounds exception will be thrown at runtime
* if the pixel coordinates are out of bounds.
* A ClassCastException will be thrown if the input object is non null
* and references anything other than an array of transferType.
* <pre>
* byte[] bandData = (byte[])raster.getDataElement(x, y, w, h, null);
* int numDataElements = raster.getNumDataElements();
* byte[] pixel = new byte[numDataElements];
* // To find a data element at location (x2, y2)
* System.arraycopy(bandData, ((y2-y)*w + (x2-x))*numDataElements,
* pixel, 0, numDataElements);
* </pre>
* @param x The X coordinate of the upper left pixel location.
* @param y The Y coordinate of the upper left pixel location.
* @param width Width of the pixel rectangle.
* @param height Height of the pixel rectangle.
* @param outData An object reference to an array of type defined by
* getTransferType() and length w*h*getNumDataElements().
* If null an array of appropriate type and size will be
* allocated.
* @return An object reference to an array of type defined by
* getTransferType() with the request pixel data.
*/
throw new ArrayIndexOutOfBoundsException
("Coordinate out of bounds!");
}
byte outData[];
outData = new byte[numDataElements*w*h];
} else {
}
for (int c = 0; c < numDataElements; c++) {
int off = c;
int dataOffset = dataOffsets[c];
off += numDataElements;
}
}
}
return outData;
}
/**
* Returns a byte array of data elements from the specified rectangular
* region for the specified band.
* An ArrayIndexOutOfBounds exception will be thrown at runtime
* if the pixel coordinates are out of bounds.
* <pre>
* byte[] bandData = raster.getByteData(x, y, w, h, null);
* // To find the data element at location (x2, y2)
* byte bandElement = bandData[((y2-y)*w + (x2-x))];
* </pre>
* @param x The X coordinate of the upper left pixel location.
* @param y The Y coordinate of the upper left pixel location.
* @param width Width of the pixel rectangle.
* @param height Height of the pixel rectangle.
* @param band The band to return.
* @param outData If non-null, data elements for all bands
* at the specified location are returned in this array.
* @return Data array with data elements for all bands.
*/
public byte[] getByteData(int x, int y, int w, int h,
// Bounds check for 'band' will be performed automatically
throw new ArrayIndexOutOfBoundsException
("Coordinate out of bounds!");
}
outData = new byte[scanlineStride*h];
}
if (scanlineStride == w) {
} else {
int off = 0;
off += w;
}
}
return outData;
}
/**
* Returns a byte array of data elements from the specified rectangular
* region.
* An ArrayIndexOutOfBounds exception will be thrown at runtime
* if the pixel coordinates are out of bounds.
* <pre>
* byte[] bandData = raster.getByteData(x, y, w, h, null);
* int numDataElements = raster.getNumDataElements();
* byte[] pixel = new byte[numDataElements];
* // To find a data element at location (x2, y2)
* System.arraycopy(bandData, ((y2-y)*w + (x2-x))*numDataElements,
* pixel, 0, numDataElements);
* </pre>
* @param x The X coordinate of the upper left pixel location.
* @param y The Y coordinate of the upper left pixel location.
* @param width Width of the pixel rectangle.
* @param height Height of the pixel rectangle.
* @param outData If non-null, data elements for all bands
* at the specified location are returned in this array.
* @return Data array with data elements for all bands.
*/
throw new ArrayIndexOutOfBoundsException
("Coordinate out of bounds!");
}
}
for (int c = 0; c < numDataElements; c++) {
int off = c;
int dataOffset = dataOffsets[c];
// REMIND: Should keep track if dataoffsets are in a nice order
off += numDataElements;
}
}
}
return outData;
}
/**
* Stores the data elements for all bands at the specified location.
* An ArrayIndexOutOfBounds exception will be thrown at runtime
* if the pixel coordinate is out of bounds.
* A ClassCastException will be thrown if the input object is non null
* and references anything other than an array of transferType.
* @param x The X coordinate of the pixel location.
* @param y The Y coordinate of the pixel location.
* @param inData An object reference to an array of type defined by
* getTransferType() and length getNumDataElements()
* containing the pixel data to place at x,y.
*/
throw new ArrayIndexOutOfBoundsException
("Coordinate out of bounds!");
}
for (int i = 0; i < numDataElements; i++) {
}
markDirty();
}
/**
* Stores the Raster data at the specified location.
* An ArrayIndexOutOfBounds exception will be thrown at runtime
* if the pixel coordinate is out of bounds.
* @param x The X coordinate of the pixel location.
* @param y The Y coordinate of the pixel location.
* @param inRaster Raster of data to place at x,y location.
*/
throw new ArrayIndexOutOfBoundsException
("Coordinate out of bounds!");
}
}
/**
* Stores the Raster data at the specified location.
* @param dstX The absolute X coordinate of the destination pixel
* that will receive a copy of the upper-left pixel of the
* inRaster
* @param dstY The absolute Y coordinate of the destination pixel
* that will receive a copy of the upper-left pixel of the
* inRaster
* @param width The number of pixels to store horizontally
* @param height The number of pixels to store vertically
* @param inRaster Raster of data to place at x,y location.
*/
// Assume bounds checking has been performed previously
return;
}
// // REMIND: Do something faster!
// if (inRaster instanceof ByteBandedRaster) {
// }
// Grab one scanline at a time
}
}
/**
* Stores an array of data elements into the specified rectangular
* region.
* An ArrayIndexOutOfBounds exception will be thrown at runtime
* if the pixel coordinates are out of bounds.
* A ClassCastException will be thrown if the input object is non null
* and references anything other than an array of transferType.
* The data elements in the
* data array are assumed to be packed. That is, a data element
* for the nth band at location (x2, y2) would be found at:
* <pre>
* inData[((y2-y)*w + (x2-x))*numDataElements + n]
* </pre>
* @param x The X coordinate of the upper left pixel location.
* @param y The Y coordinate of the upper left pixel location.
* @param w Width of the pixel rectangle.
* @param h Height of the pixel rectangle.
* @param inData An object reference to an array of type defined by
* getTransferType() and length w*h*getNumDataElements()
* containing the pixel data to place between x,y and
* x+h, y+h.
*/
throw new ArrayIndexOutOfBoundsException
("Coordinate out of bounds!");
}
for (int c = 0; c < numDataElements; c++) {
int off = c;
int dataOffset = dataOffsets[c];
off += numDataElements;
}
}
}
markDirty();
}
/**
* Stores a byte array of data elements into the specified rectangular
* region.
* An ArrayIndexOutOfBounds exception will be thrown at runtime
* if the pixel coordinates are out of bounds.
* The data elements in the
* data array are assumed to be packed. That is, a data element
* for the nth band at location (x2, y2) would be found at:
* <pre>
* inData[((y2-y)*w + (x2-x))*numDataElements + n]
* </pre>
* @param x The X coordinate of the upper left pixel location.
* @param y The Y coordinate of the upper left pixel location.
* @param w Width of the pixel rectangle.
* @param h Height of the pixel rectangle.
* @param band The band to set.
* @param inData The data elements to be stored.
*/
public void putByteData(int x, int y, int w, int h,
// Bounds check for 'band' will be performed automatically
throw new ArrayIndexOutOfBoundsException
("Coordinate out of bounds!");
}
int xoff;
int off = 0;
int xstart;
int ystart;
if (scanlineStride == w) {
} else {
off += w;
}
}
markDirty();
}
/**
* Stores a byte array of data elements into the specified rectangular
* region.
* An ArrayIndexOutOfBounds exception will be thrown at runtime
* if the pixel coordinates are out of bounds.
* The data elements in the
* data array are assumed to be packed. That is, a data element
* for the nth band at location (x2, y2) would be found at:
* <pre>
* inData[((y2-y)*w + (x2-x))*numDataElements + n]
* </pre>
* @param x The X coordinate of the upper left pixel location.
* @param y The Y coordinate of the upper left pixel location.
* @param w Width of the pixel rectangle.
* @param h Height of the pixel rectangle.
* @param inData The data elements to be stored.
*/
throw new ArrayIndexOutOfBoundsException
("Coordinate out of bounds!");
}
for (int c = 0; c < numDataElements; c++) {
int off = c;
int dataOffset = dataOffsets[c];
off += numDataElements;
}
}
}
markDirty();
}
/**
* 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 of the subraster.
* @param height Height 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.
*/
int bandList[]) {
if (x < this.minX) {
throw new RasterFormatException("x lies outside raster");
}
if (y < this.minY) {
throw new RasterFormatException("y lies outside raster");
}
throw new RasterFormatException("(x + width) is outside raster") ;
}
throw new RasterFormatException("(y + height) is outside raster");
}
else
sm = sampleModel;
return new ByteBandedRaster(sm,
this);
}
/**
* Creates a 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.
*/
int bandList[]) {
}
/**
* Creates a Raster with the same layout but using a different
* width and height, and with new zeroed data arrays.
*/
if (w <= 0 || h <=0) {
throw new RasterFormatException("negative "+
((w <= 0) ? "width" : "height"));
}
}
/**
* Creates a Raster with the same layout and the same
* width and height, and with new zeroed data arrays. If
* the Raster is a subRaster, this will call
* createCompatibleRaster(width, height).
*/
}
/**
* Verify that the layout parameters are consistent with the data.
* Verifies whether the data buffer has enough data for the raster,
* taking into account offsets, after ensuring all offsets are >=0.
* @throws RasterFormatException if a problem is detected.
*/
private void verify() {
/* Need to re-verify the dimensions since a sample model may be
* specified to the constructor
*/
{
throw new RasterFormatException("Invalid raster dimension");
}
if (scanlineStride < 0 ||
{
// integer overflow
throw new RasterFormatException("Incorrect scanline stride: "
+ scanlineStride);
}
throw new RasterFormatException("Incorrect scanline stride: "
+ scanlineStride);
}
}
// Make sure data for Raster is in a legal range
if (dataOffsets[i] < 0) {
throw new RasterFormatException("Data offsets for band "+i+
"("+dataOffsets[i]+
") must be >= 0");
}
}
throw new RasterFormatException("Invalid raster dimension");
}
int maxIndex = 0;
int index;
for (int i=0; i < numDataElements; i++) {
throw new RasterFormatException("Invalid raster dimension");
}
}
}
throw new RasterFormatException("Data array too small "+
" and should be > "+
" )");
}
}
else {
for (int i=0; i < numDataElements; i++) {
throw new RasterFormatException("Data array too small "+
" and should be > "+
maxIndex+" )");
}
}
}
}
+ height
+" #bands "+numDataElements
}
}