0N/A/*
2362N/A * Copyright (c) 1998, 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.SinglePixelPackedSampleModel;
0N/Aimport java.awt.image.DataBuffer;
0N/Aimport java.awt.image.DataBufferInt;
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 32-bit
0N/A * data elements stored in close proximity to each other in a integer 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 32).
0N/A * There is only one pixel stride and one scanline stride for all
0N/A * bands. For a given pixel, all samples fit in N data elements and these
0N/A * N data elements hold samples for only one pixel. This type of Raster
0N/A * can be used with a PackedColorModel.
0N/A * <p>
0N/A * For example, if there is only one data element per pixel, a
0N/A * SinglePixelPackedSampleModel can be used to represent multiple
0N/A * bands with a PackedColorModel (including a DirectColorModel) for
0N/A * color interpretation.
0N/A *
0N/A */
0N/Apublic class IntegerInterleavedRaster extends IntegerComponentRaster {
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 /**
0N/A * Constructs a IntegerInterleavedRaster 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.
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 IntegerInterleavedRaster(SampleModel sampleModel,
0N/A 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 IntegerInterleavedRaster with the given SampleModel
0N/A * and DataBuffer. The Raster's upper left corner is origin and
0N/A * it is the same sizes the SampleModel. The DataBuffer is not
0N/A * initialized and must be a DataBufferInt compatible with SampleModel.
0N/A * SampleModel must be of type SinglePixelPackedSampleModel.
0N/A * @param sampleModel The SampleModel that specifies the layout.
0N/A * @param dataBuffer The DataBufferInt that contains the image data.
0N/A * @param origin The Point that specifies the origin.
0N/A */
0N/A public IntegerInterleavedRaster(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 IntegerInterleavedRaster with the given SampleModel,
0N/A * DataBuffer, and parent. DataBuffer must be a DataBufferInt and
0N/A * SampleModel must be of type SinglePixelPackedSampleModel.
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 coodinate 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 DataBufferInt 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 IntegerInterleavedRaster(SampleModel sampleModel,
0N/A DataBuffer dataBuffer,
0N/A Rectangle aRegion,
0N/A Point origin,
0N/A IntegerInterleavedRaster parent){
0N/A super(sampleModel,dataBuffer,aRegion,origin,parent);
0N/A this.maxX = minX + width;
0N/A this.maxY = minY + height;
0N/A if (!(dataBuffer instanceof DataBufferInt)) {
0N/A throw new RasterFormatException("IntegerInterleavedRasters must have" +
0N/A "integer DataBuffers");
0N/A }
0N/A DataBufferInt dbi = (DataBufferInt)dataBuffer;
0N/A this.data = stealData(dbi, 0);
0N/A
0N/A if (sampleModel instanceof SinglePixelPackedSampleModel) {
0N/A SinglePixelPackedSampleModel sppsm =
0N/A (SinglePixelPackedSampleModel)sampleModel;
0N/A this.scanlineStride = sppsm.getScanlineStride();
0N/A this.pixelStride = 1;
0N/A this.dataOffsets = new int[1];
0N/A this.dataOffsets[0] = dbi.getOffset();
0N/A this.bandOffset = this.dataOffsets[0];
0N/A int xOffset = aRegion.x - origin.x;
0N/A int yOffset = aRegion.y - origin.y;
0N/A dataOffsets[0] += xOffset+yOffset*scanlineStride;
0N/A this.numDataElems = sppsm.getNumDataElements();
0N/A } else {
0N/A throw new RasterFormatException("IntegerInterleavedRasters must have"+
0N/A " SinglePixelPackedSampleModel");
0N/A }
5817N/A verify();
0N/A }
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 data offset for the specified band. The data offset
0N/A * is the index into the data array in which the first sample
0N/A * of the first scanline is stored.
0N/A */
0N/A public int getDataOffset(int band) {
0N/A return dataOffsets[band];
0N/A }
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.
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 int[] 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 int outData[];
0N/A if (obj == null) {
0N/A outData = new int[1];
0N/A } else {
0N/A outData = (int[])obj;
0N/A }
0N/A int off = (y-minY)*scanlineStride + (x-minX) + dataOffsets[0];
0N/A outData[0] = data[off];
0N/A
0N/A return outData;
0N/A }
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 * int[] bandData = (int[])raster.getDataElements(x, y, w, h, null);
0N/A * int numDataElements = raster.getNumDataElements();
0N/A * int[] pixel = new int[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 int outData[];
0N/A if (obj instanceof int[]) {
0N/A outData = (int[])obj;
0N/A } else {
0N/A outData = new int[w*h];
0N/A }
0N/A int yoff = (y-minY)*scanlineStride + (x-minX) + dataOffsets[0];
0N/A int off = 0;
0N/A
0N/A for (int ystart = 0; ystart < h; ystart++) {
0N/A System.arraycopy(data, yoff, outData, off, w);
0N/A off += w;
0N/A yoff += scanlineStride;
0N/A }
0N/A
0N/A return outData;
0N/A }
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 int inData[] = (int[])obj;
0N/A
0N/A int off = (y-minY)*scanlineStride + (x-minX) + dataOffsets[0];
0N/A
0N/A data[off] = inData[0];
0N/A
0N/A markDirty();
0N/A }
0N/A
0N/A
0N/A /**
0N/A * Stores the Raster data at the specified location.
0N/A * The transferType of the inputRaster must match this raster.
0N/A * An ArrayIndexOutOfBoundsException 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 = x + inRaster.getMinX();
0N/A int dstOffY = y + inRaster.getMinY();
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 // Write inRaster (minX, minY) to (dstX, dstY)
0N/A
0N/A int srcOffX = inRaster.getMinX();
0N/A int srcOffY = inRaster.getMinY();
0N/A int tdata[] = null;
0N/A
0N/A if (inRaster instanceof IntegerInterleavedRaster) {
0N/A IntegerInterleavedRaster ict = (IntegerInterleavedRaster) inRaster;
0N/A
0N/A // Extract the raster parameters
0N/A tdata = ict.getDataStorage();
0N/A int tss = ict.getScanlineStride();
0N/A int toff = ict.getDataOffset(0);
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 // Fastest case. We can copy scanlines
0N/A // Loop through all of the scanlines and copy the data
0N/A for (int startY=0; startY < height; startY++) {
0N/A System.arraycopy(tdata, srcOffset, 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 Object odata = null;
0N/A for (int startY=0; startY < height; startY++) {
0N/A // Grab one scanline at a time
0N/A odata = inRaster.getDataElements(srcOffX, srcOffY+startY,
0N/A width, 1, odata);
0N/A setDataElements(dstX, dstY+startY, width, 1, odata);
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 int inData[] = (int[])obj;
0N/A int yoff = (y-minY)*scanlineStride + (x-minX) + dataOffsets[0];
0N/A int off = 0;
0N/A
0N/A for (int 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 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 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 raster");
0N/A }
0N/A if (y < this.minY) {
0N/A throw new RasterFormatException("y lies outside raster");
0N/A }
0N/A if ((x+width < x) || (x+width > this.minX + this.width)) {
0N/A throw new RasterFormatException("(x + width) is outside raster");
0N/A }
0N/A if ((y+height < y) || (y+height > this.minY + this.height)) {
0N/A throw new RasterFormatException("(y + height) is outside 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 IntegerInterleavedRaster(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 /**
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. 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,
0N/A int bandList[]) {
0N/A return createWritableChild(x, y, width, height, x0, y0, bandList);
0N/A }
0N/A
0N/A
0N/A /**
0N/A * Creates a raster with the same band 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 IntegerInterleavedRaster(sm, new Point(0,0));
0N/A }
0N/A
0N/A /**
0N/A * Creates a raster with the same data 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 public String toString() {
0N/A return new String ("IntegerInterleavedRaster: width = "+width
0N/A +" height = " + height
0N/A +" #Bands = " + numBands
0N/A +" xOff = "+sampleModelTranslateX
0N/A +" yOff = "+sampleModelTranslateY
0N/A +" dataOffset[0] "+dataOffsets[0]);
0N/A }
0N/A
0N/A// /**
0N/A// * For debugging... prints a region of a one-band IntegerInterleavedRaster
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 "+(sampleModelTranslateY+y+yoff)+": ");
0N/A// for (int xoff = 0; xoff < w; xoff++, off+= pixelStride) {
0N/A// System.out.print(Integer.toHexString(data[off])+" ");
0N/A// }
0N/A// System.out.println("");
0N/A// }
0N/A// }
0N/A
0N/A}