#!/bin/sh

[ -r .conf ] || exit 125

UFRAW='ufraw-batch --interpolation=ahd --out-type=tiff --out-depth=16 --overwrite --zip --rotate=no'
DCRAW='dcraw -v -r 2.245000 1.000000 1.505000 1.000000 -M -H 2 -q 3 -o 1 -b 0.79370 -n 20 -m 4 -t 0 -4 -T'
#DCRAW='dcraw -v -r 2.245000 1.000000 1.505000 1.000000 -M -H 0 -q 3 -o 1 -b 1.00000 -n 20 -m 4 -t 0 -4 -T'
#DCRAW='dcraw -v -r 2.150000 1.000000 1.600000 1.000000 -M -H 0 -q 3 -o 1 -b 1.00000 -n 20 -m 4 -t 0 -4 -T'
#DCRAW='dcraw -v -w -H 0 -q 3 -n 300 -4 -T'
EXIFTOOL='exiftool -overwrite_original -P'
#EXIFTOOL='true'
EXIFTFF="${EXIFTOOL} -TagsFromFile"
#EXIFTAG='-ImageDescription -Make -Model -Artist -WhitePoint -Copyright -GPS:all -DateTimeOriginal -CreateDate -UserComment -ColorSpace -OwnerName -SerialNumber -ISO -ExposureTime -FNumber -FocalLength -FocalLengthIn35mmFormat -Composite:DigitalZoom -FocalPlaneDiagonal -FocalPlaneXSize -FocalPlaneYSize -FocalPlaneResolutionUnit -FocalPlaneXResolution -FocalPlaneYResolution -ExifImageWidth -ExifImageHeight -ImageWidth -ImageHeight -LensType -LensModel -Lens -Orientation'
EXIFTAG='-EXIF:All -GPS:All'
PMATIC='eval panomatic -n ${CPUS} --fullscale --surfscore 10 --kdtreesteps 100 --kdtreeseconddist 0.3 --ransacdist 100 --minmatches 48'
REMAPLDR='eval nona -v -t ${CPUS} -m TIFF_m -r ldr -p UINT${DEPTH} -z DEFLATE -o tmp-'
LDRSFX='tif'
#REMAPHDR='eval nona -v -t ${CPUS} -m EXR_m -r hdr -p FLOAT -z DEFLATE -o tmp-'
#HDRSFX='exr'
REMAPHDR='eval nona -v -t ${CPUS} -m TIFF_m -r hdr -p FLOAT -z DEFLATE -o tmp-'
HDRSFX='tif'
FULLA='eval fulla -v -t ${CPUS} -s'
CCM='color_correct -E -c -p.03 -D64 -a-4096:4096+1024 -k24:40/32 -g28:36/32'
BLEND='eval enblend -v --compression=${ENCOMP} -m ${MEM} -b 16384 --fine-mask'
#BLEND='eval wine ~/.wine/drive_c/bin/enblend.exe -v --compression=${ENCOMP} -m ${MEM} -b 16384 --fine-mask'
#BLEND='eval enfuse -v --compression=${ENCOMP} -m ${MEM} -b 16384 --wExposure=0.01  --wSaturation=0.01  --wContrast=0.99 --HardMask'
#BLEND='sh ../../tools/smartblend'
#EFUSE='sh ../../tools/composite -c "-blur 0x1.25 -sigmoidal-contrast 4x20%"'
#EFUSE='eval enfuse -v --compression=${ENCOMP} -m ${MEM} -b 16384 --exposure-mu=0.45 --exposure-sigma=0.6'
EFUSE='eval enfuse -v --compression=${ENCOMP} -m ${MEM} -b 16384 --hard-mask --gray-projector=channel-mixer:0:1:0'
#EFUSE='eval enfuse -v --compression=${ENCOMP} -m ${MEM} -b 16384 --wExposure=1.00  --wSaturation=0.05  --wContrast=0.33 --HardMask --GrayProjector=value'
FFUSE='eval enfuse -v --compression=${ENCOMP} -m ${MEM} -b 16384 --wExposure=0.01  --wSaturation=0.01  --wContrast=0.99 --HardMask'
MERGE='hugin_hdrmerge -v -m khan -i 8 -a m'
#MERGE='hugin_hdrmerge -v -m khan -i 8 -a di'
#MERGE='hugin_hdrmerge -v -m avg -c -a di'
#MERGE='eval enfuse -v --compression=${ENCOMP} -m ${MEM} -b 16384'
#CONV='convert -limit memory 512 -limit map 256'
CONV='convert'
#INPNT='greycstoration -alpha 0.9 -sigma 0.2 -p 5 -a 1 -dt 20 -iter 5 -bits 16 -m mask.png -inpaint'
#INPNT='greycstoration -alpha 0.9 -sigma 0.2 -iter 512 -bits 16 -inpaint'
INPNT='greycstoration -iter 1024 -bits 16 -inpaint'
DEGHOST='deghosting_mask -v -s 50 -c 1.0 -t 150 -i 6 -a mw'

RGB='-fx (s<=0.03928)?s/12.92:((s+0.055)/1.055)^2.4'
sRGB='-fx (s<=0.00304)?s*12.92:s^0.416666667*1.055-0.055'

PFSMAX=75013120
PFSHOST=192.168.0.55

LANG=C; export LANG

. ./.conf

[ -r "${DST}${3}.pto" ] && DST=${DST}${3}

#[ -n "${3}" -a "${3}" != 1 -a "${3}" != c ] \
[ -r "${DST}.pto" ] && [ "$(ptoget -p v "${DST}.pto")" != 360 ] \
|| BLEND="${BLEND} -w"

[ -n "${TMPDIR}" ] || TMPDIR=.

[ -n "${DEPTH}" ] || DEPTH="${2}"
[ -n "${DEPTH}" ] || DEPTH=16

[ -z "${KEEPHDR}" ] || KEEPHDR="tee ${DST}.pfs"
[ -n "${KEEPHDR}" ] || KEEPHDR=cat

[ -n "${CPUS}" ] || CPUS=$(sysctl -n kern.smp.cpus)
[ -n "${CPUS}" ] || CPUS=1

#[ -n "${MEM}" ] || MEM="$(($(ulimit -d)/1024))"
#[ -n "${MEM}" ] || MEM=512
[ -n "${MEM}" ] || MEM=1536
[ ${MEM} -le 2560 ] || MEM=2560

[ -n "${IMCOMP}" ] || IMCOMP=zip
[ -n "${ENCOMP}" ] || ENCOMP=DEFLATE

prep () {
    for e in ${EXP}
    do
        eval "SRC=\${SRC${e}}"
        eval "SFX=\${SFX${e}}"
        eval "UCF=\${UCF${e}}"
        N=0
        for n in ${SRC}
        do
            o=$(printf '%02d%s' ${N} ${SFX})
            N=$((${N}+1))
            #if [ "${n}" = SSKIP ]
            #then
            #    if [ ${o} = 03-e02 -o ${o} = 04-e02 ]
            #    then
            #        eval "NSFX=\${SFX$((${e}+1))}"
            #        ln -s $(printf '%02d%s.tiff' ${N} ${NSFX}) ${o}.tiff
            #    fi
            #fi
            [ -z "${n}" -o "${n}" = SKIP -o "${n}" = SSKIP ] && continue
            [ -r ${o}.tiff ] && continue
            l="$(expr "${n}" : 'LINK:\(.*\)')" && {
                ln -s "${l}" ${o}.tiff
                continue
            }
            [ -r ${n}.RAW.bz2 ] && bzcat ${n}.RAW.bz2 >${n}.RAW
            [ -r ${n}.RAW.gz ] && zcat ${n}.RAW.gz >${n}.RAW
            [ -r ${n}.CR2 ] && ln -s ${n}.CR2 ${n}.RAW
            if [ -r ${n}.RAW ]
            then
                if [ -z "${LIN}" ]
                then
                    conf=${n}.ufraw
                    [ -z "${UCONF}" ] || conf=${UCONF}.ufraw
                    [ -z "${UCF}" ] || conf=${UCF}.ufraw
                    [ -r ${conf} ] || exit 125
                    ${UFRAW} --conf=${conf} --output=${n}.tiff ${n}.RAW
                    ${CONV} ${n}.tiff \
                        -flop \
                        -compress ${IMCOMP} ${o}.tiff
                else
                    ${DCRAW} ${n}.RAW
                fi
                #${EXIFTOOL} -EXIF:All= ${n}.tiff
                #case ${N} in [6-9]) ${CONV} ${n}.tiff -rotate -90 -compress ${IMCOMP} ${n}.tiff;; esac
                tca=''
                if [ -z "${TCA}" -a -n "${LENS}" ]
                then
                    fl="$(dcraw -i -v ${n}.RAW|awk '/^Focal length: /{print $3}')"
                    #fl="$(exiftool -n -e -FocalLength ${n}.RAW |awk '{print $NF}')"
                    tca="$( ( cd ../../tools/tca && echo "disp(tca('tca.db','${LENS}',${fl},1,0))"|octave -qf|cat|head -1 ) )"
                    echo "Focal length: ${fl}; TCA correction: ${tca}"
                else
                    tca="${TCA}"
                fi
                [ -z "${tca}" ] || ${FULLA} ${tca} -o ${n}.tiff ${n}.tiff
                eval "$(identify -format 'w=%w; h=%h' ${n}.tiff)"
                ${CONV} ${n}.tiff \
                    -crop $((${w}-16))x$((${h}-16))+8+8 +repage \
                    -compress ${IMCOMP} ${o}.tiff
                rm -f ${n}.tiff
                if [ -n "${LIN}" -a "${1}" = prep -a "${DEPTH}" = 8 ]
                then
                    if [ -n "${3}" ]
                    then
                        case ${e} in
                        0) m='100,50';;
                        1) m='400,50';;
                        2) m='1600,50';;
                        *) m='100,100';;
                        esac
                    fi
                    echo "${N}/${e}: ${m}"
                    [ -z "${m}" ] || m="-modulate ${m}"
                    ${CONV} ${o}.tiff \
                        ${m} ${sRGB} \
                        -compress ${IMCOMP} ${o}.tiff
                fi
                [ "${DEPTH}" = 16 ] \
                || ${CONV} ${o}.tiff -gamma 1.5 -depth 8 -compress ${IMCOMP} ${o}.tiff
                #[ -r ${n}.JPG ] \
                #&& exiftool -overwrite_original_in_place -P -TagsFromFile ${n}.JPG ${o}.tiff
                #exiftool -overwrite_original_in_place -P -TagsFromFile ${n}.RAW ${o}.tiff
            elif [ -r ${n}.JPG ]
            then
                # sRGB => RGB
                [ -z "${LIN}" ] || rgb="${RGB}"
                ${CONV} ${n}.JPG \
                    -noise 3 -unsharp 0x0.3 \
                    ${rgb} \
                    -depth ${DEPTH} \
                    -compress ${IMCOMP} ${o}.tiff
                #exiftool -overwrite_original_in_place -P -TagsFromFile ${n}.JPG ${o}.tiff
            else
                exit 126
            fi
            #[ ${N} -eq 3 ] && cp ${o}.tiff 06.tiff
            #[ ${N} -eq 4 ] && cp ${o}.tiff 07.tiff
            [ -r mask-${o}.png -a \( "${1}" != prep -o "${DEPTH}" != 8 \) ] \
            && ${CONV} ${o}.tiff -matte \
                mask-${o}.png -compose copyopacity -composite \
                -compress ${IMCOMP} ${o}.tiff
            #    mask-${o}.png -compose multiply -composite \
            [ -r ${n}.RAW ] && ${EXIFTFF} ${n}.RAW ${EXIFTAG} ${o}.tiff
            [ -r ${n}.RAW -a \( -r ${n}.RAW.bz2 -o -r ${n}.RAW.gz -o -r ${n}.CR2 \) ] \
            && rm -f ${n}.RAW
            [ -r ${n}.JPG ] && ${EXIFTFF} ${n}.JPG ${EXIFTAG} ${o}.tiff
            #[ ${N} -eq 54 ] && ln -s ${o}.tiff "54${SFX}.tiff"
        done
    done
}

auto () {
    autoexp () {
        x=${1}; shift
        e=${1}; shift
        [ -r ${DST}${e}${x}-ao.pto ] && continue
        [ ${#} -lt 2 ] && continue
        S=''
        for a in ${*}
        do
            s="$(printf '%s%s.tiff' ${a} ${e})"
            [ -r "${s}" ] && S="${S} ${s}"
        done
        printf 'e=%s; S=%s\n' ${e} "${S}"
        ${PMATIC} \
            --linearmatch --linearmatchlen 8 \
            --sieve1width 64 --sieve1height 48 --sieve1size 8 \
            --sieve2width 16 --sieve2height 16 --sieve2size 4 \
            -o ${DST}${e}${x}-pm.pto ${S}
        autooptimiser -a -l -s -o ${DST}${e}${x}-ao.pto ${DST}${e}${x}-pm.pto
    }
    autoexpex () {
        e="${1}"; shift
        [ -r ${DST}${e}-ao.pto ] && continue
        if [ -z "${ROWS}" ]
        then
            autoexp '' "${e}" "${@}"
            return ${?}
        fi
        eval "${ROWS}"
        for re in ${REXP}
        do
            eval "RSFX=\${RSFX${re}}"
            [ "${e}" = "${RSFX}" ] && break
        done
        if [ ${?} -eq 0 ]
        then
            eval "RROWS=\${ROWS${re}}"
            for r in ${RROWS}
            do
                eval "RSFX=\${RSFX${re}${r}}"
                eval "RSRC=\${RSRC${re}${r}}"
                autoexp "${RSFX}" "${e}" ${RSRC}
            done
            ptomerge ${DST}${e}[^-]*-ao.pto ${DST}${e}-ao.pto
            ptosort -i n ${DST}${e}-ao.pto ${DST}${e}-ao.pto
            autooptimiser -a -l -s -o ${DST}${e}-ao.pto ${DST}${e}-ao.pto
            return ${?}
        fi
        autoexp '' "${e}" "${@}"
    }
    autosect () {
        a=${1}; shift
        a="$(printf '%02d' ${a})"
        [ -r ${DST}-${a}-ao.pto ] && continue
        echo "${DST}-${a}-ao.pto: ${#}"
        [ ${#} -lt 2 ] && continue
        S=''
        for e in ${*}
        do
            eval "$(echo ${e} | awk 'BEGIN{FS=":"}{print "e="$1"; aa="$2}')"
            if [ -z "${aa}" ]
            then
                aa=${a}
            else
                aa="$(printf '%02d' ${aa})"
            fi
            s="$(printf '%s%s.tiff' ${aa} ${e})"
            [ -r "${s}" ] && S="${S} ${s}"
        done
        printf 'a=%s; S=%s\n' ${a} "${S}"
        ${PMATIC} \
            --sieve1width 64 --sieve1height 48 --sieve1size 8 \
            --sieve2width 32 --sieve2height 24 --sieve2size 4 \
            -o ${DST}-${a}-pm.pto ${S}
        autooptimiser -a -o ${DST}-${a}-ao.pto ${DST}-${a}-pm.pto
    }
    for e in ${EXP}
    do
        eval "SFX=\${SFX${e}}"
        A=''
        for a in ${SECT}
        do
            eval "echo \"\$EXP${a}\" | grep -q '\<${e}\>'" || continue
            A="${A} $(printf '%02d' ${a})"
        done
        autoexpex "${SFX}" ${A}
    done
    for a in ${SECT}
    do
        eval "AEXP=\${EXP${a}}"
        E=''
        for e in ${AEXP}
        do
            eval "$(echo ${e} | awk 'BEGIN{FS=":"}{print "e="$1"; aa="$2}')"
            eval "SFX=\${SFX${e}}"
            [ -z "${aa}" ] || SFX=${SFX}:${aa}
            E="${E} ${SFX}"
        done
        autosect ${a} ${E}
    done
}

cluster () {
    clexp () {
        e=${1}
        [ -r ${DST}${e}-ao.pto ] || return
        [ -z "${2}" ] || D="-D${2}"
        [ -z "${3}" ] || d="-d${3}"
        yrp=y,r,p/0-
        [ -z "${4}" ] || yrp="y,r,p/0-$((${4}-1)),$((${4}+1))-:r,p/${4}"
        echo "yrp: ${yrp}"
        perl ../../tools/ptsed -O0xff \
            -s -Vb,v/0:a,c,d,e/0-:${yrp} \
            -o ${DST}${e}-ao.txt ${DST}${e}-ao.pto
        PToptimizer ${DST}${e}-ao.txt
        perl ../../tools/ptscluster -O0xff \
            -x48 -y36 ${D} ${d} \
            -o ${DST}${e}-ao.txt ${DST}${e}-ao.txt \
        && perl ../../tools/ptsed -O0xff \
            -s -r \
            -o ${DST}${e}-ao.txt ${DST}${e}-ao.txt
        PToptimizer ${DST}${e}-ao.txt
        perl ../../tools/ptscluster -O0xff \
            -x24 -y18 ${D} ${d} \
            -o ${DST}${e}-ao.txt ${DST}${e}-ao.txt \
        && perl ../../tools/ptsed -O0xff \
            -s -r \
            -o ${DST}${e}-ao.txt ${DST}${e}-ao.txt
        PToptimizer ${DST}${e}-ao.txt
        perl ../../tools/ptscluster -O0xff \
            ${D} ${d} \
            -o ${DST}${e}-ao.txt ${DST}${e}-ao.txt \
        && perl ../../tools/ptsed -O0xff \
            -s -r \
            -o ${DST}${e}-ao.txt ${DST}${e}-ao.txt
        PToptimizer ${DST}${e}-ao.txt
        perl ../../tools/ptsed -O0xff \
            -i \
            -o ${DST}${e}.pto ${DST}${e}-ao.txt
    }
    clsect () {
        a=${1}
        a="$(printf '%02d' ${a})"
        [ -r ${DST}-${a}-ao.pto ] || return
        #    -Ib,v/1-:=0:a,c,d,e/0-:0 \
        perl ../../tools/ptsed -O0xff \
            -Vy,r,p/1- \
            -s -o ${DST}-${a}-ao.txt ${DST}-${a}-ao.pto
        PToptimizer ${DST}-${a}-ao.txt
        perl ../../tools/ptscluster -O0xff \
            -r0x21 -x48 -y36 -d12 -D24 \
            -o ${DST}-${a}-ao.txt ${DST}-${a}-ao.txt \
        && perl ../../tools/ptsed -O0xff \
            -s -r \
            -o ${DST}-${a}-ao.txt ${DST}-${a}-ao.txt
        PToptimizer ${DST}-${a}-ao.txt
        perl ../../tools/ptscluster -O0xff \
            -r0x21 -x48 -y36 -d8 -D16 \
            -o ${DST}-${a}-ao.txt ${DST}-${a}-ao.txt \
        && perl ../../tools/ptsed -O0xff \
            -s -r \
            -o ${DST}-${a}-ao.txt ${DST}-${a}-ao.txt
        PToptimizer ${DST}-${a}-ao.txt
        perl ../../tools/ptscluster -O0xff \
            -r0x21 -x48 -y36 -D10 \
            -o ${DST}-${a}-ao.txt ${DST}-${a}-ao.txt \
        && perl ../../tools/ptsed -O0xff \
            -s -r \
            -Va,c,d,e/1-:y,r,p/1- \
            -o ${DST}-${a}-ao.txt ${DST}-${a}-ao.txt
        PToptimizer ${DST}-${a}-ao.txt
        perl ../../tools/ptscluster -O0xff \
            -r0x21 -x24 -y18 -D7 \
            -o ${DST}-${a}-ao.txt ${DST}-${a}-ao.txt \
        && perl ../../tools/ptsed -O0xff \
            -s -r \
            -o ${DST}-${a}-ao.txt ${DST}-${a}-ao.txt
        PToptimizer ${DST}-${a}-ao.txt
        perl ../../tools/ptscluster -O0xff \
            -r0x21 -D5 \
            -o ${DST}-${a}-ao.txt ${DST}-${a}-ao.txt \
        && perl ../../tools/ptsed -O0xff \
            -s -r \
            -o ${DST}-${a}-ao.txt ${DST}-${a}-ao.txt
        #PToptimizer ${DST}-${a}-ao.txt
        #perl ../../tools/ptscluster -O0xff \
        #    -r0x21 -D3 \
        #    -o ${DST}-${a}-ao.txt ${DST}-${a}-ao.txt \
        #&& perl ../../tools/ptsed -O0xff \
        #    -s -r \
        #    -o ${DST}-${a}-ao.txt ${DST}-${a}-ao.txt
        PToptimizer ${DST}-${a}-ao.txt
        perl ../../tools/ptsed -O0xff \
            -i \
            -o ${DST}-${a}.pto ${DST}-${a}-ao.txt
    }
    for e in ${EXP}
    do
        eval "SFX=\${SFX${e}}"
        anc="$(eval "echo \"\${ANC${e}}\"")"
        [ -n "${anc}" ] || anc="${ANC}"
        echo "anv: ${anc}; ANC: ${ANC}"
        clexp "${SFX}" "${DMAX}" "${DMIN}" "${anc}"
    done
    for a in ${SECT}
    do
        clsect ${a}
    done
}

mkldrmf () {
    RM='rm -f'
    [ -z "${3}" ] || RM='echo'
    f=$(awk '/^p /{if(match($0,/ w[0-9]+/)){w=substr($0,RSTART+2,RLENGTH-2);match($0,/ h[0-9]+/);h=substr($0,RSTART+2,RLENGTH-2);print w"x"h}nextfile}' ${DST}.pto)
    [ -z "${f}" ] || f="-f ${f}"
    c=$(awk '/^p /{if(match($0,/ S[0-9]+,[0-9]+,[0-9]+,[0-9]+/)){split(substr($0,RSTART+2,RLENGTH-2),a,/,/);print(a[2]-a[1])"x"(a[4]-a[3])"+"a[1]"+"a[3]}nextfile}' ${DST}.pto)
    [ -z "${c}" ] || f="-f ${c}"
    #expr "${BLEND}" : '.*smartblend' >/dev/null && f=''
    cat <<EOT
DST?=${DST}

CPUS?=${CPUS}
MEM?=${MEM}
DEPTH?=${DEPTH}
IMCOMP?=${IMCOMP}
ENCOMP?=${ENCOMP}

CONV?=${CONV}
REMAP?=${REMAPLDR}
BLEND?=${BLEND} ${f}
EFUSE?=${EFUSE}
DEGHOST?=${DEGHOST}

RM=${RM}
MV=mv

all: \${DST}.tiff

EOT
    SRC=''
    BLIN=''
    for a in ${SECT}
    do
        FUIN=''
        t=''
        eval "AEXP=\${EXP${a}}"
        nexp=$(echo "${AEXP}" | wc -w)
        for e in ${AEXP}
        do
            eval "$(echo ${e} | awk 'BEGIN{FS=":"}{print "e="$1"; aa="$2}')"
            eval "SFX=\${SFX${e}}"
            eval "EV=\${EV${e}}; AEV=\${EV${e}_${a}}"
            eval "ATARG=\${TARG_${a}}; ETARG=\${TARG${e}}; EATARG=\${TARG${e}_${a}}"
            [ -n "${aa}" ] || aa=${a}
            n="$(printf '%02d%s.tiff' ${aa} ${SFX})"
            SRC="${SRC} ${n}"
            eval "$(awk '
                BEGIN {
                    i=0;
                }
                /^p / {
                    if (match($0,/ E[^ ]+/))
                        print "EP="substr($0,RSTART+2,RLENGTH-2);
                }
                /^i / {
                    if (match($0," n\"?"n"\"?")) {
                        print "I="i;
                        if (match($0,/ Eev[^ ]+/))
                            print "E="substr($0,RSTART+4,RLENGTH-4);
                        nextfile
                    }
                    ++i;
                }
            ' n=${n} ${DST}.pto)"
            [ -n "${I}" ] || return 1
            t=$(printf 'tmp-%04d.%s' ${I} ${LDRSFX})
            [ ${nexp} -gt 1 ] || E="${EP}"
            [ -z "${EV}" ] || E="${EV}"
            [ -z "${AEV}" ] || E="${AEV}"
            [ -z "${E}" ] || E="-e ${E}"
            [ ${a} -ne ${aa} ] \
            || cat <<EOT
${t}: \${DST}.pto ${n}
	@\${REMAP} -i ${I} ${E} \${DST}.pto
EOT
            FARG="${t}"
            [ -z "${LIN}" ] \
            || FARG="-profile '../../tools/profiles/RGB(dcraw).icm' ${t} -profile '../../tools/profiles/sRGB Color Space Profile.ICM'"
            [ -z "${ATARG}" ] \
            || FARG="${FARG} ${ATARG}"
            [ -z "${ETARG}" ] \
            || FARG="${FARG} ${ETARG}"
            [ -z "${EATARG}" ] \
            || FARG="${FARG} ${EATARG}"
            cat <<EOT
f${t}: ${t}
EOT
            if [ "${FARG}" = "${t}" ]
            then
                cat <<EOT
	@ln ${t} f${t}
EOT
            else
                cat <<EOT
	@\${CONV} \\
		${FARG} \\
		-compress \${IMCOMP} f${t}
EOT
            fi
            cat <<EOT
	@\${RM} ${t}
EOT
            FUIN="${FUIN} f${t}"
        done
        FOUT=$(printf 'etmp-%02d.%s' ${a} ${LDRSFX})
        cat <<EOT
${FOUT}: ${FUIN}
EOT
        if [ ${nexp} -gt 1 ]
        then
            [ -z "${STK}" ] \
            || cat <<EOT
	@env CONV='\${CONV}' RM='\${RM}' sh ../../tools/stackmask ${FUIN}
EOT
            [ -z "${DGH}" ] \
            || cat <<EOT
	@\${DEGHOST} ${FUIN}
	@for n in ${FUIN}; \\
	 do \\
		\${CONV} \$\${n} \\( +clone -fx '1-s.o' +matte \$\${n%.tif}_mask.tif -compose multiply -composite \\) -compose copy-opacity -composite \$\${n}; \\
		\${RM} \$\${n%.tif}_mask.tif; \\
	 done
EOT
            cat <<EOT
	@\${EFUSE} -o ${FOUT} ${FUIN}
EOT
        else
            cat <<EOT
	@ln ${FUIN} ${FOUT}
EOT
        fi
        cat <<EOT
	@\${RM} ${FUIN}
EOT
        BLIN="${BLIN} ${FOUT}"
    done
    SRC="$(echo "${SRC}"|sed -e 's/^ *\(.*\) *$/\1/')"
    BLIN="$(echo "${BLIN}"|sed -e 's/^ *\(.*\) *$/\1/')"
    cat <<EOT

SRC=${SRC}
BLIN=${BLIN}

tmp.tiff: \${BLIN}
	@\${RM} \${SRC}
	@\${BLEND} -o tmp.tiff \${BLIN}
	@\${RM} \${BLIN}
\${DST}.tiff: tmp.tiff
	@\${CONV} tmp.tiff \\
		+matte +repage \\
		-compress \${IMCOMP} \${DST}.tiff
	@\${RM} tmp.tiff
EOT
#		-sharpen 0x0.25 \\
#		-rotate 90 \\
#		-unsharp 0x0.20 \\
#	@pfsin tmp.tiff \\
#	 | pfstmo_reinhard02 -v -k 0.09 \\
#	 | pfsgamma -m 0.9 -g 2.2 \\
#	 | pfsoutimgmagick \${DST}.tiff
#	@\${RM} tmp.tiff
#	@\${CONV} \${DST}.tiff \\
#		+matte +repage \\
#		-unsharp 0x0.3 \\
#		-compress \${IMCOMP} \${DST}.tiff
#		-unsharp 0x0.80+0.6+0.05 \\
}

mkhdrmf () {
    RM='rm -f'
    [ -z "${3}" ] || RM='echo'
    f=$(awk '/^p /{if(match($0,/ w[0-9]+/)){w=substr($0,RSTART+2,RLENGTH-2);match($0,/ h[0-9]+/);h=substr($0,RSTART+2,RLENGTH-2);print w"x"h}nextfile}' ${DST}.pto)
    [ -z "${f}" ] || f="-f ${f}"
    c=$(awk '/^p /{if(match($0,/ S[0-9]+,[0-9]+,[0-9]+,[0-9]+/)){split(substr($0,RSTART+2,RLENGTH-2),a,/,/);print(a[2]-a[1])"x"(a[4]-a[3])"+"a[1]"+"a[3]}nextfile}' ${DST}.pto)
    c=$(awk '/^p /{if(match($0,/ S[0-9]+,[0-9]+,[0-9]+,[0-9]+/)){split(substr($0,RSTART+2,RLENGTH-2),a,/,/);print(a[2]-a[1])"x"(a[4]-a[3])}nextfile}' ${DST}.pto)
    [ -z "${c}" ] || f="-f ${c}"
    cat <<EOT
DST?=${DST}

CPUS?=${CPUS}
MEM?=${MEM}
DEPTH?=${DEPTH}
IMCOMP?=${IMCOMP}
ENCOMP?=${ENCOMP}

CONV?=${CONV}
REMAP?=${REMAPHDR}
BLEND?=${BLEND} ${f}
MERGE?=${MERGE}

RM=${RM}
MV=mv

all: \${DST}.tiff

EOT
    SRC=''
    BLIN=''
    for a in ${SECT}
    do
        FUIN=''
        MASK=''
        t=''
        eval "AEXP=\${EXP${a}}"
        for e in ${AEXP}
        do
            DEP=${t}
            eval "$(echo ${e} | awk 'BEGIN{FS=":"}{print "e="$1"; aa="$2}')"
            eval "SFX=\${SFX${e}}"
            [ -n "${aa}" ] || aa=${a}
            n="$(printf '%02d%s.tiff' ${aa} ${SFX})"
            SRC="${SRC} ${n}"
            eval "$(awk 'BEGIN{i=0}/^i /{if(match($0," n\"?"n"\"?")){print "I="i;if(match($0,/ Eev[^ ]+/))print "E="substr($0,RSTART+4,RLENGTH-4);nextfile}++i}' n=${n} ${DST}.pto)"
            [ -n "${I}" ] || return 1
            [ -n "${E}" ] || E=0
            t=$(printf 'tmp-%04d.%s' ${I} ${HDRSFX})
            m=$(printf 'tmp-%04d_gray.pgm' ${I})
            FUIN="${FUIN} ${t}"
            MASK="${MASK} ${m}"
            #[ ${a} -eq ${aa} ] || continue
            FOUT=${t}
            cat <<EOT
${t}: \${DST}.pto ${n} ${DEP}
	@\${REMAP} -i ${I} \${DST}.pto
EOT
        done
        FUIN="$(echo "${FUIN}"|sed -e 's/^ *\(.*\) *$/\1/')"
        MASK="$(echo "${MASK}"|sed -e 's/^ *\(.*\) *$/\1/')"
        [ "${FUIN}" = "${FOUT}" ] || \
        cat <<EOT
	@\${MERGE} -o .${FOUT} ${FUIN}
	@\${RM} ${FUIN}
	@\${MV} .${FOUT} ${FOUT}
EOT
        cat <<EOT
	@\${RM} ${MASK}
EOT
        BLIN="${BLIN} ${FOUT}"
    done
    SRC="$(echo "${SRC}"|sed -e 's/^ *\(.*\) *$/\1/')"
    BLIN="$(echo "${BLIN}"|sed -e 's/^ *\(.*\) *$/\1/')"
    resize='-sharpen 0x0.75'
    #[ "${3}" = a ] \
    #|| resize='-unsharp 0x0.3 -resize x4096 -unsharp 0x0.75+0.6+0.05'
    #[ "${3}" = b ] \
    #|| resize='-unsharp 0x0.75+0.6+0.05'
    bright=-1.33
    light=0.95
    chrom=0.40
    [ "${3}" = b ] && bright=-1.50
    [ "${3}" = b ] && light=0.9625
    cat <<EOT

SRC=${SRC}
BLIN=${BLIN}

tmp.tiff: \${BLIN}
	@\${RM} \${SRC}
	@\${BLEND} -o tmp.tiff \${BLIN}
	@\${RM} \${BLIN}
\${DST}.tiff: tmp.tiff
	@pfsin tmp.tiff \\
	 | pfsclamp --min 1e-20: --max 1e+20 \\
     | pfswb -v -r 1.0000 -g 1.0000 -b 1.0000 \\
	 | pfstmo_reinhard05 -v -b ${bright} -c ${chrom} -l ${light} \\
	 | pfsoutimgmagick -s -b 16 \${DST}.tiff
	@\${RM} tmp.tiff
	@\${CONV} \${DST}.tiff \\
		+matte +repage \\
		${resize} \\
		-compress \${IMCOMP} \${DST}.tiff
EOT
#	 | pfstmo_mantiuk06 -v -f 0.45 -s 0.75 \\
#	 | pfsgamma -m 0.825 -g 2.2 \\
#	 | pfstmo_mantiuk08 -v -d pd=crt -c 0.40 -e 1.00 \\
#	 | pfsgamma -m 0.65 -g 1.00 \\
#	 | pfstmo_reinhard02 -v -k 0.090 \\
#	 | pfsgamma -m 0.825 -g 2.185 \\
#	 | pfstmo_reinhard02 -v -k 0.0225 \\
#	 | pfsgamma -m 0.750 -g 2.300 \\
#	 | pfsoutimgmagick \${DST}.tiff
}

inpaint () {
    g=${1}
    n=${2}
    [ -r mask-${DST}.${n}.png ] || return
    ${CONV} ${DST}.tiff \
        -crop ${g} \
        -compress ${IMCOMP} ${DST}.${n}.tiff 
    mv ${DST}.tiff ${DST}.0.tiff
    greycstoration -inpaint ${DST}.${n}.tiff \
        -iter 1024 -bits 16 \
        -m mask-${DST}.${n}.png \
        -o ${DST}.${n}.tiff
    ${CONV} ${DST}.0.tiff ${DST}.${n}.tiff \
        -geometry ${g} \
        -compose src-over -composite \
        -compress ${IMCOMP} ${DST}.tiff
    rm ${DST}.0.tiff ${DST}.${n}.tiff
}

apply_patch () {
    g=${1}; shift
    se=${1}; shift
    de=${1}; shift
    n=${1}; shift
    [ -n ${n} ] || n=1
    rgb=''; srgb=''
    [ -n ${LIN} ] || { rgb="${RGB}"; srgb="${sRGB}"; }
    ${CONV} ${DST}-e${se}.tiff \
        -crop ${g} +repage \
        ${rgb} "${@}" ${srgb} \
        -compress ${IMCOMP} ${DST}-e${de}.${n}.tiff 
    [ -r mask-${DST}-e${de}.${n}.png ] \
    && ${CONV} ${DST}-e${de}.${n}.tiff \
        mask-${DST}-e${de}.${n}.png -compose copyopacity -composite \
        -compress ${IMCOMP} ${DST}-e${de}.${n}.tiff
    mv ${DST}-e${de}.tiff ${DST}-e${de}.0.tiff
    ${CONV} ${DST}-e${de}.0.tiff ${DST}-e${de}.${n}.tiff \
        -geometry ${g} \
        -compose src-over -composite \
        -compress ${IMCOMP} ${DST}-e${de}.tiff
    rm ${DST}-e${de}.0.tiff ${DST}-e${de}.${n}.tiff
}

apply_patch2 () 
{
    dg=${1}; shift
    sg=${1}; shift
    n=${1}; shift
    [ -n ${n} ] || n=1
    rgb=''; srgb=''
    [ -n ${LIN} ] || { rgb="${RGB}"; srgb="${sRGB}"; }
    ${CONV} ${DST}.tiff \
        -crop ${sg} +repage \
        ${rgb} "${@}" ${srgb} \
        -compress ${IMCOMP} ${DST}.${n}.tiff 
    [ -r mask-${DST}.${n}.png ] \
    && ${CONV} ${DST}.${n}.tiff \
        mask-${DST}.${n}.png -compose copyopacity -composite \
        -compress ${IMCOMP} ${DST}.${n}.tiff
    mv ${DST}.tiff ${DST}.0.tiff
    ${CONV} ${DST}.0.tiff ${DST}.${n}.tiff \
        -geometry ${dg} \
        -compose src-over -composite \
        -compress ${IMCOMP} ${DST}.tiff
    rm ${DST}.0.tiff ${DST}.${n}.tiff
}

mkpano () {
    f=$(awk '/^p /{if(match($0,/w[0-9]+/)){w=substr($0,RSTART+1,RLENGTH-1);match($0,/h[0-9]+/);h=substr($0,RSTART+1,RLENGTH-1);print w"x"h}nextfile}' ${DST}.pto)
    [ -z "${f}" ] || f="-f ${f}"
    c=$(awk '/^p /{if(match($0,/S[0-9]+,[0-9]+,[0-9]+,[0-9]+/)){split(substr($0,RSTART+1,RLENGTH-1),a,/,/);print(a[2]-a[1])"x"(a[4]-a[3])"+"a[1]"+"a[3]}nextfile}' ${DST}.pto)
    [ -z "${c}" ] || f="-f ${c}"
    #[ -z "${c}" ] || c="-crop ${c} +repage"
    for e in ${EXP}
    do
        eval "SFX=\${SFX${e}}"
        eval "SRC=\${SRC${e}}"
        [ -r ${DST}${SFX}.tiff ] && continue
        RMIN=''
        BLIN=''
        N=0
        for s in ${SRC}
        do
            n="$(printf '%02d%s.tiff' ${N} ${SFX})"
            N=$((${N}+1))
            [ ${s} = SKIP ] && continue
            eval "$(awk 'BEGIN{i=0}/^i /{if(match($0," n\"?"n"\"?")){print "I="i;if(match($0,/ Eev[^ ]+/))print "E="substr($0,RSTART+4,RLENGTH-4);nextfile}++i}' n=${n} ${DST}.pto)"
            [ -n "${I}" ] || return 1
            RMIN="${RMIN} -i ${I}"
            BLIN=$(printf "${BLIN} tmp-%04d.tif" ${I})
        done
        ${REMAPLDR} ${RMIN} ${DST}.pto \
        && ${BLEND} ${f} -o tmp${SFX}.tiff ${BLIN} \
        && rm ${BLIN} \
        && ${CONV} tmp${SFX}.tiff \
            +matte +repage \
            -unsharp 0x1.00+0.6+0.05 \
            -compress ${IMCOMP} ${DST}${SFX}.tiff \
        && rm tmp${SFX}.tiff
            #-modulate 100,95 \
    done
    [ -n "${3}" ] \
    || for e in ${EXP}
    do
        eval "SFX=\${SFX${e}}"
        rm [0-9][0-9]${SFX}.tiff
    done
    #apply_patch 3072x864+0+512 04 05 1  -modulate  50.000,100
    #apply_patch 3072x864+0+512 04 06 1  -modulate  25.000,100
    #apply_patch 64x64+1440+1472 05 04 2 -modulate 200.000,100
    #apply_patch 64x64+1440+1472 05 03 2 -modulate 400.000,100
    #apply_patch 3072x864+0+512 04 05 1 -evaluate multiply 0.495 -modulate 100,126
    #apply_patch 3072x864+0+512 04 06 1 -evaluate multiply 0.225 -modulate 100,129
    #apply_patch 64x64+1440+1472 05 04 2 -modulate 100,75 -evaluate multiply 2.20
    #apply_patch 64x64+1440+1472 05 03 2 +contrast -modulate 100,65 -evaluate multiply 3.70
    #inpaint 416x64+1536+1920 1
}

mkpanohdr () {
    [ -r ${DST}.tiff ] && continue
    f=$(awk '/^p /{if(match($0,/w[0-9]+/)){w=substr($0,RSTART+1,RLENGTH-1);match($0,/h[0-9]+/);h=substr($0,RSTART+1,RLENGTH-1);print w"x"h}nextfile}' ${DST}.pto)
    [ -z "${f}" ] || f="-f ${f}"
    c=$(awk '/^p /{if(match($0,/S[0-9]+,[0-9]+,[0-9]+,[0-9]+/)){split(substr($0,RSTART+1,RLENGTH-1),a,/,/);print(a[2]-a[1])"x"(a[4]-a[3])"+"a[1]"+"a[3]}nextfile}' ${DST}.pto)
    [ -z "${c}" ] || f="-f ${c}"
    #[ -z "${c}" ] || c="-crop ${c} +repage"
    BLIN=''
    for a in ${SECT}
    do
        eval "AEXP=\${EXP${a}}"
        RMIN=''
        MGIN=''
        GPGM=''
        for e in ${AEXP}
        do
            eval "SFX=\${SFX${e}}"
            n="$(printf '%02d%s.tiff' ${a} ${SFX})"
            eval "$(awk 'BEGIN{i=0}/^i /{if(match($0," n\"?"n"\"?")){print "I="i;if(match($0,/ Eev[^ ]+/))print "E="substr($0,RSTART+4,RLENGTH-4);nextfile}++i}' n=${n} ${DST}.pto)"
            [ -n "${I}" ] || return 1
            RMIN="${RMIN} -i ${I}"
            MGIN="$(printf "${MGIN} tmp-%04d.%s" ${I} ${HDRSFX})"
            GPGM="$(printf "${GPGM} tmp-%04d_gray.pgm" ${I})"
        done
        MGOUT="$(printf 'tmp-%02d.%s' ${a} ${HDRSFX})"
        BLIN="${BLIN} ${MGOUT}"
        ${REMAPHDR} ${RMIN} ${DST}.pto \
        && ${MERGE} -o ${MGOUT} ${MGIN} \
        && rm ${MGIN} ${GPGM}
    done
    ${BLEND} ${f} -o hdr.tiff ${BLIN} \
    && rm ${BLIN} \
    && pfsin hdr.tiff \
	   | pfsclamp --min 1e-20: --max 1e+20 \
       | pfswb -v -r 1.000 -g 1.000 -b 1.000 \
       | pfstmo_reinhard02 -v -k 0.0900 \
       | pfsgamma -m 0.95 -g 2.2 \
       | pfsoutimgmagick tmp.tiff \
    && rm hdr.tiff \
    && ${CONV} tmp.tiff \
        +matte +repage \
        -unsharp 0x1.00+0.6+0.05 \
        -compress ${IMCOMP} ${DST}.tiff \
    && rm tmp.tiff
       #| pfstmo_reinhard05 -v -l 0.1 -c 0.05 \
    [ -n "${3}" ] \
    || for e in ${EXP}
    do
        eval "SFX=\${SFX${e}}"
        rm [0-9][0-9]${SFX}.tiff
    done
    test -r ${DST}.tiff
}

mkpanofuse () {
    [ -r ${DST}.tiff ] && continue
    f=$(awk '/^p /{if(match($0,/w[0-9]+/)){w=substr($0,RSTART+1,RLENGTH-1);match($0,/h[0-9]+/);h=substr($0,RSTART+1,RLENGTH-1);print w"x"h}nextfile}' ${DST}.pto)
    [ -z "${f}" ] || f="-f ${f}"
    c=$(awk '/^p /{if(match($0,/S[0-9]+,[0-9]+,[0-9]+,[0-9]+/)){split(substr($0,RSTART+1,RLENGTH-1),a,/,/);print(a[2]-a[1])"x"(a[4]-a[3])"+"a[1]"+"a[3]}nextfile}' ${DST}.pto)
    [ -z "${c}" ] || f="-f ${c}"
    #[ -z "${c}" ] || c="-crop ${c} +repage"
    BLIN=''
    for a in ${SECT}
    do
        eval "AEXP=\${EXP${a}}"
        FUIN=''
        for e in ${AEXP}
        do
            eval "SFX=\${SFX${e}}"
            n="$(printf '%02d%s.tiff' ${a} ${SFX})"
            eval "$(awk 'BEGIN{i=0}/^i /{if(match($0," n\"?"n"\"?")){print "I="i;if(match($0,/ Eev[^ ]+/))print "E="substr($0,RSTART+4,RLENGTH-4);nextfile}++i}' n=${n} ${DST}.pto)"
            [ -n "${I}" ] || return 1
            [ -n "${E}" ] || E=0
            ${REMAPLDR} -i ${I} -e ${E} ${DST}.pto
            FUIN="$(printf "${FUIN} tmp-%04d.tif" ${I})"
        done
        FUOUT="$(printf 'tmp-%02d.tiff' ${a})"
        BLIN="${BLIN} ${FUOUT}"
        ${EFUSE} -o ${FUOUT} ${FUIN} \
        && rm ${FUIN}
    done
    ${BLEND} ${f} -o tmp.tiff ${BLIN} \
    && rm ${BLIN} \
    && ${CONV} tmp.tiff \
        +matte +repage \
        -unsharp 0x1.00+0.6+0.05 \
        -compress ${IMCOMP} ${DST}.tiff \
    && rm tmp.tiff
    [ -n "${3}" ] \
    || for e in ${EXP}
    do
        eval "SFX=\${SFX${e}}"
        rm [0-9][0-9]${SFX}.tiff
    done
    test -r ${DST}.tiff
}

mkhdr () {
    #pfsinhdrgen ${DST}.hdrgen \
    #| pfshdrcalibrate -v -r linear -g 8 -b 16 -s ${DST}.0.m
    PFSCAL='pfshdrcalibrate -v -x -r linear -g 8'
    [ -r ${DST}.m ] \
    && PFSCAL="pfshdrcalibrate -v -x -c none -f ${DST}.m"
    PFSCNT=2
    [ -r ${DST}.hdrgen ] \
    && PFSCNT=$(wc -l ${DST}.hdrgen|sed -e 's/ *\([0-9]*\) *.*/\1/')
    INIMG=${DST}.tiff
    [ -r ${DST}.hdrgen ] \
    && INIMG=$(awk '{print $1;nextfile}' ${DST}.hdrgen)
    PFSSIZE=$(identify -format "%w*%h*${PFSCNT}" ${INIMG}|bc)
    PFSEVAL=eval
    [ ${PFSSIZE} -le ${PFSMAX} ] || PFSEVAL="rsh ${PFSHOST}"
    PFSIN="pfsintiff --linear ${DST}.tiff"
    [ -r ${DST}.hdrgen ] \
    && PFSIN="pfsinhdrgen ${DST}.hdrgen | ${PFSCAL} -b ${DEPTH}"
    [ -r ${DST}.pfs.bz2 ] \
    && PFSIN="bzcat ${DST}.pfs.bz2"
    PFSCHAIN="cd $(pwd) && ${PFSIN}"

    cat >&2 <<EOM
Image size is ${PFSSIZE} pxel(s)
Max allowed for local operation is ${PFSMAX} pxel(s)
Pipeline
  ${PFSCHAIN}
will be executed using '${PFSEVAL}' command
EOM

    ${PFSEVAL} "${PFSCHAIN}" \
    | pfswb -v \
    | pfstmo_mantiuk08 -v -d pd=crt -c 0.75 -e 1.00 \
    | pfsgamma -m 0.95 -g 1.00 \
    | pfsoutimgmagick ${DST}.0.tiff
    [ ${?} -ne 0 -o -n "${3}" ] \
    || for e in ${EXP}
       do
        eval "SFX=\${SFX${e}}"
        [ -z "${SFX}" ] || rm ${DST}${SFX}.tiff
       done
    ${CONV} ${DST}.tiff \
        -sigmoidal-contrast 4x82% \
        -matte -channel A \
        ${DST}.0.tiff -fx '0.4+0.55*(v.luminance^3)' \
        ${DST}.0.tiff +swap \
        -compose src-over -composite \
        -compress ${IMCOMP} ${DST}.tiff
    rm ${DST}.0.tiff
    #test -r ${DST}.tiff
}

qtvr () {
    mkdir -p ${TMPDIR}/${DST}
    eval "${CONV} ${DST}.tiff ${FINAL} ${*} -compress ${IMCOMP} '${TMPDIR}/${DST}/${DST}.tiff'"
    n=''
    [ -z "${NAME}" ] || n="--name='${NAME}'"
    yaw=''
    [ -z "${YAW}" ] || yaw="--yaw='${YAW}'"
    BASEDIR="$(pwd)" \
    && cd "${TMPDIR}/${DST}" \
    && eval "erect2cubic --erect=${DST}.tiff \
        ${yaw} \
        --ptofile=cubic.pto" \
    && ptsed -Pw,h:2048 -o cubic.pto cubic.pto \
    && ${REMAPLDR} -o face- cubic.pto \
    && for n in 0 1 2 3 4 5
       do
        ${CONV} face-000${n}.tif \
            -sharpen 0x1.16331 \
            -quality 90 -write face-big-${n}.jpg \
            -resize 1024 -sharpen 0x0.76105 \
            -quality 90 -write face-${n}.jpg \
            -resize 512 -sharpen 0x0.51848 \
            -quality 90 -write face-small-${n}.jpg \
            -resize 256 -sharpen 0x0.42410 \
            -quality 80 preview-${n}.jpg \
        && rm face-000${n}.tif
        #${CONV} face-000${n}.tif \
        #    -unsharp 0x0.75+0.5+0.05 \
        #    -quality 90 -write face-${n}.jpg \
        #    -resize 1024 -unsharp 0x0.75+0.5+0.04 \
        #    -quality 90 -write face-small-${n}.jpg \
        #    -resize 512 -unsharp 0x0.65+0.5+0.04 \
        #    -resize 256 -unsharp 0x0.45+0.4+0.03 \
        #    -quality 80 preview-${n}.jpg \
       done \
    && eval "jpeg2qtvr \
        --prefix=face-small- --preview=preview- ${n} \
        --width=400 --height=300 \
        --outfile='${BASEDIR}/${DST}-small.mov'" \
    && eval "jpeg2qtvr \
        --prefix=face- --preview=preview- ${n} \
        --width=768 --height=576 \
        --outfile='${BASEDIR}/${DST}.mov'" \
    && eval "jpeg2qtvr \
        --prefix=face-big- --preview=preview- ${n} \
        --width=1280 --height=960 \
        --outfile='${BASEDIR}/${DST}-big.mov'"
    cd "${BASEDIR}"
    rm -rf ${TMPDIR}/${DST}
    test -r ${DST}.mov
}

globe () {
    true || {
    h=$(identify -format '%h' ${DST}.tiff)
    y=$((${h}/2))
    r=$((${h}/64))
    s=$((${r}/3))
    echo "globe: h=${h}; y=${y}; blur ${r}x${s}"
    ${CONV} ${DST}.tiff \
        \(  -size ${h}x${h} xc:'rgba(44.314%,49.020%,55.294%,1.0)' \
            \(  -colorspace gray -virtual-pixel white \
                -size ${h}x${h} xc:white \
                -fill black -box black -draw "circle ${y},${y} ${y},1" \
                -blur ${r}x${s} +matte \
            \) \
            -compose copyopacity -composite \
        \) \
        -compose src-over -composite \
        -compress ${IMCOMP} ${DST}.tiff
    }
    #${FULLA} -g 0.09:0.03:-0.9:1.75 -o ${DST}.tiff ${DST}.tiff
    #${FULLA} -g 0.09:0.03:-0.9:1.78 -o ${DST}.tiff ${DST}.tiff
    ${FULLA} -g 0.09:0.03:-0.925:1.805 -o ${DST}.tiff ${DST}.tiff
    #${FULLA} -g 0.09:0.03:-0.9:1.60 -o ${DST}.tiff ${DST}.tiff
}

final_fe () {
    SFX=${1}
    shift
    h=$(identify -format '%h' ${DST}.tiff)
    y=$((${h}/2))
    r=$((${h}/64))
    s=$((${r}/3))
    echo "h=${h}; y=${y}; blur ${r}x${s}"
    eval "${CONV} \
        \( ${DST}.tiff \
            ${FINAL} \
        \) \
        \(  -colorspace gray -virtual-pixel black -size ${h}x${h} xc:black \
            -fill white -box white -draw 'circle ${y},${y} ${y},1' \
            -blur ${r}x${s} \
        \) \
        -compose multiply -composite \
        ${@} \
        -quality 90 ${DST}${SFX}.jpeg"
    r=${?}
    return ${r}
}

final () {
    SFX=${1}
    shift
    eval "${CONV} ${DST}.tiff ${FINAL} ${@} -quality 90 ${DST}${SFX}.jpeg"
    r=${?}
    return ${r}
}

[ "${1}" = cluster -o "${1}" = mkldrmf -o "${1}" = mkhdrmf ] \
|| prep "${@}"
[ "${1}" = prep ] && exit
[ "${1}" = auto ] && { auto "${@}"; exit; }
[ "${1}" = cluster ] && { cluster "${@}"; exit; }
[ "${1}" = mkldrmf ] && { mkldrmf "${@}"; exit; }
[ "${1}" = mkhdrmf ] && { mkhdrmf "${@}"; exit; }
#mkpano "${@}" || exit ${?}
#mkpanohdr "${@}" || exit ${?}
#mkpanofuse "${@}" || exit ${?}
{ mkldrmf "${@}" >Makefile && make && { [ -n "${3}" ] || rm Makefile; } } || exit ${?}
#{ mkhdrmf "${@}" >Makefile && make && { [ -n "${3}" ] || rm Makefile; } } || exit ${?}
#if [ "${3}" = a ]
#then
#    inpaint 480x480+4224+5056 1
#    inpaint 64x128+4832+4576 2
#else
#    inpaint 320x64+64+2720 1
#    inpaint 128x64+7840+2688 2
#fi
#[ "${3}" = a ] || {
#    apply_patch2 64x96+1536+1952 64x96+1546+1952 1
#    apply_patch2 96x320+1504+2032 96x320+1568+2032 2
#}
[ "${1}" = pano ] && exit
#mkhdr "${@}" || exit ${?}
#[ "${1}" = hdr ] && exit
#[ -n "${3}" -a "${3}" != 1 ] || qtvr 
#[ "${3}" = b ] && globe
#resize='-resize x3072'
#[ "${3}" = d ] && resize=''
#[ "${3}" = b ] && resize="-crop 3840x3840+256+64 +repage ${resize}"
final=final
#[ "${3}" = a ] && final='final_fe'
echo ${final}
${CONV} ${DST}.tiff    ${resize}     -sharpen 0x0.85000 -compress ${IMCOMP} ${DST}.tiff \
&& ${final} -huge -adaptive-sharpen 1x0.33 -sharpen 0x0.8 \
&& ${CONV} ${DST}.tiff -resize x1536 -sharpen 0x0.64418 -compress ${IMCOMP} ${DST}.tiff \
&& ${final} -big -adaptive-sharpen 1x0.33 -sharpen 0x0.45 \
&& ${CONV} ${DST}.tiff -resize x768  -sharpen 0x0.48820 -compress ${IMCOMP} ${DST}.tiff \
&& ${final} -middle -adaptive-sharpen 1x0.33 -sharpen 0x0.55 \
&& ${CONV} ${DST}.tiff -resize x384  -sharpen 0x0.36998 -compress ${IMCOMP} ${DST}.tiff \
&& ${CONV} ${DST}.tiff -resize x256  -sharpen 0x0.31459 -compress ${IMCOMP} ${DST}.tiff \
&& ${final} -small -adaptive-sharpen 1x0.33 -sharpen 0x0.75 -gamma 1.05 \
&& rm ${DST}.tiff
exit
${CONV} ${DST}.tiff    ${resize}     -compress ${IMCOMP} ${DST}.tiff \
&& ${final} -huge -sharpen 0x1.41420 \
&& ${CONV} ${DST}.tiff -resize x1536 -compress ${IMCOMP} ${DST}.tiff \
&& ${final} -big -sharpen 0x1.09590 \
&& ${CONV} ${DST}.tiff -resize x768  -compress ${IMCOMP} ${DST}.tiff \
&& ${final} -middle -sharpen 0x0.84924 \
&& ${CONV} ${DST}.tiff -resize x384  -sharpen 0x0.65809 -compress ${IMCOMP} ${DST}.tiff \
&& ${CONV} ${DST}.tiff -resize x256  -compress ${IMCOMP} ${DST}.tiff \
&& ${final} -small -sharpen 0x0.50997 -gamma 1.05 \
&& rm ${DST}.tiff
exit
${CONV} ${DST}.tiff    ${resize}     -sharpen 0x1.41420 -compress ${IMCOMP} ${DST}.tiff \
&& ${final} -huge \
&& ${CONV} ${DST}.tiff -resize x1536 -sharpen 0x1.09590 -compress ${IMCOMP} ${DST}.tiff \
&& ${final} -big \
&& ${CONV} ${DST}.tiff -resize x768  -sharpen 0x0.84924 -compress ${IMCOMP} ${DST}.tiff \
&& ${final} -middle \
&& ${CONV} ${DST}.tiff -resize x384  -sharpen 0x0.65809 -compress ${IMCOMP} ${DST}.tiff \
&& ${CONV} ${DST}.tiff -resize x256  -sharpen 0x0.50997 -compress ${IMCOMP} ${DST}.tiff \
&& ${final} -small -gamma 1.05 \
&& rm ${DST}.tiff
exit
${CONV} ${DST}.tiff    ${resize}     -sharpen 0x1.83038 -compress ${IMCOMP} ${DST}.tiff \
&& ${final} -huge \
&& ${CONV} ${DST}.tiff -resize x1536 -sharpen 0x1.16331 -compress ${IMCOMP} ${DST}.tiff \
&& ${final} -big \
&& ${CONV} ${DST}.tiff -resize x768  -sharpen 0x0.76105 -compress ${IMCOMP} ${DST}.tiff \
&& ${final} -middle \
&& ${CONV} ${DST}.tiff -resize x384  -sharpen 0x0.51848 -compress ${IMCOMP} ${DST}.tiff \
&& ${CONV} ${DST}.tiff -resize x256  -sharpen 0x0.42410 -compress ${IMCOMP} ${DST}.tiff \
&& ${final} -small -gamma 1.05 \
&& rm ${DST}.tiff
exit
${CONV} ${DST}.tiff    ${resize}     -sharpen 0x1.37500 -compress ${IMCOMP} ${DST}.tiff \
&& ${final} -huge \
&& ${CONV} ${DST}.tiff -resize x1536 -sharpen 0x1.09130 -compress ${IMCOMP} ${DST}.tiff \
&& ${final} -big \
&& ${CONV} ${DST}.tiff -resize x768  -sharpen 0x0.86620 -compress ${IMCOMP} ${DST}.tiff \
&& ${final} -middle \
&& ${CONV} ${DST}.tiff -resize x384  -sharpen 0x0.68750 -compress ${IMCOMP} ${DST}.tiff \
&& ${CONV} ${DST}.tiff -resize x256  -sharpen 0x0.54567 -compress ${IMCOMP} ${DST}.tiff \
&& ${final} -small -gamma 1.10 \
&& rm ${DST}.tiff
exit
${CONV} ${DST}.tiff    ${resize}     -sharpen 0x1.25000 -compress ${IMCOMP} ${DST}.tiff \
&& ${final} -huge \
&& ${CONV} ${DST}.tiff -resize x1536 -sharpen 0x1.05110 -compress ${IMCOMP} ${DST}.tiff \
&& ${final} -big \
&& ${CONV} ${DST}.tiff -resize x768  -sharpen 0x0.88388 -compress ${IMCOMP} ${DST}.tiff \
&& ${final} -middle \
&& ${CONV} ${DST}.tiff -resize x384  -sharpen 0x0.74325 -compress ${IMCOMP} ${DST}.tiff \
&& ${CONV} ${DST}.tiff -resize x256  -sharpen 0x0.62500 -compress ${IMCOMP} ${DST}.tiff \
&& ${final} -small -gamma 1.10 \
&& rm ${DST}.tiff
exit
${CONV} ${DST}.tiff    ${resize}     -unsharp 0x0.75+0.6+0.05 -compress ${IMCOMP} ${DST}.tiff \
&& ${final} -huge \
&& ${CONV} ${DST}.tiff -resize x1536 -unsharp 0x0.75+0.6+0.04 -compress ${IMCOMP} ${DST}.tiff \
&& ${final} -big \
&& ${CONV} ${DST}.tiff -resize x768  -unsharp 0x0.65+0.5+0.04 -compress ${IMCOMP} ${DST}.tiff \
&& ${final} -middle \
&& ${CONV} ${DST}.tiff -resize x384  -unsharp 0x0.55+0.5+0.04 -compress ${IMCOMP} ${DST}.tiff \
&& ${CONV} ${DST}.tiff -resize x256  -unsharp 0x0.45+0.4+0.03 -compress ${IMCOMP} ${DST}.tiff \
&& ${final} -small -gamma 1.10 \
&& rm ${DST}.tiff
exit
