#! /bin/sh
# This script tests: pamcat
# Also requires: pbmmake pgmramp pgmnoise pamundice ppmtoppm

tmpdir=${tmpdir:-/tmp}

check2x2_pbm=${tmpdir}/check2x2.pbm
options_h=${tmpdir}/options_h
options_v=${tmpdir}/options_v

echo "Test 1.  Should print a simple PBM image five times"
# Check input from stdin and from designated input file
# Similar to tests in stdin-p?m.test

pbmmake -g 2 2 | tee ${check2x2_pbm} | pamcat -lr -plain
pamcat -tb -plain ${check2x2_pbm}
pamcat -tb -plain < ${check2x2_pbm}
pamcat -lr -black -plain ${check2x2_pbm}
cat ${check2x2_pbm} | pamcat -tb -white -plain

cat > ${options_h} <<EOF
-leftright
-lr -jtop
--leftright --jcenter
-lr --jbottom
--leftright --white
-lr -black
EOF

cat > ${options_v} <<EOF
-topbottom
--topbottom --jleft
--tb -jcenter
--topbottom --jright
-tb -white
--topbottom --black
EOF

echo "Test 2.  Should print 3141425598 9, then match twelve times"
cat ${check2x2_pbm} | cksum

cat ${options_h} ${options_v} | \
while read opt
  do
  pamcat ${opt} ${check2x2_pbm} | \
    cmp -s - ${check2x2_pbm} && echo "match" || echo "no match"	  
  done

rm ${check2x2_pbm}


maze2h_pbm=${tmpdir}/maze2h.pbm
maze2v_pbm=${tmpdir}/maze2v.pbm
maze3h_pbm=${tmpdir}/maze3h.pbm
maze3v_pbm=${tmpdir}/maze3v.pbm

echo "Test 3.  Should print 2197356643 895, then match seven times"
pamundice -quiet -across 2 maze.pbm | \
    tee ${maze2h_pbm} | cksum

cat ${options_h} | \
while read opt
  do
  pamcat ${opt} maze.pbm maze.pbm | \
    cmp -s - ${maze2h_pbm} && echo "match" || echo "no match"	  
  done
cat maze.pbm | pamcat -lr -black maze.pbm - | \
    cmp -s - ${maze2h_pbm} && echo "match" || echo "no match"


echo "Test 4.  Should print 3313353797 954, then match six times"
pamundice -quiet -down 2 maze.pbm | tee ${maze2v_pbm} | cksum

cat ${options_v} | \
while read opt
  do
  pamcat ${opt} maze.pbm maze.pbm | \
    cmp -s - ${maze2v_pbm} && echo "match" || echo "no match"
  done


echo "Test 5.  Should print 1731660895 1308, then match eight times"
pamundice -quiet -across 3 maze.pbm | tee ${maze3h_pbm} | cksum

cat ${options_h} | \
while read opt
  do
  pamcat ${opt} ${maze2h_pbm} maze.pbm | \
    cmp -s - ${maze3h_pbm} && echo "match" || echo "no match"
  done
pamcat -lr maze.pbm ${maze2h_pbm} |  \
    cmp -s - ${maze3h_pbm} && echo "match" || echo "no match"

cat ${maze2h_pbm} | pamcat -lr - maze.pbm | \
    cmp -s - ${maze3h_pbm} && echo "match" || echo "no match"


echo "Test 6.  Should print 2985957591 1426, then match nine times"
pamundice -quiet -down 3 maze.pbm | tee ${maze3v_pbm} | cksum

cat ${options_v} | \
while read opt
  do
  pamcat ${opt} ${maze2v_pbm} maze.pbm | \
    cmp -s - ${maze3v_pbm} && echo "match" || echo "no match"	  
  done
pamcat -tb maze.pbm maze.pbm maze.pbm | \
    cmp -s - ${maze3v_pbm} && echo "match" || echo "no match"
pamcat -tb maze.pbm ${maze2v_pbm} | \
    cmp -s - ${maze3v_pbm} && echo "match" || echo "no match"
cat maze.pbm | pamcat -tb - ${maze2v_pbm} | \
    cmp -s - ${maze3v_pbm} && echo "match" || echo "no match"

rm ${maze2h_pbm}
rm ${maze2v_pbm}
rm ${maze3h_pbm}
rm ${maze3v_pbm}


echo "Test 7.  Should print a simple PGM image three times"
# Use sed to scrape off space at line end for compatibility
# with older versions of pamcat

diag4_pgm=${tmpdir}/diag4.pgm

pgmramp -diag 4 4 -maxval=3 | tee ${diag4_pgm} | pamcat -lr -plain |\
  sed 's/ *$//'
pamcat -tb -plain ${diag4_pgm} | sed 's/ *$//'
cat ${diag4_pgm} | pamcat -tb -plain | sed 's/ *$//'

rm ${diag4_pgm}

noise_pgm=${tmpdir}/noise.pgm
noise6h_pgm=${tmpdir}/noiseh6.pgm
noise5v_pgm=${tmpdir}/noisev5.pgm

pgmnoise -randomseed=100 4 4 > ${noise_pgm}

echo "Test 8.  Should print 1102493995 27, then match twelve times"
pgmnoise -randomseed=100 4 4 | tee ${noise_pgm} | cksum

cat ${options_h} ${options_v} | \
while read opt
  do
  pamcat ${opt} ${noise_pgm} | cmp -s ${noise_pgm} - && echo "match" || echo "no match"
  done


echo "Test 9.  Should print 3418874152 108, then match eight times"
pamundice -quiet -across 6 ${noise_pgm} | tee ${noise6h_pgm} | cksum

cat ${options_h} | \
while read opt
  do
  pamcat ${opt} ${noise_pgm} ${noise_pgm} ${noise_pgm} \
         ${noise_pgm} ${noise_pgm} ${noise_pgm} | \
  cmp -s ${noise6h_pgm} - && echo "match" || echo "no match"

  done
pamcat -lr ${noise_pgm} ${noise_pgm} | \
    pamcat -lr - ${noise_pgm} ${noise_pgm} ${noise_pgm} ${noise_pgm} | \
    cmp -s ${noise6h_pgm} - && echo "match" || echo "no match"

pamcat -lr ${noise_pgm} ${noise_pgm} ${noise_pgm} | \
    pamcat -lr ${noise_pgm} - ${noise_pgm} ${noise_pgm} | \
    cmp -s ${noise6h_pgm} - && echo "match" || echo "no match"

rm ${noise6h_pgm}


echo "Test 10.  Should print 481335892 92, then match eight times"
pamundice -quiet -down 5 ${noise_pgm} | tee ${noise5v_pgm} | cksum

cat ${options_v} | \
while read opt
  do
  pamcat ${opt} ${noise_pgm} ${noise_pgm} ${noise_pgm} ${noise_pgm} ${noise_pgm} | \
    cmp -s ${noise5v_pgm} - && echo "match" || echo "no match"
  done
pamcat -tb ${noise_pgm} ${noise_pgm} ${noise_pgm} | \
    pamcat -tb ${noise_pgm} ${noise_pgm} - | \
    cmp -s ${noise5v_pgm} - && echo "match" || echo "no match"    
pamcat -tb ${noise_pgm} ${noise_pgm} ${noise_pgm} ${noise_pgm} | \
    pamcat -tb - ${noise_pgm} | \
    cmp -s ${noise5v_pgm} - && echo "match" || echo "no match"     

rm ${noise5v_pgm}
rm ${noise_pgm}

diag8_ppm=${tmpdir}/diag8.ppm
diag2h_ppm=${tmpdir}/diag2h.ppm
diag2v_ppm=${tmpdir}/diag2v.ppm
diag3h_ppm=${tmpdir}/diag3h.ppm
diag3v_ppm=${tmpdir}/diag3v.ppm

pgmramp -diag 8 8 -maxval 7 | ppmtoppm > ${diag8_ppm}

echo "Test 11.  Should print 2097565808 394, then match seven times"
pamundice -quiet -across 2 ${diag8_ppm} | tee ${diag2h_ppm} | cksum

cat ${options_h} | \
while read opt
  do
  pamcat ${opt} ${diag8_ppm} ${diag8_ppm} | \
    cmp -s ${diag2h_ppm} - && echo "match" || echo "no match"     
  done
cat ${diag8_ppm} | pamcat -lr -white ${diag8_ppm} - | \
    cmp -s ${diag2h_ppm} - && echo "match" || echo "no match"     


echo "Test 12.  Should print 3086569577 394"
pamcat -tb ${diag8_ppm} ${diag8_ppm} | tee ${diag2v_ppm} | cksum


echo "Test 13.  Should print 4288335051 586, then match twice"
pamundice -quiet -across 3 ${diag8_ppm} | tee ${diag3h_ppm} | cksum

pamcat -lr ${diag8_ppm}  ${diag2h_ppm} | \
    cmp -s ${diag3h_ppm} - && echo "match" || echo "no match"     
cat ${diag2h_ppm} | pamcat -lr - ${diag8_ppm} | \
    cmp -s ${diag3h_ppm} - && echo "match" || echo "no match"     	


echo "Test 14.  Should print 642720605 586, then match eight times"
pamundice -quiet -down 3 ${diag8_ppm} | tee ${diag3v_ppm} | cksum 

cat ${options_v} | \
while read opt
  do
  pamcat ${opt} ${diag8_ppm} ${diag8_ppm} ${diag8_ppm} | \
    cmp -s ${diag3v_ppm} - && echo "match" || echo "no match"
  done
pamcat -tb ${diag2v_ppm} ${diag8_ppm} | \
    cmp -s ${diag3v_ppm} - && echo "match" || echo "no match"
cat ${diag8_ppm} | pamcat -tb ${diag2v_ppm} - | \
    cmp -s ${diag3v_ppm} - && echo "match" || echo "no match"

rm ${diag2h_ppm}
rm ${diag2v_ppm}
rm ${diag3h_ppm}
rm ${diag3v_ppm}
rm ${options_h}
rm ${options_v}

echo "Test 15.  Should print 3622741282 1019 twice"
pamcat -tb -white testgrid.pbm ${diag8_ppm} | cksum
pamcat -tb -jcenter -white testgrid.pbm ${diag8_ppm} | cksum

echo "Test 16.  Should print 1401081637 1019"
pamcat -tb -jleft -white testgrid.pbm ${diag8_ppm}  | cksum

echo "Test 17.  Should print 2756501917 1019"
pamcat -tb -jright -white testgrid.pbm ${diag8_ppm} | cksum

rm ${diag8_ppm}


echo "Test 18. Should print 587933655 107742 twice"
pamcat -lr -black testgrid.pbm testimg.ppm | cksum
pamcat -lr -jcenter -black testgrid.pbm testimg.ppm | cksum

echo "Test 19.  Should print 3948141157 107742"
pamcat -lr -jtop -black testgrid.pbm testimg.ppm | cksum

echo "Test 20.  Should print 3910239002 107742"
pamcat -lr -jbottom -black testgrid.pbm testimg.ppm | cksum
