Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(285)

Side by Side Diff: build/sanitize-png-files.sh

Issue 11905002: Add -o option to optimize png file size (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Created 7 years, 11 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « no previous file | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 #!/bin/bash 1 #!/bin/bash
2 # Copyright (c) 2010 The Chromium Authors. All rights reserved. 2 # Copyright (c) 2010 The Chromium Authors. All rights reserved.
3 # Use of this source code is governed by a BSD-style license that can be 3 # Use of this source code is governed by a BSD-style license that can be
4 # found in the LICENSE file. 4 # found in the LICENSE file.
5 5
6 ALL_DIRS=" 6 # The optimization code is based on pngslim (http://goo.gl/a0XHg)
7 # and executes similar pipleline to optimize the png file size.
msw 2013/01/16 03:43:11 nit: "a similar"
oshima 2013/01/16 18:29:04 Done.
8 # The steps that requires pngoptimizercl/pngrewrite/deflopts are omitted,
msw 2013/01/16 03:43:11 nits: "that require" and "deflopt"
oshima 2013/01/16 18:29:04 Done.
9 # but this runs allother processes, including:
msw 2013/01/16 03:43:11 nit: "all other"
oshima 2013/01/16 18:29:04 Done.
10 # 1) various color-dependent optimization using optpng.
msw 2013/01/16 03:43:11 nits: "optimizations" "optipng"
oshima 2013/01/16 18:29:04 Done.
11 # 2) optimize number of huffman blocks.
msw 2013/01/16 03:43:11 nit: "optimize the"
oshima 2013/01/16 18:29:04 Done.
12 # 3) randomized huffman table.
msw 2013/01/16 03:43:11 nit: "randomize the"
oshima 2013/01/16 18:29:04 Done.
13 # 4) Further optimize using optipng () and advdef (zlib stream).
msw 2013/01/16 03:43:11 nit: remove "()"
oshima 2013/01/16 18:29:04 Done.
14 # Due to the step 3), each run may produce slightly different results.
15 #
16 # TOTO(oshima): Use different parameters for large file to reduce
msw 2013/01/16 03:43:11 nits: "TODO" and "files"
oshima 2013/01/16 18:29:04 Done. (moved to random_huffman_table_trial)
17 # runtime.
msw 2013/01/16 03:43:11 nit: this fits on the line above.
oshima 2013/01/16 18:29:04 Done.
18 # Note(oshima): In my experiment, advdef didn't reduce much. I'm keeping it
19 # for now as it does take much time to run.
msw 2013/01/16 03:43:11 nit: Did you mean "doesn't"? Otherwise, this doesn
oshima 2013/01/16 18:29:04 Done.
20
21 readonly ALL_DIRS="
7 ash/resources 22 ash/resources
8 ui/resources 23 ui/resources
9 chrome/app/theme 24 chrome/app/theme
10 chrome/browser/resources 25 chrome/browser/resources
11 chrome/renderer/resources 26 chrome/renderer/resources
12 webkit/glue/resources 27 webkit/glue/resources
13 remoting/resources 28 remoting/resources
14 remoting/webapp 29 remoting/webapp
15 " 30 "
16 31
17 function sanitize_file { 32 # Constants used for optimization
33 readonly MIN_BLOCK_SIZE=128
34 readonly LIMIT_BLOCKS=256
35 readonly RANDOM_TRIALS=100
36
37 # Global variables for stats
38 TOTAL_OLD_BYTES=0
39 TOTAL_NEW_BYTES=0
40 TOTAL_FILE=0
41 PROCESSED_FILE=0
42
43 declare -a THROBBER_STR=('-' '\\' '|' '/')
44 THROBBER_COUNT=0
45
46 # Show throbber character at current cursor position.
47 function throbber {
48 echo -ne "${THROBBER_STR[$THROBBER_COUNT]}\b"
49 let THROBBER_COUNT=($THROBBER_COUNT+1)%4
50 }
51
52 # Usage: process_rgb <file> <png_out_options> ...
msw 2013/01/16 03:43:11 s/process_rgb/pngout_loop/
oshima 2013/01/16 18:29:04 Done.
53 # Optimize the png file using pngout with given option
msw 2013/01/16 03:43:11 nit: "the given options"
oshima 2013/01/16 18:29:04 Done.
54 # using vairous block split threshold and filter types.
msw 2013/01/16 03:43:11 nits: "various" and "thresholds"
oshima 2013/01/16 18:29:04 Done.
55 function pngout_loop {
56 local file=$1
57 shift
58 local opts=$*
59 for i in 0 128 256 512; do
60 for j in $(seq 0 5); do
61 throbber
62 pngout -q -k1 -s1 -b$i -f$j $opts $file
63 done
64 done
65 }
66
67 # Usage: process_grayscale <file>
68 # Optimize grayscale image for all color bit depth.
msw 2013/01/16 03:43:11 nit: "images" and "depths" here and lines 77 and 8
oshima 2013/01/16 18:29:04 Done.
69 function process_grayscale {
70 echo -n "|gray"
71 for opt in -d1 -d2 -d4 -d8; do
msw 2013/01/16 03:43:11 Can we use -d0 to minimize the bit depth without l
oshima 2013/01/16 18:29:04 I added TODO.
72 pngout_loop $file -c0 $opt
msw 2013/01/16 03:43:11 According to the documentation, "If no /c# option
oshima 2013/01/16 18:29:04 added TODO
73 done
74 }
75
76 # Usage: process_grayscale_alpha <file>
77 # Optimize grayscale+alpha image for all color bit depth.
msw 2013/01/16 03:43:11 nit: "(with alpha)", "(w/ alpha)", or similar.
oshima 2013/01/16 18:29:04 Done.
78 function process_grayscale_alpha {
79 echo -n "|gray-a"
80 pngout_loop $file -c4
81 for opt in -d1 -d2 -d4 -d8; do
82 pngout_loop $file -c3 $opt
83 done
84 }
85
86 # Usage: process_rgb <file>
87 # Optimize rgb (w w/o alpha) image for all color bit depth.
msw 2013/01/16 03:43:11 nit: "(with or without alpha)", "(w/ or w/o alpha)
oshima 2013/01/16 18:29:04 Done.
88 function process_rgb {
89 echo -n "|rgb"
90 for opt in -d1 -d2 -d4 -d8; do
91 pngout_loop $file -c3 $opt
92 done
93 pngout_loop $file -c2
94 pngout_loop $file -c6
95 }
96
97 # Usage: huffman_blocks <file>
98 # Optimize huffman blocks
msw 2013/01/16 03:43:11 nit: "the huffman", and add a trailing period.
oshima 2013/01/16 18:29:04 Done.
99 function huffman_blocks {
100 local file=$1
101 echo -n "|huffman"
102 local size=$(stat -c%s $file)
103 let MAX_BLOCKS=$size/$MIN_BLOCK_SIZE
104 if [ $MAX_BLOCKS -gt $LIMIT_BLOCKS ]; then
105 MAX_BLOCKS=$LIMIT_BLOCKS
106 fi
107 for i in $(seq 2 $MAX_BLOCKS); do
108 throbber
109 pngout -q -k1 -ks -s1 -n$i $file
110 done
111 }
112
113 # Usage: random_huffmantable_trial <file>
msw 2013/01/16 03:43:11 nit: random_huffman_table
oshima 2013/01/16 18:29:04 Done.
114 # Try compressing using randomized initial huffman table.
msw 2013/01/16 03:43:11 nit: "a randomized"
oshima 2013/01/16 18:29:04 changed to Try compressing by randomizing initial
115 function random_huffmantable_trial {
msw 2013/01/16 03:43:11 Wow! This has got to be why this script is so slow
oshima 2013/01/16 18:29:04 what makes it really slow is that pngout takes rea
116 echo -n "|random"
117 local file=$1
118 local old_size=$(stat -c%s $file)
119 for i in $(seq 1 $RANDOM_TRIALS); do
120 throbber
121 pngout -q -k -ks -s0 -r $file
msw 2013/01/16 03:43:11 This -k option should be -k1 if you're following p
oshima 2013/01/16 18:29:04 thank you for the catch! Fixed.
122 done
123 local new_size=$(stat -c%s $file)
124 if [ $new_size -lt $old_size ]; then
125 random_huffmantable_trial $file
126 fi
127 }
128
129 # Usage: final_comprssion <file>
130 # Further compreess using optpng and advdef.
msw 2013/01/16 03:43:11 nits: "compress" and "optipng"
oshima 2013/01/16 18:29:04 Done.
131 function final_compression {
132 echo -n "|final"
133 local file=$1
134 for i in 32k 16k 8k 4k 2k 1k 512; do
msw 2013/01/16 03:43:11 Should we also try 256 here? I guess pngslim doesn
oshima 2013/01/16 18:29:04 I'll try. I added TODO
135 throbber
136 optipng -q -nb -nc -zw$i -zc1-9 -zm1-9 -zs0-3 -f0-5 $file
msw 2013/01/16 03:43:11 optipng docs claim that quiet mode is -quiet, not
oshima 2013/01/16 18:29:04 yes, it -q does work.
137 done
138 for i in $(seq 1 4); do
139 throbber
140 advdef -q -z -$i $file
msw 2013/01/16 03:43:11 Strange, your code looks correct according to advd
oshima 2013/01/16 18:29:04 yes, I looked at the doc and fixed. I guess both w
141 done
142 echo -ne "\r"
143 }
144
145 # Usage: optimize_size <file>
146 # Performs png file optimization.
147 function optimize_size {
18 tput el 148 tput el
19 echo -ne "$1\r" 149 echo -n "$file "
msw 2013/01/16 03:43:11 Does this work? (using $file before it's defined i
oshima 2013/01/16 18:29:04 thank you for the catch. Fixed.
150 local file=$1
151
152 advdef -q -z -4 $file
153
154 pngout -q -s4 -c0 -force $file $file.tmp.png
155 if [ -f $file.tmp.png ]; then
156 rm $file.tmp.png
157 process_grayscale $file
158 process_grayscale_alpha $file
159 else
160 pngout -q -s4 -c4 -force $file $file.tmp.png
161 if [ -f $file.tmp.png ]; then
162 rm $file.tmp.png
163 process_grayscale_alpha $file
164 else
165 process_rgb $file
166 fi
167 fi
168
169 echo -n "|filter"
170 optipng -q -zc9 -zm8 -zs0-3 -f0-5 $file
171 pngout -q -k1 -s1 $file
172
173 huffman_blocks $file
174
175 echo -n "|strategy"
176 for i in 3 2 0; do
msw 2013/01/16 03:43:11 Should we also run Strategy 1 here? I guess pngsli
oshima 2013/01/16 18:29:04 Added TODO
177 pngout -q -k1 -ks -s$i $file
178 done
179
180 random_huffmantable_trial $file
181
182 final_compression $file
183 }
184
185 # Usage: process_fie <file>
msw 2013/01/16 03:43:11 nit: "process_file"
oshima 2013/01/16 18:29:04 Done.
186 function process_file {
20 local file=$1 187 local file=$1
21 local name=$(basename $file) 188 local name=$(basename $file)
22 # -rem alla removes all ancillary chunks except for tRNS 189 # -rem alla removes all ancillary chunks except for tRNS
23 pngcrush -d $TMP_DIR -brute -reduce -rem alla $file > /dev/null 190 pngcrush -d $TMP_DIR -brute -reduce -rem alla $file > /dev/null
24 191
25 mv "$TMP_DIR/$name" "$file" 192 if [ ! -z "$OPTIMIZE" ]; then
193 optimize_size $TMP_DIR/$name
194 fi
195 }
196
197 # Usage: sanitize_file <file>
198 function sanitize_file {
199 local file=$1
200 local name=$(basename $file)
201 local old=$(stat -c%s $file)
202 local tmp_file=$TMP_DIR/$name
203
204 process_file $file
205
206 local new=$(stat -c%s $tmp_file)
207 let diff=$old-$new
208 let TOTAL_OLD_BYTES+=$old
209 let TOTAL_NEW_BYTES+=$new
210 let percent=($diff*100)/$old
211 let TOTAL_FILE+=1
212
213 tput el
214 if [ $new -lt $old ]; then
215 echo -ne "$file : $old => $new ($diff bytes : $percent %)\n"
216 mv "$tmp_file" "$file"
217 let PROCESSED_FILE+=1
218 else
219 if [ -z "$OPTIMIZE" ]; then
220 echo -ne "$file : skipped\r"
221 fi
222 rm $tmp_file
223 fi
26 } 224 }
27 225
28 function sanitize_dir { 226 function sanitize_dir {
29 local dir=$1 227 local dir=$1
30 for f in $(find $dir -name "*.png"); do 228 for f in $(find $dir -name "*.png"); do
31 sanitize_file $f 229 sanitize_file $f
32 done 230 done
33 } 231 }
34 232
233 function install_if_not_installed {
234 local program=$1
235 dpkg -s $program > /dev/null 2>&1
236 if [ "$?" != "0" ]; then
237 read -p "Couldn't find $program. Do you want to install? (y/n)"
238 [ "$REPLY" == "y" ] && sudo apt-get install $program
239 [ "$REPLY" == "y" ] || exit
240 fi
241 }
242
243 function fail_if_not_installed {
244 local program=$1
245 local url=$2
246 which $program > /dev/null
247 if [ $? != 0 ]; then
248 echo "Couldn't find $program. Please download and install it from $url"
249 exit 1
250 fi
251 }
252
253 function show_help {
254 local program=$(basename $0)
255 echo \
256 "Usage: $program [options] dir ...
257
258 $program is a utility to reduce the size of png files by removing
259 unnecessary chunks and compress the image.
msw 2013/01/16 03:43:11 nit: "compressing"
oshima 2013/01/16 18:29:04 Done.
260
261 Options:
262 -o Agressively optimize file size. Warnig: this is *VERY* slow and
msw 2013/01/16 03:43:11 nits: "Aggressively" and "Warning"
oshima 2013/01/16 18:29:04 Done.
263 can take hours to process all files.
264 -h Print this help text."
265 exit 1
266 }
267
35 if [ ! -e ../.gclient ]; then 268 if [ ! -e ../.gclient ]; then
36 echo "$0 must be run in src directory" 269 echo "$0 must be run in src directory"
37 exit 1 270 exit 1
38 fi 271 fi
39 272
40 # Make sure we have pngcrush installed. 273 # Parse options
41 dpkg -s pngcrush > /dev/null 2>&1 274 while getopts oh opts
42 if [ "$?" != "0" ]; then 275 do
43 read -p "Couldn't fnd pngcrush. Do you want to install? (y/n)" 276 case $opts in
44 [ "$REPLY" == "y" ] && sudo apt-get install pngcrush 277 o)
45 [ "$REPLY" == "y" ] || exit 278 OPTIMIZE=true;
279 shift;;
280 [h?])
281 show_help;;
282 esac
283 done
284
285 # Make sure we have all necessary commands installed.
286 install_if_not_installed pngcrush
287 if [ ! -z "$OPTIMIZE" ]; then
288 install_if_not_installed optipng
289 fail_if_not_installed advdef "http://advancemame.sourceforge.net/comp-download .html"
290 fail_if_not_installed pngout "http://www.jonof.id.au/kenutils"
46 fi 291 fi
47 292
48 # Create tmp directory for crushed png file. 293 # Create tmp directory for crushed png file.
49 TMP_DIR=$(mktemp -d) 294 TMP_DIR=$(mktemp -d)
50 295
51 # Make sure we cleanup temp dir 296 # Make sure we cleanup temp dir
52 trap "rm -rf $TMP_DIR" EXIT 297 trap "rm -rf $TMP_DIR" EXIT
53 298
54 # If no arguments passed, sanitize all directories. 299 # If no directories specified, sanitize all directories.
msw 2013/01/16 03:43:11 nit: "directories are specified"
oshima 2013/01/16 18:29:04 Done.
55 DIRS=$* 300 DIRS=$@
56 set ${DIRS:=$ALL_DIRS} 301 set ${DIRS:=$ALL_DIRS}
57 302
58 for d in $DIRS; do 303 for d in $DIRS; do
59 echo "Sanitizing png files in $d" 304 echo "Sanitizing png files in $d"
60 sanitize_dir $d 305 sanitize_dir $d
61 echo 306 echo
62 done 307 done
63 308
309 # Print the results
msw 2013/01/16 03:43:11 nit: Add a trailing period.
oshima 2013/01/16 18:29:04 Done.
310 let diff=$TOTAL_OLD_BYTES-$TOTAL_NEW_BYTES
311 let percent=$diff*100/$TOTAL_OLD_BYTES
312 echo "Processed $PROCESSED_FILE files (out of $TOTAL_FILE files)" \
313 "in $(date -u -d @$SECONDS +%T)s"
314 echo "Result : $TOTAL_OLD_BYTES => $TOTAL_NEW_BYTES" \
msw 2013/01/16 03:43:11 nit: Specify the units like "$TOTAL_OLD_BYTES byte
oshima 2013/01/16 18:29:04 Done.
315 "($diff bytes : $percent %)"
316
OLDNEW
« no previous file with comments | « no previous file | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698