Scroll nonblocking infoblock by moving half a block away.
[supertux.git] / tools / png_recompress.sh
1 #!/bin/bash
2 # $Id$
3
4 # Copyright (C) 2007 Arvid Norlander <anmaster AT berlios DOT de>
5 #
6 # This program is free software; you can redistribute it and/or
7 # modify it under the terms of the GNU General Public License
8 # as published by the Free Software Foundation; either version 2
9 # of the License, or (at your option) any later version.
10 #
11 # This program is distributed in the hope that it will be useful,
12 # but WITHOUT ANY WARRANTY; without even the implied warranty of
13 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14 # GNU General Public License for more details.
15 #
16 # You should have received a copy of the GNU General Public License
17 # along with this program; if not, write to the Free Software
18 # Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
19 # 02111-1307, USA.
20
21 # This script recompressess .png files using pngcrush and
22 # optipng to get the smallest images. All recompression is
23 # looseless.
24 #
25 # This script needs at least bash3, bash2 will not work
26 #
27 # TODO:
28 #  * Make it work recursivly
29
30 if [ -z "$1" ] || [ "$1" == "--help" ]; then
31         echo "Usage: $(basename $0) files..."
32         echo -e '\e[1mNOTE: Files must be in same directory as the script is run from!\e[0m'
33         echo 'Examples:'
34         echo "  $0 *.png"
35         echo '     this works'
36         echo "  $0 */*.png"
37         echo '     this does NOT work'
38         exit 1
39 fi
40
41 # Check that the tools we use exist:
42 if ! type pngcrush > /dev/null 2>&1; then
43         echo "Can't find pngcrush!"
44         echo "This script depends on the pngcrush tool to be in PATH."
45         echo "Please install it or, if it is already installed add the"
46         echo "directory it is in to PATH and try again."
47         exit 1
48 fi
49 if ! type optipng > /dev/null 2>&1; then
50         echo "Can't find optipng!"
51         echo "This script depends on the optipng tool to be in PATH."
52         echo "Please install it or, if it is already installed add the"
53         echo "directory it is in to PATH and try again."
54         exit 1
55 fi
56
57 TMPPATH="$$.png-recompress"
58
59 echo -e "Please wait, this can take a \e[1mlong\e[0m time."
60
61 echo -e "\n\n\n\e[1mPass 1: pngcrush\e[0m\n\n\n"
62 for image in "$@"; do
63         if [ -d "$image" ]; then continue; fi
64         fname=${image##*/}
65         dname=`dirname -- "$image"`
66         echo -e "\e[1m$image\e[0m : $(du -b $image | awk '{print $1}')"
67         newsize="$(pngcrush -reduce -brute -d "$TMPPATH" "$image" | grep -E "filesize reduction")"
68         echo "$newsize"
69         if [[ $newsize =~ ".+reduction.+" ]]; then
70                 cp -v "${TMPPATH}/$fname" "$dname/$fname"
71         else
72                 rm -v "${TMPPATH}/$fname"
73         fi
74         echo
75 done
76 rm -rvf "$TMPPATH"
77
78 echo -e "\n\n\n\e[1mPass 2: optipng\e[0m\n\n\n"
79 for image in "$@"; do
80         if [ -d "$image" ]; then continue; fi
81         fname=${image##*/}
82         dname=`dirname -- "$image"`
83         echo -e "\e[1m$image\e[0m : $(du -b $image | awk '{print $1}')"
84         newsize="$(optipng -i 0 -o 7 -dir "$TMPPATH" "$image" | grep -E '^Output file size')"
85         echo "$newsize"
86         if [[ $newsize =~ ".+decrease.+" ]]; then
87                 cp -v "${TMPPATH}/$fname" "$dname/$fname"
88         else
89                 rm -v "${TMPPATH}/$fname"
90         fi
91         echo
92 done
93 rm -rvf "$TMPPATH"