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

SinglePixelPackedSampleModel

public class SinglePixelPackedSampleModel extends SampleModel
The SinglePixelPackedSampleModel class represents pixel data where several samples combine to create a single pixel and are stored in a single data array element. This class supports TYPE_BYTE, TYPE_USHORT, TYPE_INT data types.
since
Android 1.0

Fields Summary
private int[]
bitMasks
The bit masks.
private int[]
bitOffsets
The bit offsets.
private int[]
bitSizes
The bit sizes.
private int
scanlineStride
The scanline stride.
private int
maxBitSize
The max bit size.
Constructors Summary
public SinglePixelPackedSampleModel(int dataType, int w, int h, int[] bitMasks)
Instantiates a new SinglePixelPackedSampleModel with the specified parameters.

param
dataType the data type of samples.
param
w the width of the image data.
param
h the height of the image data.
param
bitMasks the bit masks for all the bands.

        this(dataType, w, h, w, bitMasks);
    
public SinglePixelPackedSampleModel(int dataType, int w, int h, int scanlineStride, int[] bitMasks)
Instantiates a new SinglePixelPackedSampleModel 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
scanlineStride the scanline stride of the image data.
param
bitMasks the bit masks for all the bands.


        super(dataType, w, h, bitMasks.length);

        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;
        this.bitMasks = bitMasks.clone();
        this.bitOffsets = new int[this.numBands];
        this.bitSizes = new int[this.numBands];

        this.maxBitSize = 0;

        for (int i = 0; i < this.numBands; i++) {
            int offset = 0;
            int size = 0;
            int mask = bitMasks[i];

            if (mask != 0) {
                while ((mask & 1) == 0) {
                    mask >>>= 1;
                    offset++;
                }

                while ((mask & 1) == 1) {
                    mask >>>= 1;
                    size++;
                }

                if (mask != 0) {
                    // awt.62=Wrong mask : {0}
                    throw new IllegalArgumentException(Messages.getString("awt.62", bitMasks[i])); //$NON-NLS-1$
                }
            }

            this.bitOffsets[i] = offset;
            this.bitSizes[i] = size;

            if (this.maxBitSize < size) {
                this.maxBitSize = size;
            }

        }

    
Methods Summary
public java.awt.image.SampleModelcreateCompatibleSampleModel(int w, int h)

        return new SinglePixelPackedSampleModel(this.dataType, w, h, this.bitMasks);
    
public java.awt.image.DataBuffercreateDataBuffer()

        DataBuffer data = null;
        int size = (this.height - 1) * scanlineStride + width;

        switch (this.dataType) {
            case DataBuffer.TYPE_BYTE:
                data = new DataBufferByte(size);
                break;
            case DataBuffer.TYPE_USHORT:
                data = new DataBufferUShort(size);
                break;
            case DataBuffer.TYPE_INT:
                data = new DataBufferInt(size);
                break;
        }
        return data;
    
public java.awt.image.SampleModelcreateSubsetSampleModel(int[] bands)

        if (bands.length > this.numBands) {
            // awt.64=The number of the bands in the subset is greater than the
            // number of bands in the sample model
            throw new RasterFormatException(Messages.getString("awt.64")); //$NON-NLS-1$
        }

        int masks[] = new int[bands.length];
        for (int i = 0; i < bands.length; i++) {
            masks[i] = this.bitMasks[bands[i]];
        }
        return new SinglePixelPackedSampleModel(this.dataType, this.width, this.height,
                this.scanlineStride, masks);
    
public booleanequals(java.lang.Object o)
Compares this SinglePixelPackedSampleModel object with the specified object.

param
o the Object to be compared.
return
true, if this SinglePixelPackedSampleModel object is equal to the specified object, false otherwise.

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

        SinglePixelPackedSampleModel model = (SinglePixelPackedSampleModel)o;
        return this.width == model.width && this.height == model.height
                && this.numBands == model.numBands && this.dataType == model.dataType
                && Arrays.equals(this.bitMasks, model.bitMasks)
                && Arrays.equals(this.bitOffsets, model.bitOffsets)
                && Arrays.equals(this.bitSizes, model.bitSizes)
                && this.scanlineStride == model.scanlineStride;
    
public int[]getBitMasks()
Gets an array of the bit masks for all bands.

return
an array of the bit masks for all bands.

        return bitMasks.clone();
    
public int[]getBitOffsets()
Gets an array of the bit offsets of the data array elements.

return
an array of the bit offsets.

        return bitOffsets.clone();
    
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)data.getElem(y * scanlineStride + x);
                obj = bdata;
                break;
            case DataBuffer.TYPE_USHORT:
                short sdata[];
                if (obj == null) {
                    sdata = new short[1];
                } else {
                    sdata = (short[])obj;
                }

                sdata[0] = (short)data.getElem(y * scanlineStride + x);
                obj = sdata;
                break;
            case DataBuffer.TYPE_INT:
                int idata[];
                if (obj == null) {
                    idata = new int[1];
                } else {
                    idata = (int[])obj;
                }

                idata[0] = data.getElem(y * scanlineStride + x);
                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);
    
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[this.numBands];
        } else {
            pixel = iArray;
        }

        for (int i = 0; i < this.numBands; i++) {
            pixel[i] = getSample(x, y, i, data);
        }

        return pixel;
    
public int[]getPixels(int x, int y, int w, int h, int[] iArray, java.awt.image.DataBuffer data)

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

        int pixels[];

        if (iArray == null) {
            pixels = new int[w * h * this.numBands];
        } else {
            pixels = iArray;
        }

        int idx = 0;

        for (int i = y; i < y + h; i++) {
            for (int j = x; j < x + w; j++) {
                for (int n = 0; n < this.numBands; n++) {
                    pixels[idx++] = getSample(j, i, n, data);
                }
            }
        }
        return pixels;
    
public intgetSample(int x, int y, int b, 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 sample = data.getElem(y * scanlineStride + x);
        return ((sample & this.bitMasks[b]) >>> this.bitOffsets[b]);
    
public intgetSampleSize(int band)

        return bitSizes[band];
    
public int[]getSampleSize()

        return bitSizes.clone();
    
public int[]getSamples(int x, int y, int w, int h, int b, int[] iArray, java.awt.image.DataBuffer data)

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

        int samples[];
        int idx = 0;

        if (iArray == null) {
            samples = new int[w * h];
        } else {
            samples = iArray;
        }

        for (int i = y; i < y + h; i++) {
            for (int j = x; j < x + w; j++) {
                samples[idx++] = getSample(j, i, b, data);
            }
        }

        return samples;
    
public intgetScanlineStride()
Gets the scanline stride.

return
the scanline stride

        return this.scanlineStride;
    
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;
        for (int element : bitMasks) {
            hash ^= element;
            tmp = hash >>> 24;
            hash <<= 8;
            hash |= tmp;
        }
        for (int element : bitOffsets) {
            hash ^= element;
            tmp = hash >>> 24;
            hash <<= 8;
            hash |= tmp;
        }
        for (int element : bitSizes) {
            hash ^= element;
            tmp = hash >>> 24;
            hash <<= 8;
            hash |= tmp;
        }
        hash ^= scanlineStride;
        return hash;
    
public voidsetDataElements(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:
                data.setElem(y * scanlineStride + x, ((byte[])obj)[0] & 0xff);
                break;
            case DataBuffer.TYPE_USHORT:
                data.setElem(y * scanlineStride + x, ((short[])obj)[0] & 0xffff);
                break;
            case DataBuffer.TYPE_INT:
                data.setElem(y * scanlineStride + x, ((int[])obj)[0]);
                break;
        }
    
public voidsetPixel(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$
        }
        for (int i = 0; i < this.numBands; i++) {
            setSample(x, y, i, iArray[i], data);
        }
    
public voidsetPixels(int x, int y, int w, int h, int[] iArray, java.awt.image.DataBuffer data)

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

        int idx = 0;

        for (int i = y; i < y + h; i++) {
            for (int j = x; j < x + w; j++) {
                for (int n = 0; n < this.numBands; n++) {
                    setSample(j, i, n, iArray[idx++], data);
                }
            }
        }
    
public voidsetSample(int x, int y, int b, int s, 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 tmp = data.getElem(y * scanlineStride + x);
        tmp &= ~this.bitMasks[b];
        tmp |= (s << this.bitOffsets[b]) & this.bitMasks[b];
        data.setElem(y * scanlineStride + x, tmp);
    
public voidsetSamples(int x, int y, int w, int h, int b, int[] iArray, java.awt.image.DataBuffer data)

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

        int idx = 0;
        for (int i = y; i < y + h; i++) {
            for (int j = x; j < x + w; j++) {
                setSample(x + j, y + i, b, iArray[idx++], data);
            }
        }