MultiPixelPackedSampleModel
class represents
one-banded images and can pack multiple one-sample
pixels into one data element. Pixels are not allowed to span data elements.
The data type can be DataBuffer.TYPE_BYTE, DataBuffer.TYPE_USHORT,
or DataBuffer.TYPE_INT. Each pixel must be a power of 2 number of bits
and a power of 2 number of pixels must fit exactly in one data element.
Pixel bit stride is equal to the number of bits per pixel. Scanline
stride is in data elements and the last several data elements might be
padded with unused pixels. Data bit offset is the offset in bits from
the beginning of the DataBuffer
to the first pixel and must be
a multiple of pixel bit stride.
The following code illustrates extracting the bits for pixel
x, y
from DataBuffer data
and storing the pixel data in data elements of type
dataType
:
int dataElementSize = DataBuffer.getDataTypeSize(dataType);
int bitnum = dataBitOffset + x*pixelBitStride;
int element = data.getElem(y*scanlineStride + bitnum/dataElementSize);
int shift = dataElementSize - (bitnum & (dataElementSize-1))
- pixelBitStride;
int pixel = (element >> shift) & ((1 << pixelBitStride) - 1);
-
Field Summary
Fields declared in class java.awt.image.SampleModel
dataType, height, numBands, width
-
Constructor Summary
ConstructorDescriptionMultiPixelPackedSampleModel
(int dataType, int w, int h, int numberOfBits) Constructs aMultiPixelPackedSampleModel
with the specified data type, width, height and number of bits per pixel.MultiPixelPackedSampleModel
(int dataType, int w, int h, int numberOfBits, int scanlineStride, int dataBitOffset) Constructs aMultiPixelPackedSampleModel
with specified data type, width, height, number of bits per pixel, scanline stride and data bit offset. -
Method Summary
Modifier and TypeMethodDescriptioncreateCompatibleSampleModel
(int w, int h) Creates a newMultiPixelPackedSampleModel
with the specified width and height.Creates aDataBuffer
that corresponds to thisMultiPixelPackedSampleModel
.createSubsetSampleModel
(int[] bands) Creates a newMultiPixelPackedSampleModel
with a subset of the bands of thisMultiPixelPackedSampleModel
.int
getBitOffset
(int x) Returns the offset, in bits, into the data element in which it is stored for thex
th pixel of a scanline.int
Returns the data bit offset in bits.getDataElements
(int x, int y, Object obj, DataBuffer data) Returns data for a single pixel in a primitive array of type TransferType.int
Returns the number of data elements needed to transfer one pixel via thegetDataElements(int, int, java.lang.Object, java.awt.image.DataBuffer)
andsetDataElements(int, int, java.lang.Object, java.awt.image.DataBuffer)
methods.int
getOffset
(int x, int y) Returns the offset of pixel (x, y) in data array elements.int[]
getPixel
(int x, int y, int[] iArray, DataBuffer data) Returns the specified single band pixel in the first element of anint
array.int
Returns the pixel bit stride in bits.int
getSample
(int x, int y, int b, DataBuffer data) Returns asint
the sample in a specified band for the pixel located at (x, y).int[]
Returns the number of bits per sample for all bands.int
getSampleSize
(int band) Returns the number of bits per sample for the specified band.int
Returns the scanline stride.int
Returns the TransferType used to transfer pixels by way of thegetDataElements
andsetDataElements
methods.void
setDataElements
(int x, int y, Object obj, DataBuffer data) Sets the data for a single pixel in the specifiedDataBuffer
from a primitive array of type TransferType.void
setPixel
(int x, int y, int[] iArray, DataBuffer data) Sets a pixel in theDataBuffer
using anint
array for input.void
setSample
(int x, int y, int b, int s, DataBuffer data) Sets a sample in the specified band for the pixel located at (x, y) in theDataBuffer
using anint
for input.Methods declared in class java.awt.image.SampleModel
getDataElements, getDataType, getHeight, getNumBands, getPixel, getPixel, getPixels, getPixels, getPixels, getSampleDouble, getSampleFloat, getSamples, getSamples, getSamples, getWidth, setDataElements, setPixel, setPixel, setPixels, setPixels, setPixels, setSample, setSample, setSamples, setSamples, setSamples
-
Constructor Details
-
MultiPixelPackedSampleModel
public MultiPixelPackedSampleModel(int dataType, int w, int h, int numberOfBits) Constructs aMultiPixelPackedSampleModel
with the specified data type, width, height and number of bits per pixel.- Parameters:
dataType
- the data type for storing samplesw
- the width, in pixels, of the region of image data describedh
- the height, in pixels, of the region of image data describednumberOfBits
- the number of bits per pixel- Throws:
IllegalArgumentException
- ifdataType
is not eitherDataBuffer.TYPE_BYTE
,DataBuffer.TYPE_USHORT
, orDataBuffer.TYPE_INT
-
MultiPixelPackedSampleModel
public MultiPixelPackedSampleModel(int dataType, int w, int h, int numberOfBits, int scanlineStride, int dataBitOffset) Constructs aMultiPixelPackedSampleModel
with specified data type, width, height, number of bits per pixel, scanline stride and data bit offset.- Parameters:
dataType
- the data type for storing samplesw
- the width, in pixels, of the region of image data describedh
- the height, in pixels, of the region of image data describednumberOfBits
- the number of bits per pixelscanlineStride
- the line stride of the image datadataBitOffset
- the data bit offset for the region of image data described- Throws:
RasterFormatException
- if the number of bits per pixel is not a power of 2 or if a power of 2 number of pixels do not fit in one data element.IllegalArgumentException
- ifw
orh
is not greater than 0IllegalArgumentException
- ifdataType
is not eitherDataBuffer.TYPE_BYTE
,DataBuffer.TYPE_USHORT
, orDataBuffer.TYPE_INT
-
-
Method Details
-
createCompatibleSampleModel
Creates a newMultiPixelPackedSampleModel
with the specified width and height. The newMultiPixelPackedSampleModel
has the same storage data type and number of bits per pixel as thisMultiPixelPackedSampleModel
.- Specified by:
createCompatibleSampleModel
in classSampleModel
- Parameters:
w
- the specified widthh
- the specified height- Returns:
- a
SampleModel
with the specified width and height and with the same storage data type and number of bits per pixel as thisMultiPixelPackedSampleModel
. - Throws:
IllegalArgumentException
- ifw
orh
is not greater than 0
-
createDataBuffer
Creates aDataBuffer
that corresponds to thisMultiPixelPackedSampleModel
. TheDataBuffer
object's data type and size is consistent with thisMultiPixelPackedSampleModel
. TheDataBuffer
has a single bank.- Specified by:
createDataBuffer
in classSampleModel
- Returns:
- a
DataBuffer
with the same data type and size as thisMultiPixelPackedSampleModel
.
-
getNumDataElements
public int getNumDataElements()Returns the number of data elements needed to transfer one pixel via thegetDataElements(int, int, java.lang.Object, java.awt.image.DataBuffer)
andsetDataElements(int, int, java.lang.Object, java.awt.image.DataBuffer)
methods. For aMultiPixelPackedSampleModel
, this is one.- Specified by:
getNumDataElements
in classSampleModel
- Returns:
- the number of data elements.
- See Also:
-
getSampleSize
public int[] getSampleSize()Returns the number of bits per sample for all bands.- Specified by:
getSampleSize
in classSampleModel
- Returns:
- the number of bits per sample.
-
getSampleSize
public int getSampleSize(int band) Returns the number of bits per sample for the specified band.- Specified by:
getSampleSize
in classSampleModel
- Parameters:
band
- the specified band- Returns:
- the number of bits per sample for the specified band.
-
getOffset
public int getOffset(int x, int y) Returns the offset of pixel (x, y) in data array elements.- Parameters:
x
- the X coordinate of the specified pixely
- the Y coordinate of the specified pixel- Returns:
- the offset of the specified pixel.
-
getBitOffset
public int getBitOffset(int x) Returns the offset, in bits, into the data element in which it is stored for thex
th pixel of a scanline. This offset is the same for all scanlines.- Parameters:
x
- the specified pixel- Returns:
- the bit offset of the specified pixel.
-
getScanlineStride
public int getScanlineStride()Returns the scanline stride.- Returns:
- the scanline stride of this
MultiPixelPackedSampleModel
.
-
getPixelBitStride
public int getPixelBitStride()Returns the pixel bit stride in bits. This value is the same as the number of bits per pixel.- Returns:
- the
pixelBitStride
of thisMultiPixelPackedSampleModel
.
-
getDataBitOffset
public int getDataBitOffset()Returns the data bit offset in bits.- Returns:
- the
dataBitOffset
of thisMultiPixelPackedSampleModel
.
-
getTransferType
public int getTransferType()Returns the TransferType used to transfer pixels by way of thegetDataElements
andsetDataElements
methods. The TransferType might or might not be the same as the storage DataType. The TransferType is one of DataBuffer.TYPE_BYTE, DataBuffer.TYPE_USHORT, or DataBuffer.TYPE_INT.- Overrides:
getTransferType
in classSampleModel
- Returns:
- the transfertype.
- See Also:
-
createSubsetSampleModel
Creates a newMultiPixelPackedSampleModel
with a subset of the bands of thisMultiPixelPackedSampleModel
. Since aMultiPixelPackedSampleModel
only has one band, the bands argument must have a length of one and indicate the zeroth band.- Specified by:
createSubsetSampleModel
in classSampleModel
- Parameters:
bands
- the specified bands- Returns:
- a new
SampleModel
with a subset of bands of thisMultiPixelPackedSampleModel
. - Throws:
RasterFormatException
- if the number of bands requested is not one.IllegalArgumentException
- ifw
orh
is not greater than 0
-
getSample
Returns asint
the sample in a specified band for the pixel located at (x, y). AnArrayIndexOutOfBoundsException
is thrown if the coordinates are not in bounds.- Specified by:
getSample
in classSampleModel
- Parameters:
x
- the X coordinate of the specified pixely
- the Y coordinate of the specified pixelb
- the band to return, which is assumed to be 0data
- theDataBuffer
containing the image data- Returns:
- the specified band containing the sample of the specified pixel.
- Throws:
ArrayIndexOutOfBoundsException
- if the specified coordinates are not in bounds.- See Also:
-
setSample
Sets a sample in the specified band for the pixel located at (x, y) in theDataBuffer
using anint
for input. AnArrayIndexOutOfBoundsException
is thrown if the coordinates are not in bounds.- Specified by:
setSample
in classSampleModel
- Parameters:
x
- the X coordinate of the specified pixely
- the Y coordinate of the specified pixelb
- the band to return, which is assumed to be 0s
- the input sample as anint
data
- theDataBuffer
where image data is stored- Throws:
ArrayIndexOutOfBoundsException
- if the coordinates are not in bounds.- See Also:
-
getDataElements
Returns data for a single pixel in a primitive array of type TransferType. For aMultiPixelPackedSampleModel
, the array has one element, and the type is the smallest of DataBuffer.TYPE_BYTE, DataBuffer.TYPE_USHORT, or DataBuffer.TYPE_INT that can hold a single pixel. Generally,obj
should be passed in asnull
, so that theObject
is created automatically and is the correct primitive data type.The following code illustrates transferring data for one pixel from
DataBuffer db1
, whose storage layout is described byMultiPixelPackedSampleModel
mppsm1
, toDataBuffer db2
, whose storage layout is described byMultiPixelPackedSampleModel mppsm2
. The transfer is generally more efficient than usinggetPixel
orsetPixel
.MultiPixelPackedSampleModel mppsm1, mppsm2; DataBufferInt db1, db2; mppsm2.setDataElements(x, y, mppsm1.getDataElements(x, y, null, db1), db2);
UsinggetDataElements
orsetDataElements
to transfer between twoDataBuffer/SampleModel
pairs is legitimate if theSampleModels
have the same number of bands, corresponding bands have the same number of bits per sample, and the TransferTypes are the same.If
obj
is notnull
, it should be a primitive array of type TransferType. Otherwise, aClassCastException
is thrown. AnArrayIndexOutOfBoundsException
is thrown if the coordinates are not in bounds, or ifobj
is notnull
and is not large enough to hold the pixel data.- Specified by:
getDataElements
in classSampleModel
- Parameters:
x
- the X coordinate of the specified pixely
- the Y coordinate of the specified pixelobj
- a primitive array in which to return the pixel data ornull
.data
- theDataBuffer
containing the image data.- Returns:
- an
Object
containing data for the specified pixel. - Throws:
ClassCastException
- ifobj
is not a primitive array of type TransferType or is notnull
ArrayIndexOutOfBoundsException
- if the coordinates are not in bounds, or ifobj
is notnull
or not large enough to hold the pixel data- See Also:
-
getPixel
Returns the specified single band pixel in the first element of anint
array.ArrayIndexOutOfBoundsException
is thrown if the coordinates are not in bounds.- Overrides:
getPixel
in classSampleModel
- Parameters:
x
- the X coordinate of the specified pixely
- the Y coordinate of the specified pixeliArray
- the array containing the pixel to be returned ornull
data
- theDataBuffer
where image data is stored- Returns:
- an array containing the specified pixel.
- Throws:
ArrayIndexOutOfBoundsException
- if the coordinates are not in bounds- See Also:
-
setDataElements
Sets the data for a single pixel in the specifiedDataBuffer
from a primitive array of type TransferType. For aMultiPixelPackedSampleModel
, only the first element of the array holds valid data, and the type must be the smallest of DataBuffer.TYPE_BYTE, DataBuffer.TYPE_USHORT, or DataBuffer.TYPE_INT that can hold a single pixel.The following code illustrates transferring data for one pixel from
DataBuffer db1
, whose storage layout is described byMultiPixelPackedSampleModel
mppsm1
, toDataBuffer db2
, whose storage layout is described byMultiPixelPackedSampleModel mppsm2
. The transfer is generally more efficient than usinggetPixel
orsetPixel
.MultiPixelPackedSampleModel mppsm1, mppsm2; DataBufferInt db1, db2; mppsm2.setDataElements(x, y, mppsm1.getDataElements(x, y, null, db1), db2);
UsinggetDataElements
orsetDataElements
to transfer between twoDataBuffer/SampleModel
pairs is legitimate if theSampleModel
objects have the same number of bands, corresponding bands have the same number of bits per sample, and the TransferTypes are the same.obj
must be a primitive array of type TransferType. Otherwise, aClassCastException
is thrown. AnArrayIndexOutOfBoundsException
is thrown if the coordinates are not in bounds, or ifobj
is not large enough to hold the pixel data.- Specified by:
setDataElements
in classSampleModel
- Parameters:
x
- the X coordinate of the pixel locationy
- the Y coordinate of the pixel locationobj
- a primitive array containing pixel datadata
- theDataBuffer
containing the image data- See Also:
-
setPixel
Sets a pixel in theDataBuffer
using anint
array for input.ArrayIndexOutOfBoundsException
is thrown if the coordinates are not in bounds.- Overrides:
setPixel
in classSampleModel
- Parameters:
x
- the X coordinate of the pixel locationy
- the Y coordinate of the pixel locationiArray
- the input pixel in anint
arraydata
- theDataBuffer
containing the image data- See Also:
-