/
SharedBigDataViewerData.java
708 lines (632 loc) · 23.8 KB
/
SharedBigDataViewerData.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
/*-
* #%L
* Mastodon
* %%
* Copyright (C) 2014 - 2024 Tobias Pietzsch, Jean-Yves Tinevez
* %%
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR CONTRIBUTORS BE
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
* #L%
*/
package org.mastodon.views.bdv;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import org.jdom2.Document;
import org.jdom2.Element;
import org.jdom2.JDOMException;
import org.jdom2.input.SAXBuilder;
import org.jdom2.output.Format;
import org.jdom2.output.XMLOutputter;
import org.mastodon.util.DummySpimData;
import org.scijava.ui.behaviour.io.InputTriggerConfig;
import bdv.BigDataViewer;
import bdv.TransformEventHandler2D;
import bdv.TransformEventHandler3D;
import bdv.ViewerImgLoader;
import bdv.cache.CacheControl;
import bdv.img.imagestack.ImageStackImageLoader;
import bdv.img.virtualstack.VirtualStackImageLoader;
import bdv.spimdata.SequenceDescriptionMinimal;
import bdv.spimdata.SpimDataMinimal;
import bdv.spimdata.WrapBasicImgLoader;
import bdv.spimdata.XmlIoSpimDataMinimal;
import bdv.tools.InitializeViewerState;
import bdv.tools.bookmarks.Bookmarks;
import bdv.tools.brightness.ConverterSetup;
import bdv.tools.transformation.ManualTransformation;
import bdv.util.Bounds;
import bdv.viewer.BasicViewerState;
import bdv.viewer.ConverterSetups;
import bdv.viewer.DisplayMode;
import bdv.viewer.Source;
import bdv.viewer.SourceAndConverter;
import bdv.viewer.ViewerOptions;
import bdv.viewer.ViewerPanel;
import bdv.viewer.ViewerState;
import ch.systemsx.cisd.hdf5.exceptions.HDF5FileNotFoundException;
import ij.CompositeImage;
import ij.IJ;
import ij.ImagePlus;
import ij.io.FileInfo;
import ij.process.LUT;
import mpicbg.spim.data.SpimDataException;
import mpicbg.spim.data.SpimDataIOException;
import mpicbg.spim.data.XmlHelpers;
import mpicbg.spim.data.generic.AbstractSpimData;
import mpicbg.spim.data.generic.sequence.AbstractSequenceDescription;
import mpicbg.spim.data.generic.sequence.BasicImgLoader;
import mpicbg.spim.data.generic.sequence.BasicViewSetup;
import mpicbg.spim.data.registration.ViewRegistration;
import mpicbg.spim.data.registration.ViewRegistrations;
import mpicbg.spim.data.sequence.Channel;
import mpicbg.spim.data.sequence.FinalVoxelDimensions;
import mpicbg.spim.data.sequence.TimePoint;
import mpicbg.spim.data.sequence.TimePoints;
import net.imglib2.FinalDimensions;
import net.imglib2.RandomAccessibleInterval;
import net.imglib2.realtransform.AffineTransform3D;
import net.imglib2.type.numeric.ARGBType;
public class SharedBigDataViewerData
{
private final ArrayList< SourceAndConverter< ? > > sources;
private final ConverterSetups setups;
private final ManualTransformation manualTransformation;
private final Bookmarks bookmarks;
private final ViewerOptions options;
private final InputTriggerConfig inputTriggerConfig;
private final AbstractSpimData< ? > spimData;
private final CacheControl cache;
private final boolean is2D;
private File proposedSettingsFile;
private SharedBigDataViewerData(
final AbstractSpimData< ? > spimData,
final ArrayList< SourceAndConverter< ? > > sources,
final ConverterSetups setups,
final CacheControl cache )
{
this.spimData = spimData;
this.sources = sources;
this.setups = setups;
this.cache = cache;
final ViewerOptions lvo = new ViewerOptions();
this.inputTriggerConfig = ( lvo.values.getInputTriggerConfig() != null )
? lvo.values.getInputTriggerConfig()
: new InputTriggerConfig();
this.manualTransformation = new ManualTransformation( sources );
this.bookmarks = new Bookmarks();
this.is2D = computeIs2D();
this.options = lvo
.inputTriggerConfig( inputTriggerConfig )
.transformEventHandlerFactory( is2D
? TransformEventHandler2D::new
: TransformEventHandler3D::new );
if ( WrapBasicImgLoader.wrapImgLoaderIfNecessary( spimData ) )
System.err.println(
"WARNING:\nOpening <SpimData> dataset that is not suited for interactive browsing.\nConsider resaving as HDF5 for better performance." );
WrapBasicImgLoader.removeWrapperIfPresent( spimData );
}
public boolean tryLoadSettings( final String xmlFilename )
{
proposedSettingsFile = null;
if ( xmlFilename.startsWith( "http://" ) )
{
// load settings.xml from the BigDataServer
final String settings = xmlFilename + "settings";
{
try
{
loadSettings( settings, null );
return true;
}
catch ( final FileNotFoundException e )
{}
catch ( final Exception e )
{
e.printStackTrace();
}
}
}
else if ( xmlFilename.endsWith( ".xml" ) )
{
final String settings =
xmlFilename.substring( 0, xmlFilename.length() - ".xml".length() ) + ".settings" + ".xml";
proposedSettingsFile = new File( settings );
if ( proposedSettingsFile.isFile() )
{
try
{
loadSettings( settings, null );
return true;
}
catch ( final Exception e )
{
e.printStackTrace();
}
}
}
return false;
}
public void loadSettings( final String xmlFilename, final ViewerPanel viewer ) throws IOException, JDOMException
{
final SAXBuilder sax = new SAXBuilder();
final Document doc = sax.build( xmlFilename );
final Element root = doc.getRootElement();
if ( viewer != null )
viewer.stateFromXml( root );
restoreFromXmlSetupAssignments( root );
manualTransformation.restoreFromXml( root );
bookmarks.restoreFromXml( root );
}
public void saveSettings( final String xmlFilename, final ViewerPanel viewer ) throws IOException
{
final Element root = new Element( "Settings" );
if ( viewer != null )
root.addContent( viewer.stateToXml() );
root.addContent( toXmlSetupAssignments() );
root.addContent( manualTransformation.toXml() );
root.addContent( bookmarks.toXml() );
final Document doc = new Document( root );
final XMLOutputter xout = new XMLOutputter( Format.getPrettyFormat() );
xout.output( doc, new FileWriter( xmlFilename ) );
}
/**
* Creates an {@link Element} containing suitable information for the
* <code>SetupAssignments</code> part of the serialization of the BDV
* settings.
*
* @return a new {@link Element}.
*/
public Element toXmlSetupAssignments()
{
final Element elem = new Element( "SetupAssignments" );
final Element elemConverterSetups = new Element( "ConverterSetups" );
for ( int i = 0; i < sources.size(); ++i )
{
final SourceAndConverter< ? > source = sources.get( i );
final ConverterSetup setup = setups.getConverterSetup( source );
final Element elemConverterSetup = new Element( "ConverterSetup" );
elemConverterSetup.addContent( XmlHelpers.intElement( "id", setup.getSetupId() ) );
elemConverterSetup.addContent( XmlHelpers.doubleElement( "min", setup.getDisplayRangeMin() ) );
elemConverterSetup.addContent( XmlHelpers.doubleElement( "max", setup.getDisplayRangeMax() ) );
elemConverterSetup.addContent( XmlHelpers.intElement( "color", setup.getColor().get() ) );
elemConverterSetup.addContent( XmlHelpers.intElement( "groupId", i ) );
elemConverterSetups.addContent( elemConverterSetup );
}
elem.addContent( elemConverterSetups );
// Can't save MinMaxGroups without calling to deprecated methods.
// Put a dummy list instead copying single source for now.
final Element elemMinMaxGroups = new Element( "MinMaxGroups" );
for ( int i = 0; i < sources.size(); ++i )
{
final SourceAndConverter< ? > source = sources.get( i );
final ConverterSetup setup = setups.getConverterSetup( source );
final Bounds bounds = setups.getBounds().getBounds( setup );
final Element elemMinMaxGroup = new Element( "MinMaxGroup" );
elemMinMaxGroup.addContent( XmlHelpers.intElement( "id", i ) );
elemMinMaxGroup.addContent( XmlHelpers.doubleElement( "fullRangeMin", bounds.getMinBound() ) );
elemMinMaxGroup.addContent( XmlHelpers.doubleElement( "fullRangeMax", bounds.getMaxBound() ) );
elemMinMaxGroup.addContent( XmlHelpers.doubleElement( "rangeMin", bounds.getMinBound() ) );
elemMinMaxGroup.addContent( XmlHelpers.doubleElement( "rangeMax", bounds.getMaxBound() ) );
elemMinMaxGroup.addContent( XmlHelpers.doubleElement( "currentMin", setup.getDisplayRangeMin() ) );
elemMinMaxGroup.addContent( XmlHelpers.doubleElement( "currentMax", setup.getDisplayRangeMax() ) );
elemMinMaxGroups.addContent( elemMinMaxGroup );
}
elem.addContent( elemMinMaxGroups );
return elem;
}
private void restoreFromXmlSetupAssignments( final Element parent )
{
final Element elemSetupAssignments = parent.getChild( "SetupAssignments" );
if ( elemSetupAssignments == null )
return;
final Element elemConverterSetups = elemSetupAssignments.getChild( "ConverterSetups" );
final List< Element > converterSetupNodes = elemConverterSetups.getChildren( "ConverterSetup" );
if ( converterSetupNodes.size() != sources.size() )
throw new IllegalArgumentException();
final Element elemMinMaxGroups = elemSetupAssignments.getChild( "MinMaxGroups" );
final List< Element > minMaxGroupNodes = elemMinMaxGroups.getChildren( "MinMaxGroup" );
for ( final Element elem : minMaxGroupNodes )
{
final int id = Integer.parseInt( elem.getChildText( "id" ) );
// final double fullRangeMin = Double.parseDouble( elem.getChildText( "fullRangeMin" ) );
// final double fullRangeMax = Double.parseDouble( elem.getChildText( "fullRangeMax" ) );
final double rangeMin = Double.parseDouble( elem.getChildText( "rangeMin" ) );
final double rangeMax = Double.parseDouble( elem.getChildText( "rangeMax" ) );
// final double currentMin = Double.parseDouble( elem.getChildText( "currentMin" ) );
// final double currentMax = Double.parseDouble( elem.getChildText( "currentMax" ) );
final SourceAndConverter< ? > source = sources.get( id );
final ConverterSetup setup = setups.getConverterSetup( source );
setups.getBounds().setBounds( setup, new Bounds( rangeMin, rangeMax ) );
}
for ( final Element elem : converterSetupNodes )
{
final int id = Integer.parseInt( elem.getChildText( "id" ) );
final double min = Double.parseDouble( elem.getChildText( "min" ) );
final double max = Double.parseDouble( elem.getChildText( "max" ) );
final int color = Integer.parseInt( elem.getChildText( "color" ) );
// final int groupId = Integer.parseInt( elem.getChildText( "groupId" ) );
final ConverterSetup setup = getSetupById( id );
setup.setDisplayRange( min, max );
setup.setColor( new ARGBType( color ) );
}
}
private ConverterSetup getSetupById( final int id )
{
for ( final SourceAndConverter< ? > source : sources )
{
final ConverterSetup setup = setups.getConverterSetup( source );
if ( setup.getSetupId() == id )
return setup;
}
return null;
}
public AbstractSpimData< ? > getSpimData()
{
return spimData;
}
public ViewerOptions getOptions()
{
return options;
}
public InputTriggerConfig getInputTriggerConfig()
{
return inputTriggerConfig;
}
public ArrayList< SourceAndConverter< ? > > getSources()
{
return sources;
}
public ConverterSetups getConverterSetups()
{
return setups;
}
public int getNumTimepoints()
{
return spimData.getSequenceDescription().getTimePoints().size();
}
public CacheControl getCache()
{
return cache;
}
public Bookmarks getBookmarks()
{
return bookmarks;
}
public ManualTransformation getManualTransformation()
{
return manualTransformation;
}
public File getProposedSettingsFile()
{
return proposedSettingsFile;
}
public void setProposedSettingsFile( final File file )
{
this.proposedSettingsFile = file;
}
public boolean is2D()
{
return is2D;
}
/**
* Utility that returns <code>true</code> if all the sources specified are
* 2D.
*
* @return <code>true</code> if all the sources specified are 2D.
*/
private boolean computeIs2D()
{
for ( final SourceAndConverter< ? > sac : sources )
{
final Source< ? > source = sac.getSpimSource();
for ( int t = 0; t < getNumTimepoints(); t++ )
{
if ( source.isPresent( t ) )
{
final RandomAccessibleInterval< ? > level = source.getSource( t, 0 );
if ( level.dimension( 2 ) > 1 )
return false;
break;
}
}
}
return true;
}
public static SharedBigDataViewerData fromSpimDataXmlFile( final String spimDataXmlFilename ) throws SpimDataException
{
try
{
final AbstractSpimData< ? > spimData = new XmlIoSpimDataMinimal().load( spimDataXmlFilename );
return fromSpimData( spimDataXmlFilename, spimData );
}
catch ( final RuntimeException e )
{
if ( FileNotFoundException.class.isInstance( e.getCause() ) )
throw new SpimDataIOException( "Could not find the image data file:\n" + e.getCause().getMessage() );
else if ( HDF5FileNotFoundException.class.isInstance( e ) )
throw new SpimDataIOException( "Error in the BDV XML file:" + spimDataXmlFilename
+ "\nCould not find the HDF5 image data file:\n"
+ e.getMessage() );
throw ( e );
}
}
public static SharedBigDataViewerData fromDummyFilename( final String spimDataXmlFilename )
{
final AbstractSpimData< ? > spimData = DummySpimData.tryCreate( spimDataXmlFilename );
return fromSpimData( spimDataXmlFilename, spimData );
}
/**
* Returns a "dummy" {@link SharedBigDataViewerData} object. Pixel sizes,
* image sizes, and image transformations are read from the given
* BigDataViewer XML. The actual image data is not loaded, all pixels are
* black.
*
* @param spimDataXmlFilename
* the pseudo XML file, containing the dummy information.
* @return a "dummy" {@link SharedBigDataViewerData} object.
* @throws SpimDataException
* if something wrong happens when build the spim data object.
*/
public static SharedBigDataViewerData createDummyDataFromSpimDataXml( final String spimDataXmlFilename ) throws SpimDataException
{
final AbstractSpimData< ? > spimData = DummySpimData.fromSpimDataXml( spimDataXmlFilename );
return fromSpimData( spimDataXmlFilename, spimData );
}
private static SharedBigDataViewerData fromSpimData(
final String spimDataXmlFilename,
final AbstractSpimData< ? > spimData )
{
final AbstractSequenceDescription< ?, ?, ? > seq = spimData.getSequenceDescription();
final CacheControl cache = ( ( ViewerImgLoader ) seq.getImgLoader() ).getCacheControl();
final ArrayList< ConverterSetup > converterSetups = new ArrayList<>();
final ArrayList< SourceAndConverter< ? > > sources = new ArrayList<>();
BigDataViewer.initSetups( spimData, converterSetups, sources );
final ConverterSetups setups = new ConverterSetups( new BasicViewerState() );
for ( int i = 0; i < sources.size(); ++i )
setups.put( sources.get( i ), converterSetups.get( i ) );
WrapBasicImgLoader.removeWrapperIfPresent( spimData );
final SharedBigDataViewerData sbdv = new SharedBigDataViewerData(
spimData,
sources,
setups,
cache );
if ( !sbdv.tryLoadSettings( spimDataXmlFilename ) )
{
final BasicViewerState state = new BasicViewerState();
state.addSource( sources.get( 0 ) );
state.setCurrentSource( sources.get( 0 ) );
InitializeViewerState.initBrightness( 0.001, 0.999, state, setups );
}
return sbdv;
}
/*
* FROM IMAGEPLUS.
*/
public static SharedBigDataViewerData fromImagePlus( final ImagePlus imp )
{
// check the image type
switch ( imp.getType() )
{
case ImagePlus.GRAY8:
case ImagePlus.GRAY16:
case ImagePlus.GRAY32:
case ImagePlus.COLOR_RGB:
break;
default:
IJ.showMessage(
imp.getShortTitle() + ": Only 8, 16, 32-bit images and RGB images are supported currently!" );
return null;
}
// get calibration and image size
final double pw = imp.getCalibration().pixelWidth;
final double ph = imp.getCalibration().pixelHeight;
final double pd = imp.getCalibration().pixelDepth;
String punit = imp.getCalibration().getUnit();
if ( punit == null || punit.isEmpty() )
punit = "pixel";
final FinalVoxelDimensions voxelSize = new FinalVoxelDimensions( punit, pw, ph, pd );
final int w = imp.getWidth();
final int h = imp.getHeight();
final int d = imp.getNSlices();
final FinalDimensions size = new FinalDimensions( w, h, d );
// create ImgLoader wrapping the image
final BasicImgLoader imgLoader;
int setupIdOffset = 0;
if ( imp.getStack().isVirtual() )
{
switch ( imp.getType() )
{
case ImagePlus.GRAY8:
imgLoader = VirtualStackImageLoader.createUnsignedByteInstance( imp, setupIdOffset );
break;
case ImagePlus.GRAY16:
imgLoader = VirtualStackImageLoader.createUnsignedShortInstance( imp, setupIdOffset );
break;
case ImagePlus.GRAY32:
imgLoader = VirtualStackImageLoader.createFloatInstance( imp, setupIdOffset );
break;
case ImagePlus.COLOR_RGB:
default:
imgLoader = VirtualStackImageLoader.createARGBInstance( imp, setupIdOffset );
break;
}
}
else
{
switch ( imp.getType() )
{
case ImagePlus.GRAY8:
imgLoader = ImageStackImageLoader.createUnsignedByteInstance( imp, setupIdOffset );
break;
case ImagePlus.GRAY16:
imgLoader = ImageStackImageLoader.createUnsignedShortInstance( imp, setupIdOffset );
break;
case ImagePlus.GRAY32:
imgLoader = ImageStackImageLoader.createFloatInstance( imp, setupIdOffset );
break;
case ImagePlus.COLOR_RGB:
default:
imgLoader = ImageStackImageLoader.createARGBInstance( imp, setupIdOffset );
break;
}
}
final int numTimepoints = imp.getNFrames();
final int numSetups = imp.getNChannels();
// create setups from channels
final HashMap< Integer, BasicViewSetup > setups = new HashMap<>( numSetups );
for ( int s = 0; s < numSetups; ++s )
{
final BasicViewSetup setup = new BasicViewSetup( setupIdOffset + s,
String.format( imp.getTitle() + " channel %d", s + 1 ), size, voxelSize );
setup.setAttribute( new Channel( s + 1 ) );
setups.put( setupIdOffset + s, setup );
}
// create timepoints
final ArrayList< TimePoint > timepoints = new ArrayList<>( numTimepoints );
for ( int t = 0; t < numTimepoints; ++t )
timepoints.add( new TimePoint( t ) );
final SequenceDescriptionMinimal seq =
new SequenceDescriptionMinimal( new TimePoints( timepoints ), setups, imgLoader, null );
// create ViewRegistrations from the images calibration
final AffineTransform3D sourceTransform = new AffineTransform3D();
sourceTransform.set( pw, 0, 0, 0, 0, ph, 0, 0, 0, 0, pd, 0 );
final ArrayList< ViewRegistration > registrations = new ArrayList<>();
for ( int t = 0; t < numTimepoints; ++t )
for ( int s = 0; s < numSetups; ++s )
registrations.add( new ViewRegistration( t, setupIdOffset + s, sourceTransform ) );
final ArrayList< ConverterSetup > converterSetups = new ArrayList<>();
final ArrayList< SourceAndConverter< ? > > sources = new ArrayList<>();
final File basePath = new File( "." );
final AbstractSpimData< ? > spimData =
new SpimDataMinimal( basePath, seq, new ViewRegistrations( registrations ) );
WrapBasicImgLoader.wrapImgLoaderIfNecessary( spimData );
BigDataViewer.initSetups( spimData, converterSetups, sources );
final CacheControl.CacheControls cache = new CacheControl.CacheControls();
cache.addCacheControl(
( ( ViewerImgLoader ) spimData.getSequenceDescription().getImgLoader() ).getCacheControl() );
setupIdOffset += imp.getNChannels();
final BasicViewerState state = new BasicViewerState();
for ( final SourceAndConverter< ? > sourceAndConverter : sources )
state.addSource( sourceAndConverter );
final ConverterSetups css = new ConverterSetups( state );
for ( int i = 0; i < sources.size(); i++ )
css.put( sources.get( i ), converterSetups.get( i ) );
final SharedBigDataViewerData sbdv = new SharedBigDataViewerData(
spimData,
sources,
css,
cache );
// File info
final FileInfo fileInfo = imp.getOriginalFileInfo();
String imageFileName;
String imageFolder;
if ( null != fileInfo )
{
imageFileName = fileInfo.fileName;
imageFolder = fileInfo.directory;
}
else
{
imageFileName = imp.getShortTitle();
imageFolder = "";
}
final String imageSourceFilename = new File( imageFolder, imageFileName ).getAbsolutePath();
if ( !sbdv.tryLoadSettings( imageSourceFilename ) )
{
int channelOffset = 0;
final int numActiveChannels = transferChannelVisibility( imp, state );
transferChannelSettings( channelOffset, imp, state, css );
channelOffset += imp.getNChannels();
state.setDisplayMode( numActiveChannels > 1 ? DisplayMode.FUSED : DisplayMode.SINGLE );
}
return sbdv;
}
/**
* @return number of setups that were set active.
*/
private static int transferChannelVisibility(
final ImagePlus imp,
final ViewerState state )
{
final int nChannels = imp.getNChannels();
final CompositeImage ci = imp.isComposite() ? ( CompositeImage ) imp : null;
final List< SourceAndConverter< ? > > sources = state.getSources();
if ( ci != null && ci.getCompositeMode() == IJ.COMPOSITE )
{
final boolean[] activeChannels = ci.getActiveChannels();
int numActiveChannels = 0;
for ( int i = 0; i < Math.min( activeChannels.length, nChannels ); ++i )
{
final SourceAndConverter< ? > source = sources.get( i );
state.setSourceActive( source, activeChannels[ i ] );
state.setCurrentSource( source );
numActiveChannels += activeChannels[ i ] ? 1 : 0;
}
return numActiveChannels;
}
else
{
final int activeChannel = imp.getChannel() - 1;
for ( int i = 0; i < nChannels; ++i )
state.setSourceActive( sources.get( i ), i == activeChannel );
state.setCurrentSource( sources.get( activeChannel ) );
return 1;
}
}
private static void transferChannelSettings( final int channelOffset, final ImagePlus imp, final ViewerState state,
final ConverterSetups converterSetups )
{
final int nChannels = imp.getNChannels();
final CompositeImage ci = imp.isComposite() ? ( CompositeImage ) imp : null;
final List< SourceAndConverter< ? > > sources = state.getSources();
if ( ci != null )
{
final int mode = ci.getCompositeMode();
final boolean transferColor = mode == IJ.COMPOSITE || mode == IJ.COLOR;
for ( int c = 0; c < nChannels; ++c )
{
final LUT lut = ci.getChannelLut( c + 1 );
final ConverterSetup setup = converterSetups.getConverterSetup( sources.get( channelOffset + c ) );
if ( transferColor )
setup.setColor( new ARGBType( lut.getRGB( 255 ) ) );
setup.setDisplayRange( lut.min, lut.max );
}
}
else
{
final double displayRangeMin = imp.getDisplayRangeMin();
final double displayRangeMax = imp.getDisplayRangeMax();
for ( int i = 0; i < nChannels; ++i )
{
final ConverterSetup setup = converterSetups.getConverterSetup( sources.get( channelOffset + i ) );
final LUT[] luts = imp.getLuts();
if ( luts.length != 0 )
setup.setColor( new ARGBType( luts[ 0 ].getRGB( 255 ) ) );
setup.setDisplayRange( displayRangeMin, displayRangeMax );
}
}
}
}