Class PixelIterator


  • public class PixelIterator
    extends Object
    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:
    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 this PixelIterator 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 the TYPE_* constants defined in the DataBuffer 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 returns true 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, or null 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, or null 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, or null 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, or null if none.