forked from est31/leaftest
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmapper.sh
executable file
·148 lines (125 loc) · 4 KB
/
mapper.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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
#!/usr/bin/env bash
MAPDIR=$1
SPAWNPOS=$2
DIMENSIONS=$3
DIMENSIONS=${DIMENSIONS=6144}
MAPPERDIR=${MAPPERDIR=.}
mapperpath=${mapperpath=$MAPPERDIR/minetestmapper}
if [ ! -f $mapperpath ]; then
echo "Error, "$mapperpath" doesn't exist."
echo "Please specify the path to the minetestmapper executable via the MAPPERDIR variable"
exit 1
fi
if [ ! -d "$MAPDIR" ]; then
echo "Error, the directory \"$MAPDIR\" doesn't exist."
echo "Please specify the path to the minetest world"
exit 1
fi
if [ -f downscaling/perc ]; then
echo "Using SSIM based downscaling"
downscale() {
downscaling/perc 2 < $1 > ${1}_small
mv -f ${1}_small $1
}
else
echo "Using downscaling provided by imagemagick"
downscale() {
convert $1 -resize 50% $1
}
fi
JOBNUM=${JOBNUM=1}
case "$JOBNUM" in
''|*[!0-9]*) echo "Bad number of jobs '$JOBNUM'. Please specify a positive integer." ; exit 1 ;;
*) ;;
esac
# echo "Number of jobs: $JOBNUM"
prefix_func="\
prefix() { \
while read line ; do \
echo \"\${1}\${line}\"; \
done \
}"
# retains || usage when doing | prefix "sth"
# use it when needed
prefix_pipefail="\
set -o pipefail ; \
$prefix_func"
# prefix should be usable within this script as well
eval $prefix_func
#sh -c "$prefix_func ; echo -e 'a\nb\nc\n' | prefix 'PREF '"
#bash -c "$prefix_pipefail ; (echo -e 'a\nb\nc\n' ; false) | prefix 'PREF '" || echo "ERR"
#exit 1
if [ -z "$scriptdir" ]; then
scriptdir=`readlink -f $0`
scriptdir=`dirname $scriptdir`
fi
tiledir=${tiledir=$scriptdir/www/tiles}
spawnx=${SPAWNPOS%%,*}
spawnx=${spawnx=0}
spawny=${SPAWNPOS##*,}
spawny=${spawny=0}
#echo "spawn position: $spawnx $spawny"
tilesize=${tilesize=256}
zoomlevelnum=${zoomlevelnum=3}
tilenum=$(($DIMENSIONS/$tilesize))
mkdir -p ${tiledir}/20
rm -f ${tiledir}/20/*
jobparam="-P$JOBNUM"
echo "Creating tile images with minetestmapper…"
for x in $(seq 0 $tilenum)
do
for y in $(seq 0 $tilenum)
do
posx=$(($spawnx+$tilesize*($x-$tilenum/2)))
posy=$(($spawny+$tilesize*($tilenum/2-$y)))
# Execute sh -c "something"
printf "%s\x00" "-c"
printf "$prefix_pipefail ; $mapperpath ${MAPPERPARAMS} -i ${MAPDIR} --geometry ${posx},${posy}+${tilesize}+${tilesize} -o ${tiledir}/20/map_${x}_${y}.png \
2> >(>&2 prefix '[TILEGEN 20/map_${x}_${y}.png ERR]: ') | prefix '[TILEGEN 20/map_${x}_${y}.png]: ' \
|| (>&2 echo 'minetesmapper for tile [${x},${y}] ended with non zero exit code'; exit 255)\x00"
done
done | xargs -n2 $jobparam -0 bash # bash required because of "set -o pipefail" usage
xargs_exit=$?
if [ $xargs_exit -ne 0 ]; then
# xargs unfortunately exits immediately, without even waiting for sub-processes
sleep 1
>&2 echo "Error: xargs ended with non-zero exit code $xargs_exit."
exit 1
fi
# images are joined together and downscaled
echo "Generating lower resolution tile images…"
mult=1
for s in $(seq 1 $zoomlevelnum)
do
echo "$s/$zoomlevelnum …"
mult=$(($mult*2))
tnum=$(($tilenum/$mult-1))
zoomlevel=$((20-$s))
zoomlevelbefore=$((20-$s+1))
zlv=$zoomlevel # shorter name
dir=$tiledir/$zoomlevel
dirb=$tiledir/$zoomlevelbefore
mkdir -p $dir
rm -f ${dir}/*
for x in $(seq 0 $tnum)
do
for y in $(seq 0 $tnum)
do
montage $dirb/map_$(($x*2))_$((y*2)).png $dirb/map_$(($x*2+1))_$((y*2)).png $dirb/map_$(($x*2))_$((y*2+1)).png $dirb/map_$(($x*2+1))_$((y*2+1)).png -geometry +0+0 $dir/map_${x}_${y}.png \
2> >(>&2 prefix "[MONTAGE $zlv/map_${x}_${y}.png ERR]: ") | prefix "[MONTAGE $zlv/map_${x}_${y}.png]: "
if [ ${PIPESTATUS[0]} -ne 0 ]; then
echo "montage exited with non zero exit code, aborting."
exit 1
fi
downscale $dir/map_${x}_${y}.png \
2> >(>&2 prefix "[SHRINK $zlv/map_${x}_${y}.png ERR]: ") | prefix "[SHRINK $zlv/map_${x}_${y}.png]: "
if [ ${PIPESTATUS[0]} -ne 0 ]; then
echo "shrinking exited with non zero exit code, aborting."
exit 1
fi
done
done
done
zoommin=$((20-$zoomlevelnum))
#write the resulting config into a json file
echo "{\"mapsize\":$DIMENSIONS, \"spawn\":{\"x\":$spawnx,\"y\":$spawny}, \"zoommin\":$zoommin, \"tilesize\":$tilesize}" > ${conffile=$tiledir/conf.json}