#! /bin/bash # Last edited on 2007-06-30 16:34:58 by stolfi PROG_NAME=${0##*/} PROG_DESC="computes a normal map from photos under various light fields" PROG_HELP=( "${PROG_NAME} \\" "\n [ -scale {SCALE} ] {RUNTAG} {IPREFIX} {NFIELDS} { {GPREFIX} {POSX} {POSY} }..." ) PROG_INFO=( "\nNAME" "\n ${PROG_NAME} - ${PROG_DESC}." "\n" "\nSYNOPSIS" "\n export TOOLDIR={TOOLDIR}" "\n ${PROG_HELP[@]}" "\n" "\nDESCRIPTION" "\n Reads the following files, for {KK} from 00 to {NFIELDS-1}:" "\n" "\n \"{IPREFIX}-{KK}.png\" - photos of some scene under various light fields." "\n \"{GPREFIX}-nrm.fni\" - normal map of a /light gauge/ object." "\n \"{GPREFIX}-{KK}-fit.fni\" - photos of the gauge under same light fields." "\n" "\n All photos of the scene must be taken with the camera in the" "\n same position and settings (except for lighting conditions), so" "\n that they are precisely registered with each other; and ditto" "\n for the gauge photos. Moreover, each gauge photo must be taken" "\n under the same light field (lighting conditions) as the corresponding" "\n scene photo." "\n" "\n Each gauge prefix must be followed by the approximate coordinates" "\n of the gauge's center in the image. These are relevant only if there" "\n are more than one gauge. Note that the Y coordinate is measured from" "\n the bottom of the image." "\n" "\n The {SCALE} is applied to the PNG files when producing the FNI files," "\n and to the gauge positions as well." "\n" "\n The script writes out the following files:" "\n" "\n \"{IPREFIX}-{RUNTAG}-nrm.fni\" - computed normal map of scene." "\n \"{IPREFIX}-{RUNTAG}-clr.fni\" - computed color map of scene." "\n" "\nSEE ALSO" "\n add-default-center-radius(1)" "\n extract-gauge-image(1)" "\nAUTHOR" "\n Created 2006-04-18 by Jorge Stolfi, Unicamp" ) # ---------------------------------------------------------------------- # INTERNAL OPTIONS showresults=1 # ---------------------------------------------------------------------- # COMMAND LINE PARSING # Parse command line switches: scale=100 while [[ ( $# -ge 1 ) && ( "/$1" =~ "/-.*" ) ]]; do if [[ ( $# -ge 2 ) && ( "/$1" == "/-scale" ) ]]; then scale="$2"; shift; shift; else echo "unknown option $1" 1>&2 ; echo -e "usage:\n ${PROG_HELP[@]}" 1>&2 ; exit 1 fi done # Get positional parameters if [[ $# -ge 6 ]]; then runtag="$1"; shift; iprefix="$1"; shift; nfields="$1"; shift; # Collect prefixes and positions of gauges: gprefixes=( ); gpositions=( ); while [[ $# -ge 3 ]]; do gprefixes[${#gprefixes[@]}]="$1"; shift; rawposx="$1"; shift; rawposy="$1"; shift; posx=`echo "a=${rawposx}*${scale}/100; scale=2; (a+a)/2" | bc -lq` posy=`echo "a=${rawposy}*${scale}/100; scale=2; (a+a)/2" | bc -lq` gpositions[${#gpositions[@]}]="${posx},${posy}" done else echo 'wrong number of arguments "'"$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 # ---------------------------------------------------------------------- # Temporary file name prefix: tmp="/tmp/$$" function makenames() { # The call "makenames {PRE} {NUM} {SUF}" echoes # "{PRE}00{SUF}", "{PRE}01{SUF}", ... "{PRE}{NUM-1}{SUF}". pre=$1; num=$2; suf=$3; i=0 while [[ ${i} -lt ${num} ]]; do printf "%s%02d%s\n" "${pre}" ${i} "${suf}" i=$(( ${i} + 1 )) done } # Create scene FNI files from PNG files, # build scene file name list ${scenefnis[@]}, # output name lists scenefnis=( ) for name in `makenames "${iprefix}-" ${nfields} ""` ; do pngfile="${name}.png" fnifile="${name}.fni" scenefnis[${#scenefnis[@]}]="${fnifile}" echo "converting ${pngfile} -> ${fnifile} ... " convert ${pngfile} -resize "${scale}%" PPM:- \ | pnm-to-fni -min 0 0 0 -max 1 1 1 \ > ${fnifile} done echo "scene FNI files = ( ${scenefnis[@]} )" # Build gauge argument list ${gaugeops[@]} gaugeops=( ) i=0 while [[ ${i} -lt ${#gprefixes[@]} ]]; do gprefix="${gprefixes[${i}]}" gpos=( `echo "${gpositions[${i}]}" | sed -e 's:,: :'` ) gnrmfile="${gprefix}-nrm.fni" gaugefnis=( `makenames "${gprefix}-" ${nfields} "-fit.fni"` ) echo "gauge coordinates = ( ${gpos[@]} )" echo "gauge normal map = ${gnrmfile}" echo "gauge FNI files = ( ${gaugefnis[@]} )" gaugeops=( ${gaugeops[@]} "-gauge" "${gpos[@]}" "${gnrmfile}" "${gaugefnis[@]}" ) i=$(( ${i} + 1 )) done echo "calling photo-to-normal... " outpre="${iprefix}-${runtag}" photo-to-normal \ -scene "${scenefnis[@]}" \ -maxval 255 -noise 0.0 \ "${gaugeops[@]}" \ -outPrefix "${outpre}" # Display results: if [[ ${showresults} -gt 0 ]]; then nrmfnifile="${outpre}-nrm.fni" nrmpngfile="${outpre}-nrm.png" fni-to-pnm -channels 3 -min -1 -1 -1 -max +1 +1 +1 < ${nrmfnifile} > ${nrmpngfile} clrfnifile="${outpre}-clr.fni" clrpngfile="${outpre}-clr.png" fni-to-pnm -channels 3 -min 0 0 0 -max 1 1 1 < ${clrfnifile} > ${clrpngfile} if [[ -s ${nrmpngfile} ]]; then display -title "%f" ${nrmpngfile} ${clrpngfile} else echo "file ${nrmpngfile} does not exist." fi fi # Cleanup: /bin/rm -f ${scenefnis[@]}