71709e5fe1b1f43b2e81f97c18f63a8fb97e3d09
[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 cached()
30 {
31         flag=$1; shift
32         method=$1; shift
33         infile1=$1; shift
34         infile2=$1; shift
35         outfile1=$1; shift
36         outfile2=$1; shift
37         if ! $flag; then
38                 return 0
39         fi
40         if [ x"$infile1" = x"$outfile1" ]; then
41                 keep=true
42         fi
43         options=`echo "$*" | git hash-object --stdin`
44         sum=`git hash-object "$infile1"`
45         if [ -n "$infile2" ]; then
46                 sum=$sum`git hash-object "$infile2"`
47         fi
48         mkdir -p "$CACHEDIR/$method-$options"
49         name1="$CACHEDIR/$method-$options/$sum-1.${outfile1##*.}"
50         [ -z "$outfile2" ] || name2="$CACHEDIR/$method-$options/$sum-2.${outfile2##*.}"
51         tempfile1="${name1%/*}/new-${name1##*/}"
52         [ -z "$outfile2" ] || tempfile2="${name2%/*}/new-${name2##*/}"
53         if [ -f "$name1" ] && { [ -z "$outfile2" ] || [ -f "$name2" ]; }; then
54                 case "$outfile1" in */*) mkdir -p "${outfile1%/*}"; esac && { ln "$name1" "$outfile1" 2>/dev/null || cp "$name1" "$outfile1"; }
55                 [ -z "$outfile2" ] || { case "$outfile2" in */*) mkdir -p "${outfile2%/*}"; esac && { ln "$name2" "$outfile2" 2>/dev/null || cp "$name2" "$outfile2"; }; }
56                 conv=true
57         elif "$method" "$infile1" "$infile2" "$tempfile1" "$tempfile2" "$@"; then
58                 mv "$tempfile1" "$name1"
59                 [ -z "$outfile2" ] || mv "$tempfile2" "$name2"
60                 case "$outfile1" in */*) mkdir -p "${outfile1%/*}"; esac && { ln "$name1" "$outfile1" 2>/dev/null || cp "$name1" "$outfile1"; }
61                 [ -z "$outfile2" ] || { case "$outfile2" in */*) mkdir -p "${outfile2%/*}"; esac && { ln "$name2" "$outfile2" 2>/dev/null || cp "$name2" "$outfile2"; }; }
62                 conv=true
63         else
64                 rm -f "$tempfile1"
65                 rm -f "$tempfile2"
66                 exit 1
67         fi
68 }
69
70 reduce_jpeg2_dds()
71 {
72         i=$1; shift
73         ia=$1; shift
74         o=$1; shift; shift 
75         convert "$i" "$ia" -compose CopyOpacity -composite "$tmpdir/x.tga" && \
76         "$meprefix"compress-texture "$dds_tool" dxt5 "$tmpdir/x.tga" "$o" $1
77 }
78
79 reduce_jpeg2_jpeg2()
80 {
81         i=$1; shift
82         ia=$1; shift
83         o=$1; shift
84         oa=$1; shift
85         cp "$i" "$o" && jpegoptim --strip-all -m"$1" "$o" && \
86         cp "$ia" "$oa" && jpegoptim --strip-all -m"$2" "$oa"
87 }
88
89 reduce_jpeg_jpeg()
90 {
91         i=$1; shift; shift
92         o=$1; shift; shift
93         cp "$i" "$o" && jpegoptim --strip-all -m"$1" "$o"
94 }
95
96 reduce_ogg()
97 {
98         i=$1; shift; shift
99         o=$1; shift; shift
100         oggdec -o "$tmpdir/x.wav" "$i" && \
101         oggenc -q"$1" -o "$o" "$tmpdir/x.wav"
102 }
103
104 reduce_rgba_dds()
105 {
106         i=$1; shift; shift
107         o=$1; shift; shift
108         convert "$i" "$tmpdir/x.tga" && \
109         "$meprefix"compress-texture "$dds_tool" dxt5 "$tmpdir/x.tga" "$o" $1
110 }
111
112 reduce_rgba_jpeg2()
113 {
114         i=$1; shift; shift
115         o=$1; shift
116         oa=$1; shift
117         convert "$i" -alpha extract -quality 100 "$o" && \
118         convert "$i" -alpha off     -quality 100 "$oa" && \
119         jpegoptim --strip-all -m"$1" "$o" && \
120         jpegoptim --strip-all -m"$2" "$oa"
121 }
122
123 reduce_rgb_dds()
124 {
125         i=$1; shift; shift
126         o=$1; shift; shift
127         convert "$i" "$tmpdir/x.tga" && \
128         "$meprefix"compress-texture "$dds_tool" dxt1 "$tmpdir/x.tga" "$o" $1
129 }
130
131 reduce_rgb_jpeg()
132 {
133         i=$1; shift; shift
134         o=$1; shift; shift
135         convert "$i" "$o" && \
136         jpegoptim --strip-all -m"$1" "$o"
137 }
138
139 has_alpha()
140 {
141         i=$1; shift; shift
142         o=$1; shift; shift
143         if convert "$F" -depth 16 RGBA:- | perl -e 'while(read STDIN, $_, 8) { substr($_, 6, 2) eq "\xFF\xFF" or exit 1; } exit 0;'; then
144                 # no alpha
145                 : > "$o"
146         else
147                 # has alpha
148                 echo yes > "$o"
149         fi
150 }
151
152 for F in "$@"; do
153         echo >&2 "Handling $F..."
154         conv=false
155         keep=false
156         case "$F" in
157                 *_alpha.jpg)
158                         # handle in *.jpg case
159
160                         # they always got converted, I assume
161                         conv=true
162                         keep=$do_jpeg
163                         ;;
164                 *.jpg)
165                         if [ -f "${F%.jpg}_alpha.jpg" ]; then
166                                 cached "$do_dds"  reduce_jpeg2_dds   "$F" "${F%.*}_alpha.jpg" "dds/${F%.*}.dds" ""                  "$dds_flags"
167                                 cached "$do_jpeg" reduce_jpeg2_jpeg2 "$F" "${F%.*}_alpha.jpg" "$F"              "${F%.*}_alpha.jpg" "$jpeg_qual_rgb" "$jpeg_qual_a"
168                         else                                   
169                                 cached "$do_dds"  reduce_rgb_dds     "$F" ""                  "dds/${F%.*}.dds" ""                  "$dds_flags"
170                                 cached "$do_jpeg" reduce_jpeg_jpeg   "$F" ""                  "$F"              ""                  "$jpeg_qual_rgb"
171                         fi
172                         ;;
173                 *.png|*.tga)
174                         cached true has_alpha "$F" "" "$F.hasalpha" ""
175                         if [ -s "$F.hasalpha" ]; then
176                                 cached "$do_dds"  reduce_rgba_dds    "$F" ""                  "dds/${F%.*}.dds" ""                  "$dds_flags"
177                                 cached "$do_jpeg" reduce_rgba_jpeg2  "$F" ""                  "${F%.*}.jpg"     "${F%.*}_alpha.jpg" "$jpeg_qual_rgb" "$jpeg_qual_a"
178                                 rm -f "$F" # TGA becomes useless after JPEGging
179                         else                                                             
180                                 cached "$do_dds"  reduce_rgb_dds     "$F" ""                  "dds/${F%.*}.dds" ""                  "$dds_flags"
181                                 cached "$do_jpeg" reduce_rgb_jpeg    "$F" ""                  "${F%.*}.jpg"     ""                  "$jpeg_qual_rgb"
182                                 rm -f "$F" # TGA becomes useless after JPEGging
183                         fi
184                         rm -f "$F.hasalpha"
185                         ;;
186                 *.ogg)
187                         cached "$do_ogg" reduce_ogg "$F" "" "$F" "" "$ogg_qual"
188                         ;;
189         esac
190         if $del_src; then
191                 if $conv; then
192                         if ! $keep; then
193                                 rm -f "$F"
194                         fi
195                 fi
196         fi
197 done