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}
17 : ${dds_tool:=compressonator-dxtc}
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.tga" && \
255 cwebp $1 "$tmpdir/x.tga" -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 cwebp $1 "$i" -o "$o"
351 if convert "$i" -depth 16 RGBA:- | perl -e 'while(read STDIN, $_, 8) { substr($_, 6, 2) eq "\xFF\xFF" or exit 1; } exit 0;'; then
362 selfprofile prepareconvert
365 echo >&2 "Handling $F..."
368 jqual_rgb=$jpeg_qual_rgb
378 *.ogg) will_ogg=false ;;
382 ./sounds/misc/talk*.wav) will_ogg=false ;; # engine "feature"
383 *_bump) will_dds=false ;;
384 ./models/player/*) will_dds=false ;;
385 ./models/sprites/*) will_dds=false ;;
391 # we can't DDS compress the 2D textures, sorry
392 # but JPEG is still fine
400 export S2TC_COLORDIST_MODE=NORMALMAP
401 export S2TC_RANDOM_COLORS=256
402 export S2TC_REFINE_COLORS=LOOP
405 export S2TC_COLORDIST_MODE=SRGB_MIXED
406 export S2TC_RANDOM_COLORS=64
407 export S2TC_REFINE_COLORS=LOOP
411 # for deluxemaps, lightmaps and normalmaps, enforce high jpeg quality (like on alpha channels)
412 if [ "$jqual_a" -gt "$jqual_rgb" ]; then
414 ./maps/*/lm_[0-9][0-9][0-9][13579]) # deluxemap
418 ./maps/*/lm_[0-9][0-9][0-9][02468]) # lightmap
431 ./particles/particlefont) # particlefont uses premultiplied alpha
436 if $do_jpeg_if_not_dds; then
443 if $do_webp_if_not_dds; then
450 selfprofile startconvert
453 # handle in *.jpg case
455 # they always got converted, I assume
456 if $will_dds || $will_jpeg || $will_webp; then
462 if [ -f "${f}_alpha.jpg" ]; then
463 cached "$will_dds" reduce_jpeg2_dds$pm "$F" "${f}_alpha.jpg" "dds/${f}.dds" "" "$dds_flags"
464 cached "$will_jpeg" reduce_jpeg2_jpeg2 "$F" "${f}_alpha.jpg" "$F" "${f}_alpha.jpg" "$jqual_rgb" "$jqual_a"
465 #eval wflags=\$webp_flags_${webp_mode}a
466 #cached "$will_webp" reduce_jpeg2_webp "$F" "${f}_alpha.jpg" "${f}.webp" "" "$wflags"
468 cached "$will_dds" reduce_rgb_dds "$F" "" "dds/${f}.dds" "" "$dds_flags"
469 cached "$will_jpeg" reduce_jpeg_jpeg "$F" "" "$F" "" "$jqual_rgb"
470 #eval wflags=\$webp_flags_${webp_mode}
471 #cached "$will_webp" reduce_rgba_webp "$F" "" "${f}.webp" "" "$wflags"
475 cached true has_alpha "$F" "" "$F.hasalpha" ""
477 if [ -s "$F.hasalpha" ]; then
478 cached "$will_dds" reduce_rgba_dds$pm "$F" "" "dds/${f}.dds" "" "$dds_flags"
479 cached "$will_jpeg" reduce_rgba_jpeg2 "$F" "" "${f}.jpg" "${f}_alpha.jpg" "$jqual_rgb" "$jqual_a"
480 eval wflags=\$webp_flags_${webp_mode}a
481 cached "$will_webp" reduce_rgba_webp "$F" "" "${f}.webp" "" "$wflags"
483 cached "$will_dds" reduce_rgb_dds "$F" "" "dds/${f}.dds" "" "$dds_flags"
484 cached "$will_jpeg" reduce_rgb_jpeg "$F" "" "${f}.jpg" "" "$jqual_rgb"
485 eval wflags=\$webp_flags_${webp_mode}
486 cached "$will_webp" reduce_rgba_webp "$F" "" "${f}.webp" "" "$wflags"
491 cached "$will_ogg" reduce_ogg_ogg "$F" "" "$F" "" "$ogg_qual"
493 ./sound/misc/null.wav)
494 # never convert this one
497 cached "$will_ogg" reduce_wav_ogg "$F" "" "${f}.ogg" "" "$ogg_qual"
500 selfprofile marktodelete
504 # FIXME can't have spaces in filenames that way
505 to_delete="$to_delete $F"
509 selfprofile symlinkfixing
510 # fix up DDS paths by a symbolic link
511 if [ -f "dds/${f}.dds" ]; then
512 if [ -z "${f##./textures/*}" ]; then
513 if [ -n "${f##./textures/*/*}" ]; then
514 ln -snf "textures/${f#./textures/}.dds" "dds/${f#./textures/}.dds"
521 for F in $to_delete; do
524 selfprofile finished_time
525 set | grep ^selfprofile_counter_ >&2