/*
 * #%L
 * ImageJ software for multidimensional image processing and analysis.
 * %%
 * Copyright (C) 2014 - 2015 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.AbstractHybridOp;
import net.imagej.ops.ComputerOp;
import net.imagej.ops.Contingent;
import net.imagej.ops.FunctionOp;
import net.imagej.ops.OpService;
import net.imagej.ops.Ops;
import net.imagej.ops.Ops.Create;
import net.imagej.ops.Ops.Map;
import net.imglib2.RandomAccessibleInterval;
import net.imglib2.type.Type;
import net.imglib2.util.Intervals;
import net.imglib2.util.Util;
import net.imglib2.view.Views;

import org.scijava.plugin.Parameter;
import org.scijava.plugin.Plugin;

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

	@Parameter
	protected OpService ops;

	// used internally
	private ComputerOp<RandomAccessibleInterval<T>, RandomAccessibleInterval<T>> mapComputer;

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

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

	@SuppressWarnings({ "unchecked", "rawtypes" })
	@Override
	public void initialize() {
		mapComputer = (ComputerOp) ops().computer(
				Map.class,
				out() == null ? RandomAccessibleInterval.class : out(),
				in(),
				ops.computer(Ops.Copy.Type.class, out() == null ? Type.class
						: Views.iterable(out()).firstElement().getClass(),
						Views.iterable(in()).firstElement().getClass()));

		createFunc = (FunctionOp) ops().function(Create.Img.class,
				RandomAccessibleInterval.class, in(),
				Util.getTypeFromInterval(in()));
	}

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

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

@@ -1831,16 +1831,44 @@
 ps.Map;%0A
+import net.imagej.ops.RAIs;%0A
 import n
@@ -2896,56 +2896,8 @@
 %09%7D%0A%0A
-%09@SuppressWarnings(%7B %22unchecked%22, %22rawtypes%22 %7D)%0A
 %09@Ov
@@ -2935,145 +2935,87 @@
 ) %7B%0A
+%0A
 %09%09
-mapComputer = (ComputerOp) ops().computer(%0A%09%09%09%09Map.class,%0A%09%09%09%09out() == null ? RandomAccessibleInterval.class : out(),%0A%09%09%09%09in(),%0A%09%09%09%09ops
+final ComputerOp%3C? extends Object, ? extends Object%3E typeComputer = ops%0A%09%09%09%09
 .com
@@ -3182,45 +3182,99 @@
 s())
-)
 ;%0A%0A%09%09
-createFunc = (FunctionOp) ops()
+mapComputer = RAIs.computer(ops(), Map.class, in(), typeComputer);%0A%09%09createFunc = RAIs
 .fun
@@ -3279,16 +3279,23 @@
 unction(
+ops(), 
 Create.I
@@ -3307,44 +3307,8 @@
 ass,
-%0A%09%09%09%09RandomAccessibleInterval.class,
  in(

use RAIs and Functions to initialize ops