/*
 * #%L
 * ImageJ software for multidimensional image processing and analysis.
 * %%
 * Copyright (C) 2014 - 2016 Board of Regents of the University of
 * Wisconsin-Madison, University of Konstanz and Brian Northan.
 * %%
 * 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.ops.image.project;

import static org.junit.Assert.assertEquals;

import net.imagej.ops.AbstractOpTest;
import net.imagej.ops.Op;
import net.imagej.ops.Ops;
import net.imglib2.RandomAccess;
import net.imglib2.img.Img;
import net.imglib2.type.numeric.RealType;
import net.imglib2.type.numeric.integer.UnsignedByteType;

import org.junit.Before;
import org.junit.Test;

public class ProjectTest extends AbstractOpTest {

	private final int PROJECTION_DIM = 2;

	private Img<UnsignedByteType> in;
	private Img<UnsignedByteType> out1;
	private Img<UnsignedByteType> out2;
	private Op op;

	@Before
	public void initImg() {
		in = generateUnsignedByteArrayTestImg(false, 10, 10, 10);

		final RandomAccess<UnsignedByteType> randomAccess = in.randomAccess();

		// at each x,y,z fill with x+y
		for (int x = 0; x < 10; x++) {
			for (int y = 0; y < 10; y++) {
				for (int z = 0; z < 10; z++) {
					randomAccess.setPosition(new long[] { x, y, z });
					randomAccess.get().setReal(x + y);
				}
			}
		}

		out1 = generateUnsignedByteArrayTestImg(false, 10, 10);
		out2 = generateUnsignedByteArrayTestImg(false, 10, 10);

		op = ops.op(Ops.Stats.Sum.class, RealType.class, out1);
	}

	@Test
	public void testProjector() {
		ops.image().project(out1, in, op, PROJECTION_DIM);
		ops.image().project(out2, in, op, PROJECTION_DIM);
		testEquality(out1, out2);

		ops.run(ProjectRAIToII.class, out1, in, op, PROJECTION_DIM);
		ops.run(ProjectRAIToII.class, out2, in, op, PROJECTION_DIM);
		testEquality(out1, out2);
	}

	private void testEquality(final Img<UnsignedByteType> img1,
		final Img<UnsignedByteType> img2)
	{
		final RandomAccess<UnsignedByteType> img1RandomAccess = img1.randomAccess();
		final RandomAccess<UnsignedByteType> img2RandomAccess = img2.randomAccess();

		// at each x,y position the sum projection should be (x+y) *size(z)
		for (int x = 0; x < 10; x++) {
			for (int y = 0; y < 10; y++) {
				img1RandomAccess.setPosition(new long[] { x, y });
				img2RandomAccess.setPosition(new long[] { x, y });

				assertEquals(img1RandomAccess.get().get(), in.dimension(
					PROJECTION_DIM) * (x + y));
				assertEquals(img2RandomAccess.get().get(), in.dimension(
					PROJECTION_DIM) * (x + y));
			}
		}
	}
}

@@ -1681,16 +1681,17 @@
 j.ops.Op
+s
 ;%0Aimport
@@ -1698,35 +1698,114 @@
  net.imagej.ops.
+special.computer.Computers;%0Aimport net.imagej.ops.special.computer.UnaryComputer
 Op
-s
 ;%0Aimport net.img
@@ -1855,50 +1855,8 @@
 mg;%0A
-import net.imglib2.type.numeric.RealType;%0A
 impo
@@ -2168,18 +2168,77 @@
 private 
-Op
+UnaryComputerOp%3CIterable%3CUnsignedByteType%3E, UnsignedByteType%3E
  op;%0A%0A%09@
@@ -2778,15 +2778,29 @@
 p = 
-ops.op(
+Computers.unary(ops, 
 Ops.
@@ -2816,20 +2816,28 @@
 .class, 
-Real
+UnsignedByte
 Type.cla
@@ -2835,25 +2835,28 @@
 eType.class,
- 
+%0A%09%09%09
 out1);%0A%09%7D%0A%0A%09

use type-safe signature