png_recompress.sh 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. #!/bin/bash
  2. # $Id$
  3. # Copyright (C) 2007-2008 Arvid Norlander <anmaster AT tele2 DOT se>
  4. #
  5. # This program is free software: you can redistribute it and/or modify
  6. # it under the terms of the GNU General Public License as published by
  7. # the Free Software Foundation, either version 3 of the License, or
  8. # (at your option) any later version.
  9. #
  10. # This program is distributed in the hope that it will be useful,
  11. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. # GNU General Public License for more details.
  14. #
  15. # You should have received a copy of the GNU General Public License
  16. # along with this program. If not, see <http://www.gnu.org/licenses/>.
  17. #
  18. # This script recompresses .png files using optipng and
  19. # advpng to get the smallest images. All recompression is
  20. # lossless.
  21. #
  22. # This script needs at least bash3, bash2 will not work
  23. #
  24. # TODO:
  25. # * Make it work recursively on a directory.
  26. # Check for new enough bash version
  27. fail_old_bash() {
  28. echo "Sorry your bash version is too old!"
  29. echo "You need at least version 3.0 of bash."
  30. echo "Please install a newer version:"
  31. echo " * Either use your distro's packages."
  32. echo " * Or see http://www.gnu.org/software/bash/"
  33. exit 2
  34. }
  35. # Check bash version. We need at least 3.1
  36. # Lets not use anything like =~ here because
  37. # that may not work on old bash versions.
  38. if [[ "${BASH_VERSINFO[0]}" -lt 3 ]]; then
  39. fail_old_bash
  40. fi
  41. if [[ -z "$1" ]] || [[ "$1" == "--help" ]]; then
  42. echo "Usage: $(basename $0) files..."
  43. exit 1
  44. fi
  45. if ! type optipng > /dev/null 2>&1; then
  46. echo "Can't find optipng!"
  47. echo "This script depends on the optipng tool to be in PATH."
  48. echo "Please install it or, if it is already installed add the"
  49. echo "directory it is in to PATH and try again."
  50. echo "Homepage of this tool is: http://optipng.sourceforge.net/"
  51. exit 1
  52. fi
  53. if ! type advpng > /dev/null 2>&1; then
  54. echo "Can't find advpng!"
  55. echo "This script depends on the advpng tool to be in PATH."
  56. echo "Please install it or, if it is already installed add the"
  57. echo "directory it is in to PATH and try again."
  58. echo "Homepage of this tool is: http://advancemame.sourceforge.net/comp-readme.html"
  59. echo "Hint: For package name in your distro, try looking for \"advancecomp\"."
  60. exit 1
  61. fi
  62. TMPPATH="$$.png-recompress"
  63. echo -e "Please wait, this can take a \e[1mlong\e[0m time."
  64. dooptipng() {
  65. optipng -i 0 -o 7 "$@" | \
  66. awk '
  67. /^\*\* Processing:/ { print "\nFile: " $3 }
  68. /^Input file size/ { print "Input: " $5,$6 }
  69. /^Output file size/ { print "Output: " $5,$6,$7,$8,$9,$10,$11 }
  70. /is already optimized/ { print "Output: No change" }
  71. '
  72. }
  73. doadvpng() {
  74. echo " In Out % Filename"
  75. advpng -z -4 "$@"
  76. }
  77. echo -e "\n\n\n\e[1mPass 1: optipng\e[0m\n\n\n"
  78. dooptipng "$@"
  79. echo -e "\n\n\n\e[1mPass 2: advpng\e[0m\n\n\n"
  80. doadvpng "$@"
  81. echo -e "\n\n\n\e[1mPass 3: optipng again (as advpng often makes optipng more effective)\e[0m\n\n\n"
  82. dooptipng "$@"