Projet

Général

Profil

Paste
Télécharger (2,53 ko) Statistiques
| Branche: | Révision:

root / to_tiles / gen_tiles.sh @ 8b0958ea

1
#!/bin/bash
2

    
3
test_mode=false
4
memory_limit=256
5
crop_x=256
6
crop_y=256
7
min_scale=0
8
max_scale=8
9
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"
10

    
11
if ! which anytopnm pnmscale convert > /dev/null; then
12
    echo "il faut installer les paquets netpbm et imageMagick pour utiliser ce script !"
13
fi
14

    
15
while getopts m:M:x:y:p:ht prs
16
 do
17
 case $prs in
18
    t)        test_mode=true;;
19
    x)        crop_x=$OPTARG;;
20
    y)        crop_y=$OPTARG;;
21
    m)        min_scale=$OPTARG;;
22
    M)        max_scale=$OPTARG;;
23
    p)        prefix=$OPTARG;;
24
    \? | h)   echo -e $usage
25
              exit 2;;
26
 esac
27
done
28
shift `expr $OPTIND - 1`
29

    
30
if [ -z "$1" ]; then
31
    echo -e "usage :\n$usage"
32
    exit 1
33
elif [ ! -f "$1" ]; then
34
    echo -e "le paramètre $1 ne correspond pas à un nom de fichier !"
35
    exit 1
36
fi
37

    
38
fname=$1
39
dir=$(dirname $fname)
40

    
41
if [ -z "$prefix" ]; then
42
    prefix=$(basename $1|sed 's/\..*$//')
43
fi
44

    
45
wfname=$prefix.pnm
46
if ! $test_mode; then
47
    anytopnm $fname > $wfname
48
else
49
    echo "anytopnm $fname > $wfname"
50
fi
51

    
52
echo "préfixe : "$prefix
53

    
54
for ((z=$min_scale; z <= $max_scale; z++))
55
do
56
    fprefix=${prefix}_00$z
57
    printf -v ratio %1.4lf $(echo "1 / (2^$z)" | bc -l)
58
    echo génération du ratio $ratio
59
    zwfname=tmp.pnm
60

    
61
    if $test_mode; then
62
	if [ $ratio = 1.0000 ]; then
63
	    zwfname=$wfname
64
	else
65
	    echo "pnmscale $ratio $wfname > $zwfname"
66
	fi
67
	echo convert $zwfname \
68
	    -limit memory $memory_limit \
69
            -crop ${crop_x}x${crop_x} \
70
            -set filename:tile "%[fx:page.x/${crop_x}]_%[fx:page.y/${crop_y}]" \
71
            +repage +adjoin "${fprefix}_%[filename:tile].jpg"
72
    else
73
	if [ $ratio = 1.0000 ]; then
74
	    zwfname=$wfname
75
	else
76
	    pnmscale $ratio $wfname > $zwfname
77
	fi
78
	convert $zwfname \
79
	    -limit memory $memory_limit \
80
            -crop ${crop_x}x${crop_x} \
81
            -set filename:tile "%[fx:page.x/${crop_x}]_%[fx:page.y/${crop_y}]" \
82
            +repage +adjoin "${fprefix}_%[filename:tile].jpg"
83
    fi
84
done
85

    
86
echo  ${fprefix}_*
87

    
88
if ! $test_mode; then
89
## les lignes ci dessous sont destinnées à mettre des 0 en debut des numéros de ligne et de colonnes
90
## Il y a certainement plus simple mais là c'est du rapide et efficace.
91
    rename 's/_(\d\d)_(\d+\.jpg)$/_0$1_$2/' ${prefix}_*
92
    rename 's/_(\d)_(\d+\.jpg)$/_00$1_$2/' ${prefix}_*
93
    rename 's/_(\d+)_(\d\d)(\.jpg)$/_$1_0$2$3/' ${prefix}_*
94
    rename 's/_(\d+)_(\d)(\.jpg)$/_$1_00$2$3/' ${prefix}_*
95
    rm $zwfname $wfname
96
else
97
    echo rm $zwfname $wfname
98
fi