Package org.opengis.test.coverage.image
Class PixelIterator
- Object
-
- PixelIterator
-
public class PixelIterator extends Object
A row-major iterator over sample values in aRaster
orRenderedImage
. For any image (tiled or not), this class iterates first over the bands, then over the columns and finally over the rows. If the image is tiled, then this iterator will perform the necessary calls to theRenderedImage.getTile(int, int)
method for each row in order to perform the iteration as if the image was untiled.On creation, this iterator is positioned before the first sample value. To use this iterator, invoke the
next()
method in awhile
loop as below:PixelIterator it = new PixelIterator(image); while (it.next()) { float value = it.getSampleFloat(); // Do some processing with the value here... }
- Since:
- 3.1
- See Also:
Assert.assertSampleValuesEqual(String, RenderedImage, RenderedImage, double)
-
-
Constructor Summary
Constructors Constructor Description PixelIterator(Raster raster)
Creates an iterator for the whole area of the given raster.PixelIterator(Raster raster, Rectangle subArea, int xSubsampling, int ySubsampling, int[] sourceBands)
Creates an iterator for a sub-area of the given raster.PixelIterator(RenderedImage image)
Creates an iterator for the whole area of the given image.PixelIterator(RenderedImage image, Rectangle subArea, int xSubsampling, int ySubsampling, int[] sourceBands)
Creates an iterator for a sub-area of the given image.
-
Method Summary
Modifier and Type Method Description void
assertSampleValuesEqual(PixelIterator actual, double tolerance)
Compares all sample values iterated by thisPixelIterator
with the sample values iterated by the given iterator.int
getBand()
Returns the current band index.int
getDataType()
Returns the type of the sample values, as one of theTYPE_*
constants defined in theDataBuffer
class.int
getSample()
Returns the sample value at the current position, as an integer.double
getSampleDouble()
Returns the sample value at the current position, as a double-precision floating point number.float
getSampleFloat()
Returns the sample value at the current position, as a floating point number.int
getX()
Returns the current x coordinate.int
getY()
Returns the current y coordinate.boolean
next()
Moves to the next sample values and returnstrue
if the iteration has more pixels.String
toString()
Returns a string representation of this iterator position for debugging purpose.
-
-
-
Constructor Detail
-
PixelIterator
public PixelIterator(Raster raster)
Creates an iterator for the whole area of the given raster.- Parameters:
raster
- The raster for which to create an iterator.
-
PixelIterator
public PixelIterator(RenderedImage image)
Creates an iterator for the whole area of the given image.- Parameters:
image
- The image for which to create an iterator.
-
PixelIterator
public PixelIterator(Raster raster, Rectangle subArea, int xSubsampling, int ySubsampling, int[] sourceBands)
Creates an iterator for a sub-area of the given raster.- Parameters:
raster
- the raster to iterate over.subArea
- rectangle which represent raster sub area iteration, ornull
if none.xSubsampling
- the iteration step when moving to the next pixel.ySubsampling
- the iteration step when moving to the next scan line.sourceBands
- the source bands, ornull
if none.
-
PixelIterator
public PixelIterator(RenderedImage image, Rectangle subArea, int xSubsampling, int ySubsampling, int[] sourceBands)
Creates an iterator for a sub-area of the given image.- Parameters:
image
- the image to iterate over.subArea
- rectangle which represent image sub area iteration, ornull
if none.xSubsampling
- the iteration step when moving to the next pixel.ySubsampling
- the iteration step when moving to the next scan line.sourceBands
- the source bands, ornull
if none.
-
-
Method Detail
-
next
public boolean next()
Moves to the next sample values and returnstrue
if the iteration has more pixels.- Returns:
true
if the next sample value exist.
-
getX
public int getX()
Returns the current x coordinate. The coordinate values range from image X minimum (inclusive) to that minimum plus the image width (exclusive).- Returns:
- the current x coordinate.
- See Also:
RenderedImage.getMinX()
,RenderedImage.getWidth()
-
getY
public int getY()
Returns the current y coordinate. The coordinate values range from image Y minimum (inclusive) to that minimum plus the image height (exclusive).- Returns:
- the current y coordinate.
- See Also:
RenderedImage.getMinY()
,RenderedImage.getHeight()
-
getBand
public int getBand()
Returns the current band index. The index values range from 0 (inclusive) to the number of bands (exclusive), or to thesourceBands
array length (exclusive) if the array given to the constructor was non-null.- Returns:
- the current band index.
- See Also:
SampleModel.getNumBands()
-
getDataType
public int getDataType()
Returns the type of the sample values, as one of theTYPE_*
constants defined in theDataBuffer
class.- Returns:
- the type of the sample values.
- See Also:
SampleModel.getDataType()
,DataBuffer.TYPE_BYTE
,DataBuffer.TYPE_SHORT
,DataBuffer.TYPE_USHORT
,DataBuffer.TYPE_INT
,DataBuffer.TYPE_FLOAT
,DataBuffer.TYPE_DOUBLE
-
getSample
public int getSample()
Returns the sample value at the current position, as an integer. This method is appropriate for the byte, short, unsigned short and integer datatypes.- Returns:
- the sample value at the current position.
- See Also:
Raster.getSample(int, int, int)
,DataBuffer.TYPE_BYTE
,DataBuffer.TYPE_SHORT
,DataBuffer.TYPE_USHORT
,DataBuffer.TYPE_INT
-
getSampleFloat
public float getSampleFloat()
Returns the sample value at the current position, as a floating point number.- Returns:
- the sample value at the current position.
- See Also:
Raster.getSampleFloat(int, int, int)
,DataBuffer.TYPE_FLOAT
-
getSampleDouble
public double getSampleDouble()
Returns the sample value at the current position, as a double-precision floating point number.- Returns:
- the sample value at the current position.
- See Also:
Raster.getSampleDouble(int, int, int)
,DataBuffer.TYPE_DOUBLE
-
assertSampleValuesEqual
public void assertSampleValuesEqual(PixelIterator actual, double tolerance) throws AssertionError
Compares all sample values iterated by thisPixelIterator
with the sample values iterated by the given iterator. If a mismatch is found, then anAssertionError
is thrown with a detailed error message.This method does not verify the image sizes, number of tiles, number of bands, color model or datatype. Consequently this method is robust to the following differences:
If the images have different sizes, then an "Unexpected end of iteration" exception will be thrown when the first iterator reaches the iteration end.- Parameters:
actual
- the iterator that contains the actual values to be compared with the "expected" sample values.tolerance
- the tolerance threshold for floating point comparison. This threshold does not apply to integer types.- Throws:
AssertionError
- if a value in this iterator is not equals to a value in the given iterator with the given tolerance threshold.
-
-