Skip to content

Commit

Permalink
Merge pull request #237 multithreaded copy
Browse files Browse the repository at this point in the history
Implement ImgUtil.copy from RandomAccessibleInterval to RandomAccessibleInterval
using multithreaded LoopBuilder.
  • Loading branch information
maarzt authored Jan 28, 2020
2 parents 6a5ec46 + 9ff573d commit 7e37c9b
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 28 deletions.
35 changes: 7 additions & 28 deletions src/main/java/net/imglib2/util/ImgUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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) );
}
}
12 changes: 12 additions & 0 deletions src/test/java/net/imglib2/util/ImgUtilTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -366,5 +369,14 @@ public void testCopyImgOfTIntArrayIntIntArray()
assertArrayEquals( expected[ i ], output );
}
}

@Test
public void testCopyRAItoRAI()
{
RandomAccessibleInterval<IntType> source = ArrayImgs.ints(new int[]{1, 2, 3, 4, 5, 6}, 2, 3);
RandomAccessibleInterval<IntType> destination = ArrayImgs.ints(2, 3);
ImgUtil.copy( source, destination );
ImgLib2Assert.assertImageEquals( source, destination );
}

}

0 comments on commit 7e37c9b

Please sign in to comment.