#! /bin/bash
# This script tests: pamtotiff tifftopnm
# Also requires: ppmpat pampick ppmtopgm

# Failure message
## If this test fails, the cause may be a problem in the TIFF library.

tmpdir=${tmpdir:-/tmp}
tartan_ppm=${tmpdir}/tartan.ppm
gingham_ppm=${tmpdir}/gingham.ppm
argylecolor_ppm=${tmpdir}/argyle1.ppm
argylegray_ppm=${tmpdir}/argyle2.ppm
multiimage_tiff=${tmpdir}/multiimage.ppm

# PPM image
# Should print 1926073387 101484 five times

echo PPM-rose
pamtotiff testimg.ppm | tifftopnm -headerdump | cksum
pamtotiff -truecolor testimg.ppm -output ${multiimage_tiff} && \
  tifftopnm ${multiimage_tiff} -byrow | cksum
pamtotiff -truecolor -packbits testimg.ppm | tifftopnm | cksum
pamtotiff -truecolor -rowsperstrip=2 testimg.ppm | tifftopnm | cksum
pamtotiff -truecolor -lsb2msb  testimg.ppm | \
  tifftopnm -respectfillorder | cksum

# PPM image with six colors
# Should print 1646869394 29498 four times

echo PPM-6
ppmpat -tartan -color=rgb:0/8/8,rgb:8/f/8,rgb:f/f/0 108 91 | \
  tee ${tartan_ppm} | cksum
pamtotiff -truecolor ${tartan_ppm} | tifftopnm | cksum
pamtotiff ${tartan_ppm} | tifftopnm -respectfillorder | cksum
pamtotiff ${tartan_ppm} -append -output ${multiimage_tiff} && \
  tifftopnm ${multiimage_tiff} | pampick 1 | cksum


# PPM image with two colors
# Should print 3743909631 14836 four times

echo PPM-2
ppmpat -argyle2 -color=rgb:0/0/0,rgb:f/8/f,rgb:0/0/0 61 81 | \
  tee ${argylecolor_ppm} | cksum
pamtotiff -indexbits=2 ${argylecolor_ppm} -append -output ${multiimage_tiff} && \
  tifftopnm ${multiimage_tiff} | pampick 2 | cksum
pamtotiff -indexbits=1 ${argylecolor_ppm} | tifftopnm | cksum
pamtotiff -indexbits=1 -g4 ${argylecolor_ppm} | tifftopnm | cksum

# PPM image with six shades of gray
# pamtotiff detects the absence of colors and produces a grayscale
# tiff image unless -color is specified
# Pipeline output is PGM unless -color is specified
# Should print 603974118 4954 six times

echo PPM-gray
ppmpat -gingham3 -color=rgb:0/0/0,rgb:ff/ff/ff,rgb:0/0/0 61 81 | \
  tee ${gingham_ppm} | ppmtopgm | cksum
pamtotiff -color ${gingham_ppm} | tifftopnm | cksum
pamtotiff -indexbits=1,2 -mb -fill ${gingham_ppm} | tifftopnm | cksum
pamtotiff -indexbits=1,2 -mw ${gingham_ppm} | tifftopnm | cksum
pamtotiff -indexbits=1,2,4,8 -mb ${gingham_ppm} | tifftopnm | cksum
pamtotiff -mb ${gingham_ppm} -append -output ${multiimage_tiff} && \
  tifftopnm ${multiimage_tiff} | pampick 3 | cksum

# PPM image with two shades of gray (bilevel)
# pamtotiff detects the absence of colors and produces a grayscale
# tiff image unless -color is specified
# Pipeline output is PGM unless -color is specified
# Should print 1001018459 4954 four times

echo PPM-bilevel-black-white
ppmpat -argyle2 -color=rgb:0/0/0,rgb:ff/ff/ff,rgb:0/0/0 61 81 | \
  tee ${argylegray_ppm} | ppmtopgm | cksum
pamtotiff -color -fill ${argylegray_ppm} | tifftopnm | cksum
pamtotiff -indexbits=1 -mw ${argylegray_ppm} | tifftopnm | cksum
pamtotiff -mb ${argylegray_ppm} -append -output ${multiimage_tiff} && \
  tifftopnm ${multiimage_tiff} | pampick 4 | cksum

# PBM image
# Should print 2425386270 41 three times

echo PBM
pamtotiff testgrid.pbm | tifftopnm | cksum
pamtotiff -g3 -fill testgrid.pbm | tifftopnm | cksum
pamtotiff -g4 -mb testgrid.pbm -append -output ${multiimage_tiff} && \
  tifftopnm ${multiimage_tiff} | pampick 5 | cksum

# Extract individual images from multi-image tiff file

echo "Extract from multi-image TIFF"
tifftopnm ${multiimage_tiff} | pampick 0 | cksum
tifftopnm ${multiimage_tiff} | pampick 1 | cksum
tifftopnm ${multiimage_tiff} | pampick 2 | cksum
tifftopnm ${multiimage_tiff} | pampick 3 | cksum
tifftopnm ${multiimage_tiff} | pampick 4 | cksum

rm ${tartan_ppm} ${gingham_ppm} ${argylecolor_ppm} \
   ${argylegray_ppm} ${multiimage_tiff}