Object
PixelIterator
A row-major iterator over sample values in a
Raster or RenderedImage.
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 the RenderedImage.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 a while 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:
-
Constructor Summary
ConstructorsConstructorDescriptionPixelIterator(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 TypeMethodDescriptionvoidassertSampleValuesEqual(PixelIterator actual, double tolerance) Compares all sample values iterated by thisPixelIteratorwith the sample values iterated by the given iterator.intgetBand()Returns the current band index.intReturns the type of the sample values, as one of theTYPE_*constants defined in theDataBufferclass.intReturns the sample value at the current position, as an integer.doubleReturns the sample value at the current position, as a double-precision floating point number.floatReturns the sample value at the current position, as a floating point number.intgetX()Returns the current x coordinate.intgetY()Returns the current y coordinate.booleannext()Moves to the next sample values and returnstrueif the iteration has more pixels.toString()Returns a string representation of this iterator position for debugging purpose.
-
Constructor Details
-
PixelIterator
Creates an iterator for the whole area of the given raster.- Parameters:
raster- The raster for which to create an iterator.
-
PixelIterator
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, ornullif 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, ornullif 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, ornullif 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, ornullif none.
-
-
Method Details
-
next
Moves to the next sample values and returnstrueif the iteration has more pixels.- Returns:
trueif the next sample value exist.
-
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:
-
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:
-
getBand
Returns the current band index. The index values range from 0 (inclusive) to the number of bands (exclusive), or to thesourceBandsarray length (exclusive) if the array given to the constructor was non-null.- Returns:
- the current band index.
- See Also:
-
getDataType
Returns the type of the sample values, as one of theTYPE_*constants defined in theDataBufferclass.- Returns:
- the type of the sample values.
- See Also:
-
getSample
-
getSampleFloat
Returns the sample value at the current position, as a floating point number.- Returns:
- the sample value at the current position.
- See Also:
-
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:
-
assertSampleValuesEqual
Compares all sample values iterated by thisPixelIteratorwith the sample values iterated by the given iterator. If a mismatch is found, then anAssertionErroris 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:
- Differences in the (x, y) origin;
- Differences in tile layout (images are compared as if they were untiled);
- Differences in the datatype (values are compared using the widest of this iterator datatype and the datatype of the given iterator).
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.
-
toString
-