Chromium Code Reviews| 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 # The optimization code is based on pngslim (http://goo.gl/a0XHg) | 6 # The optimization code is based on pngslim (http://goo.gl/a0XHg) |
| 7 # and executes a similar pipleline to optimize the png file size. | 7 # and executes a similar pipleline to optimize the png file size. |
| 8 # The steps that require pngoptimizercl/pngrewrite/deflopt are omitted, | 8 # The steps that require pngoptimizercl/pngrewrite/deflopt are omitted, |
| 9 # but this runs all other processes, including: | 9 # but this runs all other processes, including: |
| 10 # 1) various color-dependent optimizations using optipng. | 10 # 1) various color-dependent optimizations using optipng. |
| 11 # 2) optimize the number of huffman blocks. | 11 # 2) optimize the number of huffman blocks. |
| 12 # 3) randomize the huffman table. | 12 # 3) randomize the huffman table. |
| 13 # 4) Further optimize using optipng and advdef (zlib stream). | 13 # 4) Further optimize using optipng and advdef (zlib stream). |
| 14 # Due to the step 3), each run may produce slightly different results. | 14 # Due to the step 3), each run may produce slightly different results. |
| 15 # | 15 # |
| 16 # Note(oshima): In my experiment, advdef didn't reduce much. I'm keeping it | 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. | 17 # for now as it does not take much time to run. |
| 18 | 18 |
|
Nico
2013/02/06 23:34:56
Should this have a `set -e` `set -u` up here?
oshima
2013/02/06 23:46:23
Good catch. I overlooked it. Since I need to fix
a
| |
| 19 readonly ALL_DIRS=" | 19 readonly ALL_DIRS=" |
| 20 ash/resources | 20 ash/resources |
| 21 ui/resources | 21 ui/resources |
| 22 chrome/app/theme | 22 chrome/app/theme |
| 23 chrome/browser/resources | 23 chrome/browser/resources |
| 24 chrome/renderer/resources | 24 chrome/renderer/resources |
| 25 webkit/glue/resources | 25 webkit/glue/resources |
| 26 remoting/resources | 26 remoting/resources |
| 27 remoting/webapp | 27 remoting/webapp |
| 28 " | 28 " |
| (...skipping 150 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 179 optipng -q -nb -nc -zw$i -zc1-9 -zm1-9 -zs0-3 -f0-5 $file | 179 optipng -q -nb -nc -zw$i -zc1-9 -zm1-9 -zs0-3 -f0-5 $file |
| 180 done | 180 done |
| 181 fi | 181 fi |
| 182 for i in $(seq 1 4); do | 182 for i in $(seq 1 4); do |
| 183 throbber | 183 throbber |
| 184 advdef -q -z -$i $file | 184 advdef -q -z -$i $file |
| 185 done | 185 done |
| 186 echo -ne "\r" | 186 echo -ne "\r" |
| 187 } | 187 } |
| 188 | 188 |
| 189 # Usage: get_color_type <file> | |
| 190 # Returns the color type name of the png file. Here is the list of names | |
| 191 # for each color type codes. | |
| 192 # 0 : grayscale | |
| 193 # 2 : RGB | |
| 194 # 3 : colormap | |
| 195 # 4 : gray+alpha | |
| 196 # 6 : RGBA | |
| 197 # See http://en.wikipedia.org/wiki/Portable_Network_Graphics#Color_depth | |
| 198 # for details about the color type code. | |
| 199 function get_color_type { | |
| 200 local file=$1 | |
| 201 echo $(file $file | awk -F, '{print $3}' | awk '{print $2}') | |
| 202 } | |
| 203 | |
| 189 # Usage: optimize_size <file> | 204 # Usage: optimize_size <file> |
| 190 # Performs png file optimization. | 205 # Performs png file optimization. |
| 191 function optimize_size { | 206 function optimize_size { |
| 192 tput el | 207 tput el |
| 193 local file=$1 | 208 local file=$1 |
| 194 echo -n "$file " | 209 echo -n "$file " |
| 195 | 210 |
| 196 advdef -q -z -4 $file | 211 advdef -q -z -4 $file |
| 197 | 212 |
| 198 pngout -q -s4 -c0 -force $file $file.tmp.png | 213 pngout -q -s4 -c0 -force $file $file.tmp.png |
| 199 if [ -f $file.tmp.png ]; then | 214 if [ -f $file.tmp.png ]; then |
| 200 rm $file.tmp.png | 215 rm $file.tmp.png |
| 201 process_grayscale $file | 216 process_grayscale $file |
| 202 process_grayscale_alpha $file | 217 process_grayscale_alpha $file |
| 203 else | 218 else |
| 204 pngout -q -s4 -c4 -force $file $file.tmp.png | 219 pngout -q -s4 -c4 -force $file $file.tmp.png |
| 205 if [ -f $file.tmp.png ]; then | 220 if [ -f $file.tmp.png ]; then |
| 206 rm $file.tmp.png | 221 rm $file.tmp.png |
| 207 process_grayscale_alpha $file | 222 process_grayscale_alpha $file |
| 208 else | 223 else |
| 209 process_rgb $file | 224 process_rgb $file |
| 210 fi | 225 fi |
| 211 fi | 226 fi |
| 212 | 227 |
| 213 echo -n "|filter" | 228 echo -n "|filter" |
| 214 optipng -q -zc9 -zm8 -zs0-3 -f0-5 $file | 229 local old_color_type=$(get_color_type $file) |
| 230 optipng -q -zc9 -zm8 -zs0-3 -f0-5 $file -out $file.tmp.png | |
| 231 local new_color_type=$(get_color_type $file.tmp.png) | |
| 232 # optipng may corrupt a png file when reducing the color type | |
| 233 # to grayscale/grayscale+alpha. Just skip such cases until | |
| 234 # the bug is fixed. See crbug.com/174505, crbug.com/174084. | |
| 235 # The issue is reported in | |
| 236 # https://sourceforge.net/tracker/?func=detail&aid=3603630&group_id=151404&ati d=780913 | |
| 237 if [[ $old_color_type == "RGBA" && $new_color_type =~ gray.* ]] ; then | |
| 238 rm $file.tmp.png | |
| 239 echo -n "[skip opting]" | |
| 240 else | |
| 241 mv $file.tmp.png $file | |
| 242 fi | |
| 215 pngout -q -k1 -s1 $file | 243 pngout -q -k1 -s1 $file |
| 216 | 244 |
| 217 huffman_blocks $file | 245 huffman_blocks $file |
| 218 | 246 |
| 219 # TODO(oshima): Experiment with strategy 1. | 247 # TODO(oshima): Experiment with strategy 1. |
| 220 echo -n "|strategy" | 248 echo -n "|strategy" |
| 221 if [ $OPTIMIZE_LEVEL == 2 ]; then | 249 if [ $OPTIMIZE_LEVEL == 2 ]; then |
| 222 for i in 3 2 0; do | 250 for i in 3 2 0; do |
| 223 pngout -q -k1 -ks -s$i $file | 251 pngout -q -k1 -ks -s$i $file |
| 224 done | 252 done |
| (...skipping 145 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 370 done | 398 done |
| 371 | 399 |
| 372 # Print the results. | 400 # Print the results. |
| 373 let diff=$TOTAL_OLD_BYTES-$TOTAL_NEW_BYTES | 401 let diff=$TOTAL_OLD_BYTES-$TOTAL_NEW_BYTES |
| 374 let percent=$diff*100/$TOTAL_OLD_BYTES | 402 let percent=$diff*100/$TOTAL_OLD_BYTES |
| 375 echo "Processed $PROCESSED_FILE files (out of $TOTAL_FILE files)" \ | 403 echo "Processed $PROCESSED_FILE files (out of $TOTAL_FILE files)" \ |
| 376 "in $(date -u -d @$SECONDS +%T)s" | 404 "in $(date -u -d @$SECONDS +%T)s" |
| 377 echo "Result : $TOTAL_OLD_BYTES => $TOTAL_NEW_BYTES bytes" \ | 405 echo "Result : $TOTAL_OLD_BYTES => $TOTAL_NEW_BYTES bytes" \ |
| 378 "($diff bytes : $percent %)" | 406 "($diff bytes : $percent %)" |
| 379 | 407 |
| OLD | NEW |