home *** CD-ROM | disk | FTP | other *** search
Java Source | 1998-03-20 | 19.9 KB | 473 lines |
- /*
- * @(#)WritableRaster.java 1.20 98/03/18
- *
- * Copyright 1997, 1998 by Sun Microsystems, Inc.,
- * 901 San Antonio Road, Palo Alto, California, 94303, U.S.A.
- * All rights reserved.
- *
- * This software is the confidential and proprietary information
- * of Sun Microsystems, Inc. ("Confidential Information"). You
- * shall not disclose such Confidential Information and shall use
- * it only in accordance with the terms of the license agreement
- * you entered into with Sun.
- */
-
- /* ****************************************************************
- ******************************************************************
- ******************************************************************
- *** COPYRIGHT (c) Eastman Kodak Company, 1997
- *** As an unpublished work pursuant to Title 17 of the United
- *** States Code. All rights reserved.
- ******************************************************************
- ******************************************************************
- ******************************************************************/
-
- package java.awt.image;
- import java.awt.Rectangle;
- import java.awt.Point;
-
- /**
- * This class provides methods for storing image data and inherits
- * methods for retrieving image data from it's parent class Raster.
- * It contains a DataBuffer object that holds a buffer of image
- * data in some format, a SampleModel which describes th formate and is
- * capabable of storing and retrieving Samples from the DataBuffer
- * and a Rect that defines the WritableRaster's coordinate space. (Upper
- * left corner, width and height.)
- */
-
- public class WritableRaster extends Raster {
-
- /**
- * Constructs a WritableRaster with the given SampleModel. The
- * WritableRaster's upper left corner is origin and it is the
- * same size as the SampleModel. A DataBuffer large enough to
- * describe the WriteableRaster is automatically created.
- * @param sampleModel The SampleModel that specifies the layout.
- * @param origin The Point that specified the origin.
- */
- protected WritableRaster(SampleModel sampleModel,
- Point origin) {
- this(sampleModel,
- sampleModel.createCompatibleDataBuffer(),
- new Rectangle(origin.x,
- origin.y,
- sampleModel.getWidth(),
- sampleModel.getHeight()),
- origin,
- null);
- }
-
- /**
- * Constructs a WritableRaster with the given SampleModel and DataBuffer.
- * The WritableRaster's upper left corner is origin and it is the same
- * size as the SampleModel. The DataBuffer is not initialized and must
- * be compatible with SampleModel.
- * @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.
- */
- protected WritableRaster(SampleModel sampleModel,
- DataBuffer dataBuffer,
- Point origin) {
- this(sampleModel,
- dataBuffer,
- new Rectangle(origin.x,
- origin.y,
- sampleModel.getWidth(),
- sampleModel.getHeight()),
- origin,
- null);
- }
-
- /**
- * Constructs a WriteableRaster with the given SampleModel, DataBuffer,
- * and parent. When translated into the base Raster's coordinate
- * system, aRegion must be contained by the base Raster. Origin
- * is the coodinate 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.) The parameter parent must be
- * a WritableRaster or an exception will be thrown.
- *
- * 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.
- */
- protected WritableRaster(SampleModel sampleModel,
- DataBuffer dataBuffer,
- Rectangle aRegion,
- Point origin,
- WritableRaster parent){
- super(sampleModel,dataBuffer,aRegion,origin,parent);
- if (parent != null) {
- if (!(parent instanceof WritableRaster)) {
- throw new RasterFormatException("Attempted to construct a " +
- "WritableRaster with a non-writable Parent");
- }
- }
- }
-
-
- /** Returns the parent WritableRaster (if any) of this Raster. */
- public WritableRaster getWritableParent() {
- return (WritableRaster)parent;
- }
-
- /**
- * Create a WritableRaster with the same size, SampleModel and DataBuffer
- * as this one, but with a different location.
- * @param location The upper left corner of the new returned Raster.
- */
- public WritableRaster createWritableTranslatedRaster(Point location) {
- return createWritableSubRaster(minX,minY,width,height,
- location.x,location.y,null);
- }
-
- /**
- * Returns a WritableRaster which references this WritableRaster's
- * DataBuffer. x,y, width and height form a rectangle in this
- * WritableRaster's coordinate space. An error is thrown if this
- * rectangle is not fully contained by this Raster
- * @param x The X coordinate of the upper left hand corner.
- * @param y The Y coordinate of the upper left hand corner.
- * @param width Width of the region starting at (x,y).
- * @param height Height of the region starting at (x,y).
- */
- public WritableRaster createWritableSubRaster(int x, int y,
- int w, int h) {
- return createWritableSubRaster(x, y, w, h, x, y, null);
- }
-
- /**
- * Creates a subraster given a region of the raster. Returns a
- * WritableRaster which references this WritableRaster's
- * DataBuffer.
- * @param rect is a rectangle in this WritableRaster's coordinate
- * space. An error is thrown if rect is not fully contained by this
- * WriteableRaster.
- * @param rect The rectangle specifying the dimensions of the subraster.
- */
- public WritableRaster createWritableSubRaster(Rectangle r) {
- return createWritableSubRaster(r.x, r.y,
- r.width, r.height,
- r.x, r.y,
- null);
- }
-
- /**
- * Returns a translated WritableRaster which references this
- * WriteableRaster's DataBuffer. The parameters x,y, width and height
- * form a rectangle. The new WritableRaster's coordinate system has
- * an upper left corner of x0, y0. An error is thrown if rectangle is
- * not fully contained by this WritableRaster.
- * @param x X of the upper left corner in this Raster's coordinates.
- * @param y Y of the upper left corner in this Raster's coordinates.
- * @param width Width of the region starting at (x,y).
- * @param height Height of the region starting at (x,y).
- * @param x0 X of the upper left corner of returned Raster.
- * @param y0 Y of the upper left corner of returned Raster.
- * @param bandList Array of band indices.
- */
- public WritableRaster createWritableSubRaster(int x, int y,
- int w, int h,
- int x0, int y0,
- int bandList[]) {
- if (x < this.minX) {
- throw new RasterFormatException("x lies outside raster");
- }
- if (y < this.minY) {
- throw new RasterFormatException("y lies outside raster");
- }
- if (x+width > this.width + this.minX) {
- throw new RasterFormatException("(x + width) is outside raster");
- }
- if (y+height > this.height + this.minY) {
- throw new RasterFormatException("(y + height) is outside raster");
- }
-
- SampleModel sm;
-
- if (bandList != null)
- sm = sampleModel.createSubsetSampleModel(sampleModel.width,
- sampleModel.height,
- bandList);
- else
- sm = sampleModel;
-
- int deltaX = x0 - x;
- int deltaY = y0 - y;
-
- return new WritableRaster(sm,
- dataBuffer,
- new Rectangle(x0,y0,width,height),
- new Point(baseRasterOriginX+deltaX,
- baseRasterOriginY+deltaY),
- this);
- }
-
- /**
- * Stores the data elements for all channels at the specified location.
- * There will be no explicit bounds checking on the parameters.
- * An ArrayOutOfBounds exception will be thrown at runtime
- * if data outside of the array is accessed.
- * 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.
- */
- public void setPixelData(int x, int y, Object obj) {
- sampleModel.setPixelData(x-baseRasterOriginX, y-baseRasterOriginY,
- obj, dataBuffer);
- }
-
- /**
- * Stores the Raster data at the specified location.
- * There will be no explicit bounds checking on the parameters.
- * An ArrayOutOfBounds exception will be thrown at runtime
- * if data outside of the array is accessed.
- * @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.
- */
- public void setPixelData(int x, int y, Raster inRaster) {
- int width = inRaster.getWidth();
- int height = inRaster.getHeight();
- int srcOffX = inRaster.getMinX();
- int srcOffY = inRaster.getMinY();
- int dstOffX = x+inRaster.getMinX();
- int dstOffY = y+inRaster.getMinY();
- Object tdata = null;
-
- for (int startY=0; startY < height; startY++) {
- tdata = inRaster.getPixelData(srcOffX, srcOffY+startY,
- width, 1, tdata);
- setPixelData(dstOffX, dstOffY+startY,
- width, 1, tdata);
- }
- }
-
- /**
- * Stores an array of data elements into the specified rectangular
- * region.
- * There will be no explicit bounds checking on the parameters.
- * An ArrayOutOfBounds exception will be thrown at runtime
- * if data outside of the array is accessed.
- * 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 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.
- */
- public void setPixelData(int x, int y, int w, int h, Object obj) {
- sampleModel.setPixelData(x-baseRasterOriginX,y-baseRasterOriginY,
- w,h,obj,dataBuffer);
- }
-
- /**
- * Copies pixels from Raster srcRaster to this WritableRaster. Each pixel
- * in srcRaster is copied to the same x,y address in this raster, unless
- * the address falls outside the bounds of this raster.
- * @param srcRaster The Raster from which to copy pixels.
- */
- public void setRect(Raster srcRaster) {
- setRect(0,0,srcRaster);
- }
-
- /**
- * Copies pixels from Raster srcRaster to this WritableRaster. For
- * each a,b address in srcRaster, the corresponding pixel is copied
- * to address a+x, b+y in this raster, unless a+x,b+y falls outside
- * the bounds of this raster.
- * @param x The X coordinate of the pixel location.
- * @param y The Y coordinate of the pixel location.
- * @param srcRaster The Raster from which to copy pixels.
- */
- public void setRect(int x, int y, Raster srcRaster) {
- int width = srcRaster.getWidth();
- int height = srcRaster.getHeight();
- int[] tdata = null;
- int srcOffX = srcRaster.getMinX();
- int srcOffY = srcRaster.getMinY();
- int dstOffX = x+srcRaster.getMinX();
- int dstOffY = y+srcRaster.getMinY();
-
-
- for (int startY=0; startY < height; startY++) {
- // Grab one scanline at a time
- tdata = srcRaster.getPixel(srcOffX, srcOffY+startY,
- width, 1, tdata);
- setPixel(dstOffX, dstOffY+startY,
- width, 1, tdata);
- }
- }
-
- /**
- * Sets a pixel in the DataBuffer using an int array for input.
- * @param x The X coordinate of the pixel location.
- * @param y The Y coordinate of the pixel location.
- * @param iarray The input pixels in a int array.
- */
- public void setPixel(int x, int y, int iarray[]) {
- sampleModel.setPixel(x-baseRasterOriginX,y-baseRasterOriginY,
- iarray,dataBuffer);
- }
-
- /**
- * Sets a pixel in the DataBuffer using a float array for input
- * specified pixel.
- * @param x The X coordinate of the pixel location.
- * @param y The Y coordinate of the pixel location.
- * @param farray The input pixels in a float array.
- */
- public void setPixel(int x, int y, float farray[]) {
- sampleModel.setPixel(x-baseRasterOriginX,y-baseRasterOriginY,
- farray,dataBuffer);
- }
-
- /**
- * Sets a pixel in the DataBuffer using a double array for input
- * specified pixel.
- * @param x The X coordinate of the pixel location.
- * @param y The Y coordinate of the pixel location.
- * @param darray The input pixels in a double array.
- */
- public void setPixel(int x, int y, double darray[]) {
- sampleModel.setPixel(x-baseRasterOriginX,y-baseRasterOriginY,
- darray,dataBuffer);
- }
-
- /**
- * Sets a region of pixels using in input pixel array.
- * @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.
- */
- public void setPixel(int x, int y, int w, int h, int iarray[]) {
- sampleModel.setPixel(x-baseRasterOriginX,y-baseRasterOriginY,
- w,h,iarray,dataBuffer);
- }
-
- /**
- * Sets region of pixels using in input pixel array.
- * @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 farray The input float pixel array.
- */
- public void setPixel(int x, int y, int w, int h, float farray[]) {
- sampleModel.setPixel(x-baseRasterOriginX,y-baseRasterOriginY,
- w,h,farray,dataBuffer);
- }
-
- /**
- * Sets region of pixels using in input pixel array.
- * @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 darray The input double pixel array.
- */
- public void setPixel(int x, int y, int w, int h, double darray[])
- {
- sampleModel.setPixel(x-baseRasterOriginX,y-baseRasterOriginY,
- w,h,darray,dataBuffer);
- }
-
- /**
- * Sets a sample in the DataBuffer using a int for input.
- * @param x The X coordinate of the pixel location.
- * @param y The Y coordinate of the pixel location.
- * @param b The band to set.
- * @param s The input sample.
- */
- public void setSample(int x, int y, int b, int s) {
- sampleModel.setSample(x-baseRasterOriginX, y-baseRasterOriginY, b, s,
- dataBuffer);
- }
-
- /**
- * Sets a sample in the DataBuffer using a float array for input.
- * @param x The X coordinate of the pixel location.
- * @param y The Y coordinate of the pixel location.
- * @param b The band to set.
- * @param s The input sample as a float.
- */
- public void setSample(int x, int y, int b, float s){
- sampleModel.setSample(x-baseRasterOriginX,y-baseRasterOriginY,
- b,s,dataBuffer);
- }
-
- /**
- * Sets a sample in the DataBuffer using a double array for input.
- * @param x The X coordinate of the pixel location.
- * @param y The Y coordinate of the pixel location.
- * @param b The band to set.
- * @param s The input sample as a double.
- */
- public void setSample(int x, int y, int b, double s){
- sampleModel.setSample(x-baseRasterOriginX,y-baseRasterOriginY,
- b,s,dataBuffer);
- }
-
- /**
- * Sets a region of samples using an input integer buffer as input.
- * @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 b The band to set.
- * @param iarray The input int sample array.
- */
- public void setSample(int x, int y, int w, int h, int b,
- int iarray[]) {
- sampleModel.setSample(x-baseRasterOriginX,y-baseRasterOriginY,
- w,h,b,iarray,dataBuffer);
- }
-
- /**
- * Sets a region of samples using an input float buffer as input.
- * @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 b The band to set.
- * @param farray The input float sample array.
- */
- public void setSample(int x, int y, int w, int h, int b,
- float farray[]) {
- sampleModel.setSample(x-baseRasterOriginX,y-baseRasterOriginY,
- w,h,b,farray,dataBuffer);
- }
-
- /**
- * Sets a region of samples using an input double buffer as input.
- * @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 b The band to set.
- * @param darray The input double sample array.
- */
- public void setSample(int x, int y, int w, int h, int b,
- double darray[]) {
- sampleModel.setSample(x-baseRasterOriginX,y-baseRasterOriginY,
- w,h,b,darray,dataBuffer);
- }
-
- }
-