Skip to content

Commit

Permalink
WIP: Add converters for all common image types
Browse files Browse the repository at this point in the history
[CTR: There is one failing test... due to how SingleInputPreprocessors
work. They eagerly try to convert. Now that this commit adds so many
converters, we get a situation where e.g. the ActiveDatasetPreprocessor
takes precedence over the ActiveImageDisplayPreprocessor when filling
single ImageDisplay parameters, which is obviously bad... blargh.]

This work is a major step toward resolving imagej/imagej-legacy#229,
but does not quite do the entire job, since ij.ImagePlus is another
image type on that list, which is not available from imagej-common.
But we can add converters to imagej-legacy for ImagePlus to+from all
other image types, and then we'll have everything covered.

See also imagej/imagej-ops#54.
  • Loading branch information
ctrueden committed Nov 11, 2022
1 parent 572affd commit efa149b
Show file tree
Hide file tree
Showing 15 changed files with 769 additions and 486 deletions.
27 changes: 2 additions & 25 deletions src/main/java/net/imagej/DefaultDatasetService.java
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,6 @@
import net.imglib2.converter.Converters;
import net.imglib2.img.Img;
import net.imglib2.img.ImgFactory;
import net.imglib2.img.ImgView;
import net.imglib2.img.cell.CellImgFactory;
import net.imglib2.img.planar.PlanarImgFactory;
import net.imglib2.type.NativeType;
Expand All @@ -62,7 +61,6 @@
import net.imglib2.type.numeric.integer.UnsignedShortType;
import net.imglib2.type.numeric.real.DoubleType;
import net.imglib2.type.numeric.real.FloatType;
import net.imglib2.util.Util;

import net.imglib2.view.Views;
import org.scijava.log.LogService;
Expand Down Expand Up @@ -228,7 +226,7 @@ private Dataset createARGBType(ImgPlus<ARGBType> imgPlus) {
public <T extends Type<T>> Dataset create(
final RandomAccessibleInterval<T> rai)
{
return create(wrapToImgPlus(rai));
return create(ImgPlus.wrap(rai));
}

private ImgPlus< UnsignedByteType > argbToMultiChannel(
Expand All @@ -239,7 +237,7 @@ private ImgPlus< UnsignedByteType > argbToMultiChannel(
.argbChannel(rai, 1);
RandomAccessibleInterval<UnsignedByteType> green = Converters.argbChannel(rai, 2);
RandomAccessibleInterval<UnsignedByteType> blue = Converters.argbChannel(rai, 3);
Img<UnsignedByteType> channels = wrapToImg(Views.stack(red, green, blue));
Img<UnsignedByteType> channels = ImgPlus.wrapToImg(Views.stack(red, green, blue));
int n = imgPlus.numDimensions();
CalibratedAxis[] axes = new CalibratedAxis[n + 1];
for (int d = 0; d < n; d++) axes[d] = imgPlus.axis(d);
Expand Down Expand Up @@ -346,25 +344,4 @@ private void invalidParams(final int bitsPerPixel, final boolean signed,
throw new IllegalArgumentException("Invalid parameters: bitsPerPixel=" +
bitsPerPixel + ", signed=" + signed + ", floating=" + floating);
}

private <T extends Type<T>> ImgPlus<T> wrapToImgPlus(
final RandomAccessibleInterval<T> rai)
{
if (rai instanceof ImgPlus) return (ImgPlus<T>) rai;
return new ImgPlus<>(wrapToImg(rai));
}

private <T extends Type<T>> Img<T> wrapToImg(
final RandomAccessibleInterval<T> rai)
{
if (rai instanceof Img) return (Img<T>) rai;
return ImgView.wrap(rai, imgFactory(rai));
}

private <T> ImgFactory<T> imgFactory(
final RandomAccessibleInterval<T> rai)
{
final T type = Util.getTypeFromInterval(rai);
return Util.getSuitableImgFactory(rai, type);
}
}
31 changes: 31 additions & 0 deletions src/main/java/net/imagej/ImgPlus.java
Original file line number Diff line number Diff line change
Expand Up @@ -45,12 +45,16 @@
import net.imglib2.Interval;
import net.imglib2.Positionable;
import net.imglib2.RandomAccess;
import net.imglib2.RandomAccessibleInterval;
import net.imglib2.RealPositionable;
import net.imglib2.display.ColorTable;
import net.imglib2.img.Img;
import net.imglib2.img.ImgFactory;
import net.imglib2.img.ImgView;
import net.imglib2.img.WrappedImg;
import net.imglib2.img.array.ArrayImg;
import net.imglib2.type.Type;
import net.imglib2.util.Util;

/**
* A simple container for storing an {@link Img} together with its metadata.
Expand All @@ -68,6 +72,33 @@ public class ImgPlus<T> extends AbstractCalibratedRealInterval<CalibratedAxis>
implements Img<T>, WrappedImg<T>, ImgPlusMetadata
{

/** Wraps a {@link RandomAccessibleInterval} into an {@link ImgPlus}. */
public static <T extends Type<T>> ImgPlus<T> wrap(
final RandomAccessibleInterval<T> rai)
{
if (rai instanceof ImgPlus) return (ImgPlus<T>) rai;
return new ImgPlus<>(wrapToImg(rai));
}

/** Wraps a {@link RandomAccessibleInterval} into an {@link Img}. */
public static <T extends Type<T>> Img<T> wrapToImg(
final RandomAccessibleInterval<T> rai)
{
if (rai instanceof Img) return (Img<T>) rai;
return ImgView.wrap(rai, imgFactory(rai));
}

/**
* Gets an {@link ImgFactory} suitable for creating {@link Img}s of the given
* {@link RandomAccessibleInterval}'s container, type, and dimensions.
*/
public static <T> ImgFactory<T> imgFactory(
final RandomAccessibleInterval<T> rai)
{
final T type = Util.getTypeFromInterval(rai);
return Util.getSuitableImgFactory(rai, type);
}

private final Img<T> img;

private String name;
Expand Down

This file was deleted.

57 changes: 0 additions & 57 deletions src/main/java/net/imagej/convert/DatasetToImgConverter.java

This file was deleted.

57 changes: 0 additions & 57 deletions src/main/java/net/imagej/convert/DatasetToImgPlusConverter.java

This file was deleted.

Loading

0 comments on commit efa149b

Please sign in to comment.