]> git.xonotic.org Git - xonotic/xonotic.git/blobdiff - misc/tools/cached-converter.sh
jpegoptim is hereby FIRED (causes chroma subsampling)
[xonotic/xonotic.git] / misc / tools / cached-converter.sh
index c500136ff86b6ebeefd62cc35d31aa2882677cee..be4d5b6df598353c5cbe98465369142505e44860 100755 (executable)
@@ -4,6 +4,7 @@ set -e
 
 : ${CACHEDIR:=$HOME/.xonotic-cached-converter}
 : ${do_jpeg:=true}
+: ${do_jpeg_if_not_dds:=false}
 : ${jpeg_qual_rgb:=95}
 : ${jpeg_qual_a:=99}
 : ${do_dds:=true}
@@ -11,6 +12,7 @@ set -e
 : ${do_ogg:=false}
 : ${ogg_qual:=1}
 : ${del_src:=false}
+: ${git_src_repo:=}
 
 me=$0
 case "$me" in
@@ -26,6 +28,8 @@ tmpdir=`mktemp -d -t cached-converter.XXXXXX`
 trap 'exit 1' INT
 trap 'rm -rf "$tmpdir"' EXIT
 
+lastinfiles=
+lastinfileshash=
 cached()
 {
        flag=$1; shift
@@ -37,13 +41,26 @@ cached()
        if ! $flag; then
                return 0
        fi
+       #sleep 0.25
        if [ x"$infile1" = x"$outfile1" ]; then
                keep=true
        fi
        options=`echo "$*" | git hash-object --stdin`
-       sum=`git hash-object "$infile1"`
-       if [ -n "$infile2" ]; then
-               sum=$sum`git hash-object "$infile2"`
+       if [ x"$infile1/../$infile2" = x"$lastinfiles" ]; then
+               sum=$lastinfileshash
+       else
+               if [ -n "$git_src_repo" ]; then
+                       sum=`( cd "$git_src_repo"; git rev-parse --revs-only HEAD:"${infile1#./}" | grep . ) || git hash-object "$infile1"`
+                       if [ -n "$infile2" ]; then
+                               sum=$sum`( cd "$git_src_repo"; git rev-parse --revs-only HEAD:"${infile2#./}" | grep . ) || git hash-object "$infile2"`
+                       fi
+               else
+                       sum=`git hash-object "$infile1"`
+                       if [ -n "$infile2" ]; then
+                               sum=$sum`git hash-object "$infile2"`
+                       fi
+               fi
+               lastinfileshash=$sum
        fi
        mkdir -p "$CACHEDIR/$method-$options"
        name1="$CACHEDIR/$method-$options/$sum-1.${outfile1##*.}"
@@ -82,23 +99,50 @@ reduce_jpeg2_jpeg2()
        ia=$1; shift
        o=$1; shift
        oa=$1; shift
-       cp "$i" "$o" && jpegoptim --strip-all -m"$1" "$o" && \
-       cp "$ia" "$oa" && jpegoptim --strip-all -m"$2" "$oa"
+       if convert "$i" TGA:- | cjpeg -targa -quality "$1" -optimize -sample 1x1,1x1,1x1 > "$o"; then
+               if [ "`stat -c %s "$i"`" -lt "`stat -c %s "$o"`" ]; then
+                       cp "$i" "$o"
+               fi
+       else
+               return 0
+       fi
+       if convert "$ia" TGA:- | cjpeg -targa -quality "$2" -optimize -sample 1x1,1x1,1x1 > "$oa"; then
+               if [ "`stat -c %s "$ia"`" -lt "`stat -c %s "$oa"`" ]; then
+                       cp "$ia" "$oa"
+               fi
+       else
+               return 1
+       fi
 }
 
 reduce_jpeg_jpeg()
 {
        i=$1; shift; shift
        o=$1; shift; shift
-       cp "$i" "$o" && jpegoptim --strip-all -m"$1" "$o"
+       if convert "$i" TGA:- | cjpeg -targa -quality "$1" -optimize -sample 1x1,1x1,1x1 > "$o"; then
+               if [ "`stat -c %s "$i"`" -lt "`stat -c %s "$o"`" ]; then
+                       cp "$i" "$o"
+               fi
+       else
+               return 0
+       fi
 }
 
-reduce_ogg()
+reduce_ogg_ogg()
 {
        i=$1; shift; shift
        o=$1; shift; shift
+       tags=`vorbiscomment -R -l "$i" || true`
        oggdec -o "$tmpdir/x.wav" "$i" && \
        oggenc -q"$1" -o "$o" "$tmpdir/x.wav"
+       echo "$tags" | vorbiscomment -R -w "$o" || true
+}
+
+reduce_wav_ogg()
+{
+       i=$1; shift; shift
+       o=$1; shift; shift
+       oggenc -q"$1" -o "$o" "$i"
 }
 
 reduce_rgba_dds()
@@ -114,10 +158,16 @@ reduce_rgba_jpeg2()
        i=$1; shift; shift
        o=$1; shift
        oa=$1; shift
-       convert "$i" -alpha extract -quality 100 "$o" && \
-       convert "$i" -alpha off     -quality 100 "$oa" && \
-       jpegoptim --strip-all -m"$1" "$o" && \
-       jpegoptim --strip-all -m"$2" "$oa"
+       if convert "$i" TGA:- | cjpeg -targa -quality "$1" -optimize -sample 1x1,1x1,1x1 > "$o"; then
+               :
+       else
+               return 0
+       fi
+       if convert "$ia" TGA:- | cjpeg -targa -quality "$2" -optimize -sample 1x1,1x1,1x1 > "$oa"; then
+               :
+       else
+               return 1
+       fi
 }
 
 reduce_rgb_dds()
@@ -132,8 +182,11 @@ reduce_rgb_jpeg()
 {
        i=$1; shift; shift
        o=$1; shift; shift
-       convert "$i" "$o" && \
-       jpegoptim --strip-all -m"$1" "$o"
+       if convert "$i" TGA:- | cjpeg -targa -quality "$1" -optimize -sample 1x1,1x1,1x1 > "$o"; then
+               :
+       else
+               return 0
+       fi
 }
 
 has_alpha()
@@ -149,50 +202,114 @@ has_alpha()
        fi
 }
 
+to_delete=
 for F in "$@"; do
+       f=${F%.*}
+
        echo >&2 "Handling $F..."
        conv=false
        keep=false
+       jqual_rgb=$jpeg_qual_rgb
+       jqual_a=$jpeg_qual_a
+
+       will_jpeg=$do_jpeg
+       will_dds=$do_dds
+       case "$f" in
+               *_bump) will_dds=false ;;
+               ./models/player/*) will_dds=false ;;
+               ./models/sprites/*) will_dds=false ;;
+               ./textures/*) ;;
+               ./models/*) ;;
+               ./particles/*) ;;
+               ./progs/*) ;;
+               *)
+                       # we can't DDS compress the 2D textures, sorry
+                       # but JPEG is still fine
+                       will_dds=false
+                       ;;
+       esac
+
+       # for deluxemaps, lightmaps and normalmaps, enforce high jpeg quality (like on alpha channels)
+       if [ "$jqual_a" -gt "$jqual_rgb" ]; then
+               case "$f" in
+                       ./maps/*/lm_[0-9][0-9][0-9][13579]) # deluxemap
+                               jqual_rgb=$jqual_a
+                               ;;
+                       ./maps/*/lm_[0-9][0-9][0-9][02468]) # lightmap
+                               jqual_rgb=$jqual_a
+                               ;;
+                       *_norm) # normalmap
+                               jqual_rgb=$jqual_a
+                               ;;
+               esac
+       fi
+
+       if $do_jpeg_if_not_dds; then
+               if $will_dds; then
+                       will_jpeg=false
+               else
+                       will_jpeg=true
+               fi
+       fi
+
        case "$F" in
                *_alpha.jpg)
                        # handle in *.jpg case
 
                        # they always got converted, I assume
-                       conv=true
-                       keep=$do_jpeg
+                       if $will_dds || $will_jpeg; then
+                               conv=true
+                       fi
+                       keep=$will_jpeg
                        ;;
                *.jpg)
-                       if [ -f "${F%.jpg}_alpha.jpg" ]; then
-                               cached "$do_dds"  reduce_jpeg2_dds   "$F" "${F%.*}_alpha.jpg" "dds/${F%.*}.dds" ""                  "$dds_flags"
-                               cached "$do_jpeg" reduce_jpeg2_jpeg2 "$F" "${F%.*}_alpha.jpg" "$F"              "${F%.*}_alpha.jpg" "$jpeg_qual_rgb" "$jpeg_qual_a"
+                       if [ -f "${f}_alpha.jpg" ]; then
+                               cached "$will_dds"  reduce_jpeg2_dds   "$F" "${f}_alpha.jpg" "dds/${f}.dds" ""               "$dds_flags"
+                               cached "$will_jpeg" reduce_jpeg2_jpeg2 "$F" "${f}_alpha.jpg" "$F"           "${f}_alpha.jpg" "$jqual_rgb" "$jqual_a"
                        else                                   
-                               cached "$do_dds"  reduce_rgb_dds     "$F" ""                  "dds/${F%.*}.dds" ""                  "$dds_flags"
-                               cached "$do_jpeg" reduce_jpeg_jpeg   "$F" ""                  "$F"              ""                  "$jpeg_qual_rgb"
+                               cached "$will_dds"  reduce_rgb_dds     "$F" ""               "dds/${f}.dds" ""               "$dds_flags"
+                               cached "$will_jpeg" reduce_jpeg_jpeg   "$F" ""               "$F"           ""               "$jqual_rgb"
                        fi
                        ;;
                *.png|*.tga)
                        cached true has_alpha "$F" "" "$F.hasalpha" ""
                        conv=false
                        if [ -s "$F.hasalpha" ]; then
-                               cached "$do_dds"  reduce_rgba_dds    "$F" ""                  "dds/${F%.*}.dds" ""                  "$dds_flags"
-                               cached "$do_jpeg" reduce_rgba_jpeg2  "$F" ""                  "${F%.*}.jpg"     "${F%.*}_alpha.jpg" "$jpeg_qual_rgb" "$jpeg_qual_a"
-                               rm -f "$F" # TGA becomes useless after JPEGging
+                               cached "$will_dds"  reduce_rgba_dds    "$F" ""               "dds/${f}.dds" ""               "$dds_flags"
+                               cached "$will_jpeg" reduce_rgba_jpeg2  "$F" ""               "${f}.jpg"     "${f}_alpha.jpg" "$jqual_rgb" "$jqual_a"
                        else                                                             
-                               cached "$do_dds"  reduce_rgb_dds     "$F" ""                  "dds/${F%.*}.dds" ""                  "$dds_flags"
-                               cached "$do_jpeg" reduce_rgb_jpeg    "$F" ""                  "${F%.*}.jpg"     ""                  "$jpeg_qual_rgb"
-                               rm -f "$F" # TGA becomes useless after JPEGging
+                               cached "$will_dds"  reduce_rgb_dds     "$F" ""               "dds/${f}.dds" ""               "$dds_flags"
+                               cached "$will_jpeg" reduce_rgb_jpeg    "$F" ""               "${f}.jpg"     ""               "$jqual_rgb"
                        fi
                        rm -f "$F.hasalpha"
                        ;;
                *.ogg)
-                       cached "$do_ogg" reduce_ogg "$F" "" "$F" "" "$ogg_qual"
+                       cached "$do_ogg" reduce_ogg_ogg "$F" "" "$F" "" "$ogg_qual"
+                       ;;
+               ./sound/misc/null.wav)
+                       # never convert this one
+                       ;;
+               *.wav)
+                       cached "$do_ogg" reduce_wav_ogg "$F" "" "${f}.ogg" "" "$ogg_qual"
                        ;;
        esac
        if $del_src; then
                if $conv; then
                        if ! $keep; then
-                               rm -f "$F"
+                               # FIXME can't have spaces in filenames that way
+                               to_delete="$to_delete $F"
                        fi
                fi
        fi
+       # fix up DDS paths by a symbolic link
+       if [ -f "dds/${f}.dds" ]; then
+               if [ -z "${f##./textures/*}" ]; then
+                       if [ -n "${f##./textures/*/*}" ]; then
+                               ln -snf "textures/${f#./textures/}.dds" "dds/${f#./textures/}.dds"
+                       fi
+               fi
+       fi
+done
+for F in $to_delete; do
+       rm -f "$F"
 done