Skip to content

Commit 9ac2368

Browse files
gselzerwiedenm
authored andcommitted
WIP: Copy Ops and other stuff
1 parent ac24014 commit 9ac2368

23 files changed

Lines changed: 2321 additions & 0 deletions
Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
/*
2+
* #%L
3+
* ImageJ software for multidimensional image processing and analysis.
4+
* %%
5+
* Copyright (C) 2014 - 2018 ImageJ developers.
6+
* %%
7+
* Redistribution and use in source and binary forms, with or without
8+
* modification, are permitted provided that the following conditions are met:
9+
*
10+
* 1. Redistributions of source code must retain the above copyright notice,
11+
* this list of conditions and the following disclaimer.
12+
* 2. Redistributions in binary form must reproduce the above copyright notice,
13+
* this list of conditions and the following disclaimer in the documentation
14+
* and/or other materials provided with the distribution.
15+
*
16+
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
17+
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18+
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19+
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR CONTRIBUTORS BE
20+
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
21+
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
22+
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
23+
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
24+
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
25+
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
26+
* POSSIBILITY OF SUCH DAMAGE.
27+
* #L%
28+
*/
29+
30+
package net.imagej.ops.copy;
31+
32+
import java.lang.reflect.Array;
33+
import java.util.function.Function;
34+
35+
import net.imglib2.img.array.ArrayImg;
36+
import net.imglib2.img.basictypeaccess.array.ArrayDataAccess;
37+
import net.imglib2.type.NativeType;
38+
import net.imglib2.util.Intervals;
39+
40+
import org.scijava.Priority;
41+
import org.scijava.ops.OpDependency;
42+
import org.scijava.ops.core.Op;
43+
import org.scijava.ops.core.computer.Computer;
44+
import org.scijava.param.Parameter;
45+
import org.scijava.plugin.Plugin;
46+
import org.scijava.struct.ItemIO;
47+
48+
/**
49+
* Copying {@link ArrayImg} into another {@link ArrayImg}
50+
*
51+
* @author Christian Dietz (University of Konstanz)
52+
* @param <T>
53+
*/
54+
@Plugin(type = Op.class, name = "copy.img", priority = Priority.VERY_HIGH)
55+
@Parameter(key = "input")
56+
@Parameter(key = "copy", type = ItemIO.BOTH)
57+
public class CopyArrayImg<T extends NativeType<T>, A extends ArrayDataAccess<A>>
58+
implements Computer<ArrayImg<T, A>, ArrayImg<T, A>> {
59+
@Override
60+
public void compute(final ArrayImg<T, A> input, final ArrayImg<T, A> output) {
61+
62+
if (!Intervals.equalDimensions(input, output))
63+
throw new IllegalArgumentException("The Dimensions of the input and copy images must be the same!");
64+
65+
final Object inArray = input.update(null).getCurrentStorageArray();
66+
System.arraycopy(inArray, 0, output.update(null).getCurrentStorageArray(), 0, Array.getLength(inArray));
67+
68+
}
69+
}
70+
71+
@Plugin(type = Op.class, name = "copy.img", priority = Priority.VERY_HIGH)
72+
@Parameter(key = "input")
73+
@Parameter(key = "copy", type = ItemIO.OUTPUT)
74+
class CopyArrayImgFunction<T extends NativeType<T>, A extends ArrayDataAccess<A>>
75+
implements Function<ArrayImg<T, A>, ArrayImg<T, A>> {
76+
77+
@OpDependency(name = "copy.img")
78+
private Computer<ArrayImg<T, A>, ArrayImg<T, A>> copyOp;
79+
80+
@Override
81+
public ArrayImg<T, A> apply(ArrayImg<T, A> input) {
82+
// NB: Workaround for ArrayImgFactory not overriding create(Dimensions, T).
83+
final long[] dims = new long[input.numDimensions()];
84+
input.dimensions(dims);
85+
final ArrayImg<T, ?> copy = input.factory().create(dims, input.firstElement().createVariable());
86+
87+
// TODO: Find a way to guarantee the type.
88+
@SuppressWarnings("unchecked")
89+
final ArrayImg<T, A> typedCopy = (ArrayImg<T, A>) copy;
90+
copyOp.compute(input, typedCopy);
91+
92+
return typedCopy;
93+
}
94+
95+
}
Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
/*
2+
* #%L
3+
* ImageJ software for multidimensional image processing and analysis.
4+
* %%
5+
* Copyright (C) 2014 - 2018 ImageJ developers.
6+
* %%
7+
* Redistribution and use in source and binary forms, with or without
8+
* modification, are permitted provided that the following conditions are met:
9+
*
10+
* 1. Redistributions of source code must retain the above copyright notice,
11+
* this list of conditions and the following disclaimer.
12+
* 2. Redistributions in binary form must reproduce the above copyright notice,
13+
* this list of conditions and the following disclaimer in the documentation
14+
* and/or other materials provided with the distribution.
15+
*
16+
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
17+
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18+
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19+
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR CONTRIBUTORS BE
20+
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
21+
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
22+
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
23+
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
24+
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
25+
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
26+
* POSSIBILITY OF SUCH DAMAGE.
27+
* #L%
28+
*/
29+
30+
package net.imagej.ops.copy;
31+
32+
import java.util.function.BiFunction;
33+
import java.util.function.Function;
34+
35+
import net.imglib2.Dimensions;
36+
import net.imglib2.IterableInterval;
37+
38+
import org.scijava.ops.OpDependency;
39+
import org.scijava.ops.core.Op;
40+
import org.scijava.ops.core.computer.Computer;
41+
import org.scijava.ops.util.Maps;
42+
import org.scijava.param.Parameter;
43+
import org.scijava.plugin.Plugin;
44+
import org.scijava.struct.ItemIO;
45+
46+
/**
47+
* Copies an {@link IterableInterval} into another {@link IterableInterval}
48+
*
49+
* @author Christian Dietz (University of Konstanz)
50+
* @param <T>
51+
*/
52+
@Plugin(type = Op.class, name = "copy.iterableInterval", priority = 1.0)
53+
@Parameter(key = "input")
54+
@Parameter(key = "output", type = ItemIO.BOTH)
55+
public class CopyII<T> implements Computer<IterableInterval<T>, IterableInterval<T>> {
56+
57+
// used internally
58+
//TODO: make sure that the matcher autoLifts the type copying Op
59+
@OpDependency(name = "copy.type")
60+
private Computer<T, T> copyOp;
61+
62+
@Override
63+
public void compute(final IterableInterval<T> input, final IterableInterval<T> output) {
64+
if (!input.iterationOrder().equals(output.iterationOrder()))
65+
throw new IllegalArgumentException("input and output must be of the same dimensions!");
66+
Computer<Iterable<T>, Iterable<T>> mapped = Maps.Computers.Iterables.liftBoth(copyOp);
67+
mapped.compute(input, output);
68+
}
69+
}
70+
71+
@Plugin(type = Op.class, name = "copy.iterableInterval", priority = 1.0)
72+
@Parameter(key = "input")
73+
@Parameter(key = "output", type = ItemIO.OUTPUT)
74+
class CopyIIFunction<T> implements Function<IterableInterval<T>, IterableInterval<T>> {
75+
76+
@OpDependency(name = "create.img")
77+
private BiFunction<Dimensions, T, IterableInterval<T>> imgCreator;
78+
@OpDependency(name = "copy.iterableInterval")
79+
private Computer<IterableInterval<T>, IterableInterval<T>> copyOp;
80+
81+
@Override
82+
public IterableInterval<T> apply(IterableInterval<T> input) {
83+
IterableInterval<T> output = imgCreator.apply(input, input.firstElement());
84+
copyOp.compute(input, output);
85+
return output;
86+
}
87+
88+
}
Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
/*
2+
* #%L
3+
* ImageJ software for multidimensional image processing and analysis.
4+
* %%
5+
* Copyright (C) 2014 - 2018 ImageJ developers.
6+
* %%
7+
* Redistribution and use in source and binary forms, with or without
8+
* modification, are permitted provided that the following conditions are met:
9+
*
10+
* 1. Redistributions of source code must retain the above copyright notice,
11+
* this list of conditions and the following disclaimer.
12+
* 2. Redistributions in binary form must reproduce the above copyright notice,
13+
* this list of conditions and the following disclaimer in the documentation
14+
* and/or other materials provided with the distribution.
15+
*
16+
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
17+
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18+
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19+
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR CONTRIBUTORS BE
20+
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
21+
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
22+
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
23+
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
24+
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
25+
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
26+
* POSSIBILITY OF SUCH DAMAGE.
27+
* #L%
28+
*/
29+
30+
package net.imagej.ops.copy;
31+
32+
import java.util.function.Function;
33+
34+
import net.imglib2.img.Img;
35+
import net.imglib2.type.NativeType;
36+
37+
import org.scijava.ops.OpDependency;
38+
import org.scijava.ops.core.Op;
39+
import org.scijava.ops.core.computer.Computer;
40+
import org.scijava.param.Parameter;
41+
import org.scijava.plugin.Plugin;
42+
import org.scijava.struct.ItemIO;
43+
44+
/**
45+
* Copying {@link Img} into another {@link Img}. Exists mainly for convenience
46+
* reasons.
47+
*
48+
* @author Christian Dietz (University of Konstanz)
49+
* @param <T>
50+
*/
51+
@Plugin(type = Op.class, name = "copy.img")
52+
@Parameter(key = "input")
53+
@Parameter(key = "output", type = ItemIO.BOTH)
54+
public class CopyImg<T extends NativeType<T>> implements Computer<Img<T>, Img<T>> {
55+
56+
@OpDependency(name = "copy.iterableInterval")
57+
private Computer<Iterable<T>, Iterable<T>> copyComputer;
58+
59+
@Override
60+
public void compute(final Img<T> input, final Img<T> output) {
61+
if (!input.iterationOrder().equals(output.iterationOrder()))
62+
throw new IllegalArgumentException("input and output must have the same iteration order!");
63+
copyComputer.compute(input, output);
64+
}
65+
}
66+
67+
@Plugin(type = Op.class, name = "copy.img")
68+
@Parameter(key = "input")
69+
@Parameter(key = "output", type = ItemIO.OUTPUT)
70+
class CopyImgFunction<T extends NativeType<T>> implements Function<Img<T>, Img<T>> {
71+
72+
@OpDependency(name = "copy.img")
73+
private Computer<Img<T>, Img<T>> copyComputer;
74+
75+
@OpDependency(name = "create.img")
76+
private Function<Img<T>, Img<T>> createFunc;
77+
78+
@Override
79+
public Img<T> apply(Img<T> input) {
80+
Img<T> output = createFunc.apply(input);
81+
copyComputer.compute(input, output);
82+
return output;
83+
}
84+
85+
}
Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
1+
/*
2+
* #%L
3+
* ImageJ software for multidimensional image processing and analysis.
4+
* %%
5+
* Copyright (C) 2014 - 2018 ImageJ developers.
6+
* %%
7+
* Redistribution and use in source and binary forms, with or without
8+
* modification, are permitted provided that the following conditions are met:
9+
*
10+
* 1. Redistributions of source code must retain the above copyright notice,
11+
* this list of conditions and the following disclaimer.
12+
* 2. Redistributions in binary form must reproduce the above copyright notice,
13+
* this list of conditions and the following disclaimer in the documentation
14+
* and/or other materials provided with the distribution.
15+
*
16+
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
17+
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18+
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19+
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR CONTRIBUTORS BE
20+
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
21+
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
22+
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
23+
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
24+
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
25+
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
26+
* POSSIBILITY OF SUCH DAMAGE.
27+
* #L%
28+
*/
29+
30+
package net.imagej.ops.copy;
31+
32+
import net.imagej.ops.Contingent;
33+
import net.imagej.ops.Ops;
34+
import net.imagej.ops.special.computer.Computers;
35+
import net.imagej.ops.special.computer.UnaryComputerOp;
36+
import net.imagej.ops.special.function.Functions;
37+
import net.imagej.ops.special.function.UnaryFunctionOp;
38+
import net.imagej.ops.special.hybrid.AbstractUnaryHybridCF;
39+
import net.imglib2.RandomAccessibleInterval;
40+
import net.imglib2.roi.labeling.ImgLabeling;
41+
import net.imglib2.roi.labeling.LabelingMapping;
42+
import net.imglib2.type.NativeType;
43+
import net.imglib2.type.numeric.IntegerType;
44+
import net.imglib2.util.Intervals;
45+
import net.imglib2.util.Util;
46+
47+
import org.scijava.plugin.Plugin;
48+
49+
/**
50+
* Copying {@link ImgLabeling} into another {@link ImgLabeling}
51+
*
52+
* @author Christian Dietz (University of Konstanz)
53+
* @param <T>
54+
*/
55+
@Plugin(type = Ops.Copy.ImgLabeling.class)
56+
public class CopyImgLabeling<T extends IntegerType<T> & NativeType<T>, L>
57+
extends AbstractUnaryHybridCF<ImgLabeling<L, T>, ImgLabeling<L, T>>
58+
implements Ops.Copy.ImgLabeling, Contingent {
59+
60+
61+
private UnaryComputerOp<RandomAccessibleInterval<T>, RandomAccessibleInterval<T>> raiCopyOp;
62+
private UnaryComputerOp<LabelingMapping<L>, LabelingMapping<L>> mappingCopyOp;
63+
private UnaryFunctionOp<ImgLabeling<L, T>, ImgLabeling<L, T>> outputCreator;
64+
65+
66+
@SuppressWarnings({ "rawtypes", "unchecked" })
67+
@Override
68+
public void initialize() {
69+
raiCopyOp = Computers.unary(ops(), Ops.Copy.RAI.class, in().getIndexImg() ,in().getIndexImg());
70+
mappingCopyOp = Computers.unary(ops(), Ops.Copy.LabelingMapping.class, in().getMapping(), in().getMapping());
71+
outputCreator = (UnaryFunctionOp) Functions.unary(ops(), Ops.Create.ImgLabeling.class, ImgLabeling.class, in());
72+
}
73+
74+
@Override
75+
public ImgLabeling<L, T> createOutput(final ImgLabeling<L, T> input) {
76+
return outputCreator.calculate(input);
77+
}
78+
79+
80+
@Override
81+
public void compute(final ImgLabeling<L, T> input,
82+
final ImgLabeling<L, T> output) {
83+
raiCopyOp.compute(input.getIndexImg(), output.getIndexImg());
84+
mappingCopyOp.compute(input.getMapping(), output.getMapping());
85+
}
86+
87+
@Override
88+
public boolean conforms() {
89+
if (out() != null) {
90+
return Intervals.equalDimensions(in(), out())
91+
&& Util.getTypeFromInterval(in().getIndexImg()).getClass() == Util
92+
.getTypeFromInterval(out().getIndexImg())
93+
.getClass();
94+
}
95+
96+
return true;
97+
}
98+
99+
}

0 commit comments

Comments
 (0)