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

import net.imagej.ops.Ops;
import net.imagej.ops.special.AbstractUnaryFunctionOp;
import net.imglib2.Dimensions;
import net.imglib2.IterableInterval;
import net.imglib2.RandomAccessibleInterval;
import net.imglib2.exception.IncompatibleTypeException;
import net.imglib2.img.Img;
import net.imglib2.img.ImgFactory;
import net.imglib2.type.NativeType;
import net.imglib2.util.Util;

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

/**
 * Default implementation of the "create.img" op.
 *
 * @author Daniel Seebacher (University of Konstanz)
 * @author Tim-Oliver Buchholz (University of Konstanz)
 * @param <T>
 */
@Plugin(type = Ops.Create.Img.class)
public class DefaultCreateImg<T> extends
	AbstractUnaryFunctionOp<Dimensions, Img<T>> implements Ops.Create.Img
{

	@Parameter(required = false)
	private T outType;

	@Parameter(required = false)
	private ImgFactory<T> fac;

	@SuppressWarnings("unchecked")
	@Override
	public Img<T> compute1(final Dimensions input) {
		// FIXME: not guaranteed to be a T unless a Class<T> is given.
		if (outType == null) {
			if (input instanceof IterableInterval) {
				outType = (T) ((IterableInterval<?>) input).firstElement();
			}
			else if (input instanceof RandomAccessibleInterval) {
				outType = (T) Util.getTypeFromInterval(
					(RandomAccessibleInterval<?>) input);
			}
			else {
				// HACK: For Java 6 compiler.
				@SuppressWarnings("rawtypes")
				final NativeType o = ops().create().nativeType();
				final T result = (T) o;
				outType = result;
			}
		}

		if (fac == null) {
			if (input instanceof Img) {
				final Img<?> inImg = ((Img<?>) input);
				if (inImg.firstElement().getClass().isInstance(outType)) {
					fac = (ImgFactory<T>) inImg.factory();
				}
				else {
					try {
						// HACK: For Java 6 compiler.
						final Object o = inImg.factory().imgFactory(outType);
						final ImgFactory<T> result = (ImgFactory<T>) o;
						fac = result;
					}
					catch (final IncompatibleTypeException e) {
						// FIXME: outType may not be a NativeType, but imgFactory needs one.
						fac = (ImgFactory<T>) ops().create().imgFactory(input, outType);
					}
				}
			}
			else {
				// FIXME: outType may not be a NativeType, but imgFactory needs one.
				fac = (ImgFactory<T>) ops().create().imgFactory(input, outType);
			}
		}

		return fac.create(input, outType);
	}

}

@@ -2524,46 +2524,24 @@
 lic 
-Img%3CT%3E compute1(final Dimensions input
+void initialize(
 ) %7B%0A
@@ -2631,35 +2631,34 @@
 ull) %7B%0A%09%09%09if (in
-put
+()
  instanceof Iter
@@ -2716,19 +2716,18 @@
 l%3C?%3E) in
-put
+()
 ).firstE
@@ -2751,27 +2751,26 @@
 %09else if (in
-put
+()
  instanceof 
@@ -2874,27 +2874,26 @@
 erval%3C?%3E) in
-put
+()
 );%0A%09%09%09%7D%0A%09%09%09e
@@ -3110,19 +3110,18 @@
 %09%09if (in
-put
+()
  instanc
@@ -3168,19 +3168,18 @@
 g%3C?%3E) in
-put
+()
 );%0A%09%09%09%09i
@@ -3657,35 +3657,34 @@
 ().imgFactory(in
-put
+()
 , outType);%0A%09%09%09%09
@@ -3834,19 +3834,18 @@
 ctory(in
-put
+()
 , outTyp
@@ -3857,16 +3857,80 @@
 %09%09%7D%0A%09%09%7D%0A
+%09%7D%0A%0A%09@Override%0A%09public Img%3CT%3E compute1(final Dimensions input) %7B
 %0A%09%09retur

use initialize()