Merge branch 'master' into divVerent/crypto2
[xonotic/xonotic.git] / misc / tools / cached-converter.sh
1 #!/bin/sh
2
3 set -e
4
5 : ${CACHEDIR:=$HOME/.xonotic-cached-converter}
6 : ${do_jpeg:=true}
7 : ${do_jpeg_if_not_dds:=false}
8 : ${jpeg_qual_rgb:=95}
9 : ${jpeg_qual_a:=99}
10 : ${do_dds:=true}
11 : ${dds_tool:=compressonator-dxtc}
12 : ${do_ogg:=false}
13 : ${ogg_qual:=1}
14 : ${del_src:=false}
15
16 me=$0
17 case "$me" in
18         */*)
19                 meprefix=${me%/*}/
20                 ;;
21         *)
22                 meprefix=
23                 ;;
24 esac
25
26 tmpdir=`mktemp -d -t cached-converter.XXXXXX`
27 trap 'exit 1' INT
28 trap 'rm -rf "$tmpdir"' EXIT
29
30 lastinfiles=
31 lastinfileshash=
32 cached()
33 {
34         flag=$1; shift
35         method=$1; shift
36         infile1=$1; shift
37         infile2=$1; shift
38         outfile1=$1; shift
39         outfile2=$1; shift
40         if ! $flag; then
41                 return 0
42         fi
43         if [ x"$infile1" = x"$outfile1" ]; then
44                 keep=true
45         fi
46         options=`echo "$*" | git hash-object --stdin`
47         if [ x"$infile1/../$infile2" = x"$lastinfiles" ]; then
48                 sum=$lastinfileshash
49         else
50                 sum=`git hash-object "$infile1"`
51                 if [ -n "$infile2" ]; then
52                         sum=$sum`git hash-object "$infile2"`
53                 fi
54                 lastinfileshash=$sum
55         fi
56         mkdir -p "$CACHEDIR/$method-$options"
57         name1="$CACHEDIR/$method-$options/$sum-1.${outfile1##*.}"
58         [ -z "$outfile2" ] || name2="$CACHEDIR/$method-$options/$sum-2.${outfile2##*.}"
59         tempfile1="${name1%/*}/new-${name1##*/}"
60         [ -z "$outfile2" ] || tempfile2="${name2%/*}/new-${name2##*/}"
61         if [ -f "$name1" ] && { [ -z "$outfile2" ] || [ -f "$name2" ]; }; then
62                 case "$outfile1" in */*) mkdir -p "${outfile1%/*}"; esac && { ln "$name1" "$outfile1" 2>/dev/null || cp "$name1" "$outfile1"; }
63                 [ -z "$outfile2" ] || { case "$outfile2" in */*) mkdir -p "${outfile2%/*}"; esac && { ln "$name2" "$outfile2" 2>/dev/null || cp "$name2" "$outfile2"; }; }
64                 conv=true
65         elif "$method" "$infile1" "$infile2" "$tempfile1" "$tempfile2" "$@"; then
66                 mv "$tempfile1" "$name1"
67                 [ -z "$outfile2" ] || mv "$tempfile2" "$name2"
68                 case "$outfile1" in */*) mkdir -p "${outfile1%/*}"; esac && { ln "$name1" "$outfile1" 2>/dev/null || cp "$name1" "$outfile1"; }
69                 [ -z "$outfile2" ] || { case "$outfile2" in */*) mkdir -p "${outfile2%/*}"; esac && { ln "$name2" "$outfile2" 2>/dev/null || cp "$name2" "$outfile2"; }; }
70                 conv=true
71         else
72                 rm -f "$tempfile1"
73                 rm -f "$tempfile2"
74                 exit 1
75         fi
76 }
77
78 reduce_jpeg2_dds()
79 {
80         i=$1; shift
81         ia=$1; shift
82         o=$1; shift; shift 
83         convert "$i" "$ia" -compose CopyOpacity -composite "$tmpdir/x.tga" && \
84         "$meprefix"compress-texture "$dds_tool" dxt5 "$tmpdir/x.tga" "$o" $1
85 }
86
87 reduce_jpeg2_jpeg2()
88 {
89         i=$1; shift
90         ia=$1; shift
91         o=$1; shift
92         oa=$1; shift
93         cp "$i" "$o" && jpegoptim --strip-all -m"$1" "$o" && \
94         cp "$ia" "$oa" && jpegoptim --strip-all -m"$2" "$oa"
95 }
96
97 reduce_jpeg_jpeg()
98 {
99         i=$1; shift; shift
100         o=$1; shift; shift
101         cp "$i" "$o" && jpegoptim --strip-all -m"$1" "$o"
102 }
103
104 reduce_ogg_ogg()
105 {
106         i=$1; shift; shift
107         o=$1; shift; shift
108         tags=`vorbiscomment -R -l "$i"`
109         oggdec -o "$tmpdir/x.wav" "$i" && \
110         oggenc -q"$1" -o "$o" "$tmpdir/x.wav"
111         echo "$tags" | vorbiscomment -R -w "$o"
112 }
113
114 reduce_wav_ogg()
115 {
116         i=$1; shift; shift
117         o=$1; shift; shift
118         oggenc -q"$1" -o "$o" "$i"
119 }
120
121 reduce_rgba_dds()
122 {
123         i=$1; shift; shift
124         o=$1; shift; shift
125         convert "$i" "$tmpdir/x.tga" && \
126         "$meprefix"compress-texture "$dds_tool" dxt5 "$tmpdir/x.tga" "$o" $1
127 }
128
129 reduce_rgba_jpeg2()
130 {
131         i=$1; shift; shift
132         o=$1; shift
133         oa=$1; shift
134         convert "$i" -alpha off     -quality 100 "$o" && \
135         convert "$i" -alpha extract -quality 100 "$oa" && \
136         jpegoptim --strip-all -m"$1" "$o" && \
137         jpegoptim --strip-all -m"$2" "$oa"
138 }
139
140 reduce_rgb_dds()
141 {
142         i=$1; shift; shift
143         o=$1; shift; shift
144         convert "$i" "$tmpdir/x.tga" && \
145         "$meprefix"compress-texture "$dds_tool" dxt1 "$tmpdir/x.tga" "$o" $1
146 }
147
148 reduce_rgb_jpeg()
149 {
150         i=$1; shift; shift
151         o=$1; shift; shift
152         convert "$i" "$o" && \
153         jpegoptim --strip-all -m"$1" "$o"
154 }
155
156 has_alpha()
157 {
158         i=$1; shift; shift
159         o=$1; shift; shift
160         if convert "$F" -depth 16 RGBA:- | perl -e 'while(read STDIN, $_, 8) { substr($_, 6, 2) eq "\xFF\xFF" or exit 1; } exit 0;'; then
161                 # no alpha
162                 : > "$o"
163         else
164                 # has alpha
165                 echo yes > "$o"
166         fi
167 }
168
169 to_delete=
170 for F in "$@"; do
171         f=${F%.*}
172
173         echo >&2 "Handling $F..."
174         conv=false
175         keep=false
176
177         will_jpeg=$do_jpeg
178         will_dds=$do_dds
179         case "$f" in
180                 *_bump) will_dds=false ;;
181                 ./models/player/*) will_dds=false ;;
182                 ./models/sprites/*) will_dds=false ;;
183                 ./textures/*) ;;
184                 ./models/*) ;;
185                 ./particles/*) ;;
186                 ./progs/*) ;;
187                 *)
188                         # we can't DDS compress the 2D textures, sorry
189                         # but JPEG is still fine
190                         will_dds=false
191                         ;;
192         esac
193
194         if $do_jpeg_if_not_dds; then
195                 if $will_dds; then
196                         will_jpeg=false
197                 else
198                         will_jpeg=true
199                 fi
200         fi
201
202         case "$F" in
203                 *_alpha.jpg)
204                         # handle in *.jpg case
205
206                         # they always got converted, I assume
207                         if $will_dds || $will_jpeg; then
208                                 conv=true
209                         fi
210                         keep=$will_jpeg
211                         ;;
212                 *.jpg)
213                         if [ -f "${f}_alpha.jpg" ]; then
214                                 cached "$will_dds"  reduce_jpeg2_dds   "$F" "${f}_alpha.jpg" "dds/${f}.dds" ""               "$dds_flags"
215                                 cached "$will_jpeg" reduce_jpeg2_jpeg2 "$F" "${f}_alpha.jpg" "$F"           "${f}_alpha.jpg" "$jpeg_qual_rgb" "$jpeg_qual_a"
216                         else                                   
217                                 cached "$will_dds"  reduce_rgb_dds     "$F" ""               "dds/${f}.dds" ""               "$dds_flags"
218                                 cached "$will_jpeg" reduce_jpeg_jpeg   "$F" ""               "$F"           ""               "$jpeg_qual_rgb"
219                         fi
220                         ;;
221                 *.png|*.tga)
222                         cached true has_alpha "$F" "" "$F.hasalpha" ""
223                         conv=false
224                         if [ -s "$F.hasalpha" ]; then
225                                 cached "$will_dds"  reduce_rgba_dds    "$F" ""               "dds/${f}.dds" ""               "$dds_flags"
226                                 cached "$will_jpeg" reduce_rgba_jpeg2  "$F" ""               "${f}.jpg"     "${f}_alpha.jpg" "$jpeg_qual_rgb" "$jpeg_qual_a"
227                         else                                                             
228                                 cached "$will_dds"  reduce_rgb_dds     "$F" ""               "dds/${f}.dds" ""               "$dds_flags"
229                                 cached "$will_jpeg" reduce_rgb_jpeg    "$F" ""               "${f}.jpg"     ""               "$jpeg_qual_rgb"
230                         fi
231                         rm -f "$F.hasalpha"
232                         ;;
233                 *.ogg)
234                         cached "$do_ogg" reduce_ogg_ogg "$F" "" "$F" "" "$ogg_qual"
235                         ;;
236                 *.wav)
237                         cached "$do_ogg" reduce_wav_ogg "$F" "" "$F" "" "$ogg_qual"
238                         ;;
239         esac
240         if $del_src; then
241                 if $conv; then
242                         if ! $keep; then
243                                 # FIXME can't have spaces in filenames that way
244                                 to_delete="$to_delete $F"
245                         fi
246                 fi
247         fi
248         # fix up DDS paths by a symbolic link
249         if [ -f "dds/${f}.dds" ]; then
250                 if [ -z "${f##./textures/*}" ]; then
251                         if [ -n "${f##./textures/*/*}" ]; then
252                                 ln -snf "textures/${f#./textures/}.dds" "dds/${f#./textures/}.dds"
253                         fi
254                 fi
255         fi
256 done
257 for F in $to_delete; do
258         rm -f "$F"
259 done