Tools: Migrate compress-pngs.sh to Python.

Migrate compress-pngs from a Bash script that ran Make to a Python
script, which should be usable on more platforms.

Add Efficient Compression Tool (ect) to the list of compressors.

Add the compressors to the various *-setup.sh scripts, but comment them
out for now.
This commit is contained in:
Gerald Combs 2021-09-12 13:45:54 -07:00
parent 77e6110c59
commit 55a67fd66a
9 changed files with 120 additions and 49 deletions

View File

@ -3636,7 +3636,6 @@ if(SHELLCHECK_EXECUTABLE)
packaging/macosx/osx-app.sh.in
packaging/macosx/osx-dmg.sh.in
packaging/source/git-export-release.sh.in
tools/compress-pngs.sh
tools/debian-setup.sh
tools/fuzz-test.sh
tools/gen-bugnote

View File

@ -78,7 +78,7 @@ set(WSUG_FILES
${COMMON_FILES}
)
# Note: Images should be minimized using tools/compress-pngs.sh.
# Note: Images should be minimized using tools/compress-pngs.py.
set(WSUG_GRAPHICS
wsug_graphics/related-ack.png
wsug_graphics/related-current.png

View File

@ -77,6 +77,13 @@ ADDITIONAL_LIST="
brotli-dev \
"
# Uncomment to add PNG compression utilities used by compress-pngs:
# ADDITIONAL_LIST="$ADDITIONAL_LIST \
# advancecomp \
# optipng \
# oxipng \
# pngcrush"
# Adds package $2 to list variable $1 if the package is found.
# If $3 is given, then this version requirement must be satisfied.
add_package() {

View File

@ -58,6 +58,12 @@ ADDITIONAL_LIST="\
lua52 \
"
# Uncomment to add PNG compression utilities used by compress-pngs:
# ADDITIONAL_LIST="$ADDITIONAL_LIST \
# advancecomp \
# optipng \
# pngcrush"
# Guess which package manager we will use
PM=`which pkgin 2> /dev/null || which pkg 2> /dev/null || which pkg_add 2> /dev/null`

89
tools/compress-pngs.py Executable file
View File

@ -0,0 +1,89 @@
#!/usr/bin/env python3
#
# Compress PNGs
#
# By Gerald Combs <gerald@wireshark.org
#
# SPDX-License-Identifier: GPL-2.0-or-later
#
'''Run various compression and optimization utilities on one or more PNGs'''
import argparse
import concurrent.futures
import shutil
import subprocess
import sys
PNG_FILE_ARG = '%PNG_FILE_ARG%'
def get_compressors():
# Add *lossless* compressors here.
compressors = {
# https://github.com/shssoichiro/oxipng
'oxipng': { 'args': ['--opt', 'max', '--strip', 'safe', PNG_FILE_ARG] },
# http://optipng.sourceforge.net/
'optipng': { 'args': ['-o3', '-quiet', PNG_FILE_ARG] },
# https://github.com/amadvance/advancecomp
'advpng': { 'args': ['--recompress', '--shrink-insane', PNG_FILE_ARG] },
# https://github.com/amadvance/advancecomp
'advdef': { 'args': ['--recompress', '--shrink-insane', PNG_FILE_ARG] },
# https://pmt.sourceforge.io/pngcrush/
'pngcrush': { 'args': ['-q', '-ow', '-brute', '-reduce', '-noforce', PNG_FILE_ARG, 'pngcrush.$$$$.png'] },
# https://github.com/fhanau/Efficient-Compression-Tool
'ect': { 'args': ['-5', '--mt-deflate', '--mt-file', '-strip', PNG_FILE_ARG]}
}
for compressor in compressors:
compressor_path = shutil.which(compressor)
if compressor_path:
compressors[compressor]['path'] = compressor_path
return compressors
def compress_png(png_file, compressors):
for compressor in compressors:
if not compressors[compressor].get('path', False):
next
args = compressors[compressor]['args']
args = [arg.replace(PNG_FILE_ARG, png_file) for arg in args]
try:
compress_proc = subprocess.run([compressor] + args)
except Exception:
print('{} returned {}:'.format(compressor, compress_proc.returncode))
def main():
parser = argparse.ArgumentParser(description='Compress PNGs')
parser.add_argument('--list', action='store_true',
help='List available compressors')
parser.add_argument('png_files', nargs='+', metavar='png file', help='Files to compress')
args = parser.parse_args()
compressors = get_compressors()
c_count = 0
for compressor in compressors:
if 'path' in compressors[compressor]:
c_count += 1
if c_count < 1:
sys.stderr.write('No compressors found\n')
sys.exit(1)
if args.list:
for compressor in compressors:
path = compressors[compressor].get('path', 'Not found')
print('{}: {}'.format(compressor, path))
sys.exit(0)
with concurrent.futures.ProcessPoolExecutor() as executor:
futures = []
for png_file in args.png_files:
print('Compressing {}'.format(png_file))
futures.append(executor.submit(compress_png, png_file, compressors))
concurrent.futures.wait(futures)
if __name__ == '__main__':
main()

View File

@ -1,46 +0,0 @@
#!/bin/bash
#
# compress-pngs.sh
# Run various compression and optimization utilities on one or more PNGs
#
# Wireshark - Network traffic analyzer
# By Gerald Combs <gerald@wireshark.org>
# Copyright 2013 Gerald Combs
#
# SPDX-License-Identifier: GPL-2.0-or-later
#
while getopts h OPTCHAR
do
case $OPTCHAR in
h|?)
echo "Usage: compress-pngs.sh file1.png [file2.png] ..." 1>&1
exit 0
;;
esac
done
# Other utilities:
# PNGOUT (http://advsys.net/ken/utils.htm). Closed source.
# pngquant (https://pngquant.org/). Lossy.
JOBS=8
PNG_FILES=$(printf "%q " "$@")
export PNG_FILES
(
cat <<"FIN"
all: $(PNG_FILES)
$(PNG_FILES): FORCE
@echo Compressing $@
@hash oxipng 2>/dev/null && oxipng --opt 4 --strip safe "$@"
@hash optipng 2>/dev/null && optipng -o3 -quiet "$@"
@hash advpng 2>/dev/null && advpng --recompress --shrink-insane "$@"
@hash advdef 2>/dev/null && advdef --recompress --shrink-insane "$@"
@hash pngcrush 2>/dev/null && pngcrush -q -ow -brute -reduce -noforce "$@" pngout.$$$$.png
FORCE:
FIN
) | make -j $JOBS -f -

View File

@ -89,6 +89,12 @@ ADDITIONAL_LIST="libnl-3-dev \
xsltproc \
libspeexdsp-dev"
# Uncomment to add PNG compression utilities used by compress-pngs:
# ADDITIONAL_LIST="$ADDITIONAL_LIST \
# advancecomp \
# optipng \
# pngcrush"
DEBDEPS_LIST="debhelper \
dh-python \
docbook-xml \

View File

@ -19,6 +19,9 @@ brew install sparkle opus c-ares glib libgcrypt gnutls lua@5.1 cmake python nght
# Uncomment to enable automatic updates using Sparkle
# brew cask install sparkle
# Uncomment to add PNG compression utilities used by compress-pngs:
# brew install advancecomp optipng oxipng pngcrush
brew doctor
exit 0

View File

@ -56,7 +56,7 @@ BASIC_LIST="cmake \
glib2-devel \
libpcap-devel \
zlib-devel \
libgcrypt-devel"
libgcrypt-devel"
ADDITIONAL_LIST="libcap-devel \
libssh-devel \
@ -69,6 +69,13 @@ ADDITIONAL_LIST="libcap-devel \
spandsp-devel \
systemd-devel"
# Uncomment to add PNG compression utilities used by compress-pngs:
# ADDITIONAL_LIST="$ADDITIONAL_LIST \
# advancecomp \
# optipng \
# oxipng \
# pngcrush"
RPMDEPS_LIST="rpm-build"
# Guess which package manager we will use