-
Notifications
You must be signed in to change notification settings - Fork 1
/
drawWater.sh
executable file
·98 lines (85 loc) · 2.52 KB
/
drawWater.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
#!/usr/bin/env bash
set -euo pipefail
IFS=$'\n\t'
if [[ $# -ne 3 ]]; then
echo >&2 "$0 <water_info.xml> <image> <size>"
exit 1
fi
XML="$1"
IMG="$2"
SIZE="$3"
DIM="${SIZE}x${SIZE}"
CENTER="$((SIZE / 2))"
WATER_IMG="water-${IMG}"
MASK_IMG="mask-${IMG}"
TMPIMG="tmp-${IMG}"
water() {
# no argument expansion is intended with $c
# shellcheck disable=SC2016
xmlstarlet sel -t -m "//Water" --sort a:n:- "str:tokenize(@pos, ',')[2]" \
--var "c=str:tokenize(@pos, ',')" \
-v '$c[2]' -o ';' \
-v '$c[1]' -o , -v '$c[3]' -o ';' \
-v @minx -o , -v @maxx -o , -v @minz -o , -v @maxz \
-n "${XML}" | tr -d ' '
}
water > water_info.txt
mapfile -t DEPTHS < <(cut -d ';' -f 1 water_info.txt | sort -u)
convert -size "${DIM}" xc:black -alpha on -transparent black "${MASK_IMG}"
for depth in "${DEPTHS[@]}"; do
echo "Depth $depth"
file="water${depth}.txt"
threshold=$((depth * 256 + 128))
grep "^${depth};" water_info.txt > water_depth.txt
mapfile -t MASKS < <(cut -d ';' -f 3 water_depth.txt | sort -u)
echo "push defs" > "${file}"
for mask in "${MASKS[@]}"; do
echo "Clip path mask $mask"
IFS=',' read -r x1 x2 z1 z2 <<<"$mask"
xmin=$((CENTER + x1))
xmax=$((CENTER + x2 - 1))
zmin=$((CENTER - z2))
zmax=$((CENTER - z1 - 1))
#clip_path="${xmin}_${zmin}_${xmax}_${zmax}"
clip_path="$mask"
cat >> "${file}" <<-CLIPMASK
push clip-path "${clip_path}"
push graphic-context
rectangle ${xmin},${zmin} ${xmax},${zmax}
pop graphic-context
pop clip-path
CLIPMASK
done
# shellcheck disable=SC2129
echo "pop defs" >> "${file}"
echo "fill white" >> "${file}"
echo "border-color white" >> "${file}"
for mask in "${MASKS[@]}"; do
echo "Filling Mask $mask"
clip_path="$mask"
mapfile -t POINTS < <(grep ";${mask}$" water_depth.txt | cut -d ';' -f 2)
echo "push graphic-context" >> "${file}"
echo "clip-path url(#${clip_path})" >> "${file}"
for point in "${POINTS[@]}"; do
IFS=',' read -r x z <<<"$point"
xabs=$((CENTER + x))
zabs=$((CENTER - z))
echo "color ${xabs},${zabs} filltoborder" >> "${file}"
done
echo "pop graphic-context" >> "${file}"
done
time convert -size "${DIM}" -depth 16 gray:dtm.raw -flip \
-threshold "$threshold" \
-depth 8 \
-write mpr:mask \
-monitor -draw "@${file}" \
mpr:mask -compose minus_src -composite \
-alpha on -transparent black \
"${MASK_IMG}" -compose src-over -composite \
"${TMPIMG}"
mv -f "${TMPIMG}" "${MASK_IMG}"
done
convert "${IMG}" \
\( "${MASK_IMG}" -fill '#738cce' -opaque white \) \
-composite "${WATER_IMG}"
echo "${WATER_IMG}"