5 : ${CACHEDIR:=$HOME/.xonotic-cached-converter}
7 : ${do_jpeg_if_not_dds:=false}
11 : ${dds_tool:=compressonator-dxtc}
17 selfprofile_t0=`date +%s`
21 selfprofile_t=`date +%s`
22 eval "selfprofile_counter_$selfprofile_step=\$((\$selfprofile_counter_$selfprofile_step+$selfprofile_t))"
24 eval "selfprofile_counter_$selfprofile_step=\$((\$selfprofile_counter_$selfprofile_step-$selfprofile_t))"
25 selfprofile_t0=$selfprofile_t
38 tmpdir=`mktemp -d -t cached-converter.XXXXXX`
40 trap 'rm -rf "$tmpdir"' EXIT
43 use_magnet_to_acquire_checksum_faster()
45 # ,--'' ~~~~~~~^^^~._ '.
46 # ,.-' ~~~~~~~~~~^^^^^~~~._._ \
47 # | /^^^^^| /^^^^^^^^\\ \ \
48 # ,/___ < o> < (OO) > _ \
50 # |(|-'^^;,- ,| __ ^~~^^^^^^^; |\
51 # \\` | <;_ __ |`--- ..-^^/- | ||
52 # \`-|Oq-.____`________~='^^|__,/ ' //
53 # \ || | | | | \ ..-;| / '/
54 # | ||#|#|the|==|game!|'^` |/' /'
55 # | \\\\^\***|***| \ ,,;' /
56 # | `-=\_\__\___\__..-' ,.- - ,/
57 # | . `-_ ------ _,-'^-'^,-'
58 # | `-._________..--''^,-''^
60 # `----------'^ PROBLEM?
62 magnet=`GIT_DIR="$git_src_repo/.git" git ls-files -s "$1"`
63 if [ -n "$magnet" ]; then
68 sum=$sum`git hash-object "$1"`
86 if [ x"$infile1" = x"$outfile1" ]; then
89 options=`echo "$*" | git hash-object --stdin`
90 selfprofile convert_findchecksum
91 if [ x"$infile1/../$infile2" = x"$lastinfiles" ]; then
95 for infile in "$infile1" "$infile2"; do
97 */background_l2.tga|*/background_ingame_l2.tga)
102 if [ -n "$git_src_repo" ] && ! $evil; then
104 use_magnet_to_acquire_checksum_faster "${infile1#./}"
105 if [ -n "$infile2" ]; then
106 use_magnet_to_acquire_checksum_faster "${infile2#./}"
109 sum=`git hash-object "$infile1"`
110 if [ -n "$infile2" ]; then
111 sum=$sum`git hash-object "$infile2"`
116 selfprofile convert_makecachedir
117 mkdir -p "$CACHEDIR/$method-$options"
118 name1="$CACHEDIR/$method-$options/$sum-1.${outfile1##*.}"
119 [ -z "$outfile2" ] || name2="$CACHEDIR/$method-$options/$sum-2.${outfile2##*.}"
120 tempfile1="${name1%/*}/new-${name1##*/}"
121 [ -z "$outfile2" ] || tempfile2="${name2%/*}/new-${name2##*/}"
122 if [ -f "$name1" ] && { [ -z "$outfile2" ] || [ -f "$name2" ]; }; then
123 selfprofile convert_copyoutput
124 case "$outfile1" in */*) mkdir -p "${outfile1%/*}"; esac && { ln -vf "$name1" "$outfile1" || { rm -f "$outfile1" && cp "$name1" "$outfile1"; }; }
125 [ -z "$outfile2" ] || { case "$outfile2" in */*) mkdir -p "${outfile2%/*}"; esac && { ln -vf "$name2" "$outfile2" || { rm -f "$outfile2" && cp "$name2" "$outfile2"; }; }; }
127 elif selfprofile convert_makeoutput; "$method" "$infile1" "$infile2" "$tempfile1" "$tempfile2" "$@"; then
128 mv "$tempfile1" "$name1"
129 [ -z "$outfile2" ] || mv "$tempfile2" "$name2"
130 case "$outfile1" in */*) mkdir -p "${outfile1%/*}"; esac && { ln -f "$name1" "$outfile1" 2>/dev/null || { rm -f "$outfile1" && cp "$name1" "$outfile1"; }; }
131 [ -z "$outfile2" ] || { case "$outfile2" in */*) mkdir -p "${outfile2%/*}"; esac && { ln -f "$name2" "$outfile2" 2>/dev/null || { rm -f "$outfile2" && cp "$name2" "$outfile2"; }; }; }
134 selfprofile convert_cleartemp
137 selfprofile convert_finished
140 selfprofile convert_finished
148 convert "$i" "$ia" -compose CopyOpacity -composite "$tmpdir/x.tga" && \
149 "$meprefix"compress-texture "$dds_tool" dxt5 "$tmpdir/x.tga" "$o" $1
152 reduce_jpeg2_dds_premul()
157 convert "$i" "$ia" -compose CopyOpacity -composite "$tmpdir/x.tga" && \
158 "$meprefix"compress-texture "$dds_tool" dxt4 "$tmpdir/x.tga" "$o" $1
167 if convert "$i" TGA:- | cjpeg -targa -quality "$1" -optimize -sample 1x1,1x1,1x1 > "$o"; then
168 if [ "`stat -c %s "$i"`" -lt "`stat -c %s "$o"`" ]; then
174 if convert "$ia" TGA:- | cjpeg -targa -quality "$2" -optimize -sample 1x1,1x1,1x1 > "$oa"; then
175 if [ "`stat -c %s "$ia"`" -lt "`stat -c %s "$oa"`" ]; then
187 if convert "$i" TGA:- | cjpeg -targa -quality "$1" -optimize -sample 1x1,1x1,1x1 > "$o"; then
188 if [ "`stat -c %s "$i"`" -lt "`stat -c %s "$o"`" ]; then
200 tags=`vorbiscomment -R -l "$i" || true`
201 oggdec -o "$tmpdir/x.wav" "$i" && \
202 oggenc -q"$1" -o "$o" "$tmpdir/x.wav"
203 echo "$tags" | vorbiscomment -R -w "$o" || true
210 oggenc -q"$1" -o "$o" "$i"
217 convert "$i" "$tmpdir/x.tga" && \
218 "$meprefix"compress-texture "$dds_tool" dxt5 "$tmpdir/x.tga" "$o" $1
221 reduce_rgba_dds_premul()
225 convert "$i" "$tmpdir/x.tga" && \
226 "$meprefix"compress-texture "$dds_tool" dxt4 "$tmpdir/x.tga" "$o" $1
234 if convert "$i" -alpha off TGA:- | cjpeg -targa -quality "$1" -optimize -sample 1x1,1x1,1x1 > "$o"; then
239 if convert "$i" -alpha extract TGA:- | cjpeg -targa -quality "$2" -optimize -sample 1x1,1x1,1x1 > "$oa"; then
250 convert "$i" "$tmpdir/x.tga" && \
251 "$meprefix"compress-texture "$dds_tool" dxt1 "$tmpdir/x.tga" "$o" $1
258 if convert "$i" TGA:- | cjpeg -targa -quality "$1" -optimize -sample 1x1,1x1,1x1 > "$o"; then
269 if convert "$i" -depth 16 RGBA:- | perl -e 'while(read STDIN, $_, 8) { substr($_, 6, 2) eq "\xFF\xFF" or exit 1; } exit 0;'; then
280 selfprofile prepareconvert
283 echo >&2 "Handling $F..."
286 jqual_rgb=$jpeg_qual_rgb
292 *_bump) will_dds=false ;;
293 ./models/player/*) will_dds=false ;;
294 ./models/sprites/*) will_dds=false ;;
300 # we can't DDS compress the 2D textures, sorry
301 # but JPEG is still fine
306 # for deluxemaps, lightmaps and normalmaps, enforce high jpeg quality (like on alpha channels)
307 if [ "$jqual_a" -gt "$jqual_rgb" ]; then
309 ./maps/*/lm_[0-9][0-9][0-9][13579]) # deluxemap
312 ./maps/*/lm_[0-9][0-9][0-9][02468]) # lightmap
323 ./particles/particlefont) # particlefont uses premultiplied alpha
328 if $do_jpeg_if_not_dds; then
335 selfprofile startconvert
338 # handle in *.jpg case
340 # they always got converted, I assume
341 if $will_dds || $will_jpeg; then
347 if [ -f "${f}_alpha.jpg" ]; then
348 cached "$will_dds" reduce_jpeg2_dds$pm "$F" "${f}_alpha.jpg" "dds/${f}.dds" "" "$dds_flags"
349 cached "$will_jpeg" reduce_jpeg2_jpeg2 "$F" "${f}_alpha.jpg" "$F" "${f}_alpha.jpg" "$jqual_rgb" "$jqual_a"
351 cached "$will_dds" reduce_rgb_dds "$F" "" "dds/${f}.dds" "" "$dds_flags"
352 cached "$will_jpeg" reduce_jpeg_jpeg "$F" "" "$F" "" "$jqual_rgb"
356 cached true has_alpha "$F" "" "$F.hasalpha" ""
358 if [ -s "$F.hasalpha" ]; then
359 cached "$will_dds" reduce_rgba_dds$pm "$F" "" "dds/${f}.dds" "" "$dds_flags"
360 cached "$will_jpeg" reduce_rgba_jpeg2 "$F" "" "${f}.jpg" "${f}_alpha.jpg" "$jqual_rgb" "$jqual_a"
362 cached "$will_dds" reduce_rgb_dds "$F" "" "dds/${f}.dds" "" "$dds_flags"
363 cached "$will_jpeg" reduce_rgb_jpeg "$F" "" "${f}.jpg" "" "$jqual_rgb"
368 cached "$do_ogg" reduce_ogg_ogg "$F" "" "$F" "" "$ogg_qual"
370 ./sound/misc/null.wav)
371 # never convert this one
374 cached "$do_ogg" reduce_wav_ogg "$F" "" "${f}.ogg" "" "$ogg_qual"
377 selfprofile marktodelete
381 # FIXME can't have spaces in filenames that way
382 to_delete="$to_delete $F"
386 selfprofile symlinkfixing
387 # fix up DDS paths by a symbolic link
388 if [ -f "dds/${f}.dds" ]; then
389 if [ -z "${f##./textures/*}" ]; then
390 if [ -n "${f##./textures/*/*}" ]; then
391 ln -snf "textures/${f#./textures/}.dds" "dds/${f#./textures/}.dds"
398 for F in $to_delete; do
401 selfprofile finished_time
402 set | grep ^selfprofile_counter_ >&2