#! /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

BLACK="rgbi:0.0/0.0/0.0"
WHITE="rgbi:1.0/1.0/1.0"
LIGHTGRAY="rgbi:0.6/0.6/0.6"
LIGHTGRAYv=0.6
DARKGRAY="rgbi:0.01/0.01/0.01"
DARKGRAYv=0.01
PURPLE="rgb:A0/20/F0"
YELLOW="rgb:F0/F0/00"

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_ppm} - && 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
