5 : ${CACHEDIR:=$HOME/.xonotic-cached-converter}
7 : ${do_jpeg_if_not_dds:=false}
11 : ${do_webp_if_not_dds:=false}
12 : ${webp_flags_lq:=-lossless -q 100}
13 : ${webp_flags_hq:=-lossless -q 100}
14 : ${webp_flags_alq:=-lossless -q 100 -q_alpha 100}
15 : ${webp_flags_ahq:=-lossless -q 100 -q_alpha 100}
23 : ${dds_noalpha:=dxt1}
24 : ${dds_prealpha:=dxt2 dxt4}
25 : ${dds_sepalpha:=dxt3 dxt5}
27 selfprofile_t0=`date +%s`
31 selfprofile_t=`date +%s`
32 eval "selfprofile_counter_$selfprofile_step=\$((\$selfprofile_counter_$selfprofile_step+$selfprofile_t))"
34 eval "selfprofile_counter_$selfprofile_step=\$((\$selfprofile_counter_$selfprofile_step-$selfprofile_t))"
35 selfprofile_t0=$selfprofile_t
48 tmpdir=`mktemp -d -t cached-converter.XXXXXX`
50 trap 'rm -rf "$tmpdir"' EXIT
53 use_magnet_to_acquire_checksum_faster()
55 # ,--'' ~~~~~~~^^^~._ '.
56 # ,.-' ~~~~~~~~~~^^^^^~~~._._ \
57 # | /^^^^^| /^^^^^^^^\\ \ \
58 # ,/___ < o> < (OO) > _ \
60 # |(|-'^^;,- ,| __ ^~~^^^^^^^; |\
61 # \\` | <;_ __ |`--- ..-^^/- | ||
62 # \`-|Oq-.____`________~='^^|__,/ ' //
63 # \ || | | | | \ ..-;| / '/
64 # | ||#|#|the|==|game!|'^` |/' /'
65 # | \\\\^\***|***| \ ,,;' /
66 # | `-=\_\__\___\__..-' ,.- - ,/
67 # | . `-_ ------ _,-'^-'^,-'
68 # | `-._________..--''^,-''^
70 # `----------'^ PROBLEM?
72 magnet=`GIT_DIR="$git_src_repo/.git" git ls-files -s "$1"`
73 if [ -n "$magnet" ]; then
86 if [ x"$1/../$2" = x"$lastinfiles" ]; then
90 for _a_f in "$1" "$2"; do
92 */background_l2.tga|*/background_ingame_l2.tga)
97 if [ -n "$git_src_repo" ] && ! $_a_e; then
98 _a_s=`use_magnet_to_acquire_checksum_faster "${1#./}"`
100 _a_s=$_a_s`use_magnet_to_acquire_checksum_faster "${2#./}"`
103 _a_s=`git hash-object "$1"`
105 _a_s=$_a_s`git hash-object "$2"`
108 lastinfileshash=$_a_s
109 lastinfiles="$1/../$2"
126 if [ x"$infile1" = x"$outfile1" ]; then
129 options=`echo "$*" | git hash-object --stdin`
130 selfprofile convert_findchecksum
131 sum=`acquire_checksum "$infile1" "$infile2"`
132 selfprofile convert_makecachedir
133 mkdir -p "$CACHEDIR/$method-$options"
134 name1="$CACHEDIR/$method-$options/$sum-1.${outfile1##*.}"
135 [ -z "$outfile2" ] || name2="$CACHEDIR/$method-$options/$sum-2.${outfile2##*.}"
136 tempfile1="${name1%/*}/new-${name1##*/}"
137 [ -z "$outfile2" ] || tempfile2="${name2%/*}/new-${name2##*/}"
138 if [ -f "$name1" ] && { [ -z "$outfile2" ] || [ -f "$name2" ]; }; then
139 selfprofile convert_copyoutput
140 case "$outfile1" in */*) mkdir -p "${outfile1%/*}"; esac && { ln -f "$name1" "$outfile1" 2>/dev/null || { rm -f "$outfile1" && cp "$name1" "$outfile1"; }; }
141 [ -z "$outfile2" ] || { case "$outfile2" in */*) mkdir -p "${outfile2%/*}"; esac && { ln -f "$name2" "$outfile2" 2>/dev/null || { rm -f "$outfile2" && cp "$name2" "$outfile2"; }; }; }
143 elif selfprofile convert_makeoutput; "$method" "$infile1" "$infile2" "$tempfile1" "$tempfile2" "$@"; then
144 mv "$tempfile1" "$name1"
145 [ -z "$outfile2" ] || mv "$tempfile2" "$name2"
146 case "$outfile1" in */*) mkdir -p "${outfile1%/*}"; esac && { ln -f "$name1" "$outfile1" 2>/dev/null || { rm -f "$outfile1" && cp "$name1" "$outfile1"; }; }
147 [ -z "$outfile2" ] || { case "$outfile2" in */*) mkdir -p "${outfile2%/*}"; esac && { ln -f "$name2" "$outfile2" 2>/dev/null || { rm -f "$outfile2" && cp "$name2" "$outfile2"; }; }; }
150 selfprofile convert_cleartemp
153 selfprofile convert_finished
156 selfprofile convert_finished
165 for pd_dd in $pd_d; do
166 if [ -f "$pd_o" ]; then
167 "$meprefix"compress-texture "$pd_t" "$pd_dd" "$pd_i" "$pd_o".tmp.dds "$@"
168 pd_psnr_tmp=`compare -channel alpha -metric PSNR "$pd_i" "$pd_o".tmp.dds NULL: 2>&1`
169 case "$pd_psnr_tmp" in
176 echo >&2 "$pd_dd: $pd_psnr_tmp dB"
177 pd_psnr_diff=`echo "($pd_psnr_tmp) - ($pd_psnr)" | bc -l`
178 case "$pd_psnr_diff" in
180 # tmp is smaller or equal
181 # smaller PSNR is worse
186 # larger PSNR is better
188 mv "$pd_o".tmp.dds "$pd_o"
189 echo >&2 "PICKED (better)"
193 "$meprefix"compress-texture "$pd_t" "$pd_dd" "$pd_i" "$pd_o" "$@"
194 pd_psnr=`compare -channel alpha -metric PSNR "$pd_i" "$pd_o" NULL: 2>&1`
202 echo >&2 "$pd_dd: $pd_psnr dB"
203 echo >&2 "PICKED (first)"
213 convert "$i" "$ia" -compose CopyOpacity -composite -type TrueColorMatte "$tmpdir/x.tga" && \
214 pickdxta "$dds_tool" "$dds_sepalpha" "$tmpdir/x.tga" "$o" $1
217 reduce_jpeg2_dds_premul()
222 convert "$i" "$ia" -compose CopyOpacity -composite -type TrueColorMatte "$tmpdir/x.tga" && \
223 pickdxta "$dds_tool" "$dds_prealpha" "$tmpdir/x.tga" "$o" $1
232 if convert "$i" TGA:- | cjpeg -targa -quality "$1" -optimize -sample 1x1,1x1,1x1 > "$o"; then
233 if [ "`stat -c %s "$i"`" -lt "`stat -c %s "$o"`" ]; then
239 if convert "$ia" TGA:- | cjpeg -targa -quality "$2" -optimize -sample 1x1,1x1,1x1 > "$oa"; then
240 if [ "`stat -c %s "$ia"`" -lt "`stat -c %s "$oa"`" ]; then
254 convert "$i" "$ia" -compose CopyOpacity -composite -type TrueColorMatte "$tmpdir/x.png" && \
255 cwebp $1 "$tmpdir/x.png" -o "$o"
262 if convert "$i" TGA:- | cjpeg -targa -quality "$1" -optimize -sample 1x1,1x1,1x1 > "$o"; then
263 if [ "`stat -c %s "$i"`" -lt "`stat -c %s "$o"`" ]; then
275 tags=`vorbiscomment -R -l "$i" || true`
276 oggdec -o "$tmpdir/x.wav" "$i" && \
277 oggenc -q"$1" -o "$o" "$tmpdir/x.wav"
278 echo "$tags" | vorbiscomment -R -w "$o" || true
285 oggenc -q"$1" -o "$o" "$i"
292 convert "$i" -type TrueColorMatte "$tmpdir/x.tga" && \
293 pickdxta "$dds_tool" "$dds_sepalpha" "$tmpdir/x.tga" "$o" $1
296 reduce_rgba_dds_premul()
300 convert "$i" -type TrueColorMatte "$tmpdir/x.tga" && \
301 pickdxta "$dds_tool" "$dds_prealpha" "$tmpdir/x.tga" "$o" $1
309 if convert "$i" -alpha off TGA:- | cjpeg -targa -quality "$1" -optimize -sample 1x1,1x1,1x1 > "$o"; then
314 if convert "$i" -alpha extract TGA:- | cjpeg -targa -quality "$2" -optimize -sample 1x1,1x1,1x1 > "$oa"; then
325 convert "$i" -type TrueColor "$tmpdir/x.tga" && \
326 "$meprefix"compress-texture "$dds_tool" "$dds_noalpha" "$tmpdir/x.tga" "$o" $1
333 if convert "$i" TGA:- | cjpeg -targa -quality "$1" -optimize -sample 1x1,1x1,1x1 > "$o"; then
344 convert "$i" "$tmpdir/x.png" && \
345 cwebp $1 "$tmpdir/x.png" -o "$o"
352 if convert "$i" -depth 16 RGBA:- | perl -e 'while(read STDIN, $_, 8) { substr($_, 6, 2) eq "\xFF\xFF" or exit 1; } exit 0;'; then
363 selfprofile prepareconvert
366 echo >&2 "Handling $F..."
369 jqual_rgb=$jpeg_qual_rgb
379 *.ogg) will_ogg=false ;;
383 ./sound/misc/talk*.wav) will_ogg=false ;; # engine "feature"
384 *_bump.*) will_dds=false ;;
385 ./models/player/*) will_dds=false ;;
386 ./models/sprites/*) will_dds=false ;;
393 # we can't DDS compress the 2D textures, sorry
394 # but JPEG is still fine
402 export S2TC_COLORDIST_MODE=NORMALMAP
403 export S2TC_RANDOM_COLORS=256
404 export S2TC_REFINE_COLORS=LOOP
407 export S2TC_COLORDIST_MODE=SRGB_MIXED
408 export S2TC_RANDOM_COLORS=64
409 export S2TC_REFINE_COLORS=LOOP
413 # for deluxemaps, lightmaps and normalmaps, enforce high jpeg quality (like on alpha channels)
414 if [ "$jqual_a" -gt "$jqual_rgb" ]; then
416 ./maps/*/lm_[0-9][0-9][0-9][13579]) # deluxemap
420 ./maps/*/lm_[0-9][0-9][0-9][02468]) # lightmap
433 ./particles/particlefont) # particlefont uses premultiplied alpha
438 if $do_jpeg_if_not_dds; then
445 if $do_webp_if_not_dds; then
452 selfprofile startconvert
455 # handle in *.jpg case
457 # they always got converted, I assume
458 if $will_dds || $will_jpeg || $will_webp; then
464 if [ -f "${f}_alpha.jpg" ]; then
465 cached "$will_dds" reduce_jpeg2_dds$pm "$F" "${f}_alpha.jpg" "dds/${f}.dds" "" "$dds_flags"
466 cached "$will_jpeg" reduce_jpeg2_jpeg2 "$F" "${f}_alpha.jpg" "$F" "${f}_alpha.jpg" "$jqual_rgb" "$jqual_a"
467 #eval wflags=\$webp_flags_${webp_mode}a
468 #cached "$will_webp" reduce_jpeg2_webp "$F" "${f}_alpha.jpg" "${f}.webp" "" "$wflags"
470 cached "$will_dds" reduce_rgb_dds "$F" "" "dds/${f}.dds" "" "$dds_flags"
471 cached "$will_jpeg" reduce_jpeg_jpeg "$F" "" "$F" "" "$jqual_rgb"
472 #eval wflags=\$webp_flags_${webp_mode}
473 #cached "$will_webp" reduce_rgba_webp "$F" "" "${f}.webp" "" "$wflags"
477 cached true has_alpha "$F" "" "$F.hasalpha" ""
479 if [ -s "$F.hasalpha" ]; then
480 cached "$will_dds" reduce_rgba_dds$pm "$F" "" "dds/${f}.dds" "" "$dds_flags"
481 cached "$will_jpeg" reduce_rgba_jpeg2 "$F" "" "${f}.jpg" "${f}_alpha.jpg" "$jqual_rgb" "$jqual_a"
482 eval wflags=\$webp_flags_${webp_mode}a
483 cached "$will_webp" reduce_rgba_webp "$F" "" "${f}.webp" "" "$wflags"
485 cached "$will_dds" reduce_rgb_dds "$F" "" "dds/${f}.dds" "" "$dds_flags"
486 cached "$will_jpeg" reduce_rgb_jpeg "$F" "" "${f}.jpg" "" "$jqual_rgb"
487 eval wflags=\$webp_flags_${webp_mode}
488 cached "$will_webp" reduce_rgba_webp "$F" "" "${f}.webp" "" "$wflags"
493 cached "$will_ogg" reduce_ogg_ogg "$F" "" "$F" "" "$ogg_qual"
495 ./sound/misc/null.wav)
496 # never convert this one
499 cached "$will_ogg" reduce_wav_ogg "$F" "" "${f}.ogg" "" "$ogg_qual"
502 selfprofile marktodelete
506 # FIXME can't have spaces in filenames that way
507 to_delete="$to_delete $F"
511 selfprofile symlinkfixing
512 # fix up DDS paths by a symbolic link
513 if [ -f "dds/${f}.dds" ]; then
514 if [ -z "${f##./textures/*}" ]; then
515 if [ -n "${f##./textures/*/*}" ]; then
516 ln -snf "textures/${f#./textures/}.dds" "dds/${f#./textures/}.dds"
523 for F in $to_delete; do
526 selfprofile finished_time
527 set | grep ^selfprofile_counter_ >&2