#! /bin/bash # Last edited on 2006-04-29 12:04:31 by stolfi PROG_NAME=${0##*/} PROG_DESC="display an enlarged picture of a gauge with a circle on it" PROG_HELP=( " ${PROG_NAME} \\" "\n [ -resize {PERCENT} ] \\" "\n -sphere \\" "\n [ radius {RAD} [ adjust {RADADJ} ] ] \\" "\n [ center {CTRX} {CTRY} [ adjust {CTRADJ} ] ] \\" "\n [ stretch {STRX} {STRY} [ adjust {STRADJ} ] ] \\" "\n [ step {ADJSTEP} ] \\" "\n {IMAGE} \\" "\n > {OUTFILE}.png" ) PROG_INFO=( "\nNAME" "\n ${PROG_NAME} - ${PROG_DESC}." "\n" "\nSYNOPSIS" "\n${PROG_HELP[@]}" "\n" "\nDESCRIPTION" "\n Creates a copy of \"{IMAGE}\" with a {PERCENT}" "\n magnification (default 1000%). Optionally draws" "\n a circle with center ({CTRX},{CTRY}) and radius" "\n {RAD}. The dimensions are in pixels of the" "\n original image and the Y coordinate is measured" "\n UPWARDS from the BOTTOM edge of the image." "\n" "\n The \"adjust\" and \"step\" parameters are accepted" "\n for compatibility with other programs, but ignored." "\n" "\nAUTHOR" "\n Created 2006-04-16 by Jorge Stolfi, Unicamp" ) # ---------------------------------------------------------------------- # INTERNAL OPTIONS # ---------------------------------------------------------------------- # COMMAND LINE PARSING # Parse command line options: resize=1000 rad="NONE" ctrx= ctry= strx= stry= while [[ ( $# -ge 1 ) && ( "/$1" =~ "/-.*" ) ]]; do if [[ ( $# -ge 2 ) && ( "/$1" == "/-resize" ) ]]; then resize="$2"; shift; shift; elif [[ ( $# -ge 1 ) && ( "/$1" == "/-sphere" ) ]]; then shift; while [[ ( $# -ge 1 ) && ( "/$1" =~ "/[a-z]*" ) ]]; do if [[ ( $# -ge 2 ) && ( "/$1" == "/radius" ) ]]; then rad="$2"; shift; shift; elif [[ ( $# -ge 3 ) && ( "/$1" == "/center" ) ]]; then ctrx="$2"; ctry="$3"; shift; shift; shift; elif [[ ( $# -ge 3 ) && ( "/$1" == "/stretch" ) ]]; then strx="$2"; stry="$3"; shift; shift; shift; elif [[ ( $# -ge 2 ) && ( "/$1" == "/adjust" ) ]]; then shift; shift; elif [[ ( $# -ge 2 ) && ( "/$1" == "/step" ) ]]; then shift; shift; else break fi done else echo "unknown option $1" 1>&2 ; echo -e "usage:\n ${PROG_HELP[@]}" 1>&2 ; exit 1 fi done if [[ "/${rad}" == "/NONE" ]]; then echo "missing or invalid radius" 1>&2 ; echo -e "usage:\n ${PROG_HELP[@]}" 1>&2 ; exit 1 fi # Get positional parameters if [[ $# -ge 1 ]]; then image=$1; shift; else echo 'missing image name "'"$1"'" ...' 1>&2 ; echo -e "usage:\n ${PROG_HELP[@]}" 1>&2 ; exit 1 fi # Check for leftover arguments: if [[ $# -ne 0 ]]; then echo 'wrong number of arguments "'"$1"'" ...' 1>&2 ; echo -e "usage:\n ${PROG_HELP[@]}" 1>&2 ; exit 1 fi # END COMMAND LINE PARSING # ---------------------------------------------------------------------- # Draw commands for inner and outer circles: cdrawop=( ) cdrawstr=( ) idrawop=( ) idrawstr=( ) odrawop=( ) odrawstr=( ) if [[ $?rad && $?ctrx && $?ctry ]]; then # Magnification factor as a fraction: fmag=`echo "a=${resize}/100.0; scale=2; (a+a)/2" | bc -lq` echo " magnification factor = ${fmag}" 1>&2 # Compute center in mag image, with 2 decimals: fctrx=`echo "a=${fmag}*(${ctrx} + 0); scale=2; (a+a)/2" | bc -lq` fctry=`echo "a=${fmag}*(${ctry} + 0); scale=2; (a+a)/2" | bc -lq` # Compute pixel inradius in mag image, with 2 decimals: fcrad=`echo "a=${fmag}*1.0; scale=2; (a+a)/2" | bc -lq` # Compute min radius in mag image, with 2 decimals: firad=`echo "a=${fmag}*(${rad} + 0); scale=2; (a+a)/2" | bc -lq` if [[ $?strx && $?stry ]]; then # Compute stretch amount in mag image, with 2 decimals: strex=`echo "x=${strx}; y=${stry}; a=sqrt(x*x+y*y); scale=2; (a+a)/2" | bc -lq` echo " stretch amount = ${strex}" 1>&2 # Compute max radius in mag image, with 2 decimals: forad=`echo "a=${firad}+${fmag}*${strex}; scale=2; (a+a)/2" | bc -lq` fi # Center in mag images, rounded to integer: midx=`echo "a=${fctrx} + 0.5; scale=0; (a+a)/2" | bc -lq` midy=`echo "a=${fctry} + 0.5; scale=0; (a+a)/2" | bc -lq` # Generate draw commands for center spot (1 pixel diameter): cradx=`echo "a=${fctrx} + ${fcrad} + 0.5; scale=0; (a+a)/2" | bc -lq` crady=`echo "a=${fctry} + 0.5; scale=0; (a+a)/2" | bc -lq` cdrawop=( -fill none -stroke yellow -draw ) cdrawstr=( "circle ${midx},${midy} ${cradx},${crady}" ) # Generate draw commands for inner circle: iradx=`echo "a=${fctrx} + ${firad} + 0.5; scale=0; (a+a)/2" | bc -lq` irady=`echo "a=${fctry} + 0.5; scale=0; (a+a)/2" | bc -lq` idrawop=( -fill none -stroke red -draw ) idrawstr=( "circle ${midx},${midy} ${iradx},${irady}" ) if [[ $?strx && $?stry ]]; then # Generate draw commands for outer circle: oradx=`echo "a=${fctrx} + ${forad} + 0.5; scale=0; (a+a)/2" | bc -lq` orady=`echo "a=${fctry} + 0.5; scale=0; (a+a)/2" | bc -lq` odrawop=( -fill none -stroke green -draw ) odrawstr=( "circle ${midx},${midy} ${oradx},${orady}" ) fi fi echo "cdrawop = ( ${cdrawop[@]} )" 1>&2 echo "cdrawstr = ( ${cdrawstr[@]} )" 1>&2 echo "idrawop = ( ${idrawop[@]} )" 1>&2 echo "idrawstr = ( ${idrawstr[@]} )" 1>&2 echo "odrawop = ( ${odrawop[@]} )" 1>&2 echo "odrawstr = ( ${odrawstr[@]} )" 1>&2 convert ${image} \ -resize "${resize}%" \ -flip \ ${cdrawop[@]} "${cdrawstr[@]}" \ ${idrawop[@]} "${idrawstr[@]}" \ ${odrawop[@]} "${odrawstr[@]}" \ -flip \ PNG:-