package viewer.hdf5.img;

import static viewer.hdf5.Util.getCellsPath;
import static viewer.hdf5.Util.reorder;
import net.imglib2.display.nativevolatile.VolatileShortArray;
import ch.systemsx.cisd.base.mdarray.MDShortArray;
import ch.systemsx.cisd.hdf5.IHDF5Reader;

public class VolatileShortArrayLoader implements Hdf5ArrayLoader< VolatileShortArray >
{
	final IHDF5Reader hdf5Reader;

	public VolatileShortArrayLoader( final IHDF5Reader hdf5Reader )
	{
		this.hdf5Reader = hdf5Reader;
	}

	@Override
	public VolatileShortArray loadArray( final int timepoint, final int setup, final int level, final int[] dimensions, final long[] min )
	{
		synchronized( hdf5Reader )
		{
			final MDShortArray array = hdf5Reader.readShortMDArrayBlockWithOffset( getCellsPath( timepoint, setup, level ), reorder( dimensions ), reorder( min ) );
			return new VolatileShortArray( array.getAsFlatArray(), true );
		}
	}

	@Override
	public VolatileShortArray emptyArray( final int[] dimensions )
	{
		int numEntities = 1;
		for ( int i = 0; i < dimensions.length; ++i )
			numEntities *= dimensions[ i ];
		return new VolatileShortArray( numEntities, false );
	}

	@Override
	public int getBytesPerElement() {
		return 2;
	}
}

@@ -350,16 +350,24 @@
 ay %3E%0A%7B%0A%09
+private 
 final IH
@@ -389,16 +389,60 @@
 eader;%0A%0A
+%09private VolatileShortArray theEmptyArray;%0A%0A
 %09public 
@@ -533,16 +533,81 @@
 Reader;%0A
+%09%09theEmptyArray = new VolatileShortArray( 32 * 32 * 32, false );%0A
 %09%7D%0A%0A%09@Ov
@@ -1201,22 +1201,101 @@
  i %5D;%0A%09%09
-r
+if ( theEmptyArray.g
 et
+C
 ur
-n
+rentStorageArray().length %3C numEntities )%0A%09%09%09theEmptyArray =
  new Vol
@@ -1325,32 +1325,56 @@
 ities, false );%0A
+%09%09return theEmptyArray;%0A
 %09%7D%0A%0A%09@Override%0A%09

use a single VolatileShortArray for all empty cells