#! /bin/bash
# Last edited on 2018-06-28 18:26:00 by stolfilocal

PROG_NAME=${0##*/}
PROG_DESC="creates a PBM grayscale chart for photometric calibration"
PROG_HELP=(
  ''"${PROG_NAME}"' \\'
  '\n    [ -size {NH} {NV} ] \\'
  '\n    [ -help ] [ -info ]'
)
PROG_INFO=(
  'NAME'
  '\n  '"${PROG_NAME}"' - '"${PROG_DESC}"'.'
  '\n'
  '\nSYNOPSIS' \
  '\n  '"${PROG_HELP[*]}"''
  '\n'
  '\nDESCRIPTION'
  '\n    Outputs a PBM image that can be laser-printed'
  '\n  to generate a photometric alibration chart.  The'
  '\n  image is an array of /chips/, all of the same'
  '\n  size.  The chips in each row are filled with'
  '\n  black-and-white patterns of dots and/or lines'
  '\n  having a fixed density of black pixels.  The'
  '\n  density increases from top to bottom.'
  '\n'
  '\nOPTIONS'
  '\n  -size {NH} {NV}'
  '\n    Specifies the size (in pixels) of each chip.   The'
  '\n  default is "-size 1024 1024".'
  '\n'
  '\nSEE ALSO'
  '\n  Your doctor, regularly.'
  '\nAUTHOR' \
  '\n  Created 2009-02-11 by Jorge Stolfi, Unicamp'
)

# ----------------------------------------------------------------------
# COMMAND LINE PARSING

# Parse command line switches (keyword parameters): 
size="1024x1024"
while [[ ( $# -ge 1 ) && ( "/$1" =~ /-.* ) ]]; do
  if [[ ( $# -ge 3 ) && ( "/$1" == "/-size" ) ]]; then 
    shift; size="$1x$2"; shift; shift
  elif [[ ( $# -ge 1 ) && ( "/$1" == "/-help" ) ]]; then 
    shift; printf "usage:\n  ${PROG_HELP[*]}\n" 1>&2; exit 0
  elif [[ ( $# -ge 1 ) && ( "/$1" == "/-info" ) ]]; then 
    shift; printf "${PROG_INFO[*]}" 1>&2; exit 0
  else
    echo "unknown option $1" 1>&2
    printf "usage:\n  ${PROG_HELP[*]}" 1>&2 ; exit 1 
  fi
done 

# Parse remaining command line arguments (positional parameters):
if [[ $# -lt 0 ]]; then
  printf "usage:\n  ${PROG_HELP[*]}\n" 1>&2; exit 1
fi

if [[ $# -ne 0 ]]; then
  printf "usage:\n  ${PROG_HELP[*]}\n" 1>&2; exit 1
fi

# Temp file prefix:

tmp="/tmp/$$"

# Number of chips:
nchips=6

# Left patterns -- vertical lines
cell_A=( '10x10' '10x10' '10x10' '10x10' '10x10' '10x10' )
spot_A=( '00x10' '02x10' '04x10' '06x10' '08x10' '10x10' )
dark_A=(       0       0       0       0       0       0 )

# Center patterns -- squarish dots
cell_B=( '10x10' '10x10' '10x10' '10x10' '10x10' '10x10' )
spot_B=( '00x05' '04x05' '08x05' '05x08' '05x04' '05x00' )
dark_B=(       0       0       0       1       1       1 )

# Right patterns -- horizontal lines
cell_C=( '10x10' '10x10' '10x10' '10x10' '10x10' '10x10' )
spot_C=( '10x00' '10x02' '10x04' '10x06' '10x08' '10x10' )
dark_C=(       0       0       0       0       0       0 )

ic=0
chip_names=( )
while [[ ${ic} -lt ${nchips} ]]; do
  icx=`printf "%02d" "${ic}"`
  printf "making row %s ...\n" "${icx}" 1>&2
  make-pbm-gray-chip \
      -v size="${size}" \
      -v cell="${cell_A[${ic}]}" \
      -v spot="${spot_A[${ic}]}" \
      -v dark="${dark_A[${ic}]}" \
    > ${tmp}-${icx}-A.pbm
  make-pbm-gray-chip \
      -v size="${size}" \
      -v cell="${cell_B[${ic}]}" \
      -v spot="${spot_B[${ic}]}" \
      -v dark="${dark_B[${ic}]}" \
    > ${tmp}-${icx}-B.pbm
  make-pbm-gray-chip \
      -v size="${size}" \
      -v cell="${cell_C[${ic}]}" \
      -v spot="${spot_C[${ic}]}" \
      -v dark="${dark_C[${ic}]}" \
    > ${tmp}-${icx}-C.pbm
    
  pnmcat -lr ${tmp}-${icx}-{A,B,C}.pbm \
    > ${tmp}-${icx}.pbm
    
  chip_names=( ${chip_names[@]} ${tmp}-${icx}.pbm )
  
  ic=$(( ${ic} + 1 ))
done

pnmcat -tb ${chip_names[@]} 

rm -f ${tmp}-*.pbm
