File indexing completed on 2024-04-21 05:41:59

0001 #!/bin/sh
0002 
0003 # Copyright 2008 Urs Wolfer <uwolfer @ kde.org>
0004 # Copyright 2012, 2013 Bruno George Moraes <brunogm0 @ gmail.com>
0005 #
0006 # This program is free software; you can redistribute it and/or
0007 # modify it under the terms of the GNU General Public License as
0008 # published by the Free Software Foundation; either version 2 of
0009 # the License, or (at your option) any later version.
0010 #
0011 # This program is distributed in the hope that it will be useful,
0012 # but WITHOUT ANY WARRANTY; without even the implied warranty of
0013 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
0014 # GNU General Public License for more details.
0015 #
0016 #
0017 # Thin wrapper script around optipng, advdef and pngout.
0018 # It filters the output and shows a summary at the end of the optimization run.
0019 # Tested with:
0020 # * OptiPNG 0.7.1 http://optipng.sourceforge.net/
0021 # * advdef (AdvanceCOMP) 1.15 http://advancemame.sourceforge.net/comp-readme.html
0022 # * PNGOUT http://www.jonof.id.au/kenutils (Note: no-cost download; non-Free software)
0023 
0024 # ${foo:-bar} syntax sets foo=bar if not already set
0025 optipng_tool=$(command -v optipng)
0026 optipng_tool=${optipng_tool:-/bin/true}
0027 
0028 advancecomp_tool=$(command -v advdef)
0029 advancecomp_tool=${advancecomp_tool:-/bin/true}
0030 
0031 pngout_tool=$(command -v pngout)
0032 pngout_tool=${pngout_tool:-/bin/true}
0033 
0034 thread_count=$(grep -c ^processor /proc/cpuinfo)
0035 thread_count=${thread_count:-4}
0036 
0037 if test "$1" = "-h" -o "$1" = "--help"; then
0038     echo "Usage: optimizegraphics [FILE]";
0039     echo "If [FILE] is not defined, it optimizes all files (PNG,MNG,SVGZ) recursively starting at the current working directory.";
0040     echo "To achieve the best optimization please use all supported tools (optipng,advdef,pngout).";
0041     exit;
0042 fi
0043 
0044 if [ ${optipng_tool} = "/bin/true" ]; then
0045     echo "Please install optipng to increase PNG compression.";
0046 fi
0047 if [ ${pngout_tool} = "/bin/true" ];  then
0048     echo "Please install pngout to increase PNG compression.";
0049 fi
0050 if [ ${advancecomp_tool} = "/bin/true" ]; then
0051     echo "Please install advancecomp to optimize PNG, MNG and SVGZ.";
0052 fi
0053 
0054 if [ $# -ne 0 ]; then # file is defined
0055     if [ ! -e "$1" ]; then
0056         echo "File $1 doesn't exist!"
0057         exit 1
0058     fi
0059 
0060     ${optipng_tool} -preserve -o6 "$1";
0061     ${advancecomp_tool} -z -4 "$1";
0062     ${pngout_tool} -ks "$1";
0063 
0064     exit $?
0065 
0066 else # do it recursively
0067     echo "Recursive parallel optimization using detected tools.";
0068     STARTSIZE=`du -sb | awk '{ print $1 }'`;
0069 
0070     # OptiPNG pass
0071     if [ "${optipng_tool}" != "/bin/true" ] ; then
0072         find . -name "*.png" -print0 | \
0073             xargs -t0r -n 1 -P $thread_count \
0074                 "${optipng_tool}" -quiet -preserve -o6
0075     else
0076         echo "Skipping OptiPNG optimizations for PNG file type"
0077     fi
0078 
0079     # AdvanceCOMP pass
0080     if [ "${advancecomp_tool}" != "/bin/true" ] ; then
0081         find . \( -name "*.svgz" -o -name '*.mng' -o -name '*.png' \) -print0 | \
0082             xargs -t0r -n 1 -P $thread_count \
0083                 "${advancecomp_tool}" -q -z -4
0084     else
0085         echo "Skipping AdvanceCOMP optimizations for SVGZ, MNG, PNG file types"
0086     fi
0087 
0088     # PNGout pass
0089     if [ "${pngout_tool}" != "/bin/true" ] ; then
0090         find . -name "*.png" -print0 | \
0091             xargs -t0r -n 1 -P $thread_count \
0092                 "${pngout_tool}" -q -ks
0093     else
0094         echo "Skipping PNGout optimizations for PNG file type"
0095     fi
0096 
0097     ENDSIZE=`du -sb | awk '{ print $1 }'`;
0098 
0099     REDUCEDSIZE=$(( $STARTSIZE - $ENDSIZE ));
0100     REDUCEDPER=$(( 100 * $REDUCEDSIZE / $STARTSIZE ));
0101 
0102     echo "optimizegraphics: Losslessly optimized PNG, MNG, and SVGZ files.";
0103     echo "Went from $STARTSIZE to $ENDSIZE bytes"
0104     echo "Reduced " $REDUCEDPER" % disk space:" $REDUCEDSIZE" Bytes, " \
0105         $(( $REDUCEDSIZE / 1024 )) "KiB, " $(( $REDUCEDSIZE / 1024 / 1024 )) "MiB";
0106 fi