11
11
* %%
12
12
* Redistribution and use in source and binary forms, with or without
13
13
* modification, are permitted provided that the following conditions are met:
14
- *
14
+ *
15
15
* 1. Redistributions of source code must retain the above copyright notice,
16
16
* this list of conditions and the following disclaimer.
17
17
* 2. Redistributions in binary form must reproduce the above copyright notice,
18
18
* this list of conditions and the following disclaimer in the documentation
19
19
* and/or other materials provided with the distribution.
20
- *
20
+ *
21
21
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
22
22
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23
23
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
33
33
*/
34
34
package net .imglib2 .algorithm .blocks .transform ;
35
35
36
+ import net .imglib2 .algorithm .blocks .BlockSupplier ;
36
37
import net .imglib2 .algorithm .blocks .DefaultUnaryBlockOperator ;
37
38
import net .imglib2 .algorithm .blocks .UnaryBlockOperator ;
38
39
import net .imglib2 .algorithm .blocks .ClampType ;
46
47
47
48
import static net .imglib2 .type .PrimitiveType .FLOAT ;
48
49
50
+ import java .util .function .Function ;
51
+
49
52
/**
50
53
* Affine transform in 2D/3D with n-linear or nearest-neighbor interpolation.
51
54
*/
@@ -69,26 +72,58 @@ public enum Interpolation
69
72
}
70
73
71
74
/**
72
- * Create a {@code UnaryBlockOperator} to interpolate and affine-transform
73
- * blocks of the standard ImgLib2 {@code RealType}s.
75
+ * Interpolate and affine-transform blocks of the standard ImgLib2 {@code
76
+ * RealType}s.
74
77
* <p>
75
78
* Only 2D and 3D are supported currently!
79
+ * <p>
80
+ * The returned factory function creates an operator matching the type a
81
+ * given input {@code BlockSupplier<T>}.
76
82
*
77
- * @param type
78
- * instance of the input type
79
83
* @param transformFromSource
80
84
* a 2D or 3D affine transform
81
85
* @param interpolation
82
86
* which interpolation method to use
83
87
* @param <T>
84
88
* the input/output type
85
89
*
86
- * @return {@code UnaryBlockOperator} to affine-transform blocks of type {@code T}
90
+ * @return factory for {@code UnaryBlockOperator} to affine-transform blocks of type {@code T}
91
+ */
92
+ public static < T extends NativeType < T > >
93
+ Function < BlockSupplier < T >, UnaryBlockOperator < T , T > > affine ( final AffineGet transformFromSource , Interpolation interpolation )
94
+ {
95
+ return affine ( transformFromSource , interpolation , ComputationType .AUTO );
96
+ }
97
+
98
+ /**
99
+ * Interpolate and affine-transform blocks of the standard ImgLib2 {@code
100
+ * RealType}s.
101
+ * <p>
102
+ * Only 2D and 3D are supported currently!
103
+ * <p>
104
+ * The returned factory function creates an operator matching the type a
105
+ * given input {@code BlockSupplier<T>}.
106
+ *
107
+ * @param transformFromSource
108
+ * a 2D or 3D affine transform
109
+ * @param interpolation
110
+ * which interpolation method to use
111
+ * @param computationType
112
+ * For n-linear interpolation, this specifies in which precision
113
+ * intermediate values should be computed. For {@code AUTO}, the type
114
+ * that can represent the input/output type without loss of precision
115
+ * is picked. That is, {@code FLOAT} for u8, i8, u16, i16, i32, f32,
116
+ * and otherwise {@code DOUBLE} for u32, i64, f64. For nearest-neighbor
117
+ * interpolation, {@code computationType} is not used.
118
+ * @param <T>
119
+ * the input/output type
120
+ *
121
+ * @return factory for {@code UnaryBlockOperator} to affine-transform blocks of type {@code T}
87
122
*/
88
123
public static < T extends NativeType < T > >
89
- UnaryBlockOperator < T , T > affine ( final T type , final AffineGet transformFromSource , Interpolation interpolation )
124
+ Function < BlockSupplier < T >, UnaryBlockOperator < T , T > > affine ( final AffineGet transformFromSource , Interpolation interpolation , final ComputationType computationType )
90
125
{
91
- return affine ( type , transformFromSource , interpolation , ComputationType . AUTO );
126
+ return s -> createAffineOperator ( s . getType () , transformFromSource , interpolation , computationType , ClampType . CLAMP );
92
127
}
93
128
94
129
/**
@@ -116,7 +151,7 @@ UnaryBlockOperator< T, T > affine( final T type, final AffineGet transformFromSo
116
151
* @return {@code UnaryBlockOperator} to affine-transform blocks of type {@code T}
117
152
*/
118
153
public static < T extends NativeType < T > >
119
- UnaryBlockOperator < T , T > affine ( final T type , final AffineGet transformFromSource , Interpolation interpolation , final ComputationType computationType )
154
+ UnaryBlockOperator < T , T > createAffineOperator ( final T type , final AffineGet transformFromSource , Interpolation interpolation , final ComputationType computationType , final ClampType clampType )
120
155
{
121
156
final int n = transformFromSource .numDimensions ();
122
157
if ( n < 2 || n > 3 ) {
@@ -145,7 +180,7 @@ UnaryBlockOperator< T, T > affine( final T type, final AffineGet transformFromSo
145
180
final UnaryBlockOperator < ?, ? > op = processAsFloat
146
181
? _affine ( transformToSource , interpolation , new FloatType () )
147
182
: _affine ( transformToSource , interpolation , new DoubleType () );
148
- return op .adaptSourceType ( type , ClampType .NONE ).adaptTargetType ( type , ClampType . CLAMP );
183
+ return op .adaptSourceType ( type , ClampType .NONE ).adaptTargetType ( type , clampType );
149
184
}
150
185
else // if ( interpolation == Interpolation.NEARESTNEIGHBOR )
151
186
{
@@ -156,8 +191,6 @@ UnaryBlockOperator< T, T > affine( final T type, final AffineGet transformFromSo
156
191
private static < T extends NativeType < T > > UnaryBlockOperator < T , T > _affine ( final AffineGet transform , final Interpolation interpolation , final T type )
157
192
{
158
193
final int n = transform .numDimensions ();
159
- if ( n < 2 || n > 3 )
160
- throw new IllegalArgumentException ( "Only 2D and 3D affine transforms are supported currently" );
161
194
return new DefaultUnaryBlockOperator <>( type , type , n , n ,
162
195
n == 2
163
196
? new Affine2DProcessor <>( ( AffineTransform2D ) transform , interpolation , type .getNativeTypeFactory ().getPrimitiveType () )
0 commit comments