OLD | NEW |
---|---|
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 # pngoptimizercl/pngrewrite/deflopts are omitted, but this runs all | |
8 # other processes. | |
9 | |
10 readonly ALL_DIRS=" | |
7 ash/resources | 11 ash/resources |
8 ui/resources | 12 ui/resources |
9 chrome/app/theme | 13 chrome/app/theme |
10 chrome/browser/resources | 14 chrome/browser/resources |
11 chrome/renderer/resources | 15 chrome/renderer/resources |
12 webkit/glue/resources | 16 webkit/glue/resources |
13 remoting/resources | 17 remoting/resources |
14 remoting/webapp | 18 remoting/webapp |
15 " | 19 " |
16 | 20 |
17 function sanitize_file { | 21 # Constnats used for optimization |
Nico
2013/01/15 17:23:58
typo Constnats
oshima
2013/01/15 18:31:19
Done.
| |
22 readonly MIN_BLOCK_SIZE=128 | |
23 readonly LIMIT_BLOCKS=256 | |
24 readonly RANDOM_TRIALS=100 | |
25 | |
26 # Global variables for stats | |
27 TOTAL_OLD_BYTES=0 | |
28 TOTAL_NEW_BYTES=0 | |
29 TOTAL_FILE=0 | |
30 PROCESSED_FILE=0 | |
31 | |
32 declare -a THROBBER_STR=('-' '\\' '|' '/') | |
33 THROBBER_COUNT=0 | |
34 | |
35 function throbber { | |
36 echo -ne "${THROBBER_STR[$THROBBER_COUNT]}\b" | |
37 let THROBBER_COUNT=($THROBBER_COUNT+1)%4 | |
38 } | |
39 | |
40 function pngout_loop { | |
41 local file=$1 | |
42 shift | |
43 local opts=$* | |
44 for i in 0 128 256 512; do | |
45 for j in $(seq 0 5); do | |
46 throbber | |
47 pngout -q -k1 -s1 -b$i -f$j $opts $file | |
48 done | |
49 done | |
50 } | |
51 | |
52 function process_grayscale { | |
53 echo -n "|gray" | |
54 for opt in -d1 -d2 -d4 -d8; do | |
55 pngout_loop $file -c0 $opt | |
56 done | |
57 } | |
58 | |
59 function process_grayscale_alpha { | |
60 echo -n "|gray-a" | |
61 pngout_loop $file -c4 | |
62 for opt in -d1 -d2 -d4 -d8; do | |
63 pngout_loop $file -c3 $opt | |
64 done | |
65 } | |
66 | |
67 function process_rgb { | |
68 echo -n "|rgb" | |
69 for opt in -d1 -d2 -d4 -d8; do | |
70 pngout_loop $file -c3 $opt | |
71 done | |
72 pngout_loop $file -c2 | |
73 pngout_loop $file -c6 | |
74 } | |
75 | |
76 function huffman_blocks { | |
77 local file=$1 | |
78 echo -n "|huffman" | |
79 local size=$(stat -c%s $file) | |
80 let MAX_BLOCKS=$size/$MIN_BLOCK_SIZE | |
81 if [ $MAX_BLOCKS -gt $LIMIT_BLOCKS ]; then | |
82 MAX_BLOCKS=$LIMIT_BLOCKS | |
83 fi | |
84 for i in $(seq 2 $MAX_BLOCKS); do | |
85 throbber | |
86 pngout -q -k1 -ks -s1 -n$i $file | |
87 done | |
88 } | |
89 | |
90 function random_trial { | |
91 echo -n "|random" | |
92 local file=$1 | |
93 local old_size=$(stat -c%s $file) | |
94 for i in $(seq 1 $RANDOM_TRIALS); do | |
95 throbber | |
96 pngout -q -k -ks -s0 -r $file | |
97 done | |
98 local new_size=$(stat -c%s $file) | |
99 if [ $new_size -lt $old_size ]; then | |
100 random_trial $file | |
101 fi | |
102 } | |
103 | |
104 function final_compression { | |
105 echo -n "|final" | |
106 local file=$1 | |
107 for i in 32k 16k 8k 4k 2k 1k 512; do | |
108 throbber | |
109 optipng -q -nb -nc -zw$i -zc1-9 -zm1-9 -zs0-3 -f0-5 $file | |
110 done | |
111 for i in $(seq 1 4); do | |
112 throbber | |
113 advdef -q -z -$i $file | |
114 done | |
115 echo -ne "\r" | |
116 } | |
117 | |
118 function optimize_size { | |
18 tput el | 119 tput el |
19 echo -ne "$1\r" | 120 echo -n "$file " |
121 local file=$1 | |
122 | |
123 advdef -q -z -4 $file | |
124 | |
125 pngout -q -s4 -c0 -force $file $file.tmp.png | |
126 if [ -f $file.tmp.png ]; then | |
127 rm $file.tmp.png | |
128 process_grayscale $file | |
129 process_grayscale_alpha $file | |
130 else | |
131 pngout -q -s4 -c4 -force $file $file.tmp.png | |
132 if [ -f $file.tmp.png ]; then | |
133 rm $file.tmp.png | |
134 process_grayscale_alpha $file | |
135 else | |
136 process_rgb $file | |
137 fi | |
138 fi | |
139 | |
140 echo -n "|filter" | |
141 optipng -q -zc9 -zm8 -zs0-3 -f0-5 $file | |
142 pngout -q -k1 -s1 $file | |
143 | |
144 huffman_blocks $file | |
145 | |
146 echo -n "|strategy" | |
147 for i in 3 2 0; do | |
148 pngout -q -k1 -ks -s$i $file | |
149 done | |
150 | |
151 random_trial $file | |
152 | |
153 final_compression $file | |
154 } | |
155 | |
156 function process_file { | |
20 local file=$1 | 157 local file=$1 |
21 local name=$(basename $file) | 158 local name=$(basename $file) |
22 # -rem alla removes all ancillary chunks except for tRNS | 159 # -rem alla removes all ancillary chunks except for tRNS |
23 pngcrush -d $TMP_DIR -brute -reduce -rem alla $file > /dev/null | 160 pngcrush -d $TMP_DIR -brute -reduce -rem alla $file > /dev/null |
24 | 161 |
25 mv "$TMP_DIR/$name" "$file" | 162 if [ ! -z "$OPTIMIZE" ]; then |
163 optimize_size $TMP_DIR/$name | |
164 fi | |
165 } | |
166 | |
167 function sanitize_file { | |
168 local file=$1 | |
169 local name=$(basename $file) | |
170 local old=$(stat -c%s $file) | |
171 local tmp_file=$TMP_DIR/$name | |
172 | |
173 process_file $file | |
174 | |
175 local new=$(stat -c%s $tmp_file) | |
176 let diff=$old-$new | |
177 let TOTAL_OLD_BYTES+=$old | |
178 let TOTAL_NEW_BYTES+=$new | |
179 let percent=($diff*100)/$old | |
180 let TOTAL_FILE+=1 | |
181 | |
182 tput el | |
183 if [ $new -lt $old ]; then | |
184 echo -ne "$file : $old => $new ($diff bytes : $percent %)\n" | |
185 mv "$tmp_file" "$file" | |
186 let PROCESSED_FILE+=1 | |
187 else | |
188 if [ -z "$OPTIMIZE" ]; then | |
189 echo -ne "$file : skipped\r" | |
190 fi | |
191 rm $tmp_file | |
192 fi | |
26 } | 193 } |
27 | 194 |
28 function sanitize_dir { | 195 function sanitize_dir { |
29 local dir=$1 | 196 local dir=$1 |
30 for f in $(find $dir -name "*.png"); do | 197 for f in $(find $dir -name "*.png"); do |
31 sanitize_file $f | 198 sanitize_file $f |
32 done | 199 done |
33 } | 200 } |
34 | 201 |
202 function install_if_not_installed { | |
203 local program=$1 | |
204 dpkg -s $program > /dev/null 2>&1 | |
205 if [ "$?" != "0" ]; then | |
206 read -p "Couldn't fnd $program. Do you want to install? (y/n)" | |
Nico
2013/01/15 17:23:58
typo fnd
oshima
2013/01/15 18:31:19
Done.
| |
207 [ "$REPLY" == "y" ] && sudo apt-get install $program | |
208 [ "$REPLY" == "y" ] || exit | |
209 fi | |
210 } | |
211 | |
212 function fail_if_not_installed { | |
213 local program=$1 | |
214 local url=$2 | |
215 which $program > /dev/null | |
216 if [ $? != 0 ]; then | |
217 echo "Couldn't find $program. Please download and install it from $url" | |
218 exit 1 | |
219 fi | |
220 } | |
221 | |
222 function show_help { | |
223 local program=$(basename $0) | |
224 echo \ | |
225 "Usage: $program [options] dir ... | |
226 | |
227 $program is a utility to reduce the size of png files by removing | |
228 unnecessary chunks and compress the image. | |
229 | |
230 Options: | |
231 -o Agressively optimize file size. Warnig: this is *VERY* slow and | |
232 can take hours to process all files. | |
233 -h Print this help text." | |
234 exit 1 | |
235 } | |
236 | |
35 if [ ! -e ../.gclient ]; then | 237 if [ ! -e ../.gclient ]; then |
36 echo "$0 must be run in src directory" | 238 echo "$0 must be run in src directory" |
37 exit 1 | 239 exit 1 |
38 fi | 240 fi |
39 | 241 |
40 # Make sure we have pngcrush installed. | 242 # Parse options |
41 dpkg -s pngcrush > /dev/null 2>&1 | 243 while getopts oh opts |
42 if [ "$?" != "0" ]; then | 244 do |
43 read -p "Couldn't fnd pngcrush. Do you want to install? (y/n)" | 245 case $opts in |
44 [ "$REPLY" == "y" ] && sudo apt-get install pngcrush | 246 o) |
45 [ "$REPLY" == "y" ] || exit | 247 OPTIMIZE=true; |
248 shift;; | |
249 [h?]) | |
250 show_help;; | |
251 esac | |
252 done | |
253 | |
254 # Make sure we have all necessary commands installed. | |
255 install_if_not_installed pngcrush | |
256 if [ ! -z "$OPTIMIZE" ]; then | |
257 install_if_not_installed optipng | |
258 fail_if_not_installed advdef "http://advancemame.sourceforge.net/comp-download .html" | |
259 fail_if_not_installed pngout "http://www.jonof.id.au/kenutils" | |
46 fi | 260 fi |
47 | 261 |
48 # Create tmp directory for crushed png file. | 262 # Create tmp directory for crushed png file. |
49 TMP_DIR=$(mktemp -d) | 263 TMP_DIR=$(mktemp -d) |
50 | 264 |
51 # Make sure we cleanup temp dir | 265 # Make sure we cleanup temp dir |
52 trap "rm -rf $TMP_DIR" EXIT | 266 trap "rm -rf $TMP_DIR" EXIT |
53 | 267 |
54 # If no arguments passed, sanitize all directories. | 268 # If no directories specified, sanitize all directories. |
55 DIRS=$* | 269 DIRS=$@ |
56 set ${DIRS:=$ALL_DIRS} | 270 set ${DIRS:=$ALL_DIRS} |
57 | 271 |
58 for d in $DIRS; do | 272 for d in $DIRS; do |
59 echo "Sanitizing png files in $d" | 273 echo "Sanitizing png files in $d" |
60 sanitize_dir $d | 274 sanitize_dir $d |
61 echo | 275 echo |
62 done | 276 done |
63 | 277 |
278 # Print the results | |
279 let diff=$TOTAL_OLD_BYTES-$TOTAL_NEW_BYTES | |
280 let percent=$diff*100/$TOTAL_OLD_BYTES | |
281 echo "Processed $PROCESSED_FILE files (out of $TOTAL_FILE files)" \ | |
282 "in $(date -u -d @$SECONDS +%T)s" | |
283 echo "Result : $TOTAL_OLD_BYTES => $TOTAL_NEW_BYTES" \ | |
284 "($diff bytes : $percent %)" | |
285 | |
OLD | NEW |