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

CATMAID ViewerImgLoader

parent 40423592
No related branches found
No related tags found
No related merge requests found
......@@ -27,7 +27,7 @@ import org.jdom2.output.XMLOutputter;
import bdv.export.ProgressWriter;
import bdv.export.ProgressWriterConsole;
import bdv.img.hdf5.Hdf5ImageLoader;
import bdv.img.catmaid.CatmaidImageLoader;
import bdv.tools.HelpDialog;
import bdv.tools.InitializeViewerState;
import bdv.tools.RecordMovieDialog;
......@@ -109,7 +109,7 @@ public class BigDataViewer
}
viewerFrame = new ViewerFrame( width, height, sources, seq.numTimepoints(),
( ( Hdf5ImageLoader ) seq.imgLoader ).getCache() );
( ( CatmaidImageLoader ) seq.imgLoader ).getCache() );
viewer = viewerFrame.getViewerPanel();
for ( final ConverterSetup cs : converterSetups )
......@@ -313,7 +313,8 @@ public class BigDataViewer
public static void main( final String[] args )
{
final String fn = "/Users/pietzsch/desktop/data/BDV130418A325/BDV130418A325_NoTempReg.xml";
final String fn = "/Users/pietzsch/desktop/data/catmaid.xml";
// final String fn = "/Users/pietzsch/desktop/data/BDV130418A325/BDV130418A325_NoTempReg.xml";
// final String fn = "/Users/pietzsch/Desktop/data/valia2/valia.xml";
// final String fn = "/Users/pietzsch/workspace/data/fast fly/111010_weber/combined.xml";
// final String fn = "/Users/pietzsch/workspace/data/mette/mette.xml";
......
package bdv.img.catmaid;
import java.io.File;
import mpicbg.spim.data.View;
import net.imglib2.RandomAccessibleInterval;
import net.imglib2.img.NativeImg;
import net.imglib2.img.basictypeaccess.volatiles.array.VolatileShortArray;
import net.imglib2.img.cell.CellImg;
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 org.jdom2.Element;
import bdv.ViewerImgLoader;
import bdv.img.cache.Cache;
import bdv.img.cache.VolatileCell;
import bdv.img.cache.VolatileGlobalCellCache;
import bdv.img.cache.VolatileGlobalCellCache.LoadingStrategy;
import bdv.img.cache.VolatileImgCells;
import bdv.img.cache.VolatileImgCells.CellCache;
public class CatmaidImageLoader implements ViewerImgLoader
{
private long width;
private long height;
private long depth;
private double resXY;
private double resZ;
private String baseUrl;
private int tileWidth;
private int tileHeight;
private int numScales;
private double[][] mipmapResolutions;
private long[][] imageDimensions;
private int[][] blockDimensions;
protected VolatileGlobalCellCache< VolatileShortArray > cache;
@Override
public void init( final Element elem, final File basePath )
{
width = 1987;
height = 1441;
depth = 460;
resXY = 5.6;
resZ = 11.2;
// baseUrl = "file:/Users/pietzsch/Desktop/data/catmaid/xy/";
baseUrl = "http://fly.mpi-cbg.de/map/fib/aligned/xy/";
tileWidth = 256;
tileHeight = 256;
numScales = getNumScales( width, height, tileWidth, tileHeight );
System.out.println( "numScales = " + numScales );
mipmapResolutions = new double[ numScales ][];
imageDimensions = new long[ numScales ][];
blockDimensions = new int[ numScales ][];
for ( int l = 0; l < numScales; ++l )
{
mipmapResolutions[ l ] = new double[] { 1 << l, 1 << l, 1 };
imageDimensions[ l ] = new long[] { width >> l, height >> l, depth };
blockDimensions[ l ] = new int[] { tileWidth, tileHeight, 1 };
}
final int[] maxLevels = new int[] { numScales - 1 };
cache = new VolatileGlobalCellCache< VolatileShortArray >(
new CatmaidVolatileShortArrayLoader( baseUrl, tileWidth, tileHeight ), 1, 1, numScales, maxLevels, 10 );
}
final static public int getNumScales( long width, long height, final long tileWidth, final long tileHeight )
{
int i = 1;
while ( ( width >>= 1 ) > tileWidth && ( height >>= 1 ) > tileHeight )
++i;
return i;
}
@Override
public Element toXml( final File basePath )
{
throw new UnsupportedOperationException( "not implemented" );
}
@Override
public RandomAccessibleInterval< FloatType > getImage( final View view )
{
throw new UnsupportedOperationException( "not implemented" );
}
@Override
public RandomAccessibleInterval< UnsignedShortType > getUnsignedShortImage( final View view )
{
return getUnsignedShortImage( view, 0 );
}
@Override
public RandomAccessibleInterval< UnsignedShortType > getUnsignedShortImage( final View view, final int level )
{
final CellImg< UnsignedShortType, VolatileShortArray, VolatileCell< VolatileShortArray > > img = prepareCachedImage( view, level, LoadingStrategy.BLOCKING );
final UnsignedShortType linkedType = new UnsignedShortType( img );
img.setLinkedType( linkedType );
return img;
}
@Override
public RandomAccessibleInterval< VolatileUnsignedShortType > getVolatileUnsignedShortImage( final View view, final int level )
{
final CellImg< VolatileUnsignedShortType, VolatileShortArray, VolatileCell< VolatileShortArray > > img = prepareCachedImage( view, level, LoadingStrategy.VOLATILE );
final VolatileUnsignedShortType linkedType = new VolatileUnsignedShortType( img );
img.setLinkedType( linkedType );
return img;
}
@Override
public double[][] getMipmapResolutions( final int setup )
{
return mipmapResolutions;
}
@Override
public int numMipmapLevels( final int setup )
{
return numScales;
}
/**
* (Almost) create a {@link CellImg} backed by the cache.
* The created image needs a {@link NativeImg#setLinkedType(net.imglib2.type.Type) linked type} before it can be used.
* The type should be either {@link UnsignedShortType} and {@link VolatileUnsignedShortType}.
*/
protected < T extends NativeType< T > > CellImg< T, VolatileShortArray, VolatileCell< VolatileShortArray > > prepareCachedImage( final View view, final int level, final LoadingStrategy loadingStrategy )
{
final long[] dimensions = imageDimensions[ level ];
final int[] cellDimensions = blockDimensions[ level ];
final CellCache< VolatileShortArray > c = cache.new VolatileCellCache( view.getTimepointIndex(), view.getSetupIndex(), level, loadingStrategy );
final VolatileImgCells< VolatileShortArray > cells = new VolatileImgCells< VolatileShortArray >( c, 1, dimensions, cellDimensions );
final CellImg< T, VolatileShortArray, VolatileCell< VolatileShortArray > > img = new CellImg< T, VolatileShortArray, VolatileCell< VolatileShortArray > >( null, cells );
return img;
}
public Cache getCache()
{
return cache;
}
}
package bdv.img.catmaid;
import java.awt.image.BufferedImage;
import java.awt.image.PixelGrabber;
import java.io.IOException;
import java.net.URL;
import javax.imageio.ImageIO;
import net.imglib2.img.basictypeaccess.volatiles.array.VolatileShortArray;
import bdv.img.cache.CacheArrayLoader;
public class CatmaidVolatileShortArrayLoader implements CacheArrayLoader< VolatileShortArray >
{
private VolatileShortArray theEmptyArray;
private final String baseUrl;
private final int tileWidth;
private final int tileHeight;
public CatmaidVolatileShortArrayLoader( final String baseUrl, final int tileWidth, final int tileHeight )
{
theEmptyArray = new VolatileShortArray( 256 * 256, false );
this.baseUrl = baseUrl;
this.tileWidth = tileWidth;
this.tileHeight = tileHeight;
}
@Override
public int getBytesPerElement()
{
return 1;
}
@Override
public VolatileShortArray loadArray( final int timepoint, final int setup, final int level, final int[] dimensions, final long[] min ) throws InterruptedException
{
final int c = ( int ) min[ 0 ] / tileWidth;
final int r = ( int ) min[ 1 ] / tileHeight;
final int z = ( int ) min[ 2 ];
final int s = level;
final String urlString =
new
StringBuffer( baseUrl ).
append( z ).
append( "/" ).
append( r ).
append( "_" ).
append( c ).
append( "_" ).
append( s ).
append( ".jpg" ).
toString();
final int w = dimensions[ 0 ];
final int h = dimensions[ 1 ];
final int[] data = new int[ w * h ];
try
{
final URL url = new URL( urlString );
// final Image image = toolkit.createImage( url );
final BufferedImage jpg = ImageIO.read( url );
/* This gymnastic is necessary to get reproducible gray
* values, just opening a JPG or PNG, even when saved by
* ImageIO, and grabbing its pixels results in gray values
* with a non-matching gamma transfer function, I cannot tell
* why... */
final BufferedImage image = new BufferedImage( w, h, BufferedImage.TYPE_INT_RGB );
image.createGraphics().drawImage( jpg, 0, 0, null );
final PixelGrabber pg = new PixelGrabber( image, 0, 0, w, h, data, 0, w );
pg.grabPixels();
// System.out.println( "success loading r=" + entry.key.r + " c=" + entry.key.c + " url(" + urlString + ")" );
}
catch (final IOException e)
{
System.out.println( "failed loading r=" + r + " c=" + c + " url(" + urlString + ")" );
}
catch (final InterruptedException e)
{
e.printStackTrace();
}
final short[] sdata = new short[ data.length ];
for ( int i = 0; i < data.length; ++i )
sdata[ i ] = ( short ) ( data[ i ] & 0x000000ff );
return new VolatileShortArray( sdata, true );
}
@Override
public VolatileShortArray emptyArray( final int[] dimensions )
{
int numEntities = 1;
for ( int i = 0; i < dimensions.length; ++i )
numEntities *= dimensions[ i ];
if ( theEmptyArray.getCurrentStorageArray().length < numEntities )
theEmptyArray = new VolatileShortArray( numEntities, false );
return theEmptyArray;
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment