/*
 * #%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.copy;

import net.imagej.ops.Contingent;
import net.imagej.ops.Ops;
import net.imagej.ops.special.chain.RAIs;
import net.imagej.ops.special.computer.Computers;
import net.imagej.ops.special.computer.UnaryComputerOp;
import net.imagej.ops.special.function.UnaryFunctionOp;
import net.imagej.ops.special.hybrid.AbstractUnaryHybridCF;
import net.imglib2.RandomAccessibleInterval;
import net.imglib2.type.Type;
import net.imglib2.util.Intervals;
import net.imglib2.util.Util;

import org.scijava.plugin.Plugin;

/**
 * Copies a {@link RandomAccessibleInterval} into another
 * {@link RandomAccessibleInterval}
 * 
 * @author Christian Dietz, University of Konstanz
 * @param <T>
 */
@Plugin(type = Ops.Copy.RAI.class, priority = 1.0)
public class CopyRAI<T> extends
	AbstractUnaryHybridCF<RandomAccessibleInterval<T>, RandomAccessibleInterval<T>>
	implements Ops.Copy.RAI, Contingent
{

	private UnaryComputerOp<RandomAccessibleInterval<T>, RandomAccessibleInterval<T>> mapComputer;

	private UnaryFunctionOp<RandomAccessibleInterval<T>, RandomAccessibleInterval<T>> createFunc;

	@Override
	public RandomAccessibleInterval<T> createOutput(
		final RandomAccessibleInterval<T> input)
	{
		return createFunc.compute1(input);
	}

	@Override
	public void initialize() {
		final Class<?> outTypeClass =
			out() == null ? Type.class : Util.getTypeFromInterval(out()).getClass();
		final T inType = Util.getTypeFromInterval(in());
		final UnaryComputerOp<T, ?> typeComputer =
			Computers.unary(ops(), Ops.Copy.Type.class, outTypeClass, inType);
		mapComputer = RAIs.computer(ops(), Ops.Map.class, in(), typeComputer);
		createFunc = RAIs.function(ops(), Ops.Create.Img.class, in(), inType);
	}

	@Override
	public void compute1(final RandomAccessibleInterval<T> input,
		final RandomAccessibleInterval<T> output)
	{
		mapComputer.compute1(input, output);
	}

	@Override
	public boolean conforms() {
		if (out() == null) return true;
		return Intervals.equalDimensions(in(), out());
	}

}

@@ -1934,16 +1934,52 @@
 terval;%0A
+import net.imglib2.type.NativeType;%0A
 import n
@@ -2868,35 +2868,25 @@
 nal 
-Class%3C?%3E outTypeClass =%0A%09%09%09
+Object outType = 
 out(
@@ -2910,24 +2910,28 @@
 class : Util
+%0A%09%09%09
 .getTypeFrom
@@ -2949,29 +2949,19 @@
 t())
-.getClass()
 ;%0A
+%0A
 %09%09final 
 T in
@@ -2956,17 +2956,22 @@
 %09%09final 
-T
+Object
  inType 
@@ -2972,20 +2972,58 @@
 nType = 
-Util
+in() == null ? NativeType.class : Util%0A%09%09%09
 .getType
@@ -3042,16 +3042,17 @@
 (in());%0A
+%0A
 %09%09final 
@@ -3067,17 +3067,17 @@
 puterOp%3C
-T
+?
 , ?%3E typ
@@ -3087,20 +3087,17 @@
 mputer =
-%0A%09%09%09
+ 
 Computer
@@ -3106,25 +3106,28 @@
 unary(ops(),
- 
+%0A%09%09%09
 Ops.Copy.Typ
@@ -3146,13 +3146,8 @@
 Type
-Class
 , in
@@ -3299,16 +3299,17 @@
 nType);%0A
+%0A
 %09%7D%0A%0A%09@Ov

Check in()==null