Skip to content

Commit

Permalink
Make copy(Interval, Object) the main PrimitiveBlocks::copy method
Browse files Browse the repository at this point in the history
Other copy() signatures have default implementations.
  • Loading branch information
tpietzsch committed Nov 20, 2024
1 parent bf2e7d8 commit 5993588
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 15 deletions.
8 changes: 3 additions & 5 deletions src/main/java/net/imglib2/blocks/FallbackPrimitiveBlocks.java
Original file line number Diff line number Diff line change
Expand Up @@ -33,15 +33,14 @@
*/
package net.imglib2.blocks;

import net.imglib2.FinalInterval;
import net.imglib2.Interval;
import net.imglib2.RandomAccessible;
import net.imglib2.img.array.ArrayImg;
import net.imglib2.img.basictypeaccess.array.ArrayDataAccess;
import net.imglib2.loops.LoopBuilder;
import net.imglib2.type.NativeType;
import net.imglib2.type.NativeTypeFactory;
import net.imglib2.util.Cast;
import net.imglib2.util.Util;
import net.imglib2.view.Views;

class FallbackPrimitiveBlocks< T extends NativeType< T >, A extends ArrayDataAccess< A > > implements PrimitiveBlocks< T >
Expand Down Expand Up @@ -84,11 +83,10 @@ public int numDimensions()
}

@Override
public void copy( final long[] srcPos, final Object dest, final int[] size )
public void copy( final Interval interval, final Object dest )
{
final ArrayImg< T, A > img = new ArrayImg<>( primitiveTypeProperties.wrap( dest ), Util.int2long( size ), type.getEntitiesPerPixel() );
final ArrayImg< T, A > img = new ArrayImg<>( primitiveTypeProperties.wrap( dest ), interval.dimensionsAsLongArray(), type.getEntitiesPerPixel() );
img.setLinkedType( nativeTypeFactory.createLinkedType( img ) );
final FinalInterval interval = FinalInterval.createMinSize( srcPos, Util.int2long( size ) );
LoopBuilder.setImages( Views.interval( source, interval ), img ).forEachPixel( ( a, b ) -> b.set( a ) );
}

Expand Down
19 changes: 18 additions & 1 deletion src/main/java/net/imglib2/blocks/PrimitiveBlocks.java
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
import static net.imglib2.blocks.PrimitiveBlocks.OnFallback.WARN;

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 @@ -105,6 +106,19 @@
*/
public interface PrimitiveBlocks< T extends NativeType< T > > extends Typed< T >, EuclideanSpace
{
/**
* Copy a block from the ({@code T}-typed) source into primitive arrays (of
* the appropriate type).
*
* @param interval
* position and size of 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[]}.
*/
void copy( Interval interval, Object dest );

/**
* Copy a block from the ({@code T}-typed) source into primitive arrays (of
* the appropriate type).
Expand All @@ -118,7 +132,10 @@ public interface PrimitiveBlocks< T extends NativeType< T > > extends Typed< T >
* @param size
* the size of the block to copy
*/
void copy( long[] srcPos, Object dest, int[] size );
default void copy( long[] srcPos, Object dest, int[] size )
{
copy( BlockInterval.wrap( srcPos, size ), dest );
}

/**
* Copy a block from the ({@code T}-typed) source into primitive arrays (of
Expand Down
25 changes: 16 additions & 9 deletions src/main/java/net/imglib2/blocks/ViewPrimitiveBlocks.java
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@

import java.util.function.Supplier;

import net.imglib2.Interval;
import net.imglib2.img.basictypeaccess.nio.BufferAccess;
import net.imglib2.transform.integer.MixedTransform;
import net.imglib2.type.NativeType;
Expand Down Expand Up @@ -94,16 +95,22 @@ public int numDimensions()
}

/**
* @param srcPos
* min coordinates of block to copy from src Img.
* Copy a block from the ({@code T}-typed) source into primitive arrays (of
* the appropriate type).
*
* @param interval
* position and size of the block to copy
* @param dest
* destination array. Type is {@code byte[]}, {@code float[]},
* etc, corresponding to the src Img's native type.
* @param size
* dimensions of block to copy from src Img.
* 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[]}.
*/
public void copy( final long[] srcPos, final Object dest, final int[] size )
public void copy( final Interval interval, final Object dest )
{
final BlockInterval blockInterval = BlockInterval.asBlockInterval( interval );
final long[] srcPos = blockInterval.min();
final int[] size = blockInterval.size();

final long[] destPos;
final int[] destSize;
if ( props.hasTransform() )
Expand Down Expand Up @@ -185,9 +192,9 @@ public int numDimensions()
}

@Override
public void copy( final long[] srcPos, final Object dest, final int[] size )
public void copy( final Interval interval, final Object dest )
{
threadSafeSupplier.get().copy( srcPos, dest, size );
threadSafeSupplier.get().copy( interval, dest );
}

@Override
Expand Down

0 comments on commit 5993588

Please sign in to comment.