From 9ff573dc07709134f22b019dba227f9fce226e69 Mon Sep 17 00:00:00 2001 From: Matthias Arzt Date: Tue, 28 Jan 2020 14:27:06 +0100 Subject: [PATCH] Use LoopBuilder to implement the multi-threaded copy --- src/main/java/net/imglib2/util/ImgUtil.java | 35 ++++--------------- .../java/net/imglib2/util/ImgUtilTest.java | 12 +++++++ 2 files changed, 19 insertions(+), 28 deletions(-) diff --git a/src/main/java/net/imglib2/util/ImgUtil.java b/src/main/java/net/imglib2/util/ImgUtil.java index 2a24c63538..1c02d9c8bf 100644 --- a/src/main/java/net/imglib2/util/ImgUtil.java +++ b/src/main/java/net/imglib2/util/ImgUtil.java @@ -35,8 +35,9 @@ package net.imglib2.util; import net.imglib2.Cursor; -import net.imglib2.RandomAccess; +import net.imglib2.RandomAccessibleInterval; import net.imglib2.img.Img; +import net.imglib2.loops.LoopBuilder; import net.imglib2.type.BooleanType; import net.imglib2.type.Type; import net.imglib2.type.numeric.IntegerType; @@ -312,34 +313,12 @@ public static < T extends BooleanType< T >> void copy( final Img< T > src, final } /** - * Copy one {@link Img} into another. - * If both have the same iteration order, the copy proceeds with two {@link Cursor}. - * If they differ in iteration order, then they are copied with a {@link RandomAccess} approach. - * - * @param src - * @param dest - * + * Copy one image into another, multi-threaded. */ - public static < T extends Type< T >> void copy( final Img< T > src, final Img< T > dest ) + public static < T extends Type< T >> void copy( final RandomAccessibleInterval< T > source, final RandomAccessibleInterval< T > destination ) { - if ( src.iterationOrder() == dest.iterationOrder() ) - { - final Cursor< T > c1 = src.cursor(), - c2 = dest.cursor(); - while ( c1.hasNext() ) - c2.next().set( c1.next() ); - } - else - { - final Cursor< T > c = src.cursor(); - final RandomAccess< T > r = dest.randomAccess(); - - while ( c.hasNext() ) - { - c.fwd(); - r.setPosition( c ); - r.get().set( c.get() ); - } - } + LoopBuilder.setImages(source, destination) + .multiThreaded() + .forEachPixel( (i,o) -> o.set(i) ); } } diff --git a/src/test/java/net/imglib2/util/ImgUtilTest.java b/src/test/java/net/imglib2/util/ImgUtilTest.java index d4cd89c68f..37a4024aec 100644 --- a/src/test/java/net/imglib2/util/ImgUtilTest.java +++ b/src/test/java/net/imglib2/util/ImgUtilTest.java @@ -42,8 +42,11 @@ import java.util.Arrays; import net.imglib2.RandomAccess; +import net.imglib2.RandomAccessibleInterval; import net.imglib2.img.Img; import net.imglib2.img.array.ArrayImgFactory; +import net.imglib2.img.array.ArrayImgs; +import net.imglib2.test.ImgLib2Assert; import net.imglib2.type.numeric.integer.IntType; import net.imglib2.type.numeric.integer.LongType; import net.imglib2.type.numeric.real.DoubleType; @@ -366,5 +369,14 @@ public void testCopyImgOfTIntArrayIntIntArray() assertArrayEquals( expected[ i ], output ); } } + + @Test + public void testCopyRAItoRAI() + { + RandomAccessibleInterval source = ArrayImgs.ints(new int[]{1, 2, 3, 4, 5, 6}, 2, 3); + RandomAccessibleInterval destination = ArrayImgs.ints(2, 3); + ImgUtil.copy( source, destination ); + ImgLib2Assert.assertImageEquals( source, destination ); + } }