0N/A/*
2362N/A * Copyright (c) 1997, 2007, Oracle and/or its affiliates. All rights reserved.
0N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
0N/A *
0N/A * This code is free software; you can redistribute it and/or modify it
0N/A * under the terms of the GNU General Public License version 2 only, as
2362N/A * published by the Free Software Foundation. Oracle designates this
0N/A * particular file as subject to the "Classpath" exception as provided
2362N/A * by Oracle in the LICENSE file that accompanied this code.
0N/A *
0N/A * This code is distributed in the hope that it will be useful, but WITHOUT
0N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
0N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
0N/A * version 2 for more details (a copy is included in the LICENSE file that
0N/A * accompanied this code).
0N/A *
0N/A * You should have received a copy of the GNU General Public License version
0N/A * 2 along with this work; if not, write to the Free Software Foundation,
0N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
0N/A *
2362N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
2362N/A * or visit www.oracle.com if you need additional information or have any
2362N/A * questions.
0N/A */
0N/A
0N/Apackage sun.awt.image;
0N/Aimport java.awt.image.Raster;
0N/Aimport java.awt.image.WritableRaster;
0N/Aimport java.awt.image.RasterFormatException;
0N/Aimport java.awt.image.SampleModel;
0N/Aimport java.awt.image.ComponentSampleModel;
0N/Aimport java.awt.image.SinglePixelPackedSampleModel;
0N/Aimport java.awt.image.DataBuffer;
0N/Aimport java.awt.image.DataBufferByte;
0N/Aimport java.awt.Rectangle;
0N/Aimport java.awt.Point;
0N/A
0N/A/**
0N/A * This class defines a Raster with pixels consisting of one or more 8-bit
0N/A * data elements stored in close proximity to each other in a single byte
0N/A * array.
0N/A * The bit precision per data element is that
0N/A * of the data type (that is, the bit precision for this Raster is 8).
0N/A * There is only one pixel stride and one scanline stride for all
0N/A * bands. This type of Raster can be used with a
0N/A * ComponentColorModel if there are multiple bands, or an
0N/A * IndexColorModel if there is only one band.
0N/A * <p>
0N/A * For example, 3-3-2 RGB image data can be represented by a
0N/A * ByteComponentRaster using a SinglePixelPackedSampleModel and
0N/A * a ComponentColorModel.
0N/A *
0N/A */
0N/Apublic class ByteComponentRaster extends SunWritableRaster {
0N/A
0N/A /** private band offset for use by native code */
0N/A protected int bandOffset;
0N/A
0N/A /** Data offsets for each band of image data. */
0N/A protected int[] dataOffsets;
0N/A
0N/A /** Scanline stride of the image data contained in this Raster. */
0N/A protected int scanlineStride;
0N/A
0N/A /** Pixel stride of the image data contained in this Raster. */
0N/A protected int pixelStride;
0N/A
0N/A /** The image data array. */
0N/A protected byte[] data;
0N/A
0N/A int type;
0N/A
0N/A /** A cached copy of minX + width for use in bounds checks. */
0N/A private int maxX;
0N/A
0N/A /** A cached copy of minY + height for use in bounds checks. */
0N/A private int maxY;
0N/A
0N/A static private native void initIDs();
0N/A static {
0N/A /* ensure that the necessary native libraries are loaded */
0N/A NativeLibLoader.loadLibraries();
0N/A initIDs();
0N/A }
0N/A
0N/A /**
0N/A * Constructs a ByteComponentRaster with the given SampleModel.
0N/A * The Raster's upper left corner is origin and it is the same
0N/A * size as the SampleModel. A DataBuffer large enough to describe the
0N/A * Raster is automatically created. SampleModel must be of type
0N/A * SinglePixelPackedSampleModel or ComponentSampleModel.
0N/A * @param sampleModel The SampleModel that specifies the layout.
0N/A * @param origin The Point that specified the origin.
0N/A */
0N/A public ByteComponentRaster(SampleModel sampleModel, Point origin) {
0N/A this(sampleModel,
0N/A sampleModel.createDataBuffer(),
0N/A new Rectangle(origin.x,
0N/A origin.y,
0N/A sampleModel.getWidth(),
0N/A sampleModel.getHeight()),
0N/A origin,
0N/A null);
0N/A }
0N/A
0N/A /**
0N/A * Constructs a ByteComponentRaster with the given SampleModel
0N/A * and DataBuffer. The Raster's upper left corner is origin and
0N/A * it is the same size as the SampleModel. The DataBuffer is not
0N/A * initialized and must be a DataBufferByte compatible with SampleModel.
0N/A * SampleModel must be of type SinglePixelPackedSampleModel
0N/A * or ComponentSampleModel.
0N/A * @param sampleModel The SampleModel that specifies the layout.
0N/A * @param dataBuffer The DataBufferShort that contains the image data.
0N/A * @param origin The Point that specifies the origin.
0N/A */
0N/A public ByteComponentRaster(SampleModel sampleModel,
0N/A DataBuffer dataBuffer,
0N/A Point origin) {
0N/A this(sampleModel,
0N/A dataBuffer,
0N/A new Rectangle(origin.x,
0N/A origin.y,
0N/A sampleModel.getWidth(),
0N/A sampleModel.getHeight()),
0N/A origin,
0N/A null);
0N/A }
0N/A
0N/A /**
0N/A * Constructs a ByteComponentRaster with the given SampleModel,
0N/A * DataBuffer, and parent. DataBuffer must be a DataBufferByte and
0N/A * SampleModel must be of type SinglePixelPackedSampleModel
0N/A * or ComponentSampleModel.
0N/A * When translated into the base Raster's
0N/A * coordinate system, aRegion must be contained by the base Raster.
0N/A * Origin is the coordinate in the new Raster's coordinate system of
0N/A * the origin of the base Raster. (The base Raster is the Raster's
0N/A * ancestor which has no parent.)
0N/A *
0N/A * Note that this constructor should generally be called by other
0N/A * constructors or create methods, it should not be used directly.
0N/A * @param sampleModel The SampleModel that specifies the layout.
0N/A * @param dataBuffer The DataBufferShort that contains the image data.
0N/A * @param aRegion The Rectangle that specifies the image area.
0N/A * @param origin The Point that specifies the origin.
0N/A * @param parent The parent (if any) of this raster.
0N/A */
0N/A public ByteComponentRaster(SampleModel sampleModel,
0N/A DataBuffer dataBuffer,
0N/A Rectangle aRegion,
0N/A Point origin,
0N/A ByteComponentRaster parent) {
0N/A super(sampleModel, dataBuffer, aRegion, origin, parent);
0N/A this.maxX = minX + width;
0N/A this.maxY = minY + height;
0N/A
0N/A if (!(dataBuffer instanceof DataBufferByte)) {
0N/A throw new RasterFormatException("ByteComponentRasters must have " +
0N/A "byte DataBuffers");
0N/A }
0N/A
0N/A DataBufferByte dbb = (DataBufferByte)dataBuffer;
0N/A this.data = stealData(dbb, 0);
0N/A if (dbb.getNumBanks() != 1) {
0N/A throw new
0N/A RasterFormatException("DataBuffer for ByteComponentRasters"+
0N/A " must only have 1 bank.");
0N/A }
0N/A int dbOffset = dbb.getOffset();
0N/A
0N/A if (sampleModel instanceof ComponentSampleModel) {
0N/A ComponentSampleModel ism = (ComponentSampleModel)sampleModel;
0N/A this.type = IntegerComponentRaster.TYPE_BYTE_SAMPLES;
0N/A this.scanlineStride = ism.getScanlineStride();
0N/A this.pixelStride = ism.getPixelStride();
0N/A this.dataOffsets = ism.getBandOffsets();
0N/A int xOffset = aRegion.x - origin.x;
0N/A int yOffset = aRegion.y - origin.y;
0N/A for (int i = 0; i < getNumDataElements(); i++) {
0N/A dataOffsets[i] += dbOffset +
0N/A xOffset*pixelStride+yOffset*scanlineStride;
0N/A }
0N/A } else if (sampleModel instanceof SinglePixelPackedSampleModel) {
0N/A SinglePixelPackedSampleModel sppsm =
0N/A (SinglePixelPackedSampleModel)sampleModel;
0N/A this.type = IntegerComponentRaster.TYPE_BYTE_PACKED_SAMPLES;
0N/A this.scanlineStride = sppsm.getScanlineStride();
0N/A this.pixelStride = 1;
0N/A this.dataOffsets = new int[1];
0N/A this.dataOffsets[0] = dbOffset;
0N/A int xOffset = aRegion.x - origin.x;
0N/A int yOffset = aRegion.y - origin.y;
0N/A dataOffsets[0] += xOffset*pixelStride+yOffset*scanlineStride;
0N/A } else {
0N/A throw new RasterFormatException("IntegerComponentRasters must " +
0N/A "have ComponentSampleModel or SinglePixelPackedSampleModel");
0N/A }
0N/A this.bandOffset = this.dataOffsets[0];
0N/A
5708N/A verify();
0N/A }
0N/A
0N/A /**
0N/A * Returns a copy of the data offsets array. For each band the data offset
0N/A * is the index into the band's data array, of the first sample of the
0N/A * band.
0N/A */
0N/A public int[] getDataOffsets() {
0N/A return (int[]) dataOffsets.clone();
0N/A }
0N/A
0N/A /**
0N/A * Returns the data offset for the specified band. The data offset
0N/A * is the index into the data array
0N/A * in which the first sample of the first scanline is stored.
0N/A * @param band The band whose offset is returned.
0N/A */
0N/A public int getDataOffset(int band) {
0N/A return dataOffsets[band];
0N/A }
0N/A
0N/A /**
0N/A * Returns the scanline stride -- the number of data array elements between
0N/A * a given sample and the sample in the same column of the next row in the
0N/A * same band.
0N/A */
0N/A public int getScanlineStride() {
0N/A return scanlineStride;
0N/A }
0N/A
0N/A /**
0N/A * Returns pixel stride -- the number of data array elements between two
0N/A * samples for the same band on the same scanline.
0N/A */
0N/A public int getPixelStride() {
0N/A return pixelStride;
0N/A }
0N/A
0N/A /**
0N/A * Returns a reference to the data array.
0N/A */
0N/A public byte[] getDataStorage() {
0N/A return data;
0N/A }
0N/A
0N/A /**
0N/A * Returns the data elements for all bands at the specified
0N/A * location.
0N/A * An ArrayIndexOutOfBounds exception will be thrown at runtime
0N/A * if the pixel coordinate is out of bounds.
0N/A * A ClassCastException will be thrown if the input object is non null
0N/A * and references anything other than an array of transferType.
0N/A * @param x The X coordinate of the pixel location.
0N/A * @param y The Y coordinate of the pixel location.
0N/A * @param outData An object reference to an array of type defined by
0N/A * getTransferType() and length getNumDataElements().
0N/A * If null an array of appropriate type and size will be
0N/A * allocated.
0N/A * @return An object reference to an array of type defined by
0N/A * getTransferType() with the request pixel data.
0N/A */
0N/A public Object getDataElements(int x, int y, Object obj) {
0N/A if ((x < this.minX) || (y < this.minY) ||
0N/A (x >= this.maxX) || (y >= this.maxY)) {
0N/A throw new ArrayIndexOutOfBoundsException
0N/A ("Coordinate out of bounds!");
0N/A }
0N/A byte outData[];
0N/A if (obj == null) {
0N/A outData = new byte[numDataElements];
0N/A } else {
0N/A outData = (byte[])obj;
0N/A }
0N/A int off = (y-minY)*scanlineStride +
0N/A (x-minX)*pixelStride;
0N/A
0N/A for (int band = 0; band < numDataElements; band++) {
0N/A outData[band] = data[dataOffsets[band] + off];
0N/A }
0N/A
0N/A return outData;
0N/A }
0N/A
0N/A /**
0N/A * Returns an array of data elements from the specified rectangular
0N/A * region.
0N/A * An ArrayIndexOutOfBounds exception will be thrown at runtime
0N/A * if the pixel coordinates are out of bounds.
0N/A * A ClassCastException will be thrown if the input object is non null
0N/A * and references anything other than an array of transferType.
0N/A * <pre>
0N/A * byte[] bandData = (byte[])raster.getDataElements(x, y, w, h, null);
0N/A * int numDataElements = raster.getNumDataElements();
0N/A * byte[] pixel = new byte[numDataElements];
0N/A * // To find a data element at location (x2, y2)
0N/A * System.arraycopy(bandData, ((y2-y)*w + (x2-x))*numDataElements,
0N/A * pixel, 0, numDataElements);
0N/A * </pre>
0N/A * @param x The X coordinate of the upper left pixel location.
0N/A * @param y The Y coordinate of the upper left pixel location.
0N/A * @param width Width of the pixel rectangle.
0N/A * @param height Height of the pixel rectangle.
0N/A * @param outData An object reference to an array of type defined by
0N/A * getTransferType() and length w*h*getNumDataElements().
0N/A * If null an array of appropriate type and size will be
0N/A * allocated.
0N/A * @return An object reference to an array of type defined by
0N/A * getTransferType() with the request pixel data.
0N/A */
0N/A public Object getDataElements(int x, int y, int w, int h, Object obj) {
0N/A if ((x < this.minX) || (y < this.minY) ||
0N/A (x + w > this.maxX) || (y + h > this.maxY)) {
0N/A throw new ArrayIndexOutOfBoundsException
0N/A ("Coordinate out of bounds!");
0N/A }
0N/A byte outData[];
0N/A if (obj == null) {
0N/A outData = new byte[w*h*numDataElements];
0N/A } else {
0N/A outData = (byte[])obj;
0N/A }
0N/A
0N/A int yoff = (y-minY)*scanlineStride +
0N/A (x-minX)*pixelStride;
0N/A int xoff;
0N/A int off = 0;
0N/A int xstart;
0N/A int ystart;
0N/A
0N/A for (ystart=0; ystart < h; ystart++, yoff += scanlineStride) {
0N/A xoff = yoff;
0N/A for (xstart=0; xstart < w; xstart++, xoff += pixelStride) {
0N/A for (int c = 0; c < numDataElements; c++) {
0N/A outData[off++] = data[dataOffsets[c] + xoff];
0N/A }
0N/A }
0N/A }
0N/A
0N/A return outData;
0N/A }
0N/A
0N/A /**
0N/A * Returns a byte array of data elements from the specified rectangular
0N/A * region for the specified band.
0N/A * An ArrayIndexOutOfBounds exception will be thrown at runtime
0N/A * if the pixel coordinates are out of bounds.
0N/A * <pre>
0N/A * byte[] bandData = raster.getByteData(x, y, w, h, null);
0N/A * // To find the data element at location (x2, y2)
0N/A * byte bandElement = bandData[((y2-y)*w + (x2-x))];
0N/A * </pre>
0N/A * @param x The X coordinate of the upper left pixel location.
0N/A * @param y The Y coordinate of the upper left pixel location.
0N/A * @param width Width of the pixel rectangle.
0N/A * @param height Height of the pixel rectangle.
0N/A * @param band The band to return.
0N/A * @param outData If non-null, data elements for all bands
0N/A * at the specified location are returned in this array.
0N/A * @return Data array with data elements for all bands.
0N/A */
0N/A public byte[] getByteData(int x, int y, int w, int h,
0N/A int band, byte[] outData) {
0N/A // Bounds check for 'band' will be performed automatically
0N/A if ((x < this.minX) || (y < this.minY) ||
0N/A (x + w > this.maxX) || (y + h > this.maxY)) {
0N/A throw new ArrayIndexOutOfBoundsException
0N/A ("Coordinate out of bounds!");
0N/A }
0N/A if (outData == null) {
0N/A outData = new byte[scanlineStride*h];
0N/A }
0N/A int yoff = (y-minY)*scanlineStride +
0N/A (x-minX)*pixelStride + dataOffsets[band];
0N/A int xoff;
0N/A int off = 0;
0N/A int xstart;
0N/A int ystart;
0N/A
0N/A if (pixelStride == 1) {
0N/A if (scanlineStride == w) {
0N/A System.arraycopy(data, yoff, outData, 0, w*h);
0N/A }
0N/A else {
0N/A for (ystart=0; ystart < h; ystart++, yoff += scanlineStride) {
0N/A System.arraycopy(data, yoff, outData, off, w);
0N/A off += w;
0N/A }
0N/A }
0N/A }
0N/A else {
0N/A for (ystart=0; ystart < h; ystart++, yoff += scanlineStride) {
0N/A xoff = yoff;
0N/A for (xstart=0; xstart < w; xstart++, xoff += pixelStride) {
0N/A outData[off++] = data[xoff];
0N/A }
0N/A }
0N/A }
0N/A
0N/A return outData;
0N/A }
0N/A
0N/A /**
0N/A * Returns a byte array of data elements from the specified rectangular
0N/A * region.
0N/A * An ArrayIndexOutOfBounds exception will be thrown at runtime
0N/A * if the pixel coordinates are out of bounds.
0N/A * <pre>
0N/A * byte[] bandData = raster.getByteData(x, y, w, h, null);
0N/A * int numDataElements = raster.getnumDataElements();
0N/A * byte[] pixel = new byte[numDataElements];
0N/A * // To find a data element at location (x2, y2)
0N/A * System.arraycopy(bandData, ((y2-y)*w + (x2-x))*numDataElements,
0N/A * pixel, 0, numDataElements);
0N/A * </pre>
0N/A * @param x The X coordinate of the upper left pixel location.
0N/A * @param y The Y coordinate of the upper left pixel location.
0N/A * @param width Width of the pixel rectangle.
0N/A * @param height Height of the pixel rectangle.
0N/A * @param outData If non-null, data elements for all bands
0N/A * at the specified location are returned in this array.
0N/A * @return Data array with data elements for all bands.
0N/A */
0N/A public byte[] getByteData(int x, int y, int w, int h, byte[] outData) {
0N/A if ((x < this.minX) || (y < this.minY) ||
0N/A (x + w > this.maxX) || (y + h > this.maxY)) {
0N/A throw new ArrayIndexOutOfBoundsException
0N/A ("Coordinate out of bounds!");
0N/A }
0N/A if (outData == null) {
0N/A outData = new byte[numDataElements*scanlineStride*h];
0N/A }
0N/A int yoff = (y-minY)*scanlineStride +
0N/A (x-minX)*pixelStride;
0N/A int xoff;
0N/A int off = 0;
0N/A int xstart;
0N/A int ystart;
0N/A
0N/A // REMIND: Should keep track if dataOffsets are in a nice order
0N/A for (ystart=0; ystart < h; ystart++, yoff += scanlineStride) {
0N/A xoff = yoff;
0N/A for (xstart=0; xstart < w; xstart++, xoff += pixelStride) {
0N/A for (int c = 0; c < numDataElements; c++) {
0N/A outData[off++] = data[dataOffsets[c] + xoff];
0N/A }
0N/A }
0N/A }
0N/A
0N/A return outData;
0N/A }
0N/A
0N/A /**
0N/A * Stores the data elements for all bands at the specified location.
0N/A * An ArrayIndexOutOfBounds exception will be thrown at runtime
0N/A * if the pixel coordinate is out of bounds.
0N/A * A ClassCastException will be thrown if the input object is non null
0N/A * and references anything other than an array of transferType.
0N/A * @param x The X coordinate of the pixel location.
0N/A * @param y The Y coordinate of the pixel location.
0N/A * @param inData An object reference to an array of type defined by
0N/A * getTransferType() and length getNumDataElements()
0N/A * containing the pixel data to place at x,y.
0N/A */
0N/A public void setDataElements(int x, int y, Object obj) {
0N/A if ((x < this.minX) || (y < this.minY) ||
0N/A (x >= this.maxX) || (y >= this.maxY)) {
0N/A throw new ArrayIndexOutOfBoundsException
0N/A ("Coordinate out of bounds!");
0N/A }
0N/A byte inData[] = (byte[])obj;
0N/A int off = (y-minY)*scanlineStride +
0N/A (x-minX)*pixelStride;
0N/A
0N/A for (int i = 0; i < numDataElements; i++) {
0N/A data[dataOffsets[i] + off] = inData[i];
0N/A }
0N/A
0N/A markDirty();
0N/A }
0N/A
0N/A /**
0N/A * Stores the Raster data at the specified location.
0N/A * An ArrayIndexOutOfBounds exception will be thrown at runtime
0N/A * if the pixel coordinates are out of bounds.
0N/A * @param x The X coordinate of the pixel location.
0N/A * @param y The Y coordinate of the pixel location.
0N/A * @param inRaster Raster of data to place at x,y location.
0N/A */
0N/A public void setDataElements(int x, int y, Raster inRaster) {
0N/A int dstOffX = inRaster.getMinX() + x;
0N/A int dstOffY = inRaster.getMinY() + y;
0N/A int width = inRaster.getWidth();
0N/A int height = inRaster.getHeight();
0N/A if ((dstOffX < this.minX) || (dstOffY < this.minY) ||
0N/A (dstOffX + width > this.maxX) || (dstOffY + height > this.maxY)) {
0N/A throw new ArrayIndexOutOfBoundsException
0N/A ("Coordinate out of bounds!");
0N/A }
0N/A
0N/A setDataElements(dstOffX, dstOffY, width, height, inRaster);
0N/A }
0N/A
0N/A /**
0N/A * Stores the Raster data at the specified location.
0N/A * @param dstX The absolute X coordinate of the destination pixel
0N/A * that will receive a copy of the upper-left pixel of the
0N/A * inRaster
0N/A * @param dstY The absolute Y coordinate of the destination pixel
0N/A * that will receive a copy of the upper-left pixel of the
0N/A * inRaster
0N/A * @param width The number of pixels to store horizontally
0N/A * @param height The number of pixels to store vertically
0N/A * @param inRaster Raster of data to place at x,y location.
0N/A */
0N/A private void setDataElements(int dstX, int dstY,
0N/A int width, int height,
0N/A Raster inRaster) {
0N/A // Assume bounds checking has been performed previously
0N/A if (width <= 0 || height <= 0) {
0N/A return;
0N/A }
0N/A
0N/A int srcOffX = inRaster.getMinX();
0N/A int srcOffY = inRaster.getMinY();
0N/A Object tdata = null;
0N/A
0N/A if (inRaster instanceof ByteComponentRaster) {
0N/A ByteComponentRaster bct = (ByteComponentRaster) inRaster;
0N/A byte[] bdata = bct.getDataStorage();
0N/A // REMIND: Do something faster!
0N/A if (numDataElements == 1) {
0N/A int toff = bct.getDataOffset(0);
0N/A int tss = bct.getScanlineStride();
0N/A
0N/A int srcOffset = toff;
0N/A int dstOffset = dataOffsets[0]+(dstY-minY)*scanlineStride+
0N/A (dstX-minX);
0N/A
0N/A
0N/A if (pixelStride == bct.getPixelStride()) {
0N/A width *= pixelStride;
0N/A for (int tmpY=0; tmpY < height; tmpY++) {
0N/A System.arraycopy(bdata, srcOffset,
0N/A data, dstOffset, width);
0N/A srcOffset += tss;
0N/A dstOffset += scanlineStride;
0N/A }
0N/A markDirty();
0N/A return;
0N/A }
0N/A }
0N/A }
0N/A
0N/A for (int startY=0; startY < height; startY++) {
0N/A // Grab one scanline at a time
0N/A tdata = inRaster.getDataElements(srcOffX, srcOffY+startY,
0N/A width, 1, tdata);
0N/A setDataElements(dstX, dstY+startY, width, 1, tdata);
0N/A }
0N/A }
0N/A
0N/A /**
0N/A * Stores an array of data elements into the specified rectangular
0N/A * region.
0N/A * An ArrayIndexOutOfBounds exception will be thrown at runtime
0N/A * if the pixel coordinates are out of bounds.
0N/A * A ClassCastException will be thrown if the input object is non null
0N/A * and references anything other than an array of transferType.
0N/A * The data elements in the
0N/A * data array are assumed to be packed. That is, a data element
0N/A * for the nth band at location (x2, y2) would be found at:
0N/A * <pre>
0N/A * inData[((y2-y)*w + (x2-x))*numDataElements + n]
0N/A * </pre>
0N/A * @param x The X coordinate of the upper left pixel location.
0N/A * @param y The Y coordinate of the upper left pixel location.
0N/A * @param w Width of the pixel rectangle.
0N/A * @param h Height of the pixel rectangle.
0N/A * @param inData An object reference to an array of type defined by
0N/A * getTransferType() and length w*h*getNumDataElements()
0N/A * containing the pixel data to place between x,y and
0N/A * x+h, y+h.
0N/A */
0N/A public void setDataElements(int x, int y, int w, int h, Object obj) {
0N/A if ((x < this.minX) || (y < this.minY) ||
0N/A (x + w > this.maxX) || (y + h > this.maxY)) {
0N/A throw new ArrayIndexOutOfBoundsException
0N/A ("Coordinate out of bounds!");
0N/A }
0N/A byte inData[] = (byte[])obj;
0N/A int yoff = (y-minY)*scanlineStride +
0N/A (x-minX)*pixelStride;
0N/A int xoff;
0N/A int off = 0;
0N/A int xstart;
0N/A int ystart;
0N/A
0N/A if (numDataElements == 1) {
0N/A int srcOffset = 0;
0N/A int dstOffset = yoff + dataOffsets[0];
0N/A for (ystart=0; ystart < h; ystart++) {
0N/A xoff = yoff;
0N/A System.arraycopy(inData, srcOffset,
0N/A data, dstOffset, w);
0N/A
0N/A srcOffset += w;
0N/A dstOffset += scanlineStride;
0N/A }
0N/A markDirty();
0N/A return;
0N/A }
0N/A
0N/A for (ystart=0; ystart < h; ystart++, yoff += scanlineStride) {
0N/A xoff = yoff;
0N/A for (xstart=0; xstart < w; xstart++, xoff += pixelStride) {
0N/A for (int c = 0; c < numDataElements; c++) {
0N/A data[dataOffsets[c] + xoff] = inData[off++];
0N/A }
0N/A }
0N/A }
0N/A
0N/A markDirty();
0N/A }
0N/A
0N/A /**
0N/A * Stores a byte array of data elements into the specified rectangular
0N/A * region for the specified band.
0N/A * An ArrayIndexOutOfBounds exception will be thrown at runtime
0N/A * if the pixel coordinates are out of bounds.
0N/A * The data elements in the
0N/A * data array are assumed to be packed. That is, a data element
0N/A * at location (x2, y2) would be found at:
0N/A * <pre>
0N/A * inData[((y2-y)*w + (x2-x)) + n]
0N/A * </pre>
0N/A * @param x The X coordinate of the upper left pixel location.
0N/A * @param y The Y coordinate of the upper left pixel location.
0N/A * @param w Width of the pixel rectangle.
0N/A * @param h Height of the pixel rectangle.
0N/A * @param band The band to set.
0N/A * @param inData The data elements to be stored.
0N/A */
0N/A public void putByteData(int x, int y, int w, int h,
0N/A int band, byte[] inData) {
0N/A // Bounds check for 'band' will be performed automatically
0N/A if ((x < this.minX) || (y < this.minY) ||
0N/A (x + w > this.maxX) || (y + h > this.maxY)) {
0N/A throw new ArrayIndexOutOfBoundsException
0N/A ("Coordinate out of bounds!");
0N/A }
0N/A int yoff = (y-minY)*scanlineStride +
0N/A (x-minX)*pixelStride + dataOffsets[band];
0N/A int xoff;
0N/A int off = 0;
0N/A int xstart;
0N/A int ystart;
0N/A
0N/A if (pixelStride == 1) {
0N/A if (scanlineStride == w) {
0N/A System.arraycopy(inData, 0, data, yoff, w*h);
0N/A }
0N/A else {
0N/A for (ystart=0; ystart < h; ystart++, yoff += scanlineStride) {
0N/A System.arraycopy(inData, off, data, yoff, w);
0N/A off += w;
0N/A }
0N/A }
0N/A }
0N/A else {
0N/A for (ystart=0; ystart < h; ystart++, yoff += scanlineStride) {
0N/A xoff = yoff;
0N/A for (xstart=0; xstart < w; xstart++, xoff += pixelStride) {
0N/A data[xoff] = inData[off++];
0N/A }
0N/A }
0N/A }
0N/A
0N/A markDirty();
0N/A }
0N/A
0N/A /**
0N/A * Stores a byte array of data elements into the specified rectangular
0N/A * region.
0N/A * An ArrayIndexOutOfBounds exception will be thrown at runtime
0N/A * if the pixel coordinates are out of bounds.
0N/A * The data elements in the
0N/A * data array are assumed to be packed. That is, a data element
0N/A * for the nth band at location (x2, y2) would be found at:
0N/A * <pre>
0N/A * inData[((y2-y)*w + (x2-x))*numDataElements + n]
0N/A * </pre>
0N/A * @param x The X coordinate of the upper left pixel location.
0N/A * @param y The Y coordinate of the upper left pixel location.
0N/A * @param w Width of the pixel rectangle.
0N/A * @param h Height of the pixel rectangle.
0N/A * @param inData The data elements to be stored.
0N/A */
0N/A public void putByteData(int x, int y, int w, int h, byte[] inData) {
0N/A if ((x < this.minX) || (y < this.minY) ||
0N/A (x + w > this.maxX) || (y + h > this.maxY)) {
0N/A throw new ArrayIndexOutOfBoundsException
0N/A ("Coordinate out of bounds!");
0N/A }
0N/A int yoff = (y-minY)*scanlineStride +
0N/A (x-minX)*pixelStride;
0N/A
0N/A int xoff;
0N/A int off = 0;
0N/A int xstart;
0N/A int ystart;
0N/A
0N/A if (numDataElements == 1) {
0N/A yoff += dataOffsets[0];
0N/A if (pixelStride == 1) {
0N/A if (scanlineStride == w) {
0N/A System.arraycopy(inData, 0, data, yoff, w*h);
0N/A }
0N/A else {
0N/A for (ystart=0; ystart < h; ystart++) {
0N/A System.arraycopy(inData, off, data, yoff, w);
0N/A off += w;
0N/A yoff += scanlineStride;
0N/A }
0N/A }
0N/A }
0N/A else {
0N/A for (ystart=0; ystart < h; ystart++, yoff += scanlineStride) {
0N/A xoff = yoff;
0N/A for (xstart=0; xstart < w; xstart++, xoff += pixelStride) {
0N/A data[xoff] = inData[off++];
0N/A }
0N/A }
0N/A }
0N/A }
0N/A else {
0N/A for (ystart=0; ystart < h; ystart++, yoff += scanlineStride) {
0N/A xoff = yoff;
0N/A for (xstart=0; xstart < w; xstart++, xoff += pixelStride) {
0N/A for (int c = 0; c < numDataElements; c++) {
0N/A data[dataOffsets[c] + xoff] = inData[off++];
0N/A }
0N/A }
0N/A }
0N/A }
0N/A
0N/A markDirty();
0N/A }
0N/A
0N/A /**
0N/A * Creates a subraster given a region of the raster. The x and y
0N/A * coordinates specify the horizontal and vertical offsets
0N/A * from the upper-left corner of this raster to the upper-left corner
0N/A * of the subraster. A subset of the bands of the parent Raster may
0N/A * be specified. If this is null, then all the bands are present in the
0N/A * subRaster. A translation to the subRaster may also be specified.
0N/A * Note that the subraster will reference the same
0N/A * DataBuffer as the parent raster, but using different offsets.
0N/A * @param x X offset.
0N/A * @param y Y offset.
0N/A * @param width Width (in pixels) of the subraster.
0N/A * @param height Height (in pixels) of the subraster.
0N/A * @param x0 Translated X origin of the subraster.
0N/A * @param y0 Translated Y origin of the subraster.
0N/A * @param bandList Array of band indices.
0N/A * @exception RasterFormatException
0N/A * if the specified bounding box is outside of the parent raster.
0N/A */
0N/A public Raster createChild(int x, int y,
0N/A int width, int height,
0N/A int x0, int y0, int[] bandList) {
0N/A WritableRaster newRaster = createWritableChild(x, y,
0N/A width, height,
0N/A x0, y0,
0N/A bandList);
0N/A return (Raster) newRaster;
0N/A }
0N/A
0N/A /**
0N/A * Creates a Writable subRaster given a region of the Raster. The x and y
0N/A * coordinates specify the horizontal and vertical offsets
0N/A * from the upper-left corner of this Raster to the upper-left corner
0N/A * of the subRaster. A subset of the bands of the parent Raster may
0N/A * be specified. If this is null, then all the bands are present in the
0N/A * subRaster. A translation to the subRaster may also be specified.
0N/A * Note that the subRaster will reference the same
0N/A * DataBuffer as the parent Raster, but using different offsets.
0N/A * @param x X offset.
0N/A * @param y Y offset.
0N/A * @param width Width (in pixels) of the subraster.
0N/A * @param height Height (in pixels) of the subraster.
0N/A * @param x0 Translated X origin of the subraster.
0N/A * @param y0 Translated Y origin of the subraster.
0N/A * @param bandList Array of band indices.
0N/A * @exception RasterFormatException
0N/A * if the specified bounding box is outside of the parent Raster.
0N/A */
0N/A public WritableRaster createWritableChild(int x, int y,
0N/A int width, int height,
0N/A int x0, int y0,
0N/A int[] bandList) {
0N/A if (x < this.minX) {
0N/A throw new RasterFormatException("x lies outside the raster");
0N/A }
0N/A if (y < this.minY) {
0N/A throw new RasterFormatException("y lies outside the raster");
0N/A }
0N/A if ((x+width < x) || (x+width > this.minX + this.width)) {
0N/A throw new RasterFormatException("(x + width) is outside of Raster");
0N/A }
0N/A if ((y+height < y) || (y+height > this.minY + this.height)) {
0N/A throw new RasterFormatException("(y + height) is outside of Raster");
0N/A }
0N/A
0N/A SampleModel sm;
0N/A
0N/A if (bandList != null)
0N/A sm = sampleModel.createSubsetSampleModel(bandList);
0N/A else
0N/A sm = sampleModel;
0N/A
0N/A int deltaX = x0 - x;
0N/A int deltaY = y0 - y;
0N/A
0N/A return new ByteComponentRaster(sm,
0N/A dataBuffer,
0N/A new Rectangle(x0, y0, width, height),
0N/A new Point(sampleModelTranslateX+deltaX,
0N/A sampleModelTranslateY+deltaY),
0N/A this);
0N/A }
0N/A
0N/A /**
0N/A * Creates a Raster with the same layout but using a different
0N/A * width and height, and with new zeroed data arrays.
0N/A */
0N/A public WritableRaster createCompatibleWritableRaster(int w, int h) {
0N/A if (w <= 0 || h <=0) {
0N/A throw new RasterFormatException("negative "+
0N/A ((w <= 0) ? "width" : "height"));
0N/A }
0N/A
0N/A SampleModel sm = sampleModel.createCompatibleSampleModel(w, h);
0N/A
0N/A return new ByteComponentRaster(sm , new Point(0,0));
0N/A
0N/A }
0N/A
0N/A /**
0N/A * Creates a Raster with the same layout and the same
0N/A * width and height, and with new zeroed data arrays. If
0N/A * the Raster is a subRaster, this will call
0N/A * createCompatibleRaster(width, height).
0N/A */
0N/A public WritableRaster createCompatibleWritableRaster() {
0N/A return createCompatibleWritableRaster(width,height);
0N/A }
0N/A
0N/A /**
5708N/A * Verify that the layout parameters are consistent with the data.
5708N/A *
5708N/A * The method verifies whether scanline stride and pixel stride do not
5708N/A * cause an integer overflow during calculation of a position of the pixel
5708N/A * in data buffer. It also verifies whether the data buffer has enough data
5708N/A * to correspond the raster layout attributes.
5708N/A *
5708N/A * @throws RasterFormatException if an integer overflow is detected,
5708N/A * or if data buffer has not enough capacity.
0N/A */
5708N/A protected final void verify() {
5817N/A /* Need to re-verify the dimensions since a sample model may be
5817N/A * specified to the constructor
5817N/A */
5817N/A if (width <= 0 || height <= 0 ||
5817N/A height > (Integer.MAX_VALUE / width))
5817N/A {
5817N/A throw new RasterFormatException("Invalid raster dimension");
5817N/A }
5817N/A
5708N/A for (int i = 0; i < dataOffsets.length; i++) {
0N/A if (dataOffsets[i] < 0) {
5708N/A throw new RasterFormatException("Data offsets for band " + i
5708N/A + "(" + dataOffsets[i]
5708N/A + ") must be >= 0");
0N/A }
0N/A }
0N/A
5708N/A // we can be sure that width and height are greater than 0
5708N/A if (scanlineStride < 0 ||
6367N/A scanlineStride > (Integer.MAX_VALUE / height) ||
6367N/A scanlineStride > data.length)
5708N/A {
5708N/A // integer overflow
5708N/A throw new RasterFormatException("Incorrect scanline stride: "
5708N/A + scanlineStride);
5708N/A }
5708N/A int lastScanOffset = (height - 1) * scanlineStride;
5708N/A
5708N/A if (pixelStride < 0 ||
6367N/A pixelStride > (Integer.MAX_VALUE / width) ||
6367N/A pixelStride > data.length)
5708N/A {
5708N/A // integer overflow
5708N/A throw new RasterFormatException("Incorrect pixel stride: "
5708N/A + pixelStride);
5708N/A }
5708N/A int lastPixelOffset = (width - 1) * pixelStride;
5708N/A
5708N/A if (lastPixelOffset > (Integer.MAX_VALUE - lastScanOffset)) {
5708N/A // integer overflow
5708N/A throw new RasterFormatException("Incorrect raster attributes");
5708N/A }
5708N/A lastPixelOffset += lastScanOffset;
5708N/A
6335N/A int index;
6335N/A int maxIndex = 0;
5708N/A for (int i = 0; i < numDataElements; i++) {
5708N/A if (dataOffsets[i] > (Integer.MAX_VALUE - lastPixelOffset)) {
5708N/A throw new RasterFormatException("Incorrect band offset: "
5708N/A + dataOffsets[i]);
5708N/A
5708N/A }
5708N/A
6335N/A index = lastPixelOffset + dataOffsets[i];
5817N/A
6335N/A if (index > maxIndex) {
6335N/A maxIndex = index;
0N/A }
0N/A }
6335N/A if (data.length <= maxIndex) {
6335N/A throw new RasterFormatException("Data array too small (should be > "
6335N/A + maxIndex + " )");
0N/A }
0N/A }
0N/A
0N/A public String toString() {
0N/A return new String ("ByteComponentRaster: width = "+width+" height = "
0N/A + height
0N/A +" #numDataElements "+numDataElements
0N/A // +" xOff = "+xOffset+" yOff = "+yOffset
0N/A +" dataOff[0] = "+dataOffsets[0]);
0N/A }
0N/A
0N/A// /**
0N/A// * For debugging... prints a region of a one-band ByteComponentRaster
0N/A// */
0N/A// public void print(int x, int y, int w, int h) {
0N/A// // REMIND: Only works for 1 band!
0N/A// System.out.println(this);
0N/A// int offset = dataOffsets[0] + y*scanlineStride + x*pixelStride;
0N/A// int off;
0N/A// for (int yoff=0; yoff < h; yoff++, offset += scanlineStride) {
0N/A// off = offset;
0N/A// System.out.print("Line "+(y+yoff)+": ");
0N/A// for (int xoff = 0; xoff < w; xoff++, off+= pixelStride) {
0N/A// String s = Integer.toHexString(data[off]);
0N/A// if (s.length() == 8) {
0N/A// s = s.substring(6,8);
0N/A// }
0N/A// System.out.print(s+" ");
0N/A// }
0N/A// System.out.println("");
0N/A// }
0N/A// }
0N/A
0N/A
0N/A}