#! /bin/csh -f 
# Last edited on 2001-10-29 00:26:18 by stolfi

set usage = "run-PZSplitExt BATCH..."

set cmd = $0
echo "=== begin ${cmd:t} ===" 
echo "${cmd:t} $*"

# Runs PZSplitExt on a set of scanned fragment images

set path = ( \
  ${STOLFIHOME}/programs/pm3-1.1.15/THESES/hcgl/1/bin/${PLATFORM} \
  ${cmd:h} \
  ${path} \
)

if ( $#argv < 1 ) then
  echo "no batches?"
  echo "usage: ${usage}"; exit 1
endif

set batches = ( $* )

foreach batch ( ${batches} )
  set bdir = "data/batches/${batch}"
  source ${bdir}/${batch}/set-gray-levels.csh
  @ d = ${hi} - ${lo}
  @ av = ${lo} - ( ${d} / 7 )
  if ( ${av} < 0 ) set av = 0
  @ bv = ${hi} + ( ${d} / 7 )
  set rawfile = "${bdir}/image-raw.jpg"
  set normfile = "${bdir}/image-norm.pgm"
  if ( ! ( -r ${rawfile} ) ) then
    echp "file ${rawfile} missing or unreadable"; exit 1
  endif
  set oprefix = "${bdir}/split"
  echo "${infile} ${av} ${bv} -> ${oprefix}-NNNN.pgm" 
  /bin/rm -f ${normfile}{,.gz}
  convert ${rawfile} pgm:- \
    | pgmnorm -bvalue ${av} -wvalue ${bv} \
    | pnmdepth 255 \
    > ${normfile}
  nice PZSplitExt \
    -inFile "${normfile:r}" \
    -outPrefix "${oprefix}" \
    -sep 127 
  gzip ${normfile}
end

echo "=== end ${cmd:t} =========" 
