|
| 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.project; |
| 31 | + |
| 32 | +import static org.junit.Assert.assertEquals; |
| 33 | + |
| 34 | +import net.imagej.ops.AbstractOpTest; |
| 35 | +import net.imglib2.RandomAccess; |
| 36 | +import net.imglib2.img.Img; |
| 37 | +import net.imglib2.type.numeric.integer.UnsignedByteType; |
| 38 | + |
| 39 | +import org.junit.Before; |
| 40 | +import org.junit.Test; |
| 41 | +import org.scijava.ops.core.computer.Computer; |
| 42 | +import org.scijava.ops.core.computer.GenericComputers; |
| 43 | +import org.scijava.ops.types.Nil; |
| 44 | +import org.scijava.ops.util.Computers; |
| 45 | + |
| 46 | +public class ProjectTest extends AbstractOpTest { |
| 47 | + |
| 48 | + private final int PROJECTION_DIM = 2; |
| 49 | + |
| 50 | + private Img<UnsignedByteType> in; |
| 51 | + private Img<UnsignedByteType> out1; |
| 52 | + private Img<UnsignedByteType> out2; |
| 53 | + private Computer<Iterable<UnsignedByteType>, UnsignedByteType> op; |
| 54 | + private Computer<Iterable<UnsignedByteType>, UnsignedByteType> genericOp; |
| 55 | + |
| 56 | + @Before |
| 57 | + public void initImg() { |
| 58 | + in = generateUnsignedByteArrayTestImg(false, 10, 10, 10); |
| 59 | + |
| 60 | + final RandomAccess<UnsignedByteType> randomAccess = in.randomAccess(); |
| 61 | + |
| 62 | + // at each x,y,z fill with x+y |
| 63 | + for (int x = 0; x < 10; x++) { |
| 64 | + for (int y = 0; y < 10; y++) { |
| 65 | + for (int z = 0; z < 10; z++) { |
| 66 | + randomAccess.setPosition(new long[] { x, y, z }); |
| 67 | + randomAccess.get().setReal(x + y); |
| 68 | + } |
| 69 | + } |
| 70 | + } |
| 71 | + |
| 72 | + out1 = generateUnsignedByteArrayTestImg(false, 10, 10); |
| 73 | + out2 = generateUnsignedByteArrayTestImg(false, 10, 10); |
| 74 | + |
| 75 | + op = Computers.unary(ops, "stats.sum", new Nil<Iterable<UnsignedByteType>>() {}, |
| 76 | + new Nil<UnsignedByteType>() {}); |
| 77 | + genericOp = GenericComputers.Computers.generic(op, |
| 78 | + new Nil<Computer<Iterable<UnsignedByteType>, UnsignedByteType>>() {}.getType()); |
| 79 | + } |
| 80 | + |
| 81 | + @Test |
| 82 | + public void testProjector() { |
| 83 | + //TODO: uncomment when this Op is ported (assuming it will be?) |
| 84 | + // ops.run(DefaultProjectParallel.class, out1, in, op, PROJECTION_DIM); |
| 85 | + // ops.run(DefaultProjectParallel.class, out2, in, op, PROJECTION_DIM); |
| 86 | + // testEquality(out1, out2); |
| 87 | + |
| 88 | + ops.run("project", in, genericOp, PROJECTION_DIM, out1); |
| 89 | + ops.run("project", in, genericOp, PROJECTION_DIM, out2); |
| 90 | + testEquality(out1, out2); |
| 91 | + } |
| 92 | + |
| 93 | + private void testEquality(final Img<UnsignedByteType> img1, final Img<UnsignedByteType> img2) { |
| 94 | + final RandomAccess<UnsignedByteType> img1RandomAccess = img1.randomAccess(); |
| 95 | + final RandomAccess<UnsignedByteType> img2RandomAccess = img2.randomAccess(); |
| 96 | + |
| 97 | + // at each x,y position the sum projection should be (x+y) *size(z) |
| 98 | + for (int x = 0; x < 10; x++) { |
| 99 | + for (int y = 0; y < 10; y++) { |
| 100 | + img1RandomAccess.setPosition(new long[] { x, y }); |
| 101 | + img2RandomAccess.setPosition(new long[] { x, y }); |
| 102 | + |
| 103 | + assertEquals(img1RandomAccess.get().get(), // |
| 104 | + in.dimension(PROJECTION_DIM) * (x + y)); |
| 105 | + assertEquals(img2RandomAccess.get().get(), // |
| 106 | + in.dimension(PROJECTION_DIM) * (x + y)); |
| 107 | + } |
| 108 | + } |
| 109 | + } |
| 110 | +} |
0 commit comments