#! /bin/bash
# Last edited on 2023-09-18 11:25:20 by stolfi

sg="$1"; shift
oc="$1"; shift
img="$1"; shift
tp1="$1"; shift
x1="$1"; shift
y1="$1"; shift
r1="$1"; shift
tp2="$1"; shift
x2="$1"; shift
y2="$1"; shift
r2="$1"; shift
OUNIT="$1"; shift
MAG="$1"; shift
ifile="$1"; shift
ofile="$1"; shift

echo "=== extracting occurrence ${oc} of sign ${sg} in image ${img} ===" 1>&2
echo "feature 1 type = ${tp1} pos = (${x1},${y1}) radius = ${r1}" 1>&2
echo "feature 2 type = ${tp2} pos = (${x2},${y2}) radius = ${r2}" 1>&2
echo "input image = ${ifile}" 1>&2
echo "output image = ${ofile}" 1>&2

for tp in ${tp1} ${tp2}; do 
  if [[ ( "/${tp}" != "/C" ) && ( "/${tp}" != "/X" ) ]]; then
    echo "** invalid mark type ${tp}" 1>&2 ; exit 1
  fi
done

if [[ ! ( -s ${ifile} ) ]]; then
  echo "** ${ifile} does not exist" 1>&2 ; exit 1
fi

rotang=0.0 # Post-extraction rotation angle - zero for now.

# Compute the crop parameters:
rada=360 # Radius of first clipping (user units) 
radb=180 # Radius of second clipping (user units) 
exargs=(\
   ` glyph_extract_image_ops.gawk \
      -v x1="${x1}" -v y1="${y1}" \
      -v x2="${x2}" -v y2="${y2}" \
      -v rada=${rada} -v radb=${radb} \
      -v OUNIT=${OUNIT} \
  ` \
)
echo "exargs = ${exargs[*]}" 1>&2 

convert \
  ${ifile} \
  ${exargs[@]} \
  ${ofile}
  
