Skip to content

Commit 77806c8

Browse files
committed
Convert: add operator factories, refactor, javadoc
1 parent 49d1351 commit 77806c8

File tree

2 files changed

+95
-8
lines changed

2 files changed

+95
-8
lines changed

src/main/java/net/imglib2/algorithm/blocks/UnaryBlockOperator.java

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -34,11 +34,10 @@
3434
package net.imglib2.algorithm.blocks;
3535

3636
import net.imglib2.algorithm.blocks.convert.ClampType;
37+
import net.imglib2.algorithm.blocks.convert.Convert;
3738
import net.imglib2.type.NativeType;
3839
import net.imglib2.util.Cast;
3940

40-
import static net.imglib2.algorithm.blocks.convert.Convert.convert;
41-
4241
/**
4342
* Wraps {@code BlockProcessor<I,O>}, where {@code I} is the primitive array
4443
* type backing ImgLib2 {@code NativeType} {@code S} and {@code O} is the
@@ -147,7 +146,7 @@ default < U extends NativeType< U > > UnaryBlockOperator< U, T > adaptSourceType
147146
if ( newSourceType.getClass().isInstance( getSourceType() ) )
148147
return Cast.unchecked( this );
149148
else
150-
return convert( newSourceType, getSourceType(), clamp ).andThen( this );
149+
return Convert.createOperator( newSourceType, getSourceType(), clamp ).andThen( this );
151150
}
152151

153152
/**
@@ -160,6 +159,6 @@ default < U extends NativeType< U > > UnaryBlockOperator< S, U > adaptTargetTyp
160159
if ( newTargetType.getClass().isInstance( getTargetType() ) )
161160
return Cast.unchecked( this );
162161
else
163-
return this.andThen( convert( getTargetType(), newTargetType, clamp ) );
162+
return this.andThen( Convert.createOperator( getTargetType(), newTargetType, clamp ) );
164163
}
165164
}

src/main/java/net/imglib2/algorithm/blocks/convert/Convert.java

Lines changed: 92 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -33,8 +33,10 @@
3333
*/
3434
package net.imglib2.algorithm.blocks.convert;
3535

36+
import java.util.function.Function;
3637
import java.util.function.Supplier;
3738

39+
import net.imglib2.algorithm.blocks.BlockSupplier;
3840
import net.imglib2.algorithm.blocks.DefaultUnaryBlockOperator;
3941
import net.imglib2.algorithm.blocks.UnaryBlockOperator;
4042
import net.imglib2.converter.Converter;
@@ -59,15 +61,101 @@
5961
*/
6062
public class Convert
6163
{
64+
/**
65+
* Create {@link UnaryBlockOperator} to convert blocks from {@code S} to
66+
* {@code T}.
67+
* <p>
68+
* Supported source/target types are {@code ByteType}, {@code
69+
* UnsignedByteType}, {@code ShortType}, {@code UnsignedShortType}, {@code
70+
* IntType}, {@code UnsignedIntType}, {@code LongType}, {@code
71+
* UnsignedLongType}, {@code FloatType}, and {@code DoubleType}.
72+
* <p>
73+
* Target values are not clamped, so overflow may occur if the source type
74+
* has a larger range than the target type.
75+
* <p>
76+
* The returned factory function creates an operator matching the type
77+
* {@code S} of the given input {@code BlockSupplier<T>}.
78+
*
79+
* @param targetType
80+
* an instance of the target type
81+
* @param <S>
82+
* source type
83+
* @param <T>
84+
* target type
85+
*
86+
* @return factory for {@code UnaryBlockOperator} to convert blocks from {@code S} to {@code T}
87+
*/
88+
public static < S extends NativeType< S >, T extends NativeType< T > >
89+
Function< BlockSupplier< S >, UnaryBlockOperator< S, T > > convert( final T targetType )
90+
{
91+
return convert( targetType, ClampType.NONE );
92+
}
93+
94+
/**
95+
* Create {@link UnaryBlockOperator} to convert blocks from {@code S} to
96+
* {@code T}.
97+
* <p>
98+
* Supported source/target types are {@code ByteType}, {@code
99+
* UnsignedByteType}, {@code ShortType}, {@code UnsignedShortType}, {@code
100+
* IntType}, {@code UnsignedIntType}, {@code LongType}, {@code
101+
* UnsignedLongType}, {@code FloatType}, and {@code DoubleType}.
102+
* <p>
103+
* If the target type cannot represent the full range of the source type,
104+
* values are clamped according to the specified {@link ClampType}.
105+
* <p>
106+
* The returned factory function creates an operator matching the type
107+
* {@code S} of the given input {@code BlockSupplier<T>}.
108+
*
109+
* @param targetType
110+
* an instance of the target type
111+
* @param clamp
112+
* ClampType
113+
* @param <S>
114+
* source type
115+
* @param <T>
116+
* target type
117+
*
118+
* @return factory for {@code UnaryBlockOperator} to convert blocks from {@code S} to {@code T}
119+
*/
120+
public static < S extends NativeType< S >, T extends NativeType< T > >
121+
Function< BlockSupplier< S >, UnaryBlockOperator< S, T > > convert( final T targetType, final ClampType clamp )
122+
{
123+
return s -> createOperator( s.getType(), targetType, clamp );
124+
}
125+
126+
/**
127+
* Create {@link UnaryBlockOperator} to convert blocks from {@code S} to
128+
* {@code T} with the specified {@code Converter}.
129+
* <p>
130+
* The returned factory function creates an operator matching the type
131+
* {@code S} of the given input {@code BlockSupplier<T>}.
132+
*
133+
* @param targetType
134+
* an instance of the target type
135+
* @param converterSupplier
136+
* creates new converter instances
137+
* @param <S>
138+
* source type
139+
* @param <T>
140+
* target type
141+
*
142+
* @return factory for {@code UnaryBlockOperator} to convert blocks from {@code S} to {@code T}
143+
*/
144+
public static < S extends NativeType< S >, T extends NativeType< T > >
145+
Function< BlockSupplier< S >, UnaryBlockOperator< S, T > > convert( final T targetType, Supplier< Converter< ? super S, T > > converterSupplier )
146+
{
147+
return s -> createOperator( s.getType(), targetType, converterSupplier );
148+
}
149+
62150
/**
63151
* Create {@link UnaryBlockOperator} to convert blocks between {@code
64152
* sourceType} and {@code targetType}.
65153
* No clamping.
66154
*/
67155
public static < S extends NativeType< S >, T extends NativeType< T > >
68-
UnaryBlockOperator< S, T > convert( final S sourceType, final T targetType )
156+
UnaryBlockOperator< S, T > createOperator( final S sourceType, final T targetType )
69157
{
70-
return convert( sourceType, targetType, ClampType.NONE );
158+
return createOperator( sourceType, targetType, ClampType.NONE );
71159
}
72160

73161
/**
@@ -76,7 +164,7 @@ UnaryBlockOperator< S, T > convert( final S sourceType, final T targetType )
76164
* Clamp target values according to {@code clamp}.
77165
*/
78166
public static < S extends NativeType< S >, T extends NativeType< T > >
79-
UnaryBlockOperator< S, T > convert( final S sourceType, final T targetType, final ClampType clamp )
167+
UnaryBlockOperator< S, T > createOperator( final S sourceType, final T targetType, final ClampType clamp )
80168
{
81169
return new DefaultUnaryBlockOperator<>(
82170
sourceType, targetType, 0, 0,
@@ -88,7 +176,7 @@ UnaryBlockOperator< S, T > convert( final S sourceType, final T targetType, fina
88176
* sourceType} and {@code targetType} with the specified {@code Converter}.
89177
*/
90178
public static < S extends NativeType< S >, T extends NativeType< T > >
91-
UnaryBlockOperator< S, T > convert( final S sourceType, final T targetType, Supplier< Converter< ? super S, T > > converterSupplier )
179+
UnaryBlockOperator< S, T > createOperator( final S sourceType, final T targetType, Supplier< Converter< ? super S, T > > converterSupplier )
92180
{
93181
return new DefaultUnaryBlockOperator<>(
94182
sourceType, targetType, 0, 0,

0 commit comments

Comments
 (0)