Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 );
}

}