Commit b8c85c2e authored by pkasting@chromium.org's avatar pkasting@chromium.org

Update optimize_png_files.sh to work on msysgit bash.

This bash is moderately archaic (version 3), and various tools (e.g. tput) are
missing.

This also modifies the ouput of the script so that in verbose mode, as it runs,
each filename is trimmed to fit on one line, including the throbber, then
replaced by the full filename when printing the final file status.  This makes
for a cleaner output spew with long filenames in particular.

BUG=none
TEST=none
R=oshima@chromium.org

Review URL: https://codereview.chromium.org/291153007

git-svn-id: svn://svn.chromium.org/chrome/trunk/src@272364 0039d316-1c4b-4281-b951-d872f2087c98
parent 1d25a02b
#!/bin/bash #!/bin/bash -i
# Copyright 2013 The Chromium Authors. All rights reserved. # Copyright 2013 The Chromium Authors. All rights reserved.
# Use of this source code is governed by a BSD-style license that can be # Use of this source code is governed by a BSD-style license that can be
# found in the LICENSE file. # found in the LICENSE file.
...@@ -54,11 +54,11 @@ PROCESSED_FILE=0 ...@@ -54,11 +54,11 @@ PROCESSED_FILE=0
declare -a THROBBER_STR=('-' '\\' '|' '/') declare -a THROBBER_STR=('-' '\\' '|' '/')
THROBBER_COUNT=0 THROBBER_COUNT=0
VERBOSE=0 VERBOSE=false
# Echo only if verbose option is set. # Echo only if verbose option is set.
function info { function info {
if [ $VERBOSE -eq 1 ]; then if $VERBOSE ; then
echo $@ echo $@
fi fi
} }
...@@ -66,7 +66,8 @@ function info { ...@@ -66,7 +66,8 @@ function info {
# Show throbber character at current cursor position. # Show throbber character at current cursor position.
function throbber { function throbber {
info -ne "${THROBBER_STR[$THROBBER_COUNT]}\b" info -ne "${THROBBER_STR[$THROBBER_COUNT]}\b"
let THROBBER_COUNT=($THROBBER_COUNT+1)%4 let THROBBER_COUNT=$THROBBER_COUNT+1
let THROBBER_COUNT=$THROBBER_COUNT%4
} }
# Usage: pngout_loop <file> <png_out_options> ... # Usage: pngout_loop <file> <png_out_options> ...
...@@ -77,13 +78,13 @@ function pngout_loop { ...@@ -77,13 +78,13 @@ function pngout_loop {
shift shift
local opts=$* local opts=$*
if [ $OPTIMIZE_LEVEL == 1 ]; then if [ $OPTIMIZE_LEVEL == 1 ]; then
for j in $(seq 0 5); do for j in $(eval echo {0..5}); do
throbber throbber
pngout -q -k1 -s1 -f$j $opts $file pngout -q -k1 -s1 -f$j $opts $file
done done
else else
for i in 0 128 256 512; do for i in 0 128 256 512; do
for j in $(seq 0 5); do for j in $(eval echo {0..5}); do
throbber throbber
pngout -q -k1 -s1 -b$i -f$j $opts $file pngout -q -k1 -s1 -b$i -f$j $opts $file
done done
...@@ -106,7 +107,7 @@ function get_color_depth_list { ...@@ -106,7 +107,7 @@ function get_color_depth_list {
# #
# TODO(oshima): Experiment with -d0 w/o -c0. # TODO(oshima): Experiment with -d0 w/o -c0.
function process_grayscale { function process_grayscale {
info -n "|gray" info -ne "\b\b\b\b\b\b\b\bgray...."
for opt in $(get_color_depth_list); do for opt in $(get_color_depth_list); do
pngout_loop $file -c0 $opt pngout_loop $file -c0 $opt
done done
...@@ -115,7 +116,7 @@ function process_grayscale { ...@@ -115,7 +116,7 @@ function process_grayscale {
# Usage: process_grayscale_alpha <file> # Usage: process_grayscale_alpha <file>
# Optimize grayscale images with alpha for all color bit depths. # Optimize grayscale images with alpha for all color bit depths.
function process_grayscale_alpha { function process_grayscale_alpha {
info -n "|gray-a" info -ne "\b\b\b\b\b\b\b\bgray-a.."
pngout_loop $file -c4 pngout_loop $file -c4
for opt in $(get_color_depth_list); do for opt in $(get_color_depth_list); do
pngout_loop $file -c3 $opt pngout_loop $file -c3 $opt
...@@ -125,7 +126,7 @@ function process_grayscale_alpha { ...@@ -125,7 +126,7 @@ function process_grayscale_alpha {
# Usage: process_rgb <file> # Usage: process_rgb <file>
# Optimize rgb images with or without alpha for all color bit depths. # Optimize rgb images with or without alpha for all color bit depths.
function process_rgb { function process_rgb {
info -n "|rgb" info -ne "\b\b\b\b\b\b\b\brgb....."
for opt in $(get_color_depth_list); do for opt in $(get_color_depth_list); do
pngout_loop $file -c3 $opt pngout_loop $file -c3 $opt
done done
...@@ -136,8 +137,8 @@ function process_rgb { ...@@ -136,8 +137,8 @@ function process_rgb {
# Usage: huffman_blocks <file> # Usage: huffman_blocks <file>
# Optimize the huffman blocks. # Optimize the huffman blocks.
function huffman_blocks { function huffman_blocks {
info -ne "\b\b\b\b\b\b\b\bhuffman."
local file=$1 local file=$1
info -n "|huffman"
local size=$(stat -c%s $file) local size=$(stat -c%s $file)
local min_block_size=$DEFAULT_MIN_BLOCK_SIZE local min_block_size=$DEFAULT_MIN_BLOCK_SIZE
local limit_blocks=$DEFAULT_LIMIT_BLOCKS local limit_blocks=$DEFAULT_LIMIT_BLOCKS
...@@ -151,7 +152,7 @@ function huffman_blocks { ...@@ -151,7 +152,7 @@ function huffman_blocks {
max_blocks=$limit_blocks max_blocks=$limit_blocks
fi fi
for i in $(seq 2 $max_blocks); do for i in $(eval echo {2..$max_blocks}); do
throbber throbber
pngout -q -k1 -ks -s1 -n$i $file pngout -q -k1 -ks -s1 -n$i $file
done done
...@@ -163,7 +164,7 @@ function huffman_blocks { ...@@ -163,7 +164,7 @@ function huffman_blocks {
# TODO(oshima): Try adjusting different parameters for large files to # TODO(oshima): Try adjusting different parameters for large files to
# reduce runtime. # reduce runtime.
function random_huffman_table_trial { function random_huffman_table_trial {
info -n "|random" info -ne "\b\b\b\b\b\b\b\brandom.."
local file=$1 local file=$1
local old_size=$(stat -c%s $file) local old_size=$(stat -c%s $file)
local trials_count=$DEFAULT_RANDOM_TRIALS local trials_count=$DEFAULT_RANDOM_TRIALS
...@@ -171,7 +172,7 @@ function random_huffman_table_trial { ...@@ -171,7 +172,7 @@ function random_huffman_table_trial {
if [ $old_size -gt $LARGE_FILE_THRESHOLD ]; then if [ $old_size -gt $LARGE_FILE_THRESHOLD ]; then
trials_count=$LARGE_RANDOM_TRIALS trials_count=$LARGE_RANDOM_TRIALS
fi fi
for i in $(seq 1 $trials_count); do for i in $(eval echo {1..$trials_count}); do
throbber throbber
pngout -q -k1 -ks -s0 -r $file pngout -q -k1 -ks -s0 -r $file
done done
...@@ -185,7 +186,7 @@ function random_huffman_table_trial { ...@@ -185,7 +186,7 @@ function random_huffman_table_trial {
# Further compress using optipng and advdef. # Further compress using optipng and advdef.
# TODO(oshima): Experiment with 256. # TODO(oshima): Experiment with 256.
function final_compression { function final_compression {
info -n "|final" info -ne "\b\b\b\b\b\b\b\bfinal..."
local file=$1 local file=$1
if [ $OPTIMIZE_LEVEL == 2 ]; then if [ $OPTIMIZE_LEVEL == 2 ]; then
for i in 32k 16k 8k 4k 2k 1k 512; do for i in 32k 16k 8k 4k 2k 1k 512; do
...@@ -193,21 +194,25 @@ function final_compression { ...@@ -193,21 +194,25 @@ function final_compression {
optipng -q -nb -nc -zw$i -zc1-9 -zm1-9 -zs0-3 -f0-5 $file optipng -q -nb -nc -zw$i -zc1-9 -zm1-9 -zs0-3 -f0-5 $file
done done
fi fi
for i in $(seq 1 4); do for i in $(eval echo {1..4}); do
throbber throbber
advdef -q -z -$i $file advdef -q -z -$i $file
done done
info -ne "\r"
# Clear the current line.
if $VERBOSE ; then
printf "\033[0G\033[K"
fi
} }
# Usage: get_color_type <file> # Usage: get_color_type <file>
# Returns the color type name of the png file. Here is the list of names # Returns the color type name of the png file. Here is the list of names
# for each color type codes. # for each color type codes.
# 0 : grayscale # 0: grayscale
# 2 : RGB # 2: RGB
# 3 : colormap # 3: colormap
# 4 : gray+alpha # 4: gray+alpha
# 6 : RGBA # 6: RGBA
# See http://en.wikipedia.org/wiki/Portable_Network_Graphics#Color_depth # See http://en.wikipedia.org/wiki/Portable_Network_Graphics#Color_depth
# for details about the color type code. # for details about the color type code.
function get_color_type { function get_color_type {
...@@ -218,9 +223,17 @@ function get_color_type { ...@@ -218,9 +223,17 @@ function get_color_type {
# Usage: optimize_size <file> # Usage: optimize_size <file>
# Performs png file optimization. # Performs png file optimization.
function optimize_size { function optimize_size {
tput el # Print filename, trimmed to ensure it + status don't take more than 1 line
local filename_length=${#file}
local -i allowed_length=$COLUMNS-11
local -i trimmed_length=$filename_length-$COLUMNS+14
if [ "$filename_length" -lt "$allowed_length" ]; then
info -n "$file|........"
else
info -n "...${file:$trimmed_length}|........"
fi
local file=$1 local file=$1
info -n "$file "
advdef -q -z -4 $file advdef -q -z -4 $file
...@@ -239,7 +252,7 @@ function optimize_size { ...@@ -239,7 +252,7 @@ function optimize_size {
fi fi
fi fi
info -n "|filter" info -ne "\b\b\b\b\b\b\b\bfilter.."
local old_color_type=$(get_color_type $file) local old_color_type=$(get_color_type $file)
optipng -q -zc9 -zm8 -zs0-3 -f0-5 $file -out $file.tmp.png optipng -q -zc9 -zm8 -zs0-3 -f0-5 $file -out $file.tmp.png
local new_color_type=$(get_color_type $file.tmp.png) local new_color_type=$(get_color_type $file.tmp.png)
...@@ -248,9 +261,8 @@ function optimize_size { ...@@ -248,9 +261,8 @@ function optimize_size {
# the bug is fixed. See crbug.com/174505, crbug.com/174084. # the bug is fixed. See crbug.com/174505, crbug.com/174084.
# The issue is reported in # The issue is reported in
# https://sourceforge.net/tracker/?func=detail&aid=3603630&group_id=151404&atid=780913 # https://sourceforge.net/tracker/?func=detail&aid=3603630&group_id=151404&atid=780913
if [[ $old_color_type == "RGBA" && $new_color_type =~ gray.* ]] ; then if [[ $old_color_type == "RGBA" && $new_color_type == gray* ]] ; then
rm $file.tmp.png rm $file.tmp.png
info -n "[skip opting]"
else else
mv $file.tmp.png $file mv $file.tmp.png $file
fi fi
...@@ -259,7 +271,7 @@ function optimize_size { ...@@ -259,7 +271,7 @@ function optimize_size {
huffman_blocks $file huffman_blocks $file
# TODO(oshima): Experiment with strategy 1. # TODO(oshima): Experiment with strategy 1.
info -n "|strategy" info -ne "\b\b\b\b\b\b\b\bstrategy"
if [ $OPTIMIZE_LEVEL == 2 ]; then if [ $OPTIMIZE_LEVEL == 2 ]; then
for i in 3 2 0; do for i in 3 2 0; do
pngout -q -k1 -ks -s$i $file pngout -q -k1 -ks -s$i $file
...@@ -280,7 +292,7 @@ function process_file { ...@@ -280,7 +292,7 @@ function process_file {
local file=$1 local file=$1
local name=$(basename $file) local name=$(basename $file)
# -rem alla removes all ancillary chunks except for tRNS # -rem alla removes all ancillary chunks except for tRNS
pngcrush -d $TMP_DIR -brute -reduce -rem alla $file > /dev/null pngcrush -d $TMP_DIR -brute -reduce -rem alla $file > /dev/null 2>&1
if [ -f $TMP_DIR/$name -a $OPTIMIZE_LEVEL != 0 ]; then if [ -f $TMP_DIR/$name -a $OPTIMIZE_LEVEL != 0 ]; then
optimize_size $TMP_DIR/$name optimize_size $TMP_DIR/$name
...@@ -301,26 +313,28 @@ function optimize_file { ...@@ -301,26 +313,28 @@ function optimize_file {
process_file $file process_file $file
if [ ! -e $tmp_file ] ; then if [ ! -e $tmp_file ] ; then
let CORRUPTED_FILE+=1 let CORRUPTED_FILE+=1
echo "The png file ($file) may be corrupted. skipping" echo "$file may be corrupted; skipping\n"
return return
fi fi
local new=$(stat -c%s $tmp_file) local new=$(stat -c%s $tmp_file)
let diff=$old-$new let diff=$old-$new
let percent=($diff*100)/$old let percent=$diff*100
let percent=$percent/$old
tput el
if [ $new -lt $old ]; then if [ $new -lt $old ]; then
echo -ne "$file : $old => $new ($diff bytes : $percent %)\n" info "$file: $old => $new ($diff bytes: $percent%)"
mv "$tmp_file" "$file" cp "$tmp_file" "$file"
let TOTAL_OLD_BYTES+=$old let TOTAL_OLD_BYTES+=$old
let TOTAL_NEW_BYTES+=$new let TOTAL_NEW_BYTES+=$new
let PROCESSED_FILE+=1 let PROCESSED_FILE+=1
else else
if [ $OPTIMIZE_LEVEL == 0 ]; then if [ $OPTIMIZE_LEVEL == 0 ]; then
info -ne "$file : skipped\r" info "$file: Skipped"
else
info "$file: Unable to reduce size"
fi fi
rm $tmp_file rm $tmp_file
fi fi
...@@ -400,6 +414,19 @@ else ...@@ -400,6 +414,19 @@ else
using_cygwin=false using_cygwin=false
fi fi
# The -i in the shebang line should result in $COLUMNS being set on newer
# versions of bash. If it's not set yet, attempt to set it.
if [ -z $COLUMNS ]; then
which tput > /dev/null 2>&1
if [ "$?" == "0" ]; then
COLUMNS=$(tput cols)
else
# No tput either... give up and just guess 80 columns.
COLUMNS=80
fi
export COLUMNS
fi
OPTIMIZE_LEVEL=1 OPTIMIZE_LEVEL=1
# Parse options # Parse options
while getopts o:r:h:v opts while getopts o:r:h:v opts
...@@ -413,13 +440,13 @@ do ...@@ -413,13 +440,13 @@ do
fi fi
;; ;;
o) o)
if [[ ! "$OPTARG" =~ [012] ]] ; then if [[ "$OPTARG" != 0 && "$OPTARG" != 1 && "$OPTARG" != 2 ]] ; then
show_help show_help
fi fi
OPTIMIZE_LEVEL=$OPTARG OPTIMIZE_LEVEL=$OPTARG
;; ;;
v) v)
let VERBOSE=1 VERBOSE=true
;; ;;
[h?]) [h?])
show_help;; show_help;;
...@@ -455,7 +482,7 @@ if $using_cygwin ; then ...@@ -455,7 +482,7 @@ if $using_cygwin ; then
fi fi
# Make sure we cleanup temp dir # Make sure we cleanup temp dir
trap "rm -rf $TMP_DIR" EXIT #trap "rm -rf $TMP_DIR" EXIT
# If no directories are specified, optimize all directories. # If no directories are specified, optimize all directories.
DIRS=$@ DIRS=$@
...@@ -489,17 +516,13 @@ else ...@@ -489,17 +516,13 @@ else
fi fi
# Print the results. # Print the results.
if [ $PROCESSED_FILE == 0 ]; then echo "Optimized $PROCESSED_FILE/$TOTAL_FILE files in" \
echo "Did not find any files (out of $TOTAL_FILE files)" \ "$(date -d "0 + $SECONDS sec" +%Ts)"
"that could be optimized" \ if [ $PROCESSED_FILE != 0 ]; then
"in $(date -u -d @$SECONDS +%T)s"
else
let diff=$TOTAL_OLD_BYTES-$TOTAL_NEW_BYTES let diff=$TOTAL_OLD_BYTES-$TOTAL_NEW_BYTES
let percent=$diff*100/$TOTAL_OLD_BYTES let percent=$diff*100/$TOTAL_OLD_BYTES
echo "Processed $PROCESSED_FILE files (out of $TOTAL_FILE files)" \ echo "Result: $TOTAL_OLD_BYTES => $TOTAL_NEW_BYTES bytes" \
"in $(date -u -d @$SECONDS +%T)s" "($diff bytes: $percent%)"
echo "Result : $TOTAL_OLD_BYTES => $TOTAL_NEW_BYTES bytes" \
"($diff bytes : $percent %)"
fi fi
if [ $CORRUPTED_FILE != 0 ]; then if [ $CORRUPTED_FILE != 0 ]; then
echo "Warning: corrupted files found: $CORRUPTED_FILE" echo "Warning: corrupted files found: $CORRUPTED_FILE"
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment