Skip to content

Commit

Permalink
improve mappedbuffer creation
Browse files Browse the repository at this point in the history
  • Loading branch information
carlosuc3m committed Nov 28, 2023
1 parent e8faabd commit 299445c
Showing 1 changed file with 5 additions and 34 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@
import io.bioimage.modelrunner.tensor.Tensor;
import net.imglib2.Cursor;
import net.imglib2.RandomAccessibleInterval;
import net.imglib2.img.Img;
import net.imglib2.type.NativeType;
import net.imglib2.type.Type;
import net.imglib2.type.numeric.RealType;
Expand All @@ -38,7 +37,7 @@
import net.imglib2.type.numeric.real.DoubleType;
import net.imglib2.type.numeric.real.FloatType;
import net.imglib2.util.Util;
import net.imglib2.view.IntervalView;
import net.imglib2.view.Views;

/**
* Class that maps {@link Tensor} objects to {@link ByteBuffer} objects.
Expand Down Expand Up @@ -121,14 +120,7 @@ private static <T extends Type<T>> void build(RandomAccessibleInterval<T> rai, B
*/
private static void buildByte(RandomAccessibleInterval<ByteType> imgTensor, ByteBuffer byteBuffer)
{
Cursor<ByteType> tensorCursor;
if (imgTensor instanceof IntervalView)
tensorCursor = ((IntervalView<ByteType>) imgTensor).cursor();
else if (imgTensor instanceof Img)
tensorCursor = ((Img<ByteType>) imgTensor).cursor();
else
throw new IllegalArgumentException("The data of the " + Tensor.class + " has "
+ "to be an instance of " + Img.class + " or " + IntervalView.class);
Cursor<ByteType> tensorCursor = Views.flatIterable(imgTensor).cursor();
while (tensorCursor.hasNext()) {
tensorCursor.fwd();
byteBuffer.put(tensorCursor.get().getByte());
Expand All @@ -146,14 +138,7 @@ else if (imgTensor instanceof Img)
*/
private static void buildInt(RandomAccessibleInterval<IntType> imgTensor, ByteBuffer byteBuffer)
{
Cursor<IntType> tensorCursor;
if (imgTensor instanceof IntervalView)
tensorCursor = ((IntervalView<IntType>) imgTensor).cursor();
else if (imgTensor instanceof Img)
tensorCursor = ((Img<IntType>) imgTensor).cursor();
else
throw new IllegalArgumentException("The data of the " + Tensor.class + " has "
+ "to be an instance of " + Img.class + " or " + IntervalView.class);
Cursor<IntType> tensorCursor = Views.flatIterable(imgTensor).cursor();
while (tensorCursor.hasNext()) {
tensorCursor.fwd();
byteBuffer.putInt(tensorCursor.get().getInt());
Expand All @@ -171,14 +156,7 @@ else if (imgTensor instanceof Img)
*/
private static void buildFloat(RandomAccessibleInterval<FloatType> imgTensor, ByteBuffer byteBuffer)
{
Cursor<FloatType> tensorCursor;
if (imgTensor instanceof IntervalView)
tensorCursor = ((IntervalView<FloatType>) imgTensor).cursor();
else if (imgTensor instanceof Img)
tensorCursor = ((Img<FloatType>) imgTensor).cursor();
else
throw new IllegalArgumentException("The data of the " + Tensor.class + " has "
+ "to be an instance of " + Img.class + " or " + IntervalView.class);
Cursor<FloatType> tensorCursor = Views.flatIterable(imgTensor).cursor();
while (tensorCursor.hasNext()) {
tensorCursor.fwd();
byteBuffer.putFloat(tensorCursor.get().getRealFloat());
Expand All @@ -196,14 +174,7 @@ else if (imgTensor instanceof Img)
*/
private static void buildDouble(RandomAccessibleInterval<DoubleType> imgTensor, ByteBuffer byteBuffer)
{
Cursor<DoubleType> tensorCursor;
if (imgTensor instanceof IntervalView)
tensorCursor = ((IntervalView<DoubleType>) imgTensor).cursor();
else if (imgTensor instanceof Img)
tensorCursor = ((Img<DoubleType>) imgTensor).cursor();
else
throw new IllegalArgumentException("The data of the " + Tensor.class + " has "
+ "to be an instance of " + Img.class + " or " + IntervalView.class);
Cursor<DoubleType> tensorCursor = Views.flatIterable(imgTensor).cursor();
while (tensorCursor.hasNext()) {
tensorCursor.fwd();
byteBuffer.putDouble(tensorCursor.get().getRealDouble());
Expand Down

0 comments on commit 299445c

Please sign in to comment.