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

import net.imagej.ops.Contingent;
import net.imagej.ops.Ops;
import net.imagej.ops.convert.scale.ScaleRealTypes;
import net.imagej.ops.special.function.Functions;
import net.imagej.ops.special.function.UnaryFunctionOp;
import net.imglib2.IterableInterval;
import net.imglib2.type.numeric.RealType;
import net.imglib2.util.Pair;

import org.scijava.plugin.Plugin;

/**
 * @author Martin Horn (University of Konstanz)
 */
@Plugin(type = Ops.Convert.NormalizeScale.class)
public class NormalizeScaleRealTypes<I extends RealType<I>, O extends RealType<O>>
	extends ScaleRealTypes<I, O> implements Ops.Convert.NormalizeScale, Contingent
{
	
	private UnaryFunctionOp<IterableInterval<I>, Pair<I, I>> minMaxFunc;

	@SuppressWarnings({ "unchecked", "rawtypes" })
	@Override
	public void initialize() {
		minMaxFunc = (UnaryFunctionOp) Functions.unary(ops(),
			Ops.Stats.MinMax.class, Pair.class, IterableInterval.class);
	}

	@Override
	public void checkInput(final I inType, final O outType) {
		outMin = outType.getMinValue();
	}

	@Override
	public void checkInput(final IterableInterval<I> in) {
		final Pair<I, I> minMax = minMaxFunc.calculate(in);
		final I inType = in.firstElement().createVariable();
		factor =
			1.0 / (minMax.getB().getRealDouble() - minMax.getA().getRealDouble()) *
				(inType.getMaxValue() - inType.getMinValue());

		inMin = minMax.getA().getRealDouble();

	}

	@Override
	public boolean conforms() {
		// only conforms if an input source has been provided and the scale factor
		// was calculated
		return factor != 0;
	}

}

@@ -2220,10 +2220,9 @@
 t%0A%7B%0A
-%09
 %0A
+
 %09pri
@@ -2288,16 +2288,43 @@
 xFunc;%0A%0A
+%09protected double outMax;%0A%0A
 %09@Suppre
@@ -2630,16 +2630,50 @@
 alue();%0A
+%09%09outMax = outType.getMaxValue();%0A
 %09%7D%0A%0A%09@Ov
@@ -2793,63 +2793,8 @@
 n);%0A
-%09%09final I inType = in.firstElement().createVariable();%0A
 %09%09fa
@@ -2803,17 +2803,8 @@
 or =
-%0A%09%09%091.0 /
  (mi
@@ -2838,32 +2838,36 @@
  - minMax.getA()
+%0A%09%09%09
 .getRealDouble()
@@ -2872,58 +2872,26 @@
 ()) 
-*%0A%09%09%09%09(inType.getMaxValue() - inType.getMinValue()
+/ (outMax - outMin
 );%0A%0A
@@ -2931,17 +2931,16 @@
 uble();%0A
-%0A
 %09%7D%0A%0A%09@Ov
@@ -3052,16 +3052,16 @@
  factor%0A
-
 %09%09// was
@@ -3072,16 +3072,98 @@
 culated%0A
+%09%09// FIXME Prevents this op from being matched for use with Ops.Convert.ImageType%0A
 %09%09return

fix conversion factor