|
| 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 | +} |
0 commit comments