#! /bin/bash -ue
# Last edited on 2025-05-04 22:43:43 by stolfi

cmd="${0##*/}"
usage="${cmd} {SIDFILE} [ {DX} {DY} {SIZEX} {SIZEY} ] > {OUT}.png"

if [[ $# -eq 1 ]]; then 
  sidf="$1"; shift;
  opts=( )
elif [[ $# -eq 5 ]]; then 
  sidf="$1"; shift;
  dx="$1"; shift;
  dy="$1"; shift;
  szx="$1"; shift;
  szy="$1"; shift;
  opts=( -ulxy ${dx} ${dy} -wh ${szx} ${szy} )
else
  echo "usage: ${usage}" 1>&2; exit 1
fi

if [[ ! -f ${sidf} ]]; then
  echo "${sidf} not found or not a file" 1>&2; exit 1
fi

# pkg=${STOLFIHOME}/pkg/mrsiddecode-2004-07-07
# prog=mrsiddecode

pkg=${STOLFIHOME}/pkg/MrSIDdecode-2006-06-1
prog=mrsidgeodecode

tiff=/tmp/$$.tif
${pkg}/${prog} -quiet -progress none -i ${sidf} -o ${tiff} ${opts[@]} 1>&2
cat ${tiff}
/bin/rm -f ${tiff}
