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