#! /bin/sh
# This script tests: pamflip
# Also requires: ppmpat pamseq pamtopnm

tmpdir=${tmpdir:-/tmp}
tartan_ppm=${tmpdir}/test.ppm
test1_pgm=${tmpdir}/test1.pgm
test2_pgm=${tmpdir}/test2.pgm
dot_p_ppm=${tmpdir}/dot.p.ppm
dot_ppm=${tmpdir}/dot.ppm
dot_plain_ppm=${tmpdir}/dot.plain.ppm

# define function
cmptarget () { cmp -s - $1 && echo "match" || echo "no match";  }

echo "Test 1.  Should print match twenty-five times"

ppmpat -tartan -mesh \
       -color=rgb:0/0/0,rgb:ff/00/ff,rgb:80/90/20 31 32 > ${tartan_ppm}

pamflip -null ${tartan_ppm} | \
    cmptarget ${tartan_ppm}

for flip in -null -lr -tb -rotate180 -xy -reflect
  do
  pamflip ${flip} ${tartan_ppm} | pamflip ${flip} | \
      cmptarget ${tartan_ppm}    
  pamflip ${flip} ${tartan_ppm} | pamflip ${flip} -inverse | \
      cmptarget ${tartan_ppm}    
  done

for flip in -rotate90 -rotate270 
  do
  pamflip ${flip} ${tartan_ppm} | pamflip ${flip} -inverse | \
      cmptarget ${tartan_ppm}
  done

pamflip -cw  ${tartan_ppm} | pamflip -ccw | \
  cmptarget ${tartan_ppm}    
pamflip -ccw ${tartan_ppm} | pamflip -cw | \
  cmptarget ${tartan_ppm}    

pamflip -xform=leftright ${tartan_ppm} | pamflip -leftright | \
  cmptarget ${tartan_ppm}

pamflip -xform=topbottom ${tartan_ppm} | pamflip -topbottom | \
  cmptarget ${tartan_ppm}

pamflip -xform=transpose ${tartan_ppm} | pamflip -transpose | \
  cmptarget ${tartan_ppm}

pamflip -xform=leftright,leftright ${tartan_ppm} | \
  cmptarget ${tartan_ppm}

pamflip -xform=topbottom,topbottom ${tartan_ppm} | \
  cmptarget ${tartan_ppm}

pamflip -xform=transpose,transpose ${tartan_ppm} | \
  cmptarget ${tartan_ppm}

pamflip -tb ${tartan_ppm} | pamflip -lr | \
  pamflip -xform=leftright,topbottom | \
  cmptarget ${tartan_ppm}

pamflip -tb ${tartan_ppm} | pamflip -lr | pamflip -xy | \
  pamflip -xform=leftright,topbottom,transpose | \
  cmptarget ${tartan_ppm}

echo "Should print 1988581932 2989"
cat ${tartan_ppm} | cksum

rm ${tartan_ppm}

pamseq 1 15 | pamtopnm -assume > ${test1_pgm}

echo "Test 2.  Should print match eleven times"
pamflip -null ${test1_pgm} | \
    cmptarget ${test1_pgm}

for flip in -null -lr -tb -rotate180 -xy -reflect
  do
  pamflip ${flip} ${test1_pgm} | pamflip ${flip} | \
    cmptarget ${test1_pgm}    
  done

pamflip -r90 ${test1_pgm} | pamflip -r270 | \
  cmptarget ${test1_pgm}

pamflip -r270 ${test1_pgm} | pamflip -r90 | \
  cmptarget ${test1_pgm}

pamflip -r90 ${test1_pgm} | pamflip -r90 | \
  pamflip -r90 | pamflip -r90 | \
  cmptarget ${test1_pgm}

pamflip -r270 ${test1_pgm} | pamflip -r270 | \
  pamflip -r270 | pamflip -r270 | \
  cmptarget ${test1_pgm}

echo "Should print 2729474106 27"
cat ${test1_pgm} | cksum


pamflip -r180 ${test1_pgm} > ${test2_pgm}

rm ${test1_pgm}


echo "Test 3."

echo "Should print match eleven times"

pamflip -null ${test2_pgm} | \
  cmptarget ${test2_pgm}

for flip in -null -lr -tb -rotate180 -xy -reflect
  do
  pamflip ${flip} ${test2_pgm} | pamflip ${flip} | \
    cmptarget ${test2_pgm}    
  done

pamflip -r90 ${test2_pgm} | pamflip -r270 | \
  cmptarget ${test2_pgm}

pamflip -r270 ${test2_pgm} | pamflip -r90 | \
  cmptarget ${test2_pgm}

pamflip -r90 ${test2_pgm} | pamflip -r90 | \
    pamflip -r90 | pamflip -r90 | \
  cmptarget ${test2_pgm}

pamflip -r270 ${test2_pgm} | pamflip -r270 | \
    pamflip -r270 | pamflip -r270 | \
  cmptarget ${test2_pgm}


echo "Should print no match five times"

pamflip -cw ${test2_pgm}  | pamflip -cw | \
  cmptarget ${test2_pgm}

pamflip -ccw ${test2_pgm} | pamflip -ccw | \
  cmptarget ${test2_pgm}

pamflip -xy ${test2_pgm} | pamflip -reflect | \
  cmptarget ${test2_pgm}

pamflip -r90 ${test2_pgm} | pamflip -r90 | pamflip -r90 | \
  cmptarget ${test2_pgm}

pamflip -r270 ${test2_pgm} | pamflip -r270 | pamflip -r270 | \
  cmptarget ${test2_pgm}


echo "Should print 1849343241 27"
cat ${test2_pgm} | cksum

rm ${test2_pgm}


echo "Test 4. Should print a single pixel PPM image maxval 65535 in"
echo "plain (ascii) format; then match seventeen times"

cat > ${dot_plain_ppm} <<EOF
P3
1 1
65535
1 10000 65535
EOF

cat ${dot_plain_ppm} | pamflip -null | tee ${dot_ppm} | pamflip -null -plain
rm ${dot_plain_ppm}

# The next seven are not round-trip
pamflip -null ${dot_ppm} | \
  cmptarget ${dot_ppm}

pamflip -lr ${dot_ppm} | \
  cmptarget ${dot_ppm}

pamflip -tb ${dot_ppm} | \
  cmptarget ${dot_ppm}

pamflip -xy ${dot_ppm} | \
  cmptarget ${dot_ppm}

pamflip -reflect ${dot_ppm} | \
  cmptarget ${dot_ppm}

pamflip -r90 ${dot_ppm} | \
  cmptarget ${dot_ppm}

pamflip -r180 ${dot_ppm} | \
  cmptarget ${dot_ppm}

pamflip -r270 ${dot_ppm} | \
  cmptarget ${dot_ppm}


pamflip -lr ${dot_ppm} | pamflip -lr | \
  cmptarget ${dot_ppm}

pamflip -tb ${dot_ppm} | pamflip -tb | \
  cmptarget ${dot_ppm}

pamflip -r180 ${dot_ppm} | pamflip -r180 | \
  cmptarget ${dot_ppm}

pamflip -xy ${dot_ppm} | pamflip -xy | \
  cmptarget ${dot_ppm}

pamflip -reflect ${dot_ppm} | pamflip -reflect | \
  cmptarget ${dot_ppm}

pamflip -r90 ${dot_ppm} | pamflip -r270 | \
  cmptarget ${dot_ppm}

pamflip -r270 ${dot_ppm} | pamflip -r90 | \
  cmptarget ${dot_ppm}

pamflip -r90 ${dot_ppm} | pamflip -r90 | pamflip -r90 | pamflip -r90 | \
  cmptarget ${dot_ppm}

pamflip -r270 ${dot_ppm} | pamflip -r270 | \
  pamflip -r270 | pamflip -r270 | \
  cmptarget ${dot_ppm}
 

echo "Should print 2434897823 19"
cat ${dot_ppm} | cksum

rm ${dot_ppm}


