From 5e703f0927e1fa63c1cbef272486dec292cfd5a2 Mon Sep 17 00:00:00 2001 From: hinerm Date: Tue, 10 Sep 2024 12:25:33 -0500 Subject: [PATCH] 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. --- .../convert/DatasetToImagePlusConverter.java | 16 +++++++++++++++- 1 file changed, 15 insertions(+), 1 deletion(-) diff --git a/src/main/java/net/imagej/legacy/convert/DatasetToImagePlusConverter.java b/src/main/java/net/imagej/legacy/convert/DatasetToImagePlusConverter.java index c6acf0e7..5c0e431c 100644 --- a/src/main/java/net/imagej/legacy/convert/DatasetToImagePlusConverter.java +++ b/src/main/java/net/imagej/legacy/convert/DatasetToImagePlusConverter.java @@ -34,6 +34,8 @@ import java.util.Collection; import net.imagej.Dataset; +import net.imagej.display.ImageDisplay; +import net.imagej.legacy.LegacyImageMap; import org.scijava.Priority; import org.scijava.convert.Converter; @@ -66,7 +68,19 @@ public class DatasetToImagePlusConverter extends public T convert(final Object src, final Class dest) { if (!legacyEnabled()) throw new UnsupportedOperationException(); final Dataset d = (Dataset) src; - final Object imp = legacyService.getImageMap().registerDataset(d); + LegacyImageMap imageMap = legacyService.getImageMap(); + Object imp = null; + // First see if we can find a display already showing our Dataset + for (ImageDisplay display : imageMap.getImageDisplays()) { + if (display.isDisplaying(d)) { + imp = imageMap.lookupImagePlus(display); + break; + } + } + if (imp == null) { + // No existing display so register the dataset + imp = imageMap.registerDataset(d); + } @SuppressWarnings("unchecked") final T typedImp = (T) imp; return typedImp;