home *** CD-ROM | disk | FTP | other *** search
Java Source | 1998-03-20 | 23.0 KB | 695 lines |
- /*
- * @(#)ComponentSampleModel.java 1.14 98/03/18
- *
- * Copyright 1997 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 SampleModel. It stores the N samples which
- * make up a pixel in N separate data array elements all of which
- * are in the same bank of a dataBuffer. All data array elements reside
- * in the first bank of a DataBuffer. Each sample occupies one data
- * array element. Accessor methods are
- * provided so that image data can be manipulated directly. This class can
- * support different kinds of interleaving. 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.
- * Band offsets denotes the number of data array elements to the start
- * of each band in a pixel. The bands are numbered from 0 to n-1.
- */
-
- public class ComponentSampleModel extends SampleModel
- {
- /** Offsets for all bands in data array elements. */
- private int bandOffsets[];
-
- /**
- * Line stride (in data array elements) of the region of image
- * data described by this ComponentSampleModel.
- */
- private int scanlineStride;
-
- /** Pixel stride (in data array elements) of the region of image
- * data described by this ComponentSampleModel.
- */
- private int pixelStride;
-
- /** A tag specifying the data type. Tags defined in DataBuffer. */
- private int dataType;
-
- static private native void initIDs();
- static {
- ColorModel.loadLibraries();
- initIDs();
- }
-
- /**
- * Constructs a ComponentSampleModel 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 pixelStride The pixel stride of the region of image
- * data described.
- * @param scanlineStride The line stride of the region of image
- * data described.
- * @param bandOffsets The offsets of all bands.
- */
- public ComponentSampleModel(int dataType,
- int w, int h,
- int pixelStride,
- int scanlineStride,
- int bandOffsets[]) {
- super(dataType, w, h, bandOffsets.length);
- this.dataType = dataType;
- this.pixelStride = pixelStride;
- this.scanlineStride = scanlineStride;
- this.bandOffsets = (int[])bandOffsets.clone();
- }
-
-
- /**
- * Returns the size of the data buffer (in data elements) needed
- * for a data buffer that matches this ComponentSampleModel.
- */
- private long getBufferSize() {
- int maxBandOff=bandOffsets[0];
- for (int i=1; i<bandOffsets.length; i++)
- maxBandOff = Math.max(maxBandOff,bandOffsets[i]);
-
- long size = 0;
- if (maxBandOff >= 0)
- size += maxBandOff+1;
- if (pixelStride > 0)
- size += pixelStride * (width-1);
- if (scanlineStride > 0)
- size += scanlineStride*(height-1);
- return size;
- }
-
- /**
- * Sorts band offsets so that they are in min to max order
- */
- private int []orderBands(int orig[], int step) {
- int ret[] = (int[])orig.clone();
-
- for (int i = 0; i < ret.length; i++) {
- int index = i;
- for (int j = i+1; j < ret.length; j++) {
- if (ret[index] > ret[j]) {
- index = j;
- }
- }
- int tmp = ret[index];
- ret[index] = ret[i];
- ret[i] = tmp;
- }
- return ret;
- }
-
- /**
- * This creates a new ComponentSampleModel with the specified
- * width and height.
- */
- public SampleModel createCompatibleSampleModel(int w, int h) {
- SampleModel ret=null;
- long size;
- int minBandOff=bandOffsets[0];
- int maxBandOff=bandOffsets[0];
- for (int i=1; i<bandOffsets.length; i++) {
- minBandOff = Math.min(minBandOff,bandOffsets[i]);
- maxBandOff = Math.max(maxBandOff,bandOffsets[i]);
- }
- maxBandOff -= minBandOff;
-
- int bands = bandOffsets.length;
- int bandOff[];
- int pStride = Math.abs(pixelStride);
- int lStride = Math.abs(scanlineStride);
- int bStride = Math.abs(maxBandOff);
-
- if (pStride > lStride) {
- if (pStride > bStride) {
- if (lStride > bStride) { // pix > line > band
- bandOff = new int[bandOffsets.length];
- for (int i=0; i<bands; i++)
- bandOff[i] = bandOffsets[i]-minBandOff;
- lStride = bStride+1;
- pStride = lStride*h;
- } else { // pix > band > line
- bandOff = orderBands(bandOffsets,lStride*h);
- pStride = bands*lStride*h;
- }
- } else { // band > pix > line
- pStride = lStride*h;
- bandOff = orderBands(bandOffsets,pStride*w);
- }
- } else {
- if (pStride > bStride) { // line > pix > band
- bandOff = new int[bandOffsets.length];
- for (int i=0; i<bands; i++)
- bandOff[i] = bandOffsets[i]-minBandOff;
- pStride = bStride+1;
- lStride = pStride*w;
- } else {
- if (lStride > bStride) { // line > band > pix
- bandOff = orderBands(bandOffsets,pStride*w);
- lStride = bands*pStride*w;
- } else { // band > line > pix
- lStride = pStride*w;
- bandOff = orderBands(bandOffsets,lStride*h);
- }
- }
- }
-
- // make sure we make room for negative offsets...
- int base = 0;
- if (scanlineStride < 0) {
- base += lStride*h;
- lStride *= -1;
- }
- if (pixelStride < 0) {
- base += pStride*w;
- pStride *= -1;
- }
-
- for (int i=0; i<bands; i++)
- bandOff[i] += base;
- return new ComponentSampleModel(dataType, w, h, pStride,
- lStride, bandOff);
- }
-
- /**
- * This creates a new ComponentSampleModel with the specified
- * width and height and with a subset of the bands of this
- * ComponentSampleModel.
- */
- public SampleModel createSubsetSampleModel(int w, int h, int bands[]) {
- if ((w!=width) || (h!=height)) {
- SampleModel sm = createCompatibleSampleModel(w, h);
- return sm.createSubsetSampleModel(w, h, bands);
- } else {
- int newBandOffsets[] = new int[bands.length];
- for (int i=0; i<bands.length; i++)
- newBandOffsets[i] = bandOffsets[bands[i]];
- return new ComponentSampleModel(this.dataType, w, h,
- this.pixelStride,
- this.scanlineStride,
- newBandOffsets);
- }
- }
-
- /**
- * Creates a DataBuffer that corresponds to this ComponentSampleModel.
- * The DataBuffer's width and height will match this ComponentSampleModel's.
- */
- public DataBuffer createCompatibleDataBuffer() {
- DataBuffer dataBuffer = null;
-
- int size = (int)getBufferSize();
- switch (dataType) {
- case DataBuffer.BYTE_DATA:
- dataBuffer = new DataBufferByte(size);
- break;
- case DataBuffer.SHORT_DATA:
- dataBuffer = new DataBufferShort(size);
- break;
- case DataBuffer.INT_DATA:
- dataBuffer = new DataBufferInt(size);
- break;
- }
-
- return dataBuffer;
- }
-
-
- /**
- * Creates a DataBuffer that corresponds to this ComponentSampleModel,
- * with a different width and height.
- */
- public DataBuffer createCompatibleDataBuffer(int desiredWidth,
- int desiredHeight) {
- DataBuffer dataBuffer = null;
-
- int size = (int)(desiredWidth*desiredHeight*numBands);
- switch (dataType) {
- case DataBuffer.BYTE_DATA:
- dataBuffer = new DataBufferByte(size);
- break;
- case DataBuffer.SHORT_DATA:
- dataBuffer = new DataBufferShort(size);
- break;
- case DataBuffer.INT_DATA:
- dataBuffer = new DataBufferInt(size);
- 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 ComponentSampleModel csm as
- * <pre>
- * data.getElem(csm.getOffset(x, y));
- * </pre>
- */
- public long getOffset(int x, int y) {
- long offset = y*scanlineStride + x*pixelStride;
- return offset;
- }
-
- /** Gets offset for band b of pixel (x,y).
- * A sample of band b can be retrieved from a dataBuffer data with a
- * ComponentSampleModel csm as
- * <pre>
- * data.getElem(csm.getOffset(x, y, b));
- * </pre>
- */
- public long getOffset(int x, int y, int b) {
- long offset = y*scanlineStride + x*pixelStride + bandOffsets[b];
- 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;
- }
-
- /**
- * Gets the difference, in data array elements, between a sample of
- * fromBand and a sample of toBand for the same pixel.
- */
- public long getDifference(int fromBand, int toBand) {
- return bandOffsets[toBand] - bandOffsets[fromBand];
- }
-
- /** Returns the band offset of all bands. */
- public int [] getBandOffsets() {
- return (int[])bandOffsets.clone();
- }
-
- /** Returns the scanline stride of this ComponentSampleModel. */
- public int getScanlineStride() {
- return scanlineStride;
- }
-
- /** Returns the pixel stride of this ComponentSampleModel. */
- public int getPixelStride() {
- return pixelStride;
- }
-
- /**
- * Returns the number of data elements required to store
- * one pixel. For a ComponentSampleModel 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>
- * ComponentSampleModel csm1, csm2;
- * DataBufferInt db1, db2;
- * csm2.setPixelData(x, y, csm1.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(pixelOffset + bandOffsets[i]);
- }
-
- 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(pixelOffset + bandOffsets[i]);
- }
-
- 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(pixelOffset + bandOffsets[i]);
- }
-
- 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(pixelOffset + bandOffsets[i]);
- }
- 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(pixelOffset + bandOffsets[k]);
- }
- 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(y*scanlineStride + x*pixelStride +
- bandOffsets[b]);
- 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[];
- if (iArray != null) {
- samples = iArray;
- } else {
- samples = new int [w*h];
- }
- int lineOffset = y*scanlineStride + x*pixelStride + bandOffsets[b];
- int srcOffset = 0;
-
- for (int i = 0; i < h; i++) {
- int sampleOffset = lineOffset;
- for (int j = 0; j < w; j++) {
- samples[srcOffset++] = data.getElem(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>
- * ComponentSampleModel csm1, csm2;
- * DataBufferInt db1, db2;
- * csm2.setPixelData(x, y, csm1.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(pixelOffset + bandOffsets[i],
- ((int)barray[i])&0xff);
- }
- break;
-
- case DataBuffer.SHORT_DATA:
-
- short[] sarray = (short[])obj;
-
- for (int i=0; i<numDataElems; i++) {
- data.setElem(pixelOffset + bandOffsets[i],
- ((int)sarray[i])&0xffff);
- }
- break;
-
- case DataBuffer.INT_DATA:
-
- int[] iarray = (int[])obj;
-
- for (int i=0; i<numDataElems; i++) {
- data.setElem(pixelOffset + bandOffsets[i], 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(pixelOffset + bandOffsets[i],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(pixelOffset + bandOffsets[k],
- 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(y*scanlineStride + x*pixelStride + bandOffsets[b], 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 samples in an int 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 + bandOffsets[b];
- int srcOffset = 0;
-
- for (int i = 0; i < h; i++) {
- int sampleOffset = lineOffset;
- for (int j = 0; j < w; j++) {
- data.setElem(sampleOffset, iArray[srcOffset++]);
- sampleOffset += pixelStride;
- }
- lineOffset += scanlineStride;
- }
- }
- }
-
-