5 : ${CACHEDIR:=$HOME/.xonotic-cached-converter}
7 : ${do_jpeg_if_not_dds:=false}
11 : ${dds_tool:=compressonator-dxtc}
16 : ${dds_noalpha:=dxt1}
17 : ${dds_prealpha:=dxt4}
18 : ${dds_sepalpha:=dxt5}
20 selfprofile_t0=`date +%s`
24 selfprofile_t=`date +%s`
25 eval "selfprofile_counter_$selfprofile_step=\$((\$selfprofile_counter_$selfprofile_step+$selfprofile_t))"
27 eval "selfprofile_counter_$selfprofile_step=\$((\$selfprofile_counter_$selfprofile_step-$selfprofile_t))"
28 selfprofile_t0=$selfprofile_t
41 tmpdir=`mktemp -d -t cached-converter.XXXXXX`
43 trap 'rm -rf "$tmpdir"' EXIT
46 use_magnet_to_acquire_checksum_faster()
48 # ,--'' ~~~~~~~^^^~._ '.
49 # ,.-' ~~~~~~~~~~^^^^^~~~._._ \
50 # | /^^^^^| /^^^^^^^^\\ \ \
51 # ,/___ < o> < (OO) > _ \
53 # |(|-'^^;,- ,| __ ^~~^^^^^^^; |\
54 # \\` | <;_ __ |`--- ..-^^/- | ||
55 # \`-|Oq-.____`________~='^^|__,/ ' //
56 # \ || | | | | \ ..-;| / '/
57 # | ||#|#|the|==|game!|'^` |/' /'
58 # | \\\\^\***|***| \ ,,;' /
59 # | `-=\_\__\___\__..-' ,.- - ,/
60 # | . `-_ ------ _,-'^-'^,-'
61 # | `-._________..--''^,-''^
63 # `----------'^ PROBLEM?
65 magnet=`GIT_DIR="$git_src_repo/.git" git ls-files -s "$1"`
66 if [ -n "$magnet" ]; then
71 sum=$sum`git hash-object "$1"`
89 if [ x"$infile1" = x"$outfile1" ]; then
92 options=`echo "$*" | git hash-object --stdin`
93 selfprofile convert_findchecksum
94 if [ x"$infile1/../$infile2" = x"$lastinfiles" ]; then
98 for infile in "$infile1" "$infile2"; do
100 */background_l2.tga|*/background_ingame_l2.tga)
105 if [ -n "$git_src_repo" ] && ! $evil; then
107 use_magnet_to_acquire_checksum_faster "${infile1#./}"
108 if [ -n "$infile2" ]; then
109 use_magnet_to_acquire_checksum_faster "${infile2#./}"
112 sum=`git hash-object "$infile1"`
113 if [ -n "$infile2" ]; then
114 sum=$sum`git hash-object "$infile2"`
119 selfprofile convert_makecachedir
120 mkdir -p "$CACHEDIR/$method-$options"
121 name1="$CACHEDIR/$method-$options/$sum-1.${outfile1##*.}"
122 [ -z "$outfile2" ] || name2="$CACHEDIR/$method-$options/$sum-2.${outfile2##*.}"
123 tempfile1="${name1%/*}/new-${name1##*/}"
124 [ -z "$outfile2" ] || tempfile2="${name2%/*}/new-${name2##*/}"
125 if [ -f "$name1" ] && { [ -z "$outfile2" ] || [ -f "$name2" ]; }; then
126 selfprofile convert_copyoutput
127 case "$outfile1" in */*) mkdir -p "${outfile1%/*}"; esac && { ln -f "$name1" "$outfile1" 2>/dev/null || { rm -f "$outfile1" && cp "$name1" "$outfile1"; }; }
128 [ -z "$outfile2" ] || { case "$outfile2" in */*) mkdir -p "${outfile2%/*}"; esac && { ln -f "$name2" "$outfile2" 2>/dev/null || { rm -f "$outfile2" && cp "$name2" "$outfile2"; }; }; }
130 elif selfprofile convert_makeoutput; "$method" "$infile1" "$infile2" "$tempfile1" "$tempfile2" "$@"; then
131 mv "$tempfile1" "$name1"
132 [ -z "$outfile2" ] || mv "$tempfile2" "$name2"
133 case "$outfile1" in */*) mkdir -p "${outfile1%/*}"; esac && { ln -f "$name1" "$outfile1" 2>/dev/null || { rm -f "$outfile1" && cp "$name1" "$outfile1"; }; }
134 [ -z "$outfile2" ] || { case "$outfile2" in */*) mkdir -p "${outfile2%/*}"; esac && { ln -f "$name2" "$outfile2" 2>/dev/null || { rm -f "$outfile2" && cp "$name2" "$outfile2"; }; }; }
137 selfprofile convert_cleartemp
140 selfprofile convert_finished
143 selfprofile convert_finished
151 convert "$i" "$ia" -compose CopyOpacity -composite "$tmpdir/x.tga" && \
152 "$meprefix"compress-texture "$dds_tool" "$dds_sepalpha" "$tmpdir/x.tga" "$o" $1
155 reduce_jpeg2_dds_premul()
160 convert "$i" "$ia" -compose CopyOpacity -composite "$tmpdir/x.tga" && \
161 "$meprefix"compress-texture "$dds_tool" "$dds_prealpha" "$tmpdir/x.tga" "$o" $1
170 if convert "$i" TGA:- | cjpeg -targa -quality "$1" -optimize -sample 1x1,1x1,1x1 > "$o"; then
171 if [ "`stat -c %s "$i"`" -lt "`stat -c %s "$o"`" ]; then
177 if convert "$ia" TGA:- | cjpeg -targa -quality "$2" -optimize -sample 1x1,1x1,1x1 > "$oa"; then
178 if [ "`stat -c %s "$ia"`" -lt "`stat -c %s "$oa"`" ]; then
190 if convert "$i" TGA:- | cjpeg -targa -quality "$1" -optimize -sample 1x1,1x1,1x1 > "$o"; then
191 if [ "`stat -c %s "$i"`" -lt "`stat -c %s "$o"`" ]; then
203 tags=`vorbiscomment -R -l "$i" || true`
204 oggdec -o "$tmpdir/x.wav" "$i" && \
205 oggenc -q"$1" -o "$o" "$tmpdir/x.wav"
206 echo "$tags" | vorbiscomment -R -w "$o" || true
213 oggenc -q"$1" -o "$o" "$i"
220 convert "$i" "$tmpdir/x.tga" && \
221 "$meprefix"compress-texture "$dds_tool" "$dds_sepalpha" "$tmpdir/x.tga" "$o" $1
224 reduce_rgba_dds_premul()
228 convert "$i" "$tmpdir/x.tga" && \
229 "$meprefix"compress-texture "$dds_tool" "$dds_prealpha" "$tmpdir/x.tga" "$o" $1
237 if convert "$i" -alpha off TGA:- | cjpeg -targa -quality "$1" -optimize -sample 1x1,1x1,1x1 > "$o"; then
242 if convert "$i" -alpha extract TGA:- | cjpeg -targa -quality "$2" -optimize -sample 1x1,1x1,1x1 > "$oa"; then
253 convert "$i" "$tmpdir/x.tga" && \
254 "$meprefix"compress-texture "$dds_tool" "$dds_noalpha" "$tmpdir/x.tga" "$o" $1
261 if convert "$i" TGA:- | cjpeg -targa -quality "$1" -optimize -sample 1x1,1x1,1x1 > "$o"; then
272 if convert "$i" -depth 16 RGBA:- | perl -e 'while(read STDIN, $_, 8) { substr($_, 6, 2) eq "\xFF\xFF" or exit 1; } exit 0;'; then
283 selfprofile prepareconvert
286 echo >&2 "Handling $F..."
289 jqual_rgb=$jpeg_qual_rgb
296 ./sounds/misc/talk*.wav) will_ogg=false ;; # engine "feature"
297 *_bump) will_dds=false ;;
298 ./models/player/*) will_dds=false ;;
299 ./models/sprites/*) will_dds=false ;;
305 # we can't DDS compress the 2D textures, sorry
306 # but JPEG is still fine
311 # for deluxemaps, lightmaps and normalmaps, enforce high jpeg quality (like on alpha channels)
312 if [ "$jqual_a" -gt "$jqual_rgb" ]; then
314 ./maps/*/lm_[0-9][0-9][0-9][13579]) # deluxemap
317 ./maps/*/lm_[0-9][0-9][0-9][02468]) # lightmap
328 ./particles/particlefont) # particlefont uses premultiplied alpha
333 if $do_jpeg_if_not_dds; then
340 selfprofile startconvert
343 # handle in *.jpg case
345 # they always got converted, I assume
346 if $will_dds || $will_jpeg; then
352 if [ -f "${f}_alpha.jpg" ]; then
353 cached "$will_dds" reduce_jpeg2_dds$pm "$F" "${f}_alpha.jpg" "dds/${f}.dds" "" "$dds_flags"
354 cached "$will_jpeg" reduce_jpeg2_jpeg2 "$F" "${f}_alpha.jpg" "$F" "${f}_alpha.jpg" "$jqual_rgb" "$jqual_a"
356 cached "$will_dds" reduce_rgb_dds "$F" "" "dds/${f}.dds" "" "$dds_flags"
357 cached "$will_jpeg" reduce_jpeg_jpeg "$F" "" "$F" "" "$jqual_rgb"
361 cached true has_alpha "$F" "" "$F.hasalpha" ""
363 if [ -s "$F.hasalpha" ]; then
364 cached "$will_dds" reduce_rgba_dds$pm "$F" "" "dds/${f}.dds" "" "$dds_flags"
365 cached "$will_jpeg" reduce_rgba_jpeg2 "$F" "" "${f}.jpg" "${f}_alpha.jpg" "$jqual_rgb" "$jqual_a"
367 cached "$will_dds" reduce_rgb_dds "$F" "" "dds/${f}.dds" "" "$dds_flags"
368 cached "$will_jpeg" reduce_rgb_jpeg "$F" "" "${f}.jpg" "" "$jqual_rgb"
373 cached "$will_ogg" reduce_ogg_ogg "$F" "" "$F" "" "$ogg_qual"
375 ./sound/misc/null.wav)
376 # never convert this one
379 cached "$will_ogg" reduce_wav_ogg "$F" "" "${f}.ogg" "" "$ogg_qual"
382 selfprofile marktodelete
386 # FIXME can't have spaces in filenames that way
387 to_delete="$to_delete $F"
391 selfprofile symlinkfixing
392 # fix up DDS paths by a symbolic link
393 if [ -f "dds/${f}.dds" ]; then
394 if [ -z "${f##./textures/*}" ]; then
395 if [ -n "${f##./textures/*/*}" ]; then
396 ln -snf "textures/${f#./textures/}.dds" "dds/${f#./textures/}.dds"
403 for F in $to_delete; do
406 selfprofile finished_time
407 set | grep ^selfprofile_counter_ >&2