Updated version of png_recompress.sh
authorArvid Norlander <anmaster@berlios.de>
Tue, 8 Jan 2008 20:51:00 +0000 (20:51 +0000)
committerArvid Norlander <anmaster@berlios.de>
Tue, 8 Jan 2008 20:51:00 +0000 (20:51 +0000)
SVN-Revision: 5268

tools/png_recompress.sh

index f515536..5ca5674 100755 (executable)
@@ -1,56 +1,67 @@
 #!/bin/bash
 # $Id$
 
-# Copyright (C) 2007 Arvid Norlander <anmaster AT berlios DOT de>
+#  Copyright (C) 2007-2008 Arvid Norlander <anmaster AT tele2 DOT se>
 #
-# This program is free software; you can redistribute it and/or
-# modify it under the terms of the GNU General Public License
-# as published by the Free Software Foundation; either version 2
-# of the License, or (at your option) any later version.
+#  This program is free software: you can redistribute it and/or modify
+#  it under the terms of the GNU General Public License as published by
+#  the Free Software Foundation, either version 3 of the License, or
+#  (at your option) any later version.
 #
-# This program is distributed in the hope that it will be useful,
-# but WITHOUT ANY WARRANTY; without even the implied warranty of
-# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
-# GNU General Public License for more details.
+#  This program is distributed in the hope that it will be useful,
+#  but WITHOUT ANY WARRANTY; without even the implied warranty of
+#  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+#  GNU General Public License for more details.
 #
-# You should have received a copy of the GNU General Public License
-# along with this program; if not, write to the Free Software
-# Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
-# 02111-1307, USA.
-
-# This script recompressess .png files using pngcrush and
-# optipng to get the smallest images. All recompression is
+#  You should have received a copy of the GNU General Public License
+#  along with this program.  If not, see <http://www.gnu.org/licenses/>.
+#
+# This script recompressess .png files using optipng and
+# advpng to get the smallest images. All recompression is
 # looseless.
 #
 # This script needs at least bash3, bash2 will not work
 #
 # TODO:
-#  * Make it work recursivly
+#  * Make it work recursivly on a directory.
 
-if [ -z "$1" ] || [ "$1" == "--help" ]; then
+# Check for new enough bash version
+fail_old_bash() {
+       echo "Sorry your bash version is too old!"
+       echo "You need at least version 3.0 of bash."
+       echo "Please install a newer version:"
+       echo " * Either use your distro's packages."
+       echo " * Or see http://www.gnu.org/software/bash/"
+       exit 2
+}
+
+# Check bash version. We need at least 3.1
+# Lets not use anything like =~ here because
+# that may not work on old bash versions.
+if [[ "${BASH_VERSINFO[0]}" -lt 3 ]]; then
+       fail_old_bash
+fi
+
+if [[ -z "$1" ]] || [[ "$1" == "--help" ]]; then
        echo "Usage: $(basename $0) files..."
-       echo -e '\e[1mNOTE: Files must be in same directory as the script is run from!\e[0m'
-       echo 'Examples:'
-       echo "  $0 *.png"
-       echo '     this works'
-       echo "  $0 */*.png"
-       echo '     this does NOT work'
        exit 1
 fi
 
-# Check that the tools we use exist:
-if ! type pngcrush > /dev/null 2>&1; then
-       echo "Can't find pngcrush!"
-       echo "This script depends on the pngcrush tool to be in PATH."
+if ! type optipng > /dev/null 2>&1; then
+       echo "Can't find optipng!"
+       echo "This script depends on the optipng tool to be in PATH."
        echo "Please install it or, if it is already installed add the"
        echo "directory it is in to PATH and try again."
+       echo "Homepage of this tool is: http://optipng.sourceforge.net/"
        exit 1
 fi
-if ! type optipng > /dev/null 2>&1; then
-       echo "Can't find optipng!"
+
+if ! type advpng > /dev/null 2>&1; then
+       echo "Can't find advpng!"
        echo "This script depends on the optipng tool to be in PATH."
        echo "Please install it or, if it is already installed add the"
        echo "directory it is in to PATH and try again."
+       echo "Homepage of this tool is: http://advancemame.sourceforge.net/comp-readme.html"
        exit 1
 fi
 
@@ -58,36 +69,26 @@ TMPPATH="$$.png-recompress"
 
 echo -e "Please wait, this can take a \e[1mlong\e[0m time."
 
-echo -e "\n\n\n\e[1mPass 1: pngcrush\e[0m\n\n\n"
-for image in "$@"; do
-       if [ -d "$image" ]; then continue; fi
-       fname=${image##*/}
-       dname=`dirname -- "$image"`
-       echo -e "\e[1m$image\e[0m : $(du -b $image | awk '{print $1}')"
-       newsize="$(pngcrush -reduce -brute -d "$TMPPATH" "$image" | grep -E "filesize reduction")"
-       echo "$newsize"
-       if grep -q reduction <<< "$newsize"; then
-               cp -v "${TMPPATH}/$fname" "$dname/$fname"
-       else
-               rm -v "${TMPPATH}/$fname"
-       fi
-       echo
-done
-rm -rvf "$TMPPATH"
+dooptipng() {
+       optipng -i 0 -o 7 "$@" | \
+               awk '
+                       /^\*\* Processing:/ { print "\nFile:   " $3 }
+                       /^Input file size/ { print "Input:  " $5,$6 }
+                       /^Output file size/ { print "Output: " $5,$6,$7,$8,$9,$10,$11 }
+                       /is already optimized/ { print "Output: No change" }
+               '
+}
+
+doadvpng() {
+       echo "         In          Out   %  Filename"
+       advpng -z -4 "$@"
+}
+
+echo -e "\n\n\n\e[1mPass 1: optipng\e[0m\n\n\n"
+dooptipng "$@"
+
+echo -e "\n\n\n\e[1mPass 2: advpng\e[0m\n\n\n"
+doadvpng "$@"
 
-echo -e "\n\n\n\e[1mPass 2: optipng\e[0m\n\n\n"
-for image in "$@"; do
-       if [ -d "$image" ]; then continue; fi
-       fname=${image##*/}
-       dname=`dirname -- "$image"`
-       echo -e "\e[1m$image\e[0m : $(du -b $image | awk '{print $1}')"
-       newsize="$(optipng -i 0 -o 7 -dir "$TMPPATH" "$image" | grep -E '^Output file size')"
-       echo "$newsize"
-       if grep -q decrease <<< "$newsize"; then
-               cp -v "${TMPPATH}/$fname" "$dname/$fname"
-       else
-               rm -v "${TMPPATH}/$fname"
-       fi
-       echo
-done
-rm -rvf "$TMPPATH"
+echo -e "\n\n\n\e[1mPass 3: optipng again (as advpng often makes optipng more effective)\e[0m\n\n\n"
+dooptipng "$@"