#! /bin/sh
# This script tests: pnmpad
# Also requires: pbmmake pgmmake ppmmake pamcut ppmtopgm

tmpdir=${tmpdir:-/tmp}

white_pbm="${tmpdir}"/white.pbm
black_pbm="${tmpdir}"/black.pbm
test127_pgm="${tmpdir}"/testimg1.pgm
test255_pgm="${tmpdir}"/testimg2.pgm
test511_pgm="${tmpdir}"/testimg3.pgm
test127_ppm="${tmpdir}"/testimg1.ppm
test255_ppm="${tmpdir}"/testimg2.ppm
test511_ppm="${tmpdir}"/testimg3.ppm
target_pnm="${tmpdir}"/target.pnm

RGBDEF=./rgbt.txt

LIGHTGRAY="rgbi:0.6/0.6/0.6"
LIGHTGRAYv=0.6
DARKGRAY="rgbi:0.01/0.01/0.01"
DARKGRAYv=0.01

pbmmake -w 1 1 > ${white_pbm}
pbmmake -b 1 1 > ${black_pbm}
pgmmake 0.25 1 1 -maxval 127 > ${test127_pgm}
pgmmake 0.25 1 1 -maxval 255 > ${test255_pgm}
pgmmake 0.25 1 1 -maxval 511 > ${test511_pgm}
ppmmake yellow 1 1 -maxval 127 > ${test127_ppm}
ppmmake yellow 1 1 -maxval 255 > ${test255_ppm}
ppmmake yellow 1 1 -maxval 511 > ${test511_ppm}

ptop="-top 1"

# define function
cmptarget () { pamcut -top 0 -left 0 -height 1 -width 1 $@ |\
                cmp -s ${target_pnm} - && echo "match" || echo "failure";  }

echo "Test 1. PBM white infile"

infile=${white_pbm}

echo "Should print match seven times"

pbmmake -w 1 1 > ${target_pnm}
pnmpad ${ptop} -white ${infile} | cmptarget
pnmpad ${ptop} -extend-edge ${infile} | cmptarget
pnmpad ${ptop} -detect-background ${infile} | cmptarget
pnmpad ${ptop} -color white ${infile} | cmptarget
pnmpad ${ptop} -color white -promote none   ${infile} | cmptarget
pnmpad ${ptop} -color white -promote format ${infile} | cmptarget
pnmpad ${ptop} -color ${LIGHTGRAY}  -promote none   ${infile} | cmptarget

echo "Should print match seven times"

pbmmake -b 1 1 > ${target_pnm}
pnmpad ${ptop} ${infile} | cmptarget
pnmpad ${ptop} -black ${infile} | cmptarget
pnmpad ${ptop} -color black ${infile} | cmptarget
pnmpad ${ptop} -color black -promote none   ${infile} | cmptarget
pnmpad ${ptop} -color black -promote format ${infile} | cmptarget
pnmpad ${ptop} -color ${DARKGRAY} -promote none ${infile} | cmptarget
pnmpad ${ptop} -color purple   -promote none ${infile} | cmptarget

echo "Should print match once"

pgmmake -maxval 1 ${LIGHTGRAYv} 1 1 > ${target_pnm}
pnmpad ${ptop} -color ${LIGHTGRAY} -promote format ${infile} | cmptarget

echo "Should print match once"

pgmmake -maxval 1 ${DARKGRAYv} 1 1 > ${target_pnm}
pnmpad ${ptop} -color ${DARKGRAY} -promote format ${infile} | cmptarget

echo "Should print match twice"

pgmmake -maxval 255 ${LIGHTGRAYv} 1 1 > ${target_pnm}
pnmpad ${ptop} -color ${LIGHTGRAY} ${infile} | cmptarget
pnmpad ${ptop} -color ${LIGHTGRAY} -promote all ${infile} | cmptarget

echo "Should print match twice"

pgmmake -maxval 255 ${DARKGRAYv} 1 1 > ${target_pnm}
pnmpad ${ptop} -color ${DARKGRAY} ${infile} | cmptarget
pnmpad ${ptop} -color ${DARKGRAY} -promote all ${infile} | cmptarget

echo "Should print match once"

ppmmake -maxval 1 purple 1 1 > ${target_pnm}
pnmpad ${ptop} -color purple -promote format ${infile} | cmptarget

echo "Should print match twice"

ppmmake -maxval 255 purple 1 1 > ${target_pnm}
pnmpad ${ptop} -color purple ${infile} | cmptarget
pnmpad ${ptop} -color purple -promote all ${infile} | cmptarget

echo
echo "Test 2. PBM black infile"

infile=${black_pbm}

echo "Should print match nine times"

pbmmake -b 1 1 > ${target_pnm}
pnmpad ${ptop} ${infile} | cmptarget
pnmpad ${ptop} -black ${infile} | cmptarget
pnmpad ${ptop} -color black ${infile} | cmptarget
pnmpad ${ptop} -color black -promote none   ${infile} | cmptarget
pnmpad ${ptop} -color black -promote format ${infile} | cmptarget
pnmpad ${ptop} -color ${DARKGRAY} -promote none ${infile} | cmptarget
pnmpad ${ptop} -color purple   -promote none ${infile} | cmptarget
pnmpad ${ptop} -extend-edge ${infile} | cmptarget
pnmpad ${ptop} -detect-background ${infile} | cmptarget

echo "Should print match five times"

pbmmake -w 1 1 > ${target_pnm}
pnmpad ${ptop} -white ${infile} | cmptarget
pnmpad ${ptop} -color white ${infile} | cmptarget
pnmpad ${ptop} -color white -promote none   ${infile} | cmptarget
pnmpad ${ptop} -color white -promote format ${infile} | cmptarget
pnmpad ${ptop} -color ${LIGHTGRAY} -promote none ${infile} | cmptarget

echo
echo "Test 3. PGM maxval 127 infile"

infile=${test127_pgm}

echo "Should print match five times"

pgmmake -maxval 127 0.0 1 1 > ${target_pnm}
pnmpad ${ptop} ${infile} | cmptarget
pnmpad ${ptop} -black ${infile} | cmptarget
pnmpad ${ptop} -color black ${infile} | cmptarget
pnmpad ${ptop} -color black -promote none   ${infile} | cmptarget
pnmpad ${ptop} -color black -promote format ${infile} | cmptarget

echo "Should print match four times"

pgmmake -maxval 127 1.0 1 1 > ${target_pnm}
pnmpad ${ptop} -white ${infile} | cmptarget
pnmpad ${ptop} -color white ${infile} | cmptarget
pnmpad ${ptop} -color white -promote none   ${infile} | cmptarget
pnmpad ${ptop} -color white -promote format ${infile} | cmptarget

echo "Should print match twice"

pamcut -t 0 -h 1 -w 1 ${infile} > ${target_pnm}
pnmpad ${ptop} -extend-edge ${infile} | cmptarget
pnmpad ${ptop} -detect-background ${infile} | cmptarget

echo "Should print match twice"

pgmmake -maxval 127 ${LIGHTGRAYv} 1 1 > ${target_pnm}
pnmpad ${ptop} -color ${LIGHTGRAY} -promote none ${infile} | cmptarget
pnmpad ${ptop} -color ${LIGHTGRAY} -promote format ${infile} | cmptarget

echo "Should print match twice"

pgmmake -maxval 127 ${DARKGRAYv} 1 1 > ${target_pnm}
pnmpad ${ptop} -color ${DARKGRAY} -promote none ${infile} | cmptarget
pnmpad ${ptop} -color ${DARKGRAY} -promote format ${infile} | cmptarget

echo "Should print match once"

ppmmake -maxval 127 purple 1 1 | ppmtopgm > ${target_pnm} 
pnmpad ${ptop} -color purple   -promote none ${infile} | cmptarget

echo "Should print match twice"

pgmmake -maxval 255 ${LIGHTGRAYv} 1 1 > ${target_pnm}
pnmpad ${ptop} -color ${LIGHTGRAY}              ${infile} | cmptarget
pnmpad ${ptop} -color ${LIGHTGRAY} -promote all ${infile} | cmptarget

echo "Should print match twice"

pgmmake -maxval 255 ${DARKGRAYv} 1 1 > ${target_pnm}
pnmpad ${ptop} -color ${DARKGRAY}              ${infile} | cmptarget
pnmpad ${ptop} -color ${DARKGRAY} -promote all ${infile} | cmptarget

echo "Should print match once"

ppmmake -maxval 127 purple 1 1 > ${target_pnm}
pnmpad ${ptop} -color purple -promote format ${infile} | cmptarget

echo "Should print match twice"

ppmmake -maxval 255 purple 1 1 > ${target_pnm}
pnmpad ${ptop} -color purple ${infile} | cmptarget
pnmpad ${ptop} -color purple -promote all    ${infile} | cmptarget


echo
echo "Test 4. PGM maxval 255 infile"

infile=${test255_pgm}

echo "Should print match five times"

pgmmake -maxval 255 0.0 1 1 > ${target_pnm}
pnmpad ${ptop} ${infile} | cmptarget
pnmpad ${ptop} -black ${infile} | cmptarget
pnmpad ${ptop} -color black ${infile} | cmptarget
pnmpad ${ptop} -color black -promote none   ${infile} | cmptarget
pnmpad ${ptop} -color black -promote format ${infile} | cmptarget

echo "Should print match four times"

pgmmake -maxval 255 1.0 1 1 > ${target_pnm}
pnmpad ${ptop} -white ${infile} | cmptarget
pnmpad ${ptop} -color white ${infile} | cmptarget
pnmpad ${ptop} -color white -promote none   ${infile} | cmptarget
pnmpad ${ptop} -color white -promote format ${infile} | cmptarget

echo "Should print match twice"

pamcut -t 0 -h 1 -w 1 ${infile} > ${target_pnm}
pnmpad ${ptop} -extend-edge ${infile} | cmptarget
pnmpad ${ptop} -detect-background ${infile} | cmptarget

echo "Should print match twice"

pgmmake -maxval 255 ${LIGHTGRAYv} 1 1 > ${target_pnm}
pnmpad ${ptop} -color ${LIGHTGRAY} -promote none ${infile} | cmptarget
pnmpad ${ptop} -color ${LIGHTGRAY} -promote format ${infile} | cmptarget

echo "Should print match twice"

pgmmake -maxval 255 ${DARKGRAYv} 1 1 > ${target_pnm}
pnmpad ${ptop} -color ${DARKGRAY} -promote none ${infile} | cmptarget
pnmpad ${ptop} -color ${DARKGRAY} -promote format ${infile} | cmptarget

echo "Should print match once"

ppmmake -maxval 255  purple 1 1 | ppmtopgm > ${target_pnm}
pnmpad ${ptop} -color purple   -promote none ${infile} | cmptarget

echo "Should print match four times"

pgmmake -maxval 255 ${LIGHTGRAYv} 1 1 > ${target_pnm}
pnmpad ${ptop} -color ${LIGHTGRAY}                 ${infile} | cmptarget
pnmpad ${ptop} -color ${LIGHTGRAY} -promote none   ${infile} | cmptarget
pnmpad ${ptop} -color ${LIGHTGRAY} -promote format ${infile} | cmptarget
pnmpad ${ptop} -color ${LIGHTGRAY} -promote all    ${infile} | cmptarget

echo "Should print match four times"

pgmmake -maxval 255 ${DARKGRAYv} 1 1 > ${target_pnm}
pnmpad ${ptop} -color ${DARKGRAY}                 ${infile} | cmptarget
pnmpad ${ptop} -color ${DARKGRAY} -promote none   ${infile} | cmptarget
pnmpad ${ptop} -color ${DARKGRAY} -promote format ${infile} | cmptarget
pnmpad ${ptop} -color ${DARKGRAY} -promote all    ${infile} | cmptarget

echo "Should print match three times"

ppmmake -maxval 255 purple 1 1 > ${target_pnm}
pnmpad ${ptop} -color purple ${infile} | cmptarget
pnmpad ${ptop} -color purple -promote format ${infile} | cmptarget
pnmpad ${ptop} -color purple -promote all    ${infile} | cmptarget

echo
echo "Test 5. PGM maxval 511 infile"

infile=${test511_pgm}

echo "Should print match five times"

pgmmake -maxval 511 0.0 1 1 > ${target_pnm}
pnmpad ${ptop} ${infile} | cmptarget
pnmpad ${ptop} -black ${infile} | cmptarget
pnmpad ${ptop} -color black ${infile} | cmptarget
pnmpad ${ptop} -color black -promote none   ${infile} | cmptarget
pnmpad ${ptop} -color black -promote format ${infile} | cmptarget

echo "Should print match four times"

pgmmake -maxval 511 1.0 1 1 > ${target_pnm}
pnmpad ${ptop} -white ${infile} | cmptarget
pnmpad ${ptop} -color white ${infile} | cmptarget
pnmpad ${ptop} -color white -promote none   ${infile} | cmptarget
pnmpad ${ptop} -color white -promote format ${infile} | cmptarget

echo "Should print match twice"

pamcut -t 0 -h 1 -w 1 ${infile} > ${target_pnm}
pnmpad ${ptop} -extend-edge ${infile} | cmptarget
pnmpad ${ptop} -detect-background ${infile} | cmptarget

echo "Should print match twice"

pgmmake -maxval 511 ${LIGHTGRAYv} 1 1 > ${target_pnm}
pnmpad ${ptop} -color ${LIGHTGRAY} -promote none ${infile} | cmptarget
pnmpad ${ptop} -color ${LIGHTGRAY} -promote format ${infile} | cmptarget

echo "Should print match twice"

pgmmake -maxval 511 ${DARKGRAYv} 1 1 > ${target_pnm}
pnmpad ${ptop} -color ${DARKGRAY} -promote none ${infile} | cmptarget
pnmpad ${ptop} -color ${DARKGRAY} -promote format ${infile} | cmptarget

echo "Should print match once"

ppmmake -maxval 511 purple 1 1 | ppmtopgm > ${target_pnm}
pnmpad ${ptop} -color purple -promote none ${infile} | cmptarget

echo "Should print match twice"

pgmmake -maxval 511 ${LIGHTGRAYv} 1 1 > ${target_pnm}
pnmpad ${ptop} -color ${LIGHTGRAY}              ${infile} | cmptarget
pnmpad ${ptop} -color ${LIGHTGRAY} -promote all ${infile} | cmptarget

echo "Should print match twice"

pgmmake -maxval 511 ${DARKGRAYv} 1 1 > ${target_pnm}
pnmpad ${ptop} -color ${DARKGRAY}              ${infile} | cmptarget
pnmpad ${ptop} -color ${DARKGRAY} -promote all ${infile} | cmptarget

echo "Should print match three times"

ppmmake -maxval 511 purple 1 1 > ${target_pnm}
pnmpad ${ptop} -color purple ${infile} | cmptarget
pnmpad ${ptop} -color purple -promote format ${infile} | cmptarget
pnmpad ${ptop} -color purple -promote all    ${infile} | cmptarget


echo
echo "Test 6. PPM maxval 127 infile"

infile=${test127_ppm}

echo "Should print match five times"

ppmmake -maxval 127 black 1 1 > ${target_pnm}
pnmpad ${ptop} ${infile} | cmptarget
pnmpad ${ptop} -black ${infile} | cmptarget
pnmpad ${ptop} -color black ${infile} | cmptarget
pnmpad ${ptop} -color black -promote none   ${infile} | cmptarget
pnmpad ${ptop} -color black -promote format ${infile} | cmptarget

echo "Should print match four times"

ppmmake -maxval 127 white 1 1 > ${target_pnm}
pnmpad ${ptop} -white ${infile} | cmptarget
pnmpad ${ptop} -color white ${infile} | cmptarget
pnmpad ${ptop} -color white -promote none   ${infile} | cmptarget
pnmpad ${ptop} -color white -promote format ${infile} | cmptarget

echo "Should print match twice"

ppmmake -maxval 127 purple 1 1 > ${target_pnm}
ppmmake -maxval 127 purple 1 2 | pnmpad ${ptop} -extend-edge | cmptarget
ppmmake -maxval 127 purple 1 2 | pnmpad ${ptop} -detect-background | cmptarget

echo "Should print match twice"

ppmmake -maxval 127 ${LIGHTGRAY} 1 1 > ${target_pnm}
pnmpad ${ptop} -color ${LIGHTGRAY} -promote none ${infile} | cmptarget
pnmpad ${ptop} -color ${LIGHTGRAY} -promote format ${infile} | cmptarget

echo "Should print match twice"

ppmmake -maxval 127 ${DARKGRAY} 1 1 > ${target_pnm}
pnmpad ${ptop} -color ${DARKGRAY} -promote none ${infile} | cmptarget
pnmpad ${ptop} -color ${DARKGRAY} -promote format ${infile} | cmptarget

echo "Should print match once"

ppmmake -maxval 127 purple 1 1 > ${target_pnm} 
pnmpad ${ptop} -color purple -promote none ${infile} | cmptarget

echo "Should print match twice"

ppmmake -maxval 255 ${LIGHTGRAY} 1 1 > ${target_pnm}
pnmpad ${ptop} -color ${LIGHTGRAY}                  ${infile} | cmptarget
pnmpad ${ptop} -color ${LIGHTGRAY}     -promote all ${infile} | cmptarget

echo "Should print match twice"

ppmmake -maxval 255 ${DARKGRAY} 1 1 > ${target_pnm}
pnmpad ${ptop} -color ${DARKGRAY}              ${infile} | cmptarget
pnmpad ${ptop} -color ${DARKGRAY} -promote all ${infile} | cmptarget

echo "Should print match twice"

ppmmake -maxval 255 purple 1 1 > ${target_pnm}
pnmpad ${ptop} -color purple ${infile} | cmptarget
pnmpad ${ptop} -color purple -promote all ${infile} | cmptarget

echo
echo "Test 7. PPM maxval 511 infile"

infile=${test511_ppm}

echo "Should print match five times"

ppmmake -maxval 511 black 1 1 > ${target_pnm}
pnmpad ${ptop} ${infile} | cmptarget
pnmpad ${ptop} -black ${infile} | cmptarget
pnmpad ${ptop} -color black ${infile} | cmptarget
pnmpad ${ptop} -color black -promote none   ${infile} | cmptarget
pnmpad ${ptop} -color black -promote format ${infile} | cmptarget

echo "Should print match four times"

ppmmake -maxval 511 white 1 1 > ${target_pnm}
pnmpad ${ptop} -white ${infile} | cmptarget
pnmpad ${ptop} -color white ${infile} | cmptarget
pnmpad ${ptop} -color white -promote none   ${infile} | cmptarget
pnmpad ${ptop} -color white -promote format ${infile} | cmptarget

echo "Should print match six times"

ppmmake -maxval 511 purple 1 1 > ${target_pnm}
ppmmake -maxval 511 purple 1 5 | pnmpad ${ptop} -extend-edge | cmptarget
ppmmake -maxval 511 purple 1 5 | pnmpad ${ptop} -detect-background | cmptarget
pnmpad ${ptop} -color purple ${infile} | cmptarget
pnmpad ${ptop} -color purple -promote none ${infile} | cmptarget
pnmpad ${ptop} -color purple -promote format ${infile} | cmptarget
pnmpad ${ptop} -color purple -promote all    ${infile} | cmptarget

echo "Should print match twice"

ppmmake -maxval 511 ${LIGHTGRAY} 1 1 > ${target_pnm}
pnmpad ${ptop} -color ${LIGHTGRAY} -promote none ${infile} | cmptarget
pnmpad ${ptop} -color ${LIGHTGRAY} -promote format ${infile} | cmptarget

echo "Should print match twice"

ppmmake -maxval 511 ${DARKGRAY} 1 1 > ${target_pnm}
pnmpad ${ptop} -color ${DARKGRAY} -promote none ${infile} | cmptarget
pnmpad ${ptop} -color ${DARKGRAY} -promote format ${infile} | cmptarget

echo "Should print match twice"

ppmmake -maxval 511 ${LIGHTGRAY} 1 1 > ${target_pnm}
pnmpad ${ptop} -color ${LIGHTGRAY}                  ${infile} | cmptarget
pnmpad ${ptop} -color ${LIGHTGRAY}     -promote all ${infile} | cmptarget

echo "Should print match twice"

ppmmake -maxval 511 ${DARKGRAY} 1 1 > ${target_pnm}
pnmpad ${ptop} -color ${DARKGRAY}              ${infile} | cmptarget
pnmpad ${ptop} -color ${DARKGRAY} -promote all ${infile} | cmptarget

rm ${white_pbm} ${black_pbm} ${test127_pgm} ${test255_pgm} \
   ${test511_pgm} ${test127_ppm} ${test255_ppm} ${test511_ppm} ${target_pnm}
