72f427c71d89bf150ac23dd4a4a40ba36431c74f
[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                 ./textures/*) ;;
183                 ./models/*) ;;
184                 ./particles/*) ;;
185                 ./progs/*) ;;
186                 *)
187                         # we can't DDS compress the 2D textures, sorry
188                         # but JPEG is still fine
189                         will_dds=false
190                         ;;
191         esac
192
193         if $do_jpeg_if_not_dds; then
194                 if $will_dds; then
195                         will_jpeg=false
196                 else
197                         will_jpeg=true
198                 fi
199         fi
200
201         case "$F" in
202                 *_alpha.jpg)
203                         # handle in *.jpg case
204
205                         # they always got converted, I assume
206                         if $will_dds || $will_jpeg; then
207                                 conv=true
208                         fi
209                         keep=$will_jpeg
210                         ;;
211                 *.jpg)
212                         if [ -f "${f}_alpha.jpg" ]; then
213                                 cached "$will_dds"  reduce_jpeg2_dds   "$F" "${f}_alpha.jpg" "dds/${f}.dds" ""               "$dds_flags"
214                                 cached "$will_jpeg" reduce_jpeg2_jpeg2 "$F" "${f}_alpha.jpg" "$F"           "${f}_alpha.jpg" "$jpeg_qual_rgb" "$jpeg_qual_a"
215                         else                                   
216                                 cached "$will_dds"  reduce_rgb_dds     "$F" ""               "dds/${f}.dds" ""               "$dds_flags"
217                                 cached "$will_jpeg" reduce_jpeg_jpeg   "$F" ""               "$F"           ""               "$jpeg_qual_rgb"
218                         fi
219                         ;;
220                 *.png|*.tga)
221                         cached true has_alpha "$F" "" "$F.hasalpha" ""
222                         conv=false
223                         if [ -s "$F.hasalpha" ]; then
224                                 cached "$will_dds"  reduce_rgba_dds    "$F" ""               "dds/${f}.dds" ""               "$dds_flags"
225                                 cached "$will_jpeg" reduce_rgba_jpeg2  "$F" ""               "${f}.jpg"     "${f}_alpha.jpg" "$jpeg_qual_rgb" "$jpeg_qual_a"
226                         else                                                             
227                                 cached "$will_dds"  reduce_rgb_dds     "$F" ""               "dds/${f}.dds" ""               "$dds_flags"
228                                 cached "$will_jpeg" reduce_rgb_jpeg    "$F" ""               "${f}.jpg"     ""               "$jpeg_qual_rgb"
229                         fi
230                         rm -f "$F.hasalpha"
231                         ;;
232                 *.ogg)
233                         cached "$do_ogg" reduce_ogg_ogg "$F" "" "$F" "" "$ogg_qual"
234                         ;;
235                 *.wav)
236                         cached "$do_ogg" reduce_wav_ogg "$F" "" "$F" "" "$ogg_qual"
237                         ;;
238         esac
239         if $del_src; then
240                 if $conv; then
241                         if ! $keep; then
242                                 # FIXME can't have spaces in filenames that way
243                                 to_delete="$to_delete $F"
244                         fi
245                 fi
246         fi
247         # fix up DDS paths by a symbolic link
248         if [ -f "dds/${f}.dds" ]; then
249                 if [ -z "${f##./textures/*}" ]; then
250                         if [ -n "${f##./textures/*/*}" ]; then
251                                 ln -snf "textures/${f#./textures/}.dds" "dds/${f#./textures/}.dds"
252                         fi
253                 fi
254         fi
255 done
256 for F in $to_delete; do
257         rm -f "$F"
258 done