f8281f90bbf1d4617d7ce922cedb1b14f973de67
[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         method=$1; shift
21         infile1=$1; shift
22         infile2=$1; shift
23         outfile1=$1; shift
24         outfile2=$1; shift
25         options=`echo "$*" | git hash-object --stdin`
26         sum=`git hash-object "$infile1"`
27         if [ -n "$infile2" ]; then
28         sum=$sum`git hash-object "$infile2"`
29         fi
30         mkdir -p "$CACHEDIR/$method-$options"
31         name1="$CACHEDIR/$method/$sum-1.${outfile1##*.}"
32         [ -z "$outfile2" ] || name2="$CACHEDIR/$method/$sum-2.${outfile2##*.}"
33         tempfile1="$name1.new"
34         [ -z "$outfile2" ] || tempfile2="$name2.new"
35         if "$method" "$infile1" "$infile2" "$tempfile1" "$tempfile2" "$@"; then
36         mv "$tempfile1" "$name1"
37         [ -z "$outfile2" ] || mv "$tempfile2" "$name2"
38         ln "$name1" "$outfile1" 2>/dev/null || cp "$name1" "$outfile1"
39         [ -z "$outfile2" ] || ln "$name2" "$outfile2" 2>/dev/null || cp "$name2" "$outfile2"
40         else
41         rm -f "$tempfile1"
42         rm -f "$tempfile2"
43         exit 1
44         fi
45 }
46
47 reduce_jpeg2_dds()
48 {
49         i=$1; shift
50         ia=$1; shift
51         o=$1; shift; shift 
52         convert "$i" "$ia" -compose CopyOpacity -composite "$tmpdir/x.png" && \
53         nvcompress -alpha -bc3 $1 "$tmpdir/x.png" "$o"
54 }
55
56 reduce_jpeg2_jpeg2()
57 {
58         i=$1; shift
59         ia=$1; shift
60         o=$1; shift
61         oa=$1; shift
62         cp "$i" "$o" && jpegoptim --strip-all -m"$1" "$o" && \
63         cp "$ia" "$oa" && jpegoptim --strip-all -m"$2" "$oa"
64 }
65
66 reduce_jpeg2_jpeg2()
67 {
68         i=$1; shift; shift
69         o=$1; shift; shift
70         cp "$i" "$o" && jpegoptim --strip-all -m"$1" "$o"
71 }
72
73 reduce_ogg()
74 {
75         i=$1; shift; shift
76         o=$1; shift; shift
77         oggdec -o "$tmpdir/x.wav" "$i" && \
78         oggenc -q"$1" -o "$o" "$tmpdir/x.wav"
79 }
80
81 reduce_rgba_dds()
82 {
83         i=$1; shift; shift
84         o=$1; shift; shift
85         nvcompress -alpha -bc3 $1 "$i" "$o"
86 }
87
88 reduce_rgba_jpeg2()
89 {
90         i=$1; shift; shift
91         o=$1; shift
92         oa=$1; shift
93         convert "$X" -alpha extract -quality 100 "$o" && \
94         convert "$X" -alpha off     -quality 100 "$oa" && \
95         jpegoptim --strip-all -m"$1" "$o" && \
96         jpegoptim --strip-all -m"$2" "$oa"
97 }
98
99 reduce_rgb_dds()
100 {
101         i=$1; shift; shift
102         o=$1; shift; shift
103         nvcompress -bc1 $1 "$i" "$o"
104 }
105
106 reduce_rgb_jpeg()
107 {
108         i=$1; shift; shift
109         o=$1; shift; shift
110         convert "$X" "$o" && \
111         jpegoptim --strip-all -m"$1" "$o"
112 }
113
114
115 for F in "$@"; do
116         case "$F" in
117         *_alpha.jpg)
118                 # handle in *.jpg case
119                 ;;
120         *.jpg)
121                 if [ -f "${F%.jpg}_alpha.jpg" ]; then
122                         cached "$do_jpeg" reduce_jpeg2_jpeg2 "$F" "${F%.*}_alpha.jpg" "$F"              "${F%.*}_alpha.jpg" "$jpeg_qual_rgb"
123                         cached "$do_jpeg" reduce_jpeg2_dds   "$F" "${F%.*}_alpha.jpg" "$F"              "${F%.*}_alpha.jpg" "$jpeg_qual_rgb"
124                 else                                   
125                         cached "$do_jpeg" reduce_jpeg_jpeg   "$F" ""                  "$F"              ""                  "$jpeg_qual_rgb"
126                         cached "$do_dds"  reduce_rgb_dds     "$F" ""                  "dds/${F%.*}.dds" ""                  "$dds_flags"
127                 fi
128                 ;;
129         *.png|*.tga)
130                 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
131                         cached "$do_jpeg" reduce_rgb_jpeg    "$F" ""                  "${F%.*}.jpg"     ""                  "$jpeg_qual_rgb"
132                         cached "$do_dds"  reduce_rgb_dds     "$F" ""                  "dds/${F%.*}.dds" ""                  "$dds_flags"
133                 else                                                             
134                         cached "$do_jpeg" reduce_rgba_jpeg2  "$F" ""                  "${F%.*}.jpg"     "${F%.*}_alpha.jpg" "$jpeg_qual_rgb" "$jpeg_qual_a"
135                         cached "$do_dds"  reduce_rgba_dds    "$F" ""                  "dds/${F%.*}.dds" ""                  "$dds_flags"
136                 fi
137                 ;;
138         *.ogg)
139                 cached "$do_ogg" reduce_ogg "$F" "" "$F" "" "$ogg_qual"
140                 ;;
141         esac
142 done