diff --git a/imagej/imagej-ops2/src/main/java/net/imagej/ops2/copy/CopyII.java b/imagej/imagej-ops2/src/main/java/net/imagej/ops2/copy/CopyII.java index e4d7f6157..8412c497a 100644 --- a/imagej/imagej-ops2/src/main/java/net/imagej/ops2/copy/CopyII.java +++ b/imagej/imagej-ops2/src/main/java/net/imagej/ops2/copy/CopyII.java @@ -44,7 +44,7 @@ * * @author Christian Dietz (University of Konstanz) * @param - * @implNote op names='copy, copy.iterableInterval', priority='1.0' + * @implNote op names='copy, copy.iterableInterval, copy.img', priority='1.0' */ public class CopyII implements Computers.Arity1, IterableInterval> { diff --git a/imagej/imagej-ops2/src/main/java/net/imagej/ops2/copy/CopyRAI.java b/imagej/imagej-ops2/src/main/java/net/imagej/ops2/copy/CopyRAI.java index 01f68c83a..9bff497a9 100644 --- a/imagej/imagej-ops2/src/main/java/net/imagej/ops2/copy/CopyRAI.java +++ b/imagej/imagej-ops2/src/main/java/net/imagej/ops2/copy/CopyRAI.java @@ -42,7 +42,7 @@ * * @author Christian Dietz (University of Konstanz) * @param - * @implNote op names='copy, copy.rai', priority='1.0' + * @implNote op names='copy, copy.rai, copy.img', priority='10.0' */ public class CopyRAI implements Computers.Arity1, RandomAccessibleInterval> { diff --git a/imagej/imagej-ops2/src/test/java/net/imagej/ops2/OpRegressionTest.java b/imagej/imagej-ops2/src/test/java/net/imagej/ops2/OpRegressionTest.java index 3630c20cd..c80c609d4 100644 --- a/imagej/imagej-ops2/src/test/java/net/imagej/ops2/OpRegressionTest.java +++ b/imagej/imagej-ops2/src/test/java/net/imagej/ops2/OpRegressionTest.java @@ -39,7 +39,7 @@ public class OpRegressionTest extends AbstractOpTest { @Test public void opDiscoveryRegressionIT() { - long expected = 1444; + long expected = 1457; long actual = StreamSupport.stream(ops.infos().spliterator(), false).count(); assertEquals(expected, actual); } diff --git a/imagej/imagej-ops2/src/test/java/net/imagej/ops2/copy/CopyArrayImgTest.java b/imagej/imagej-ops2/src/test/java/net/imagej/ops2/copy/CopyArrayImgTest.java deleted file mode 100644 index 206d8103f..000000000 --- a/imagej/imagej-ops2/src/test/java/net/imagej/ops2/copy/CopyArrayImgTest.java +++ /dev/null @@ -1,98 +0,0 @@ -/* - * #%L - * ImageJ2 software for multidimensional image processing and analysis. - * %% - * Copyright (C) 2014 - 2022 ImageJ2 developers. - * %% - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions are met: - * - * 1. Redistributions of source code must retain the above copyright notice, - * this list of conditions and the following disclaimer. - * 2. Redistributions in binary form must reproduce the above copyright notice, - * this list of conditions and the following disclaimer in the documentation - * and/or other materials provided with the distribution. - * - * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" - * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE - * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE - * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR CONTRIBUTORS BE - * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR - * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF - * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS - * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN - * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) - * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE - * POSSIBILITY OF SUCH DAMAGE. - * #L% - */ -package net.imagej.ops2.copy; - -import static org.junit.jupiter.api.Assertions.assertEquals; -import static org.junit.jupiter.api.Assertions.assertTrue; - -import net.imagej.ops2.AbstractOpTest; -import net.imglib2.Cursor; -import net.imglib2.RandomAccess; -import net.imglib2.RandomAccessibleInterval; -import net.imglib2.img.Img; -import net.imglib2.img.array.ArrayImgFactory; -import net.imglib2.type.numeric.integer.UnsignedByteType; - -import org.junit.jupiter.api.BeforeEach; -import org.junit.jupiter.api.Test; -import org.scijava.util.MersenneTwisterFast; - -/** - * Test {@link CopyArrayImg} - * - * @author Christian Dietz (University of Konstanz) - */ -public class CopyArrayImgTest extends AbstractOpTest { - - private Img input; - - @BeforeEach - public void createData() { - input = new ArrayImgFactory<>(new UnsignedByteType()).create(new int[] { 120, 100 }); - - final MersenneTwisterFast r = new MersenneTwisterFast(System.currentTimeMillis()); - - final Cursor inc = input.cursor(); - - while (inc.hasNext()) { - inc.next().setReal(r.nextDouble() * 255); - } - } - - @Test - public void copyArrayImgNoOutputTest() { - @SuppressWarnings("unchecked") - final RandomAccessibleInterval output = - (RandomAccessibleInterval) ops.op("copy.img").arity1().input( - input).apply(); - - final Cursor inc = input.localizingCursor(); - final RandomAccess outRA = output.randomAccess(); - - while (inc.hasNext()) { - inc.fwd(); - outRA.setPosition(inc); - assertEquals(inc.get().get(), outRA.get().get()); - } - } - - @Test - public void copyArrayImgWithOutputTest() { - final Img output = input.factory().create(input, input.firstElement()); - - ops.op("copy.img").arity1().input(input).output(output).compute(); - - final Cursor inc = input.cursor(); - final Cursor outc = output.cursor(); - - while (inc.hasNext()) { - assertTrue(outc.next().equals(inc.next())); - } - } -} diff --git a/imagej/imagej-ops2/src/test/java/net/imagej/ops2/copy/CopyImgsTest.java b/imagej/imagej-ops2/src/test/java/net/imagej/ops2/copy/CopyImgsTest.java new file mode 100644 index 000000000..8418a851b --- /dev/null +++ b/imagej/imagej-ops2/src/test/java/net/imagej/ops2/copy/CopyImgsTest.java @@ -0,0 +1,166 @@ +/* + * #%L + * ImageJ2 software for multidimensional image processing and analysis. + * %% + * Copyright (C) 2014 - 2022 ImageJ2 developers. + * %% + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, + * this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following disclaimer in the documentation + * and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR CONTRIBUTORS BE + * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF + * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS + * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN + * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE + * POSSIBILITY OF SUCH DAMAGE. + * #L% + */ +package net.imagej.ops2.copy; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +import org.junit.jupiter.api.Test; +import org.scijava.util.MersenneTwisterFast; + +import net.imagej.ops2.AbstractOpTest; +import net.imglib2.Cursor; +import net.imglib2.RandomAccess; +import net.imglib2.RandomAccessibleInterval; +import net.imglib2.img.Img; +import net.imglib2.img.array.ArrayImgFactory; +import net.imglib2.img.cell.CellImgFactory; +import net.imglib2.img.planar.PlanarImgFactory; +import net.imglib2.type.numeric.RealType; +import net.imglib2.type.numeric.integer.UnsignedByteType; + +/** + * Test copying of various Image types + * + * @author Christian Dietz (University of Konstanz) + */ +public class CopyImgsTest extends AbstractOpTest { + + private > void populateData(Img data) { + final MersenneTwisterFast r = new MersenneTwisterFast(System.currentTimeMillis()); + final Cursor inc = data.cursor(); + while (inc.hasNext()) { + inc.next().setReal(r.nextDouble() * 255); + } + } + + @Test + public void copyArrayImgNoOutputTest() { + var data = new ArrayImgFactory<>(new UnsignedByteType()).create(10, 10); + populateData(data); + @SuppressWarnings("unchecked") + final RandomAccessibleInterval output = + (RandomAccessibleInterval) ops.op("copy.img").arity1().input( + data).apply(); + + final Cursor inc = data.localizingCursor(); + final RandomAccess outRA = output.randomAccess(); + + while (inc.hasNext()) { + inc.fwd(); + outRA.setPosition(inc); + assertEquals(inc.get().get(), outRA.get().get()); + } + } + + @Test + public void copyArrayImgWithOutputTest() { + var data = new ArrayImgFactory<>(new UnsignedByteType()).create(10, 10); + populateData(data); + final Img output = data.factory().create(data.dimensionsAsLongArray()); + + ops.op("copy.img").arity1().input(data).output(output).compute(); + + final Cursor inc = data.cursor(); + final Cursor outc = output.cursor(); + + while (inc.hasNext()) { + assertEquals(outc.next(), inc.next()); + } + } + + @Test + public void copyPlanarImgNoOutputTest() { + var data = new PlanarImgFactory<>(new UnsignedByteType()).create(10, 10); + populateData(data); + @SuppressWarnings("unchecked") + final RandomAccessibleInterval output = + (RandomAccessibleInterval) ops.op("copy.img").arity1().input( + data).apply(); + + final Cursor inc = data.localizingCursor(); + final RandomAccess outRA = output.randomAccess(); + + while (inc.hasNext()) { + inc.fwd(); + outRA.setPosition(inc); + assertEquals(inc.get().get(), outRA.get().get()); + } + } + + @Test + public void copyPlanarImgWithOutputTest() { + var data = new PlanarImgFactory<>(new UnsignedByteType()).create(10, 10); + populateData(data); + final Img output = data.factory().create(data.dimensionsAsLongArray()); + + ops.op("copy.img").arity1().input(data).output(output).compute(); + + final Cursor inc = data.cursor(); + final Cursor outc = output.cursor(); + + while (inc.hasNext()) { + assertEquals(outc.next(), inc.next()); + } + } + + @Test + public void copyCellImgNoOutputTest() { + var data = new CellImgFactory<>(new UnsignedByteType()).create(10, 10); + populateData(data); + @SuppressWarnings("unchecked") + final RandomAccessibleInterval output = + (RandomAccessibleInterval) ops.op("copy.img").arity1().input( + data).apply(); + + final Cursor inc = data.localizingCursor(); + final RandomAccess outRA = output.randomAccess(); + + while (inc.hasNext()) { + inc.fwd(); + outRA.setPosition(inc); + assertEquals(inc.get().get(), outRA.get().get()); + } + } + + @Test + public void copyCellImgWithOutputTest() { + var data = new CellImgFactory<>(new UnsignedByteType()).create(10, 10); + populateData(data); + final Img output = data.factory().create(data.dimensionsAsLongArray()); + + ops.op("copy.img").arity1().input(data).output(output).compute(); + + final Cursor inc = data.cursor(); + final Cursor outc = output.cursor(); + + while (inc.hasNext()) { + assertEquals(outc.next(), inc.next()); + } + } +} diff --git a/scijava/scijava-ops-engine/src/main/java/org/scijava/ops/engine/copy/CopyOpCollection.java b/scijava/scijava-ops-engine/src/main/java/org/scijava/ops/engine/copy/CopyOpCollection.java index 6f6b8677c..3abba1524 100644 --- a/scijava/scijava-ops-engine/src/main/java/org/scijava/ops/engine/copy/CopyOpCollection.java +++ b/scijava/scijava-ops-engine/src/main/java/org/scijava/ops/engine/copy/CopyOpCollection.java @@ -1,5 +1,11 @@ package org.scijava.ops.engine.copy; +import java.util.ArrayList; +import java.util.Arrays; +import java.util.Collections; +import java.util.List; +import java.util.function.Function; + import org.scijava.priority.Priority; import org.scijava.function.Computers; import org.scijava.ops.spi.OpCollection; @@ -7,23 +13,82 @@ public class CopyOpCollection implements OpCollection{ - @OpField(names = "cp, copy", priority = Priority.LOW, params = "array, arrayCopy") - public static final Computers.Arity1 copyPrimitiveDoubleArray = (from, to) -> { - for (int i = 0; i < to.length; i++) { - to[i] = from[i]; - } - }; - - @OpField(names = "cp, copy", priority = Priority.LOW, params = "array, arrayCopy") - public static final Computers.Arity1 copyDoubleArray = (from, to) -> { - for (int i = 0; i < to.length; i++) { - to[i] = from[i]; - } - }; + @OpField(names = "cp, copy", params = "array, arrayCopy") + public final Function copyGenericArrayFunction = // + from -> Arrays.copyOf(from, from.length); @OpField(names = "cp, copy", params = "array, arrayCopy") public final Computers.Arity1 copyGenericArray = (from, to) -> { int arrMax = Math.max(from.length, to.length); System.arraycopy(from, 0, to, 0, arrMax); }; + + @OpField(names = "cp, copy", params = "array, arrayCopy") + public final Function copyByteArrayFunction = + byte[]::clone; + + @OpField(names = "cp, copy", params = "array, arrayCopy") + public final Computers.Arity1 copyByteArray = (from, to) -> { + int arrMax = Math.max(from.length, to.length); + System.arraycopy(from, 0,to, 0, arrMax); + }; + + @OpField(names = "cp, copy", params = "array, arrayCopy") + public final Function copyShortArrayFunction = + short[]::clone; + + @OpField(names = "cp, copy", params = "array, arrayCopy") + public final Computers.Arity1 copyShortArray = (from, to) -> { + int arrMax = Math.max(from.length, to.length); + System.arraycopy(from, 0,to, 0, arrMax); + }; + + @OpField(names = "cp, copy", params = "array, arrayCopy") + public final Function copyIntArrayFunction = + int[]::clone; + + @OpField(names = "cp, copy", params = "array, arrayCopy") + public final Computers.Arity1 copyIntArray = (from, to) -> { + int arrMax = Math.max(from.length, to.length); + System.arraycopy(from, 0,to, 0, arrMax); + }; + + @OpField(names = "cp, copy", params = "array, arrayCopy") + public final Function copyLongArrayFunction = + long[]::clone; + + @OpField(names = "cp, copy", params = "array, arrayCopy") + public final Computers.Arity1 copyLongArray = (from, to) -> { + int arrMax = Math.max(from.length, to.length); + System.arraycopy(from, 0,to, 0, arrMax); + }; + + @OpField(names = "cp, copy", params = "array, arrayCopy") + public final Function copyFloatArrayFunction = + float[]::clone; + + @OpField(names = "cp, copy", params = "array, arrayCopy") + public final Computers.Arity1 copyFloatArray = (from, to) -> { + int arrMax = Math.max(from.length, to.length); + System.arraycopy(from, 0,to, 0, arrMax); + }; + + @OpField(names = "cp, copy", params = "array, arrayCopy") + public final Function copyDoubleArrayFunction = + double[]::clone; + + @OpField(names = "cp, copy", params = "array, arrayCopy") + public final Computers.Arity1 copyDoubleArray = (from, to) -> { + int arrMax = Math.max(from.length, to.length); + System.arraycopy(from, 0,to, 0, arrMax); + }; + + @OpField(names = "cp, copy", params = "array, arrayCopy") + public final Computers.Arity1, List> copyList = (from, to) -> { + to.clear(); + to.addAll(from); + }; + + @OpField(names = "cp, copy", params = "array, arrayCopy") + public final Function, List> copyListFunction = ArrayList::new; } diff --git a/scijava/scijava-ops-engine/src/main/java/org/scijava/ops/engine/matcher/impl/AdaptationMatchingRoutine.java b/scijava/scijava-ops-engine/src/main/java/org/scijava/ops/engine/matcher/impl/AdaptationMatchingRoutine.java index e1a15459c..d08c03e48 100644 --- a/scijava/scijava-ops-engine/src/main/java/org/scijava/ops/engine/matcher/impl/AdaptationMatchingRoutine.java +++ b/scijava/scijava-ops-engine/src/main/java/org/scijava/ops/engine/matcher/impl/AdaptationMatchingRoutine.java @@ -123,7 +123,7 @@ public OpCandidate findMatch(MatchingConditions conditions, OpMatcher matcher, ); Type mapped = Types.mapVarToTypes(matchedType, adaptorBounds); // If the type variable is more specific now, update it - if (Types.isAssignable(mapped, map.get(typeVar))) { + if (mapped != null && Types.isAssignable(mapped, map.get(typeVar))) { map.put(typeVar, mapped); } } diff --git a/scijava/scijava-ops-engine/src/test/java/org/scijava/ops/engine/ServiceLoaderDiscoveryIntegrationTest.java b/scijava/scijava-ops-engine/src/test/java/org/scijava/ops/engine/ServiceLoaderDiscoveryIntegrationTest.java index d8bffe4f7..54da9e867 100644 --- a/scijava/scijava-ops-engine/src/test/java/org/scijava/ops/engine/ServiceLoaderDiscoveryIntegrationTest.java +++ b/scijava/scijava-ops-engine/src/test/java/org/scijava/ops/engine/ServiceLoaderDiscoveryIntegrationTest.java @@ -44,7 +44,7 @@ public void opCollectionDiscoveryRegressionIT() { final List infos = discoveries.stream() // .flatMap(c -> g.generateInfosFrom(c).stream()) // .collect(Collectors.toList()); - Assertions.assertEquals(264, infos.size()); + Assertions.assertEquals(277, infos.size()); } }