Skip to content

Commit 71a9f1d

Browse files
committed
DefaultDatasetService: fix non-NativeType wrapping
When wrapping an ImgLib2 image whose type is not a NativeType, the image factory heuristic was barfing. Here is an example failure seen with the latest imagej-common + imagej-legacy: java.lang.IllegalArgumentException: Cannot create factory for non-Img RAI of non-native-type: class net.imglib2.type.logic.BoolType at net.imagej.DefaultDatasetService.wrapToImg(DefaultDatasetService.java:329) at net.imagej.DefaultDatasetService.wrapToImgPlus(DefaultDatasetService.java:316) at net.imagej.DefaultDatasetService.create(DefaultDatasetService.java:209) at net.imagej.legacy.convert.roi.RealMaskRealIntervalToImageRoiConverter.convert(RealMaskRealIntervalToImageRoiConverter.java:119) at net.imagej.legacy.convert.roi.RealMaskRealIntervalToImageRoiConverter.convert(RealMaskRealIntervalToImageRoiConverter.java:1) at net.imagej.legacy.convert.roi.AbstractMaskPredicateToRoiConverter.convert(AbstractMaskPredicateToRoiConverter.java:73) at org.scijava.convert.AbstractConvertService.convert(AbstractConvertService.java:128) at net.imagej.legacy.convert.roi.RealMaskRealIntervalToImageRoiConverterTest.testConversion(RealMaskRealIntervalToImageRoiConverterTest.java:92) Fortunately, ImgLib2 5.1.0 has a shiny new utility method for creating the most appropriate ImgFactory for different scenarios. In this case, it will now fall back to ListImgFactory.
1 parent fb57a24 commit 71a9f1d

File tree

2 files changed

+4
-20
lines changed

2 files changed

+4
-20
lines changed

Diff for: pom.xml

+1-1
Original file line numberDiff line numberDiff line change
@@ -145,7 +145,7 @@ Institute of Molecular Cell Biology and Genetics.</license.copyrightOwners>
145145
<releaseProfiles>deploy-to-imagej</releaseProfiles>
146146

147147
<scijava-common.version>2.73.0</scijava-common.version>
148-
<imglib2.version>5.0.0</imglib2.version>
148+
<imglib2.version>5.1.0</imglib2.version>
149149
<imglib2-roi.version>0.5.0</imglib2-roi.version>
150150
</properties>
151151

Diff for: src/main/java/net/imagej/DefaultDatasetService.java

+3-19
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,6 @@
4343
import net.imglib2.img.Img;
4444
import net.imglib2.img.ImgFactory;
4545
import net.imglib2.img.ImgView;
46-
import net.imglib2.img.array.ArrayImgFactory;
4746
import net.imglib2.img.cell.CellImgFactory;
4847
import net.imglib2.img.planar.PlanarImgFactory;
4948
import net.imglib2.type.NativeType;
@@ -59,7 +58,6 @@
5958
import net.imglib2.type.numeric.integer.UnsignedShortType;
6059
import net.imglib2.type.numeric.real.DoubleType;
6160
import net.imglib2.type.numeric.real.FloatType;
62-
import net.imglib2.util.Intervals;
6361
import net.imglib2.util.Util;
6462

6563
import org.scijava.log.LogService;
@@ -320,27 +318,13 @@ private <T extends RealType<T>> Img<T> wrapToImg(
320318
final RandomAccessibleInterval<T> rai)
321319
{
322320
if (rai instanceof Img) return (Img<T>) rai;
323-
// NB: In order to synthesize an ImgFactory here, the RAI
324-
// type must extend NativeType. So let's check!
325-
final T type = Util.getTypeFromInterval(rai);
326-
if (!(type instanceof NativeType)) {
327-
throw new IllegalArgumentException(
328-
"Cannot create factory for non-Img RAI of non-native-type: " + //
329-
type.getClass());
330-
}
331-
@SuppressWarnings({ "rawtypes", "unchecked" })
332-
final ImgFactory<T> imgFactory = imgFactory((RandomAccessibleInterval) rai);
333-
return ImgView.wrap(rai, imgFactory);
321+
return ImgView.wrap(rai, imgFactory(rai));
334322
}
335323

336-
private <T extends NativeType<T>> ImgFactory<T> imgFactory(
324+
private <T> ImgFactory<T> imgFactory(
337325
final RandomAccessibleInterval<T> rai)
338326
{
339-
// TODO: Call create.imgFactory op instead. As things stand, we cannot,
340-
// because imagej-common cannot depend on imagej-ops. Perhaps this
341-
// "wrapToImgPlus" logic should not live here? Consider best approach.
342327
final T type = Util.getTypeFromInterval(rai);
343-
return rai == null || Intervals.numElements(rai) <= Integer.MAX_VALUE
344-
? new ArrayImgFactory<>(type) : new CellImgFactory<>(type);
328+
return Util.getSuitableImgFactory(rai, type);
345329
}
346330
}

0 commit comments

Comments
 (0)