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

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

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 3 1 > ${white_pbm}
#pbmmake -b 3 1 > ${black_pbm}
pgmmake 0.2 3 1 -maxval 127 > ${test127_pgm}
pgmmake 0.2 3 1 -maxval 255 > ${test255_pgm}
ppmmake yellow 3 1 -maxval 127 > ${test127_ppm}
ppmmake yellow 3 1 -maxval 511 > ${test511_ppm}

# define function
ckfmt() { pamfile || echo "failure"; }

size="-width 8"

echo "Test 1. PBM infile"

infile=${white_pbm}

echo "Should print: PBM raw, 8 by 1 fourteen times"

pnmpad ${size} ${infile} | ckfmt
pnmpad ${size} -black ${infile} | ckfmt
pnmpad ${size} -white ${infile} | ckfmt
pnmpad ${size} -extend-edge ${infile} | ckfmt
pnmpad ${size} -detect-background ${infile} | ckfmt

pnmpad ${size} -color black ${infile} | ckfmt
pnmpad ${size} -color black -promote none   ${infile} | ckfmt
pnmpad ${size} -color black -promote format ${infile} | ckfmt

pnmpad ${size} -color white ${infile} | ckfmt
pnmpad ${size} -color white -promote none   ${infile} | ckfmt
pnmpad ${size} -color white -promote format ${infile} | ckfmt

pnmpad ${size} -color ${LIGHTGRAY} -promote none ${infile} | ckfmt
pnmpad ${size} -color ${DARKGRAY} -promote none ${infile} | ckfmt
pnmpad ${size} -color purple   -promote none ${infile} | ckfmt

echo "Should print: PGM raw, 8 by 1  maxval 1 twice"

pnmpad ${size} -color ${LIGHTGRAY} -promote format ${infile} | ckfmt
pnmpad ${size} -color ${DARKGRAY} -promote format ${infile} | ckfmt

echo "Should print: PGM raw, 8 by 1  maxval 255 twice"

pnmpad ${size} -color ${LIGHTGRAY} ${infile} | ckfmt
pnmpad ${size} -color ${DARKGRAY} ${infile} | ckfmt

echo "Should print: PPM raw, 8 by 1  maxval 1 once"

pnmpad ${size} -color purple -promote format ${infile} | ckfmt

echo "Should print: PPM raw, 8 by 1  maxval 255 twice"

pnmpad ${size} -color purple ${infile} | ckfmt
pnmpad ${size} -color purple -promote all ${infile} | ckfmt

rm ${infile}

echo
echo "Test 2. PGM maxval 127 infile"

infile=${test127_pgm}

echo "Should print: PGM raw, 8 by 1  maxval 127 sixteen times"

pnmpad ${size} ${infile} | ckfmt
pnmpad ${size} -black ${infile} | ckfmt
pnmpad ${size} -white ${infile} | ckfmt
pnmpad ${size} -extend-edge ${infile} | ckfmt
pnmpad ${size} -detect-background ${infile} | ckfmt

pnmpad ${size} -color black ${infile} | ckfmt
pnmpad ${size} -color black -promote none   ${infile} | ckfmt
pnmpad ${size} -color black -promote format ${infile} | ckfmt

pnmpad ${size} -color white ${infile} | ckfmt
pnmpad ${size} -color white -promote none   ${infile} | ckfmt
pnmpad ${size} -color white -promote format ${infile} | ckfmt

pnmpad ${size} -color ${LIGHTGRAY} -promote none ${infile} | ckfmt
pnmpad ${size} -color ${LIGHTGRAY} -promote format ${infile} | ckfmt

pnmpad ${size} -color ${DARKGRAY} -promote none ${infile} | ckfmt
pnmpad ${size} -color ${DARKGRAY} -promote format ${infile} | ckfmt

pnmpad ${size} -color purple   -promote none ${infile} | ckfmt

echo "Should print: PGM raw, 8 by 1  maxval 255 four times"

pnmpad ${size} -color ${LIGHTGRAY}              ${infile} | ckfmt
pnmpad ${size} -color ${LIGHTGRAY} -promote all ${infile} | ckfmt
pnmpad ${size} -color ${DARKGRAY}               ${infile} | ckfmt
pnmpad ${size} -color ${DARKGRAY}  -promote all ${infile} | ckfmt

echo "Should print: PPM raw, 8 by 1  maxval 127 once"

pnmpad ${size} -color purple -promote format ${infile} | ckfmt

echo "Should print: PPM raw, 8 by 1  maxval 255 twice"

pnmpad ${size} -color purple ${infile} | ckfmt
pnmpad ${size} -color purple -promote all ${infile} | ckfmt

rm ${infile}

echo
echo "Test 3. PGM maxval 255 infile"

infile=${test255_pgm}

echo "Should print: PGM raw, 8 by 1  maxval 255 eighteen times"

pnmpad ${size} ${infile} | ckfmt
pnmpad ${size} -black ${infile} | ckfmt
pnmpad ${size} -white ${infile} | ckfmt
pnmpad ${size} -extend-edge ${infile} | ckfmt
pnmpad ${size} -detect-background ${infile} | ckfmt

pnmpad ${size} -color black ${infile} | ckfmt
pnmpad ${size} -color black -promote none   ${infile} | ckfmt
pnmpad ${size} -color black -promote format ${infile} | ckfmt
pnmpad ${size} -color black -promote all    ${infile} | ckfmt

pnmpad ${size} -color ${LIGHTGRAY} ${infile} | ckfmt
pnmpad ${size} -color ${LIGHTGRAY} -promote none   ${infile} | ckfmt
pnmpad ${size} -color ${LIGHTGRAY} -promote format ${infile} | ckfmt
pnmpad ${size} -color ${LIGHTGRAY} -promote all    ${infile} | ckfmt


pnmpad ${size} -color ${DARKGRAY} ${infile} | ckfmt
pnmpad ${size} -color ${DARKGRAY} -promote none   ${infile} | ckfmt
pnmpad ${size} -color ${DARKGRAY} -promote format ${infile} | ckfmt
pnmpad ${size} -color ${DARKGRAY} -promote all    ${infile} | ckfmt

pnmpad ${size} -color purple   -promote none   ${infile} | ckfmt

echo "Should print: PPM raw, 8 by 1  maxval 255 three times"

pnmpad ${size} -color purple ${infile} | ckfmt

pnmpad ${size} -color purple -promote format ${infile} | ckfmt
pnmpad ${size} -color purple -promote all    ${infile} | ckfmt

rm ${infile}

echo
echo "Test 4. PPM maxval 127 infile"

infile=${test127_ppm}

echo "Should print: PPM raw, 8 by 1  maxval 127 seventeen times"

pnmpad ${size} ${infile} | ckfmt
pnmpad ${size} -black ${infile} | ckfmt
pnmpad ${size} -white ${infile} | ckfmt
pnmpad ${size} -extend-edge ${infile} | ckfmt
pnmpad ${size} -detect-background ${infile} | ckfmt

pnmpad ${size} -color black ${infile} | ckfmt
pnmpad ${size} -color black -promote none   ${infile} | ckfmt
pnmpad ${size} -color black -promote format ${infile} | ckfmt
pnmpad ${size} -color white ${infile} | ckfmt
pnmpad ${size} -color white -promote none   ${infile} | ckfmt
pnmpad ${size} -color white -promote format ${infile} | ckfmt

pnmpad ${size} -color ${LIGHTGRAY} -promote none   ${infile} | ckfmt
pnmpad ${size} -color ${LIGHTGRAY} -promote format ${infile} | ckfmt
pnmpad ${size} -color ${DARKGRAY} -promote none   ${infile} | ckfmt
pnmpad ${size} -color ${DARKGRAY} -promote format ${infile} | ckfmt

pnmpad ${size} -color purple -promote none   ${infile} | ckfmt
pnmpad ${size} -color purple -promote format ${infile} | ckfmt

echo "Should print: PPM raw, 8 by 1  maxval 255 five times"

pnmpad ${size} -color ${LIGHTGRAY} ${infile} | ckfmt
pnmpad ${size} -color ${DARKGRAY}  ${infile} | ckfmt
pnmpad ${size} -color purple    ${infile} | ckfmt
pnmpad ${size} -color ${LIGHTGRAY} -promote all ${infile} | ckfmt
pnmpad ${size} -color purple    -promote all ${infile} | ckfmt

rm ${infile}

echo
echo "Test 5. PPM maxval 511 infile"

infile=${test511_ppm}

echo "Should print: PPM raw, 8 by 1  maxval 511 twenty-five times"

pnmpad ${size} ${infile} | ckfmt
pnmpad ${size} -black ${infile} | ckfmt
pnmpad ${size} -white ${infile} | ckfmt
pnmpad ${size} -extend-edge ${infile} | ckfmt
pnmpad ${size} -detect-background ${infile} | ckfmt

pnmpad ${size} -color black ${infile} | ckfmt
pnmpad ${size} -color black -promote none   ${infile} | ckfmt
pnmpad ${size} -color black -promote format ${infile} | ckfmt
pnmpad ${size} -color black -promote all    ${infile} | ckfmt

pnmpad ${size} -color white ${infile} | ckfmt
pnmpad ${size} -color white -promote none   ${infile} | ckfmt
pnmpad ${size} -color white -promote format ${infile} | ckfmt
pnmpad ${size} -color white -promote all    ${infile} | ckfmt

pnmpad ${size} -color ${LIGHTGRAY} ${infile} | ckfmt
pnmpad ${size} -color ${LIGHTGRAY} -promote none   ${infile} | ckfmt
pnmpad ${size} -color ${LIGHTGRAY} -promote format ${infile} | ckfmt
pnmpad ${size} -color ${LIGHTGRAY} -promote all    ${infile} | ckfmt

pnmpad ${size} -color ${DARKGRAY} ${infile} | ckfmt
pnmpad ${size} -color ${DARKGRAY} -promote none   ${infile} | ckfmt
pnmpad ${size} -color ${DARKGRAY} -promote format ${infile} | ckfmt
pnmpad ${size} -color ${DARKGRAY} -promote all    ${infile} | ckfmt

pnmpad ${size} -color purple ${infile} | ckfmt
pnmpad ${size} -color purple -promote none   ${infile} | ckfmt
pnmpad ${size} -color purple -promote format ${infile} | ckfmt
pnmpad ${size} -color purple -promote all    ${infile} | ckfmt

rm ${infile}
