Merge branch 'master' of git://git.xonotic.org/xonotic/xonotic
[xonotic/xonotic.git] / misc / tools / compress-texture
1 #!/bin/sh
2
3 # usage: compress-texture tool compression in.png out.dds
4 # example: compress-texture compressonator dxt1 foo.jpg foo.dds
5
6 echo >&2 "$0 $*"
7
8 tool=$1; shift
9 format=$1; shift
10 src=$1; shift
11 dst=$1; shift
12
13 c=
14 f=
15 fourcchack=
16 case "$tool" in
17         compressonator-dxtc)
18                 ;;
19         *)
20                 case "$format" in
21                         dxt2)
22                                 fourcchack=DXT2
23                                 format=dxt3
24                                 ;;
25                         dxt4)
26                                 fourcchack=DXT4
27                                 format=dxt5
28                                 ;;
29                 esac
30                 ;;
31 esac
32 case "$tool" in
33         compressonator-dxtc|compressonator-atic)
34                 case "$tool" in
35                         *-dxtc) c="-codec DXTC.dll" ;;
36                         *-atic) c="-codec ATICompressor.dll" ;;
37                 esac
38                 case "$format" in
39                         dxt1) f="+fourCC DXT1" ;;
40                         dxt2) f="+fourCC DXT2" ;;
41                         dxt3) f="+fourCC DXT3" ;;
42                         dxt4) f="+fourCC DXT4" ;;
43                         dxt5) f="+fourCC DXT5" ;;
44                 esac
45                 dir=`mktemp -d "$HOME/.wine/drive_c/compressonator.XXXXXX"`
46                 dir_dos="C:/${dir##*/}"
47                 ext=${src##*.}
48                 cp "$src" "$dir/src.$ext"
49
50                 # compressonator and wine suck, so we sometimes have to retry :(
51                 for retry in 1 2 3 4 5; do
52                         wine "c:/Program Files/AMD/The Compressonator 1.50/TheCompressonator.exe" -convert -mipmaps "$dir_dos/src.$ext" "$dir_dos/dst.dds" $c $f "$@" -mipper BoxFilter.dll
53                         if mv "$dir/dst.dds" "$dst"; then
54                                 break
55                         fi
56                 done
57                 rm -rf "$dir"
58                 [ -f "$dst" ]
59                 ;;
60         nvcompress)
61                 case "$format" in
62                         dxt1) f="-bc1" ;;
63                         dxt3) f="-alpha -bc3" ;;
64                         dxt5) f="-alpha -bc5" ;;
65                 esac
66                 nvcompress $f "$@" "$src" "$dst"
67                 ;;
68 esac
69
70 if [ -n "$fourcchack" ]; then
71         # use dd to hack in the right FOURCC
72         echo -n "$fourcchack" | dd of="$dst" bs=1 count=4 seek=84
73 fi