#!/bin/sh

WRKDIR="$(cd "$(dirname "${0}")";pwd)"
WRKNAME="$(basename "${WRKDIR}")"
TMPDIR="/chroot/chroot/yury/tmp/photo/${WRKNAME}"
export TMPDIR WRKDIR WRKNAME

mkdir -p "${TMPDIR}"
cd "${TMPDIR}"

convert-in () {
    cd "${TMPDIR}" || return 1
    convert "${WRKDIR}/STA_0535.JPG" -filter box -support 1.01 -resize 125% -adaptive-sharpen 1x1 -filter box -support 0.99 -resize 80% -compress LZW 00.tif &
    convert "${WRKDIR}/STB_0536.JPG" -filter box -support 1.01 -resize 125% -adaptive-sharpen 1x1 -filter box -support 0.99 -resize 80% -compress LZW 01.tif &
    convert "${WRKDIR}/STC_0537.JPG" -filter box -support 1.01 -resize 125% -adaptive-sharpen 1x1 -filter box -support 0.99 -resize 80% -compress LZW 02.tif &
    convert "${WRKDIR}/STD_0538.JPG" -filter box -support 1.01 -resize 125% -adaptive-sharpen 1x1 -filter box -support 0.99 -resize 80% -compress LZW 03.tif &
    convert "${WRKDIR}/STE_0539.JPG" -filter box -support 1.01 -resize 125% -adaptive-sharpen 1x1 -filter box -support 0.99 -resize 80% -compress LZW 04.tif &
    wait
    convert "${WRKDIR}/STF_0540.JPG" -filter box -support 1.01 -resize 125% -adaptive-sharpen 1x1 -filter box -support 0.99 -resize 80% -compress LZW 05.tif &
    convert "${WRKDIR}/STG_0541.JPG" -filter box -support 1.01 -resize 125% -adaptive-sharpen 1x1 -filter box -support 0.99 -resize 80% -compress LZW 06.tif &
    convert "${WRKDIR}/STH_0542.JPG" -filter box -support 1.01 -resize 125% -adaptive-sharpen 1x1 -filter box -support 0.99 -resize 80% -compress LZW 07.tif &
    convert "${WRKDIR}/STI_0543.JPG" -filter box -support 1.01 -resize 125% -adaptive-sharpen 1x1 -filter box -support 0.99 -resize 80% -compress LZW 08.tif &
    convert "${WRKDIR}/STJ_0544.JPG" -filter box -support 1.01 -resize 125% -adaptive-sharpen 1x1 -filter box -support 0.99 -resize 80% -compress LZW 09.tif &
    wait
    convert "${WRKDIR}/STK_0545.JPG" -filter box -support 1.01 -resize 125% -adaptive-sharpen 1x1 -filter box -support 0.99 -resize 80% -compress LZW 10.tif &
    convert "${WRKDIR}/STL_0546.JPG" -filter box -support 1.01 -resize 125% -adaptive-sharpen 1x1 -filter box -support 0.99 -resize 80% -compress LZW 11.tif &
    convert "${WRKDIR}/STM_0547.JPG" -filter box -support 1.01 -resize 125% -adaptive-sharpen 1x1 -filter box -support 0.99 -resize 80% -compress LZW 12.tif &
    convert "${WRKDIR}/STN_0548.JPG" -filter box -support 1.01 -resize 125% -adaptive-sharpen 1x1 -filter box -support 0.99 -resize 80% -compress LZW 13.tif &
    convert "${WRKDIR}/STO_0549.JPG" -filter box -support 1.01 -resize 125% -adaptive-sharpen 1x1 -filter box -support 0.99 -resize 80% -compress LZW 14.tif &
    wait
    convert "${WRKDIR}/STP_0550.JPG" -filter box -support 1.01 -resize 125% -adaptive-sharpen 1x1 -filter box -support 0.99 -resize 80% -compress LZW 15.tif &
    convert "${WRKDIR}/STQ_0551.JPG" -filter box -support 1.01 -resize 125% -adaptive-sharpen 1x1 -filter box -support 0.99 -resize 80% -compress LZW 16.tif &
    convert "${WRKDIR}/STR_0552.JPG" -filter box -support 1.01 -resize 125% -adaptive-sharpen 1x1 -filter box -support 0.99 -resize 80% -compress LZW 17.tif &
    convert "${WRKDIR}/STS_0553.JPG" -filter box -support 1.01 -resize 125% -adaptive-sharpen 1x1 -filter box -support 0.99 -resize 80% -compress LZW 18.tif &
    wait
}

remap () {
    cd "${TMPDIR}" || return 1
    sed \
        -e 's/STA_0535.JPG/00.tif/g' \
        -e 's/STB_0536.JPG/01.tif/g' \
        -e 's/STC_0537.JPG/02.tif/g' \
        -e 's/STD_0538.JPG/03.tif/g' \
        -e 's/STE_0539.JPG/04.tif/g' \
        -e 's/STF_0540.JPG/05.tif/g' \
        -e 's/STG_0541.JPG/06.tif/g' \
        -e 's/STH_0542.JPG/07.tif/g' \
        -e 's/STI_0543.JPG/08.tif/g' \
        -e 's/STJ_0544.JPG/09.tif/g' \
        -e 's/STK_0545.JPG/10.tif/g' \
        -e 's/STL_0546.JPG/11.tif/g' \
        -e 's/STM_0547.JPG/12.tif/g' \
        -e 's/STN_0548.JPG/13.tif/g' \
        -e 's/STO_0549.JPG/14.tif/g' \
        -e 's/STP_0550.JPG/15.tif/g' \
        -e 's/STQ_0551.JPG/16.tif/g' \
        -e 's/STR_0552.JPG/17.tif/g' \
        -e 's/STS_0553.JPG/18.tif/g' \
        -e 's/^p\(.*\) n"[^"]* c:[^"]*"\(.*\)$/p\1 n"TIFF_m c:LZW"\2/g' \
        -e 's/^m\(.*\) i[[:digit:]]\(.*\)$/m\1 i4\2/g' \
        "${WRKDIR}/${WRKNAME}.pto" \
    >tiff-m.pto
    nona -t 2 -o r.tif tiff-m.pto
}

contrast-down () {
    cd "${TMPDIR}" || return 1
    convert r0018.tif +contrast r13.tif &
    convert r0017.tif +contrast r13.tif &
    convert r0016.tif +contrast r13.tif &
    convert r0015.tif +contrast r13.tif &
    convert r0014.tif +contrast r13.tif &
    wait
    convert r0013.tif +contrast r13.tif &
    convert r0012.tif +contrast r12.tif &
    convert r0011.tif +contrast r11.tif &
    convert r0010.tif +contrast r10.tif &
    convert r0009.tif +contrast r09.tif &
    wait
    convert r0008.tif +contrast r08.tif &
    convert r0007.tif +contrast r07.tif &
    convert r0006.tif +contrast r06.tif &
    convert r0005.tif +contrast r05.tif &
    convert r0004.tif +contrast r04.tif &
    wait
    convert r0003.tif +contrast r03.tif &
    convert r0002.tif +contrast r02.tif &
    convert r0001.tif +contrast r01.tif &
    convert r0000.tif +contrast r00.tif &
    wait
}

contrast-up () {
    cd "${TMPDIR}" || return 1
    convert r18.tif -contrast r13.tif &
    convert r17.tif -contrast r12.tif &
    convert r16.tif -contrast r11.tif &
    convert r15.tif -contrast r10.tif &
    convert r04.tif -contrast r09.tif &
    wait
    convert r13.tif -contrast r13.tif &
    convert r12.tif -contrast r12.tif &
    convert r11.tif -contrast r11.tif &
    convert r10.tif -contrast r10.tif &
    convert r09.tif -contrast r09.tif &
    wait
    convert r08.tif -contrast r08.tif &
    convert r07.tif -contrast r07.tif &
    convert r06.tif -contrast r06.tif &
    convert r05.tif -contrast r05.tif &
    convert r04.tif -contrast r04.tif &
    wait
    convert r03.tif -contrast r03.tif &
    convert r02.tif -contrast r02.tif &
    convert r01.tif -contrast r01.tif &
    convert r00.tif -contrast r00.tif &
    wait
}

colors-1 () {
    cd "${TMPDIR}" || return 1
    # center
    cp r0009.tif r09.tif
    # left
    color_correct ${@} -o r08.tif r09.tif r0008.tif
    color_correct ${@} -o r07.tif r08.tif r0007.tif
    color_correct ${@} -o r06.tif r07.tif r0006.tif
    color_correct ${@} -o r05.tif r06.tif r0005.tif
    color_correct ${@} -o r04.tif r05.tif r0004.tif
    color_correct ${@} -o r03.tif r04.tif r0003.tif
    color_correct ${@} -o r02.tif r03.tif r0002.tif
    color_correct ${@} -o r01.tif r02.tif r0001.tif
    color_correct ${@} -o r00.tif r01.tif r0000.tif
    # right
    color_correct ${@} -o r10.tif r09.tif r0010.tif
    color_correct ${@} -o r11.tif r10.tif r0011.tif
    color_correct ${@} -o r12.tif r11.tif r0012.tif
    color_correct ${@} -o r13.tif r12.tif r0013.tif
    color_correct ${@} -o r14.tif r13.tif r0014.tif
    color_correct ${@} -o r15.tif r14.tif r0015.tif
    color_correct ${@} -o r16.tif r15.tif r0016.tif
    color_correct ${@} -o r17.tif r16.tif r0017.tif
    color_correct ${@} -o r18.tif r17.tif r0018.tif
}

colors-2 () {
    cd "${TMPDIR}" || return 1
    # left
    color_correct ${@} -o r08.tif r09.tif r08.tif
    color_correct ${@} -o r07.tif r08.tif r07.tif
    color_correct ${@} -o r06.tif r07.tif r06.tif
    color_correct ${@} -o r05.tif r06.tif r05.tif
    color_correct ${@} -o r04.tif r05.tif r04.tif
    color_correct ${@} -o r03.tif r04.tif r03.tif
    color_correct ${@} -o r02.tif r03.tif r02.tif
    color_correct ${@} -o r01.tif r02.tif r01.tif
    color_correct ${@} -o r00.tif r01.tif r00.tif
    # right
    color_correct ${@} -o r10.tif r09.tif r10.tif
    color_correct ${@} -o r11.tif r10.tif r11.tif
    color_correct ${@} -o r12.tif r11.tif r12.tif
    color_correct ${@} -o r13.tif r12.tif r13.tif
    color_correct ${@} -o r14.tif r13.tif r14.tif
    color_correct ${@} -o r15.tif r14.tif r15.tif
    color_correct ${@} -o r16.tif r15.tif r16.tif
    color_correct ${@} -o r17.tif r16.tif r17.tif
    color_correct ${@} -o r18.tif r17.tif r18.tif
}

colors () {
    cd "${TMPDIR}" || return 1
    # center
    cp r0009.tif r09.tif
    # left
    color_correct -l -g ${@}	-o r08.tif r09.tif r0008.tif
    color_correct -c ${@}		-o r08.tif r09.tif r08.tif
    color_correct -l -g ${@}	-o r07.tif r08.tif r0007.tif
    color_correct -c ${@}		-o r07.tif r08.tif r07.tif
    color_correct -l -g ${@}	-o r06.tif r07.tif r0006.tif
    color_correct -c ${@}		-o r06.tif r07.tif r06.tif
    color_correct -l -g ${@}	-o r05.tif r06.tif r0005.tif
    color_correct -c ${@}		-o r05.tif r06.tif r05.tif
    color_correct -l -g ${@}	-o r04.tif r05.tif r0004.tif
    color_correct -c ${@}		-o r04.tif r05.tif r04.tif
    color_correct -l -g ${@}	-o r03.tif r04.tif r0003.tif
    color_correct -c ${@}		-o r03.tif r04.tif r03.tif
    color_correct -l -g ${@}	-o r02.tif r03.tif r0002.tif
    color_correct -c ${@}		-o r02.tif r03.tif r02.tif
    color_correct -l -g ${@}	-o r01.tif r02.tif r0001.tif
    color_correct -c ${@}		-o r01.tif r02.tif r01.tif
    color_correct -l -g ${@}	-o r00.tif r01.tif r0000.tif
    color_correct -c ${@}		-o r00.tif r01.tif r00.tif
    # right
    color_correct -l -g ${@}	-o r10.tif r09.tif r0010.tif
    color_correct -c ${@}		-o r10.tif r09.tif r10.tif
    color_correct -l -g ${@}	-o r11.tif r10.tif r0011.tif
    color_correct -c ${@}		-o r11.tif r10.tif r11.tif
    color_correct -l -g ${@}	-o r12.tif r11.tif r0012.tif
    color_correct -c ${@}		-o r12.tif r11.tif r12.tif
    color_correct -l -g ${@}	-o r13.tif r12.tif r0013.tif
    color_correct -c ${@}		-o r13.tif r12.tif r13.tif
    color_correct -l -g ${@}	-o r14.tif r13.tif r0014.tif
    color_correct -c ${@}		-o r14.tif r13.tif r14.tif
    color_correct -l -g ${@}	-o r15.tif r14.tif r0015.tif
    color_correct -c ${@}		-o r15.tif r14.tif r15.tif
    color_correct -l -g ${@}	-o r16.tif r15.tif r0016.tif
    color_correct -c ${@}		-o r16.tif r15.tif r16.tif
    color_correct -l -g ${@}	-o r17.tif r16.tif r0017.tif
    color_correct -c ${@}		-o r17.tif r16.tif r17.tif
    color_correct -l -g ${@}	-o r18.tif r17.tif r0018.tif
    color_correct -c ${@}		-o r18.tif r17.tif r18.tif
}

stitch () {
    cd "${TMPDIR}" || return 1
    enblend \
    	-m 512 \
        -z \
        -o r.tif \
        r00.tif \
        r01.tif \
        r02.tif \
        r03.tif \
        r04.tif \
        r05.tif \
        r06.tif \
        r07.tif \
        r08.tif \
        r09.tif \
        r10.tif \
        r11.tif \
        r12.tif \
        r13.tif \
        r14.tif \
        r15.tif \
        r16.tif \
        r17.tif \
        r18.tif
}

convert-out () {
    cd "${TMPDIR}" || return 1
	convert \
		r.tif \
		-crop 19744x1920+64+578 \
		+repage \
		-modulate 100,110 \
		-compress LZW \
		"${WRKDIR}/${WRKNAME}.tiff"

	#convert \
	#	"${WRKDIR}/${WRKNAME}.tiff" \
	#	-crop 12992x1536+512+0 \
	#	+repage \
	#	-quality 90 \
	#	"${WRKDIR}/${WRKNAME}.1-big.jpeg" \
	#&
	#convert \
	#	"${WRKDIR}/${WRKNAME}.tiff" \
	#	-crop 12992x1536+512+0 \
	#	+repage \
	#	-filter box \
	#	-support 0.9 \
	#	-resize x768 \
	#	-quality 90 \
	#	"${WRKDIR}/${WRKNAME}.1.jpeg" \
	#&
	#convert \
	#	"${WRKDIR}/${WRKNAME}.tiff" \
	#	-crop 12992x1536+512+0 \
	#	+repage \
	#	-filter box \
	#	-support 0.9 \
	#	-resize 1024 \
	#	-quality 90 \
	#	"${WRKDIR}/${WRKNAME}.1-small.jpeg" \
	#&

	convert \
		"${WRKDIR}/${WRKNAME}.tiff" \
		-quality 90 \
		"${WRKDIR}/${WRKNAME}-big.jpeg" \
	&
	convert \
		"${WRKDIR}/${WRKNAME}.tiff" \
		-filter box \
		-support 0.9 \
		-resize x768 \
		-quality 90 \
		"${WRKDIR}/${WRKNAME}.jpeg" \
	&
	convert \
		"${WRKDIR}/${WRKNAME}.tiff" \
		-filter box \
		-support 0.9 \
		-resize 1024 \
		-quality 90 \
		"${WRKDIR}/${WRKNAME}-small.jpeg" \
	&
	wait
}

convert-in
remap
#contrast-down
#colors-2 -l -p.05
#colors-2 -c -p.05
#contrast-up
colors-1 -c -p.03
stitch
convert-out

cd "${WRKDIR}"
rm -rf "${TMPDIR}"
