/*
 * #%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.imglib2.Cursor;
import net.imglib2.IterableInterval;
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)
	{
		// Based on the location compute the values A, B, C, and D
		// (computation according to
		// https://en.wikipedia.org/wiki/Summed_area_table)
		final Cursor<I> cursor = input.integralCursor();

		// A
		final I valueA = cursor.get().copy();

		// B
		final I valueB = cursor.get().copy();

		// C
		final I valueC = cursor.get().copy();

		// D
		final I valueD = cursor.get().copy();

		// Compute A - B - C + D
		final I sum = cursor.get().createVariable();
		sum.setZero();
		sum.add(valueA);
		sum.sub(valueB);
		sum.sub(valueC);
		sum.add(valueD);

		// Convert to return type
		final Converter<I, DoubleType> conv = new RealDoubleConverter<>();
		final DoubleType sumAsDoubleType = new DoubleType();
		conv.convert(sum, sumAsDoubleType);

		output.set(sumAsDoubleType);
	}

}

@@ -2692,34 +2692,35 @@
 valueA = cursor.
-ge
+nex
 t().copy();%0A%0A%09%09/
@@ -2741,34 +2741,35 @@
 valueB = cursor.
-ge
+nex
 t().copy();%0A%0A%09%09/
@@ -2790,34 +2790,35 @@
 valueC = cursor.
-ge
+nex
 t().copy();%0A%0A%09%09/
@@ -2843,26 +2843,27 @@
 eD = cursor.
-ge
+nex
 t().copy();%0A

Fix incorrect use of Cursor in IntegralSum