Skip to content

Commit f93c3f6

Browse files
committed
Add ResourceManager to BigDataViewer and track added Sources
1 parent ebd1b8e commit f93c3f6

File tree

6 files changed

+649
-48
lines changed

6 files changed

+649
-48
lines changed

src/main/java/bdv/BigDataViewer.java

Lines changed: 118 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -28,16 +28,6 @@
2828
*/
2929
package bdv;
3030

31-
import bdv.tools.PreferencesDialog;
32-
import bdv.ui.UIUtils;
33-
import bdv.ui.keymap.Keymap;
34-
import bdv.ui.keymap.KeymapManager;
35-
import bdv.ui.keymap.KeymapSettingsPage;
36-
import bdv.viewer.ConverterSetups;
37-
import bdv.viewer.ViewerState;
38-
import bdv.ui.appearance.AppearanceManager;
39-
import bdv.ui.appearance.AppearanceSettingsPage;
40-
import dev.dirs.ProjectDirectories;
4131
import java.io.File;
4232
import java.io.FileNotFoundException;
4333
import java.io.FileWriter;
@@ -53,16 +43,6 @@
5343
import javax.swing.SwingUtilities;
5444
import javax.swing.filechooser.FileFilter;
5545

56-
import net.imglib2.Volatile;
57-
import net.imglib2.converter.Converter;
58-
import net.imglib2.display.ColorConverter;
59-
import net.imglib2.display.RealARGBColorConverter;
60-
import net.imglib2.display.ScaledARGBConverter;
61-
import net.imglib2.type.numeric.ARGBType;
62-
import net.imglib2.type.numeric.NumericType;
63-
import net.imglib2.type.numeric.RealType;
64-
import net.imglib2.type.volatiles.VolatileARGBType;
65-
6646
import org.jdom2.Document;
6747
import org.jdom2.Element;
6848
import org.jdom2.JDOMException;
@@ -71,6 +51,7 @@
7151
import org.jdom2.output.XMLOutputter;
7252
import org.scijava.ui.behaviour.io.InputTriggerConfig;
7353
import org.scijava.ui.behaviour.io.yaml.YamlConfigIO;
54+
import org.scijava.ui.behaviour.util.Actions;
7455

7556
import bdv.cache.CacheControl;
7657
import bdv.export.ProgressWriter;
@@ -80,6 +61,7 @@
8061
import bdv.spimdata.XmlIoSpimDataMinimal;
8162
import bdv.tools.HelpDialog;
8263
import bdv.tools.InitializeViewerState;
64+
import bdv.tools.PreferencesDialog;
8365
import bdv.tools.RecordMaxProjectionDialog;
8466
import bdv.tools.RecordMovieDialog;
8567
import bdv.tools.VisibilityAndGroupingDialog;
@@ -91,21 +73,42 @@
9173
import bdv.tools.brightness.RealARGBColorConverterSetup;
9274
import bdv.tools.brightness.SetupAssignments;
9375
import bdv.tools.crop.CropDialog;
76+
import bdv.tools.links.ResourceManager;
77+
import bdv.tools.links.resource.SpimDataMinimalFileResource;
78+
import bdv.tools.links.resource.SpimDataSetupSourceResource;
79+
import bdv.tools.links.resource.TransformedSourceResource;
9480
import bdv.tools.transformation.ManualTransformation;
9581
import bdv.tools.transformation.ManualTransformationEditor;
9682
import bdv.tools.transformation.TransformedSource;
83+
import bdv.ui.UIUtils;
84+
import bdv.ui.appearance.AppearanceManager;
85+
import bdv.ui.appearance.AppearanceSettingsPage;
86+
import bdv.ui.keymap.Keymap;
87+
import bdv.ui.keymap.KeymapManager;
88+
import bdv.ui.keymap.KeymapSettingsPage;
89+
import bdv.viewer.ConverterSetups;
9790
import bdv.viewer.NavigationActions;
9891
import bdv.viewer.SourceAndConverter;
9992
import bdv.viewer.ViewerFrame;
10093
import bdv.viewer.ViewerOptions;
10194
import bdv.viewer.ViewerPanel;
95+
import bdv.viewer.ViewerState;
96+
import dev.dirs.ProjectDirectories;
10297
import mpicbg.spim.data.SpimDataException;
10398
import mpicbg.spim.data.generic.AbstractSpimData;
10499
import mpicbg.spim.data.generic.sequence.AbstractSequenceDescription;
105100
import mpicbg.spim.data.generic.sequence.BasicViewSetup;
106101
import mpicbg.spim.data.sequence.Angle;
107102
import mpicbg.spim.data.sequence.Channel;
108-
import org.scijava.ui.behaviour.util.Actions;
103+
import net.imglib2.Volatile;
104+
import net.imglib2.converter.Converter;
105+
import net.imglib2.display.ColorConverter;
106+
import net.imglib2.display.RealARGBColorConverter;
107+
import net.imglib2.display.ScaledARGBConverter;
108+
import net.imglib2.type.numeric.ARGBType;
109+
import net.imglib2.type.numeric.NumericType;
110+
import net.imglib2.type.numeric.RealType;
111+
import net.imglib2.type.volatiles.VolatileARGBType;
109112

110113
public class BigDataViewer
111114
{
@@ -137,6 +140,8 @@ public class BigDataViewer
137140

138141
private final AppearanceManager appearanceManager;
139142

143+
private final ResourceManager resourceManager;
144+
140145
protected final PreferencesDialog preferencesDialog;
141146

142147
protected final ManualTransformationEditor manualTransformationEditor;
@@ -257,49 +262,103 @@ public static ConverterSetup createConverterSetup( final SourceAndConverter< ? >
257262
*/
258263
public static < T, V extends Volatile< T > > SourceAndConverter< T > wrapWithTransformedSource( final SourceAndConverter< T > soc )
259264
{
260-
if ( soc.asVolatile() == null )
261-
return new SourceAndConverter<>( new TransformedSource<>( soc.getSpimSource() ), soc.getConverter() );
265+
return wrapWithTransformedSource( soc, null );
266+
}
262267

263-
@SuppressWarnings( "unchecked" )
264-
final SourceAndConverter< V > vsoc = ( SourceAndConverter< V > ) soc.asVolatile();
268+
/**
269+
* Decorate source with an extra transformation, that can be edited manually
270+
* in this viewer. {@link SourceAndConverter#asVolatile() Nested volatile}
271+
* {@code SourceAndConverter} are wrapped as well, if present.
272+
*
273+
* @param soc
274+
* source to decorate
275+
* @param resources
276+
* if non-null, a {@code ResourceSpec} for the created {@code TransformedSource} wrapper.
277+
*
278+
* @return {@code TransformedSource} wrapper around {@code soc}
279+
*/
280+
public static < T, V extends Volatile< T > > SourceAndConverter< T > wrapWithTransformedSource( final SourceAndConverter< T > soc, final ResourceManager resources )
281+
{
265282
final TransformedSource< T > ts = new TransformedSource<>( soc.getSpimSource() );
266-
final TransformedSource< V > vts = new TransformedSource<>( vsoc.getSpimSource(), ts );
267-
return new SourceAndConverter<>( ts, soc.getConverter(), new SourceAndConverter<>( vts, vsoc.getConverter() ) );
283+
284+
final SourceAndConverter< V > vtsoc;
285+
if ( soc.asVolatile() == null )
286+
{
287+
vtsoc = null;
288+
}
289+
else
290+
{
291+
@SuppressWarnings( "unchecked" )
292+
final SourceAndConverter< V > vsoc = ( SourceAndConverter< V > ) soc.asVolatile();
293+
final TransformedSource< V > vts = new TransformedSource<>( vsoc.getSpimSource(), ts );
294+
vtsoc = new SourceAndConverter<>( vts, vsoc.getConverter() );
295+
}
296+
297+
final SourceAndConverter< T > tsoc = new SourceAndConverter<>( ts, soc.getConverter(), vtsoc );
298+
if (resources != null)
299+
{
300+
final TransformedSourceResource.Spec spec = new TransformedSourceResource.Spec(
301+
resources.getResourceSpec( soc ) );
302+
resources.put( tsoc, spec );
303+
resources.keepAlive( tsoc, soc );
304+
}
305+
return tsoc;
268306
}
269307

270308
private static < T extends NumericType< T >, V extends Volatile< T > & NumericType< V > > void initSetupNumericType(
271309
final AbstractSpimData< ? > spimData,
272310
final BasicViewSetup setup,
273311
final List< ConverterSetup > converterSetups,
274-
final List< SourceAndConverter< ? > > sources )
312+
final List< SourceAndConverter< ? > > sources,
313+
final ResourceManager resources )
275314
{
276315
final int setupId = setup.getId();
316+
final String setupName = createSetupName( setup );
317+
final SourceAndConverter< T > soc = createSetupSourceNumericType( spimData, setupId, setupName, resources );
318+
final SourceAndConverter< T > tsoc = wrapWithTransformedSource( soc, resources );
319+
sources.add( tsoc );
320+
321+
final ConverterSetup converterSetup = createConverterSetup( tsoc, setupId );
322+
if ( converterSetup != null )
323+
converterSetups.add( converterSetup );
324+
}
325+
326+
public static < T extends NumericType< T >, V extends Volatile< T > & NumericType< V > > SourceAndConverter< T > createSetupSourceNumericType(
327+
final AbstractSpimData< ? > spimData,
328+
final int setupId,
329+
final String name,
330+
final ResourceManager resources )
331+
{
277332
final ViewerImgLoader imgLoader = ( ViewerImgLoader ) spimData.getSequenceDescription().getImgLoader();
278333
@SuppressWarnings( "unchecked" )
279334
final ViewerSetupImgLoader< T, V > setupImgLoader = ( ViewerSetupImgLoader< T, V > ) imgLoader.getSetupImgLoader( setupId );
335+
if ( setupImgLoader == null )
336+
throw new IllegalArgumentException( "No SetupImgLoader for setup ID " + setupId + " found." );
337+
280338
final T type = setupImgLoader.getImageType();
281339
final V volatileType = setupImgLoader.getVolatileImageType();
282340

283341
if ( ! ( type instanceof NumericType ) )
284342
throw new IllegalArgumentException( "ImgLoader of type " + type.getClass() + " not supported." );
285343

286-
final String setupName = createSetupName( setup );
287-
288344
SourceAndConverter< V > vsoc = null;
289345
if ( volatileType != null )
290346
{
291-
final VolatileSpimSource< V > vs = new VolatileSpimSource<>( spimData, setupId, setupName );
347+
final VolatileSpimSource< V > vs = new VolatileSpimSource<>( spimData, setupId, name );
292348
vsoc = new SourceAndConverter<>( vs, createConverterToARGB( volatileType ) );
293349
}
294350

295-
final SpimSource< T > s = new SpimSource<>( spimData, setupId, setupName );
351+
final SpimSource< T > s = new SpimSource<>( spimData, setupId, name );
296352
final SourceAndConverter< T > soc = new SourceAndConverter<>( s, createConverterToARGB( type ), vsoc );
297-
final SourceAndConverter< T > tsoc = wrapWithTransformedSource( soc );
298-
sources.add( tsoc );
299-
300-
final ConverterSetup converterSetup = createConverterSetup( tsoc, setupId );
301-
if ( converterSetup != null )
302-
converterSetups.add( converterSetup );
353+
if (resources != null)
354+
{
355+
final SpimDataSetupSourceResource.Spec spec = new SpimDataSetupSourceResource.Spec(
356+
resources.getResourceSpec( spimData ),
357+
setupId, name );
358+
resources.put( soc, spec );
359+
resources.keepAlive( soc, spimData );
360+
}
361+
return soc;
303362
}
304363

305364
public static void initSetups(
@@ -308,7 +367,17 @@ public static void initSetups(
308367
final List< SourceAndConverter< ? > > sources )
309368
{
310369
for ( final BasicViewSetup setup : spimData.getSequenceDescription().getViewSetupsOrdered() )
311-
initSetupNumericType( spimData, setup, converterSetups, sources );
370+
initSetupNumericType( spimData, setup, converterSetups, sources, null );
371+
}
372+
373+
public static void initSetups(
374+
final AbstractSpimData< ? > spimData,
375+
final List< ConverterSetup > converterSetups,
376+
final List< SourceAndConverter< ? > > sources,
377+
final ResourceManager resources )
378+
{
379+
for ( final BasicViewSetup setup : spimData.getSequenceDescription().getViewSetupsOrdered() )
380+
initSetupNumericType( spimData, setup, converterSetups, sources, resources );
312381
}
313382

314383
@Deprecated
@@ -355,6 +424,7 @@ public BigDataViewer(
355424
final AppearanceManager optionsAppearanceManager = options.values.getAppearanceManager();
356425
keymapManager = optionsKeymapManager != null ? optionsKeymapManager : new KeymapManager( configDir );
357426
appearanceManager = optionsAppearanceManager != null ? optionsAppearanceManager : new AppearanceManager( configDir );
427+
resourceManager = options.values.getResourceManager();
358428

359429
InputTriggerConfig inputTriggerConfig = options.values.getInputTriggerConfig();
360430
final Keymap keymap = this.keymapManager.getForwardSelectedKeymap();
@@ -531,7 +601,8 @@ public static BigDataViewer open( final AbstractSpimData< ? > spimData, final St
531601

532602
final ArrayList< ConverterSetup > converterSetups = new ArrayList<>();
533603
final ArrayList< SourceAndConverter< ? > > sources = new ArrayList<>();
534-
initSetups( spimData, converterSetups, sources );
604+
final ResourceManager resources = options.values.getResourceManager();
605+
initSetups( spimData, converterSetups, sources, resources );
535606

536607
final AbstractSequenceDescription< ?, ?, ? > seq = spimData.getSequenceDescription();
537608
final int numTimepoints = seq.getTimePoints().size();
@@ -549,6 +620,8 @@ public static BigDataViewer open( final AbstractSpimData< ? > spimData, final St
549620
public static BigDataViewer open( final String xmlFilename, final String windowTitle, final ProgressWriter progressWriter, final ViewerOptions options ) throws SpimDataException
550621
{
551622
final SpimDataMinimal spimData = new XmlIoSpimDataMinimal().load( xmlFilename );
623+
final ResourceManager resources = options.values.getResourceManager();
624+
resources.put( spimData, new SpimDataMinimalFileResource.Spec( xmlFilename ) );
552625
final BigDataViewer bdv = open( spimData, windowTitle, progressWriter, options );
553626
if ( !bdv.tryLoadSettings( xmlFilename ) )
554627
InitializeViewerState.initBrightness( 0.001, 0.999, bdv.viewerFrame );
@@ -609,6 +682,11 @@ public AppearanceManager getAppearanceManager()
609682
return appearanceManager;
610683
}
611684

685+
public ResourceManager getResourceManager()
686+
{
687+
return resourceManager;
688+
}
689+
612690
public boolean tryLoadSettings( final String xmlFilename )
613691
{
614692
proposedSettingsFile = null;

0 commit comments

Comments
 (0)