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

ComponentSampleModel

public class ComponentSampleModel extends SampleModel
The ComponentSampleModel class represents a set of image data whose each element - the sample of a pixel - takes one data element of the DataBuffer.

The Bank indices denote the correspondence between the bank of data buffers and a band of image data. The Pixel stride is the number of data array elements between two samples for the same band on the same scanline. The pixel stride for a BandedSampleModel is one. The scanline stride represents the number of data array elements between a specified sample and the corresponding sample in the same column in the next scanline. The array of band offsets gives the starting offsets within each data banks of the in the DataBuffer. The bank indices represents the indices within each bank of the DataBuffer corresponding to a band of image data.

since
Android 1.0

Fields Summary
protected int[]
bandOffsets
The band offsets array of this ComponentSampleModel.
protected int[]
bankIndices
The bank indices array of this ComponentSampleModel.
protected int
numBands
The number of bands in this ComponentSampleModel.
protected int
numBanks
The number banks of this ComponentSampleModel.
protected int
scanlineStride
The scanline stride of this ComponentSampleModel.
protected int
pixelStride
The pixel stride of this ComponentSampleModel.
Constructors Summary
public ComponentSampleModel(int dataType, int w, int h, int pixelStride, int scanlineStride, int[] bankIndices, int[] bandOffsets)
Instantiates a new ComponentSampleModel with the specified properties.

param
dataType the data type of samples.
param
w the width of the image data.
param
h the height of the image data.
param
pixelStride the pixel stride of the image data.
param
scanlineStride the scanline stride of the image data.
param
bankIndices the array of the bank indices.
param
bandOffsets the array of the band offsets.


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

        if (pixelStride < 0) {
            // awt.24B=Pixel stride must be >= 0
            throw new IllegalArgumentException(Messages.getString("awt.24B")); //$NON-NLS-1$
        }

        if (scanlineStride < 0) {
            // awt.24C=Scanline stride must be >= 0
            throw new IllegalArgumentException(Messages.getString("awt.24C")); //$NON-NLS-1$
        }

        if (bankIndices.length != bandOffsets.length) {
            // awt.24D=Bank Indices length must be equal Bank Offsets length
            throw new IllegalArgumentException(Messages.getString("awt.24D")); //$NON-NLS-1$
        }

        this.pixelStride = pixelStride;
        this.scanlineStride = scanlineStride;
        this.bandOffsets = bandOffsets.clone();
        this.bankIndices = bankIndices.clone();
        this.numBands = bandOffsets.length;

        int maxBank = 0;
        for (int i = 0; i < bankIndices.length; i++) {
            if (bankIndices[i] < 0) {
                // awt.24E=Index of {0} bank must be >= 0
                throw new IllegalArgumentException(Messages.getString("awt.24E", i)); //$NON-NLS-1$
            }
            if (bankIndices[i] > maxBank) {
                maxBank = bankIndices[i];
            }
        }
        this.numBanks = maxBank + 1;

    
public ComponentSampleModel(int dataType, int w, int h, int pixelStride, int scanlineStride, int[] bandOffsets)
Instantiates a new ComponentSampleModel with the specified properties.

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
pixelStride the pixel stride of the image data.
param
scanlineStride the scanline stride of the image data.
param
bandOffsets the band offsets.


        super(dataType, w, h, bandOffsets.length);
        if (pixelStride < 0) {
            // awt.24B=Pixel stride must be >= 0
            throw new IllegalArgumentException(Messages.getString("awt.24B")); //$NON-NLS-1$
        }

        if (scanlineStride < 0) {
            // awt.24C=Scanline stride must be >= 0
            throw new IllegalArgumentException(Messages.getString("awt.24C")); //$NON-NLS-1$
        }

        this.pixelStride = pixelStride;
        this.scanlineStride = scanlineStride;
        this.bandOffsets = bandOffsets.clone();
        this.numBands = bandOffsets.length;
        this.numBanks = 1;

        this.bankIndices = new int[numBands];
        for (int i = 0; i < numBands; i++) {
            bankIndices[i] = 0;
        }
    
Methods Summary
public java.awt.image.SampleModelcreateCompatibleSampleModel(int w, int h)

        return new ComponentSampleModel(dataType, w, h, pixelStride, pixelStride * w, bankIndices,
                bandOffsets);
    
public java.awt.image.DataBuffercreateDataBuffer()

        DataBuffer data = null;

        int maxOffset = bandOffsets[0];
        for (int i = 1; i < bandOffsets.length; i++) {
            if (bandOffsets[i] > maxOffset) {
                maxOffset = bandOffsets[i];
            }
        }
        int size = (height - 1) * scanlineStride + (width - 1) * pixelStride + maxOffset + 1;

        switch (dataType) {
            case DataBuffer.TYPE_BYTE:
                data = new DataBufferByte(size, numBanks);
                break;
            case DataBuffer.TYPE_SHORT:
                data = new DataBufferShort(size, numBanks);
                break;
            case DataBuffer.TYPE_USHORT:
                data = new DataBufferUShort(size, numBanks);
                break;
            case DataBuffer.TYPE_INT:
                data = new DataBufferInt(size, numBanks);
                break;
            case DataBuffer.TYPE_FLOAT:
                data = new DataBufferFloat(size, numBanks);
                break;
            case DataBuffer.TYPE_DOUBLE:
                data = new DataBufferDouble(size, numBanks);
                break;
        }

        return data;

    
public java.awt.image.SampleModelcreateSubsetSampleModel(int[] bands)

see
java.awt.image.SampleModel#createSubsetSampleModel(int[])

        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 indices[] = new int[bands.length];
        int offsets[] = new int[bands.length];

        for (int i = 0; i < bands.length; i++) {
            indices[i] = bankIndices[bands[i]];
            offsets[i] = bandOffsets[bands[i]];
        }

        return new ComponentSampleModel(dataType, width, height, pixelStride, scanlineStride,
                indices, offsets);

    
public booleanequals(java.lang.Object o)
Compares this ComponentSampleModel with the specified Object.

param
o the Object.
return
true, if the object is a ComponentSampleModel with identical data values to this ComponentSampleModel, false otherwise.

        if ((o == null) || !(o instanceof ComponentSampleModel)) {
            return false;
        }
        ComponentSampleModel model = (ComponentSampleModel)o;
        return this.width == model.width && this.height == model.height
                && this.numBands == model.numBands && this.dataType == model.dataType
                && Arrays.equals(this.bandOffsets, model.bandOffsets)
                && Arrays.equals(this.bankIndices, model.bankIndices)
                && this.numBands == model.numBands && this.numBanks == model.numBanks
                && this.scanlineStride == model.scanlineStride
                && this.pixelStride == model.pixelStride;
    
public final int[]getBandOffsets()
Gets an array of the band offsets corresponding to this ComponentSampleModel.

return
the array of band offsets.

        return bandOffsets.clone();
    
public final int[]getBankIndices()
Gets an array of bank indices corresponding to this ComponentSampleModel.

return
the array of bank indices.

        return bankIndices.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 (dataType) {
            case DataBuffer.TYPE_BYTE:
                byte bdata[];
                if (obj == null) {
                    bdata = new byte[numBands];
                } else {
                    bdata = (byte[])obj;
                }

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

                obj = bdata;
                break;

            case DataBuffer.TYPE_SHORT:
            case DataBuffer.TYPE_USHORT:
                short sdata[];
                if (obj == null) {
                    sdata = new short[numBands];
                } else {
                    sdata = (short[])obj;
                }

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

                obj = sdata;
                break;

            case DataBuffer.TYPE_INT:
                int idata[];
                if (obj == null) {
                    idata = new int[numBands];
                } else {
                    idata = (int[])obj;
                }

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

                obj = idata;
                break;

            case DataBuffer.TYPE_FLOAT:
                float fdata[];
                if (obj == null) {
                    fdata = new float[numBands];
                } else {
                    fdata = (float[])obj;
                }

                for (int i = 0; i < numBands; i++) {
                    fdata[i] = getSampleFloat(x, y, i, data);
                }

                obj = fdata;
                break;

            case DataBuffer.TYPE_DOUBLE:
                double ddata[];
                if (obj == null) {
                    ddata = new double[numBands];
                } else {
                    ddata = (double[])obj;
                }

                for (int i = 0; i < numBands; i++) {
                    ddata[i] = getSampleDouble(x, y, i, data);
                }

                obj = ddata;
                break;
        }

        return obj;
    
public final intgetNumDataElements()

        return numBands;
    
public intgetOffset(int x, int y, int b)
Gets the offset of the specified band of the specified pixel.

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

        return y * scanlineStride + x * pixelStride + bandOffsets[b];
    
public intgetOffset(int x, int y)
Gets the offset of the first band of the specified pixel.

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

        return y * scanlineStride + x * pixelStride + bandOffsets[0];
    
public int[]getPixel(int x, int y, int[] iArray, java.awt.image.DataBuffer data)

        int pixel[];

        if (iArray == null) {
            pixel = new int[numBands];
        } else {
            pixel = iArray;
        }

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

        return pixel;
    
public final intgetPixelStride()
Gets the pixel stride.

return
the pixel stride.

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

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

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

        for (int i = y; i < y + h; i++) {
            for (int j = x; j < x + w; j++) {
                for (int n = 0; n < 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$
        }

        return data.getElem(bankIndices[b], y * scanlineStride + x * pixelStride + bandOffsets[b]);
    
public doublegetSampleDouble(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$
        }

        return data.getElemDouble(bankIndices[b], y * scanlineStride + x * pixelStride
                + bandOffsets[b]);
    
public floatgetSampleFloat(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$
        }

        return data.getElemFloat(bankIndices[b], y * scanlineStride + x * pixelStride
                + bandOffsets[b]);
    
public final intgetSampleSize(int band)

        return DataBuffer.getDataTypeSize(dataType);
    
public final int[]getSampleSize()

        int sampleSizes[] = new int[numBands];
        int size = DataBuffer.getDataTypeSize(dataType);

        for (int i = 0; i < numBands; i++) {
            sampleSizes[i] = size;
        }
        return sampleSizes;
    
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 || x + w > this.width || y + 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;
        }

        if (data == null) {
            // awt.295=data is null
            throw new NullPointerException(Messages.getString("awt.295")); //$NON-NLS-1$
        }

        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 final intgetScanlineStride()
Gets the scanline stride of this ComponentSampleModel.

return
the scanline stride of this ComponentSampleModel.

        return scanlineStride;
    
public inthashCode()
Gets a hash code of this ComponentSampleModel object.

return
a hash code of this ComponentSampleModel object.

        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 : bandOffsets) {
            hash ^= element;
            tmp = hash >>> 24;
            hash <<= 8;
            hash |= tmp;
        }
        for (int element : bankIndices) {
            hash ^= element;
            tmp = hash >>> 24;
            hash <<= 8;
            hash |= tmp;
        }
        hash ^= pixelStride;
        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 (dataType) {
            case DataBuffer.TYPE_BYTE:
                byte barr[] = (byte[])obj;
                for (int i = 0; i < numBands; i++) {
                    setSample(x, y, i, barr[i] & 0xff, data);
                }
                break;

            case DataBuffer.TYPE_SHORT:
            case DataBuffer.TYPE_USHORT:
                short sarr[] = (short[])obj;
                for (int i = 0; i < numBands; i++) {
                    setSample(x, y, i, sarr[i] & 0xffff, data);
                }
                break;

            case DataBuffer.TYPE_INT:
                int iarr[] = (int[])obj;
                for (int i = 0; i < numBands; i++) {
                    setSample(x, y, i, iarr[i], data);
                }
                break;

            case DataBuffer.TYPE_FLOAT:
                float farr[] = (float[])obj;
                for (int i = 0; i < numBands; i++) {
                    setSample(x, y, i, farr[i], data);
                }
                break;

            case DataBuffer.TYPE_DOUBLE:
                double darr[] = (double[])obj;
                for (int i = 0; i < numBands; i++) {
                    setSample(x, y, i, darr[i], data);
                }
                break;
        }
    
public voidsetPixel(int x, int y, int[] iArray, java.awt.image.DataBuffer data)

        for (int i = 0; i < 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 || x + w > this.width || y + 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 < 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$
        }

        data.setElem(bankIndices[b], y * scanlineStride + x * pixelStride + bandOffsets[b], s);
    
public voidsetSample(int x, int y, int b, float 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$
        }

        data.setElemFloat(bankIndices[b], y * scanlineStride + x * pixelStride + bandOffsets[b], s);
    
public voidsetSample(int x, int y, int b, double 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$
        }

        data
                .setElemDouble(bankIndices[b], y * scanlineStride + x * pixelStride
                        + bandOffsets[b], s);
    
public voidsetSamples(int x, int y, int w, int h, int b, int[] iArray, java.awt.image.DataBuffer data)

        if (x < 0 || y < 0 || x + w > this.width || y + 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(j, i, b, iArray[idx++], data);
            }
        }