/*
* 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 is useful for describing 1, 2, or 4 bit image data
* elements. This raster has one band whose pixels are packed
* together into individual bytes in a single byte array. This type
* of raster can be used with an IndexColorModel. This raster uses a
* MultiPixelPackedSampleModel.
*
*/
/** The data bit offset for each pixel. */
int dataBitOffset;
/** Scanline stride of the image data contained in this Raster. */
int scanlineStride;
/**
* The bit stride of a pixel, equal to the total number of bits
* required to store a pixel.
*/
int pixelBitStride;
/** The bit mask for extracting the pixel. */
int bitMask;
/** The image data array. */
byte[] data;
/** 8 minus the pixel bit stride. */
int shiftOffset;
int type;
/** 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;
static private native void initIDs();
static {
/* ensure that the necessary native libraries are loaded */
initIDs();
}
/**
* Constructs a BytePackedRaster 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
* MultiPixelPackedSampleModel.
* @param sampleModel The SampleModel that specifies the layout.
* @param origin The Point that specified the origin.
*/
this(sampleModel,
origin.y,
sampleModel.getHeight()),
null);
}
/**
* Constructs a BytePackedRaster 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 DataBufferByte compatible with SampleModel.
* SampleModel must be of type MultiPixelPackedSampleModel.
* @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,
origin.y,
sampleModel.getHeight()),
null);
}
/**
* Constructs a BytePackedRaster with the given SampleModel,
* DataBuffer, and parent. DataBuffer must be a DataBufferByte and
* SampleModel must be of type MultiPixelPackedSampleModel.
* 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.
*
* @exception RasterFormatException if the parameters do not conform
* to requirements of this Raster type.
*/
if (!(dataBuffer instanceof DataBufferByte)) {
throw new RasterFormatException("BytePackedRasters must have" +
"byte DataBuffers");
}
throw new
RasterFormatException("DataBuffer for BytePackedRasters"+
" must only have 1 bank.");
}
if (sampleModel instanceof MultiPixelPackedSampleModel) {
if (pixelBitStride != 1 &&
pixelBitStride != 2 &&
pixelBitStride != 4) {
throw new RasterFormatException
("BytePackedRasters must have a bit depth of 1, 2, or 4");
}
} else {
throw new RasterFormatException("BytePackedRasters must have"+
"MultiPixelPackedSampleModel");
}
verify(false);
}
/**
* Returns the data bit offset for the Raster. The data
* bit offset is the bit index into the data array element
* corresponding to the first sample of the first scanline.
*/
public int getDataBitOffset() {
return dataBitOffset;
}
/**
* 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.
*/
public int getScanlineStride() {
return scanlineStride;
}
/**
* Returns pixel bit stride -- the number of bits between two
* samples on the same scanline.
*/
public int getPixelBitStride() {
return pixelBitStride;
}
/**
* Returns a reference to the entire data array.
*/
public byte[] getDataStorage() {
return data;
}
/**
* Returns the data element 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 {
}
// Fix 4184283
return outData;
}
/**
* Returns the pixel data for the specified rectangle of pixels in a
* primitive array of type TransferType.
* For image data supported by the Java 2D API, this
* will be one of DataBuffer.TYPE_BYTE, DataBuffer.TYPE_USHORT, or
* DataBuffer.TYPE_INT. Data may be returned in a packed format,
* thus increasing efficiency for data transfers.
*
* An ArrayIndexOutOfBoundsException may be thrown
* if the coordinates are not in bounds.
* A ClassCastException will be thrown if the input object is non null
* and references anything other than an array of TransferType.
* @see java.awt.image.SampleModel#getDataElements(int, int, int, int, Object, DataBuffer)
* @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 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 requested pixel data.
*/
return getByteData(x, y, w, h, (byte[])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.getPixelData(x, y, w, h, null);
* int pixel;
* // To find a data element at location (x2, y2)
* pixel = 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 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 {
}
int pixbits = pixelBitStride;
int outindex = 0;
for (int j = 0; j < h; j++) {
for (int i = 0; i < w; i++) {
}
index += scanlineStride;
}
return outData;
}
/**
* Returns a byte array containing the specified data elements
* from the data array. The band index will be ignored.
* An ArrayIndexOutOfBounds exception will be thrown at runtime
* if the pixel coordinates are out of bounds.
* <pre>
* byte[] byteData = getByteData(x, y, band, w, h, null);
* // To find a data element at location (x2, y2)
* byte element = byteData[(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, is ignored.
* @param outData If non-null, data elements
* at the specified locations are returned in this array.
* @return Byte array with data elements.
*/
public byte[] getByteData(int x, int y, int w, int h,
return getByteData(x, y, w, h, outData);
}
/**
* Returns a byte array containing the specified data elements
* from the data array.
* An ArrayIndexOutOfBounds exception will be thrown at runtime
* if the pixel coordinates are out of bounds.
* <pre>
* byte[] byteData = raster.getByteData(x, y, w, h, null);
* byte pixel;
* // To find a data element at location (x2, y2)
* pixel = byteData[((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 outData If non-null, data elements
* at the specified locations are returned in this array.
* @return Byte array with data elements.
*/
throw new ArrayIndexOutOfBoundsException
("Coordinate out of bounds!");
}
outData = new byte[w * h];
}
int pixbits = pixelBitStride;
int outindex = 0;
for (int j = 0; j < h; j++) {
int element;
// Process initial portion of scanline
int i = 0;
i++;
}
// Process central portion of scanline 8 pixels at a time
switch (pixbits) {
case 1:
for (; i < w - 7; i += 8) {
bitnum += 8;
}
break;
case 2:
for (; i < w - 7; i += 8) {
bitnum += 16;
}
break;
case 4:
for (; i < w - 7; i += 8) {
bitnum += 32;
}
break;
}
// Process final portion of scanline
for (; i < w; i++) {
}
index += scanlineStride;
}
return outData;
}
/**
* Stores the data elements 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!");
}
markDirty();
}
/**
* Stores the Raster data at the specified location.
* An ArrayIndexOutOfBounds exception will be thrown at runtime
* if the pixel coordinates are 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.
*/
// Check if we can use fast code
if (!(inRaster instanceof BytePackedRaster) ||
super.setDataElements(x, y, inRaster);
return;
}
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 srcX The absolute X coordinate of the upper-left source
* pixel that will be copied into this Raster
* @param srcY The absolute Y coordinate of the upper-left source
* pixel that will be copied into this Raster
* @param width The number of pixels to store horizontally
* @param height The number of pixels to store vertically
* @param inRaster BytePackedRaster of data to place at x,y location.
*/
// Assume bounds checking has been performed previously
return;
}
int outscan = this.scanlineStride;
int outbit = (this.dataBitOffset +
// Check whether the same bit alignment is present in both
// Rasters; if so, we can copy whole bytes using
// System.arraycopy. If not, we must do a "funnel shift"
// where adjacent bytes contribute to each destination byte.
// copy is bit aligned
if (bitpos != 0) {
// Copy partial bytes on left
// Fix bug 4399076: previously had '8 - copybits' instead
// of 'bits - copybits'.
//
// Prior to the this expression, 'mask' has its rightmost
// 'bits' bits set to '1'. We want it to have a total
// of 'copybits' bits set, therefore we want to introduce
// 'bits - copybits' zeroes on the right.
}
for (int j = 0; j < height; j++) {
}
}
if (copybits >= 8) {
// Copy whole bytes
} else {
for (int j = 0; j < height; j++) {
}
}
}
if (copybits > 0) {
// Copy partial bytes on right
for (int j = 0; j < height; j++) {
}
}
} else {
// Unaligned case, see RFE #4284166
// Note that the code in that RFE is not correct
// Insert bits into the first byte of the output
// if either the starting bit position is not zero or
// we are writing fewer than 8 bits in total
// Fix mask if we're only writing a partial byte
}
for (int j = 0; j < height; j++) {
// Read two bytes from the source if possible
// Don't worry about going over a scanline boundary
// since any extra bits won't get used anyway
byte inData1 = (byte)0;
}
// Insert the new bits into the output
}
}
// Now we have outbit & 7 == 0 so we can write
// complete bytes for a while
// Make sure we have work to do in the central loop
// to avoid reading past the end of the scanline
if (copybits >= 8) {
for (int j = 0; j < height; j++) {
// Combine adjacent bytes while 8 or more bits left
for (int i = 0; i < copybytes; i++) {
++ibyte;
++obyte;
}
}
}
// Finish last byte
if (copybits > 0) {
for (int j = 0; j < height; j++) {
byte inData1 = (byte)0;
}
// Insert the new bits into the output
}
}
}
markDirty();
}
/**
* Copies pixels from Raster srcRaster to this WritableRaster.
* For each (x, y) address in srcRaster, the corresponding pixel
* is copied to address (x+dx, y+dy) in this WritableRaster,
* unless (x+dx, y+dy) falls outside the bounds of this raster.
* srcRaster must have the same number of bands as this WritableRaster.
* The copy is a simple copy of source samples to the corresponding
* destination samples. For details, see
* {@link WritableRaster#setRect(Raster)}.
*
* @param dx The X translation factor from src space to dst space
* of the copy.
* @param dy The Y translation factor from src space to dst space
* of the copy.
* @param srcRaster The Raster from which to copy pixels.
*/
// Check if we can use fast code
if (!(srcRaster instanceof BytePackedRaster) ||
return;
}
// Clip to this raster
}
}
}
}
}
/**
* 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
* at location (x2, y2) would be found at:
* <pre>
* inData[((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 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.
*/
putByteData(x, y, w, h, (byte[])obj);
}
/**
* Stores a byte array of data elements into the specified rectangular
* region. The band index will be ignored.
* 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
* at location (x2, y2) would be found at:
* <pre>
* inData[((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 w Width of the pixel rectangle.
* @param h Height of the pixel rectangle.
* @param band The band to set, is ignored.
* @param inData The data elements to be stored.
*/
public void putByteData(int x, int y, int w, int h,
putByteData(x, y, w, h, inData);
}
/**
* 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
* at location (x2, y2) would be found at:
* <pre>
* inData[((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 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!");
}
if (w == 0 || h == 0) {
return;
}
int pixbits = pixelBitStride;
int outindex = 0;
for (int j = 0; j < h; j++) {
int element;
// Process initial portion of scanline
int i = 0;
i++;
}
// Process central portion of scanline 8 pixels at a time
switch (pixbits) {
case 1:
for (; i < w - 7; i += 8) {
bitnum += 8;
}
break;
case 2:
for (; i < w - 7; i += 8) {
bitnum += 16;
}
break;
case 4:
for (; i < w - 7; i += 8) {
bitnum += 32;
}
break;
}
// Process final portion of scanline
for (; i < w; i++) {
}
index += scanlineStride;
}
markDirty();
}
/**
* Returns an int array containing all samples for a rectangle of pixels,
* one sample per array element.
* An ArrayIndexOutOfBoundsException may be thrown
* if the coordinates are not in bounds.
* @param x, y the coordinates of the upper-left pixel location
* @param w Width of the pixel rectangle
* @param h Height of the pixel rectangle
* @param iArray An optionally pre-allocated int array
* @return the samples for the specified rectangle of pixels.
*/
throw new ArrayIndexOutOfBoundsException
("Coordinate out of bounds!");
}
iArray = new int[w * h];
}
int pixbits = pixelBitStride;
int outindex = 0;
for (int j = 0; j < h; j++) {
int element;
// Process initial portion of scanline
int i = 0;
i++;
}
// Process central portion of scanline 8 pixels at a time
switch (pixbits) {
case 1:
for (; i < w - 7; i += 8) {
bitnum += 8;
}
break;
case 2:
for (; i < w - 7; i += 8) {
bitnum += 16;
}
break;
case 4:
for (; i < w - 7; i += 8) {
bitnum += 32;
}
break;
}
// Process final portion of scanline
for (; i < w; i++) {
}
index += scanlineStride;
}
return iArray;
}
/**
* Sets all samples for a rectangle of pixels from an int array containing
* one sample per array element.
* An ArrayIndexOutOfBoundsException may be thrown if the coordinates are
* not in bounds.
* @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 iArray The input int pixel array.
*/
throw new ArrayIndexOutOfBoundsException
("Coordinate out of bounds!");
}
int pixbits = pixelBitStride;
int outindex = 0;
for (int j = 0; j < h; j++) {
int element;
// Process initial portion of scanline
int i = 0;
i++;
}
// Process central portion of scanline 8 pixels at a time
switch (pixbits) {
case 1:
for (; i < w - 7; i += 8) {
bitnum += 8;
}
break;
case 2:
for (; i < w - 7; i += 8) {
bitnum += 16;
}
break;
case 4:
for (; i < w - 7; i += 8) {
bitnum += 32;
}
break;
}
// Process final portion of scanline
for (; i < w; i++) {
}
index += scanlineStride;
}
markDirty();
}
/**
* 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. Note that the subraster will reference the same
* DataBuffer as the parent raster, but using different offsets. The
* bandList is ignored.
* @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.
*/
bandList);
}
/**
* 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. The bandList is ignored.
* A translation to the subRaster may also be specified.
* Note that the subRaster will reference the same
* DataBuffer 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) {
if (x < this.minX) {
throw new RasterFormatException("x lies outside the raster");
}
if (y < this.minY) {
throw new RasterFormatException("y lies outside the raster");
}
throw new RasterFormatException("(x + width) is outside of Raster");
}
throw new RasterFormatException("(y + height) is outside of Raster");
}
}
else {
sm = sampleModel;
}
return new BytePackedRaster(sm,
this);
}
/**
* 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.
*/
}
/**
* Verify that the layout parameters are consistent with
* the data. If strictCheck
* is false, this method will check for ArrayIndexOutOfBounds conditions.
* If strictCheck is true, this method will check for additional error
* conditions such as line wraparound (width of a line greater than
* the scanline stride).
* @return String Error string, if the layout is incompatible with
* the data. Otherwise returns null.
*/
// Make sure data for Raster is in a legal range
if (dataBitOffset < 0) {
throw new RasterFormatException("Data offsets must be >= 0");
}
/* Need to re-verify the dimensions since a sample model may be
* specified to the constructor
*/
{
throw new RasterFormatException("Invalid raster dimension");
}
/*
* pixelBitstride was verified in constructor, so just make
* sure that it is safe to multiply it by width.
*/
throw new RasterFormatException("Invalid raster dimension");
}
if (scanlineStride < 0 ||
{
throw new RasterFormatException("Invalid scanline stride");
}
int lastbit = (dataBitOffset
+ pixelBitStride - 1);
throw new RasterFormatException("raster dimensions overflow " +
"array bounds");
}
if (strictCheck) {
if (height > 1) {
throw new RasterFormatException("data for adjacent" +
" scanlines overlaps");
}
}
}
}
+" #channels "+numBands
+" xOff = "+sampleModelTranslateX
+" yOff = "+sampleModelTranslateY);
}
}