/*
 * #%L
 * ImageJ software for multidimensional image processing and analysis.
 * %%
 * Copyright (C) 2014 - 2017 ImageJ developers.
 * %%
 * 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.image.invert;

import java.math.BigDecimal;

import net.imagej.ops.Ops;
import net.imagej.ops.special.computer.AbstractUnaryComputerOp;
import net.imagej.ops.special.computer.Computers;
import net.imagej.ops.special.computer.UnaryComputerOp;
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.type.numeric.integer.UnsignedVariableBitLengthType;
import net.imglib2.util.Pair;

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

/**
 * @author Martin Horn (University of Konstanz)
 * @author Gabe Selzer
 */
@Plugin(type = Ops.Image.Invert.class, priority = Priority.NORMAL)
public class InvertII<I extends RealType<I>, O extends RealType<O>> extends
	AbstractUnaryComputerOp<IterableInterval<I>, IterableInterval<O>> implements
	Ops.Image.Invert
{

	@Parameter(required = false)
	private I min;

	@Parameter(required = false)
	private I max;

	private UnaryComputerOp<IterableInterval<I>, IterableInterval<O>> mapper;

	@Override
	public void compute(final IterableInterval<I> input,
		final IterableInterval<O> output)
	{
		if (mapper == null) {
			final double minValue = min == null ? input.firstElement().getMinValue() : //
				min.getRealDouble();
			final double maxValue = max == null ? input.firstElement().getMaxValue() : //
				max.getRealDouble();
			final double minMax = maxValue + minValue;
			mapper = Computers.unary(ops(), Ops.Map.class, output, input,
				new AbstractUnaryComputerOp<I, O>()
			{

					@Override
					public void compute(I in, O out) {
						if ((minMax - in
							.getRealDouble()) <= out.getMinValue()) out.setReal(out
								.getMinValue());
						else if ((minMax - in
							.getRealDouble()) >= out.getMaxValue()) out.setReal(out
								.getMaxValue());
						else out.setReal(minMax - in.getRealDouble());
					}
				});
		}
		mapper.compute(input, output);
	}

	public static <T extends RealType<T>> T minValue(T type) {
		if (type instanceof UnsignedVariableBitLengthType) {
			return (T) new UnsignedVariableBitLengthType(0, 1);
		}
		else {
			T min = type.createVariable();
			min.setReal(min.getMinValue());
			return min;
		}
	}

}

@@ -3165,32 +3165,24 @@
 (minMax - in
-%0A%09%09%09%09%09%09%09
 .getRealDoub
@@ -3201,32 +3201,41 @@
 .getMinValue()) 
+%7B%0A%09%09%09%09%09%09%09
 out.setReal(out%0A
@@ -3229,33 +3229,24 @@
 .setReal(out
-%0A%09%09%09%09%09%09%09%09
 .getMinValue
@@ -3242,32 +3242,40 @@
 getMinValue());%0A
+%09%09%09%09%09%09%7D%0A
 %09%09%09%09%09%09else if ((
@@ -3285,24 +3285,16 @@
 Max - in
-%0A%09%09%09%09%09%09%09
 .getReal
@@ -3325,16 +3325,25 @@
 alue()) 
+%7B%0A%09%09%09%09%09%09%09
 out.setR
@@ -3353,17 +3353,8 @@
 (out
-%0A%09%09%09%09%09%09%09%09
 .get
@@ -3362,24 +3362,32 @@
 axValue());%0A
+%09%09%09%09%09%09%7D%0A
 %09%09%09%09%09%09else o

Improve logic for smaller types