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

echo "Test 1:PPM-rose  Should print 1926073387 101484 five times"
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

echo "Test 2:PPM-6  Should print 1646869394 29498 four times"
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

echo "Test 3:PPM-2  Should print 3743909631 14836 four times"
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

echo "Test 4:PPM-gray  Should print 603974118 4954 six times"
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

echo "Test 5:PPM-bilevel-black-white  Should print 1001018459 4954 four times"
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


echo "Test 6:PBM  Should print 281226646 481 three times"
pamtotiff maze.pbm | tifftopnm | cksum
pamtotiff -g3 -fill maze.pbm | tifftopnm | cksum
pamtotiff -g4 -mb maze.pbm -append -output ${multiimage_tiff} && \
  tifftopnm ${multiimage_tiff} | pampick 5 | cksum

# Extract individual images from multi-image tiff file

echo "Test 7: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}