Skip to content

Commit

Permalink
Use CellGrid.CellDimensionsAndSteps
Browse files Browse the repository at this point in the history
  • Loading branch information
tpietzsch committed May 7, 2024
1 parent 8b1882b commit e23e78a
Show file tree
Hide file tree
Showing 5 changed files with 28 additions and 33 deletions.
16 changes: 7 additions & 9 deletions src/main/java/net/imglib2/cache/img/DiskCellCache.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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
Expand All @@ -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
Expand Down
9 changes: 4 additions & 5 deletions src/main/java/net/imglib2/cache/img/EmptyCellCacheLoader.java
Original file line number Diff line number Diff line change
Expand Up @@ -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}
Expand Down Expand Up @@ -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(
Expand Down
10 changes: 5 additions & 5 deletions src/main/java/net/imglib2/cache/img/LoadedCellCacheLoader.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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;

/**
Expand Down Expand Up @@ -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(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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 );
}
}

0 comments on commit e23e78a

Please sign in to comment.