2b657bb3a5bca0a09f4ce0c10e20b224a3cdcc1c
[supertux.git] / tools / png_recompress.sh
1 #!/bin/bash
2 # $Id$
3 # This script recompressess .png files using pngcrush and
4 # optipng to get the smallest images. All recompression is
5 # looseless.
6 #
7 # NOTE: files must be in same directory as the script is run from!
8 # Examples:
9 #  ~/supertux/tools/image_recompress.sh *.png
10 #   (good)
11 #  ~/supertux/tools/image_recompress.sh */*.png
12 #   (bad, does not work)
13 #
14 # TODO:
15 #  * Make it work recursivly
16 #  * Make it work on files not in current directory
17
18 if [ -z "$1" ] || [ "$1" == "--help" ]; then
19         echo "Usage: $(basename $0) files..."
20         echo -e '\e[1mNOTE: Files must be in same directory as the script is run from!\e[0m'
21         echo 'Examples:'
22         echo "  $0 *.png"
23         echo '     this works'
24         echo "  $0 */*.png"
25         echo '     this does NOT work'
26         exit 1
27 fi
28
29 # Check that the tools we use exist:
30 if ! type pngcrush > /dev/null 2>&1; then
31         echo "Can't find pngcrush!"
32         echo "This script depends on the pngcrush tool to be in PATH."
33         echo "Please install it or, if it is already installed add the"
34         echo "directory it is in to PATH and try again."
35         exit 1
36 fi
37 if ! type optipng > /dev/null 2>&1; then
38         echo "Can't find optipng!"
39         echo "This script depends on the optipng tool to be in PATH."
40         echo "Please install it or, if it is already installed add the"
41         echo "directory it is in to PATH and try again."
42         exit 1
43 fi
44
45
46 echo -e "Please wait, this can take a \e[1mlong\e[0m time."
47
48 echo -e "\n\n\n\e[1mPass 1: pngcrush\e[0m\n\n\n"
49 for image in "$@"; do
50         echo -e "\e[1m$image\e[0m : $(du -b $image | awk '{print $1}')"
51         newsize="$(pngcrush -reduce -brute -d c "$image" | grep -E "filesize reduction")"
52         if [[ $newsize =~ ".+reduction.+" ]]; then
53                 echo "$newsize"
54                 cp -v "c/$image" "$image"
55         fi
56         echo
57 done
58 rm -rvf c
59
60 echo -e "\n\n\n\e[1mPass 2: optipng\e[0m\n\n\n"
61 for image in "$@"; do
62         echo -e "\e[1m$image\e[0m : $(du -b $image | awk '{print $1}')"
63         newsize="$(optipng -i 0 -o 7 -dir out "$image" | grep -E '^Output file size')"
64         echo "$newsize"
65         if [[ $newsize =~ ".+decrease.+" ]]; then
66                 cp -v "out/$image" "$image"
67         else
68                 rm -v "out/$image"
69         fi
70         echo
71 done
72 rm -rvf out