6d6b7684421cc23487dc6428879c19630126138a
[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         oggdec -o "$tmpdir/x.wav" "$i" && \
109         oggenc -q"$1" -o "$o" "$tmpdir/x.wav"
110 }
111
112 reduce_wav_ogg()
113 {
114         i=$1; shift; shift
115         o=$1; shift; shift
116         oggenc -q"$1" -o "$o" "$i"
117 }
118
119 reduce_rgba_dds()
120 {
121         i=$1; shift; shift
122         o=$1; shift; shift
123         convert "$i" "$tmpdir/x.tga" && \
124         "$meprefix"compress-texture "$dds_tool" dxt5 "$tmpdir/x.tga" "$o" $1
125 }
126
127 reduce_rgba_jpeg2()
128 {
129         i=$1; shift; shift
130         o=$1; shift
131         oa=$1; shift
132         convert "$i" -alpha extract -quality 100 "$o" && \
133         convert "$i" -alpha off     -quality 100 "$oa" && \
134         jpegoptim --strip-all -m"$1" "$o" && \
135         jpegoptim --strip-all -m"$2" "$oa"
136 }
137
138 reduce_rgb_dds()
139 {
140         i=$1; shift; shift
141         o=$1; shift; shift
142         convert "$i" "$tmpdir/x.tga" && \
143         "$meprefix"compress-texture "$dds_tool" dxt1 "$tmpdir/x.tga" "$o" $1
144 }
145
146 reduce_rgb_jpeg()
147 {
148         i=$1; shift; shift
149         o=$1; shift; shift
150         convert "$i" "$o" && \
151         jpegoptim --strip-all -m"$1" "$o"
152 }
153
154 has_alpha()
155 {
156         i=$1; shift; shift
157         o=$1; shift; shift
158         if convert "$F" -depth 16 RGBA:- | perl -e 'while(read STDIN, $_, 8) { substr($_, 6, 2) eq "\xFF\xFF" or exit 1; } exit 0;'; then
159                 # no alpha
160                 : > "$o"
161         else
162                 # has alpha
163                 echo yes > "$o"
164         fi
165 }
166
167 to_delete=
168 for F in "$@"; do
169         echo >&2 "Handling $F..."
170         conv=false
171         keep=false
172
173         will_jpeg=$do_jpeg
174         will_dds=$do_dds
175         case "$F" in
176                 ./textures/*) ;;
177                 ./models/*) ;;
178                 ./maps/*/*) ;;
179                 *)
180                         # we can't DDS compress the 2D textures, sorry
181                         # but JPEG is still fine
182                         will_dds=false
183                         ;;
184         esac
185
186         if $do_jpeg_if_not_dds; then
187                 if $will_dds; then
188                         will_jpeg=false
189                 else
190                         will_jpeg=true
191                 fi
192         fi
193
194         case "$F" in
195                 *_alpha.jpg)
196                         # handle in *.jpg case
197
198                         # they always got converted, I assume
199                         if $will_dds || $will_jpeg; then
200                                 conv=true
201                         fi
202                         keep=$will_jpeg
203                         ;;
204                 *.jpg)
205                         if [ -f "${F%.jpg}_alpha.jpg" ]; then
206                                 cached "$will_dds"  reduce_jpeg2_dds   "$F" "${F%.*}_alpha.jpg" "dds/${F%.*}.dds" ""                  "$dds_flags"
207                                 cached "$will_jpeg" reduce_jpeg2_jpeg2 "$F" "${F%.*}_alpha.jpg" "$F"              "${F%.*}_alpha.jpg" "$jpeg_qual_rgb" "$jpeg_qual_a"
208                         else                                   
209                                 cached "$will_dds"  reduce_rgb_dds     "$F" ""                  "dds/${F%.*}.dds" ""                  "$dds_flags"
210                                 cached "$will_jpeg" reduce_jpeg_jpeg   "$F" ""                  "$F"              ""                  "$jpeg_qual_rgb"
211                         fi
212                         ;;
213                 *.png|*.tga)
214                         cached true has_alpha "$F" "" "$F.hasalpha" ""
215                         conv=false
216                         if [ -s "$F.hasalpha" ]; then
217                                 cached "$will_dds"  reduce_rgba_dds    "$F" ""                  "dds/${F%.*}.dds" ""                  "$dds_flags"
218                                 cached "$will_jpeg" reduce_rgba_jpeg2  "$F" ""                  "${F%.*}.jpg"     "${F%.*}_alpha.jpg" "$jpeg_qual_rgb" "$jpeg_qual_a"
219                         else                                                             
220                                 cached "$will_dds"  reduce_rgb_dds     "$F" ""                  "dds/${F%.*}.dds" ""                  "$dds_flags"
221                                 cached "$will_jpeg" reduce_rgb_jpeg    "$F" ""                  "${F%.*}.jpg"     ""                  "$jpeg_qual_rgb"
222                         fi
223                         rm -f "$F.hasalpha"
224                         ;;
225                 *.ogg)
226                         cached "$do_ogg" reduce_ogg_ogg "$F" "" "$F" "" "$ogg_qual"
227                         ;;
228                 *.wav)
229                         cached "$do_ogg" reduce_wav_ogg "$F" "" "$F" "" "$ogg_qual"
230                         ;;
231         esac
232         if $del_src; then
233                 if $conv; then
234                         if ! $keep; then
235                                 # FIXME can't have spaces in filenames that way
236                                 to_delete="$to_delete $F"
237                         fi
238                 fi
239         fi
240 done
241 for F in $to_delete; do
242         rm -f "$F"
243 done