|
| 1 | + |
| 2 | +package net.imagej.ops2.adapt; |
| 3 | + |
| 4 | +import java.util.function.Function; |
| 5 | + |
| 6 | +import org.scijava.function.Computers; |
| 7 | +import org.scijava.ops.spi.OpMethod; |
| 8 | + |
| 9 | +import net.imglib2.RandomAccessibleInterval; |
| 10 | +import net.imglib2.algorithm.neighborhood.Neighborhood; |
| 11 | +import net.imglib2.algorithm.neighborhood.Shape; |
| 12 | +import net.imglib2.loops.LoopBuilder; |
| 13 | +import net.imglib2.outofbounds.OutOfBoundsFactory; |
| 14 | +import net.imglib2.outofbounds.OutOfBoundsMirrorFactory; |
| 15 | +import net.imglib2.view.Views; |
| 16 | + |
| 17 | +public class LiftNeighborhoodComputersToImg { |
| 18 | + |
| 19 | + /** |
| 20 | + * @implNote op names='adapt', priority='100.', |
| 21 | + * type='java.util.function.Function' |
| 22 | + */ |
| 23 | + public static <T, U> |
| 24 | + Computers.Arity2<RandomAccessibleInterval<T>, Shape, RandomAccessibleInterval<U>> |
| 25 | + adapt1UsingShape(Computers.Arity1<Neighborhood<T>, U> op) |
| 26 | + { |
| 27 | + OutOfBoundsMirrorFactory<T, RandomAccessibleInterval<T>> oobf // |
| 28 | + = new OutOfBoundsMirrorFactory<>( |
| 29 | + OutOfBoundsMirrorFactory.Boundary.SINGLE); |
| 30 | + return (in, shape, out) -> { |
| 31 | + var extended = Views.extend(in, oobf); |
| 32 | + var neighborhoods = shape.neighborhoodsRandomAccessible(extended); |
| 33 | + var intervaled = Views.interval(neighborhoods, in); |
| 34 | + LoopBuilder.setImages(intervaled, out).forEachPixel(op); |
| 35 | + }; |
| 36 | + } |
| 37 | + |
| 38 | + /** |
| 39 | + * @implNote op names='adapt', priority='100.', |
| 40 | + * type='java.util.function.Function' |
| 41 | + */ |
| 42 | + @OpMethod(names = "adapt", type = Function.class) |
| 43 | + public static <T, U, F extends RandomAccessibleInterval<T>> |
| 44 | + Computers.Arity3<F, Shape, OutOfBoundsFactory<T, F>, RandomAccessibleInterval<U>> |
| 45 | + adapt1UsingShapeAndOOBF(Computers.Arity1<Neighborhood<T>, U> op) |
| 46 | + { |
| 47 | + return (in, shape, oobf, out) -> { |
| 48 | + var extended = Views.extend(in, oobf); |
| 49 | + var neighborhoods = shape.neighborhoodsRandomAccessible(extended); |
| 50 | + var intervaled = Views.interval(neighborhoods, in); |
| 51 | + LoopBuilder.setImages(intervaled, out).forEachPixel(op); |
| 52 | + }; |
| 53 | + } |
| 54 | + |
| 55 | + /** |
| 56 | + * @implNote op names='adapt', priority='100.', |
| 57 | + * type='java.util.function.Function' |
| 58 | + */ |
| 59 | + public static <T, U, V> |
| 60 | + Computers.Arity3<RandomAccessibleInterval<T>, V, Shape, RandomAccessibleInterval<U>> |
| 61 | + adapt2UsingShape(Computers.Arity2<Neighborhood<T>, V, U> op) |
| 62 | + { |
| 63 | + OutOfBoundsMirrorFactory<T, RandomAccessibleInterval<T>> oobf // |
| 64 | + = new OutOfBoundsMirrorFactory<>( |
| 65 | + OutOfBoundsMirrorFactory.Boundary.SINGLE); |
| 66 | + return (in1, in2, shape, out) -> { |
| 67 | + var extended = Views.extend(in1, oobf); |
| 68 | + var neighborhoods = shape.neighborhoodsRandomAccessible(extended); |
| 69 | + var intervaled = Views.interval(neighborhoods, in1); |
| 70 | + LoopBuilder.setImages(intervaled, out).forEachPixel((in, container) -> op |
| 71 | + .compute(in, in2, container)); |
| 72 | + }; |
| 73 | + } |
| 74 | + |
| 75 | + /** |
| 76 | + * @implNote op names='adapt', priority='100.', |
| 77 | + * type='java.util.function.Function' |
| 78 | + */ |
| 79 | + @OpMethod(names = "adapt", type = Function.class) |
| 80 | + public static <T, U, V, F extends RandomAccessibleInterval<T>> |
| 81 | + Computers.Arity4<F, V, Shape, OutOfBoundsFactory<T, F>, RandomAccessibleInterval<U>> |
| 82 | + adapt2UsingShapeAndOOBF(Computers.Arity2<Neighborhood<T>, V, U> op) |
| 83 | + { |
| 84 | + return (in1, in2, shape, oobf, out) -> { |
| 85 | + var extended = Views.extend(in1, oobf); |
| 86 | + var neighborhoods = shape.neighborhoodsRandomAccessible(extended); |
| 87 | + var intervaled = Views.interval(neighborhoods, in1); |
| 88 | + LoopBuilder.setImages(intervaled, out).forEachPixel((in, container) -> op |
| 89 | + .compute(in, in2, container)); |
| 90 | + }; |
| 91 | + } |
| 92 | + |
| 93 | + /** |
| 94 | + * @implNote op names='adapt', priority='100.', |
| 95 | + * type='java.util.function.Function' |
| 96 | + */ |
| 97 | + public static <T, U, V, W> |
| 98 | + Computers.Arity4<RandomAccessibleInterval<T>, V, W, Shape, RandomAccessibleInterval<U>> |
| 99 | + adapt3UsingShape(Computers.Arity3<Neighborhood<T>, V, W, U> op) |
| 100 | + { |
| 101 | + OutOfBoundsMirrorFactory<T, RandomAccessibleInterval<T>> oobf // |
| 102 | + = new OutOfBoundsMirrorFactory<>( |
| 103 | + OutOfBoundsMirrorFactory.Boundary.SINGLE); |
| 104 | + return (in1, in2, in3, shape, out) -> { |
| 105 | + var extended = Views.extend(in1, oobf); |
| 106 | + var neighborhoods = shape.neighborhoodsRandomAccessible(extended); |
| 107 | + var intervaled = Views.interval(neighborhoods, in1); |
| 108 | + LoopBuilder.setImages(intervaled, out).forEachPixel((in, container) -> op |
| 109 | + .compute(in, in2, in3, container)); |
| 110 | + }; |
| 111 | + } |
| 112 | + |
| 113 | + /** |
| 114 | + * @implNote op names='adapt', priority='100.', |
| 115 | + * type='java.util.function.Function' |
| 116 | + */ |
| 117 | + @OpMethod(names = "adapt", type = Function.class) |
| 118 | + public static <T, U, V, W, F extends RandomAccessibleInterval<T>> |
| 119 | + Computers.Arity5<F, V, W, Shape, OutOfBoundsFactory<T, F>, RandomAccessibleInterval<U>> |
| 120 | + adapt3UsingShapeAndOOBF(Computers.Arity3<Neighborhood<T>, V, W, U> op) |
| 121 | + { |
| 122 | + return (in1, in2, in3, shape, oobf, out) -> { |
| 123 | + var extended = Views.extend(in1, oobf); |
| 124 | + var neighborhoods = shape.neighborhoodsRandomAccessible(extended); |
| 125 | + var intervaled = Views.interval(neighborhoods, in1); |
| 126 | + LoopBuilder.setImages(intervaled, out).forEachPixel((in, container) -> op |
| 127 | + .compute(in, in2, in3, container)); |
| 128 | + }; |
| 129 | + } |
| 130 | +} |
0 commit comments