allow choosing between ATICompressor and DXTC.dll
[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
14 me=$0
15 case "$me" in
16         */*)
17                 meprefix=${me%/*}/
18                 ;;
19         *)
20                 meprefix=
21                 ;;
22 esac
23
24 tmpdir=`mktemp -d -t cached-converter.XXXXXX`
25 trap 'exit 1' INT
26 trap 'rm -rf "$tmpdir"' EXIT
27
28 cached()
29 {
30         flag=$1; shift
31         method=$1; shift
32         infile1=$1; shift
33         infile2=$1; shift
34         outfile1=$1; shift
35         outfile2=$1; shift
36         if ! $flag; then
37                 return 0
38         fi
39         options=`echo "$*" | git hash-object --stdin`
40         sum=`git hash-object "$infile1"`
41         if [ -n "$infile2" ]; then
42                 sum=$sum`git hash-object "$infile2"`
43         fi
44         mkdir -p "$CACHEDIR/$method-$options"
45         name1="$CACHEDIR/$method-$options/$sum-1.${outfile1##*.}"
46         [ -z "$outfile2" ] || name2="$CACHEDIR/$method-$options/$sum-2.${outfile2##*.}"
47         tempfile1="${name1%/*}/new-${name1##*/}"
48         [ -z "$outfile2" ] || tempfile2="${name2%/*}/new-${name2##*/}"
49         if [ -f "$name1" ]; then
50                 ln "$name1" "$outfile1" 2>/dev/null || cp "$name1" "$outfile1"
51                 [ -z "$outfile2" ] || ln "$name2" "$outfile2" 2>/dev/null || cp "$name2" "$outfile2"
52         elif "$method" "$infile1" "$infile2" "$tempfile1" "$tempfile2" "$@"; then
53                 mv "$tempfile1" "$name1"
54                 [ -z "$outfile2" ] || mv "$tempfile2" "$name2"
55                 case "$outfile1" in */*) mkdir -p "${outfile1%/*}"; esac && { ln "$name1" "$outfile1" 2>/dev/null || cp "$name1" "$outfile1"; }
56                 [ -z "$outfile2" ] || { case "$outfile2" in */*) mkdir -p "${outfile2%/*}"; esac && { ln "$name2" "$outfile2" 2>/dev/null || cp "$name2" "$outfile2"; }; }
57         else
58                 rm -f "$tempfile1"
59                 rm -f "$tempfile2"
60                 exit 1
61         fi
62 }
63
64 reduce_jpeg2_dds()
65 {
66         i=$1; shift
67         ia=$1; shift
68         o=$1; shift; shift 
69         convert "$i" "$ia" -compose CopyOpacity -composite "$tmpdir/x.png" && \
70         "$meprefix"compress-texture "$dds_tool" dxt5 "$tmpdir/x.png" "$o" $1
71 }
72
73 reduce_jpeg2_jpeg2()
74 {
75         i=$1; shift
76         ia=$1; shift
77         o=$1; shift
78         oa=$1; shift
79         cp "$i" "$o" && jpegoptim --strip-all -m"$1" "$o" && \
80         cp "$ia" "$oa" && jpegoptim --strip-all -m"$2" "$oa"
81 }
82
83 reduce_jpeg_jpeg()
84 {
85         i=$1; shift; shift
86         o=$1; shift; shift
87         cp "$i" "$o" && jpegoptim --strip-all -m"$1" "$o"
88 }
89
90 reduce_ogg()
91 {
92         i=$1; shift; shift
93         o=$1; shift; shift
94         oggdec -o "$tmpdir/x.wav" "$i" && \
95         oggenc -q"$1" -o "$o" "$tmpdir/x.wav"
96 }
97
98 reduce_rgba_dds()
99 {
100         i=$1; shift; shift
101         o=$1; shift; shift
102         "$meprefix"compress-texture "$dds_tool" dxt5 "$i" "$o" $1
103 }
104
105 reduce_rgba_jpeg2()
106 {
107         i=$1; shift; shift
108         o=$1; shift
109         oa=$1; shift
110         convert "$i" -alpha extract -quality 100 "$o" && \
111         convert "$i" -alpha off     -quality 100 "$oa" && \
112         jpegoptim --strip-all -m"$1" "$o" && \
113         jpegoptim --strip-all -m"$2" "$oa"
114 }
115
116 reduce_rgb_dds()
117 {
118         i=$1; shift; shift
119         o=$1; shift; shift
120         "$meprefix"compress-texture "$dds_tool" dxt1 "$i" "$o" $1
121 }
122
123 reduce_rgb_jpeg()
124 {
125         i=$1; shift; shift
126         o=$1; shift; shift
127         convert "$i" "$o" && \
128         jpegoptim --strip-all -m"$1" "$o"
129 }
130
131
132 for F in "$@"; do
133         case "$F" in
134         *_alpha.jpg)
135                 # handle in *.jpg case
136                 ;;
137         *.jpg)
138                 if [ -f "${F%.jpg}_alpha.jpg" ]; then
139                         cached "$do_dds"  reduce_jpeg2_dds   "$F" "${F%.*}_alpha.jpg" "dds/${F%.*}.dds" ""                  "$dds_flags"
140                         cached "$do_jpeg" reduce_jpeg2_jpeg2 "$F" "${F%.*}_alpha.jpg" "$F"              "${F%.*}_alpha.jpg" "$jpeg_qual_rgb"
141                 else                                   
142                         cached "$do_dds"  reduce_rgb_dds     "$F" ""                  "dds/${F%.*}.dds" ""                  "$dds_flags"
143                         cached "$do_jpeg" reduce_jpeg_jpeg   "$F" ""                  "$F"              ""                  "$jpeg_qual_rgb"
144                 fi
145                 ;;
146         *.png|*.tga)
147                 if convert "$F" -depth 16 RGBA:- | perl -e 'while(read STDIN, $_, 8) { substr($_, 6, 2) eq "\xFF\xFF" or exit 1; } exit 0;'; then
148                         cached "$do_dds"  reduce_rgb_dds     "$F" ""                  "dds/${F%.*}.dds" ""                  "$dds_flags"
149                         cached "$do_jpeg" reduce_rgb_jpeg    "$F" ""                  "${F%.*}.jpg"     ""                  "$jpeg_qual_rgb"
150                         rm -f "$F"
151                 else                                                             
152                         cached "$do_dds"  reduce_rgba_dds    "$F" ""                  "dds/${F%.*}.dds" ""                  "$dds_flags"
153                         cached "$do_jpeg" reduce_rgba_jpeg2  "$F" ""                  "${F%.*}.jpg"     "${F%.*}_alpha.jpg" "$jpeg_qual_rgb" "$jpeg_qual_a"
154                         rm -f "$F"
155                 fi
156                 ;;
157         *.ogg)
158                 cached "$do_ogg" reduce_ogg "$F" "" "$F" "" "$ogg_qual"
159                 ;;
160         esac
161 done