Skip to content

Commit 5e703f0

Browse files
committed
Look up display before registering datasets
If there's already a display in the legacy image map for a given dataset, then we know that dataset has already been registered and do not need to re-register. Registration always creates a new ImagePlus so this avoids a bug where ImagePlus instances can proliferate.
1 parent 0aa4085 commit 5e703f0

File tree

1 file changed

+15
-1
lines changed

1 file changed

+15
-1
lines changed

src/main/java/net/imagej/legacy/convert/DatasetToImagePlusConverter.java

+15-1
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,8 @@
3434
import java.util.Collection;
3535

3636
import net.imagej.Dataset;
37+
import net.imagej.display.ImageDisplay;
38+
import net.imagej.legacy.LegacyImageMap;
3739

3840
import org.scijava.Priority;
3941
import org.scijava.convert.Converter;
@@ -66,7 +68,19 @@ public class DatasetToImagePlusConverter extends
6668
public <T> T convert(final Object src, final Class<T> dest) {
6769
if (!legacyEnabled()) throw new UnsupportedOperationException();
6870
final Dataset d = (Dataset) src;
69-
final Object imp = legacyService.getImageMap().registerDataset(d);
71+
LegacyImageMap imageMap = legacyService.getImageMap();
72+
Object imp = null;
73+
// First see if we can find a display already showing our Dataset
74+
for (ImageDisplay display : imageMap.getImageDisplays()) {
75+
if (display.isDisplaying(d)) {
76+
imp = imageMap.lookupImagePlus(display);
77+
break;
78+
}
79+
}
80+
if (imp == null) {
81+
// No existing display so register the dataset
82+
imp = imageMap.registerDataset(d);
83+
}
7084
@SuppressWarnings("unchecked")
7185
final T typedImp = (T) imp;
7286
return typedImp;

0 commit comments

Comments
 (0)