home *** CD-ROM | disk | FTP | other *** search
Java Source | 1998-03-20 | 19.1 KB | 600 lines |
- /*
- * @(#)SinglePixelPackedSampleModel.java 1.12 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 SampleModel. It stores (packs) the N samples which make
- * up a single pixel in 1 data array element. All data array elements reside
- * in the first bank of a DataBuffer. Accessor methods are provided so
- * that the image data can be manipulated directly. Scanlinestride is the
- * number of data array elements between a given sample and the sample in
- * the same column of the next scanline. Bit Masks are the masks required
- * to extract the samples representing the bands of the pixel.
- */
-
- public class SinglePixelPackedSampleModel extends SampleModel
- {
- /** Bit masks for all bands of the image data. */
- private int bitMasks[];
-
- /** Bit Offsets for all bands of the image data. */
- private int bitOffsets[];
-
- /** Bit sizes for all the bands of the image data. */
- private int bitSizes[];
-
- /** Total bit size. */
- private int totalBitSize;
-
- /** Line stride of the region of image data described by this
- * SinglePixelPackedSampleModel.
- */
- private int scanlineStride;
-
- /**
- * Constructs a SinglePixelPackedSampleModel with bitMasks.length bands.
- * Each sample is stored in a data array element in the position of
- * its corresponding bit mask. Bit masks should be contiguous and
- * should not overlap.
- * @param dataType The data type for storing samples.
- * @param w The width (in pixels) of the region of the
- * image data described.
- * @param h The height (in pixels) of the region of the
- * image data described.
- * @param bitMasks The bit masks for various bands.
- */
- public SinglePixelPackedSampleModel(int dataType, int w, int h,
- int bitMasks[]) {
- this(dataType, w, h, w, bitMasks);
- }
-
- /**
- * Constructs a SinglePixelPackedSampleModel with bitMasks.length bands
- * and a scanline stride equal to scanlineStride data array elements.
- * Each sample is stored in a data array element in the position of
- * its corresponding bit mask. Bit masks should be contiguous and
- * should not overlap.
- * @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 scanlineStride The line stride of the region of image
- * data described.
- * @param bitMasks The bit masks for various bands.
- */
- public SinglePixelPackedSampleModel(int dataType, int w, int h,
- int scanlineStride, int bitMasks[]) {
- super(dataType, w, h, bitMasks.length);
- this.dataType = dataType;
- this.bitMasks = bitMasks;
- this.scanlineStride = scanlineStride;
-
- this.bitOffsets = new int[numBands];
- this.bitSizes = new int[numBands];
-
- this.totalBitSize = 0;
- for (int i=0; i<numBands; i++) {
- int bitOffset = 0, bitSize = 0, mask;
- mask = bitMasks[i];
-
- if (mask != 0) {
- while ((mask & 1) == 0) {
- mask = mask >>> 1;
- bitOffset++;
- }
- while ((mask & 1) == 1) {
- mask = mask >>> 1;
- bitSize++;
- }
- }
- bitOffsets[i] = bitOffset;
- bitSizes[i] = bitSize;
- totalBitSize += bitSize;
- }
- }
-
- /**
- * Returns the number of data elements that are required to store
- * one pixel. For a SinglePixelPackedSampleModel, this is always one.
- */
- public int getNumDataElements() {
- return 1;
- }
-
- /**
- * Returns the size of the buffer (in data array elements)
- * needed for a data buffer that matches this
- * SinglePixelPackedSampleModel.
- */
- private long getBufferSize() {
- long size = width * height;
- return size;
- }
-
- /**
- * This creates a new SinglePixelPackedSampleModel with the specified
- * width and height.
- */
- public SampleModel createCompatibleSampleModel(int w, int h) {
- SampleModel sampleModel = new SinglePixelPackedSampleModel(dataType, w, h,
- bitMasks);
- return sampleModel;
- }
-
- /**
- * Creates a DataBuffer that corresponds to this SinglePixelPackedSampleModel.
- * The DataBuffer's width and height will match this
- * SinglePixelPackedSampleModel'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 SinglePixelPackedSampleModel,
- * 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;
- }
-
-
- /** Returns the size of a pixel in bits. */
- public int getPixelBitSize() {
- int bitSize = 0;
- int sampleSize[] = getSampleSize();
- for (int i=0; i<sampleSize.length; i++) {
- bitSize += sampleSize[i];
- }
-
- return bitSize;
- }
-
- /** Returns the size in bits of samples for all bands. */
- public int[] getSampleSize() {
- int mask;
- int sampleSize[] = new int [numBands];
- for (int i=0; i<numBands; i++) {
- sampleSize[i] = 0;
- mask = bitMasks[i] >>> bitOffsets[i];
- while ((mask & 1) != 0) {
- sampleSize[i] ++;
- mask = mask >>> 1;
- }
- }
-
- return sampleSize;
- }
-
- /** Returns the size in bits of samples for the specified band. */
- public int getSampleSize(int band) {
- int sampleSize = 0;
- int mask = bitMasks[band] >>> bitOffsets[band];
- while ((mask & 1) != 0) {
- sampleSize ++;
- mask = mask >>> 1;
- }
-
- return sampleSize;
- }
-
- /** Returns the offset (in data array elements) of pixel (x,y).
- * A sample of the first band can be retrieved from a dataBuffer
- * data with a SinglePixelPackedSampleModel sppsm as
- * <pre>
- * data.getElem(sppsm.getOffset(x, y));
- * </pre>
- */
- public long getOffset(int x, int y) {
- long offset = y * scanlineStride + x;
- return offset;
- }
-
- /** Returns the bit offset into the data array element representing
- * a pixel for each band. */
- public int [] getBitOffsets() {
- return (int[])bitOffsets.clone();
- }
-
- /** Returns the size in bits of each band. */
- public int [] getBitSizes() {
- return (int[])bitSizes.clone();
- }
-
- /** Returns the bit masks for each band. */
- public int [] getBitMasks() {
- return (int[])bitMasks.clone();
- }
-
- /** Returns the scanline stride of this SinglePixelPackedSampleModel. */
- public int getScanlineStride() {
- return scanlineStride;
- }
-
- /** Returns the transfer type of the data */
- public int getTransferType() {
- return dataType;
- }
-
- /**
- * This creates a new SinglePixelPackedSampleModel with the specified
- * width and height and with a subset of the bands of this
- * SinglePixelPackedSampleModel.
- */
- public SampleModel createSubsetSampleModel(int w, int h, int bands[]) {
- if (bands.length > numBands)
- throw new RasterFormatException("There are only " +
- numBands +
- " bands");
- int newBitMasks[] = new int[bands.length];
- for (int i=0; i<bands.length; i++)
- newBitMasks[i] = bitMasks[bands[i]];
-
- return new SinglePixelPackedSampleModel(this.dataType, w, h,
- this.scanlineStride, newBitMasks);
- }
-
- /**
- * 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>
- * SinglePixelPackedSampleModel sppsm1, sppsm2;
- * DataBufferInt db1, db2;
- * sppsm2.setPixelData(x, y, sppsm1.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();
-
- switch(type) {
-
- case DataBuffer.BYTE_DATA:
-
- byte[] bdata;
-
- if (obj == null)
- bdata = new byte[1];
- else
- bdata = (byte[])obj;
-
- bdata[0] = (byte)data.getElem(y * scanlineStride + x);
-
- obj = (Object)bdata;
- break;
-
- case DataBuffer.SHORT_DATA:
-
- short[] sdata;
-
- if (obj == null)
- sdata = new short[1];
- else
- sdata = (short[])obj;
-
- sdata[0] = (short)data.getElem(y * scanlineStride + x);
-
- obj = (Object)sdata;
- break;
-
- case DataBuffer.INT_DATA:
-
- int[] idata;
-
- if (obj == null)
- idata = new int[1];
- else
- idata = (int[])obj;
-
- idata[0] = data.getElem(y * scanlineStride + x);
-
- obj = (Object)idata;
- break;
- }
-
- return obj;
- }
-
- /**
- * Returns all samples in 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 = new int [numBands];
- } else {
- pixels = iArray;
- }
-
- int value = data.getElem(y * scanlineStride + x);
- for (int i=0; i<numBands; i++) {
- pixels[i] = (value & bitMasks[i]) >>> bitOffsets[i];
- }
- return pixels;
- }
-
- /**
- * Returns all samples for the specified rectangle of pixels in
- * an int array, 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 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;
- int dstOffset = 0;
-
- for (int i = 0; i < h; i++) {
- for (int j = 0; j < w; j++) {
- int value = data.getElem(lineOffset+j);
- for (int k=0; k < numBands; k++) {
- pixels[dstOffset++] =
- ((value & bitMasks[k]) >>> bitOffsets[k]);
- }
- }
- 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);
- return ((sample & bitMasks[b]) >>> bitOffsets[b]);
- }
-
- /**
- * 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;
- int dstOffset = 0;
-
- for (int i = 0; i < h; i++) {
- for (int j = 0; j < w; j++) {
- int value = data.getElem(lineOffset+j);
- samples[dstOffset++] =
- ((value & bitMasks[b]) >>> bitOffsets[b]);
- }
- 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>
- * SinglePixelPackedSampleModel sppsm1, sppsm2;
- * DataBufferInt db1, db2;
- * sppsm2.setPixelData(x, y, sppsm1.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();
-
- switch(type) {
-
- case DataBuffer.BYTE_DATA:
-
- byte[] barray = (byte[])obj;
- data.setElem(y*scanlineStride+x, ((int)barray[0])&0xff);
- break;
-
- case DataBuffer.SHORT_DATA:
-
- short[] sarray = (short[])obj;
- data.setElem(y*scanlineStride+x, ((int)sarray[0])&0xffff);
- break;
-
- case DataBuffer.INT_DATA:
-
- int[] iarray = (int[])obj;
- data.setElem(y*scanlineStride+x, iarray[0]);
- 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 value = 0;
- for (int i=0; i < numBands; i++) {
- value |= ((iArray[i] << bitOffsets[i]) & bitMasks[i]);
- }
- data.setElem(y*scanlineStride+x, value);
- }
-
- /**
- * Sets all samples for a 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 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;
- int srcOffset = 0;
-
- for (int i = 0; i < h; i++) {
- for (int j = 0; j < w; j++) {
- int value = 0;
- for (int k=0; k < numBands; k++) {
- value |= ((iArray[srcOffset++] << bitOffsets[k])
- & bitMasks[k]);
- }
- data.setElem(lineOffset+j,value);
- }
- 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) {
- int value = data.getElem(y*scanlineStride + x);
- value &= ~bitMasks[b];
- value |= (s << bitOffsets[b]) & bitMasks[b];
- data.setElem(y*scanlineStride + x,value);
- }
-
- /**
- * 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;
- int srcOffset = 0;
-
- for (int i = 0; i < h; i++) {
- for (int j = 0; j < w; j++) {
- int value = data.getElem(lineOffset+j);
- value &= ~bitMasks[b];
- int sample = iArray[srcOffset++];
- value |= ((int)sample << bitOffsets[b]) & bitMasks[b];
- data.setElem(lineOffset+j,value);
- }
- lineOffset += scanlineStride;
- }
- }
-
- }
-
-
-
-
-
-
-