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
56 if [ x"$infile1" = x"$outfile1" ]; then
59 options=`echo "$*" | git hash-object --stdin`
60 selfprofile convert_findchecksum
61 if [ x"$infile1/../$infile2" = x"$lastinfiles" ]; then
65 for infile in "$infile1" "$infile2"; do
67 */background_l2.tga|*/background_ingame_l2.tga)
72 if [ -n "$git_src_repo" ] && ! $evil; then
73 sum=`( cd "$git_src_repo"; git ls-files -s "${infile1#./}" | cut -d ' ' -f 2 | grep . ) || { echo >&2 "git-ls-files failed on $infile1"; git hash-object "$infile1"; }`
74 if [ -n "$infile2" ]; then
75 sum=$sum`( cd "$git_src_repo"; git ls-files -s "${infile2#./}" | cut -d ' ' -f 2 | grep . ) || { echo >&2 "git-ls-files failed on $infile2"; git hash-object "$infile2"; }`
78 sum=`git hash-object "$infile1"`
79 if [ -n "$infile2" ]; then
80 sum=$sum`git hash-object "$infile2"`
85 selfprofile convert_makecachedir
86 mkdir -p "$CACHEDIR/$method-$options"
87 name1="$CACHEDIR/$method-$options/$sum-1.${outfile1##*.}"
88 [ -z "$outfile2" ] || name2="$CACHEDIR/$method-$options/$sum-2.${outfile2##*.}"
89 tempfile1="${name1%/*}/new-${name1##*/}"
90 [ -z "$outfile2" ] || tempfile2="${name2%/*}/new-${name2##*/}"
91 if [ -f "$name1" ] && { [ -z "$outfile2" ] || [ -f "$name2" ]; }; then
92 selfprofile convert_copyoutput
93 case "$outfile1" in */*) mkdir -p "${outfile1%/*}"; esac && { ln -f "$name1" "$outfile1" 2>/dev/null || { rm -f "$outfile1" && cp "$name1" "$outfile1"; }; }
94 [ -z "$outfile2" ] || { case "$outfile2" in */*) mkdir -p "${outfile2%/*}"; esac && { ln -f "$name2" "$outfile2" 2>/dev/null || { rm -f "$outfile2" && cp "$name2" "$outfile2"; }; }; }
96 elif selfprofile convert_makeoutput; "$method" "$infile1" "$infile2" "$tempfile1" "$tempfile2" "$@"; then
97 mv "$tempfile1" "$name1"
98 [ -z "$outfile2" ] || mv "$tempfile2" "$name2"
99 case "$outfile1" in */*) mkdir -p "${outfile1%/*}"; esac && { ln -f "$name1" "$outfile1" 2>/dev/null || { rm -f "$outfile1" && cp "$name1" "$outfile1"; }; }
100 [ -z "$outfile2" ] || { case "$outfile2" in */*) mkdir -p "${outfile2%/*}"; esac && { ln -f "$name2" "$outfile2" 2>/dev/null || { rm -f "$outfile2" && cp "$name2" "$outfile2"; }; }; }
103 selfprofile convert_cleartemp
106 selfprofile convert_finished
109 selfprofile convert_finished
117 convert "$i" "$ia" -compose CopyOpacity -composite "$tmpdir/x.tga" && \
118 "$meprefix"compress-texture "$dds_tool" dxt5 "$tmpdir/x.tga" "$o" $1
121 reduce_jpeg2_dds_premul()
126 convert "$i" "$ia" -compose CopyOpacity -composite "$tmpdir/x.tga" && \
127 "$meprefix"compress-texture "$dds_tool" dxt4 "$tmpdir/x.tga" "$o" $1
136 if convert "$i" TGA:- | cjpeg -targa -quality "$1" -optimize -sample 1x1,1x1,1x1 > "$o"; then
137 if [ "`stat -c %s "$i"`" -lt "`stat -c %s "$o"`" ]; then
143 if convert "$ia" TGA:- | cjpeg -targa -quality "$2" -optimize -sample 1x1,1x1,1x1 > "$oa"; then
144 if [ "`stat -c %s "$ia"`" -lt "`stat -c %s "$oa"`" ]; then
156 if convert "$i" TGA:- | cjpeg -targa -quality "$1" -optimize -sample 1x1,1x1,1x1 > "$o"; then
157 if [ "`stat -c %s "$i"`" -lt "`stat -c %s "$o"`" ]; then
169 tags=`vorbiscomment -R -l "$i" || true`
170 oggdec -o "$tmpdir/x.wav" "$i" && \
171 oggenc -q"$1" -o "$o" "$tmpdir/x.wav"
172 echo "$tags" | vorbiscomment -R -w "$o" || true
179 oggenc -q"$1" -o "$o" "$i"
186 convert "$i" "$tmpdir/x.tga" && \
187 "$meprefix"compress-texture "$dds_tool" dxt5 "$tmpdir/x.tga" "$o" $1
190 reduce_rgba_dds_premul()
194 convert "$i" "$tmpdir/x.tga" && \
195 "$meprefix"compress-texture "$dds_tool" dxt4 "$tmpdir/x.tga" "$o" $1
203 if convert "$i" -alpha off TGA:- | cjpeg -targa -quality "$1" -optimize -sample 1x1,1x1,1x1 > "$o"; then
208 if convert "$i" -alpha extract TGA:- | cjpeg -targa -quality "$2" -optimize -sample 1x1,1x1,1x1 > "$oa"; then
219 convert "$i" "$tmpdir/x.tga" && \
220 "$meprefix"compress-texture "$dds_tool" dxt1 "$tmpdir/x.tga" "$o" $1
227 if convert "$i" TGA:- | cjpeg -targa -quality "$1" -optimize -sample 1x1,1x1,1x1 > "$o"; then
238 if convert "$i" -depth 16 RGBA:- | perl -e 'while(read STDIN, $_, 8) { substr($_, 6, 2) eq "\xFF\xFF" or exit 1; } exit 0;'; then
249 selfprofile prepareconvert
252 echo >&2 "Handling $F..."
255 jqual_rgb=$jpeg_qual_rgb
261 *_bump) will_dds=false ;;
262 ./models/player/*) will_dds=false ;;
263 ./models/sprites/*) will_dds=false ;;
269 # we can't DDS compress the 2D textures, sorry
270 # but JPEG is still fine
275 # for deluxemaps, lightmaps and normalmaps, enforce high jpeg quality (like on alpha channels)
276 if [ "$jqual_a" -gt "$jqual_rgb" ]; then
278 ./maps/*/lm_[0-9][0-9][0-9][13579]) # deluxemap
281 ./maps/*/lm_[0-9][0-9][0-9][02468]) # lightmap
292 ./particles/particlefont) # particlefont uses premultiplied alpha
297 if $do_jpeg_if_not_dds; then
304 selfprofile startconvert
307 # handle in *.jpg case
309 # they always got converted, I assume
310 if $will_dds || $will_jpeg; then
316 if [ -f "${f}_alpha.jpg" ]; then
317 cached "$will_dds" reduce_jpeg2_dds$pm "$F" "${f}_alpha.jpg" "dds/${f}.dds" "" "$dds_flags"
318 cached "$will_jpeg" reduce_jpeg2_jpeg2 "$F" "${f}_alpha.jpg" "$F" "${f}_alpha.jpg" "$jqual_rgb" "$jqual_a"
320 cached "$will_dds" reduce_rgb_dds "$F" "" "dds/${f}.dds" "" "$dds_flags"
321 cached "$will_jpeg" reduce_jpeg_jpeg "$F" "" "$F" "" "$jqual_rgb"
325 cached true has_alpha "$F" "" "$F.hasalpha" ""
327 if [ -s "$F.hasalpha" ]; then
328 cached "$will_dds" reduce_rgba_dds$pm "$F" "" "dds/${f}.dds" "" "$dds_flags"
329 cached "$will_jpeg" reduce_rgba_jpeg2 "$F" "" "${f}.jpg" "${f}_alpha.jpg" "$jqual_rgb" "$jqual_a"
331 cached "$will_dds" reduce_rgb_dds "$F" "" "dds/${f}.dds" "" "$dds_flags"
332 cached "$will_jpeg" reduce_rgb_jpeg "$F" "" "${f}.jpg" "" "$jqual_rgb"
337 cached "$do_ogg" reduce_ogg_ogg "$F" "" "$F" "" "$ogg_qual"
339 ./sound/misc/null.wav)
340 # never convert this one
343 cached "$do_ogg" reduce_wav_ogg "$F" "" "${f}.ogg" "" "$ogg_qual"
346 selfprofile marktodelete
350 # FIXME can't have spaces in filenames that way
351 to_delete="$to_delete $F"
355 selfprofile symlinkfixing
356 # fix up DDS paths by a symbolic link
357 if [ -f "dds/${f}.dds" ]; then
358 if [ -z "${f##./textures/*}" ]; then
359 if [ -n "${f##./textures/*/*}" ]; then
360 ln -snf "textures/${f#./textures/}.dds" "dds/${f#./textures/}.dds"
367 for F in $to_delete; do
370 selfprofile finished_time
371 set | grep ^selfprofile_counter_ >&2