public AbstractNativeImg( final long[] dim, final Fraction entitiesPerPixel ) { super( dim ); this.entitiesPerPixel = entitiesPerPixel; this.numEntities = entitiesPerPixel.mulCeil( numPixels ); }
public AbstractNativeImg( final long[] dim, final Fraction entitiesPerPixel ) { super( dim ); this.entitiesPerPixel = entitiesPerPixel; this.numEntities = entitiesPerPixel.mulCeil( numPixels ); }
public static int numEntitiesRangeCheck( final long[] dimensions, final Fraction entitiesPerPixel ) { final long numEntities = entitiesPerPixel.mulCeil( AbstractImg.numElements( dimensions ) ); if ( numEntities > Integer.MAX_VALUE ) throw new RuntimeException( "Number of elements in ArrayImg too big, use for example CellImg instead: " + numEntities + " > " + Integer.MAX_VALUE ); return ( int ) numEntities; }
public static int numEntitiesRangeCheck( final long[] dimensions, final Fraction entitiesPerPixel ) { final long numEntities = entitiesPerPixel.mulCeil( AbstractImg.numElements( dimensions ) ); if ( numEntities > Integer.MAX_VALUE ) throw new RuntimeException( "Number of elements in ArrayImg too big, use for example CellImg instead: " + numEntities + " > " + Integer.MAX_VALUE ); return ( int ) numEntities; }
private static < A extends ArrayDataAccess< A > > List< A > createSlices( A creator, long[] dim, Fraction entitiesPerPixel ) { int numSlices = numberOfSlices( dim ); List< A > mirror = new ArrayList<>( numSlices ); final int pixelsPerPlane = (int) (( ( dim.length > 1 ) ? dim[ 1 ] : 1 ) * dim[ 0 ]); final int numEntitiesPerSlice = ( int ) entitiesPerPixel.mulCeil( pixelsPerPlane ); for ( int i = 0; i < numSlices; ++i ) mirror.add( creator.createArray( numEntitiesPerSlice ) ); return mirror; } }
private static < A extends ArrayDataAccess< A > > List< A > createSlices( A creator, long[] dim, Fraction entitiesPerPixel ) { int numSlices = numberOfSlices( dim ); List< A > mirror = new ArrayList<>( numSlices ); final int pixelsPerPlane = (int) (( ( dim.length > 1 ) ? dim[ 1 ] : 1 ) * dim[ 0 ]); final int numEntitiesPerSlice = ( int ) entitiesPerPixel.mulCeil( pixelsPerPlane ); for ( int i = 0; i < numSlices; ++i ) mirror.add( creator.createArray( numEntitiesPerSlice ) ); return mirror; } }
/** * Computes cell size array by truncating or expanding * {@code defaultCellDimensions} to length {@code n}. Then verifies that a * cell does not contain more than {@code Integer.MAX_VALUE} entities. * * @param defaultCellDimensions * @param n * @param entitiesPerPixel * @return * @throws IllegalArgumentException */ public static int[] getCellDimensions( final int[] defaultCellDimensions, final int n, final Fraction entitiesPerPixel ) throws IllegalArgumentException { final int[] cellDimensions = new int[ n ]; final int max = defaultCellDimensions.length - 1; for ( int i = 0; i < n; i++ ) cellDimensions[ i ] = defaultCellDimensions[ ( i < max ) ? i : max ]; final long numEntities = entitiesPerPixel.mulCeil( Intervals.numElements( cellDimensions ) ); if ( numEntities > Integer.MAX_VALUE ) throw new IllegalArgumentException( "Number of entities in cell too large. Use smaller cell size." ); return cellDimensions; }
/** * Computes cell size array by truncating or expanding * {@code defaultCellDimensions} to length {@code n}. Then verifies that a * cell does not contain more than {@code Integer.MAX_VALUE} entities. * * @param defaultCellDimensions * @param n * @param entitiesPerPixel * @return * @throws IllegalArgumentException */ public static int[] getCellDimensions( final int[] defaultCellDimensions, final int n, final Fraction entitiesPerPixel ) throws IllegalArgumentException { final int[] cellDimensions = new int[ n ]; final int max = defaultCellDimensions.length - 1; for ( int i = 0; i < n; i++ ) cellDimensions[ i ] = defaultCellDimensions[ ( i < max ) ? i : max ]; final long numEntities = entitiesPerPixel.mulCeil( Intervals.numElements( cellDimensions ) ); if ( numEntities > Integer.MAX_VALUE ) throw new IllegalArgumentException( "Number of entities in cell too large. Use smaller cell size." ); return cellDimensions; }
@Override public void onRemoval( final Long key, final Cell< A > value ) { final long index = key; final String filename = blockname( index ); final int[] cellDims = new int[ n ]; value.dimensions( cellDims ); final long blocksize = entitiesPerPixel.mulCeil( Intervals.numElements( cellDims ) ); final long bytesize = blocksize * accessIo.getBytesPerElement(); try ( final RandomAccessFile mmFile = new RandomAccessFile( filename, "rw" ); ) { final MappedByteBuffer out = mmFile.getChannel().map( MapMode.READ_WRITE, 0, bytesize ); accessIo.save( value.getData(), out, ( int ) blocksize ); } catch ( final IOException e ) { throw new RuntimeException( e ); } }
@Override public Cell< A > get( final Long key ) throws Exception { final long index = key; final String filename = blockname( index ); 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 long bytesize = numEntities * accessIo.getBytesPerElement(); 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 ); } } else { return backingLoader.get( key ); } }
@Override 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 ) ); }
@Override public Cell< A > createInvalid( 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 A data = creator.getEmptyArray( numEntities ); return new Cell<>( cellDims, cellMin, data ); }
@Override public Cell< CA > get( final Long key ) throws Exception { final long index = key; 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 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 ) ); }
@Override 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 A array = creator.createArray( ( int ) numEntities ); @SuppressWarnings( { "rawtypes", "unchecked" } ) final SingleCellArrayImg< T, ? > img = new SingleCellArrayImg( cellDims, cellMin, wrapper.wrap( array ), wrapper.wrapDirty( array ), type ); loader.load( img ); return new Cell<>( cellDims, cellMin, array ); }
private < A extends ArrayDataAccess< A > > CellImg< T, A > create( final long[] dimensions, final T type, final NativeTypeFactory< T, A > typeFactory ) { verifyDimensions( dimensions ); final int n = dimensions.length; final Fraction entitiesPerPixel = type.getEntitiesPerPixel(); final int[] cellDimensions = getCellDimensions( defaultCellDimensions, n, entitiesPerPixel ); final CellGrid grid = new CellGrid( dimensions, cellDimensions ); final long[] gridDimensions = new long[ grid.numDimensions() ]; grid.gridDimensions( gridDimensions ); final Cell< A > cellType = new Cell<>( new int[] { 1 }, new long[] { 1 }, null ); final ListImg< Cell< A > > cells = new ListImg<>( gridDimensions, cellType ); final long[] cellGridPosition = new long[ n ]; final long[] cellMin = new long[ n ]; final int[] cellDims = new int[ n ]; final ListLocalizingCursor< Cell< A > > cellCursor = cells.localizingCursor(); while ( cellCursor.hasNext() ) { cellCursor.fwd(); cellCursor.localize( cellGridPosition ); grid.getCellDimensions( cellGridPosition, cellMin, cellDims ); final A data = ArrayDataAccessFactory.get( typeFactory ).createArray( ( int ) entitiesPerPixel.mulCeil( Intervals.numElements( cellDims ) ) ); cellCursor.set( new Cell<>( cellDims, cellMin, data ) ); } final CellImg< T, A > img = new CellImg<>( this, grid, cells, entitiesPerPixel ); img.setLinkedType( typeFactory.createLinkedType( img ) ); return img; }
private < A extends ArrayDataAccess< A > > CellImg< T, A > create( final long[] dimensions, final T type, final NativeTypeFactory< T, A > typeFactory ) { verifyDimensions( dimensions ); final int n = dimensions.length; final Fraction entitiesPerPixel = type.getEntitiesPerPixel(); final int[] cellDimensions = getCellDimensions( defaultCellDimensions, n, entitiesPerPixel ); final CellGrid grid = new CellGrid( dimensions, cellDimensions ); final long[] gridDimensions = new long[ grid.numDimensions() ]; grid.gridDimensions( gridDimensions ); final Cell< A > cellType = new Cell<>( new int[] { 1 }, new long[] { 1 }, null ); final ListImg< Cell< A > > cells = new ListImg<>( gridDimensions, cellType ); final long[] cellGridPosition = new long[ n ]; final long[] cellMin = new long[ n ]; final int[] cellDims = new int[ n ]; final ListLocalizingCursor< Cell< A > > cellCursor = cells.localizingCursor(); while ( cellCursor.hasNext() ) { cellCursor.fwd(); cellCursor.localize( cellGridPosition ); grid.getCellDimensions( cellGridPosition, cellMin, cellDims ); final A data = ArrayDataAccessFactory.get( typeFactory ).createArray( ( int ) entitiesPerPixel.mulCeil( Intervals.numElements( cellDims ) ) ); cellCursor.set( new Cell<>( cellDims, cellMin, data ) ); } final CellImg< T, A > img = new CellImg<>( this, grid, cells, entitiesPerPixel ); img.setLinkedType( typeFactory.createLinkedType( img ) ); return img; }