#! /bin/sh
# This script tests: pamarith
# Also requires: pamtopnm rgb3toppm pamenlarge pamcat pamseq pbmmake pgmmake
# Also requires: ppmpat pamchannel

tmpdir=${tmpdir:-/tmp}
input1_pgm=${tmpdir}/input1.pgm
input2_pgm=${tmpdir}/input2.pgm
output_pgm=${tmpdir}/output.pgm
input1_ppm=${tmpdir}/input1.ppm
input2_ppm=${tmpdir}/input2.ppm

echo "Test 1"

pamseq 1 15 | pamtopnm -assume > ${input1_pgm}
pgmmake -maxval 15 0.15 16 1 > ${input2_pgm}

rgb3toppm ${input1_pgm} ${input1_pgm} ${input1_pgm} > ${input1_ppm}
rgb3toppm ${input2_pgm} ${input2_pgm} ${input2_pgm} > ${input2_ppm}

pamcat -tb ${input1_pgm} ${input2_pgm} -plain

for fn in "-add" "-subtract" "-multiply" "-divide" "-difference" \
    "-minimum" "-maximum" "-mean" "-compare" "-equal" \
    "-and" "-or" "-nand" "-nor"  "-xor" \
    "-shiftleft" "-shiftright"
  do
  echo ${fn}
  pamarith ${fn} -plain ${input1_pgm} ${input2_pgm} | tr '\n' ' '; echo
  pamarith ${fn} ${input1_pgm} ${input2_pgm} > ${output_pgm}
  rgb3toppm  ${output_pgm} ${output_pgm} ${output_pgm} | cksum
  pamarith ${fn} ${input1_ppm} ${input2_pgm} | cksum
  pamarith ${fn} ${input1_ppm} ${input2_ppm} | cksum
  done

rm ${input1_pgm} ${input2_pgm} ${output_pgm} ${input2_ppm}

input1_pbm=${tmpdir}/input1.pbm
input2_pbm=${tmpdir}/input2.pbm

echo "Test 2 PBM"

pbmmake -g 8 1 > ${input1_pbm}
pbmmake -g 2 1 | pamenlarge -xscale=4 > ${input2_pbm}

pamcat -tb -plain ${input1_pbm} ${input2_pbm}

for fn in "-add" "-subtract" "-multiply" "-divide" "-difference" \
    "-minimum" "-maximum" "-mean" "-compare" "-equal" \
    "-and" "-or" "-nand" "-nor"  "-xor" \
    "-shiftleft" "-shiftright"
  do
  echo ${fn}
  pamarith ${fn} -plain ${input1_pbm} ${input2_pbm} | tr '\n' ' '; echo
  done

rm ${input1_pbm} ${input2_pbm}

echo "Test 3"

input3_ppm=${tmpdir}/input3.ppm
input4_ppm=${tmpdir}/input4.ppm
input4_pgm=${tmpdir}/input4.pgm

ppmpat -tartan  -color=rgb:f0/f0/00,rgb:80/00/80,rgb:0f/00/0f 20 15 \
  > ${input3_ppm}
ppmpat -argyle2 -color=rgb:01/01/01,rgb:02/02/02,rgb:05/05/05 20 15 \
  > ${input4_ppm}
pamchannel -infile ${input4_ppm} 0 > ${input4_pgm}

for fn in "-add" "-multiply" "-difference" \
    "-minimum" "-maximum" "-mean" "-equal" \
    "-and" "-or" "-nand" "-nor" "-xor"
  do
  echo ${fn}
  pamarith ${fn} ${input3_ppm} ${input4_ppm} | cksum
  pamarith ${fn} ${input4_ppm} ${input3_ppm} | cksum
  pamarith ${fn} ${input3_ppm} ${input4_pgm} | pamtopnm | cksum
  done

for fn in "-subtract" "-divide" "-compare" "-shiftleft" "-shiftright"
  do
  echo ${fn}
  pamarith ${fn} ${input3_ppm} ${input4_ppm} | cksum
  pamarith ${fn} ${input3_ppm} ${input4_pgm} | pamtopnm | cksum
  done

rm ${input3_ppm} ${input4_ppm} ${input4_pgm}

echo "Test 4 (input = output)"
echo "Prints 281226646 481 six times, then 2921940274 59 six times"

for image in maze.pbm ${input1_ppm}
  do
  echo "input image"
  cat ${image} | cksum
  for fn in "-minimum" "-maximum" "-mean" "-and" "-or"
    do
    echo ${fn}
    pamarith ${fn} ${image} ${image} | cksum
    done
  done


echo "Test 5 (blank output)"
echo "Prints 2247801875 481 three times, then 320101399 59 three times"

for image in maze.pbm ${input1_ppm}
  do
  for fn in "-subtract" "-difference" "-xor"
    do
    echo ${fn}
    pamarith ${fn} ${image} ${image} | cksum
    done
  done

rm ${input1_ppm}

echo "Test Invalid"

. ${srcdir}/test-invalid.inc

# multiple functions

invCmd "pamarith -add -subtract testimg.ppm testimg.ppm"
invCmd "pamarith -multiply -divide testimg.ppm testimg.ppm"
invCmd "pamarith -difference -minimum testimg.ppm testimg.ppm"
invCmd "pamarith -maximum -mean testimg.ppm testimg.ppm"
invCmd "pamarith -compare -and testimg.ppm testimg.ppm"
invCmd "pamarith -compare -equal testimg.ppm testimg.ppm"
invCmd "pamarith -or -nand testimg.ppm testimg.ppm"
invCmd "pamarith -nor -xor testimg.ppm testimg.ppm"
invCmd "pamarith -shiftleft -shiftright testimg.ppm testimg.ppm"

# -add does not take a value

invCmd "pamarith -add=1 testimg.ppm testimg.ppm"

# No function

invCmd "pamarith -plain testimg.ppm testimg.ppm"
invCmd "pamarith testimg.ppm testimg.ppm"

# Just one input image file

invCmd "pamarith -add testimg.ppm"

# No input image file

invCmd "pamarith -add"

# Input images with different depth (number of planes)

input1_pam=${tmpdir}/input1.pam

pamchannel -infile testimg.ppm 0 1 > ${input1_pam}
invCmd "pamarith -add testimg.ppm ${input1_pam}"
rm ${input1_pam}

# Input images with different x/y dimensions

invCmd "pamarith -add testimg.ppm testgrid.pbm"

input1_pbm=${tmpdir}/input1.pbm
input2_pbm=${tmpdir}/input2.pbm

pamenlarge -xscale=2 testgrid.pbm > ${input1_pbm}
invCmd "pamarith -add testgrid.pbm ${input1_pbm}"
rm ${input1_pbm}

pamenlarge -yscale=3 testgrid.pbm > ${input2_pbm}
invCmd "pamarith -add testgrid.pbm ${input2_pbm}"
rm ${input2_pbm}

# Invalid usage of -closeness

invCmd "pamarith -equal -closeness=100.1 testgrid.pbm"
invCmd "pamarith -equal -closeness=-10 testgrid.pbm"
invCmd "pamarith -closeness -equal testgrid.pbm"
invCmd "pamarith -compare -closeness=10 testgrid.pbm"

# Bit string functions
# Create PGM test input

input3_pgm=${tmpdir}/input3.pgm
input4_pgm=${tmpdir}/input4.pgm
input5_pgm=${tmpdir}/input5.pgm

pgmmake -maxval=4095 1.0 3 1 > ${input3_pgm}
pgmmake -maxval=4096 1.0 3 1 > ${input4_pgm}
pgmmake -maxval=8191 1.0 3 1 > ${input5_pgm}

# Bit string functions - Maxval must match

invCmd "pamarith -and ${input3_pgm} ${input5_pgm}"
invCmd "pamarith -or ${input3_pgm} ${input5_pgm}"
invCmd "pamarith -nand ${input3_pgm} ${input5_pgm}"
invCmd "pamarith -nor ${input3_pgm} ${input5_pgm}"
invCmd "pamarith -xor ${input3_pgm} ${input5_pgm}"

# Bit string functions - Maxval must be 2^n -1

invCmd "pamarith -and ${input4_pgm} ${input4_pgm}"
invCmd "pamarith -or ${input4_pgm} ${input4_pgm}"
invCmd "pamarith -nand ${input4_pgm} ${input4_pgm}"
invCmd "pamarith -nor ${input4_pgm} ${input4_pgm}"
invCmd "pamarith -xor ${input4_pgm} ${input4_pgm}"
invCmd "pamarith -shiftleft ${input4_pgm} ${input4_pgm}"
invCmd "pamarith -shiftright ${input4_pgm} ${input4_pgm}"

rm ${input3_pgm} ${input4_pgm} ${input5_pgm}

# Functions that do not allow more than two input images.
# Only the functions that are commutative and associative allow
# three or more inputs.

invCmd "pamarith -subtract testimg.ppm testimg.ppm testimg.ppm"
invCmd "pamarith -divide testimg.ppm testimg.ppm testimg.ppm"
invCmd "pamarith -compare testimg.ppm testimg.ppm testimg.ppm"
invCmd "pamarith -difference testimg.ppm testimg.ppm testimg.ppm"
invCmd "pamarith -shiftleft testimg.ppm testimg.ppm testimg.ppm"
invCmd "pamarith -shiftright testimg.ppm testimg.ppm testimg.ppm"

# Currently -equal and -mean do not allow more than two input images.
# These two cases should be removed once improvements are made.

invCmd "pamarith -equal testgrid.pbm testgrid.pbm testgrid.pbm"
invCmd "pamarith -mean testgrid.pbm  testgrid.pbm testgrid.pbm"
