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