Skip to content
Snippets Groups Projects
Commit 6cb99a78 authored by Tobias Pietzsch's avatar Tobias Pietzsch
Browse files

Remove Hdf5ImageLoader.getFloatImage(), use default implementation instead

parent 3f95955d
No related branches found
No related tags found
No related merge requests found
......@@ -38,8 +38,6 @@ import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.concurrent.Callable;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import bdv.AbstractViewerSetupImgLoader;
import bdv.ViewerImgLoader;
......@@ -63,17 +61,13 @@ import net.imglib2.Cursor;
import net.imglib2.Dimensions;
import net.imglib2.FinalDimensions;
import net.imglib2.FinalInterval;
import net.imglib2.IterableInterval;
import net.imglib2.RandomAccess;
import net.imglib2.RandomAccessibleInterval;
import net.imglib2.cache.queue.BlockingFetchQueues;
import net.imglib2.cache.queue.FetcherThreads;
import net.imglib2.cache.volatiles.CacheHints;
import net.imglib2.cache.volatiles.LoadingStrategy;
import net.imglib2.img.Img;
import net.imglib2.img.ImgFactory;
import net.imglib2.img.NativeImg;
import net.imglib2.img.array.ArrayImgFactory;
import net.imglib2.img.array.ArrayImgs;
import net.imglib2.img.basictypeaccess.array.ShortArray;
import net.imglib2.img.cell.Cell;
......@@ -83,7 +77,6 @@ import net.imglib2.img.cell.CellImgFactory;
import net.imglib2.realtransform.AffineTransform3D;
import net.imglib2.type.NativeType;
import net.imglib2.type.numeric.integer.UnsignedShortType;
import net.imglib2.type.numeric.real.FloatType;
import net.imglib2.type.volatiles.VolatileUnsignedShortType;
import net.imglib2.util.Intervals;
import net.imglib2.view.Views;
......@@ -363,27 +356,6 @@ public class Hdf5ImageLoader implements ViewerImgLoader, MultiResolutionImgLoade
}
}
/**
* normalize img to 0...1
*/
protected static void normalize( final IterableInterval< FloatType > img )
{
float currentMax = img.firstElement().get();
float currentMin = currentMax;
for ( final FloatType t : img )
{
final float f = t.get();
if ( f > currentMax )
currentMax = f;
else if ( f < currentMin )
currentMin = f;
}
final float scale = ( float ) ( 1.0 / ( currentMax - currentMin ) );
for ( final FloatType t : img )
t.set( ( t.get() - currentMin ) * scale );
}
@Override
public SetupImgLoader getSetupImgLoader( final int setupId )
{
......@@ -555,118 +527,6 @@ public class Hdf5ImageLoader implements ViewerImgLoader, MultiResolutionImgLoade
return Views.interval( new ConstantRandomAccessible<>( constant, 3 ), new FinalInterval( d ) );
}
@Override
public RandomAccessibleInterval< FloatType > getFloatImage( final int timepointId, final boolean normalize, final ImgLoaderHint... hints )
{
return getFloatImage( timepointId, 0, normalize, hints );
}
@Override
public RandomAccessibleInterval< FloatType > getFloatImage( final int timepointId, final int level, final boolean normalize, final ImgLoaderHint... hints )
{
final RandomAccessibleInterval< UnsignedShortType > ushortImg = getImage( timepointId, level, hints );
// copy unsigned short img to float img
// create float img
final FloatType f = new FloatType();
final ImgFactory< FloatType > imgFactory;
if ( Intervals.numElements( ushortImg ) <= Integer.MAX_VALUE )
{
imgFactory = new ArrayImgFactory<>( f );
}
else
{
final long[] dimsLong = new long[ ushortImg.numDimensions() ];
ushortImg.dimensions( dimsLong );
final int[] cellDimensions = computeCellDimensions(
dimsLong,
mipmapInfo.getSubdivisions()[ level ] );
imgFactory = new CellImgFactory<>( f, cellDimensions );
}
final Img< FloatType > floatImg = imgFactory.create( ushortImg );
// set up executor service
final int numProcessors = Runtime.getRuntime().availableProcessors();
final ExecutorService taskExecutor = Executors.newFixedThreadPool( numProcessors );
final ArrayList< Callable< Void > > tasks = new ArrayList<>();
// set up all tasks
final int numPortions = numProcessors * 2;
final long threadChunkSize = floatImg.size() / numPortions;
final long threadChunkMod = floatImg.size() % numPortions;
for ( int portionID = 0; portionID < numPortions; ++portionID )
{
// move to the starting position of the current thread
final long startPosition = portionID * threadChunkSize;
// the last thread may has to run longer if the number of pixels cannot be divided by the number of threads
final long loopSize = ( portionID == numPortions - 1 ) ? threadChunkSize + threadChunkMod : threadChunkSize;
if ( Views.iterable( ushortImg ).iterationOrder().equals( floatImg.iterationOrder() ) )
{
tasks.add( new Callable< Void >()
{
@Override
public Void call() throws Exception
{
final Cursor< UnsignedShortType > in = Views.iterable( ushortImg ).cursor();
final Cursor< FloatType > out = floatImg.cursor();
in.jumpFwd( startPosition );
out.jumpFwd( startPosition );
for ( long j = 0; j < loopSize; ++j )
out.next().set( in.next().getRealFloat() );
return null;
}
} );
}
else
{
tasks.add( new Callable< Void >()
{
@Override
public Void call() throws Exception
{
final Cursor< UnsignedShortType > in = Views.iterable( ushortImg ).localizingCursor();
final RandomAccess< FloatType > out = floatImg.randomAccess();
in.jumpFwd( startPosition );
for ( long j = 0; j < loopSize; ++j )
{
final UnsignedShortType vin = in.next();
out.setPosition( in );
out.get().set( vin.getRealFloat() );
}
return null;
}
} );
}
}
try
{
// invokeAll() returns when all tasks are complete
taskExecutor.invokeAll( tasks );
taskExecutor.shutdown();
}
catch ( final InterruptedException e )
{
return null;
}
if ( normalize )
// normalize the image to 0...1
normalize( floatImg );
return floatImg;
}
public MipmapInfo getMipmapInfo()
{
return mipmapInfo;
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment