From 928e14243e50f20f8508d343c35301903dda2133 Mon Sep 17 00:00:00 2001
From: Tobias Pietzsch <tobias.pietzsch@gmail.com>
Date: Mon, 3 Mar 2014 16:56:29 -0500
Subject: [PATCH] CatmaidImageLoader is ARGBType now

---
 .../bdv/img/catmaid/CatmaidImageLoader.java   | 71 ++++++-------
 .../CatmaidVolatileIntArrayLoader.java        | 99 +++++++++++++++++++
 2 files changed, 135 insertions(+), 35 deletions(-)
 create mode 100644 src/main/java/bdv/img/catmaid/CatmaidVolatileIntArrayLoader.java

diff --git a/src/main/java/bdv/img/catmaid/CatmaidImageLoader.java b/src/main/java/bdv/img/catmaid/CatmaidImageLoader.java
index dfbdca61..b64a15b3 100644
--- a/src/main/java/bdv/img/catmaid/CatmaidImageLoader.java
+++ b/src/main/java/bdv/img/catmaid/CatmaidImageLoader.java
@@ -5,12 +5,12 @@ 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.basictypeaccess.volatiles.array.VolatileIntArray;
 import net.imglib2.img.cell.CellImg;
 import net.imglib2.type.NativeType;
-import net.imglib2.type.numeric.integer.UnsignedShortType;
+import net.imglib2.type.numeric.ARGBType;
 import net.imglib2.type.numeric.real.FloatType;
-import net.imglib2.type.volatiles.VolatileUnsignedShortType;
+import net.imglib2.type.volatiles.VolatileARGBType;
 
 import org.jdom2.Element;
 
@@ -22,7 +22,7 @@ import bdv.img.cache.VolatileGlobalCellCache.LoadingStrategy;
 import bdv.img.cache.VolatileImgCells;
 import bdv.img.cache.VolatileImgCells.CellCache;
 
-public class CatmaidImageLoader implements ViewerImgLoader< UnsignedShortType, VolatileUnsignedShortType >
+public class CatmaidImageLoader implements ViewerImgLoader< ARGBType, VolatileARGBType >
 {
 	private long width;
 
@@ -48,27 +48,28 @@ public class CatmaidImageLoader implements ViewerImgLoader< UnsignedShortType, V
 
 	private int[][] blockDimensions;
 
-	protected VolatileGlobalCellCache< VolatileShortArray > cache;
+	protected VolatileGlobalCellCache< VolatileIntArray > cache;
 
 	@Override
 	public void init( final Element elem, final File basePath )
 	{
-		width = 1987;
-		height = 1441;
-		depth = 460;
+		width = Long.parseLong( elem.getChildText( "width" ) );
+		height = Long.parseLong( elem.getChildText( "height" ) );
+		depth = Long.parseLong( elem.getChildText( "depth" ) );
 
-		resXY = 5.6;
-		resZ = 11.2;
+		resXY = Double.parseDouble( elem.getChildText( "resXY" ) );
+		resZ = Double.parseDouble( elem.getChildText( "resZ" ) );
 
-//		baseUrl = "file:/Users/pietzsch/Desktop/data/catmaid/xy/";
-		baseUrl = "http://fly.mpi-cbg.de/map/fib/aligned/xy/";
+		baseUrl = elem.getChildText( "baseUrl" );
 
-		tileWidth = 256;
-		tileHeight = 256;
+		tileWidth = Integer.parseInt( elem.getChildText( "tileWidth" ) );
+		tileHeight = Integer.parseInt( elem.getChildText( "tileHeight" ) );
 
-
-		numScales = getNumScales( width, height, tileWidth, tileHeight );
-		System.out.println( "numScales = " + numScales );
+		final String numScalesString = elem.getChildText( "numScales" );
+		if ( numScalesString == null )
+			numScales = getNumScales( width, height, tileWidth, tileHeight );
+		else
+			numScales = Integer.parseInt( numScalesString );
 
 		mipmapResolutions = new double[ numScales ][];
 		imageDimensions = new long[ numScales ][];
@@ -82,8 +83,8 @@ public class CatmaidImageLoader implements ViewerImgLoader< UnsignedShortType, V
 		}
 
 		final int[] maxLevels = new int[] { numScales - 1 };
-		cache = new VolatileGlobalCellCache< VolatileShortArray >(
-				new CatmaidVolatileShortArrayLoader( baseUrl, tileWidth, tileHeight ), 1, 1, numScales, maxLevels, 10 );
+		cache = new VolatileGlobalCellCache< VolatileIntArray >(
+				new CatmaidVolatileIntArrayLoader( baseUrl, tileWidth, tileHeight ), 1, 1, numScales, maxLevels, 10 );
 	}
 
 	final static public int getNumScales( long width, long height, final long tileWidth, final long tileHeight )
@@ -109,25 +110,25 @@ public class CatmaidImageLoader implements ViewerImgLoader< UnsignedShortType, V
 	}
 
 	@Override
-	public RandomAccessibleInterval< UnsignedShortType > getImage( final View view )
+	public RandomAccessibleInterval< ARGBType > getImage( final View view )
 	{
 		return getImage( view, 0 );
 	}
 
 	@Override
-	public RandomAccessibleInterval< UnsignedShortType > getImage( final View view, final int level )
+	public RandomAccessibleInterval< ARGBType > getImage( final View view, final int level )
 	{
-		final CellImg< UnsignedShortType, VolatileShortArray, VolatileCell< VolatileShortArray > >  img = prepareCachedImage( view, level, LoadingStrategy.BLOCKING );
-		final UnsignedShortType linkedType = new UnsignedShortType( img );
+		final CellImg< ARGBType, VolatileIntArray, VolatileCell< VolatileIntArray > >  img = prepareCachedImage( view, level, LoadingStrategy.BLOCKING );
+		final ARGBType linkedType = new ARGBType( img );
 		img.setLinkedType( linkedType );
 		return img;
 	}
 
 	@Override
-	public RandomAccessibleInterval< VolatileUnsignedShortType > getVolatileImage( final View view, final int level )
+	public RandomAccessibleInterval< VolatileARGBType > getVolatileImage( final View view, final int level )
 	{
-		final CellImg< VolatileUnsignedShortType, VolatileShortArray, VolatileCell< VolatileShortArray > >  img = prepareCachedImage( view, level, LoadingStrategy.VOLATILE );
-		final VolatileUnsignedShortType linkedType = new VolatileUnsignedShortType( img );
+		final CellImg< VolatileARGBType, VolatileIntArray, VolatileCell< VolatileIntArray > >  img = prepareCachedImage( view, level, LoadingStrategy.VOLATILE );
+		final VolatileARGBType linkedType = new VolatileARGBType( img );
 		img.setLinkedType( linkedType );
 		return img;
 	}
@@ -147,16 +148,16 @@ public class CatmaidImageLoader implements ViewerImgLoader< UnsignedShortType, V
 	/**
 	 * (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}.
+	 * The type should be either {@link ARGBType} and {@link VolatileARGBType}.
 	 */
-	protected < T extends NativeType< T > > CellImg< T, VolatileShortArray, VolatileCell< VolatileShortArray > > prepareCachedImage( final View view, final int level, final LoadingStrategy loadingStrategy )
+	protected < T extends NativeType< T > > CellImg< T, VolatileIntArray, VolatileCell< VolatileIntArray > > 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 );
+		final CellCache< VolatileIntArray > c = cache.new VolatileCellCache( view.getTimepointIndex(), view.getSetupIndex(), level, loadingStrategy );
+		final VolatileImgCells< VolatileIntArray > cells = new VolatileImgCells< VolatileIntArray >( c, 1, dimensions, cellDimensions );
+		final CellImg< T, VolatileIntArray, VolatileCell< VolatileIntArray > > img = new CellImg< T, VolatileIntArray, VolatileCell< VolatileIntArray > >( null, cells );
 		return img;
 	}
 
@@ -165,18 +166,18 @@ public class CatmaidImageLoader implements ViewerImgLoader< UnsignedShortType, V
 		return cache;
 	}
 
-	private final UnsignedShortType type = new UnsignedShortType();
+	private final ARGBType type = new ARGBType();
 
-	private final VolatileUnsignedShortType volatileType = new VolatileUnsignedShortType();
+	private final VolatileARGBType volatileType = new VolatileARGBType();
 
 	@Override
-	public UnsignedShortType getImageType()
+	public ARGBType getImageType()
 	{
 		return type;
 	}
 
 	@Override
-	public VolatileUnsignedShortType getVolatileImageType()
+	public VolatileARGBType getVolatileImageType()
 	{
 		return volatileType;
 	}
diff --git a/src/main/java/bdv/img/catmaid/CatmaidVolatileIntArrayLoader.java b/src/main/java/bdv/img/catmaid/CatmaidVolatileIntArrayLoader.java
new file mode 100644
index 00000000..ebfe9e00
--- /dev/null
+++ b/src/main/java/bdv/img/catmaid/CatmaidVolatileIntArrayLoader.java
@@ -0,0 +1,99 @@
+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.VolatileIntArray;
+import bdv.img.cache.CacheArrayLoader;
+
+public class CatmaidVolatileIntArrayLoader implements CacheArrayLoader< VolatileIntArray >
+{
+	private VolatileIntArray theEmptyArray;
+
+	private final String baseUrl;
+
+	private final int tileWidth;
+
+	private final int tileHeight;
+
+	public CatmaidVolatileIntArrayLoader( final String baseUrl, final int tileWidth, final int tileHeight )
+	{
+		theEmptyArray = new VolatileIntArray( 256 * 256, false );
+		this.baseUrl = baseUrl;
+		this.tileWidth = tileWidth;
+		this.tileHeight = tileHeight;
+	}
+
+	@Override
+	public int getBytesPerElement()
+	{
+		return 1;
+	}
+
+	@Override
+	public VolatileIntArray 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();
+		}
+		return new VolatileIntArray( data, true );
+	}
+
+	@Override
+	public VolatileIntArray 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 VolatileIntArray( numEntities, false );
+		return theEmptyArray;
+	}
+}
-- 
GitLab