Class WritablePixelIterator
Object
PixelIterator
WritablePixelIterator
- All Implemented Interfaces:
Closeable, AutoCloseable
A pixel iterator capable to write sample values. This iterator can edit pixel values in place,
or write values in a different destination image than the source image. Source and destination
images must use the same sample model and the same coordinates (both for pixels and tiles).
Casting a
To check if a
Contrary to PixelIterator, WritablePixelIterator needs to be closed after
iteration in order to release tiles. Example:
try (WritablePixelIterator it = WritablePixelIterator.create(image)) {
double[] samples = null;
while (it.next()) {
samples = it.getPixel(samples); // Get values in all bands.
// Perform computation here...
it.setPixels(sample); // Replace values in all bands.
}
}
Casting a PixelIterator
To check if a PixelIterator can be used for writing pixels, a … instanceof WritablePixelIterator
check is not sufficient. The PixelIterator.isWritable() method should be invoked instead.- Since:
- 1.0
-
Nested Class Summary
Nested classes/interfaces inherited from class PixelIterator
PixelIterator.Builder, PixelIterator.Window<T>Modifier and TypeClassDescriptionstatic classBuilds pixel iterators for specified region of interest, window size or iteration order.static classPixelIterator.Window<T extends Buffer>Contains the sample values in a moving window over the image. -
Method Summary
Modifier and TypeMethodDescriptionvoidclose()Releases any resources hold by this iterator.static WritablePixelIteratorCreates an iterator for all pixels in the given image.booleanReturnstrueif this iterator can write pixel values.voidsetDataElements(Object values) Sets the data elements (not necessarily band values) of current pixel.voidsetPixel(double[] values) Sets the sample values of current pixel for all bands.voidsetPixel(float[] values) Sets the sample values of current pixel for all bands.voidsetPixel(int[] values) Sets the sample values of current pixel for all bands.voidSets all remaining pixels to floating-point values computed by the given functions.voidsetRemainingPixels(IntBinaryOperator[] bands) Sets all remaining pixels to integer values computed by the given functions.voidsetSample(int band, double value) Writes a sample value in the specified band of current pixel.voidsetSample(int band, float value) Writes a sample value in the specified band of current pixel.voidsetSample(int band, int value) Writes a sample value in the specified band of current pixel.Methods inherited from class PixelIterator
create, createWindow, getDataElements, getDataType, getDomain, getIterationOrder, getNumBands, getPixel, getPixel, getPixel, getPosition, getSample, getSampleDouble, getSampleFloat, getSampleRanges, getTransferType, moveTo, next, rewindModifier and TypeMethodDescriptionstatic PixelIteratorcreate(RenderedImage data) Creates an iterator for all pixels in the given image.<T extends Buffer>
PixelIterator.Window<T> createWindow(TransferType<T> type) Returns a moving window over the sample values in a rectangular region starting at iterator position.getDataElements(Object dest) Returns the data elements (not necessarily band values) of current pixel.Returns the type of sample values.Returns the pixel coordinates of the region where this iterator is doing the iteration.Returns the order in which pixels are traversed.intReturns the number of bands (samples per pixel) in the image or raster.double[]getPixel(double[] dest) Returns the sample values of current pixel for all bands.float[]getPixel(float[] dest) Returns the sample values of current pixel for all bands.int[]getPixel(int[] dest) Returns the sample values of current pixel for all bands.Returns the column (x) and row (y) indices of the current pixel.intgetSample(int band) Returns the sample value in the specified band of current pixel, rounded toward zero.doublegetSampleDouble(int band) Returns the sample value in the specified band of current pixel, without precision lost.floatgetSampleFloat(int band) Returns the sample value in the specified band of current pixel as a single-precision floating point number.NumberRange<?>[]Returns the range of sample values that can be stored in each band of the rendered image or raster.Returns the most efficient type (int,floatordouble) for transferring data between the underlying rasters and this iterator.voidmoveTo(int px, int py) Moves the pixel iterator to the given column (x) and row (y) indices.booleannext()Moves the iterator to the next pixel.voidrewind()Restores the iterator to the start position.
-
Method Details
-
create
Creates an iterator for all pixels in the given image. This is a convenience method fornew Builder().createWritable(data).- Parameters:
data- the image which contains the sample values on which to iterate.- Returns:
- a new iterator traversing all pixels in the given image, in arbitrary order.
-
isWritable
public boolean isWritable()Returnstrueif this iterator can write pixel values. For some implementations, being an instance ofWritablePixelIteratoris not sufficient for being able to write pixel values.Note: all instances created by
WritablePixelIterator.create(…)methods are guaranteed totrue.- Overrides:
isWritablein classPixelIterator- Returns:
trueif this iterator can be used for writing pixel values.
-
setSample
public void setSample(int band, int value) Writes a sample value in the specified band of current pixel. ThePixelIterator.next()method must have returnedtrue, or thePixelIterator.moveTo(int,int)method must have been invoked successfully, before thissetSample(int, int)method is invoked. If above condition is not met, then this method behavior is undefined (there is no explicit bounds check for performance reasons).- Parameters:
band- the band in which to set the sample value.value- the sample value to write in the specified band.- See Also:
-
setSample
public void setSample(int band, float value) Writes a sample value in the specified band of current pixel. ThePixelIterator.next()method must have returnedtrue, or thePixelIterator.moveTo(int,int)method must have been invoked successfully, before thissetSample(int, float)method is invoked. If above condition is not met, then this method behavior is undefined (there is no explicit bounds check for performance reasons).- Parameters:
band- the band in which to set the sample value.value- the sample value to write in the specified band.- See Also:
-
setSample
public void setSample(int band, double value) Writes a sample value in the specified band of current pixel. ThePixelIterator.next()method must have returnedtrue, or thePixelIterator.moveTo(int,int)method must have been invoked successfully, before thissetSample(int, double)method is invoked. If above condition is not met, then this method behavior is undefined (there is no explicit bounds check for performance reasons).- Parameters:
band- the band in which to set the sample value.value- the sample value to write in the specified band.- See Also:
-
setPixel
public void setPixel(int[] values) Sets the sample values of current pixel for all bands. ThePixelIterator.next()method must have returnedtrue, or thePixelIterator.moveTo(int,int)method must have been invoked successfully, before thissetPixel(…)method is invoked. If above condition is not met, then this method behavior is undefined (there is no explicit bounds check for performance reasons).- Parameters:
values- the new sample values for current pixel.- See Also:
-
setPixel
public void setPixel(float[] values) Sets the sample values of current pixel for all bands. ThePixelIterator.next()method must have returnedtrue, or thePixelIterator.moveTo(int,int)method must have been invoked successfully, before thissetPixel(…)method is invoked. If above condition is not met, then this method behavior is undefined (there is no explicit bounds check for performance reasons).- Parameters:
values- the new sample values for current pixel.- See Also:
-
setPixel
public void setPixel(double[] values) Sets the sample values of current pixel for all bands. ThePixelIterator.next()method must have returnedtrue, or thePixelIterator.moveTo(int,int)method must have been invoked successfully, before thissetPixel(…)method is invoked. If above condition is not met, then this method behavior is undefined (there is no explicit bounds check for performance reasons).- Parameters:
values- the new sample values for current pixel.- See Also:
-
setRemainingPixels
Sets all remaining pixels to integer values computed by the given functions. Each function will receive pixel coordinates (x, y) and shall return the sample value to store in one band of the image at that pixel position. The functions shall be given in the same order as the bands. Therefore, the function at index i computes the sample values of band i.- Parameters:
bands- functions providing sample values in each band, in order.- Throws:
IllegalArgumentException- if the number of functions is not equal to the number of bands.- Since:
- 1.7
-
setRemainingPixels
Sets all remaining pixels to floating-point values computed by the given functions. Each function will receive pixel coordinates (x, y) and shall return the sample value to store in one band of the image at that pixel position. The functions shall be given in the same order as the bands. Therefore, the function at index i computes the sample values of band i.- Parameters:
bands- functions providing sample values in each band, in order.- Throws:
IllegalArgumentException- if the number of functions is not equal to the number of bands.- Since:
- 1.7
-
setDataElements
Sets the data elements (not necessarily band values) of current pixel. TheObjectargument is a relatively opaque format (it may beint[],byte[], etc.). It should be the value provided by a call toPixelIterator.getDataElements(Object)on an image using a compatible sample model.- Parameters:
values- the new the data elements.- Since:
- 1.1
- See Also:
-
close
public void close()Releases any resources hold by this iterator. If some pixel values have been written, the changes are committed.- Specified by:
closein interfaceAutoCloseable- Specified by:
closein interfaceCloseable
-