Skip to content

Commit

Permalink
Add convenience default method PrimitveBlocks.copy(Interval, Object)
Browse files Browse the repository at this point in the history
  • Loading branch information
tpietzsch committed Sep 6, 2024
1 parent 6b5e94e commit a9212ec
Showing 1 changed file with 23 additions and 0 deletions.
23 changes: 23 additions & 0 deletions src/main/java/net/imglib2/blocks/PrimitiveBlocks.java
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,12 @@

import static net.imglib2.blocks.PrimitiveBlocks.OnFallback.FAIL;
import static net.imglib2.blocks.PrimitiveBlocks.OnFallback.WARN;
import static net.imglib2.util.Util.safeInt;

import java.util.Arrays;

import net.imglib2.EuclideanSpace;
import net.imglib2.Interval;
import net.imglib2.RandomAccessible;
import net.imglib2.Typed;
import net.imglib2.type.NativeType;
Expand Down Expand Up @@ -138,6 +142,25 @@ default void copy( int[] srcPos, Object dest, int[] size )
copy( Util.int2long( srcPos ), dest, size );
}

/**
* Copy a block from the ({@code T}-typed) source into primitive arrays (of
* the appropriate type).
*
* @param interval
* the block to copy
* @param dest
* primitive array to copy into. Must correspond to {@code T}, for
* example, if {@code T} is {@code UnsignedByteType} then {@code dest} must
* be {@code byte[]}.
*/
default void copy( Interval interval, Object dest )
{
final long[] srcPos = interval.minAsLongArray();
final int[] size = new int[ srcPos.length ];
Arrays.setAll( size, d -> safeInt( interval.dimension( d ) ) );
copy( srcPos, dest, size );
}

/**
* Get a thread-safe version of this {@code PrimitiveBlocks}.
* (Implemented as a wrapper that makes {@link ThreadLocal} copies).
Expand Down

0 comments on commit a9212ec

Please sign in to comment.