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

Use ClassCopyProvider to instantiate RealARGBColorConverter

Before, two class "copies" were hard-coded (Imp0, Imp1) and used for
volatile respectively non-volatile Types. This would still cause slow-down
if for example IntType and ShortType were mixed. Now, every Type gets its
own class copy.
parent 2fcad967
No related branches found
No related tags found
No related merge requests found
......@@ -11,7 +11,7 @@
<groupId>sc.fiji</groupId>
<artifactId>bigdataviewer-core</artifactId>
<version>6.0.3-SNAPSHOT</version>
<version>7.0.0-SNAPSHOT</version>
<name>BigDataViewer Core</name>
<description>BigDataViewer core classes with minimal dependencies.</description>
......
......@@ -174,14 +174,17 @@ public class BigDataViewer
initSetupRealTypeNonVolatile( spimData, setup, type, converterSetups, sources );
return;
}
final int setupId = setup.getId();
final V volatileType = ( ( ViewerSetupImgLoader< T, V > ) spimData.getSequenceDescription().getImgLoader().getSetupImgLoader( setupId ) ).getVolatileImageType();
final double typeMin = Math.max( 0, Math.min( type.getMinValue(), 65535 ) );
final double typeMax = Math.max( 0, Math.min( type.getMaxValue(), 65535 ) );
final RealARGBColorConverter< V > vconverter = new RealARGBColorConverter.Imp0<>( typeMin, typeMax );
final RealARGBColorConverter< V > vconverter = RealARGBColorConverter.create( volatileType, typeMin, typeMax );
vconverter.setColor( new ARGBType( 0xffffffff ) );
final RealARGBColorConverter< T > converter = new RealARGBColorConverter.Imp1<>( typeMin, typeMax );
final RealARGBColorConverter< T > converter = RealARGBColorConverter.create( type, typeMin, typeMax );
converter.setColor( new ARGBType( 0xffffffff ) );
final int setupId = setup.getId();
final String setupName = createSetupName( setup );
final VolatileSpimSource< T, V > vs = new VolatileSpimSource<>( spimData, setupId, setupName );
final SpimSource< T > s = vs.nonVolatile();
......@@ -207,7 +210,7 @@ public class BigDataViewer
{
final double typeMin = type.getMinValue();
final double typeMax = type.getMaxValue();
final RealARGBColorConverter< T > converter = new RealARGBColorConverter.Imp1<>( typeMin, typeMax );
final RealARGBColorConverter< T > converter = RealARGBColorConverter.create( type, typeMin, typeMax );
converter.setColor( new ARGBType( 0xffffffff ) );
final int setupId = setup.getId();
......
......@@ -129,7 +129,7 @@ public class BoundingBoxDialog extends JDialog
};
// set up a converter from the source type (UnsignedShortType in this case) to ARGBType
final RealARGBColorConverter< UnsignedShortType > converter = new RealARGBColorConverter.Imp1<>( 0, 3000 );
final RealARGBColorConverter< UnsignedShortType > converter = RealARGBColorConverter.create( new UnsignedShortType(), 0, 3000 );
converter.setColor( new ARGBType( 0x00994499 ) ); // set bounding box color to magenta
// create a ConverterSetup (can be used by the brightness dialog to adjust the converter settings)
......
......@@ -30,123 +30,116 @@
package net.imglib2.display;
import net.imglib2.converter.Converter;
import net.imglib2.loops.ClassCopyProvider;
import net.imglib2.type.numeric.ARGBType;
import net.imglib2.type.numeric.RealType;
public abstract class RealARGBColorConverter< R extends RealType< ? > > implements ColorConverter, Converter< R, ARGBType >
public interface RealARGBColorConverter< R extends RealType< ? > > extends ColorConverter, Converter< R, ARGBType >
{
protected double min = 0;
public static < R extends RealType< ? > > RealARGBColorConverter< R > create( final R type, final double min, final double max )
{
return Instances.create( type, min, max );
}
}
protected double max = 1;
class Instances
{
@SuppressWarnings( "rawtypes" )
private static ClassCopyProvider< RealARGBColorConverter > provider;
protected final ARGBType color = new ARGBType( ARGBType.rgba( 255, 255, 255, 255 ) );
@SuppressWarnings( "unchecked" )
public static < R extends RealType< ? > > RealARGBColorConverter< R > create( final R type, final double min, final double max )
{
if ( provider == null )
{
synchronized ( Instances.class )
{
if ( provider == null )
provider = new ClassCopyProvider<>( Imp.class, RealARGBColorConverter.class, double.class, double.class );
}
}
return provider.newInstanceForKey( type, min, max );
}
protected int A;
public static class Imp< R extends RealType< ? > > implements RealARGBColorConverter< R >
{
private double min = 0;
protected double scaleR;
private double max = 1;
protected double scaleG;
private final ARGBType color = new ARGBType( ARGBType.rgba( 255, 255, 255, 255 ) );
protected double scaleB;
private int A;
public RealARGBColorConverter( final double min, final double max )
{
this.min = min;
this.max = max;
update();
}
private double scaleR;
protected int black;
private double scaleG;
@Override
public ARGBType getColor()
{
return color.copy();
}
private double scaleB;
@Override
public void setColor( final ARGBType c )
{
color.set( c );
update();
}
private int black;
@Override
public boolean supportsColor()
{
return true;
}
public Imp( final double min, final double max )
{
this.min = min;
this.max = max;
update();
}
@Override
public double getMin()
{
return min;
}
@Override
public ARGBType getColor()
{
return color.copy();
}
@Override
public double getMax()
{
return max;
}
@Override
public void setColor( final ARGBType c )
{
color.set( c );
update();
}
@Override
public void setMax( final double max )
{
this.max = max;
update();
}
@Override
public boolean supportsColor()
{
return true;
}
@Override
public void setMin( final double min )
{
this.min = min;
update();
}
@Override
public double getMin()
{
return min;
}
private void update()
{
final double scale = 1.0 / ( max - min );
final int value = color.get();
A = ARGBType.alpha( value );
scaleR = ARGBType.red( value ) * scale;
scaleG = ARGBType.green( value ) * scale;
scaleB = ARGBType.blue( value ) * scale;
black = ARGBType.rgba( 0, 0, 0, A );
}
@Override
public double getMax()
{
return max;
}
public static class Imp0< R extends RealType< ? > > extends RealARGBColorConverter< R >
{
public Imp0( final double min, final double max )
@Override
public void setMax( final double max )
{
super( min, max );
this.max = max;
update();
}
@Override
public void convert( final R input, final ARGBType output )
public void setMin( final double min )
{
final double v = input.getRealDouble() - min;
if ( v < 0 )
{
output.set( black );
}
else
{
final int r0 = ( int ) ( scaleR * v + 0.5 );
final int g0 = ( int ) ( scaleG * v + 0.5 );
final int b0 = ( int ) ( scaleB * v + 0.5 );
final int r = Math.min( 255, r0 );
final int g = Math.min( 255, g0 );
final int b = Math.min( 255, b0 );
output.set( ARGBType.rgba( r, g, b, A) );
}
this.min = min;
update();
}
}
public static class Imp1< R extends RealType< ? > > extends RealARGBColorConverter< R >
{
public Imp1( final double min, final double max )
private void update()
{
super( min, max );
final double scale = 1.0 / ( max - min );
final int value = color.get();
A = ARGBType.alpha( value );
scaleR = ARGBType.red( value ) * scale;
scaleG = ARGBType.green( value ) * scale;
scaleB = ARGBType.blue( value ) * scale;
black = ARGBType.rgba( 0, 0, 0, A );
}
@Override
......@@ -165,7 +158,7 @@ public abstract class RealARGBColorConverter< R extends RealType< ? > > implemen
final int r = Math.min( 255, r0 );
final int g = Math.min( 255, g0 );
final int b = Math.min( 255, b0 );
output.set( ARGBType.rgba( r, g, b, A) );
output.set( ARGBType.rgba( r, g, b, A ) );
}
}
}
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment