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