From e23e78ae55ae2e6e497339cf12a97620bd77f8cb Mon Sep 17 00:00:00 2001 From: tpietzsch Date: Thu, 1 Feb 2024 14:13:44 +0100 Subject: [PATCH] Use CellGrid.CellDimensionsAndSteps --- .../net/imglib2/cache/img/DiskCellCache.java | 16 +++++++--------- .../imglib2/cache/img/EmptyCellCacheLoader.java | 9 ++++----- .../imglib2/cache/img/LoadedCellCacheLoader.java | 10 +++++----- .../cache/img/RandomAccessibleCacheLoader.java | 14 ++++++-------- .../util/IntervalKeyLoaderAsLongKeyLoader.java | 12 ++++++------ 5 files changed, 28 insertions(+), 33 deletions(-) diff --git a/src/main/java/net/imglib2/cache/img/DiskCellCache.java b/src/main/java/net/imglib2/cache/img/DiskCellCache.java index fc7bef5..464d252 100644 --- a/src/main/java/net/imglib2/cache/img/DiskCellCache.java +++ b/src/main/java/net/imglib2/cache/img/DiskCellCache.java @@ -55,6 +55,7 @@ import net.imglib2.cache.IoSync; import net.imglib2.img.cell.Cell; import net.imglib2.img.cell.CellGrid; +import net.imglib2.img.cell.CellGrid.CellDimensionsAndSteps; import net.imglib2.util.Fraction; import net.imglib2.util.Intervals; @@ -125,16 +126,14 @@ public Cell< A > get( final Long key ) throws Exception if ( new File( filename ).exists() ) { final long[] cellMin = new long[ n ]; - final int[] cellDims = new int[ n ]; - grid.getCellDimensions( index, cellMin, cellDims ); - final long numEntities = entitiesPerPixel.mulCeil( Intervals.numElements( cellDims ) ); + final CellDimensionsAndSteps dimsAndSteps = grid.getCellDimensions( index, cellMin ); + final long numEntities = entitiesPerPixel.mulCeil( dimsAndSteps.numPixels() ); final long bytesize = numEntities * accessIo.getBytesPerElement(); - try ( - final RandomAccessFile mmFile = new RandomAccessFile( filename, "r" ); ) + try ( final RandomAccessFile mmFile = new RandomAccessFile( filename, "r" ) ) { final MappedByteBuffer in = mmFile.getChannel().map( MapMode.READ_ONLY, 0, bytesize ); final A access = accessIo.load( in, ( int ) numEntities ); - return new Cell<>( cellDims, cellMin, access ); + return new Cell<>( dimsAndSteps, cellMin, access ); } } else @@ -154,9 +153,8 @@ public Cell< A > reconstruct( final Long key, final A valueData ) { final long index = key; final long[] cellMin = new long[ n ]; - final int[] cellDims = new int[ n ]; - grid.getCellDimensions( index, cellMin, cellDims ); - return new Cell<>( cellDims, cellMin, valueData ); + final CellDimensionsAndSteps dimsAndSteps = grid.getCellDimensions( index, cellMin ); + return new Cell<>( dimsAndSteps, cellMin, valueData ); } @Override diff --git a/src/main/java/net/imglib2/cache/img/EmptyCellCacheLoader.java b/src/main/java/net/imglib2/cache/img/EmptyCellCacheLoader.java index 06c6ed0..9a65fa2 100644 --- a/src/main/java/net/imglib2/cache/img/EmptyCellCacheLoader.java +++ b/src/main/java/net/imglib2/cache/img/EmptyCellCacheLoader.java @@ -41,10 +41,10 @@ import net.imglib2.img.basictypeaccess.array.ArrayDataAccess; import net.imglib2.img.cell.Cell; import net.imglib2.img.cell.CellGrid; +import net.imglib2.img.cell.CellGrid.CellDimensionsAndSteps; import net.imglib2.type.NativeType; import net.imglib2.type.PrimitiveType; import net.imglib2.util.Fraction; -import net.imglib2.util.Intervals; /** * A {@link CacheLoader} that produces empty cells of {@link ArrayDataAccess} @@ -84,10 +84,9 @@ public Cell< A > get( final Long key ) throws Exception { final long index = key; final long[] cellMin = new long[ grid.numDimensions() ]; - final int[] cellDims = new int[ grid.numDimensions() ]; - grid.getCellDimensions( index, cellMin, cellDims ); - final long numEntities = entitiesPerPixel.mulCeil( Intervals.numElements( cellDims ) ); - return new Cell<>( cellDims, cellMin, creator.createArray( ( int ) numEntities ) ); + final CellDimensionsAndSteps dimsAndSteps = grid.getCellDimensions( index, cellMin ); + final long numEntities = entitiesPerPixel.mulCeil( dimsAndSteps.numPixels() ); + return new Cell<>( dimsAndSteps, cellMin, creator.createArray( ( int ) numEntities ) ); } public static < T extends NativeType< T >, A extends ArrayDataAccess< A > > EmptyCellCacheLoader< A > get( diff --git a/src/main/java/net/imglib2/cache/img/LoadedCellCacheLoader.java b/src/main/java/net/imglib2/cache/img/LoadedCellCacheLoader.java index cc55df0..1090c18 100644 --- a/src/main/java/net/imglib2/cache/img/LoadedCellCacheLoader.java +++ b/src/main/java/net/imglib2/cache/img/LoadedCellCacheLoader.java @@ -58,6 +58,7 @@ import net.imglib2.img.basictypeaccess.array.ShortArray; import net.imglib2.img.cell.Cell; import net.imglib2.img.cell.CellGrid; +import net.imglib2.img.cell.CellGrid.CellDimensionsAndSteps; import net.imglib2.type.NativeType; import net.imglib2.type.PrimitiveType; import net.imglib2.util.Fraction; @@ -115,14 +116,13 @@ public Cell< A > get( final Long key ) throws Exception { final long index = key; final long[] cellMin = new long[ grid.numDimensions() ]; - final int[] cellDims = new int[ grid.numDimensions() ]; - grid.getCellDimensions( index, cellMin, cellDims ); - final long numEntities = entitiesPerPixel.mulCeil( Intervals.numElements( cellDims ) ); + final CellDimensionsAndSteps dimsAndSteps = grid.getCellDimensions( index, cellMin ); + final long numEntities = entitiesPerPixel.mulCeil( dimsAndSteps.numPixels() ); final A array = creator.createArray( ( int ) numEntities ); @SuppressWarnings( { "rawtypes", "unchecked" } ) - final SingleCellArrayImg< T, ? > img = new SingleCellArrayImg( cellDims, cellMin, wrapper.wrap( array ), wrapper.wrapDirty( array ), type ); + final SingleCellArrayImg< T, ? > img = new SingleCellArrayImg( dimsAndSteps.dimensions(), cellMin, wrapper.wrap( array ), wrapper.wrapDirty( array ), type ); loader.load( img ); - return new Cell<>( cellDims, cellMin, array ); + return new Cell<>( dimsAndSteps, cellMin, array ); } public static < T extends NativeType< T >, A extends ArrayDataAccess< A > > LoadedCellCacheLoader< T, A > get( diff --git a/src/main/java/net/imglib2/cache/img/RandomAccessibleCacheLoader.java b/src/main/java/net/imglib2/cache/img/RandomAccessibleCacheLoader.java index dd09ba0..bf1603c 100644 --- a/src/main/java/net/imglib2/cache/img/RandomAccessibleCacheLoader.java +++ b/src/main/java/net/imglib2/cache/img/RandomAccessibleCacheLoader.java @@ -36,6 +36,7 @@ import static net.imglib2.img.basictypeaccess.AccessFlags.DIRTY; import static net.imglib2.img.basictypeaccess.AccessFlags.VOLATILE; +import java.util.Arrays; import java.util.Set; import java.util.function.Function; @@ -78,11 +79,11 @@ import net.imglib2.img.basictypeaccess.volatiles.array.VolatileShortArray; import net.imglib2.img.cell.Cell; import net.imglib2.img.cell.CellGrid; +import net.imglib2.img.cell.CellGrid.CellDimensionsAndSteps; import net.imglib2.type.NativeType; import net.imglib2.type.PrimitiveType; import net.imglib2.type.NativeTypeFactory; import net.imglib2.util.Fraction; -import net.imglib2.util.Intervals; import net.imglib2.view.Views; /** @@ -151,22 +152,19 @@ public Cell< CA > get( final Long key ) throws Exception final int n = grid.numDimensions(); final long[] cellMin = new long[ n ]; - final int[] cellDims = new int[ n ]; final long[] cellMax = new long[ n ]; - - grid.getCellDimensions( index, cellMin, cellDims ); - final long numEntities = entitiesPerPixel.mulCeil( Intervals.numElements( cellDims ) ); + final CellDimensionsAndSteps dimsAndSteps = grid.getCellDimensions( index, cellMin ); + Arrays.setAll( cellMax, d -> cellMin[ d ] + dimsAndSteps.dimensions()[ d ] - 1 ); + final long numEntities = entitiesPerPixel.mulCeil( dimsAndSteps.numPixels() ); final A data = creator.createArray( ( int ) numEntities ); final T t = createType( data ); t.updateIndex( 0 ); - for ( int d = 0; d < n; ++d ) - cellMax[ d ] = cellMin[ d ] + cellDims[ d ] - 1; for ( final T s : Views.interval( source, cellMin, cellMax ) ) { t.set( s ); t.incIndex(); } - return new Cell<>( cellDims, cellMin, rewrap.apply( data ) ); + return new Cell<>( dimsAndSteps, cellMin, rewrap.apply( data ) ); } public static < T extends NativeType< T >, A extends ArrayDataAccess< A >, CA extends ArrayDataAccess< CA > > RandomAccessibleCacheLoader< T, A, CA > get( diff --git a/src/main/java/net/imglib2/cache/util/IntervalKeyLoaderAsLongKeyLoader.java b/src/main/java/net/imglib2/cache/util/IntervalKeyLoaderAsLongKeyLoader.java index 2da5bde..0125544 100644 --- a/src/main/java/net/imglib2/cache/util/IntervalKeyLoaderAsLongKeyLoader.java +++ b/src/main/java/net/imglib2/cache/util/IntervalKeyLoaderAsLongKeyLoader.java @@ -33,8 +33,8 @@ */ package net.imglib2.cache.util; +import java.util.Arrays; import java.util.function.Function; -import java.util.stream.IntStream; import net.imglib2.FinalInterval; import net.imglib2.Interval; @@ -79,10 +79,10 @@ public Cell< A > get( final Long key ) final int n = grid.numDimensions(); final long[] cellMin = new long[ n ]; - final int[] cellDims = new int[ n ]; - grid.getCellDimensions( index, cellMin, cellDims ); - final long[] cellMax = IntStream.range( 0, n ).mapToLong( d -> cellMin[ d ] + cellDims[ d ] - 1 ).toArray(); - final A result = intervalKeyLoader.apply( new FinalInterval( cellMin, cellMax ) ); - return new Cell<>( cellDims, cellMin, result ); + final long[] cellMax = new long[ n ]; + final CellGrid.CellDimensionsAndSteps dimsAndSteps = grid.getCellDimensions( index, cellMin ); + Arrays.setAll( cellMax, d -> cellMin[ d ] + dimsAndSteps.dimensions()[ d ] - 1 ); + final A result = intervalKeyLoader.apply( FinalInterval.wrap( cellMin, cellMax ) ); + return new Cell<>( dimsAndSteps, cellMin, result ); } }