5 : ${CACHEDIR:=$HOME/.xonotic-cached-converter}
7 : ${do_jpeg_if_not_dds:=false}
11 : ${dds_tool:=compressonator-dxtc}
27 tmpdir=`mktemp -d -t cached-converter.XXXXXX`
29 trap 'rm -rf "$tmpdir"' EXIT
45 if [ x"$infile1" = x"$outfile1" ]; then
48 options=`echo "$*" | git hash-object --stdin`
49 if [ x"$infile1/../$infile2" = x"$lastinfiles" ]; then
52 if [ -n "$git_src_repo" ]; then
53 sum=`( cd "$git_src_repo"; git rev-parse --revs-only HEAD:"${infile1#./}" | grep . ) || git hash-object "$infile1"`
54 if [ -n "$infile2" ]; then
55 sum=$sum`( cd "$git_src_repo"; git rev-parse --revs-only HEAD:"${infile2#./}" | grep . ) || git hash-object "$infile2"`
58 sum=`git hash-object "$infile1"`
59 if [ -n "$infile2" ]; then
60 sum=$sum`git hash-object "$infile2"`
65 mkdir -p "$CACHEDIR/$method-$options"
66 name1="$CACHEDIR/$method-$options/$sum-1.${outfile1##*.}"
67 [ -z "$outfile2" ] || name2="$CACHEDIR/$method-$options/$sum-2.${outfile2##*.}"
68 tempfile1="${name1%/*}/new-${name1##*/}"
69 [ -z "$outfile2" ] || tempfile2="${name2%/*}/new-${name2##*/}"
70 if [ -f "$name1" ] && { [ -z "$outfile2" ] || [ -f "$name2" ]; }; then
71 case "$outfile1" in */*) mkdir -p "${outfile1%/*}"; esac && { ln "$name1" "$outfile1" 2>/dev/null || cp "$name1" "$outfile1"; }
72 [ -z "$outfile2" ] || { case "$outfile2" in */*) mkdir -p "${outfile2%/*}"; esac && { ln "$name2" "$outfile2" 2>/dev/null || cp "$name2" "$outfile2"; }; }
74 elif "$method" "$infile1" "$infile2" "$tempfile1" "$tempfile2" "$@"; then
75 mv "$tempfile1" "$name1"
76 [ -z "$outfile2" ] || mv "$tempfile2" "$name2"
77 case "$outfile1" in */*) mkdir -p "${outfile1%/*}"; esac && { ln "$name1" "$outfile1" 2>/dev/null || cp "$name1" "$outfile1"; }
78 [ -z "$outfile2" ] || { case "$outfile2" in */*) mkdir -p "${outfile2%/*}"; esac && { ln "$name2" "$outfile2" 2>/dev/null || cp "$name2" "$outfile2"; }; }
92 convert "$i" "$ia" -compose CopyOpacity -composite "$tmpdir/x.tga" && \
93 "$meprefix"compress-texture "$dds_tool" dxt5 "$tmpdir/x.tga" "$o" $1
96 reduce_jpeg2_dds_premul()
101 convert "$i" "$ia" -compose CopyOpacity -composite "$tmpdir/x.tga" && \
102 "$meprefix"compress-texture "$dds_tool" dxt4 "$tmpdir/x.tga" "$o" $1
111 if convert "$i" TGA:- | cjpeg -targa -quality "$1" -optimize -sample 1x1,1x1,1x1 > "$o"; then
112 if [ "`stat -c %s "$i"`" -lt "`stat -c %s "$o"`" ]; then
118 if convert "$ia" TGA:- | cjpeg -targa -quality "$2" -optimize -sample 1x1,1x1,1x1 > "$oa"; then
119 if [ "`stat -c %s "$ia"`" -lt "`stat -c %s "$oa"`" ]; then
131 if convert "$i" TGA:- | cjpeg -targa -quality "$1" -optimize -sample 1x1,1x1,1x1 > "$o"; then
132 if [ "`stat -c %s "$i"`" -lt "`stat -c %s "$o"`" ]; then
144 tags=`vorbiscomment -R -l "$i" || true`
145 oggdec -o "$tmpdir/x.wav" "$i" && \
146 oggenc -q"$1" -o "$o" "$tmpdir/x.wav"
147 echo "$tags" | vorbiscomment -R -w "$o" || true
154 oggenc -q"$1" -o "$o" "$i"
161 convert "$i" "$tmpdir/x.tga" && \
162 "$meprefix"compress-texture "$dds_tool" dxt5 "$tmpdir/x.tga" "$o" $1
165 reduce_rgba_dds_premul()
169 convert "$i" "$tmpdir/x.tga" && \
170 "$meprefix"compress-texture "$dds_tool" dxt4 "$tmpdir/x.tga" "$o" $1
178 if convert "$i" -alpha off TGA:- | cjpeg -targa -quality "$1" -optimize -sample 1x1,1x1,1x1 > "$o"; then
183 if convert "$i" -alpha extract TGA:- | cjpeg -targa -quality "$2" -optimize -sample 1x1,1x1,1x1 > "$oa"; then
194 convert "$i" "$tmpdir/x.tga" && \
195 "$meprefix"compress-texture "$dds_tool" dxt1 "$tmpdir/x.tga" "$o" $1
202 if convert "$i" TGA:- | cjpeg -targa -quality "$1" -optimize -sample 1x1,1x1,1x1 > "$o"; then
213 if convert "$i" -depth 16 RGBA:- | perl -e 'while(read STDIN, $_, 8) { substr($_, 6, 2) eq "\xFF\xFF" or exit 1; } exit 0;'; then
226 echo >&2 "Handling $F..."
229 jqual_rgb=$jpeg_qual_rgb
235 *_bump) will_dds=false ;;
236 ./models/player/*) will_dds=false ;;
237 ./models/sprites/*) will_dds=false ;;
243 # we can't DDS compress the 2D textures, sorry
244 # but JPEG is still fine
249 # for deluxemaps, lightmaps and normalmaps, enforce high jpeg quality (like on alpha channels)
250 if [ "$jqual_a" -gt "$jqual_rgb" ]; then
252 ./maps/*/lm_[0-9][0-9][0-9][13579]) # deluxemap
255 ./maps/*/lm_[0-9][0-9][0-9][02468]) # lightmap
266 ./particles/particlefont) # particlefont uses premultiplied alpha
271 if $do_jpeg_if_not_dds; then
281 # handle in *.jpg case
283 # they always got converted, I assume
284 if $will_dds || $will_jpeg; then
290 if [ -f "${f}_alpha.jpg" ]; then
291 cached "$will_dds" reduce_jpeg2_dds$pm "$F" "${f}_alpha.jpg" "dds/${f}.dds" "" "$dds_flags"
292 cached "$will_jpeg" reduce_jpeg2_jpeg2 "$F" "${f}_alpha.jpg" "$F" "${f}_alpha.jpg" "$jqual_rgb" "$jqual_a"
294 cached "$will_dds" reduce_rgb_dds "$F" "" "dds/${f}.dds" "" "$dds_flags"
295 cached "$will_jpeg" reduce_jpeg_jpeg "$F" "" "$F" "" "$jqual_rgb"
299 cached true has_alpha "$F" "" "$F.hasalpha" ""
301 if [ -s "$F.hasalpha" ]; then
302 cached "$will_dds" reduce_rgba_dds$pm "$F" "" "dds/${f}.dds" "" "$dds_flags"
303 cached "$will_jpeg" reduce_rgba_jpeg2 "$F" "" "${f}.jpg" "${f}_alpha.jpg" "$jqual_rgb" "$jqual_a"
305 cached "$will_dds" reduce_rgb_dds "$F" "" "dds/${f}.dds" "" "$dds_flags"
306 cached "$will_jpeg" reduce_rgb_jpeg "$F" "" "${f}.jpg" "" "$jqual_rgb"
311 cached "$do_ogg" reduce_ogg_ogg "$F" "" "$F" "" "$ogg_qual"
313 ./sound/misc/null.wav)
314 # never convert this one
317 cached "$do_ogg" reduce_wav_ogg "$F" "" "${f}.ogg" "" "$ogg_qual"
323 # FIXME can't have spaces in filenames that way
324 to_delete="$to_delete $F"
328 # fix up DDS paths by a symbolic link
329 if [ -f "dds/${f}.dds" ]; then
330 if [ -z "${f##./textures/*}" ]; then
331 if [ -n "${f##./textures/*/*}" ]; then
332 ln -snf "textures/${f#./textures/}.dds" "dds/${f#./textures/}.dds"
337 for F in $to_delete; do