2 # Copyright 2013 The Chromium Authors. All rights reserved.
3 # Use of this source code is governed by a BSD-style license that can be
4 # found in the LICENSE file.
6 # The optimization code is based on pngslim (http://goo.gl/a0XHg)
7 # and executes a similar pipleline to optimize the png file size.
8 # The steps that require pngoptimizercl/pngrewrite/deflopt are omitted,
9 # but this runs all other processes, including:
10 # 1) various color-dependent optimizations using optipng.
11 # 2) optimize the number of huffman blocks.
12 # 3) randomize the huffman table.
13 # 4) Further optimize using optipng and advdef (zlib stream).
14 # Due to the step 3), each run may produce slightly different results.
16 # Note(oshima): In my experiment, advdef didn't reduce much. I'm keeping it
17 # for now as it does not take much time to run.
21 chrome/android/java/res
23 chrome/browser/resources
24 chrome/renderer/resources
25 content/public/android/java/res
26 content/renderer/resources
27 content/shell/resources
30 ui/webui/resources/images
32 win8/metro_driver/resources
35 # Files larger than this file size (in bytes) will
36 # use the optimization parameters tailored for large files.
37 LARGE_FILE_THRESHOLD
=3000
39 # Constants used for optimization
40 readonly DEFAULT_MIN_BLOCK_SIZE
=128
41 readonly DEFAULT_LIMIT_BLOCKS
=256
42 readonly DEFAULT_RANDOM_TRIALS
=100
43 # Taken from the recommendation in the pngslim's readme.txt.
44 readonly LARGE_MIN_BLOCK_SIZE
=1
45 readonly LARGE_LIMIT_BLOCKS
=2
46 readonly LARGE_RANDOM_TRIALS
=1
48 # Global variables for stats
55 declare -a THROBBER_STR
=('-' '\\' '|' '/')
60 # Echo only if verbose option is set.
67 # Show throbber character at current cursor position.
69 info
-ne "${THROBBER_STR[$THROBBER_COUNT]}\b"
70 let THROBBER_COUNT
=$THROBBER_COUNT+1
71 let THROBBER_COUNT
=$THROBBER_COUNT%4
74 # Usage: pngout_loop <file> <png_out_options> ...
75 # Optimize the png file using pngout with the given options
76 # using various block split thresholds and filter types.
77 function pngout_loop
{
81 if [ $OPTIMIZE_LEVEL == 1 ]; then
82 for j
in $
(eval echo {0.
.5}); do
84 pngout
-q -k1 -s1 -f$j $opts $file
87 for i
in 0 128 256 512; do
88 for j
in $
(eval echo {0.
.5}); do
90 pngout
-q -k1 -s1 -b$i -f$j $opts $file
96 # Usage: get_color_depth_list
97 # Returns the list of color depth options for current optimization level.
98 function get_color_depth_list
{
99 if [ $OPTIMIZE_LEVEL == 1 ]; then
102 echo "-d1 -d2 -d4 -d8"
106 # Usage: process_grayscale <file>
107 # Optimize grayscale images for all color bit depths.
109 # TODO(oshima): Experiment with -d0 w/o -c0.
110 function process_grayscale
{
111 info
-ne "\b\b\b\b\b\b\b\bgray...."
112 for opt
in $
(get_color_depth_list
); do
113 pngout_loop
$file -c0 $opt
117 # Usage: process_grayscale_alpha <file>
118 # Optimize grayscale images with alpha for all color bit depths.
119 function process_grayscale_alpha
{
120 info
-ne "\b\b\b\b\b\b\b\bgray-a.."
121 pngout_loop
$file -c4
122 for opt
in $
(get_color_depth_list
); do
123 pngout_loop
$file -c3 $opt
127 # Usage: process_rgb <file>
128 # Optimize rgb images with or without alpha for all color bit depths.
129 function process_rgb
{
130 info
-ne "\b\b\b\b\b\b\b\brgb....."
131 for opt
in $
(get_color_depth_list
); do
132 pngout_loop
$file -c3 $opt
134 pngout_loop
$file -c2
135 pngout_loop
$file -c6
138 # Usage: huffman_blocks <file>
139 # Optimize the huffman blocks.
140 function huffman_blocks
{
141 info
-ne "\b\b\b\b\b\b\b\bhuffman."
143 local size
=$
(stat
-c%s
$file)
144 local min_block_size
=$DEFAULT_MIN_BLOCK_SIZE
145 local limit_blocks
=$DEFAULT_LIMIT_BLOCKS
147 if [ $size -gt $LARGE_FILE_THRESHOLD ]; then
148 min_block_size
=$LARGE_MIN_BLOCK_SIZE
149 limit_blocks
=$LARGE_LIMIT_BLOCKS
151 let max_blocks
=$size/$min_block_size
152 if [ $max_blocks -gt $limit_blocks ]; then
153 max_blocks
=$limit_blocks
156 for i
in $
(eval echo {2..
$max_blocks}); do
158 pngout
-q -k1 -ks -s1 -n$i $file
162 # Usage: random_huffman_table_trial <file>
163 # Try compressing by randomizing the initial huffman table.
165 # TODO(oshima): Try adjusting different parameters for large files to
167 function random_huffman_table_trial
{
168 info
-ne "\b\b\b\b\b\b\b\brandom.."
170 local old_size
=$
(stat
-c%s
$file)
171 local trials_count
=$DEFAULT_RANDOM_TRIALS
173 if [ $old_size -gt $LARGE_FILE_THRESHOLD ]; then
174 trials_count
=$LARGE_RANDOM_TRIALS
176 for i
in $
(eval echo {1..
$trials_count}); do
178 pngout
-q -k1 -ks -s0 -r $file
180 local new_size
=$
(stat
-c%s
$file)
181 if [ $new_size -lt $old_size ]; then
182 random_huffman_table_trial
$file
186 # Usage: final_comprssion <file>
187 # Further compress using optipng and advdef.
188 # TODO(oshima): Experiment with 256.
189 function final_compression
{
190 info
-ne "\b\b\b\b\b\b\b\bfinal..."
192 if [ $OPTIMIZE_LEVEL == 2 ]; then
193 for i
in 32k
16k
8k
4k
2k
1k
512; do
195 optipng
-q -nb -nc -zw$i -zc1-9 -zm1-9 -zs0-3 -f0-5 $file
198 for i
in $
(eval echo {1.
.4}); do
200 advdef
-q -z -$i $file
203 # Clear the current line.
205 printf "\033[0G\033[K"
209 # Usage: get_color_type <file>
210 # Returns the color type name of the png file. Here is the list of names
211 # for each color type codes.
217 # See http://en.wikipedia.org/wiki/Portable_Network_Graphics#Color_depth
218 # for details about the color type code.
219 function get_color_type
{
221 echo $
(file $file |
awk -F, '{print $3}' |
awk '{print $2}')
224 # Usage: optimize_size <file>
225 # Performs png file optimization.
226 function optimize_size
{
227 # Print filename, trimmed to ensure it + status don't take more than 1 line
228 local filename_length
=${#file}
229 local -i allowed_length
=$COLUMNS-11
230 local -i trimmed_length
=$filename_length-$COLUMNS+14
231 if [ "$filename_length" -lt "$allowed_length" ]; then
232 info
-n "$file|........"
234 info
-n "...${file:$trimmed_length}|........"
239 advdef
-q -z -4 $file
241 pngout
-q -s4 -c0 -force $file $file.tmp.png
242 if [ -f $file.tmp.png
]; then
244 process_grayscale
$file
245 process_grayscale_alpha
$file
247 pngout
-q -s4 -c4 -force $file $file.tmp.png
248 if [ -f $file.tmp.png
]; then
250 process_grayscale_alpha
$file
256 info
-ne "\b\b\b\b\b\b\b\bfilter.."
257 local old_color_type
=$
(get_color_type
$file)
258 optipng
-q -zc9 -zm8 -zs0-3 -f0-5 $file -out $file.tmp.png
259 local new_color_type
=$
(get_color_type
$file.tmp.png
)
260 # optipng may corrupt a png file when reducing the color type
261 # to grayscale/grayscale+alpha. Just skip such cases until
262 # the bug is fixed. See crbug.com/174505, crbug.com/174084.
263 # The issue is reported in
264 # https://sourceforge.net/tracker/?func=detail&aid=3603630&group_id=151404&atid=780913
265 if [[ $old_color_type == "RGBA" && $new_color_type == gray
* ]] ; then
268 mv $file.tmp.png
$file
270 pngout
-q -k1 -s1 $file
274 # TODO(oshima): Experiment with strategy 1.
275 info
-ne "\b\b\b\b\b\b\b\bstrategy"
276 if [ $OPTIMIZE_LEVEL == 2 ]; then
278 pngout
-q -k1 -ks -s$i $file
281 pngout
-q -k1 -ks -s1 $file
284 if [ $OPTIMIZE_LEVEL == 2 ]; then
285 random_huffman_table_trial
$file
288 final_compression
$file
291 # Usage: process_file <file>
292 function process_file
{
294 local name
=$
(basename $file)
295 # -rem alla removes all ancillary chunks except for tRNS
296 pngcrush
-d $TMP_DIR -brute -reduce -rem alla
$file > /dev
/null
2>&1
298 if [ -f $TMP_DIR/$name -a $OPTIMIZE_LEVEL != 0 ]; then
299 optimize_size
$TMP_DIR/$name
303 # Usage: optimize_file <file>
304 function optimize_file
{
306 if $using_cygwin ; then
307 file=$
(cygpath
-w $file)
310 local name
=$
(basename $file)
311 local old
=$
(stat
-c%s
$file)
312 local tmp_file
=$TMP_DIR/$name
317 if [ ! -e $tmp_file ] ; then
318 let CORRUPTED_FILE
+=1
319 echo "$file may be corrupted; skipping\n"
323 local new
=$
(stat
-c%s
$tmp_file)
325 let percent
=$diff*100
326 let percent
=$percent/$old
328 if [ $new -lt $old ]; then
329 info
"$file: $old => $new ($diff bytes: $percent%)"
330 cp "$tmp_file" "$file"
331 let TOTAL_OLD_BYTES
+=$old
332 let TOTAL_NEW_BYTES
+=$new
333 let PROCESSED_FILE
+=1
335 if [ $OPTIMIZE_LEVEL == 0 ]; then
336 info
"$file: Skipped"
338 info
"$file: Unable to reduce size"
344 function optimize_dir
{
346 if $using_cygwin ; then
347 dir
=$
(cygpath
-w $dir)
350 for f
in $
(find $dir -name "*.png"); do
355 function install_if_not_installed
{
358 which $program > /dev
/null
2>&1
359 if [ "$?" != "0" ]; then
360 if $using_cygwin ; then
361 echo "Couldn't find $program. " \
362 "Please run cygwin's setup.exe and install the $package package."
365 read -p "Couldn't find $program. Do you want to install? (y/n)"
366 [ "$REPLY" == "y" ] && sudo apt-get
install $package
367 [ "$REPLY" == "y" ] ||
exit
372 function fail_if_not_installed
{
375 which $program > /dev
/null
2>&1
377 echo "Couldn't find $program. Please download and install it from $url ."
383 local program
=$
(basename $0)
385 "Usage: $program [options] <dir> ...
387 $program is a utility to reduce the size of png files by removing
388 unnecessary chunks and compressing the image.
391 -o<optimize_level> Specify optimization level: (default is 1)
392 0 Just run pngcrush. It removes unnecessary chunks and perform basic
393 optimization on the encoded data.
394 1 Optimize png files using pngout/optipng and advdef. This can further
395 reduce addtional 5~30%. This is the default level.
396 2 Aggressively optimize the size of png files. This may produce
397 addtional 1%~5% reduction. Warning: this is *VERY*
398 slow and can take hours to process all files.
399 -r<revision> If this is specified, the script processes only png files
400 changed since this revision. The <dir> options will be used
401 to narrow down the files under specific directories.
402 -v Shows optimization process for each file.
403 -h Print this help text."
407 if [ ! -e ..
/.gclient
]; then
408 echo "$0 must be run in src directory"
412 if [ "$(expr substr $(uname -s) 1 6)" == "CYGWIN" ]; then
418 # The -i in the shebang line should result in $COLUMNS being set on newer
419 # versions of bash. If it's not set yet, attempt to set it.
420 if [ -z $COLUMNS ]; then
421 which tput > /dev
/null
2>&1
422 if [ "$?" == "0" ]; then
425 # No tput either... give up and just guess 80 columns.
433 while getopts o
:r
:h
:v opts
437 COMMIT
=$
(git svn find-rev r
$OPTARG |
tail -1) ||
exit
438 if [ -z "$COMMIT" ] ; then
439 echo "Revision $OPTARG not found"
444 if [[ "$OPTARG" != 0 && "$OPTARG" != 1 && "$OPTARG" != 2 ]] ; then
447 OPTIMIZE_LEVEL
=$OPTARG
457 # Remove options from argument list.
458 shift $
(($OPTIND -1))
460 # Make sure we have all necessary commands installed.
461 install_if_not_installed pngcrush pngcrush
462 if [ $OPTIMIZE_LEVEL -ge 1 ]; then
463 install_if_not_installed optipng optipng
465 if $using_cygwin ; then
466 fail_if_not_installed advdef
"http://advancemame.sourceforge.net/comp-readme.html"
468 install_if_not_installed advdef advancecomp
471 if $using_cygwin ; then
472 pngout_url
="http://www.advsys.net/ken/utils.htm"
474 pngout_url
="http://www.jonof.id.au/kenutils"
476 fail_if_not_installed pngout
$pngout_url
479 # Create tmp directory for crushed png file.
481 if $using_cygwin ; then
482 TMP_DIR
=$
(cygpath
-w $TMP_DIR)
485 # Make sure we cleanup temp dir
486 #trap "rm -rf $TMP_DIR" EXIT
488 # If no directories are specified, optimize all directories.
490 set ${DIRS:=$ALL_DIRS}
492 info
"Optimize level=$OPTIMIZE_LEVEL"
494 if [ -n "$COMMIT" ] ; then
495 ALL_FILES
=$
(git
diff --name-only $COMMIT HEAD
$DIRS |
grep "png$")
496 ALL_FILES_LIST
=( $ALL_FILES )
497 echo "Processing ${#ALL_FILES_LIST[*]} files"
498 for f
in $ALL_FILES; do
502 echo "Skipping deleted file: $f";
508 info
"Optimizing png files in $d"
511 elif [ -f $d ] ; then
514 echo "Not a file or directory: $d";
520 echo "Optimized $PROCESSED_FILE/$TOTAL_FILE files in" \
521 "$(date -d "0 + $SECONDS sec
" +%Ts)"
522 if [ $PROCESSED_FILE != 0 ]; then
523 let diff=$TOTAL_OLD_BYTES-$TOTAL_NEW_BYTES
524 let percent
=$diff*100/$TOTAL_OLD_BYTES
525 echo "Result: $TOTAL_OLD_BYTES => $TOTAL_NEW_BYTES bytes" \
526 "($diff bytes: $percent%)"
528 if [ $CORRUPTED_FILE != 0 ]; then
529 echo "Warning: corrupted files found: $CORRUPTED_FILE"
530 echo "Please contact the author of the CL that landed corrupted png files"