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

import net.imglib2.exception.IncompatibleTypeException;
import net.imglib2.img.Img;
import net.imglib2.img.ImgFactory;
import net.imglib2.type.Type;

import org.scijava.ItemIO;
import org.scijava.plugin.Parameter;

/**
 * Abstract class for creating an image from a factory and a type. Derived
 * classes will contain logic to determine size and values of the image.
 * 
 * @author bnorthan
 * @param <V>
 */
abstract public class AbstractCreateKernelImg<V extends Type<V>, W extends Type<W>, FAC extends ImgFactory<W>>
{

	@Parameter(type = ItemIO.OUTPUT)
	protected Img<V> output;

	@Parameter(required = false)
	protected Type<V> outType;

	@Parameter(required = false)
	protected ImgFactory<V> fac;

	protected void createOutputImg(long[] dims, ImgFactory<V> fac,
		Type<V> outType, FAC defaultFactory, W defaultType)
	{

		// no factory and no type
		if ((fac == null) && (outType == null)) {
			@SuppressWarnings("unchecked")
			Img<V> temp = (Img<V>) defaultFactory.create(dims, defaultType);

			output = temp;
		}
		// type but no factory
		else if ((fac == null) && (outType != null)) {

			try {
				Img<V> temp =
					defaultFactory.imgFactory(outType.createVariable()).create(dims,
						outType.createVariable());

				output = temp;
			}
			catch (IncompatibleTypeException ex) {

			}
		}
		// factory but no type
		else if ((fac != null) && (outType == null)) {
			try {
				@SuppressWarnings("unchecked")
				Img<V> temp =
					(Img<V>) fac.imgFactory(defaultType.createVariable()).create(dims,
						defaultType.createVariable());

				output = temp;
			}
			catch (IncompatibleTypeException ex) {

			}

		}
		// type and a factory passed in
		else {
			output = fac.create(dims, outType.createVariable());
		}

	}

	protected void createOutputImg(long[] dims, Type<V> outType,
		FAC defaultFactory, W defaultType)
	{
		createOutputImg(dims, null, outType, defaultFactory, defaultType);
	}

	protected void createOutputImg(long[] dims, ImgFactory<V> fac,
		FAC defaultFactory, W defaultType)
	{
		createOutputImg(dims, fac, null, defaultFactory, defaultType);
	}

}
@@ -1578,24 +1578,84 @@
 el.create;%0A%0A
+import net.imagej.ops.OpService;%0Aimport net.imagej.ops.Ops;%0A
 import net.i
@@ -2165,16 +2165,53 @@
 %3CW%3E%3E%0A%7B%0A%0A
+%09@Parameter%0A%09private OpService ops;%0A%0A
 %09@Parame
@@ -2584,86 +2584,39 @@
 %0A%09%09%09
-@SuppressWarnings(%22unchecked%22)%0A%09%09%09Img%3CV%3E temp = (Img%3CV%3E) defaultFactory
+output = (Img%3CV%3E) ops
 .create
+img
 (dim
@@ -2633,28 +2633,25 @@
 Type
-);%0A%0A%09%09%09output = temp
+, defaultFactory)
 ;%0A%09%09
@@ -2730,572 +2730,216 @@
 ) %7B%0A
-%0A
 %09%09%09
-try %7B%0A%09%09%09%09Img%3CV%3E temp =%0A%09%09%09%09%09defaultFactory.imgFactory(outType.createVariable()).create(dims,%0A%09%09%09%09%09%09outType.createVariable());%0A%0A%09%09%09%09output = temp;%0A%09%09%09%7D%0A%09%09%09catch (IncompatibleTypeException ex) %7B%0A%0A%09%09%09%7D%0A%09%09%7D%0A%09%09// factory but no type%0A%09%09else if ((fac != null) && (outType == null)) %7B%0A%09%09%09try %7B%0A%09%09%09%09@SuppressWarnings(%22unchecked%22)%0A%09%09%09%09Img%3CV%3E temp =%0A%09%09%09%09%09(Img%3CV%3E) fac.imgFactory(defaultType.createVariable()).create(dims,%0A%09%09%09%09%09%09defaultType.createVariable());%0A%0A%09%09%09%09output = temp;%0A%09%09%09%7D%0A%09%09%09catch (IncompatibleTypeException ex) %7B%0A%0A%09%09%09%7D%0A%0A%09%09%7D%0A%09%09// type and a factory passed in
+output = (Img%3CV%3E) ops.createimg(dims, outType, defaultFactory);%0A%09%09%7D%0A%09%09// factory but no type%0A%09%09else if ((fac != null) && (outType == null)) %7B%0A%09%09%09output = (Img%3CV%3E) ops.createimg(dims, defaultType, fac);%0A%09%09%7D
 %0A%09%09e
@@ -2960,18 +2960,30 @@
 t = 
-fac
+(Img%3CV%3E) ops
 .create
+img
 (dim
@@ -2996,25 +2996,13 @@
 Type
-.createVariable()
+, fac
 );%0A%09
@@ -3358,8 +3358,9 @@
 );%0A%09%7D%0A%0A%7D
+%0A

use createimg op