|
| 1 | +package net.imagej.ops2.copy; |
| 2 | + |
| 3 | +import net.imglib2.Dimensions; |
| 4 | +import net.imglib2.IterableInterval; |
| 5 | +import net.imglib2.RandomAccessibleInterval; |
| 6 | +import net.imglib2.img.array.ArrayImg; |
| 7 | +import net.imglib2.img.basictypeaccess.array.ArrayDataAccess; |
| 8 | +import net.imglib2.loops.LoopBuilder; |
| 9 | +import net.imglib2.roi.labeling.ImgLabeling; |
| 10 | +import net.imglib2.roi.labeling.LabelingMapping; |
| 11 | +import net.imglib2.type.NativeType; |
| 12 | +import net.imglib2.type.Type; |
| 13 | +import net.imglib2.type.numeric.IntegerType; |
| 14 | +import net.imglib2.util.Intervals; |
| 15 | +import net.imglib2.util.Util; |
| 16 | +import org.scijava.function.Computers; |
| 17 | +import org.scijava.ops.engine.util.Maps; |
| 18 | +import org.scijava.ops.spi.OpDependency; |
| 19 | + |
| 20 | +import java.lang.reflect.Array; |
| 21 | + |
| 22 | +public class Copiers { |
| 23 | + |
| 24 | + /** |
| 25 | + * Copy {@link Type} to another {@link Type} |
| 26 | + * |
| 27 | + * @author Christian Dietz (University of Konstanz) |
| 28 | + * @param <T> the {@link Type} of the objects involved |
| 29 | + * @param input the input {@link Type} |
| 30 | + * @param output the {@link Type} that will be filled with the value of {@code input} |
| 31 | + * @implNote op names='copy, copy.type', type='org.scijava.function.Computers$Arity1' |
| 32 | + */ |
| 33 | + public static <T extends Type<T>> void copyType(final T input, final T output) { |
| 34 | + output.set(input); |
| 35 | + } |
| 36 | + |
| 37 | + /** |
| 38 | + * Copies a {@link RandomAccessibleInterval} into another |
| 39 | + * {@link RandomAccessibleInterval} |
| 40 | + * |
| 41 | + * @param <T> the element type of each image |
| 42 | + * @param input the {@link RandomAccessibleInterval} whose data will be copied |
| 43 | + * @param copy the {@link RandomAccessibleInterval} that will be filled with the contents of {@code input} |
| 44 | + * @author Christian Dietz (University of Konstanz) |
| 45 | + * @implNote op names='copy, copy.rai, copy.img', priority='10.0', type='org.scijava.function.Computers$Arity1' |
| 46 | + */ |
| 47 | + public static <T> void copyRAI( // |
| 48 | + final @OpDependency(name = "copy") Computers.Arity1<T, T> copier, // |
| 49 | + final RandomAccessibleInterval<T> input, // |
| 50 | + final RandomAccessibleInterval<T> copy // |
| 51 | + ) { |
| 52 | + ensureEqualDimensions(input, copy); |
| 53 | + LoopBuilder.setImages(input, copy).forEachPixel(copier::compute); |
| 54 | + } |
| 55 | + |
| 56 | + /** |
| 57 | + * Copying {@link ImgLabeling} into another {@link ImgLabeling} |
| 58 | + * |
| 59 | + * @param <T> the element type of each image |
| 60 | + * @param raiCopier an Op that can copy {@link RandomAccessibleInterval}s |
| 61 | + * @param mappingCopier an Op that can copy {@link LabelingMapping}s |
| 62 | + * @param input the {@link ImgLabeling} to copy |
| 63 | + * @param output the destination container of the copy operation |
| 64 | + * @author Christian Dietz (University of Konstanz) |
| 65 | + * @implNote op names='copy, copy.imgLabeling', type='org.scijava.function.Computers$Arity1' |
| 66 | + */ |
| 67 | + public static <T extends IntegerType<T> & NativeType<T>, L> void copyImgLabeling( // |
| 68 | + final @OpDependency(name = "copy") Computers.Arity1<RandomAccessibleInterval<T>, RandomAccessibleInterval<T>> raiCopier, // |
| 69 | + final @OpDependency(name = "copy") Computers.Arity1<LabelingMapping<L>, LabelingMapping<L>> mappingCopier, // |
| 70 | + final ImgLabeling<L, T> input, // |
| 71 | + final ImgLabeling<L, T> output // |
| 72 | + ) { |
| 73 | + ensureEqualDimensions(input, output); |
| 74 | + if (Util.getTypeFromInterval(input.getIndexImg()).getClass() != Util.getTypeFromInterval(output.getIndexImg()) |
| 75 | + .getClass()) |
| 76 | + throw new IllegalArgumentException("input and output index images must be of the same type!"); |
| 77 | + raiCopier.compute(input.getIndexImg(), output.getIndexImg()); |
| 78 | + mappingCopier.compute(input.getMapping(), output.getMapping()); |
| 79 | + } |
| 80 | + |
| 81 | + /** |
| 82 | + * Copies a {@link LabelingMapping} into another {@link LabelingMapping} |
| 83 | + * |
| 84 | + * @author Christian Dietz (University of Konstanz) |
| 85 | + * @param <L> the type of the {@link LabelingMapping} elements |
| 86 | + * @param input the {@link LabelingMapping} to copy |
| 87 | + * @param output the destination container of the copy operation |
| 88 | + * @implNote op names='copy, copy.labelingMapping', priority='10000.', type='org.scijava.function.Computers$Arity1' |
| 89 | + */ |
| 90 | + public static <L> void copyLabelingMapping(final LabelingMapping<L> input, final LabelingMapping<L> output) { |
| 91 | + output.setLabelSets(input.getLabelSets()); |
| 92 | + } |
| 93 | + |
| 94 | + /** |
| 95 | + * Copying {@link ArrayImg} into another {@link ArrayImg} |
| 96 | + * |
| 97 | + * @author Christian Dietz (University of Konstanz) |
| 98 | + * @param <T> the type of the elements of each image |
| 99 | + * @param <A> the type of the backing data storage for each image |
| 100 | + * @param input the {@link ArrayImg} to copy |
| 101 | + * @param output the destination container of the copy operation |
| 102 | + * @implNote op names='copy, copy.img', priority='10000.', type='org.scijava.function.Computers$Arity1' |
| 103 | + */ |
| 104 | + public static <T extends NativeType<T>, A extends ArrayDataAccess<A>> void copyArrayImage( // |
| 105 | + final ArrayImg<T, A> input, |
| 106 | + final ArrayImg<T, A> output |
| 107 | + ) { |
| 108 | + ensureEqualDimensions(input, output); |
| 109 | + |
| 110 | + final Object inArray = input.update(null).getCurrentStorageArray(); |
| 111 | + final Object outArray = output.update(null).getCurrentStorageArray(); |
| 112 | + System.arraycopy(inArray, 0, outArray, 0, Array.getLength(inArray)); |
| 113 | + } |
| 114 | + |
| 115 | + |
| 116 | + /** |
| 117 | + * Copies an {@link IterableInterval} into another {@link IterableInterval} |
| 118 | + * TODO: Can we delete this in favor of some lifting operation? |
| 119 | + * |
| 120 | + * @author Christian Dietz (University of Konstanz) |
| 121 | + * @param <T> the element type of the {@link IterableInterval}s |
| 122 | + * @param copier an Op responsible for copying element types |
| 123 | + * @param input the {@link IterableInterval} to copy |
| 124 | + * @param output the destination container of the copy operation |
| 125 | + * @implNote op names='copy, copy.iterableInterval, copy.img', priority='1.0' |
| 126 | + */ |
| 127 | + public static <T> void copyIterableInterval( // |
| 128 | + @OpDependency(name = "copy.type") final Computers.Arity1<T, T> copier, // |
| 129 | + final IterableInterval<T> input, // |
| 130 | + final IterableInterval<T> output |
| 131 | + ) { |
| 132 | + if (!input.iterationOrder().equals(output.iterationOrder())) |
| 133 | + throw new IllegalArgumentException("input and output must be of the same dimensions!"); |
| 134 | + Computers.Arity1<Iterable<T>, Iterable<T>> mapped = Maps.ComputerMaps.Iterables.liftBoth(copier); |
| 135 | + mapped.compute(input, output); |
| 136 | + } |
| 137 | + |
| 138 | + private static void ensureEqualDimensions(Dimensions d1, Dimensions d2) { |
| 139 | + if (!Intervals.equalDimensions(d1, d2)) |
| 140 | + throw new IllegalArgumentException("The Dimensions of the input and copy images must be the same!"); |
| 141 | + } |
| 142 | +} |
0 commit comments