1
|
#!/bin/bash
|
2
|
|
3
|
trap clean_tmp EXIT
|
4
|
# fin d'eviter tout problème de locales en reste en C de base.
|
5
|
|
6
|
set -e
|
7
|
export LANG=C
|
8
|
|
9
|
# pour éliminer systématiquement les fichier temporaires créés ic
|
10
|
function clean_tmp() {
|
11
|
if [ -n "$wfname" ]; then
|
12
|
rm $wfname
|
13
|
fi
|
14
|
if [ -n "$tmp_file" ]; then
|
15
|
rm $tmp_file
|
16
|
fi
|
17
|
}
|
18
|
|
19
|
test_mode=false
|
20
|
memory_limit=256
|
21
|
crop_x=256
|
22
|
crop_y=256
|
23
|
min_scale=0
|
24
|
max_scale=8
|
25
|
usage="$0 [-x <x_tile_size>] [-y <y_tile_size>] [-p <prefix_result>] [-t] [-h] [-m <min_zoom>] [-M <max_zoom>] <image_to_convert>\n example: $0 -r test_res"
|
26
|
|
27
|
if ! which anytopnm pnmscale convert > /dev/null; then
|
28
|
echo "il faut installer les paquets netpbm et imageMagick pour utiliser ce script !"
|
29
|
fi
|
30
|
|
31
|
while getopts m:M:x:y:p:ht prs
|
32
|
do
|
33
|
case $prs in
|
34
|
t) test_mode=true;;
|
35
|
x) crop_x=$OPTARG;;
|
36
|
y) crop_y=$OPTARG;;
|
37
|
m) min_scale=$OPTARG;;
|
38
|
M) max_scale=$OPTARG;;
|
39
|
p) prefix=$OPTARG;;
|
40
|
\? | h) echo -e $usage
|
41
|
exit 2;;
|
42
|
esac
|
43
|
done
|
44
|
shift `expr $OPTIND - 1`
|
45
|
|
46
|
if [ -z "$1" ]; then
|
47
|
echo -e "usage :\n$usage"
|
48
|
exit 1
|
49
|
elif [ ! -f "$1" ]; then
|
50
|
echo -e "le paramètre $1 ne correspond pas à un nom de fichier !"
|
51
|
exit 1
|
52
|
fi
|
53
|
|
54
|
fname=$1
|
55
|
dir=$(dirname $fname)
|
56
|
|
57
|
if [ -z "$prefix" ]; then
|
58
|
prefix=$(basename $1|sed 's/\..*$//')
|
59
|
fi
|
60
|
|
61
|
wfname=$(mktemp ${prefix}_XXXX.pnm)
|
62
|
if ! $test_mode; then
|
63
|
anytopnm $fname > $wfname
|
64
|
else
|
65
|
echo "anytopnm $fname > $wfname"
|
66
|
fi
|
67
|
|
68
|
echo "préfixe : "$prefix
|
69
|
|
70
|
tmp_file=$(mktemp)
|
71
|
|
72
|
for ((z=$min_scale; z <= $max_scale; z++))
|
73
|
do
|
74
|
fprefix=${prefix}_00$z
|
75
|
printf -v ratio %1.4lf $(echo "1 / (2^$z)" | bc -l)
|
76
|
echo génération du ratio $ratio
|
77
|
zwfname=$tmp_file
|
78
|
|
79
|
if $test_mode; then
|
80
|
if [ $ratio = 1.0000 ]; then
|
81
|
zwfname=$wfname
|
82
|
else
|
83
|
echo "pnmscale $ratio $wfname > $zwfname"
|
84
|
fi
|
85
|
echo convert $zwfname \
|
86
|
-limit memory $memory_limit \
|
87
|
-crop ${crop_x}x${crop_x} \
|
88
|
-set filename:tile "%[fx:page.x/${crop_x}]_%[fx:page.y/${crop_y}]" \
|
89
|
+repage +adjoin "${fprefix}_%[filename:tile].jpg"
|
90
|
else
|
91
|
if [ $ratio = 1.0000 ]; then
|
92
|
zwfname=$wfname
|
93
|
else
|
94
|
if ! pnmscale $ratio $wfname > $zwfname; then
|
95
|
echo "operation 'pnmscale $ratio $wfname > $zwfname' en erreur"
|
96
|
exit 1
|
97
|
fi
|
98
|
fi
|
99
|
if convert $zwfname \
|
100
|
-limit memory $memory_limit \
|
101
|
-crop ${crop_x}x${crop_x} \
|
102
|
-set filename:tile "%[fx:page.x/${crop_x}]_%[fx:page.y/${crop_y}]" \
|
103
|
+repage +adjoin "${fprefix}_%[filename:tile].jpg"; then
|
104
|
echo "Nombre des fichiers produits :" $(ls -la ${fprefix}_*| wc -l)
|
105
|
else
|
106
|
echo "operation 'convert' en erreur"
|
107
|
exit 2
|
108
|
fi
|
109
|
fi
|
110
|
done
|
111
|
|
112
|
if ! $test_mode; then
|
113
|
## les lignes ci dessous sont destinnées à mettre des 0 en debut des numéros de ligne et de colonnes
|
114
|
## Il y a certainement plus simple mais là c'est du rapide et efficace.
|
115
|
rename 's/_(\d\d)_(\d+\.jpg)$/_0$1_$2/' ${prefix}_*
|
116
|
rename 's/_(\d)_(\d+\.jpg)$/_00$1_$2/' ${prefix}_*
|
117
|
rename 's/_(\d+)_(\d\d)(\.jpg)$/_$1_0$2$3/' ${prefix}_*
|
118
|
rename 's/_(\d+)_(\d)(\.jpg)$/_$1_00$2$3/' ${prefix}_*
|
119
|
fi
|