File: optimizegraphics

package info (click to toggle)
kde-dev-scripts 4%3A25.04.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 3,612 kB
  • sloc: perl: 15,615; lisp: 5,627; sh: 4,560; python: 3,892; ruby: 1,386; makefile: 13; sed: 9
file content (106 lines) | stat: -rwxr-xr-x 3,777 bytes parent folder | download | duplicates (6)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
#!/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
# published by the Free Software Foundation; either version 2 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.
#
#
# 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.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)

# ${foo:-bar} syntax sets foo=bar if not already set
optipng_tool=$(command -v optipng)
optipng_tool=${optipng_tool:-/bin/true}

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 (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
    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
    echo "Recursive parallel optimization using detected tools.";
    STARTSIZE=`du -sb | awk '{ print $1 }'`;

    # OptiPNG pass
    if [ "${optipng_tool}" != "/bin/true" ] ; then
        find . -name "*.png" -print0 | \
            xargs -t0r -n 1 -P $thread_count \
                "${optipng_tool}" -quiet -preserve -o6
    else
        echo "Skipping OptiPNG optimizations for PNG file type"
    fi

    # AdvanceCOMP pass
    if [ "${advancecomp_tool}" != "/bin/true" ] ; then
        find . \( -name "*.svgz" -o -name '*.mng' -o -name '*.png' \) -print0 | \
            xargs -t0r -n 1 -P $thread_count \
                "${advancecomp_tool}" -q -z -4
    else
        echo "Skipping AdvanceCOMP optimizations for SVGZ, MNG, PNG file types"
    fi

    # PNGout pass
    if [ "${pngout_tool}" != "/bin/true" ] ; then
        find . -name "*.png" -print0 | \
            xargs -t0r -n 1 -P $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, 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