/*
 * #%L
 * ImageJ OPS: a framework for reusable algorithms.
 * %%
 * Copyright (C) 2014 Board of Regents of the University of
 * Wisconsin-Madison and University of Konstanz.
 * %%
 * 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 imagej.ops.convolve;

import imagej.ops.Contingent;
import imagej.ops.Function;
import imagej.ops.Op;
import net.imglib2.Cursor;
import net.imglib2.FinalInterval;
import net.imglib2.RandomAccess;
import net.imglib2.RandomAccessibleInterval;
import net.imglib2.type.numeric.RealType;
import net.imglib2.util.Intervals;
import net.imglib2.view.Views;

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

/**
 * Convolves an image naively.
 */
@Plugin(type = Op.class, name = "convolve")
public class ConvolveNaive<I extends RealType<I>, K extends RealType<K>, O extends RealType<O>>
	extends Function<RandomAccessibleInterval<I>, RandomAccessibleInterval<O>>
	implements Contingent
{

	@Parameter
	private RandomAccessibleInterval<K> kernel;

	@Override
	public RandomAccessibleInterval<O> compute(RandomAccessibleInterval<I> input,
		RandomAccessibleInterval<O> output)
	{
		// TODO: try a decomposition of the kernel into n 1-dim kernels

		final long[] min = new long[input.numDimensions()];
		final long[] max = new long[input.numDimensions()];

		for (int d = 0; d < kernel.numDimensions(); d++) {
			min[d] = -kernel.dimension(d);
			max[d] = kernel.dimension(d) + output.dimension(d);
		}

		final RandomAccess<I> inRA =
			input.randomAccess(new FinalInterval(min, max));

		final Cursor<K> kernelC = Views.iterable(kernel).localizingCursor();

		final Cursor<O> outC = Views.iterable(output).localizingCursor();

		final long[] pos = new long[input.numDimensions()];
		final long[] kernelRadius = new long[kernel.numDimensions()];
		for (int i = 0; i < kernelRadius.length; i++) {
			kernelRadius[i] = kernel.dimension(i) / 2;
		}

		float val;

		while (outC.hasNext()) {
			// image
			outC.fwd();
			outC.localize(pos);

			// kernel inlined version of the method convolve
			val = 0;
			inRA.setPosition(pos);

			kernelC.reset();
			while (kernelC.hasNext()) {
				kernelC.fwd();

				for (int i = 0; i < kernelRadius.length; i++) {
					if (kernelRadius[i] > 0) { // dimension
																			// can
																			// have
																			// zero
																			// extension
																			// e.g.
																			// vertical
																			// 1d
																			// kernel
						inRA.setPosition(pos[i] + kernelC.getLongPosition(i) -
							kernelRadius[i], i);
					}
				}

				val += inRA.get().getRealDouble() * kernelC.get().getRealDouble();
			}

			outC.get().setReal(val);
		}
		return output;
	}

	@Override
	public ConvolveNaive<I, K, O> copy() {
		ConvolveNaive<I, K, O> conv = new ConvolveNaive<I, K, O>();
		conv.kernel = kernel;
		return conv;
	}

	@Override
	public boolean conforms() {
		// conforms only if the kernel is sufficiently small
		return Intervals.numElements(kernel) <= 9;
	}

}

@@ -3475,271 +3475,100 @@
 %09%09%09%09
-if (kernelRadius%5Bi%5D %3E 0) %7B // dimension%0A%09%09%09%09%09%09%09%09%09%09%09%09%09%09%09%09%09%09%09// can%0A%09%09%09%09%09%09%09%09%09%09%09%09%09%09%09%09%09%09%09// have%0A%09%09%09%09%09%09%09%09%09%09%09%09%09%09%09%09%09%09%09// zero%0A%09%09%09%09%09%09%09%09%09%09%09%09%09%09%09%09%09%09%09// extension%0A%09%09%09%09%09%09%09%09%09%09%09%09%09%09%09%09%09%09%09// e.g.%0A%09%09%09%09%09%09%09%09%09%09%09%09%09%09%09%09%09%09%09// vertical%0A%09%09%09%09%09%09%09%09%09%09%09%09%09%09%09%09%09%09%09// 1d%0A%09%09%09%09%09%09%09%09%09%09%09%09%09%09%09%09%09%09%09// kernel
+// dimension can have zero extension e.g. vertical 1d kernel%0A%09%09%09%09%09if (kernelRadius%5Bi%5D %3E 0) %7B
 %0A%09%09%09

fix comment style