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

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: fix comments 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 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.
15 #
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.
18
19 readonly ALL_DIRS="
7 ash/resources 20 ash/resources
8 ui/resources 21 ui/resources
9 chrome/app/theme 22 chrome/app/theme
10 chrome/browser/resources 23 chrome/browser/resources
11 chrome/renderer/resources 24 chrome/renderer/resources
12 webkit/glue/resources 25 webkit/glue/resources
13 remoting/resources 26 remoting/resources
14 remoting/webapp 27 remoting/webapp
15 " 28 "
16 29
17 function sanitize_file { 30 # Constants used for optimization
31 readonly MIN_BLOCK_SIZE=128
32 readonly LIMIT_BLOCKS=256
33 readonly RANDOM_TRIALS=100
34
35 # Global variables for stats
36 TOTAL_OLD_BYTES=0
37 TOTAL_NEW_BYTES=0
38 TOTAL_FILE=0
39 PROCESSED_FILE=0
40
41 declare -a THROBBER_STR=('-' '\\' '|' '/')
42 THROBBER_COUNT=0
43
44 # Show throbber character at current cursor position.
45 function throbber {
46 echo -ne "${THROBBER_STR[$THROBBER_COUNT]}\b"
47 let THROBBER_COUNT=($THROBBER_COUNT+1)%4
48 }
49
50 # Usage: pngout_loop <file> <png_out_options> ...
51 # Optimize the png file using pngout with the given options
52 # using various block split thresholds and filter types.
53 function pngout_loop {
54 local file=$1
55 shift
56 local opts=$*
57 for i in 0 128 256 512; do
58 for j in $(seq 0 5); do
59 throbber
60 pngout -q -k1 -s1 -b$i -f$j $opts $file
61 done
62 done
63 }
64
65 # Usage: process_grayscale <file>
66 # Optimize grayscale images for all color bit depths.
67 #
68 # TODO(oshima): Experiment with -d0 w/o -c0.
69 function process_grayscale {
70 echo -n "|gray"
71 for opt in -d1 -d2 -d4 -d8; do
72 pngout_loop $file -c0 $opt
73 done
74 }
75
76 # Usage: process_grayscale_alpha <file>
77 # Optimize grayscale images with alpha for all color bit depths.
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 images with or without alpha for all color bit depths.
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 the huffman blocks.
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_huffman_table_trial <file>
114 # Try compressing by randomizing the initial huffman table.
115 #
116 # TODO(oshima): Try adjusting different parameters for large files to
117 # reduce runtime.
118 function random_huffman_table_trial {
119 echo -n "|random"
120 local file=$1
121 local old_size=$(stat -c%s $file)
122 for i in $(seq 1 $RANDOM_TRIALS); do
123 throbber
124 pngout -q -k1 -ks -s0 -r $file
125 done
126 local new_size=$(stat -c%s $file)
127 if [ $new_size -lt $old_size ]; then
128 random_huffman_table_trial $file
129 fi
130 }
131
132 # Usage: final_comprssion <file>
133 # Further compress using optipng and advdef.
134 # TODO(oshima): Experiment with 256.
135 function final_compression {
136 echo -n "|final"
137 local file=$1
138 for i in 32k 16k 8k 4k 2k 1k 512; do
139 throbber
140 optipng -q -nb -nc -zw$i -zc1-9 -zm1-9 -zs0-3 -f0-5 $file
141 done
142 for i in $(seq 1 4); do
143 throbber
144 advdef -q -z -$i $file
145 done
146 echo -ne "\r"
147 }
148
149 # Usage: optimize_size <file>
150 # Performs png file optimization.
151 function optimize_size {
18 tput el 152 tput el
19 echo -ne "$1\r" 153 local file=$1
154 echo -n "$file "
155
156 advdef -q -z -4 $file
157
158 pngout -q -s4 -c0 -force $file $file.tmp.png
159 if [ -f $file.tmp.png ]; then
160 rm $file.tmp.png
161 process_grayscale $file
162 process_grayscale_alpha $file
163 else
164 pngout -q -s4 -c4 -force $file $file.tmp.png
165 if [ -f $file.tmp.png ]; then
166 rm $file.tmp.png
167 process_grayscale_alpha $file
168 else
169 process_rgb $file
170 fi
171 fi
172
173 echo -n "|filter"
174 optipng -q -zc9 -zm8 -zs0-3 -f0-5 $file
175 pngout -q -k1 -s1 $file
176
177 huffman_blocks $file
178
179 # TODO(oshima): Experiment with strategy 1.
180 echo -n "|strategy"
181 for i in 3 2 0; do
182 pngout -q -k1 -ks -s$i $file
183 done
184
185 random_huffman_table_trial $file
186
187 final_compression $file
188 }
189
190 # Usage: process_file <file>
191 function process_file {
20 local file=$1 192 local file=$1
21 local name=$(basename $file) 193 local name=$(basename $file)
22 # -rem alla removes all ancillary chunks except for tRNS 194 # -rem alla removes all ancillary chunks except for tRNS
23 pngcrush -d $TMP_DIR -brute -reduce -rem alla $file > /dev/null 195 pngcrush -d $TMP_DIR -brute -reduce -rem alla $file > /dev/null
24 196
25 mv "$TMP_DIR/$name" "$file" 197 if [ ! -z "$OPTIMIZE" ]; then
198 optimize_size $TMP_DIR/$name
199 fi
200 }
201
202 # Usage: sanitize_file <file>
203 function sanitize_file {
204 local file=$1
205 local name=$(basename $file)
206 local old=$(stat -c%s $file)
207 local tmp_file=$TMP_DIR/$name
208
209 process_file $file
210
211 local new=$(stat -c%s $tmp_file)
212 let diff=$old-$new
213 let TOTAL_OLD_BYTES+=$old
214 let TOTAL_NEW_BYTES+=$new
215 let percent=($diff*100)/$old
216 let TOTAL_FILE+=1
217
218 tput el
219 if [ $new -lt $old ]; then
220 echo -ne "$file : $old => $new ($diff bytes : $percent %)\n"
221 mv "$tmp_file" "$file"
222 let PROCESSED_FILE+=1
223 else
224 if [ -z "$OPTIMIZE" ]; then
225 echo -ne "$file : skipped\r"
226 fi
227 rm $tmp_file
228 fi
26 } 229 }
27 230
28 function sanitize_dir { 231 function sanitize_dir {
29 local dir=$1 232 local dir=$1
30 for f in $(find $dir -name "*.png"); do 233 for f in $(find $dir -name "*.png"); do
31 sanitize_file $f 234 sanitize_file $f
32 done 235 done
33 } 236 }
34 237
238 function install_if_not_installed {
239 local program=$1
240 dpkg -s $program > /dev/null 2>&1
241 if [ "$?" != "0" ]; then
242 read -p "Couldn't find $program. Do you want to install? (y/n)"
243 [ "$REPLY" == "y" ] && sudo apt-get install $program
244 [ "$REPLY" == "y" ] || exit
245 fi
246 }
247
248 function fail_if_not_installed {
249 local program=$1
250 local url=$2
251 which $program > /dev/null
252 if [ $? != 0 ]; then
253 echo "Couldn't find $program. Please download and install it from $url"
254 exit 1
255 fi
256 }
257
258 function show_help {
259 local program=$(basename $0)
260 echo \
261 "Usage: $program [options] dir ...
262
263 $program is a utility to reduce the size of png files by removing
264 unnecessary chunks and compressing the image.
265
266 Options:
267 -o Aggressively optimize file size. Warning: this is *VERY* slow and
268 can take hours to process all files.
269 -h Print this help text."
270 exit 1
271 }
272
35 if [ ! -e ../.gclient ]; then 273 if [ ! -e ../.gclient ]; then
36 echo "$0 must be run in src directory" 274 echo "$0 must be run in src directory"
37 exit 1 275 exit 1
38 fi 276 fi
39 277
40 # Make sure we have pngcrush installed. 278 # Parse options
41 dpkg -s pngcrush > /dev/null 2>&1 279 while getopts oh opts
42 if [ "$?" != "0" ]; then 280 do
43 read -p "Couldn't fnd pngcrush. Do you want to install? (y/n)" 281 case $opts in
44 [ "$REPLY" == "y" ] && sudo apt-get install pngcrush 282 o)
45 [ "$REPLY" == "y" ] || exit 283 OPTIMIZE=true;
284 shift;;
285 [h?])
286 show_help;;
287 esac
288 done
289
290 # Make sure we have all necessary commands installed.
291 install_if_not_installed pngcrush
292 if [ ! -z "$OPTIMIZE" ]; then
293 install_if_not_installed optipng
294 fail_if_not_installed advdef "http://advancemame.sourceforge.net/comp-download .html"
295 fail_if_not_installed pngout "http://www.jonof.id.au/kenutils"
46 fi 296 fi
47 297
48 # Create tmp directory for crushed png file. 298 # Create tmp directory for crushed png file.
49 TMP_DIR=$(mktemp -d) 299 TMP_DIR=$(mktemp -d)
50 300
51 # Make sure we cleanup temp dir 301 # Make sure we cleanup temp dir
52 trap "rm -rf $TMP_DIR" EXIT 302 trap "rm -rf $TMP_DIR" EXIT
53 303
54 # If no arguments passed, sanitize all directories. 304 # If no directories are specified, sanitize all directories.
55 DIRS=$* 305 DIRS=$@
56 set ${DIRS:=$ALL_DIRS} 306 set ${DIRS:=$ALL_DIRS}
57 307
58 for d in $DIRS; do 308 for d in $DIRS; do
59 echo "Sanitizing png files in $d" 309 echo "Sanitizing png files in $d"
60 sanitize_dir $d 310 sanitize_dir $d
61 echo 311 echo
62 done 312 done
63 313
314 # Print the results.
315 let diff=$TOTAL_OLD_BYTES-$TOTAL_NEW_BYTES
316 let percent=$diff*100/$TOTAL_OLD_BYTES
317 echo "Processed $PROCESSED_FILE files (out of $TOTAL_FILE files)" \
318 "in $(date -u -d @$SECONDS +%T)s"
319 echo "Result : $TOTAL_OLD_BYTES => $TOTAL_NEW_BYTES bytes" \
320 "($diff bytes : $percent %)"
321
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