Skip to content

Commit

Permalink
Look up display before registering datasets
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
hinerm committed Sep 13, 2024
1 parent 0aa4085 commit 5e703f0
Showing 1 changed file with 15 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -66,7 +68,19 @@ public class DatasetToImagePlusConverter extends
public <T> T convert(final Object src, final Class<T> 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;
Expand Down

0 comments on commit 5e703f0

Please sign in to comment.