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

import net.imagej.ops.Op;
import net.imagej.ops.Ops;
import net.imagej.ops.image.integral.IntegralCursor;
import net.imglib2.algorithm.neighborhood.RectangleNeighborhood;
import net.imglib2.converter.Converter;
import net.imglib2.converter.RealDoubleConverter;
import net.imglib2.type.numeric.RealType;
import net.imglib2.type.numeric.real.DoubleType;

import org.scijava.plugin.Plugin;

/**
 * {@link Op} to calculate the {@code stats.sum} from an integral image using a
 * specialized {@code Cursor} implementation.
 *
 * @author Stefan Helfrich (University of Konstanz)
 * @param <I> input type
 */
@Plugin(type = Ops.Stats.IntegralSum.class)
public class IntegralSum<I extends RealType<I>> extends
	AbstractStatsOp<RectangleNeighborhood<I>, DoubleType> implements
	Ops.Stats.IntegralSum
{

	@Override
	public void compute1(final RectangleNeighborhood<I> input,
		final DoubleType output)
	{
		// computation according to	https://en.wikipedia.org/wiki/Summed_area_table
		final IntegralCursor<I> cursor = new IntegralCursor<>(input);
		int dimensions = input.numDimensions();
		
		// Compute \sum (-1)^{dim - ||cornerVector||_{1}} * I(x^{cornerVector})
		final DoubleType sum = new DoubleType();
		sum.setZero();

		// Convert from input to return type
		final Converter<I, DoubleType> conv = new RealDoubleConverter<>();
		DoubleType valueAsDoubleType = new DoubleType();
		
		while ( cursor.hasNext() )
		{
			// Obtain the cursor position encoded as corner vector
			int cornerInteger = cursor.getCornerRepresentation();
			final I value = cursor.next().copy();			
			conv.convert(value, valueAsDoubleType);
			
			// Determine if the value has to be added (factor==1) or subtracted (factor==-1)
			DoubleType factor = new DoubleType(Math.pow(-1.0d, dimensions - norm(cornerInteger)));
			valueAsDoubleType.mul(factor);
			
			sum.add(valueAsDoubleType);
		}
			
		output.set(sum);
	}

	/**
	 * Computes L1 norm of the position of an {@code IntegralCursor}. Computation
	 * is based on determining the number of 1 bits in the position.
	 * 
	 * @param cornerPosition position vector of an {@code IntegralCursor} encoded
	 *          as integer
	 * @return L1 norm of the position
	 */
	private int norm(int cornerPosition) {
		return Integer.bitCount(cornerPosition);
	}
	
}

@@ -2084,16 +2084,41 @@
 ion.%0A *%0A
+ * @param %3CI%3E input type%0A
  * @auth
@@ -2165,33 +2165,8 @@
 nz)%0A
- * @param %3CI%3E input type%0A
  */%0A
@@ -2642,18 +2642,16 @@
 ions();%0A
-%09%09
 %0A%09%09// Co
@@ -2937,18 +2937,16 @@
 Type();%0A
-%09%09
 %0A%09%09while
@@ -2971,16 +2971,104 @@
 ) )%0A%09%09%7B%0A
+%09%09%09final I value = cursor.next().copy();%09%09%09%0A%09%09%09conv.convert(value, valueAsDoubleType);%0A%0A
 %09%09%09// Ob
@@ -3178,98 +3178,8 @@
 ();%0A
-%09%09%09final I value = cursor.next().copy();%09%09%09%0A%09%09%09conv.convert(value, valueAsDoubleType);%0A%09%09%09
 %0A%09%09%09
@@ -3326,16 +3326,29 @@
 sions - 
+IntegralMean.
 norm(cor
@@ -3396,19 +3396,16 @@
 actor);%0A
-%09%09%09
 %0A%09%09%09sum.
@@ -3432,19 +3432,16 @@
 e);%0A%09%09%7D%0A
-%09%09%09
 %0A%09%09outpu
@@ -3460,393 +3460,6 @@
 %09%7D%0A%0A
-%09/**%0A%09 * Computes L1 norm of the position of an %7B@code IntegralCursor%7D. Computation%0A%09 * is based on determining the number of 1 bits in the position.%0A%09 * %0A%09 * @param cornerPosition position vector of an %7B@code IntegralCursor%7D encoded%0A%09 *          as integer%0A%09 * @return L1 norm of the position%0A%09 */%0A%09private int norm(int cornerPosition) %7B%0A%09%09return Integer.bitCount(cornerPosition);%0A%09%7D%0A%09%0A
 %7D%0A

Add cursor changes to IntegralSum