[prev in list] [next in list] [prev in thread] [next in thread] 

List:       kde-commits
Subject:    KDE/kdesdk/scripts
From:       Michael Pyne <mpyne () kde ! org>
Date:       2013-01-13 19:58:25
Message-ID: 20130113195825.D860AAC86C () svn ! kde ! org
[Download RAW message or body]

SVN commit 1332557 by mpyne:

optimizegraphics: Add support for pngout and AdvanceCOMP.

The work and testing leading to this patch were created and graciously improved
after review by Bruno George Moraes. It adds several things:

- Usage of the pngout tool, written by the author of the Duke Nukem 3D game
  engine. (pngout is free to download the binary of, but is non-Free software).
- Usage of the AdvanceCOMP tool, which is similar to OptiPNG, but uses the 7z
  library (so it optimizes not just PNG but also things like .svgz and .mng).
- Parallel execution of these tools to recursively optimize a subdirectory of
  images.

I've further improved it to meet coding style, remove a needless bash-ism, fix
some of the math code, and quiet the output a bit. Additionally it will use any
tools you have available instead of requiring you to run non-Free software to
work at all.

With parallel execution it is important to limit the number of simultaneous
processes xargs will run, we try grepping in /proc/cpu using Bruno's code, but
I limit it further to 4 tasks if that fails.

Thanks to Bruno for the patch and his patience, I figure this will probably be
one of the last SVN commits to kdesdk ever before we convert to git.

BUG:298935
REVIEW:7000
FIXED-IN:4.11


 M  +71 -21    optimizegraphics  


--- trunk/KDE/kdesdk/scripts/optimizegraphics #1332556:1332557
@@ -1,6 +1,7 @@
 #!/bin/sh
 
 # Copyright 2008 Urs Wolfer <uwolfer @ kde.org>
+# Copyright 2012, 2013 Bruno George Moraes <brunogm0 @ gmail.com>
 # 
 # This program is free software; you can redistribute it and/or
 # modify it under the terms of the GNU General Public License as
@@ -13,44 +14,93 @@
 # GNU General Public License for more details.
 # 
 #
-# Thin wrapper script around optipng and advdef.
+# Thin wrapper script around optipng, advdef and pngout.
 # It filters the output and shows a summary at the end of the optimization run.
 # Tested with:
-# * OptiPNG 0.5.5 http://optipng.sourceforge.net/
+# * OptiPNG 0.7.1 http://optipng.sourceforge.net/
 # * advdef (AdvanceCOMP) 1.15 http://advancemame.sourceforge.net/comp-readme.html
+# * PNGOUT http://www.jonof.id.au/kenutils (Note: no-cost download; non-Free \
software)  
-if [ ! -e /usr/bin/advdef ]; then
-    echo "Please install advancecomp";
-    exit;
-fi
+# ${foo:-bar} syntax sets foo=bar if not already set
+optipng_tool=$(command -v optipng)
+optipng_tool=${optipng_tool:-/bin/true}
 
-if [ ! -e /usr/bin/optipng ]; then
-    echo "Please install optipng";
-    exit;
-fi
+advancecomp_tool=$(command -v advdef)
+advancecomp_tool=${advancecomp_tool:-/bin/true}
 
+pngout_tool=$(command -v pngout)
+pngout_tool=${pngout_tool:-/bin/true}
+
+thread_count=$(grep -c ^processor /proc/cpuinfo)
+thread_count=${thread_count:-4}
+
 if test "$1" = "-h" -o "$1" = "--help"; then
     echo "Usage: optimizegraphics [FILE]";
-    echo "If [FILE] is not defined, it optimizes all files recursively starting at \
the current working directory."; +    echo "If [FILE] is not defined, it optimizes \
all files (PNG,MNG,SVGZ) recursively starting at the current working directory."; +   \
echo "To achieve the best optimization please use all supported tools \
(optipng,advdef,pngout).";  exit;
 fi
 
+if [ ${optipng_tool} = "/bin/true" ]; then
+    echo "Please install optipng to increase PNG compression.";
+fi
+if [ ${pngout_tool} = "/bin/true" ];  then
+    echo "Please install pngout to increase PNG compression.";
+fi
+if [ ${advancecomp_tool} = "/bin/true" ]; then
+    echo "Please install advancecomp to optimize PNG, MNG and SVGZ.";
+fi
+
 if [ $# -ne 0 ]; then # file is defined
-    optipng -o5 "$1";
-    advdef -z -4 "$1";
-exit;
+    if [ ! -e "$1" ]; then
+        echo "File $1 doesn't exist!"
+        exit 1
+    fi
+
+    ${optipng_tool} -preserve -o6 "$1";
+    ${advancecomp_tool} -z -4 "$1";
+    ${pngout_tool} -ks "$1";
+
+    exit $?
+
 else # do it recursively
-    STARTSIZE=`du -s | awk '{ print $1 }'`;
+    echo "Recursive parallel optimization using detected tools.";
+    STARTSIZE=`du -sb | awk '{ print $1 }'`;
 
-    find . -name "*.png" -exec optipng -o5 '{}' \; -print | grep \
                'Processing\|already\|% decrease';
-    find . -name "*.png" -exec advdef -z -4 '{}' \; -print | grep '% ./';
+    # OptiPNG pass
+    if [ "${optipng_tool}" != "/bin/true" ] ; then
+        find . -name "*.png" -print0 | \
+            xargs -t -0 --no-run-if-empty --max-args=1 --max-procs=$thread_count \
+                "${optipng_tool}" -quiet -preserve -o6
+    else
+        echo "Skipping OptiPNG optimizations for PNG file type"
+    fi
  
-    find . -name "*.svgz" -exec advdef -z -4 '{}' \; -print | grep '% ./';
+    # AdvanceCOMP pass
+    if [ "${advancecomp_tool}" != "/bin/true" ] ; then
+        find . \( -name "*.svgz" -o -name '*.mng' -o -name '*.png' \) -print0 | \
+            xargs -t -0 --no-run-if-empty --max-args=1 --max-procs=$thread_count \
+                "${advancecomp_tool}" -q -z -4
+    else
+        echo "Skipping AdvanceCOMP optimizations for SVGZ, MNG, PNG file types"
+    fi
 
-    ENDSIZE=`du -s | awk '{ print $1 }'`;
+    # PNGout pass
+    if [ "${pngout_tool}" != "/bin/true" ] ; then
+        find . -name "*.png" -print0 | \
+            xargs -t -0 --no-run-if-empty --max-args=1 --max-procs=$thread_count \
+                "${pngout_tool}" -q -ks
+    else
+        echo "Skipping PNGout optimizations for PNG file type"
+    fi
 
+    ENDSIZE=`du -sb | awk '{ print $1 }'`;
+
     REDUCEDSIZE=$(($STARTSIZE-$ENDSIZE));
+    REDUCEDPER=$(( 100 * $REDUCEDSIZE / $STARTSIZE ));
 
-    echo "optimizegraphics: Losslessly optimized PNG and SVGZ files with \"optipng \
                -o5\" and \"advdef -z -4\".";
-    echo "Reduced disk space:" $REDUCEDSIZE"KB ("$(($REDUCEDSIZE/1024))"MB)";
+    echo "optimizegraphics: Losslessly optimized PNG, MNG, and SVGZ files.";
+    echo "Went from $STARTSIZE to $ENDSIZE bytes"
+    echo "Reduced " $REDUCEDPER" % disk space:" $REDUCEDSIZE" Bytes, " \
+        $(( $REDUCEDSIZE / 1024 )) "KiB, " $(( $REDUCEDSIZE / 1024 / 1024 )) "MiB";
 fi


[prev in list] [next in list] [prev in thread] [next in thread] 

Configure | About | News | Add a list | Sponsored by KoreLogic