Skip to content
Snippets Groups Projects
Commit d51cc5a1 authored by Jean-Yves Tinevez's avatar Jean-Yves Tinevez
Browse files

LabelingSource: allow to specify on what mipmap level to operate

parent f931a1e3
No related branches found
No related tags found
No related merge requests found
...@@ -35,16 +35,17 @@ import viewer.render.Source; ...@@ -35,16 +35,17 @@ import viewer.render.Source;
* @author Stephan Preibisch * @author Stephan Preibisch
* *
*/ */
public class LabelingSource implements Source<ARGBType> { public class LabelingSource< T > implements Source< ARGBType >
{
private int currentTimepoint; private int currentTimepoint;
private NativeImgLabeling<Integer, IntType> currentSource; private NativeImgLabeling< Integer, IntType > currentSource;
private final Source<?> imgSource; private final Source< T > imgSource;
private final String name; private final String name;
private final LabelingTypeARGBConverter<Integer> converter; private final LabelingTypeARGBConverter< Integer > converter;
final protected static int numInterpolationMethods = 2; final protected static int numInterpolationMethods = 2;
...@@ -52,7 +53,7 @@ public class LabelingSource implements Source<ARGBType> { ...@@ -52,7 +53,7 @@ public class LabelingSource implements Source<ARGBType> {
final protected static int iNLinearMethod = 1; final protected static int iNLinearMethod = 1;
final protected InterpolatorFactory<ARGBType, RandomAccessible<ARGBType>>[] interpolatorFactories; final protected InterpolatorFactory< ARGBType, RandomAccessible< ARGBType >>[] interpolatorFactories;
private volatile int oldAlpha = -1; private volatile int oldAlpha = -1;
...@@ -62,94 +63,146 @@ public class LabelingSource implements Source<ARGBType> { ...@@ -62,94 +63,146 @@ public class LabelingSource implements Source<ARGBType> {
* The map that stores the labeling for each time point. Its values are all * The map that stores the labeling for each time point. Its values are all
* <code>null</code> until the target time-point is initialized. * <code>null</code> until the target time-point is initialized.
*/ */
private final Map<Integer, NativeImgLabeling<Integer, IntType>> labelings; private final Map< Integer, NativeImgLabeling< Integer, IntType >> labelings;
private final AffineTransform3D currentTranform = new AffineTransform3D(); private final AffineTransform3D currentTranform = new AffineTransform3D();
@SuppressWarnings("unchecked") private final int level;
public LabelingSource(final Source<?> imgSource) {
@SuppressWarnings( "unchecked" )
public LabelingSource( final Source< T > imgSource, final int level )
{
this.imgSource = imgSource; this.imgSource = imgSource;
this.level = level;
name = imgSource.getName() + " annotations"; name = imgSource.getName() + " annotations";
converter = new LabelingTypeARGBConverter<Integer>(new HashMap<List<Integer>, ARGBType>()); converter = new LabelingTypeARGBConverter< Integer >( new HashMap< List< Integer >, ARGBType >() );
interpolatorFactories = new InterpolatorFactory[numInterpolationMethods]; interpolatorFactories = new InterpolatorFactory[ numInterpolationMethods ];
interpolatorFactories[iNearestNeighborMethod] = new NearestNeighborInterpolatorFactory<ARGBType>(); interpolatorFactories[ iNearestNeighborMethod ] = new NearestNeighborInterpolatorFactory< ARGBType >();
interpolatorFactories[iNLinearMethod] = new NLinearInterpolatorFactory<ARGBType>(); interpolatorFactories[ iNLinearMethod ] = new NLinearInterpolatorFactory< ARGBType >();
labelings = new HashMap< Integer, NativeImgLabeling< Integer, IntType >>();
labelings = new HashMap<Integer, NativeImgLabeling<Integer, IntType>>(); loadTimepoint( 0 );
}
loadTimepoint(0); public LabelingSource( final Source< T > imgSource )
{
this( imgSource, 0 );
} }
@Override @Override
public boolean isPresent(final int t) { public boolean isPresent( final int t )
return imgSource.isPresent(t); {
return imgSource.isPresent( t );
} }
@Override @Override
public RandomAccessibleInterval<ARGBType> getSource(final int t, final int level) { public RandomAccessibleInterval< ARGBType > getSource( final int t, final int level )
if (t != currentTimepoint) {
loadTimepoint(t); if ( t != currentTimepoint )
if (currentAlpha != oldAlpha) {
loadTimepoint( t );
}
if ( currentAlpha != oldAlpha )
{
updateColorTable(); updateColorTable();
return Converters.convert((RandomAccessibleInterval<LabelingType<Integer>>) currentSource, converter, new ARGBType()); }
return Converters.convert( ( RandomAccessibleInterval< LabelingType< Integer >> ) currentSource, converter, new ARGBType() );
} }
@Override @Override
public RealRandomAccessible<ARGBType> getInterpolatedSource(final int t, final int level, final Interpolation method) { public RealRandomAccessible< ARGBType > getInterpolatedSource( final int t, final int level, final Interpolation method )
return Views.interpolate(Views.extendValue(getSource(t, level), new ARGBType()), interpolatorFactories[method == Interpolation.NLINEAR ? iNLinearMethod : iNearestNeighborMethod]); {
return Views.interpolate( Views.extendValue( getSource( t, level ), new ARGBType() ), interpolatorFactories[ method == Interpolation.NLINEAR ? iNLinearMethod : iNearestNeighborMethod ] );
} }
@Override @Override
public int getNumMipmapLevels() { public int getNumMipmapLevels()
{
return 1; return 1;
} }
private void loadTimepoint(final int timepoint) { /**
* Returns the mipmap level of the img source this labeling is built upon.
*
* @return the mipmap level.
*/
public int getLevel()
{
return level;
}
/**
* Exposes the {@link Source} this labeling source is built on.
*
* @return a {@link Source}.
*/
public Source< T > getImgSource()
{
return imgSource;
}
private void loadTimepoint( final int timepoint )
{
currentTimepoint = timepoint; currentTimepoint = timepoint;
if (isPresent(timepoint)) { if ( isPresent( timepoint ) )
{
currentTranform.set(imgSource.getSourceTransform(timepoint, 0)); currentTranform.set( imgSource.getSourceTransform( timepoint, level ) );
NativeImgLabeling<Integer, IntType> labeling = labelings.get(Integer.valueOf(timepoint)); NativeImgLabeling< Integer, IntType > labeling = labelings.get( Integer.valueOf( timepoint ) );
if (null == labeling) { if ( null == labeling )
final Dimensions sourceDimensions = imgSource.getSource(timepoint, 0); {
labeling = newLabeling(sourceDimensions); final Dimensions sourceDimensions = imgSource.getSource( timepoint, level );
labelings.put(Integer.valueOf(timepoint), labeling); labeling = newLabeling( sourceDimensions );
labelings.put( Integer.valueOf( timepoint ), labeling );
} }
currentSource = labeling; currentSource = labeling;
updateColorTable(); updateColorTable();
} else }
else
{
currentSource = null; currentSource = null;
}
} }
@Override @Override
public AffineTransform3D getSourceTransform(final int t, final int level) { public AffineTransform3D getSourceTransform( final int t, final int level )
if (currentTimepoint != t) { {
loadTimepoint(t); if ( currentTimepoint != t )
{
loadTimepoint( t );
} }
return currentTranform; return currentTranform;
} }
@Override @Override
public ARGBType getType() { public ARGBType getType()
{
return new ARGBType(); return new ARGBType();
} }
@Override @Override
public String getName() { public String getName()
{
return name; return name;
} }
public NativeImgLabeling<Integer, IntType> getLabeling(final int t) { public NativeImgLabeling< Integer, IntType > getLabeling( final int t )
NativeImgLabeling<Integer, IntType> target; {
if (isPresent(t)) { NativeImgLabeling< Integer, IntType > target;
target = labelings.get(Integer.valueOf(t)); if ( isPresent( t ) )
if (null == target) { {
final Dimensions sourceDimensions = imgSource.getSource(t, 0); target = labelings.get( Integer.valueOf( t ) );
target = newLabeling(sourceDimensions); if ( null == target )
labelings.put(Integer.valueOf(t), target); {
final Dimensions sourceDimensions = imgSource.getSource( t, level );
target = newLabeling( sourceDimensions );
labelings.put( Integer.valueOf( t ), target );
} }
} else { }
else
{
target = null; target = null;
} }
return target; return target;
...@@ -161,7 +214,8 @@ public class LabelingSource implements Source<ARGBType> { ...@@ -161,7 +214,8 @@ public class LabelingSource implements Source<ARGBType> {
* @param alpha * @param alpha
* an <code>int</code>, ranging from 0 to 255. * an <code>int</code>, ranging from 0 to 255.
*/ */
public void setAlpha(final int alpha) { public void setAlpha( final int alpha )
{
this.currentAlpha = alpha; this.currentAlpha = alpha;
} }
...@@ -170,32 +224,36 @@ public class LabelingSource implements Source<ARGBType> { ...@@ -170,32 +224,36 @@ public class LabelingSource implements Source<ARGBType> {
* *
* @return an <code>int</code>, ranging from 0 to 255. * @return an <code>int</code>, ranging from 0 to 255.
*/ */
public int getAlpha() { public int getAlpha()
{
return currentAlpha; return currentAlpha;
} }
public void updateColorTable() { public void updateColorTable()
{
final int a = currentAlpha; final int a = currentAlpha;
final HashMap<List<Integer>, ARGBType> colorTable = new HashMap<List<Integer>, ARGBType>(); final HashMap< List< Integer >, ARGBType > colorTable = new HashMap< List< Integer >, ARGBType >();
final LabelingMapping<Integer> mapping = currentSource.getMapping(); final LabelingMapping< Integer > mapping = currentSource.getMapping();
final int numLists = mapping.numLists(); final int numLists = mapping.numLists();
final Random random = new Random(1); final Random random = new Random( 1 );
for (int i = 0; i < numLists; ++i) { for ( int i = 0; i < numLists; ++i )
final List<Integer> list = mapping.listAtIndex(i); {
final int r = random.nextInt(256); final List< Integer > list = mapping.listAtIndex( i );
final int g = random.nextInt(256); final int r = random.nextInt( 256 );
final int b = random.nextInt(256); final int g = random.nextInt( 256 );
colorTable.put(list, new ARGBType(ARGBType.rgba(r, g, b, a))); final int b = random.nextInt( 256 );
colorTable.put( list, new ARGBType( ARGBType.rgba( r, g, b, a ) ) );
} }
colorTable.put(mapping.emptyList(), new ARGBType(0)); colorTable.put( mapping.emptyList(), new ARGBType( 0 ) );
converter.setColorTable(colorTable); converter.setColorTable( colorTable );
oldAlpha = a; oldAlpha = a;
} }
private static final NativeImgLabeling<Integer, IntType> newLabeling(final Dimensions dimensions) { private static final NativeImgLabeling< Integer, IntType > newLabeling( final Dimensions dimensions )
final NtreeImgFactory<IntType> factory = new NtreeImgFactory<IntType>(); {
final Img<IntType> img = factory.create(dimensions, new IntType()); final NtreeImgFactory< IntType > factory = new NtreeImgFactory< IntType >();
final NativeImgLabeling<Integer, IntType> labeling = new NativeImgLabeling<Integer, IntType>(img); final Img< IntType > img = factory.create( dimensions, new IntType() );
final NativeImgLabeling< Integer, IntType > labeling = new NativeImgLabeling< Integer, IntType >( img );
return labeling; return labeling;
} }
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment