#! /bin/bash
# This script tests: giftopnm pamtogif
# Also requires: ppmtorgb3 rgb3toppm pbmmake pnminvert pamcut pamdepth

tmpdir=${tmpdir:-/tmp}

# Test 1. Break up input image into three monochrome planes,
# maxval 255.  Transform each plane to gif and back to pgm.
# Reassemble the planes.  Result should be identical to input.

echo "Test 1. Should print 1926073387 101484"

test_ppm=${tmpdir}/testimg.ppm

cp testimg.ppm ${tmpdir} &&
ppmtorgb3 ${test_ppm}

test_red=${tmpdir}/testimg.red
test_grn=${tmpdir}/testimg.grn
test_blu=${tmpdir}/testimg.blu
out_red=${tmpdir}/out.red
out_grn=${tmpdir}/out.grn
#out_blu=${tmpdir}/out.blu

pamtogif ${test_red} | giftopnm > ${out_red} &&
pamtogif ${test_grn} | giftopnm > ${out_grn} &&
pamtogif ${test_blu} | giftopnm | \
  rgb3toppm ${out_red} ${out_grn} - | \
  cksum

rm ${test_ppm} ${test_grn} ${test_blu} ${out_red} ${out_grn}

echo "Test 2. Should produce 1571496937 33838 six times"

test_gif=${tmpdir}/testimg.gif

cat ${test_red} | cksum
pamtogif ${test_red} | giftopnm | cksum
pamtogif -interlace ${test_red} | giftopnm | cksum
pamtogif -noclear ${test_red} | giftopnm | cksum
pamtogif -sort ${test_red} | tee ${test_gif} | \
  giftopnm | cksum
echo "junk" >> ${test_gif} && \
  giftopnm -image=1 -quitearly ${test_gif} | cksum

rm  ${test_gif} ${test_red}

echo "Test 3. Should produce 281226646 481 six times"
# maze.pbm is too small for -noclear to take effect 

cat maze.pbm | cksum
pamtogif maze.pbm | giftopnm | cksum
pamtogif -nolzw maze.pbm | giftopnm | cksum
pamtogif -transparent=black maze.pbm | giftopnm | cksum
pamtogif -alpha=maze.pbm maze.pbm | giftopnm | cksum
pamtogif -transparent=white maze.pbm | giftopnm -alpha=- | \
  pnminvert | cksum


# Test 4.
# In this gif file the code length changes after the last image data.
# Image data: 3 bits, end code 4 bits.

echo "Test 4. Should produce P1 4 1 0101"

pbmmake -g 4 1 | \
  pamtogif -verbose | giftopnm -plain | \
  tr '\n' ' '
echo ""


# Test 5.
# Test whether saturated code tables are handled correctly.
# Test input images which use up the string code table or
# come close to doing so.


echo "Test 5. Should produce: N : 0 0 0 0 : 0 , N : 0 0 0 0 : 0"
echo "(N=238, 239, 240, 241, 255, 256, 257, 4030, 4031, 4097)"

test_pgm=${tmpdir}/testimg.pgm

# The following awk scripts produce a PGM file with no repeated
# sequences.  Obviously this cannot be compressed at all; the codes
# are consumed without ever being used.  
# Here is a smaller example with the same property:
# P2
# 8 8
# 7
# 0 0 1 1 0 2 2 0
# 3 3 0 4 4 0 5 5
# 0 6 6 0 7 7 1 2
# 1 3 1 4 1 5 1 6
# 1 7 2 3 2 4 2 5
# 2 6 2 7 3 4 3 5
# 3 6 3 7 4 5 4 6
# 4 7 5 6 5 7 6 7

test257_pgm=${tmpdir}/testimg257.pgm

maxval=15
awk -v maxval=${maxval} 'BEGIN \
{ print "P2"
         print "1 "(maxval+1) * (maxval+1) +1
         print maxval
         print 0 
         for (j=i+1; j<=maxval; ++j)
	    {print 0; print j; print j }
         for (i=1; i<=maxval; ++i)
             for (j=i+1; j<=maxval; ++j)
                 {print i; print j }
         print 0
}' >  ${test257_pgm}

for size in 238 239 240 241 255 256 257
  do
  pamcut -height=${size} ${test257_pgm} > ${test_pgm} &&
  pamtogif -verbose ${test_pgm} | giftopnm | pamdepth ${maxval} | \
    cmp - ${test_pgm}
  echo -n ${size} ":" ${PIPESTATUS[@]} ":" $? ", "
  pamtogif -nolzw -verbose ${test_pgm} | giftopnm | pamdepth ${maxval} | \
    cmp - ${test_pgm}
  echo ${size} ":" ${PIPESTATUS[@]} ":" $?
  rm ${test_pgm}
  done 

rm ${test257_pgm}

test4097_pgm=${tmpdir}/testimg4097.pgm

maxval=63
awk -v maxval=${maxval} 'BEGIN \
{ print "P2"
         print "1 "(maxval+1) * (maxval+1) +1
         print maxval
         print 0 
         for (j=i+1; j<=maxval; ++j)
	    {print 0; print j; print j }
         for (i=1; i<=maxval; ++i)
             for (j=i+1; j<=maxval; ++j)
                 {print i; print j }
         print 0
}' > ${test4097_pgm}

for size in 4030 4031 4097
  do
  pamcut -height ${size} ${test4097_pgm} > ${test_pgm} &&
  pamtogif -verbose ${test_pgm} | giftopnm | pamdepth ${maxval} | \
    cmp - ${test_pgm}
  # pamdepth ${maxval} is necessary because
  # giftopnm output is maxval 255
  echo -n ${size} ":" ${PIPESTATUS[@]} ":" $? ", "
  pamtogif -nolzw ${test_pgm} | giftopnm | pamdepth ${maxval} | \
    cmp - ${test_pgm}
  echo ${size} ":" ${PIPESTATUS[@]} ":" $?
  rm ${test_pgm}
  done 

rm ${test4097_pgm}
