5 : ${CACHEDIR:=$HOME/.xonotic-cached-converter}
7 : ${do_jpeg_if_not_dds:=false}
11 : ${dds_tool:=compressonator-dxtc}
17 : ${dds_noalpha:=dxt1}
18 : ${dds_prealpha:=dxt2 dxt4}
19 : ${dds_sepalpha:=dxt3 dxt5}
21 selfprofile_t0=`date +%s`
25 selfprofile_t=`date +%s`
26 eval "selfprofile_counter_$selfprofile_step=\$((\$selfprofile_counter_$selfprofile_step+$selfprofile_t))"
28 eval "selfprofile_counter_$selfprofile_step=\$((\$selfprofile_counter_$selfprofile_step-$selfprofile_t))"
29 selfprofile_t0=$selfprofile_t
42 tmpdir=`mktemp -d -t cached-converter.XXXXXX`
44 trap 'rm -rf "$tmpdir"' EXIT
47 use_magnet_to_acquire_checksum_faster()
49 # ,--'' ~~~~~~~^^^~._ '.
50 # ,.-' ~~~~~~~~~~^^^^^~~~._._ \
51 # | /^^^^^| /^^^^^^^^\\ \ \
52 # ,/___ < o> < (OO) > _ \
54 # |(|-'^^;,- ,| __ ^~~^^^^^^^; |\
55 # \\` | <;_ __ |`--- ..-^^/- | ||
56 # \`-|Oq-.____`________~='^^|__,/ ' //
57 # \ || | | | | \ ..-;| / '/
58 # | ||#|#|the|==|game!|'^` |/' /'
59 # | \\\\^\***|***| \ ,,;' /
60 # | `-=\_\__\___\__..-' ,.- - ,/
61 # | . `-_ ------ _,-'^-'^,-'
62 # | `-._________..--''^,-''^
64 # `----------'^ PROBLEM?
66 magnet=`GIT_DIR="$git_src_repo/.git" git ls-files -s "$1"`
67 if [ -n "$magnet" ]; then
80 if [ x"$1/../$2" = x"$lastinfiles" ]; then
84 for _a_f in "$1" "$2"; do
86 */background_l2.tga|*/background_ingame_l2.tga)
91 if [ -n "$git_src_repo" ] && ! $_a_e; then
92 _a_s=`use_magnet_to_acquire_checksum_faster "${1#./}"`
94 _a_s=$_a_s`use_magnet_to_acquire_checksum_faster "${2#./}"`
97 _a_s=`git hash-object "$1"`
99 _a_s=$_a_s`git hash-object "$2"`
102 lastinfileshash=$_a_s
103 lastinfiles="$1/../$2"
120 if [ x"$infile1" = x"$outfile1" ]; then
123 options=`echo "$*" | git hash-object --stdin`
124 selfprofile convert_findchecksum
125 sum=`acquire_checksum "$infile1" "$infile2"`
126 selfprofile convert_makecachedir
127 mkdir -p "$CACHEDIR/$method-$options"
128 name1="$CACHEDIR/$method-$options/$sum-1.${outfile1##*.}"
129 [ -z "$outfile2" ] || name2="$CACHEDIR/$method-$options/$sum-2.${outfile2##*.}"
130 tempfile1="${name1%/*}/new-${name1##*/}"
131 [ -z "$outfile2" ] || tempfile2="${name2%/*}/new-${name2##*/}"
132 if [ -f "$name1" ] && { [ -z "$outfile2" ] || [ -f "$name2" ]; }; then
133 selfprofile convert_copyoutput
134 case "$outfile1" in */*) mkdir -p "${outfile1%/*}"; esac && { ln -f "$name1" "$outfile1" 2>/dev/null || { rm -f "$outfile1" && cp "$name1" "$outfile1"; }; }
135 [ -z "$outfile2" ] || { case "$outfile2" in */*) mkdir -p "${outfile2%/*}"; esac && { ln -f "$name2" "$outfile2" 2>/dev/null || { rm -f "$outfile2" && cp "$name2" "$outfile2"; }; }; }
137 elif selfprofile convert_makeoutput; "$method" "$infile1" "$infile2" "$tempfile1" "$tempfile2" "$@"; then
138 mv "$tempfile1" "$name1"
139 [ -z "$outfile2" ] || mv "$tempfile2" "$name2"
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"; }; }; }
144 selfprofile convert_cleartemp
147 selfprofile convert_finished
150 selfprofile convert_finished
159 for pd_dd in $pd_d; do
160 if [ -f "$pd_o" ]; then
161 "$meprefix"compress-texture "$pd_t" "$pd_dd" "$pd_i" "$pd_o".tmp.dds "$@"
162 pd_psnr_tmp=`compare -channel alpha -metric PSNR "$pd_i" "$pd_o".tmp.dds NULL: 2>&1`
163 case "$pd_psnr_tmp" in
170 echo >&2 "$pd_dd: $pd_psnr_tmp dB"
171 pd_psnr_diff=`echo "($pd_psnr_tmp) - ($pd_psnr)" | bc -l`
172 case "$pd_psnr_diff" in
174 # tmp is smaller or equal
175 # smaller PSNR is worse
180 # larger PSNR is better
182 mv "$pd_o".tmp.dds "$pd_o"
183 echo >&2 "PICKED (better)"
187 "$meprefix"compress-texture "$pd_t" "$pd_dd" "$pd_i" "$pd_o" "$@"
188 pd_psnr=`compare -channel alpha -metric PSNR "$pd_i" "$pd_o" NULL: 2>&1`
196 echo >&2 "$pd_dd: $pd_psnr dB"
197 echo >&2 "PICKED (first)"
207 convert "$i" "$ia" -compose CopyOpacity -composite "$tmpdir/x.tga" && \
208 pickdxta "$dds_tool" "$dds_sepalpha" "$tmpdir/x.tga" "$o" $1
211 reduce_jpeg2_dds_premul()
216 convert "$i" "$ia" -compose CopyOpacity -composite "$tmpdir/x.tga" && \
217 pickdxta "$dds_tool" "$dds_prealpha" "$tmpdir/x.tga" "$o" $1
226 if convert "$i" TGA:- | cjpeg -targa -quality "$1" -optimize -sample 1x1,1x1,1x1 > "$o"; then
227 if [ "`stat -c %s "$i"`" -lt "`stat -c %s "$o"`" ]; then
233 if convert "$ia" TGA:- | cjpeg -targa -quality "$2" -optimize -sample 1x1,1x1,1x1 > "$oa"; then
234 if [ "`stat -c %s "$ia"`" -lt "`stat -c %s "$oa"`" ]; then
246 if convert "$i" TGA:- | cjpeg -targa -quality "$1" -optimize -sample 1x1,1x1,1x1 > "$o"; then
247 if [ "`stat -c %s "$i"`" -lt "`stat -c %s "$o"`" ]; then
259 tags=`vorbiscomment -R -l "$i" || true`
260 oggdec -o "$tmpdir/x.wav" "$i" && \
261 oggenc -q"$1" -o "$o" "$tmpdir/x.wav"
262 echo "$tags" | vorbiscomment -R -w "$o" || true
269 oggenc -q"$1" -o "$o" "$i"
276 convert "$i" "$tmpdir/x.tga" && \
277 pickdxta "$dds_tool" "$dds_sepalpha" "$tmpdir/x.tga" "$o" $1
280 reduce_rgba_dds_premul()
284 convert "$i" "$tmpdir/x.tga" && \
285 pickdxta "$dds_tool" "$dds_prealpha" "$tmpdir/x.tga" "$o" $1
293 if convert "$i" -alpha off TGA:- | cjpeg -targa -quality "$1" -optimize -sample 1x1,1x1,1x1 > "$o"; then
298 if convert "$i" -alpha extract TGA:- | cjpeg -targa -quality "$2" -optimize -sample 1x1,1x1,1x1 > "$oa"; then
309 convert "$i" "$tmpdir/x.tga" && \
310 "$meprefix"compress-texture "$dds_tool" "$dds_noalpha" "$tmpdir/x.tga" "$o" $1
317 if convert "$i" TGA:- | cjpeg -targa -quality "$1" -optimize -sample 1x1,1x1,1x1 > "$o"; then
328 if convert "$i" -depth 16 RGBA:- | perl -e 'while(read STDIN, $_, 8) { substr($_, 6, 2) eq "\xFF\xFF" or exit 1; } exit 0;'; then
339 selfprofile prepareconvert
342 echo >&2 "Handling $F..."
345 jqual_rgb=$jpeg_qual_rgb
353 *.ogg) will_ogg=false ;;
357 ./sounds/misc/talk*.wav) will_ogg=false ;; # engine "feature"
358 *_bump) will_dds=false ;;
359 ./models/player/*) will_dds=false ;;
360 ./models/sprites/*) will_dds=false ;;
366 # we can't DDS compress the 2D textures, sorry
367 # but JPEG is still fine
375 export S2TC_COLORDIST_MODE=NORMALMAP
376 export S2TC_RANDOM_COLORS=256
377 export S2TC_REFINE_COLORS=LOOP
380 export S2TC_COLORDIST_MODE=SRGB_MIXED
381 export S2TC_RANDOM_COLORS=64
382 export S2TC_REFINE_COLORS=LOOP
386 # for deluxemaps, lightmaps and normalmaps, enforce high jpeg quality (like on alpha channels)
387 if [ "$jqual_a" -gt "$jqual_rgb" ]; then
389 ./maps/*/lm_[0-9][0-9][0-9][13579]) # deluxemap
392 ./maps/*/lm_[0-9][0-9][0-9][02468]) # lightmap
403 ./particles/particlefont) # particlefont uses premultiplied alpha
408 if $do_jpeg_if_not_dds; then
415 selfprofile startconvert
418 # handle in *.jpg case
420 # they always got converted, I assume
421 if $will_dds || $will_jpeg; then
427 if [ -f "${f}_alpha.jpg" ]; then
428 cached "$will_dds" reduce_jpeg2_dds$pm "$F" "${f}_alpha.jpg" "dds/${f}.dds" "" "$dds_flags"
429 cached "$will_jpeg" reduce_jpeg2_jpeg2 "$F" "${f}_alpha.jpg" "$F" "${f}_alpha.jpg" "$jqual_rgb" "$jqual_a"
431 cached "$will_dds" reduce_rgb_dds "$F" "" "dds/${f}.dds" "" "$dds_flags"
432 cached "$will_jpeg" reduce_jpeg_jpeg "$F" "" "$F" "" "$jqual_rgb"
436 cached true has_alpha "$F" "" "$F.hasalpha" ""
438 if [ -s "$F.hasalpha" ]; then
439 cached "$will_dds" reduce_rgba_dds$pm "$F" "" "dds/${f}.dds" "" "$dds_flags"
440 cached "$will_jpeg" reduce_rgba_jpeg2 "$F" "" "${f}.jpg" "${f}_alpha.jpg" "$jqual_rgb" "$jqual_a"
442 cached "$will_dds" reduce_rgb_dds "$F" "" "dds/${f}.dds" "" "$dds_flags"
443 cached "$will_jpeg" reduce_rgb_jpeg "$F" "" "${f}.jpg" "" "$jqual_rgb"
448 cached "$will_ogg" reduce_ogg_ogg "$F" "" "$F" "" "$ogg_qual"
450 ./sound/misc/null.wav)
451 # never convert this one
454 cached "$will_ogg" reduce_wav_ogg "$F" "" "${f}.ogg" "" "$ogg_qual"
457 selfprofile marktodelete
461 # FIXME can't have spaces in filenames that way
462 to_delete="$to_delete $F"
466 selfprofile symlinkfixing
467 # fix up DDS paths by a symbolic link
468 if [ -f "dds/${f}.dds" ]; then
469 if [ -z "${f##./textures/*}" ]; then
470 if [ -n "${f##./textures/*/*}" ]; then
471 ln -snf "textures/${f#./textures/}.dds" "dds/${f#./textures/}.dds"
478 for F in $to_delete; do
481 selfprofile finished_time
482 set | grep ^selfprofile_counter_ >&2