From a9212ec76125411c52d1dc7bc1bbc403cb808b6b Mon Sep 17 00:00:00 2001 From: tpietzsch Date: Fri, 6 Sep 2024 23:59:46 +0200 Subject: [PATCH] Add convenience default method PrimitveBlocks.copy(Interval, Object) --- .../net/imglib2/blocks/PrimitiveBlocks.java | 23 +++++++++++++++++++ 1 file changed, 23 insertions(+) diff --git a/src/main/java/net/imglib2/blocks/PrimitiveBlocks.java b/src/main/java/net/imglib2/blocks/PrimitiveBlocks.java index 927287d79..3e3eab291 100644 --- a/src/main/java/net/imglib2/blocks/PrimitiveBlocks.java +++ b/src/main/java/net/imglib2/blocks/PrimitiveBlocks.java @@ -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; @@ -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).