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