<?xml version='1.0' encoding='UTF-8'?>
<tool id="imagej2_math" name="Operate on pixels" version="@WRAPPER_VERSION@.0">
    <description>with a mathematical expression</description>
    <macros>
        <import>imagej2_macros.xml</import>
    </macros>
    <expand macro="fiji_requirements" />
    <command>
<![CDATA[
    python $__tool_directory__/imagej2_math.py
    --input "$input"
    --input_datatype $input.ext
    --operation $operation_cond.operation
    #if str( $operation_cond.operation ) == 'Macro':
        --expression "$operation_cond.expression"
    #else if str( $operation_cond.operation ) in [ 'AND', 'OR', 'XOR' ]:
        --bin_constant $operation_cond.bin_constant
    #else if str( $operation_cond.operation ) not in [ 'Log', 'Exp', 'Square', 'Square_Root', 'Abs', 'Reciprocal' ]:
        --float_constant $operation_cond.float_constant
    #end if
    --jython_script $__tool_directory__/imagej2_math_jython_script.py
    --output_datatype $output.ext
    --output "$output"
]]>
    </command>
    <inputs>
        <param format="bmp,eps,gif,jpg,pcx,pgm,png,psd,tiff" name="input" type="data" label="Select image"/>
            <!--
            NOTE: The NaN Background option is not included here because it only works with 32-bit
            thresholded images, and incorporating the functionality from the Adjust threshold tool
            here for this option would be too messy.
            -->
            <conditional name="operation_cond">
                <param name="operation" type="select" label="Operation">
                    <option value="Add">Add</option>
                    <option value="Subtract">Subtract</option>
                    <option value="Multiply">Multiply</option>
                    <option value="Divide">Divide</option>
                    <option value="AND">AND</option>
                    <option value="OR">OR</option>
                    <option value="XOR">XOR</option>
                    <option value="Min">Min</option>
                    <option value="Max">Max</option>
                    <option value="Gamma">Gamma</option>
                    <option value="Set">Set</option>
                    <option value="Log">Log</option>
                    <option value="Exp">Exp</option>
                    <option value="Square">Square</option>
                    <option value="Square_Root">Square Root</option>
                    <option value="Abs">Abs</option>
                    <option value="Reciprocal">Reciprocal</option>
                    <option value="Macro">Macro</option>
                </param>
                <when value="Add">
                    <param name="float_constant" type="float" value="25.0" label="Value" help="Value is a floating point number" />
                </when>
                <when value="Subtract">
                    <param name="float_constant" type="float" value="25.0" label="Value" help="Value is a floating point number" />
                </when>
                <when value="Multiply">
                    <param name="float_constant" type="float" value="1.250" label="Value" help="Value is a floating point number" />
                </when>
                <when value="Divide">
                    <param name="float_constant" type="float" value="1.250" label="Value" help="Value is a floating point number" />
                </when>
                <when value="AND">
                    <param name="bin_constant" type="integer" value="11110000" label="Value (binary)" help="Value is a binary number" />
                </when>
                <when value="OR">
                    <param name="bin_constant" type="integer" value="11110000" label="Value (binary)" help="Value is a binary number" />
                </when>
                <when value="XOR">
                    <param name="bin_constant" type="integer" value="11110000" label="Value (binary)" help="Value is a binary number" />
                </when>
                <when value="Min">
                    <param name="float_constant" type="float" value="0" label="Value" help="Value is a floating point number" />
                </when>
                <when value="Max">
                    <param name="float_constant" type="float" value="255" label="Value" help="Value is a floating point number" />
                </when>
                <when value="Gamma">
                    <param name="float_constant" type="float" value="0.50" label="Value" help="Value is a floating point number" />
                </when>
                <when value="Set">
                    <param name="float_constant" type="float" value="25" label="Value" help="Value is a floating point number" />
                </when>
                <when value="Log" />
                <when value="Exp" />
                <when value="Square" />
                <when value="Square_Root" />
                <when value="Abs" />
                <when value="Reciprocal" />
                <when value="Macro">
                    <param name="expression" type="text" value="v=v+50*sin(d/17)" label="Code" help="v=pixel value, x,y and z=pixel coordinates, w=image width, h=image height, a=angle, d=distance from center" />
                </when>
            </conditional>
    </inputs>
    <outputs>
        <data name="output" format_source="input" label="${tool.name} on ${on_string}: ${operation_cond.operation}"/>
    </outputs>
    <tests>
        <test>
            <param name="input" value="blobs.gif" />
            <param name="operation" value="Multiply" />
            <output name="output" file="blobs_multiply.gif" compare="sim_size" />
        </test>
        <test>
            <param name="input" value="blobs.gif" />
            <param name="operation" value="Min" />
            <param name="float_constant" value="255" />
            <output name="output" file="blobs_min.gif" compare="sim_size" />
        </test>
        <test>
            <param name="input" value="blobs.gif" />
            <param name="operation" value="Log" />
            <output name="output" file="blobs_log.gif" compare="sim_size" />
        </test>
        <test>
            <param name="input" value="blobs.gif" />
            <param name="operation" value="Square" />
            <output name="output" file="blobs_square.gif" compare="sim_size" />
        </test>
        <test>
            <param name="input" value="blobs.gif" />
            <param name="operation" value="Macro" />
            <output name="output" file="blobs_macro.gif" compare="sim_size" />
        </test>
    </tests>
    <help>

**What it does**

<![CDATA[

Applies a mathematical expression (add, subtract, multiply, etc.) to each pixel in the image.  When the
resulting pixel value overflows/underflows the legal range of the image's data type, the value is reset
to the maximum/minimum value.

- **Add** - Adds a value to each pixel in the image.  With 8-bit images, results greater than 255 are set to 255. nWith 16-bit signed images, results greater than 65,535 are set to 65,535.
- **Subtract** - Subtracts a value from each pixel in the image.  With 8-bit and 16-bit images, results less than 0 are set to 0.
- **Multiply** - Multiplies each pixel in the image by a value.  With 8-bit images, results greater than 255 are set to 255.  With 16-bit signed images, results greater than 65,535 are set to 65,535.
- **Divide** - Divides each pixel in the image by a value.  Except for 32-bit (float) images, attempts to divide by zero are ignored.  With 32-bit images, dividing by zero results in either NaN (0/0) or Infinity.
- **AND** - Performs a bitwise AND of each pixel in the image with the value.
- **OR** - Performs a bitwise OR of each pixel in the image with the value.
- **XOR** - Performs a bitwise XOR of each pixel in the image with the value.
- **Min** - Pixels in the image less than the value are replaced by the value.
- **Max** - Pixels in the image greater than the value are replaced by the value.
- **Gamma** - Applies the function f(p) = (p/255)^gamma*255 to each pixel (p) in the image, where 0.1 <= gamma <= 5.0.  For RGB images, this function is applied to all three color channels.  For 16-bit images, the image min and max are used for scaling instead of 255.
- **Set** - Fills the image with the value.
- **Log** - For 8-bit images, applies the function f(p) = log(p) * 255/log(255) to each pixel (p) in the image.  For RGB images, this function is applied to all three color channels.  For 16-bit images, the image min and max are used for scaling instead of 255.  For float images, no scaling is done.  To calculate log10 of the image, multiply the result of this operation by 0.4343 (1/log(10).
- **Exp** - Performs an exponential transform on the image.
- **Square** - Performs a square transform on the image.
- **Square Root** - Performs a square root transform on the image.
- **Abs** - Performs an absolute value transformation on the image.  This option works only with 32-bit or signed 16-bit images.  Other image types are automatically converted to 32-bit when this option is selected.
- **Reciprocal** - Generates the reciprocal of the image.  This option works only with 32-bit images.  Other image types are automatically converted to 32-bit when this option is selected.
- **Macro** - Performs image arithmetic using a user-specified expression.  It can be used to create fully-synthetic images or to perform precise pixel manipulations on existing images.

]]>

    </help>
    <expand macro="fiji_headless_citations" />
</tool>
