9b8eb8d66079366d7e945bdd5cdb0a1bd51b50fa
[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         [ -z "${outfile1##*/*}" ] && mkdir -p "${outfile1%/*}"
50         [ -z "$outfile2" ] || { [ -z "${outfile2##*/*}" ] && mkdir -p "${outfile2%/*}"; }
51         if [ -f "$name1" ]; then
52                 ln "$name1" "$outfile1" 2>/dev/null || cp "$name1" "$outfile1"
53                 [ -z "$outfile2" ] || ln "$name2" "$outfile2" 2>/dev/null || cp "$name2" "$outfile2"
54         elif "$method" "$infile1" "$infile2" "$tempfile1" "$tempfile2" "$@"; then
55                 mv "$tempfile1" "$name1"
56                 [ -z "$outfile2" ] || mv "$tempfile2" "$name2"
57                 case "$outfile1" in */*) mkdir -p "${outfile1%/*}"; esac && { ln "$name1" "$outfile1" 2>/dev/null || cp "$name1" "$outfile1"; }
58                 [ -z "$outfile2" ] || { case "$outfile2" in */*) mkdir -p "${outfile2%/*}"; esac && { ln "$name2" "$outfile2" 2>/dev/null || cp "$name2" "$outfile2"; }; }
59         else
60                 rm -f "$tempfile1"
61                 rm -f "$tempfile2"
62                 exit 1
63         fi
64 }
65
66 reduce_jpeg2_dds()
67 {
68         i=$1; shift
69         ia=$1; shift
70         o=$1; shift; shift 
71         convert "$i" "$ia" -compose CopyOpacity -composite "$tmpdir/x.tga" && \
72         "$meprefix"compress-texture "$dds_tool" dxt5 "$tmpdir/x.tga" "$o" $1
73 }
74
75 reduce_jpeg2_jpeg2()
76 {
77         i=$1; shift
78         ia=$1; shift
79         o=$1; shift
80         oa=$1; shift
81         cp "$i" "$o" && jpegoptim --strip-all -m"$1" "$o" && \
82         cp "$ia" "$oa" && jpegoptim --strip-all -m"$2" "$oa"
83 }
84
85 reduce_jpeg_jpeg()
86 {
87         i=$1; shift; shift
88         o=$1; shift; shift
89         cp "$i" "$o" && jpegoptim --strip-all -m"$1" "$o"
90 }
91
92 reduce_ogg()
93 {
94         i=$1; shift; shift
95         o=$1; shift; shift
96         oggdec -o "$tmpdir/x.wav" "$i" && \
97         oggenc -q"$1" -o "$o" "$tmpdir/x.wav"
98 }
99
100 reduce_rgba_dds()
101 {
102         i=$1; shift; shift
103         o=$1; shift; shift
104         convert "$i" "$tmpdir/x.tga" && \
105         "$meprefix"compress-texture "$dds_tool" dxt5 "$tmpdir/x.tga" "$o" $1
106 }
107
108 reduce_rgba_jpeg2()
109 {
110         i=$1; shift; shift
111         o=$1; shift
112         oa=$1; shift
113         convert "$i" -alpha extract -quality 100 "$o" && \
114         convert "$i" -alpha off     -quality 100 "$oa" && \
115         jpegoptim --strip-all -m"$1" "$o" && \
116         jpegoptim --strip-all -m"$2" "$oa"
117 }
118
119 reduce_rgb_dds()
120 {
121         i=$1; shift; shift
122         o=$1; shift; shift
123         convert "$i" "$tmpdir/x.tga" && \
124         "$meprefix"compress-texture "$dds_tool" dxt1 "$tmpdir/x.tga" "$o" $1
125 }
126
127 reduce_rgb_jpeg()
128 {
129         i=$1; shift; shift
130         o=$1; shift; shift
131         convert "$i" "$o" && \
132         jpegoptim --strip-all -m"$1" "$o"
133 }
134
135
136 for F in "$@"; do
137         case "$F" in
138         *_alpha.jpg)
139                 # handle in *.jpg case
140                 ;;
141         *.jpg)
142                 if [ -f "${F%.jpg}_alpha.jpg" ]; then
143                         cached "$do_dds"  reduce_jpeg2_dds   "$F" "${F%.*}_alpha.jpg" "dds/${F%.*}.dds" ""                  "$dds_flags"
144                         cached "$do_jpeg" reduce_jpeg2_jpeg2 "$F" "${F%.*}_alpha.jpg" "$F"              "${F%.*}_alpha.jpg" "$jpeg_qual_rgb" "$jpeg_qual_a"
145                 else                                   
146                         cached "$do_dds"  reduce_rgb_dds     "$F" ""                  "dds/${F%.*}.dds" ""                  "$dds_flags"
147                         cached "$do_jpeg" reduce_jpeg_jpeg   "$F" ""                  "$F"              ""                  "$jpeg_qual_rgb"
148                 fi
149                 ;;
150         *.png|*.tga)
151                 if convert "$F" -depth 16 RGBA:- | perl -e 'while(read STDIN, $_, 8) { substr($_, 6, 2) eq "\xFF\xFF" or exit 1; } exit 0;'; then
152                         cached "$do_dds"  reduce_rgb_dds     "$F" ""                  "dds/${F%.*}.dds" ""                  "$dds_flags"
153                         cached "$do_jpeg" reduce_rgb_jpeg    "$F" ""                  "${F%.*}.jpg"     ""                  "$jpeg_qual_rgb"
154                         rm -f "$F"
155                 else                                                             
156                         cached "$do_dds"  reduce_rgba_dds    "$F" ""                  "dds/${F%.*}.dds" ""                  "$dds_flags"
157                         cached "$do_jpeg" reduce_rgba_jpeg2  "$F" ""                  "${F%.*}.jpg"     "${F%.*}_alpha.jpg" "$jpeg_qual_rgb" "$jpeg_qual_a"
158                         rm -f "$F"
159                 fi
160                 ;;
161         *.ogg)
162                 cached "$do_ogg" reduce_ogg "$F" "" "$F" "" "$ogg_qual"
163                 ;;
164         esac
165 done