home *** CD-ROM | disk | FTP | other *** search
Java Source | 1998-03-20 | 19.6 KB | 602 lines |
- /*
- * @(#)BandedSampleModel.java 1.11 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;
-
- /**
- * This class extends the SampleModel. It provides more efficent
- * implementations for accessing image data than are provided in
- * SampleModel. This class will be used when working with images
- * which store sample data for each band in a different bank of the
- * DataBuffer. Accessor methods are provided so that image data can be
- * manipulated directly. Pixelstride is the number of
- * data array elements between two samples for the same band on the same
- * scanline. Scanlinestride is the number of data array elements between
- * a given sample and the sample in the same column of the next scanline.
- * Bank Index denotes the correspondence between a bank of the data buffer
- * and a band of image data. The image bands and the DataBuffer banks are
- * numbered from 0 to n-1.
- */
-
-
- public class BandedSampleModel extends SampleModel
- {
- /** Line stride (in data array elements) of the region of image data
- * described by this BandedSampleModel.
- */
- int scanlineStride;
-
- /** Pixel stride (in data array elements) of the region of image
- * data described by this ComponentSampleModel.
- */
- int pixelStride;
-
- /** A tag specifying the data type. Tags defined in DataBuffer. */
- int dataType;
-
- /** Index for each bank storing a band of image data. */
- int bankIndices[];
-
- /**
- * Constructs a BandedSampleModel with the specified parameters.
- * The number of bands will be given by the length of the bandOffsets array.
- * @param dataType The data type for storing samples.
- * @param w The width (in pixels) of the region of
- * image data described.
- * @param h The height (in pixels) of the region of image
- * data described.
- * @param numBands The number of bands for the region of image data
- * described.
- */
- public BandedSampleModel(int dataType, int w, int h, int numBands) {
- super(dataType, w, h, numBands);
- this.dataType = dataType;
- this.scanlineStride = width;
- this.pixelStride = 1;
- this.bankIndices = new int[numBands];
- for (int i=0; i<numBands; i++)
- bankIndices[i] = i;
- }
-
- /**
- * Constructs a BandedSampleModel with the specified parameters.
- * @param dataType The data type for storing samples.
- * @param w The width (in pixels) of the region of
- * image data described.
- * @param h The height (in pixels) of the region of
- * image data described.
- * @param numBands The number of bands for the region of image data
- * described.
- * @param scanlineStride The line stride of the region of the
- * image data described.
- * @param pixelStride The pixel stride of the region of image
- * data described.
- * @param bankIndices The bank index for each band.
- */
- public BandedSampleModel(int dataType,
- int w, int h, int numBands,
- int scanlineStride, int pixelStride,
- int bankIndices[]) {
-
- super(dataType, w, h, numBands);
- if ( numBands > bankIndices.length)
- throw new RasterFormatException("There are only " +
- bankIndices.length + " data banks");
- this.dataType = dataType;
- this.scanlineStride = scanlineStride;
- this.pixelStride = pixelStride;
- this.bankIndices = bankIndices;
- }
-
- /**
- * This creates a new BandedSampleModel with the specified
- * width and height. As much as possible the 'style' of the
- * current BandedSampleModel will be preserved.
- */
- public SampleModel createCompatibleSampleModel(int w, int h) {
- SampleModel sampleModel =
- new BandedSampleModel(dataType, w, h, numBands,
- scanlineStride, pixelStride,
- bankIndices);
- return sampleModel;
- }
-
- /**
- * This creates a new SampleModel with the specified
- * width and height and with a subset of the bands of this
- * SampleModel. Here there will be no explicit check made to ensure that
- * the subset bands asked for actually exist in the parent.
- */
- public SampleModel createSubsetSampleModel(int w, int h, int bands[]) {
- if (bands.length > bankIndices.length)
- throw new RasterFormatException("There are only " +
- bankIndices.length +
- " bands");
- int newBankIndices[] = new int[bands.length];
- for (int i=0; i<bands.length; i++)
- newBankIndices[i] = bankIndices[bands[i]];
- return new BandedSampleModel(this.dataType, w, h, bands.length,
- this.scanlineStride, this.pixelStride,
- newBankIndices);
- }
-
- /**
- * Creates a DataBuffer that corresponds to this BandedSampleModel,
- * The DataBuffer's width and height will match this BandedSampleModel's.
- */
- public DataBuffer createCompatibleDataBuffer() {
- DataBuffer dataBuffer = null;
-
- int size = scanlineStride * height;
- switch (dataType) {
- case DataBuffer.BYTE_DATA:
- dataBuffer = new DataBufferByte(size, numBands);
- break;
- case DataBuffer.SHORT_DATA:
- dataBuffer = new DataBufferShort(size, numBands);
- break;
- case DataBuffer.INT_DATA:
- dataBuffer = new DataBufferInt(size, numBands);
- break;
- }
-
- return dataBuffer;
- }
-
-
- /**
- * Creates a DataBuffer that corresponds to this BandedSampleModel,
- * with a different width and height.
- */
- public DataBuffer createCompatibleDataBuffer(int desiredWidth,
- int desiredHeight) {
- DataBuffer dataBuffer = null;
- int size = desiredWidth * pixelStride * desiredHeight;
-
- switch (dataType) {
- case DataBuffer.BYTE_DATA:
- dataBuffer = new DataBufferByte(size, numBands);
- break;
- case DataBuffer.SHORT_DATA:
- dataBuffer = new DataBufferShort(size, numBands);
- break;
- case DataBuffer.INT_DATA:
- dataBuffer = new DataBufferInt(size, numBands);
- break;
- }
- return dataBuffer;
- }
-
- /** Gets offset of the first band of pixel (x,y).
- * A sample of the first band can be retrieved from a dataBuffer
- * data with a BandedSampleModel bsm as
- * <pre>
- * data.getElem(bsm.getOffset(x, y));
- * </pre>
- */
- public long getOffset(int x, int y) {
- long offset = y*scanlineStride + x*pixelStride;
- return offset;
- }
-
- /** Returns the size in bits of samples for all bands. */
- public int[] getSampleSize() {
- int sampleSize[] = new int [numBands];
- int sizeInBits = 0;
- switch (dataType) {
- case DataBuffer.BYTE_DATA:
- sizeInBits = 8;
- break;
- case DataBuffer.SHORT_DATA:
- sizeInBits = 16;
- break;
- case DataBuffer.INT_DATA:
- sizeInBits = 32;
- break;
- }
-
- for (int i=0; i<numBands; i++)
- sampleSize[i] = sizeInBits;
-
- return sampleSize;
- }
-
- /** Returns the size in bits of samples for the specified band. */
- public int getSampleSize(int band) {
- int sampleSize = 0;
- switch (dataType) {
- case DataBuffer.BYTE_DATA:
- sampleSize = 8;
- break;
- case DataBuffer.SHORT_DATA:
- sampleSize = 16;
- break;
- case DataBuffer.INT_DATA:
- sampleSize = 32;
- break;
- }
- return sampleSize;
- }
-
- /** Returns the bank indices for all bands. */
- public int [] getBankIndices() {
- return bankIndices;
- }
-
- /** Returns the scanline stride of this BandedSampleModel. */
- public int getScanlineStride() {
- return scanlineStride;
- }
-
- /** Returns the pixel stride of this BandedSampleModel. */
- public int getPixelStride() {
- return pixelStride;
- }
-
- /**
- * Returns the number of data elements required to store
- * one pixel. For a BandedSampleModel, this is identical to the
- * number of bands.
- */
- public int getNumDataElements() {
- return getNumBands();
- }
-
- /**
- * Returns the transfer type of the underlying data. For a
- * ComponentSampleModel, this is identical to the dataType.
- */
- public int getTransferType() {
- return dataType;
- }
-
- /**
- * Returns the pixel data in an array of primitives that can be byte,
- * short or int. Which primitive type is returned depends on
- * the transfer type. Data is returned in the packed format,
- * thus increasing efficiency for data transfers. Generally, obj
- * should be passed in as null, so that the Object will be created
- * automatically and will be of the right primitive data type.
- * <pre>
- * BandedSampleModel bsm1, bsm2;
- * DataBufferInt db1, db2;
- * bsm2.setPixelData(x, y, bsm1.getPixelData(x, y, null, db1), db2);
- * </pre>
- * @param x The X coordinate of the pixel location.
- * @param y The Y coordinate of the pixel location.
- * @param obj If non-null, returns the primitive array in this object.
- * @param data The DataBuffer containing the image data.
- */
- public Object getPixelData(int x, int y, Object obj, DataBuffer data) {
-
- int type = getTransferType();
- int numDataElems = getNumDataElements();
- int pixelOffset = y*scanlineStride + x*pixelStride;
-
- switch(type) {
-
- case DataBuffer.BYTE_DATA:
-
- byte[] bdata;
-
- if (obj == null)
- bdata = new byte[numDataElems];
- else
- bdata = (byte[])obj;
-
- for (int i=0; i<numDataElems; i++) {
- bdata[i] = (byte)data.getElem(bankIndices[i], pixelOffset);
- }
-
- obj = (Object)bdata;
- break;
-
- case DataBuffer.SHORT_DATA:
-
- short[] sdata;
-
- if (obj == null)
- sdata = new short[numDataElems];
- else
- sdata = (short[])obj;
-
- for (int i=0; i<numDataElems; i++) {
- sdata[i] = (short)data.getElem(bankIndices[i], pixelOffset);
- }
-
- obj = (Object)sdata;
- break;
-
- case DataBuffer.INT_DATA:
-
- int[] idata;
-
- if (obj == null)
- idata = new int[numDataElems];
- else
- idata = (int[])obj;
-
- for (int i=0; i<numDataElems; i++) {
- idata[i] = data.getElem(bankIndices[i], pixelOffset);
- }
-
- obj = (Object)idata;
- break;
- }
-
- return obj;
- }
-
- /**
- * Returns all samples for the specified pixel in an int array.
- * @param x The X coordinate of the pixel location.
- * @param y The Y coordinate of the pixel location.
- * @param iArray If non-null, returns the samples in this array.
- * @param data The DataBuffer containing the image data.
- */
- public int[] getPixel(int x, int y, int iArray[], DataBuffer data) {
-
- int pixels[];
-
- if (iArray != null)
- pixels = iArray;
- else
- pixels = new int [numBands];
-
- int pixelOffset = y*scanlineStride + x*pixelStride;
- for (int i=0; i<numBands; i++) {
- pixels[i] = data.getElem(bankIndices[i], pixelOffset);
- }
- return pixels;
- }
-
- /**
- * Returns all samples for the specified rectangle of pixels in
- * an int array, one sample per data array element.
- * @param x The X coordinate of the upper left pixel location.
- * @param y The Y coordinate of the upper left pixel location.
- * @param w The width of the pixel rectangle.
- * @param h The height of the pixel rectangle.
- * @param iArray If non-null, returns the samples in this array.
- * @param data The DataBuffer containing the image data.
- */
- public int[] getPixel(int x, int y, int w, int h,
- int iArray[], DataBuffer data) {
- int pixels[];
-
- if (iArray != null)
- pixels = iArray;
- else
- pixels = new int [w*h*numBands];
-
- int lineOffset = y*scanlineStride + x*pixelStride;
- int srcOffset = 0;
-
- for (int i = 0; i < h; i++) {
- int pixelOffset = lineOffset;
- for (int j = 0; j < w; j++) {
- for (int k=0; k < numBands; k++) {
- pixels[srcOffset++] =
- data.getElem(bankIndices[k], pixelOffset);
- }
- pixelOffset += pixelStride;
- }
- lineOffset += scanlineStride;
- }
- return pixels;
- }
-
- /**
- * Returns as int the sample in a specified band for the pixel
- * located at (x,y).
- * @param x The X coordinate of the pixel location.
- * @param y The Y coordinate of the pixel location.
- * @param b The band to return.
- * @param data The DataBuffer containing the image data.
- */
- public int getSample(int x, int y, int b, DataBuffer data) {
- int sample = data.getElem(bankIndices[b],
- y*scanlineStride + x*pixelStride);
- return sample;
- }
-
- /**
- * Returns the samples in a specified band for the specified rectangle
- * of pixels in an int array, one sample per data array element.
- * @param x The X coordinate of the upper left pixel location.
- * @param y The Y coordinate of the upper left pixel location.
- * @param w The width of the pixel rectangle.
- * @param h The height of the pixel rectangle.
- * @param b The band to return.
- * @param iArray If non-null, returns the samples in this array.
- * @param data The DataBuffer containing the image data.
- */
- public int[] getSample(int x, int y, int w, int h, int b,
- int iArray[], DataBuffer data) {
- int samples[];
- int bank = bankIndices[b];
-
- if (iArray != null)
- samples = iArray;
- else
- samples = new int [w*h];
-
- int lineOffset = y*scanlineStride + x*pixelStride;
- int srcOffset = 0;
-
- for (int i = 0; i < h; i++) {
- int sampleOffset = lineOffset;
- for (int j = 0; j < w; j++) {
- samples[srcOffset++] =
- data.getElem(bank, sampleOffset);
- sampleOffset += pixelStride;
- }
- lineOffset += scanlineStride;
- }
- return samples;
- }
-
- /**
- * Puts the pixel data from an Object that contains an
- * array of primitives that can be byte,
- * short or int. Which primitive type it contains depends on
- * the transfer type. Data in the Object is in the packed format,
- * thus increasing efficiency for data transfers.
- * <pre>
- * BandedSampleModel bsm1, bsm2;
- * DataBufferInt db1, db2;
- * bsm2.setPixelData(x, y, bsm1.getPixelData(x, y, null, db1), db2);
- * </pre>
- * @param x The X coordinate of the pixel location.
- * @param y The Y coordinate of the pixel location.
- * @param obj If non-null, returns the primitive array in this object.
- * @param data The DataBuffer containing the image data.
- */
- public void setPixelData(int x, int y, Object obj, DataBuffer data) {
-
- int type = getTransferType();
- int numDataElems = getNumDataElements();
- int pixelOffset = y*scanlineStride + x*pixelStride;
-
- switch(type) {
-
- case DataBuffer.BYTE_DATA:
-
- byte[] barray = (byte[])obj;
-
- for (int i=0; i<numDataElems; i++) {
- data.setElem(bankIndices[i], pixelOffset,
- ((int)barray[i])&0xff);
- }
- break;
-
- case DataBuffer.SHORT_DATA:
-
- short[] sarray = (short[])obj;
-
- for (int i=0; i<numDataElems; i++) {
- data.setElem(bankIndices[i], pixelOffset,
- ((int)sarray[i])&0xffff);
- }
- break;
-
- case DataBuffer.INT_DATA:
-
- int[] iarray = (int[])obj;
-
- for (int i=0; i<numDataElems; i++) {
- data.setElem(bankIndices[i], pixelOffset, iarray[i]);
- }
- break;
-
- }
- }
-
- /**
- * Sets a pixel in the DataBuffer using an int array of samples for input.
- * @param x The X coordinate of the pixel location.
- * @param y The Y coordinate of the pixel location.
- * @param iArray The input samples in an int array.
- * @param data The DataBuffer containing the image data.
- */
- public void setPixel(int x, int y, int iArray[], DataBuffer data) {
- int pixelOffset = y*scanlineStride + x*pixelStride;
- for (int i=0; i<numBands; i++) {
- data.setElem(bankIndices[i], pixelOffset, iArray[i]);
- }
- }
-
- /**
- * Sets all samples for a rectangle of pixels from an int array containing
- * one sample per array element.
- * @param x The X coordinate of the upper left pixel location.
- * @param y The Y coordinate of the upper left pixel location.
- * @param w The width of the pixel rectangle.
- * @param h The height of the pixel rectangle.
- * @param iArray The input samples in an int array.
- * @param data The DataBuffer containing the image data.
- */
- public void setPixel(int x, int y, int w, int h,
- int iArray[], DataBuffer data) {
-
- int lineOffset = y*scanlineStride + x*pixelStride;
- int srcOffset = 0;
-
- for (int i = 0; i < h; i++) {
- int pixelOffset = lineOffset;
- for (int j = 0; j < w; j++) {
- for (int k=0; k < numBands; k++) {
- data.setElem(bankIndices[k], pixelOffset,
- iArray[srcOffset++]);
- }
- pixelOffset += pixelStride;
- }
- lineOffset += scanlineStride;
- }
- }
-
- /**
- * Sets a sample in the specified band for the pixel located at (x,y)
- * in the DataBuffer using an 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 as an int.
- * @param data The DataBuffer containing the image data.
- */
- public void setSample(int x, int y, int b, int s,
- DataBuffer data) {
- data.setElem(bankIndices[b], y*scanlineStride + x*pixelStride, s);
- }
-
- /**
- * Sets the samples in the specified band for the specified rectangle
- * of pixels from an int array containing one sample per data array element.
- * @param x The X coordinate of the upper left pixel location.
- * @param y The Y coordinate of the upper left pixel location.
- * @param w The width of the pixel rectangle.
- * @param h The height of the pixel rectangle.
- * @param b The band to set.
- * @param iArray The input sample array.
- * @param data The DataBuffer containing the image data.
- */
- public void setSample(int x, int y, int w, int h, int b,
- int iArray[], DataBuffer data) {
- int lineOffset = y*scanlineStride + x*pixelStride;
- int srcOffset = 0;
- int bank = bankIndices[b];
-
- for (int i = 0; i < h; i++) {
- int sampleOffset = lineOffset;
- for (int j = 0; j < w; j++) {
- data.setElem(bank, sampleOffset, iArray[srcOffset++]);
- sampleOffset += pixelStride;
- }
- lineOffset += scanlineStride;
- }
- }
-
- }
-
-