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