Skip to content

Commit

Permalink
Add BlockProcessor.setTargetInterval(long[] pos, int[] size) with def…
Browse files Browse the repository at this point in the history
…ault implementation
  • Loading branch information
tpietzsch committed May 7, 2024
1 parent aeb5385 commit c87ccee
Show file tree
Hide file tree
Showing 10 changed files with 116 additions and 38 deletions.
13 changes: 12 additions & 1 deletion src/main/java/net/imglib2/algorithm/blocks/BlockAlgoUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,17 @@ CellLoader< T > cellLoader( final BlockSupplier< T > blocks )
}


// ======== INTERNAL ======================================================


static int safeInt( final long value )
{
if ( value > Integer.MAX_VALUE )
throw new IllegalArgumentException( "value too large" );
return ( int ) value;
}


// ======== DEPRECATED ====================================================


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

@Deprecated
public static < S extends NativeType< S >, T extends NativeType< T >, I, O >
public static < S extends NativeType< S >, T extends NativeType< T > >
CachedCellImg< T, ? > cellImg(
final PrimitiveBlocks< S > blocks,
final UnaryBlockOperator< S, T > operator,
Expand Down
10 changes: 10 additions & 0 deletions src/main/java/net/imglib2/algorithm/blocks/BlockProcessor.java
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,9 @@
*/
package net.imglib2.algorithm.blocks;

import java.util.Arrays;

import net.imglib2.FinalInterval;
import net.imglib2.Interval;
import net.imglib2.blocks.PrimitiveBlocks;

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

void setTargetInterval( Interval interval );

default void setTargetInterval( long[] srcPos, int[] size )
{
final long[] srcMax = new long[ srcPos.length ];
Arrays.setAll( srcMax, d -> srcPos[ d ] + size[ d ] - 1 );
setTargetInterval( FinalInterval.wrap( srcPos, srcMax ) );
}

long[] getSourcePos();

int[] getSourceSize();
Expand Down
7 changes: 4 additions & 3 deletions src/main/java/net/imglib2/algorithm/blocks/BlockSupplier.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package net.imglib2.algorithm.blocks;

import static net.imglib2.algorithm.blocks.BlockAlgoUtils.safeInt;
import static net.imglib2.blocks.PrimitiveBlocks.OnFallback.WARN;

import java.util.Arrays;
Expand Down Expand Up @@ -61,7 +62,7 @@ default void copy( Interval interval, Object dest )
{
final long[] srcPos = interval.minAsLongArray();
final int[] size = new int[ srcPos.length ];
Arrays.setAll( size, d -> ( int ) interval.dimension( d ) );
Arrays.setAll( size, d -> safeInt( interval.dimension( d ) ) );
copy( srcPos, dest, size );
}

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

/**
Expand Down Expand Up @@ -132,7 +133,7 @@ static < T extends NativeType< T > > BlockSupplier< T > of(
* @return a {@code BlockSupplier} accessor for {@code ra}.
* @param <T> pixel type
*/
static < T extends NativeType< T >, R extends NativeType< R > > BlockSupplier< T > of(
static < T extends NativeType< T > > BlockSupplier< T > of(
RandomAccessible< T > ra,
PrimitiveBlocks.OnFallback onFallback )
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@
*/
package net.imglib2.algorithm.blocks;

import java.util.function.Supplier;
import net.imglib2.Interval;

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

private final BlockProcessor< K, O > p1;

private Supplier< ConcatenatedBlockProcessor< I, K, O > > threadSafeSupplier;

public ConcatenatedBlockProcessor(
BlockProcessor< I, K > p0,
BlockProcessor< K, O > p1 )
Expand All @@ -65,6 +62,13 @@ public void setTargetInterval( final Interval interval )
p0.setTargetInterval( p1.getSourceInterval() );
}

@Override
public void setTargetInterval( final long[] srcPos, final int[] size )
{
p1.setTargetInterval( srcPos, size );
p0.setTargetInterval( p1.getSourcePos(), p1.getSourceSize() );
}

@Override
public long[] getSourcePos()
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,7 @@
package net.imglib2.algorithm.blocks;

import java.util.Arrays;
import java.util.function.Supplier;

import net.imglib2.FinalInterval;
import net.imglib2.Interval;
import net.imglib2.type.NativeType;
import net.imglib2.util.Cast;
import net.imglib2.util.CloseableThreadLocal;
Expand Down Expand Up @@ -44,20 +41,12 @@ public T getType()
@Override
public void copy( final long[] srcPos, final Object dest, final int[] size )
{
// p1.setTargetInterval( srcPos, size ); // TODO?
p1.setTargetInterval( interval( srcPos, size ) );
p1.setTargetInterval( srcPos, size );
final Object src = p1.getSourceBuffer();
p0.copy( p1.getSourcePos(), src, p1.getSourceSize() );
p1.compute( Cast.unchecked( src ), Cast.unchecked( dest ) );
}

private static Interval interval( long[] srcPos, int[] size )
{
final long[] srcMax = new long[ srcPos.length ];
Arrays.setAll( srcMax, d -> srcPos[ d ] + size[ d ] - 1 );
return FinalInterval.wrap( srcPos, srcMax );
}

@Override
public BlockSupplier< T > independentCopy()
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,20 @@ public void setTargetInterval( final Interval interval )
sourceLength = safeInt( Intervals.numElements( sourceSize ) );
}

@Override
public void setTargetInterval( final long[] pos, final int[] size )
{
final int n = pos.length;
if ( sourcePos == null || sourcePos.length != n )
{
sourcePos = new long[ n ];
sourceSize = new int[ n ];
}
System.arraycopy( pos, 0, sourcePos, 0, n );
System.arraycopy( size, 0, sourceSize, 0, n );
sourceLength = safeInt( Intervals.numElements( sourceSize ) );
}

private static int safeInt( final long value )
{
if ( value > Integer.MAX_VALUE )
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@
import net.imglib2.img.NativeImg;
import net.imglib2.type.NativeType;
import net.imglib2.type.NativeTypeFactory;
import net.imglib2.util.Cast;
import net.imglib2.util.Intervals;

/**
Expand Down Expand Up @@ -133,6 +134,20 @@ public void setTargetInterval( final Interval interval )
sourceLength = safeInt( Intervals.numElements( sourceSize ) );
}

@Override
public void setTargetInterval( final long[] pos, final int[] size )
{
final int n = pos.length;
if ( sourcePos == null || sourcePos.length != n )
{
sourcePos = new long[ n ];
sourceSize = new int[ n ];
}
System.arraycopy( pos, 0, sourcePos, 0, n );
System.arraycopy( size, 0, sourceSize, 0, n );
sourceLength = safeInt( Intervals.numElements( sourceSize ) );
}

private static int safeInt( final long value )
{
if ( value > Integer.MAX_VALUE )
Expand Down Expand Up @@ -201,8 +216,8 @@ private static class WrapperImpl< T extends NativeType< T >, A > extends Abstrac
WrapperImpl( T type )
{
super( new long[ 0 ] );
final NativeTypeFactory< T, A > nativeTypeFactory = ( NativeTypeFactory< T, A > ) type.getNativeTypeFactory();
props = ( PrimitiveTypeProperties< ?, A > ) PrimitiveTypeProperties.get( nativeTypeFactory.getPrimitiveType() );
final NativeTypeFactory< T, A > nativeTypeFactory = Cast.unchecked( type.getNativeTypeFactory() );
props = Cast.unchecked( PrimitiveTypeProperties.get( nativeTypeFactory.getPrimitiveType() ) );
wrapper = nativeTypeFactory.createLinkedType( this );
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ abstract class AbstractDownsample< T extends AbstractDownsample< T, P >, P > imp
// sources for every per-dimension downsampling step.
// dest is the tempArray of the next step, or final dest for the last step.
// tempArrays[0] can be used to copy the source block into.
private final TempArray< P > tempArrays[];
private final TempArray< P >[] tempArrays;
final int[] tempArraySizes;

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

private static < P > TempArray< P >[] createTempArrays( final int steps, final PrimitiveType primitiveType )
{
final TempArray< P > tempArrays[] = new TempArray[ steps ];
final TempArray< P >[] tempArrays = new TempArray[ steps ];
tempArrays[ 0 ] = TempArray.forPrimitiveType( primitiveType );
if ( steps >= 2 )
{
Expand Down Expand Up @@ -148,16 +148,40 @@ public void setTargetInterval( final Interval interval )
}

if ( destSizeChanged )
recomputeTempArraySizes();
}

protected void recomputeTempArraySizes()
{
int size = safeInt( Intervals.numElements( sourceSize ) );
tempArraySizes[ 0 ] = size;
for ( int i = 1; i < steps; ++i )
{
final int d = downsampleDims[ i - 1 ];
size = size / sourceSize[ d ] * destSize[ d ];
tempArraySizes[ i ] = size;
}
}

@Override
public void setTargetInterval( final long[] pos, final int[] size )
{
boolean destSizeChanged = false;
for ( int d = 0; d < n; ++d )
{
int size = safeInt( Intervals.numElements( sourceSize ) );
tempArraySizes[ 0 ] = size;
for ( int i = 1; i < steps; ++i )
sourcePos[ d ] = downsampleInDim[ d ] ? pos[ d ] * 2 - 1 : pos[ d ];

final int tdim = safeInt( size[ d ] );
if ( tdim != destSize[ d ] )
{
final int d = downsampleDims[ i - 1 ];
size = size / sourceSize[ d ] * destSize[ d ];
tempArraySizes[ i ] = size;
destSize[ d ] = tdim;
sourceSize[ d ] = downsampleInDim[ d ] ? tdim * 2 + 1 : tdim;
destSizeChanged = true;
}
}

if ( destSizeChanged )
recomputeTempArraySizes();
}

static int safeInt( final long value )
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,6 @@

import net.imglib2.Interval;
import net.imglib2.type.PrimitiveType;
import net.imglib2.util.Intervals;

abstract class AbstractDownsampleHalfPixel< T extends AbstractDownsampleHalfPixel< T, P >, P > extends AbstractDownsample< T, P >
{
Expand Down Expand Up @@ -68,16 +67,27 @@ public void setTargetInterval( final Interval interval )
}

if ( destSizeChanged )
recomputeTempArraySizes();
}

@Override
public void setTargetInterval( final long[] pos, final int[] size )
{
boolean destSizeChanged = false;
for ( int d = 0; d < n; ++d )
{
int size = safeInt( Intervals.numElements( sourceSize ) );
tempArraySizes[ 0 ] = size;
for ( int i = 1; i < steps; ++i )
sourcePos[ d ] = downsampleInDim[ d ] ? pos[ d ] * 2 : pos[ d ];

final int tdim = safeInt( size[ d ] );
if ( tdim != destSize[ d ] )
{
final int d = downsampleDims[ i - 1 ];
size = size / sourceSize[ d ] * destSize[ d ];
tempArraySizes[ i ] = size;
destSize[ d ] = tdim;
sourceSize[ d ] = downsampleInDim[ d ] ? tdim * 2 : tdim;
destSizeChanged = true;
}
}
}

if ( destSizeChanged )
recomputeTempArraySizes();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ abstract class AbstractTransformProcessor< T extends AbstractTransformProcessor<
public void setTargetInterval( final Interval interval )
{
interval.min( destPos );
Arrays.setAll( destSize, d -> ( int ) interval.dimension( d ) );
Arrays.setAll( destSize, d -> safeInt( interval.dimension( d ) ) );

final RealInterval bounds = estimateBounds( interval );
switch ( interpolation )
Expand Down

0 comments on commit c87ccee

Please sign in to comment.