80cdcc341c26f19a5d38d9cac6980319b97a459b
[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" ] && { [ -z "$outfile2" ] || [ -f "$name2" ]; }; then
50                 case "$outfile1" in */*) mkdir -p "${outfile1%/*}"; esac && { ln "$name1" "$outfile1" 2>/dev/null || cp "$name1" "$outfile1"; }
51                 [ -z "$outfile2" ] || { case "$outfile2" in */*) mkdir -p "${outfile2%/*}"; esac && { 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.tga" && \
70         "$meprefix"compress-texture "$dds_tool" dxt5 "$tmpdir/x.tga" "$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         convert "$i" "$tmpdir/x.tga" && \
103         "$meprefix"compress-texture "$dds_tool" dxt5 "$tmpdir/x.tga" "$o" $1
104 }
105
106 reduce_rgba_jpeg2()
107 {
108         i=$1; shift; shift
109         o=$1; shift
110         oa=$1; shift
111         convert "$i" -alpha extract -quality 100 "$o" && \
112         convert "$i" -alpha off     -quality 100 "$oa" && \
113         jpegoptim --strip-all -m"$1" "$o" && \
114         jpegoptim --strip-all -m"$2" "$oa"
115 }
116
117 reduce_rgb_dds()
118 {
119         i=$1; shift; shift
120         o=$1; shift; shift
121         convert "$i" "$tmpdir/x.tga" && \
122         "$meprefix"compress-texture "$dds_tool" dxt1 "$tmpdir/x.tga" "$o" $1
123 }
124
125 reduce_rgb_jpeg()
126 {
127         i=$1; shift; shift
128         o=$1; shift; shift
129         convert "$i" "$o" && \
130         jpegoptim --strip-all -m"$1" "$o"
131 }
132
133 has_alpha()
134 {
135         i=$1; shift; shift
136         o=$1; shift; shift
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                 # no alpha
139                 : > "$o"
140         else
141                 # has alpha
142                 echo yes > "$o"
143         fi
144 }
145
146 for F in "$@"; do
147         echo >&2 "Handling $F..."
148         case "$F" in
149         *_alpha.jpg)
150                 # handle in *.jpg case
151                 ;;
152         *.jpg)
153                 if [ -f "${F%.jpg}_alpha.jpg" ]; then
154                         cached "$do_dds"  reduce_jpeg2_dds   "$F" "${F%.*}_alpha.jpg" "dds/${F%.*}.dds" ""                  "$dds_flags"
155                         cached "$do_jpeg" reduce_jpeg2_jpeg2 "$F" "${F%.*}_alpha.jpg" "$F"              "${F%.*}_alpha.jpg" "$jpeg_qual_rgb" "$jpeg_qual_a"
156                 else                                   
157                         cached "$do_dds"  reduce_rgb_dds     "$F" ""                  "dds/${F%.*}.dds" ""                  "$dds_flags"
158                         cached "$do_jpeg" reduce_jpeg_jpeg   "$F" ""                  "$F"              ""                  "$jpeg_qual_rgb"
159                 fi
160                 ;;
161         *.png|*.tga)
162                 cached true has_alpha "$F" "" "$F.hasalpha" ""
163                 if [ -s "$F.hasalpha" ]; then
164                         cached "$do_dds"  reduce_rgba_dds    "$F" ""                  "dds/${F%.*}.dds" ""                  "$dds_flags"
165                         cached "$do_jpeg" reduce_rgba_jpeg2  "$F" ""                  "${F%.*}.jpg"     "${F%.*}_alpha.jpg" "$jpeg_qual_rgb" "$jpeg_qual_a"
166                         rm -f "$F"
167                 else                                                             
168                         cached "$do_dds"  reduce_rgb_dds     "$F" ""                  "dds/${F%.*}.dds" ""                  "$dds_flags"
169                         cached "$do_jpeg" reduce_rgb_jpeg    "$F" ""                  "${F%.*}.jpg"     ""                  "$jpeg_qual_rgb"
170                         rm -f "$F"
171                 fi
172                 rm -f "$F.hasalpha"
173                 ;;
174         *.ogg)
175                 cached "$do_ogg" reduce_ogg "$F" "" "$F" "" "$ogg_qual"
176                 ;;
177         esac
178 done