Skip to content

Commit b8c4f46

Browse files
committed
Add imglib-specific lifters
TODO: move commit before dependent tests
1 parent a46cb81 commit b8c4f46

3 files changed

Lines changed: 177 additions & 0 deletions

File tree

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
package net.imagej.ops.types.transform;
2+
3+
import net.imagej.ops.types.transform.util.Maps;
4+
import net.imglib2.RandomAccessibleInterval;
5+
6+
import org.scijava.ops.OpService;
7+
import org.scijava.ops.core.computer.BiComputer;
8+
import org.scijava.ops.matcher.OpRef;
9+
import org.scijava.ops.transform.OpMapper;
10+
import org.scijava.ops.transform.OpRefTransformUtils;
11+
import org.scijava.ops.transform.OpTransformer;
12+
import org.scijava.plugin.Plugin;
13+
14+
/**
15+
* @author David Kolb
16+
*/
17+
@Plugin(type = OpTransformer.class)
18+
public class LiftBiComputerToRAITransformer<I1, I2, O> implements
19+
OpMapper<BiComputer<I1, I2, O>, BiComputer<RandomAccessibleInterval<I1>, RandomAccessibleInterval<I2>, RandomAccessibleInterval<O>>>
20+
{
21+
22+
@Override
23+
@SuppressWarnings({ "unchecked", "rawtypes" })
24+
public Class<BiComputer<I1, I2, O>> srcClass() {
25+
return (Class) BiComputer.class;
26+
}
27+
28+
@Override
29+
@SuppressWarnings({ "unchecked", "rawtypes" })
30+
public Class<BiComputer<RandomAccessibleInterval<I1>, RandomAccessibleInterval<I2>, RandomAccessibleInterval<O>>> targetClass() {
31+
return (Class) BiComputer.class;
32+
}
33+
34+
@Override
35+
public BiComputer<RandomAccessibleInterval<I1>, RandomAccessibleInterval<I2>, RandomAccessibleInterval<O>> transformTypesafe(final OpService opService, final BiComputer<I1, I2, O> src,
36+
final OpRef targetRef)
37+
{
38+
return Maps.Computers.RAIs.liftBoth(src);
39+
}
40+
41+
@Override
42+
public OpRef getRefTransformingTo(final OpRef targetRef) {
43+
return OpRefTransformUtils.unliftTransform(targetRef, BiComputer.class, RandomAccessibleInterval.class, new Integer[] {0, 1, 2},
44+
new Integer[] { 0, 1, 2 }, new Integer[] { 0 });
45+
}
46+
}
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
package net.imagej.ops.types.transform;
2+
3+
import net.imagej.ops.types.transform.util.Maps;
4+
import net.imglib2.RandomAccessibleInterval;
5+
6+
import org.scijava.ops.OpService;
7+
import org.scijava.ops.core.computer.Computer;
8+
import org.scijava.ops.matcher.OpRef;
9+
import org.scijava.ops.transform.OpMapper;
10+
import org.scijava.ops.transform.OpRefTransformUtils;
11+
import org.scijava.ops.transform.OpTransformer;
12+
import org.scijava.plugin.Plugin;
13+
14+
/**
15+
* @author David Kolb
16+
*/
17+
@Plugin(type = OpTransformer.class)
18+
public class LiftComputerToRAITransformer<I, O> implements
19+
OpMapper<Computer<I, O>, Computer<RandomAccessibleInterval<I>, RandomAccessibleInterval<O>>>
20+
{
21+
22+
@Override
23+
@SuppressWarnings({ "unchecked", "rawtypes" })
24+
public Class<Computer<I, O>> srcClass() {
25+
return (Class) Computer.class;
26+
}
27+
28+
@Override
29+
@SuppressWarnings({ "unchecked", "rawtypes" })
30+
public Class<Computer<RandomAccessibleInterval<I>, RandomAccessibleInterval<O>>> targetClass() {
31+
return (Class) Computer.class;
32+
}
33+
34+
@Override
35+
public Computer<RandomAccessibleInterval<I>, RandomAccessibleInterval<O>> transformTypesafe(final OpService opService, final Computer<I, O> src,
36+
final OpRef targetRef)
37+
{
38+
return Maps.Computers.RAIs.liftBoth(src);
39+
}
40+
41+
@Override
42+
public OpRef getRefTransformingTo(final OpRef targetRef) {
43+
return OpRefTransformUtils.unliftTransform(targetRef, Computer.class, RandomAccessibleInterval.class, new Integer[] {0, 1},
44+
new Integer[] { 0, 1 }, new Integer[] { 0 });
45+
}
46+
}
Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
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

Comments
 (0)