Skip to content

Commit

Permalink
Use PrimitiveBlocks for downsampling
Browse files Browse the repository at this point in the history
  • Loading branch information
tpietzsch committed Sep 25, 2024
1 parent 4516879 commit c6e0de2
Showing 1 changed file with 18 additions and 52 deletions.
70 changes: 18 additions & 52 deletions src/main/java/net/preibisch/mvrecon/process/n5api/N5ApiTools.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,16 @@
import mpicbg.spim.data.sequence.ViewDescription;
import mpicbg.spim.data.sequence.ViewId;
import mpicbg.spim.data.sequence.ViewSetup;
import net.imglib2.FinalInterval;
import mpicbg.spim.data.sequence.VoxelDimensions;
import net.imglib2.Dimensions;
import net.imglib2.RandomAccessibleInterval;
import net.imglib2.algorithm.blocks.BlockAlgoUtils;
import net.imglib2.algorithm.blocks.BlockSupplier;
import net.imglib2.algorithm.blocks.downsample.Downsample;
import net.imglib2.img.Img;
import net.imglib2.img.array.ArrayImgs;
import net.imglib2.type.NativeType;
import net.imglib2.type.numeric.RealType;
import net.imglib2.type.numeric.integer.IntType;
import net.imglib2.type.numeric.integer.UnsignedByteType;
import net.imglib2.type.numeric.integer.UnsignedShortType;
Expand All @@ -40,7 +45,6 @@
import net.imglib2.view.Views;
import net.preibisch.legacy.io.IOFunctions;
import net.preibisch.mvrecon.fiji.spimdata.SpimData2;
import net.preibisch.mvrecon.process.downsampling.lazy.LazyHalfPixelDownsample2x;
import net.preibisch.mvrecon.process.interestpointregistration.pairwise.constellation.grouping.Group;
import util.Grid;

Expand Down Expand Up @@ -385,7 +389,7 @@ public static MultiResolutionLevelInfo[] setupBdvDatasetsN5(
return mrInfo;
}

public static void writeDownsampledBlock(
public static < T extends NativeType< T > & RealType< T > > void writeDownsampledBlock(
final N5Writer n5,
final MultiResolutionLevelInfo mrInfo,
final MultiResolutionLevelInfo mrInfoPreviousScale,
Expand All @@ -400,59 +404,21 @@ public static void writeDownsampledBlock(
final DataType dataType = mrInfo.dataType;// n5.getAttribute( datasetPreviousScale, DatasetAttributes.DATA_TYPE_KEY, DataType.class );
final int[] blockSize = mrInfo.blockSize;// n5.getAttribute( datasetPreviousScale, DatasetAttributes.BLOCK_SIZE_KEY, int[].class );

if ( dataType == DataType.UINT16 )
{
RandomAccessibleInterval<UnsignedShortType> downsampled = N5Utils.open(n5, datasetPreviousScale);

for ( int d = 0; d < downsampled.numDimensions(); ++d )
if ( mrInfo.relativeDownsampling[ d ] > 1 )
downsampled = LazyHalfPixelDownsample2x.init(
downsampled,
new FinalInterval( downsampled ),
new UnsignedShortType(),
blockSize,
d);

final RandomAccessibleInterval<UnsignedShortType> sourceGridBlock = Views.offsetInterval(downsampled, gridBlock[0], gridBlock[1]);
N5Utils.saveNonEmptyBlock(sourceGridBlock, n5, dataset, gridBlock[2], new UnsignedShortType());
}
else if ( dataType == DataType.UINT8 )
{
RandomAccessibleInterval<UnsignedByteType> downsampled = N5Utils.open(n5, datasetPreviousScale);

for ( int d = 0; d < downsampled.numDimensions(); ++d )
if ( mrInfo.relativeDownsampling[ d ] > 1 )
downsampled = LazyHalfPixelDownsample2x.init(
downsampled,
new FinalInterval( downsampled ),
new UnsignedByteType(),
blockSize,
d);

final RandomAccessibleInterval<UnsignedByteType> sourceGridBlock = Views.offsetInterval(downsampled, gridBlock[0], gridBlock[1]);
N5Utils.saveNonEmptyBlock(sourceGridBlock, n5, dataset, gridBlock[2], new UnsignedByteType());
}
else if ( dataType == DataType.FLOAT32 )
{
RandomAccessibleInterval<FloatType> downsampled = N5Utils.open(n5, datasetPreviousScale);;

for ( int d = 0; d < downsampled.numDimensions(); ++d )
if ( mrInfo.relativeDownsampling[ d ] > 1 )
downsampled = LazyHalfPixelDownsample2x.init(
downsampled,
new FinalInterval( downsampled ),
new FloatType(),
blockSize,
d);

final RandomAccessibleInterval<FloatType> sourceGridBlock = Views.offsetInterval(downsampled, gridBlock[0], gridBlock[1]);
N5Utils.saveNonEmptyBlock(sourceGridBlock, n5, dataset, gridBlock[2], new FloatType());
}
else
if ( dataType != DataType.UINT16 && dataType != DataType.UINT8 && dataType != DataType.FLOAT32 )
{
n5.close();
throw new RuntimeException("Unsupported pixel type: " + dataType );
}

final RandomAccessibleInterval<T> previousScale = N5Utils.open(n5, datasetPreviousScale);
final T type = previousScale.getType().createVariable();

final BlockSupplier< T > blocks = BlockSupplier.of( previousScale ).andThen( Downsample.downsample( mrInfo.relativeDownsampling ) );
final long[] dimensions = n5.getAttribute( dataset, DatasetAttributes.DIMENSIONS_KEY, long[].class );
final RandomAccessibleInterval< T > downsampled = BlockAlgoUtils.cellImg( blocks, dimensions, new int[] { 64 } );

final RandomAccessibleInterval<T> sourceGridBlock = Views.offsetInterval(downsampled, gridBlock[0], gridBlock[1]);
N5Utils.saveNonEmptyBlock(sourceGridBlock, n5, dataset, gridBlock[2], type);
}

public static ArrayList<long[][]> assembleJobs( final MultiResolutionLevelInfo mrInfo )
Expand Down

0 comments on commit c6e0de2

Please sign in to comment.