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

import java.math.BigInteger;

import net.imagej.ops.Contingent;
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.types.UnboundedIntegerType;
import net.imglib2.IterableInterval;
import net.imglib2.type.numeric.IntegerType;
import net.imglib2.type.numeric.RealType;
import net.imglib2.type.numeric.integer.LongType;
import net.imglib2.type.numeric.integer.Unsigned128BitType;
import net.imglib2.type.numeric.integer.UnsignedLongType;

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

/**
 * @author Gabe Selzer
 */
@Plugin(type = Ops.Image.Invert.class, priority = Priority.HIGH)
public class InvertIIInteger<T extends RealType<T>> extends
	AbstractUnaryComputerOp<IterableInterval<T>, IterableInterval<T>> implements
	Contingent, Ops.Image.Invert
{

	@Parameter(required = false)
	private T min;

	@Parameter(required = false)
	private T max;

	private UnaryComputerOp<IterableInterval<T>, IterableInterval<T>> mapper;


	@Override
	public void compute(final IterableInterval<T> input,
		final IterableInterval<T> output)
	{
		if (mapper == null) {
			final BigInteger minValue = min == null ? ((IntegerType)(minValue(input.firstElement()))).getBigInteger() : ((IntegerType)min).getBigInteger();
			final BigInteger maxValue = max == null ? ((IntegerType)(maxValue(input.firstElement()))).getBigInteger() : ((IntegerType)max).getBigInteger();
			final BigInteger minMax = minValue.add(maxValue);
			mapper = Computers.unary(ops(), Ops.Map.class, output, input,
				new AbstractUnaryComputerOp<T, T>()
			{

					@Override
					public void compute(T in, T out) {
						BigInteger inverted = minMax.subtract(((IntegerType)in).getBigInteger());	
						
						if( inverted.compareTo(((IntegerType)minValue(out)).getBigInteger()) <= 0) out.set(minValue(out));
						else if(inverted.compareTo(((IntegerType)maxValue(out)).getBigInteger()) >= 0) out.set(maxValue(out));
						else ((IntegerType)out).setBigInteger(inverted);
						
						
					}
				});
		}
		mapper.compute(input, output);

	}

	public static <T extends RealType<T>> T minValue(T type) {
		// TODO: Consider making minValue an op.
		final T min = type.createVariable();
		if (type instanceof UnboundedIntegerType) min.setReal(0);
		else min.setReal(min.getMinValue());
		return min;

	}

	public static <T extends RealType<T>> T maxValue(T type) {
		// TODO: Consider making maxValue an op.
		final T max = type.createVariable();
		if (max instanceof Unsigned128BitType) {
			final Unsigned128BitType t = (Unsigned128BitType) max;
			t.set(t.getMaxBigIntegerValue());
		}
		else if (max instanceof UnsignedLongType) {
			final UnsignedLongType t = (UnsignedLongType) max;
			t.set(t.getMaxBigIntegerValue());
		}
		else if (max instanceof UnboundedIntegerType) {
			max.setReal(0);
		}
		else {
			max.setReal(type.getMaxValue());
		}
		return max;
	}

	@Override
	public boolean conforms() {
		final T inType = in().firstElement();
		return inType instanceof Unsigned128BitType ||
			inType instanceof UnsignedLongType ||
			inType instanceof UnboundedIntegerType ||
			inType instanceof LongType;
	}

}

@@ -4688,23 +4688,23 @@
 nt();%0A%09%09
-return 
+%0A%09%09if (
 inType i
@@ -4717,144 +4717,200 @@
 eof 
-Unsigned128Bit
+Integer
 Type
- %7C%7C
+) %7B
 %0A%09%09%09
+f
 in
-Type instanceof UnsignedLongType %7C%7C%0A%09%09%09inType instanceof UnboundedIntegerType %7C%7C%0A%09%09%09inType instanceof LongTyp
+al IntegerType%3C?%3E copy = (IntegerType%3C?%3E) inType.createVariable();%0A%09%09%09copy.setInteger(Long.MAX_VALUE);%0A%09%09%09return copy.getIntegerLong() == Long.MAX_VALUE;%0A%09%09%7D%0A%09%09return fals
 e;%0A%09

Refactor conforms to allow all large types