FileDocCategorySizeDatePackage
MultiPixelPackedSampleModel.javaAPI DocAndroid 1.5 API15382Wed May 06 22:41:54 BST 2009java.awt.image

MultiPixelPackedSampleModel

public class MultiPixelPackedSampleModel extends SampleModel
The MultiPixelPackedSampleModel class represents image data with one band. This class packs multiple pixels with one sample in one data element and supports the following data types: DataBuffer.TYPE_BYTE, DataBuffer.TYPE_USHORT, or DataBuffer.TYPE_INT.
since
Android 1.0

Fields Summary
private int
pixelBitStride
The pixel bit stride.
private int
scanlineStride
The scanline stride.
private int
dataBitOffset
The data bit offset.
private int
bitMask
The bit mask.
private int
dataElementSize
The data element size.
private int
pixelsPerDataElement
The pixels per data element.
Constructors Summary
public MultiPixelPackedSampleModel(int dataType, int w, int h, int numberOfBits, int scanlineStride, int dataBitOffset)
Instantiates a new MultiPixelPackedSampleModel with the specified parameters.

param
dataType the data type of the samples.
param
w the width of the image data.
param
h the height of the image data.
param
numberOfBits the number of bits per pixel.
param
scanlineStride the scanline stride of the of the image data.
param
dataBitOffset the array of the band offsets.


        super(dataType, w, h, 1);
        if (dataType != DataBuffer.TYPE_BYTE && dataType != DataBuffer.TYPE_USHORT
                && dataType != DataBuffer.TYPE_INT) {
            // awt.61=Unsupported data type: {0}
            throw new IllegalArgumentException(Messages.getString("awt.61", //$NON-NLS-1$
                    dataType));
        }

        this.scanlineStride = scanlineStride;
        if (numberOfBits == 0) {
            // awt.20C=Number of Bits equals to zero
            throw new RasterFormatException(Messages.getString("awt.20C")); //$NON-NLS-1$
        }
        this.pixelBitStride = numberOfBits;
        this.dataElementSize = DataBuffer.getDataTypeSize(dataType);
        if (dataElementSize % pixelBitStride != 0) {
            // awt.20D=The number of bits per pixel is not a power of 2 or
            // pixels span data element boundaries
            throw new RasterFormatException(Messages.getString("awt.20D")); //$NON-NLS-1$
        }

        if (dataBitOffset % numberOfBits != 0) {
            // awt.20E=Data Bit offset is not a multiple of pixel bit stride
            throw new RasterFormatException(Messages.getString("awt.20E")); //$NON-NLS-1$
        }
        this.dataBitOffset = dataBitOffset;

        this.pixelsPerDataElement = dataElementSize / pixelBitStride;
        this.bitMask = (1 << numberOfBits) - 1;
    
public MultiPixelPackedSampleModel(int dataType, int w, int h, int numberOfBits)
Instantiates a new MultiPixelPackedSampleModel with the specified parameters.

param
dataType the data type of the samples.
param
w the width of the image data.
param
h the height of the image data.
param
numberOfBits the number of bits per pixel.


        this(dataType, w, h, numberOfBits,
                (numberOfBits * w + DataBuffer.getDataTypeSize(dataType) - 1)
                        / DataBuffer.getDataTypeSize(dataType), 0);
    
Methods Summary
public java.awt.image.SampleModelcreateCompatibleSampleModel(int w, int h)

        return new MultiPixelPackedSampleModel(dataType, w, h, pixelBitStride);
    
public java.awt.image.DataBuffercreateDataBuffer()

        DataBuffer dataBuffer = null;
        int size = scanlineStride * height;

        switch (dataType) {
            case DataBuffer.TYPE_BYTE:
                dataBuffer = new DataBufferByte(size + (dataBitOffset + 7) / 8);
                break;
            case DataBuffer.TYPE_USHORT:
                dataBuffer = new DataBufferUShort(size + (dataBitOffset + 15) / 16);
                break;
            case DataBuffer.TYPE_INT:
                dataBuffer = new DataBufferInt(size + (dataBitOffset + 31) / 32);
                break;
        }
        return dataBuffer;
    
public java.awt.image.SampleModelcreateSubsetSampleModel(int[] bands)

        if (bands != null && bands.length != 1) {
            // awt.20F=Number of bands must be only 1
            throw new RasterFormatException(Messages.getString("awt.20F")); //$NON-NLS-1$
        }
        return createCompatibleSampleModel(width, height);
    
public booleanequals(java.lang.Object o)
Compares this MultiPixelPackedSampleModel object with the specified object.

param
o the Object to be compared.
return
true, if the object is a MultiPixelPackedSampleModel with the same data parameter values as this MultiPixelPackedSampleModel, false otherwise.

        if ((o == null) || !(o instanceof MultiPixelPackedSampleModel)) {
            return false;
        }

        MultiPixelPackedSampleModel model = (MultiPixelPackedSampleModel)o;
        return this.width == model.width && this.height == model.height
                && this.numBands == model.numBands && this.dataType == model.dataType
                && this.pixelBitStride == model.pixelBitStride && this.bitMask == model.bitMask
                && this.pixelsPerDataElement == model.pixelsPerDataElement
                && this.dataElementSize == model.dataElementSize
                && this.dataBitOffset == model.dataBitOffset
                && this.scanlineStride == model.scanlineStride;
    
public intgetBitOffset(int x)
Gets the bit offset in the data element which is stored for the specified pixel of a scanline.

param
x the pixel.
return
the bit offset of the pixel in the data element.

        return (x * pixelBitStride + dataBitOffset) % dataElementSize;
    
public intgetDataBitOffset()
Gets the data bit offset.

return
the data bit offset.

        return dataBitOffset;
    
public java.lang.ObjectgetDataElements(int x, int y, java.lang.Object obj, java.awt.image.DataBuffer data)

        if (x < 0 || y < 0 || x >= this.width || y >= this.height) {
            // awt.63=Coordinates are not in bounds
            throw new ArrayIndexOutOfBoundsException(Messages.getString("awt.63")); //$NON-NLS-1$
        }
        switch (getTransferType()) {
            case DataBuffer.TYPE_BYTE:
                byte bdata[];
                if (obj == null) {
                    bdata = new byte[1];
                } else {
                    bdata = (byte[])obj;
                }
                bdata[0] = (byte)getSample(x, y, 0, data);
                obj = bdata;
                break;
            case DataBuffer.TYPE_USHORT:
                short sdata[];
                if (obj == null) {
                    sdata = new short[1];
                } else {
                    sdata = (short[])obj;
                }
                sdata[0] = (short)getSample(x, y, 0, data);
                obj = sdata;
                break;
            case DataBuffer.TYPE_INT:
                int idata[];
                if (obj == null) {
                    idata = new int[1];
                } else {
                    idata = (int[])obj;
                }
                idata[0] = getSample(x, y, 0, data);
                obj = idata;
                break;
        }

        return obj;
    
public intgetNumDataElements()

        return 1;
    
public intgetOffset(int x, int y)
Gets the offset of the specified pixel in the data array.

param
x the X coordinate of the specified pixel.
param
y the Y coordinate of the specified pixel.
return
the offset of the specified pixel.

        return y * scanlineStride + (x * pixelBitStride + dataBitOffset) / dataElementSize;
    
public int[]getPixel(int x, int y, int[] iArray, java.awt.image.DataBuffer data)

        if (x < 0 || y < 0 || x >= this.width || y >= this.height) {
            // awt.63=Coordinates are not in bounds
            throw new ArrayIndexOutOfBoundsException(Messages.getString("awt.63")); //$NON-NLS-1$
        }
        int pixel[];
        if (iArray == null) {
            pixel = new int[numBands];
        } else {
            pixel = iArray;
        }

        pixel[0] = getSample(x, y, 0, data);
        return pixel;
    
public intgetPixelBitStride()
Gets the pixel bit stride of this MultiPixelPackedSampleModel.

return
the pixel bit stride of this MultiPixelPackedSampleModel.

        return pixelBitStride;
    
public intgetSample(int x, int y, int b, java.awt.image.DataBuffer data)

        if (x < 0 || y < 0 || x >= this.width || y >= this.height || b != 0) {
            // awt.63=Coordinates are not in bounds
            throw new ArrayIndexOutOfBoundsException(Messages.getString("awt.63")); //$NON-NLS-1$
        }

        int bitnum = dataBitOffset + x * pixelBitStride;
        int elem = data.getElem(y * scanlineStride + bitnum / dataElementSize);
        int shift = dataElementSize - (bitnum & (dataElementSize - 1)) - pixelBitStride;

        return (elem >> shift) & bitMask;
    
public intgetSampleSize(int band)

        return pixelBitStride;
    
public int[]getSampleSize()

        int sampleSizes[] = {
            pixelBitStride
        };
        return sampleSizes;
    
public intgetScanlineStride()
Gets the scanline stride of this MultiPixelPackedSampleModel.

return
the scanline stride of this MultiPixelPackedSampleModel.

        return scanlineStride;
    
public intgetTransferType()

        if (pixelBitStride > 16) {
            return DataBuffer.TYPE_INT;
        } else if (pixelBitStride > 8) {
            return DataBuffer.TYPE_USHORT;
        } else {
            return DataBuffer.TYPE_BYTE;
        }
    
public inthashCode()
Returns a hash code of this MultiPixelPackedSampleModel class.

return
the hash code of this MultiPixelPackedSampleModel class.

        int hash = 0;
        int tmp = 0;

        hash = width;
        tmp = hash >>> 24;
        hash <<= 8;
        hash |= tmp;
        hash ^= height;
        tmp = hash >>> 24;
        hash <<= 8;
        hash |= tmp;
        hash ^= numBands;
        tmp = hash >>> 24;
        hash <<= 8;
        hash |= tmp;
        hash ^= dataType;
        tmp = hash >>> 24;
        hash <<= 8;
        hash |= tmp;
        hash ^= scanlineStride;
        tmp = hash >>> 24;
        hash <<= 8;
        hash |= tmp;
        hash ^= pixelBitStride;
        tmp = hash >>> 24;
        hash <<= 8;
        hash |= tmp;
        hash ^= dataBitOffset;
        tmp = hash >>> 24;
        hash <<= 8;
        hash |= tmp;
        hash ^= bitMask;
        tmp = hash >>> 24;
        hash <<= 8;
        hash |= tmp;
        hash ^= dataElementSize;
        tmp = hash >>> 24;
        hash <<= 8;
        hash |= tmp;
        hash ^= pixelsPerDataElement;
        return hash;
    
public voidsetDataElements(int x, int y, java.lang.Object obj, java.awt.image.DataBuffer data)

        setSample(x, y, obj, data, 1, 0);
    
public voidsetPixel(int x, int y, int[] iArray, java.awt.image.DataBuffer data)

        setSample(x, y, iArray, data, 2, 0);
    
public voidsetSample(int x, int y, int b, int s, java.awt.image.DataBuffer data)

        if (b != 0) {
            // awt.63=Coordinates are not in bounds
            throw new ArrayIndexOutOfBoundsException(Messages.getString("awt.63")); //$NON-NLS-1$
        }

        setSample(x, y, null, data, 3, s);
    
private voidsetSample(int x, int y, java.lang.Object obj, java.awt.image.DataBuffer data, int methodId, int s)
This method is used by other methods of this class. The behavior of this method depends on the method which has been invoke this one. The argument methodId is used to choose valid behavior in a particular case. If methodId is equal to 1 it means that this method has been invoked by the setDataElements() method, 2 - means setPixel(), and setSample() in any other cases.

param
x the x.
param
y the y.
param
obj the obj.
param
data the data.
param
methodId the method id.
param
s the s.

        if ((x < 0) || (y < 0) || (x >= this.width) || (y >= this.height)) {
            // awt.63=Coordinates are not in bounds
            throw new ArrayIndexOutOfBoundsException(Messages.getString("awt.63")); //$NON-NLS-1$
        }

        final int bitnum = dataBitOffset + x * pixelBitStride;
        final int idx = y * scanlineStride + bitnum / dataElementSize;
        final int shift = dataElementSize - (bitnum & (dataElementSize - 1)) - pixelBitStride;
        final int mask = ~(bitMask << shift);
        int elem = data.getElem(idx);

        switch (methodId) {
            case 1: { // Invoked from setDataElements()
                switch (getTransferType()) {
                    case DataBuffer.TYPE_BYTE:
                        s = ((byte[])obj)[0] & 0xff;
                        break;
                    case DataBuffer.TYPE_USHORT:
                        s = ((short[])obj)[0] & 0xffff;
                        break;
                    case DataBuffer.TYPE_INT:
                        s = ((int[])obj)[0];
                        break;
                }
                break;
            }
            case 2: { // Invoked from setPixel()
                s = ((int[])obj)[0];
                break;
            }
        }

        elem &= mask;
        elem |= (s & bitMask) << shift;
        data.setElem(idx, elem);