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

import net.imagej.ops.Ops;
import net.imagej.ops.special.computer.AbstractUnaryComputerOp;
import net.imglib2.RandomAccessibleInterval;
import net.imglib2.algorithm.neighborhood.Shape;
import net.imglib2.outofbounds.OutOfBoundsFactory;
import net.imglib2.type.logic.BitType;
import net.imglib2.type.numeric.RealType;
import net.imglib2.util.ValuePair;

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

/**
 * LocalThresholdMethod that uses the mean and operates directly of RAIs.
 *
 * @author Jonathan Hale (University of Konstanz)
 * @author Martin Horn (University of Konstanz)
 * @author Stefan Helfrich (University of Konstanz)
 */
@Plugin(type = Ops.Threshold.LocalMean.class, priority = Priority.LOW_PRIORITY)
public class LocalMeanRAI<T extends RealType<T>> extends
		AbstractUnaryComputerOp<RandomAccessibleInterval<T>, RandomAccessibleInterval<BitType>>
		implements Ops.Threshold.LocalMean {

	@Parameter
	private Shape shape;

	@Parameter(required = false)
	private OutOfBoundsFactory<T, RandomAccessibleInterval<T>> outOfBounds;

	@Parameter
	private double c;

	private LocalMean<T> localMeanOp;

	@SuppressWarnings({ "unchecked" })
	@Override
	public void initialize() {
		localMeanOp = ops().op(LocalMean.class, BitType.class,
				new ValuePair<T, RandomAccessibleInterval<T>>(null, in()), c);
	}

	@Override
	public void compute1(final RandomAccessibleInterval<T> input,
			final RandomAccessibleInterval<BitType> output) {
		// FIXME Apply in initialize()
		ops().threshold().apply(output, input, localMeanOp, shape, outOfBounds);
	}

}

@@ -1606,16 +1606,82 @@
 ps.Ops;%0A
+import net.imagej.ops.map.neighborhood.MapNeighborhoodWithCenter;%0A
 import n
@@ -1996,16 +1996,47 @@
 luePair;
+%0Aimport net.imglib2.view.Views;
 %0A%0Aimport
@@ -2502,17 +2502,16 @@
 extends%0A
-%09
 %09Abstrac
@@ -2591,17 +2591,16 @@
 tType%3E%3E%0A
-%09
 %09impleme
@@ -2626,17 +2626,17 @@
 ocalMean
- 
+%0A
 %7B%0A%0A%09@Par
@@ -2835,16 +2835,70 @@
 lMeanOp;
+%0A%09private MapNeighborhoodWithCenter%3CT, BitType%3E mapOp;
 %0A%0A%09@Supp
@@ -3026,17 +3026,16 @@
 ass,%0A%09%09%09
-%09
 new Valu
@@ -3089,16 +3089,126 @@
 )), c);%0A
+%09%09mapOp = ops().op(MapNeighborhoodWithCenter.class, out(), extend(in(),%0A%09%09%09outOfBounds), localMeanOp, shape);%0A
 %09%7D%0A%0A%09@Ov
@@ -3279,17 +3279,16 @@
 nput,%0A%09%09
-%09
 final Ra
@@ -3330,105 +3330,729 @@
 put)
- %7B%0A%09%09// FIXME Apply in initialize()%0A%09%09ops().threshold().apply(output, input, localMeanOp, shape, 
+%0A%09%7B%0A%09%09mapOp.compute1(extend(input, outOfBounds), output);%0A%09%7D%0A%0A%09/**%0A%09 * Extends an input using an %7B@link OutOfBoundsFactory%7D if available,%0A%09 * otherwise returns the unchanged input.%0A%09 *%0A%09 * @param in %7B@link RandomAccessibleInterval%7D that is to be extended%0A%09 * @param outOfBounds the factory that is used for extending%0A%09 * @return %7B@link RandomAccessibleInterval%7D extended using the%0A%09 *         %7B@link OutOfBoundsFactory%7D%0A%09 */%0A%09public static %3CT%3E RandomAccessibleInterval%3CT%3E extend(%0A%09%09final RandomAccessibleInterval%3CT%3E in,%0A%09%09final OutOfBoundsFactory%3CT, RandomAccessibleInterval%3CT%3E%3E outOfBounds)%0A%09%7B%0A%09%09// FIXME Move this method to a static utility class%0A%09%09return outOfBounds == null ? in : Views.interval((Views.extend(in,%0A%09%09%09
 outO
@@ -4059,16 +4059,22 @@
 fBounds)
+), in)
 ;%0A%09%7D%0A%0A%7D%0A

Move namespace calls into initialize()