|
| 1 | +package net.imagej.ops.types.transform.util; |
| 2 | + |
| 3 | +import java.util.function.Function; |
| 4 | + |
| 5 | +import net.imglib2.Cursor; |
| 6 | +import net.imglib2.RandomAccessibleInterval; |
| 7 | +import net.imglib2.converter.Converter; |
| 8 | +import net.imglib2.converter.read.ConvertedRandomAccessibleInterval; |
| 9 | +import net.imglib2.img.Img; |
| 10 | +import net.imglib2.type.NativeType; |
| 11 | +import net.imglib2.type.Type; |
| 12 | +import net.imglib2.util.Util; |
| 13 | +import net.imglib2.view.Views; |
| 14 | + |
| 15 | +import org.scijava.ops.core.computer.BiComputer; |
| 16 | +import org.scijava.ops.core.computer.Computer; |
| 17 | + |
| 18 | +public class Maps { |
| 19 | + |
| 20 | + private Maps() { |
| 21 | + // NB: Prevent instantiation of utility class. |
| 22 | + } |
| 23 | + |
| 24 | + public interface Functions { |
| 25 | + |
| 26 | + public interface RAIs { |
| 27 | + |
| 28 | + public static <I, O extends Type<O>> Function<RandomAccessibleInterval<I>, RandomAccessibleInterval<O>> liftBoth(final Function<I, O> function) { |
| 29 | + return in -> { |
| 30 | + O outType = function.apply(Util.getTypeFromInterval(in)); |
| 31 | + Img<O> outImg = Util.getSuitableImgFactory(in, outType).create(in); |
| 32 | + Cursor<I> inCursor = Views.flatIterable(in).cursor(); |
| 33 | + Cursor<O> outCursor = Views.flatIterable(outImg).cursor(); |
| 34 | + while(inCursor.hasNext()) { |
| 35 | + outCursor.next().set(function.apply(inCursor.next())); |
| 36 | + } |
| 37 | + return outImg; |
| 38 | + }; |
| 39 | + } |
| 40 | + |
| 41 | + public static <I extends NativeType<I>, O extends NativeType<O>> Function<RandomAccessibleInterval<I>, RandomAccessibleInterval<O>> liftBothNativesView(final Function<I, O> function) { |
| 42 | + return in -> { |
| 43 | + O outType = function.apply(Util.getTypeFromInterval(in)); |
| 44 | + Converter<I, O> converter = new Converter<I, O>(){ |
| 45 | + @Override |
| 46 | + public void convert(I input, O output) { |
| 47 | + output.set(function.apply(input)); |
| 48 | + } |
| 49 | + }; |
| 50 | + return new ConvertedRandomAccessibleInterval<>(in, converter, outType); |
| 51 | + }; |
| 52 | + } |
| 53 | + |
| 54 | + |
| 55 | + |
| 56 | + } |
| 57 | + } |
| 58 | + |
| 59 | + public interface Computers { |
| 60 | + |
| 61 | + public interface RAIs { |
| 62 | + |
| 63 | + public static <I, O> Computer<RandomAccessibleInterval<I>, RandomAccessibleInterval<O>> liftBoth(final Computer<I, O> computer) { |
| 64 | + return (raiInput, raiOutput) -> { |
| 65 | + Cursor<I> inCursor = Views.flatIterable(raiInput).cursor(); |
| 66 | + Cursor<O> outCursor = Views.flatIterable(raiOutput).cursor(); |
| 67 | + while(inCursor.hasNext() && outCursor.hasNext()) { |
| 68 | + computer.compute(inCursor.next(), outCursor.next()); |
| 69 | + } |
| 70 | + }; |
| 71 | + } |
| 72 | + |
| 73 | + public static <I1, I2, O> BiComputer<RandomAccessibleInterval<I1>, RandomAccessibleInterval<I2>, RandomAccessibleInterval<O>> liftBoth(final BiComputer<I1, I2, O> computer) { |
| 74 | + return (raiInput1, raiInput2, raiOutput) -> { |
| 75 | + Cursor<I1> inCursor1 = Views.flatIterable(raiInput1).cursor(); |
| 76 | + Cursor<I2> inCursor2 = Views.flatIterable(raiInput2).cursor(); |
| 77 | + Cursor<O> outCursor = Views.flatIterable(raiOutput).cursor(); |
| 78 | + while(inCursor1.hasNext() && inCursor2.hasNext() && outCursor.hasNext()) { |
| 79 | + computer.compute(inCursor1.next(), inCursor2.next(), outCursor.next()); |
| 80 | + } |
| 81 | + }; |
| 82 | + } |
| 83 | + } |
| 84 | + } |
| 85 | +} |
0 commit comments