Class ContourPlot.PixelConvertingSpliterator<P extends ContourPlot.PixelBase,T>
- java.lang.Object
-
- gov.nih.mipav.model.algorithms.ContourPlot.PixelConvertingSpliterator<P,T>
-
- Type Parameters:
P- the pixel type of the underlying SpliteratorT- the type of elements returned by the PixelConvertingSpliterator
- All Implemented Interfaces:
java.util.Spliterator<T>
- Enclosing class:
- ContourPlot
public static class ContourPlot.PixelConvertingSpliterator<P extends ContourPlot.PixelBase,T> extends java.lang.Object implements java.util.Spliterator<T>The PixelConvertingSpliterator enables iterating anContourPlot.Imgwith a different datatype thanContourPlot.Pixel(ContourPlot.ImgBase.spliterator()). This can come in handy if aConsumerwould be easier to write when not restricted to the Pixel datatype.For example, a specific operation should be applied to each pixel of the Img which is specified as a function that takes a 3-dimensional vector as argument with red, green and blue component e.g.
applyOperation(Vector3D vec){...}}
To obtain a spliterator of the Img that will accept aConsumer<Vector3D>a PixelConvertingSpliterator can be created from an ordinarySpliterator<Pixel>providing the following functions:
- element allocator: a function to create (allocate) an object of the desired datatype (Vector3D in this example)
-
from Pixel converter: a function to convert a Pixel object to an object
of the desired datatype e.g.
void convertPixel(Pixel px, Vector3D vec) -
to Pixel converter: a function to convert an object of the desired
datatype to a Pixel object e.g.
void convertVector(Vector3D vec, Pixel px)
APixelConvertingSpliterator<Vector3D>would then be created like this:Img img=loadImgFromSomewhere(); Spliterator<Vector3D> split = new PixelConvertingSpliterator<>( img.spliterator(), ()->{return new Vector3D();}, MyConverter::convertPixel, MyConverter::convertVector ); StreamSupport.stream(split, true) .forEach(VectorOperations::applyOperation);The reason for not simply doing an 'on the fly' conversion inside a
Spliterator<Pixel>or using theStream.map(java.util.function.Function)function is, that these methods are prone to excessive object allocation (allocating a new object for every pixel). When using the PixelConvertingSpliterator there is only one object allocation per split, and the object will be reused for each pixel within that split.- Since:
- 1.4
- Author:
- hageldave
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description static interfaceContourPlot.PixelConvertingSpliterator.PixelConverter<P extends ContourPlot.PixelBase,T>-
Nested classes/interfaces inherited from interface java.util.Spliterator
java.util.Spliterator.OfDouble, java.util.Spliterator.OfInt, java.util.Spliterator.OfLong, java.util.Spliterator.OfPrimitive<T extends java.lang.Object,T_CONS extends java.lang.Object,T_SPLITR extends java.util.Spliterator.OfPrimitive<T,T_CONS,T_SPLITR>>
-
-
Field Summary
Fields Modifier and Type Field Description protected ContourPlot.PixelConvertingSpliterator.PixelConverter<P,T>converterprotected java.util.Spliterator<? extends P>delegateSpliterator<Pixel>acting as delegate of this spliteratorprotected Telementthe element of this spliterator (reused on each pixel of the delegate)
-
Constructor Summary
Constructors Constructor Description PixelConvertingSpliterator(java.util.Spliterator<? extends P> delegate, ContourPlot.PixelConvertingSpliterator.PixelConverter<P,T> converter)PixelConvertingSpliterator(java.util.Spliterator<? extends P> delegate, java.util.function.Supplier<T> elementAllocator, java.util.function.BiConsumer<P,T> fromPixelConverter, java.util.function.BiConsumer<T,P> toPixelConverter)Constructs a new PixelConvertingSpliterator.
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description intcharacteristics()longestimateSize()voidforEachRemaining(java.util.function.Consumer<? super T> action)static ContourPlot.PixelConvertingSpliterator.PixelConverter<ContourPlot.PixelBase,double[]>getDoubleArrayConverter()static ContourPlot.PixelConvertingSpliterator<ContourPlot.PixelBase,double[]>getDoubletArrayElementSpliterator(java.util.Spliterator<? extends ContourPlot.PixelBase> pixelSpliterator)Example implementation of aPixelConvertingSpliterator<double[]>.booleantryAdvance(java.util.function.Consumer<? super T> action)java.util.Spliterator<T>trySplit()
-
-
-
Field Detail
-
delegate
protected final java.util.Spliterator<? extends P extends ContourPlot.PixelBase> delegate
Spliterator<Pixel>acting as delegate of this spliterator- Since:
- 1.4
-
element
protected final T element
the element of this spliterator (reused on each pixel of the delegate)- Since:
- 1.4
-
converter
protected ContourPlot.PixelConvertingSpliterator.PixelConverter<P extends ContourPlot.PixelBase,T> converter
-
-
Constructor Detail
-
PixelConvertingSpliterator
public PixelConvertingSpliterator(java.util.Spliterator<? extends P> delegate, java.util.function.Supplier<T> elementAllocator, java.util.function.BiConsumer<P,T> fromPixelConverter, java.util.function.BiConsumer<T,P> toPixelConverter)
Constructs a new PixelConvertingSpliterator.- Parameters:
delegate- theSpliterator<Pixel>this spliterator delegates to.elementAllocator- method for allocating an object of this spliterator's element type.fromPixelConverter- method for setting up an element of this spliterator according to its underlying pixel.toPixelConverter- method for adopting an underlying pixel value according to an element of this spliterator.- Since:
- 1.4
-
PixelConvertingSpliterator
public PixelConvertingSpliterator(java.util.Spliterator<? extends P> delegate, ContourPlot.PixelConvertingSpliterator.PixelConverter<P,T> converter)
-
-
Method Detail
-
tryAdvance
public boolean tryAdvance(java.util.function.Consumer<? super T> action)
- Specified by:
tryAdvancein interfacejava.util.Spliterator<P extends ContourPlot.PixelBase>
-
forEachRemaining
public void forEachRemaining(java.util.function.Consumer<? super T> action)
- Specified by:
forEachRemainingin interfacejava.util.Spliterator<P extends ContourPlot.PixelBase>
-
trySplit
public java.util.Spliterator<T> trySplit()
- Specified by:
trySplitin interfacejava.util.Spliterator<P extends ContourPlot.PixelBase>
-
estimateSize
public long estimateSize()
- Specified by:
estimateSizein interfacejava.util.Spliterator<P extends ContourPlot.PixelBase>
-
characteristics
public int characteristics()
- Specified by:
characteristicsin interfacejava.util.Spliterator<P extends ContourPlot.PixelBase>
-
getDoubletArrayElementSpliterator
public static ContourPlot.PixelConvertingSpliterator<ContourPlot.PixelBase,double[]> getDoubletArrayElementSpliterator(java.util.Spliterator<? extends ContourPlot.PixelBase> pixelSpliterator)
Example implementation of aPixelConvertingSpliterator<double[]>.The elements of the returned spliterator will be
double[]of length 3 with normalized red, green and blue channels on index 0, 1 and 2.Code:
Supplier<double[]> arrayAllocator = () -> { return new double[3]; }; BiConsumer<Pixel, double[]> convertToArray = (px, array) -> { array[0]=px.r_normalized(); array[1]=px.g_normalized(); array[2]=px.b_normalized(); }; BiConsumer<double[], Pixel> convertToPixel = (array, px) -> { px.setRGB_fromNormalized_preserveAlpha( // clamp values between zero and one Math.min(1, Math.max(0, array[0])), Math.min(1, Math.max(0, array[1])), Math.min(1, Math.max(0, array[2]))); }; PixelConvertingSpliterator<double[]> arraySpliterator = new PixelConvertingSpliterator<>( pixelSpliterator, arrayAllocator, convertToArray, convertToPixel);- Parameters:
pixelSpliterator- theSpliterator<Pixel>to which the returned spliterator delegates to.- Returns:
- a spliterator with float[] elements consisting of normalized RGB channels.
- Since:
- 1.4
-
getDoubleArrayConverter
public static ContourPlot.PixelConvertingSpliterator.PixelConverter<ContourPlot.PixelBase,double[]> getDoubleArrayConverter()
- Returns:
- Exemplary PixelConverter that converts to double[].
-
-