Skip to content

Commit c87ccee

Browse files
committed
Add BlockProcessor.setTargetInterval(long[] pos, int[] size) with default implementation
1 parent aeb5385 commit c87ccee

10 files changed

+116
-38
lines changed

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

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,17 @@ CellLoader< T > cellLoader( final BlockSupplier< T > blocks )
8585
}
8686

8787

88+
// ======== INTERNAL ======================================================
89+
90+
91+
static int safeInt( final long value )
92+
{
93+
if ( value > Integer.MAX_VALUE )
94+
throw new IllegalArgumentException( "value too large" );
95+
return ( int ) value;
96+
}
97+
98+
8899
// ======== DEPRECATED ====================================================
89100

90101

@@ -96,7 +107,7 @@ CellLoader< T > cellLoader( final PrimitiveBlocks< S > blocks, final UnaryBlockO
96107
}
97108

98109
@Deprecated
99-
public static < S extends NativeType< S >, T extends NativeType< T >, I, O >
110+
public static < S extends NativeType< S >, T extends NativeType< T > >
100111
CachedCellImg< T, ? > cellImg(
101112
final PrimitiveBlocks< S > blocks,
102113
final UnaryBlockOperator< S, T > operator,

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

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

36+
import java.util.Arrays;
37+
38+
import net.imglib2.FinalInterval;
3639
import net.imglib2.Interval;
3740
import net.imglib2.blocks.PrimitiveBlocks;
3841

@@ -56,6 +59,13 @@ public interface BlockProcessor< I, O >
5659

5760
void setTargetInterval( Interval interval );
5861

62+
default void setTargetInterval( long[] srcPos, int[] size )
63+
{
64+
final long[] srcMax = new long[ srcPos.length ];
65+
Arrays.setAll( srcMax, d -> srcPos[ d ] + size[ d ] - 1 );
66+
setTargetInterval( FinalInterval.wrap( srcPos, srcMax ) );
67+
}
68+
5969
long[] getSourcePos();
6070

6171
int[] getSourceSize();

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

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package net.imglib2.algorithm.blocks;
22

3+
import static net.imglib2.algorithm.blocks.BlockAlgoUtils.safeInt;
34
import static net.imglib2.blocks.PrimitiveBlocks.OnFallback.WARN;
45

56
import java.util.Arrays;
@@ -61,7 +62,7 @@ default void copy( Interval interval, Object dest )
6162
{
6263
final long[] srcPos = interval.minAsLongArray();
6364
final int[] size = new int[ srcPos.length ];
64-
Arrays.setAll( size, d -> ( int ) interval.dimension( d ) );
65+
Arrays.setAll( size, d -> safeInt( interval.dimension( d ) ) );
6566
copy( srcPos, dest, size );
6667
}
6768

@@ -83,7 +84,7 @@ default void copy( Interval interval, Object dest )
8384
*/
8485
default < U extends NativeType< U > > BlockSupplier< U > andThen( UnaryBlockOperator< T, U > operator )
8586
{
86-
return new ConcatenatedBlockSupplier< U >( this.independentCopy(), operator.independentCopy() );
87+
return new ConcatenatedBlockSupplier<>( this.independentCopy(), operator.independentCopy() );
8788
}
8889

8990
/**
@@ -132,7 +133,7 @@ static < T extends NativeType< T > > BlockSupplier< T > of(
132133
* @return a {@code BlockSupplier} accessor for {@code ra}.
133134
* @param <T> pixel type
134135
*/
135-
static < T extends NativeType< T >, R extends NativeType< R > > BlockSupplier< T > of(
136+
static < T extends NativeType< T > > BlockSupplier< T > of(
136137
RandomAccessible< T > ra,
137138
PrimitiveBlocks.OnFallback onFallback )
138139
{

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

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,6 @@
3333
*/
3434
package net.imglib2.algorithm.blocks;
3535

36-
import java.util.function.Supplier;
3736
import net.imglib2.Interval;
3837

3938
class ConcatenatedBlockProcessor< I, K, O > implements BlockProcessor< I, O >
@@ -42,8 +41,6 @@ class ConcatenatedBlockProcessor< I, K, O > implements BlockProcessor< I, O >
4241

4342
private final BlockProcessor< K, O > p1;
4443

45-
private Supplier< ConcatenatedBlockProcessor< I, K, O > > threadSafeSupplier;
46-
4744
public ConcatenatedBlockProcessor(
4845
BlockProcessor< I, K > p0,
4946
BlockProcessor< K, O > p1 )
@@ -65,6 +62,13 @@ public void setTargetInterval( final Interval interval )
6562
p0.setTargetInterval( p1.getSourceInterval() );
6663
}
6764

65+
@Override
66+
public void setTargetInterval( final long[] srcPos, final int[] size )
67+
{
68+
p1.setTargetInterval( srcPos, size );
69+
p0.setTargetInterval( p1.getSourcePos(), p1.getSourceSize() );
70+
}
71+
6872
@Override
6973
public long[] getSourcePos()
7074
{

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

Lines changed: 1 addition & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,7 @@
11
package net.imglib2.algorithm.blocks;
22

3-
import java.util.Arrays;
43
import java.util.function.Supplier;
54

6-
import net.imglib2.FinalInterval;
7-
import net.imglib2.Interval;
85
import net.imglib2.type.NativeType;
96
import net.imglib2.util.Cast;
107
import net.imglib2.util.CloseableThreadLocal;
@@ -44,20 +41,12 @@ public T getType()
4441
@Override
4542
public void copy( final long[] srcPos, final Object dest, final int[] size )
4643
{
47-
// p1.setTargetInterval( srcPos, size ); // TODO?
48-
p1.setTargetInterval( interval( srcPos, size ) );
44+
p1.setTargetInterval( srcPos, size );
4945
final Object src = p1.getSourceBuffer();
5046
p0.copy( p1.getSourcePos(), src, p1.getSourceSize() );
5147
p1.compute( Cast.unchecked( src ), Cast.unchecked( dest ) );
5248
}
5349

54-
private static Interval interval( long[] srcPos, int[] size )
55-
{
56-
final long[] srcMax = new long[ srcPos.length ];
57-
Arrays.setAll( srcMax, d -> srcPos[ d ] + size[ d ] - 1 );
58-
return FinalInterval.wrap( srcPos, srcMax );
59-
}
60-
6150
@Override
6251
public BlockSupplier< T > independentCopy()
6352
{

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

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,20 @@ public void setTargetInterval( final Interval interval )
108108
sourceLength = safeInt( Intervals.numElements( sourceSize ) );
109109
}
110110

111+
@Override
112+
public void setTargetInterval( final long[] pos, final int[] size )
113+
{
114+
final int n = pos.length;
115+
if ( sourcePos == null || sourcePos.length != n )
116+
{
117+
sourcePos = new long[ n ];
118+
sourceSize = new int[ n ];
119+
}
120+
System.arraycopy( pos, 0, sourcePos, 0, n );
121+
System.arraycopy( size, 0, sourceSize, 0, n );
122+
sourceLength = safeInt( Intervals.numElements( sourceSize ) );
123+
}
124+
111125
private static int safeInt( final long value )
112126
{
113127
if ( value > Integer.MAX_VALUE )

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

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@
4949
import net.imglib2.img.NativeImg;
5050
import net.imglib2.type.NativeType;
5151
import net.imglib2.type.NativeTypeFactory;
52+
import net.imglib2.util.Cast;
5253
import net.imglib2.util.Intervals;
5354

5455
/**
@@ -133,6 +134,20 @@ public void setTargetInterval( final Interval interval )
133134
sourceLength = safeInt( Intervals.numElements( sourceSize ) );
134135
}
135136

137+
@Override
138+
public void setTargetInterval( final long[] pos, final int[] size )
139+
{
140+
final int n = pos.length;
141+
if ( sourcePos == null || sourcePos.length != n )
142+
{
143+
sourcePos = new long[ n ];
144+
sourceSize = new int[ n ];
145+
}
146+
System.arraycopy( pos, 0, sourcePos, 0, n );
147+
System.arraycopy( size, 0, sourceSize, 0, n );
148+
sourceLength = safeInt( Intervals.numElements( sourceSize ) );
149+
}
150+
136151
private static int safeInt( final long value )
137152
{
138153
if ( value > Integer.MAX_VALUE )
@@ -201,8 +216,8 @@ private static class WrapperImpl< T extends NativeType< T >, A > extends Abstrac
201216
WrapperImpl( T type )
202217
{
203218
super( new long[ 0 ] );
204-
final NativeTypeFactory< T, A > nativeTypeFactory = ( NativeTypeFactory< T, A > ) type.getNativeTypeFactory();
205-
props = ( PrimitiveTypeProperties< ?, A > ) PrimitiveTypeProperties.get( nativeTypeFactory.getPrimitiveType() );
219+
final NativeTypeFactory< T, A > nativeTypeFactory = Cast.unchecked( type.getNativeTypeFactory() );
220+
props = Cast.unchecked( PrimitiveTypeProperties.get( nativeTypeFactory.getPrimitiveType() ) );
206221
wrapper = nativeTypeFactory.createLinkedType( this );
207222
}
208223

src/main/java/net/imglib2/algorithm/blocks/downsample/AbstractDownsample.java

Lines changed: 32 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ abstract class AbstractDownsample< T extends AbstractDownsample< T, P >, P > imp
5858
// sources for every per-dimension downsampling step.
5959
// dest is the tempArray of the next step, or final dest for the last step.
6060
// tempArrays[0] can be used to copy the source block into.
61-
private final TempArray< P > tempArrays[];
61+
private final TempArray< P >[] tempArrays;
6262
final int[] tempArraySizes;
6363

6464
private final BlockProcessorSourceInterval sourceInterval;
@@ -94,7 +94,7 @@ private static int[] downsampleDimIndices( final boolean[] downsampleInDim )
9494

9595
private static < P > TempArray< P >[] createTempArrays( final int steps, final PrimitiveType primitiveType )
9696
{
97-
final TempArray< P > tempArrays[] = new TempArray[ steps ];
97+
final TempArray< P >[] tempArrays = new TempArray[ steps ];
9898
tempArrays[ 0 ] = TempArray.forPrimitiveType( primitiveType );
9999
if ( steps >= 2 )
100100
{
@@ -148,16 +148,40 @@ public void setTargetInterval( final Interval interval )
148148
}
149149

150150
if ( destSizeChanged )
151+
recomputeTempArraySizes();
152+
}
153+
154+
protected void recomputeTempArraySizes()
155+
{
156+
int size = safeInt( Intervals.numElements( sourceSize ) );
157+
tempArraySizes[ 0 ] = size;
158+
for ( int i = 1; i < steps; ++i )
159+
{
160+
final int d = downsampleDims[ i - 1 ];
161+
size = size / sourceSize[ d ] * destSize[ d ];
162+
tempArraySizes[ i ] = size;
163+
}
164+
}
165+
166+
@Override
167+
public void setTargetInterval( final long[] pos, final int[] size )
168+
{
169+
boolean destSizeChanged = false;
170+
for ( int d = 0; d < n; ++d )
151171
{
152-
int size = safeInt( Intervals.numElements( sourceSize ) );
153-
tempArraySizes[ 0 ] = size;
154-
for ( int i = 1; i < steps; ++i )
172+
sourcePos[ d ] = downsampleInDim[ d ] ? pos[ d ] * 2 - 1 : pos[ d ];
173+
174+
final int tdim = safeInt( size[ d ] );
175+
if ( tdim != destSize[ d ] )
155176
{
156-
final int d = downsampleDims[ i - 1 ];
157-
size = size / sourceSize[ d ] * destSize[ d ];
158-
tempArraySizes[ i ] = size;
177+
destSize[ d ] = tdim;
178+
sourceSize[ d ] = downsampleInDim[ d ] ? tdim * 2 + 1 : tdim;
179+
destSizeChanged = true;
159180
}
160181
}
182+
183+
if ( destSizeChanged )
184+
recomputeTempArraySizes();
161185
}
162186

163187
static int safeInt( final long value )

src/main/java/net/imglib2/algorithm/blocks/downsample/AbstractDownsampleHalfPixel.java

Lines changed: 18 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,6 @@
3535

3636
import net.imglib2.Interval;
3737
import net.imglib2.type.PrimitiveType;
38-
import net.imglib2.util.Intervals;
3938

4039
abstract class AbstractDownsampleHalfPixel< T extends AbstractDownsampleHalfPixel< T, P >, P > extends AbstractDownsample< T, P >
4140
{
@@ -68,16 +67,27 @@ public void setTargetInterval( final Interval interval )
6867
}
6968

7069
if ( destSizeChanged )
70+
recomputeTempArraySizes();
71+
}
72+
73+
@Override
74+
public void setTargetInterval( final long[] pos, final int[] size )
75+
{
76+
boolean destSizeChanged = false;
77+
for ( int d = 0; d < n; ++d )
7178
{
72-
int size = safeInt( Intervals.numElements( sourceSize ) );
73-
tempArraySizes[ 0 ] = size;
74-
for ( int i = 1; i < steps; ++i )
79+
sourcePos[ d ] = downsampleInDim[ d ] ? pos[ d ] * 2 : pos[ d ];
80+
81+
final int tdim = safeInt( size[ d ] );
82+
if ( tdim != destSize[ d ] )
7583
{
76-
final int d = downsampleDims[ i - 1 ];
77-
size = size / sourceSize[ d ] * destSize[ d ];
78-
tempArraySizes[ i ] = size;
84+
destSize[ d ] = tdim;
85+
sourceSize[ d ] = downsampleInDim[ d ] ? tdim * 2 : tdim;
86+
destSizeChanged = true;
7987
}
8088
}
81-
}
8289

90+
if ( destSizeChanged )
91+
recomputeTempArraySizes();
92+
}
8393
}

src/main/java/net/imglib2/algorithm/blocks/transform/AbstractTransformProcessor.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,7 @@ abstract class AbstractTransformProcessor< T extends AbstractTransformProcessor<
116116
public void setTargetInterval( final Interval interval )
117117
{
118118
interval.min( destPos );
119-
Arrays.setAll( destSize, d -> ( int ) interval.dimension( d ) );
119+
Arrays.setAll( destSize, d -> safeInt( interval.dimension( d ) ) );
120120

121121
final RealInterval bounds = estimateBounds( interval );
122122
switch ( interpolation )

0 commit comments

Comments
 (0)