| OLD | NEW |
| 1 #!/bin/bash | 1 #!/bin/bash |
| 2 # Copyright 2013 The Chromium Authors. All rights reserved. | 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 | 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 |
| 19 readonly ALL_DIRS=" | 19 readonly ALL_DIRS=" |
| 20 ash/resources | 20 ash/resources |
| 21 ui/resources | |
| 22 chrome/app/theme | 21 chrome/app/theme |
| 23 chrome/browser/resources | 22 chrome/browser/resources |
| 24 chrome/renderer/resources | 23 chrome/renderer/resources |
| 24 content/public/android/java/res |
| 25 content/renderer/resources |
| 26 content/shell/resources |
| 27 remoting/resources |
| 28 ui/resources |
| 29 ui/webui/resources/images |
| 25 webkit/glue/resources | 30 webkit/glue/resources |
| 26 remoting/resources | 31 win8/metro_driver/resources |
| 27 remoting/webapp | |
| 28 " | 32 " |
| 29 | 33 |
| 30 # Files larger than this file size (in bytes) will | 34 # Files larger than this file size (in bytes) will |
| 31 # use the optimization parameters tailored for large files. | 35 # use the optimization parameters tailored for large files. |
| 32 LARGE_FILE_THRESHOLD=3000 | 36 LARGE_FILE_THRESHOLD=3000 |
| 33 | 37 |
| 34 # Constants used for optimization | 38 # Constants used for optimization |
| 35 readonly DEFAULT_MIN_BLOCK_SIZE=128 | 39 readonly DEFAULT_MIN_BLOCK_SIZE=128 |
| 36 readonly DEFAULT_LIMIT_BLOCKS=256 | 40 readonly DEFAULT_LIMIT_BLOCKS=256 |
| 37 readonly DEFAULT_RANDOM_TRIALS=100 | 41 readonly DEFAULT_RANDOM_TRIALS=100 |
| 38 # Taken from the recommendation in the pngslim's readme.txt. | 42 # Taken from the recommendation in the pngslim's readme.txt. |
| 39 readonly LARGE_MIN_BLOCK_SIZE=1 | 43 readonly LARGE_MIN_BLOCK_SIZE=1 |
| 40 readonly LARGE_LIMIT_BLOCKS=2 | 44 readonly LARGE_LIMIT_BLOCKS=2 |
| 41 readonly LARGE_RANDOM_TRIALS=1 | 45 readonly LARGE_RANDOM_TRIALS=1 |
| 42 | 46 |
| 43 # Global variables for stats | 47 # Global variables for stats |
| 44 TOTAL_OLD_BYTES=0 | 48 TOTAL_OLD_BYTES=0 |
| 45 TOTAL_NEW_BYTES=0 | 49 TOTAL_NEW_BYTES=0 |
| 46 TOTAL_FILE=0 | 50 TOTAL_FILE=0 |
| 51 CORRUPTED_FILE=0 |
| 47 PROCESSED_FILE=0 | 52 PROCESSED_FILE=0 |
| 48 | 53 |
| 49 declare -a THROBBER_STR=('-' '\\' '|' '/') | 54 declare -a THROBBER_STR=('-' '\\' '|' '/') |
| 50 THROBBER_COUNT=0 | 55 THROBBER_COUNT=0 |
| 51 | 56 |
| 57 SUBPROCESS=0 |
| 58 |
| 59 # NoOp in subprocess, echo otherwise. |
| 60 function info { |
| 61 if [ $SUBPROCESS -eq 0 ]; then |
| 62 echo $@ |
| 63 fi |
| 64 } |
| 65 |
| 52 # Show throbber character at current cursor position. | 66 # Show throbber character at current cursor position. |
| 53 function throbber { | 67 function throbber { |
| 54 echo -ne "${THROBBER_STR[$THROBBER_COUNT]}\b" | 68 info -ne "${THROBBER_STR[$THROBBER_COUNT]}\b" |
| 55 let THROBBER_COUNT=($THROBBER_COUNT+1)%4 | 69 let THROBBER_COUNT=($THROBBER_COUNT+1)%4 |
| 56 } | 70 } |
| 57 | 71 |
| 58 # Usage: pngout_loop <file> <png_out_options> ... | 72 # Usage: pngout_loop <file> <png_out_options> ... |
| 59 # Optimize the png file using pngout with the given options | 73 # Optimize the png file using pngout with the given options |
| 60 # using various block split thresholds and filter types. | 74 # using various block split thresholds and filter types. |
| 61 function pngout_loop { | 75 function pngout_loop { |
| 62 local file=$1 | 76 local file=$1 |
| 63 shift | 77 shift |
| 64 local opts=$* | 78 local opts=$* |
| (...skipping 20 matching lines...) Expand all Loading... |
| 85 else | 99 else |
| 86 echo "-d1 -d2 -d4 -d8" | 100 echo "-d1 -d2 -d4 -d8" |
| 87 fi | 101 fi |
| 88 } | 102 } |
| 89 | 103 |
| 90 # Usage: process_grayscale <file> | 104 # Usage: process_grayscale <file> |
| 91 # Optimize grayscale images for all color bit depths. | 105 # Optimize grayscale images for all color bit depths. |
| 92 # | 106 # |
| 93 # TODO(oshima): Experiment with -d0 w/o -c0. | 107 # TODO(oshima): Experiment with -d0 w/o -c0. |
| 94 function process_grayscale { | 108 function process_grayscale { |
| 95 echo -n "|gray" | 109 info -n "|gray" |
| 96 for opt in $(get_color_depth_list); do | 110 for opt in $(get_color_depth_list); do |
| 97 pngout_loop $file -c0 $opt | 111 pngout_loop $file -c0 $opt |
| 98 done | 112 done |
| 99 } | 113 } |
| 100 | 114 |
| 101 # Usage: process_grayscale_alpha <file> | 115 # Usage: process_grayscale_alpha <file> |
| 102 # Optimize grayscale images with alpha for all color bit depths. | 116 # Optimize grayscale images with alpha for all color bit depths. |
| 103 function process_grayscale_alpha { | 117 function process_grayscale_alpha { |
| 104 echo -n "|gray-a" | 118 info -n "|gray-a" |
| 105 pngout_loop $file -c4 | 119 pngout_loop $file -c4 |
| 106 for opt in $(get_color_depth_list); do | 120 for opt in $(get_color_depth_list); do |
| 107 pngout_loop $file -c3 $opt | 121 pngout_loop $file -c3 $opt |
| 108 done | 122 done |
| 109 } | 123 } |
| 110 | 124 |
| 111 # Usage: process_rgb <file> | 125 # Usage: process_rgb <file> |
| 112 # Optimize rgb images with or without alpha for all color bit depths. | 126 # Optimize rgb images with or without alpha for all color bit depths. |
| 113 function process_rgb { | 127 function process_rgb { |
| 114 echo -n "|rgb" | 128 info -n "|rgb" |
| 115 for opt in $(get_color_depth_list); do | 129 for opt in $(get_color_depth_list); do |
| 116 pngout_loop $file -c3 $opt | 130 pngout_loop $file -c3 $opt |
| 117 done | 131 done |
| 118 pngout_loop $file -c2 | 132 pngout_loop $file -c2 |
| 119 pngout_loop $file -c6 | 133 pngout_loop $file -c6 |
| 120 } | 134 } |
| 121 | 135 |
| 122 # Usage: huffman_blocks <file> | 136 # Usage: huffman_blocks <file> |
| 123 # Optimize the huffman blocks. | 137 # Optimize the huffman blocks. |
| 124 function huffman_blocks { | 138 function huffman_blocks { |
| 125 local file=$1 | 139 local file=$1 |
| 126 echo -n "|huffman" | 140 info -n "|huffman" |
| 127 local size=$(stat -c%s $file) | 141 local size=$(stat -c%s $file) |
| 128 local min_block_size=$DEFAULT_MIN_BLOCK_SIZE | 142 local min_block_size=$DEFAULT_MIN_BLOCK_SIZE |
| 129 local limit_blocks=$DEFAULT_LIMIT_BLOCKS | 143 local limit_blocks=$DEFAULT_LIMIT_BLOCKS |
| 130 | 144 |
| 131 if [ $size -gt $LARGE_FILE_THRESHOLD ]; then | 145 if [ $size -gt $LARGE_FILE_THRESHOLD ]; then |
| 132 min_block_size=$LARGE_MIN_BLOCK_SIZE | 146 min_block_size=$LARGE_MIN_BLOCK_SIZE |
| 133 limit_blocks=$LARGE_LIMIT_BLOCKS | 147 limit_blocks=$LARGE_LIMIT_BLOCKS |
| 134 fi | 148 fi |
| 135 let max_blocks=$size/$min_block_size | 149 let max_blocks=$size/$min_block_size |
| 136 if [ $max_blocks -gt $limit_blocks ]; then | 150 if [ $max_blocks -gt $limit_blocks ]; then |
| 137 max_blocks=$limit_blocks | 151 max_blocks=$limit_blocks |
| 138 fi | 152 fi |
| 139 | 153 |
| 140 for i in $(seq 2 $max_blocks); do | 154 for i in $(seq 2 $max_blocks); do |
| 141 throbber | 155 throbber |
| 142 pngout -q -k1 -ks -s1 -n$i $file | 156 pngout -q -k1 -ks -s1 -n$i $file |
| 143 done | 157 done |
| 144 } | 158 } |
| 145 | 159 |
| 146 # Usage: random_huffman_table_trial <file> | 160 # Usage: random_huffman_table_trial <file> |
| 147 # Try compressing by randomizing the initial huffman table. | 161 # Try compressing by randomizing the initial huffman table. |
| 148 # | 162 # |
| 149 # TODO(oshima): Try adjusting different parameters for large files to | 163 # TODO(oshima): Try adjusting different parameters for large files to |
| 150 # reduce runtime. | 164 # reduce runtime. |
| 151 function random_huffman_table_trial { | 165 function random_huffman_table_trial { |
| 152 echo -n "|random" | 166 info -n "|random" |
| 153 local file=$1 | 167 local file=$1 |
| 154 local old_size=$(stat -c%s $file) | 168 local old_size=$(stat -c%s $file) |
| 155 local trials_count=$DEFAULT_RANDOM_TRIALS | 169 local trials_count=$DEFAULT_RANDOM_TRIALS |
| 156 | 170 |
| 157 if [ $old_size -gt $LARGE_FILE_THRESHOLD ]; then | 171 if [ $old_size -gt $LARGE_FILE_THRESHOLD ]; then |
| 158 trials_count=$LARGE_RANDOM_TRIALS | 172 trials_count=$LARGE_RANDOM_TRIALS |
| 159 fi | 173 fi |
| 160 for i in $(seq 1 $trials_count); do | 174 for i in $(seq 1 $trials_count); do |
| 161 throbber | 175 throbber |
| 162 pngout -q -k1 -ks -s0 -r $file | 176 pngout -q -k1 -ks -s0 -r $file |
| 163 done | 177 done |
| 164 local new_size=$(stat -c%s $file) | 178 local new_size=$(stat -c%s $file) |
| 165 if [ $new_size -lt $old_size ]; then | 179 if [ $new_size -lt $old_size ]; then |
| 166 random_huffman_table_trial $file | 180 random_huffman_table_trial $file |
| 167 fi | 181 fi |
| 168 } | 182 } |
| 169 | 183 |
| 170 # Usage: final_comprssion <file> | 184 # Usage: final_comprssion <file> |
| 171 # Further compress using optipng and advdef. | 185 # Further compress using optipng and advdef. |
| 172 # TODO(oshima): Experiment with 256. | 186 # TODO(oshima): Experiment with 256. |
| 173 function final_compression { | 187 function final_compression { |
| 174 echo -n "|final" | 188 info -n "|final" |
| 175 local file=$1 | 189 local file=$1 |
| 176 if [ $OPTIMIZE_LEVEL == 2 ]; then | 190 if [ $OPTIMIZE_LEVEL == 2 ]; then |
| 177 for i in 32k 16k 8k 4k 2k 1k 512; do | 191 for i in 32k 16k 8k 4k 2k 1k 512; do |
| 178 throbber | 192 throbber |
| 179 optipng -q -nb -nc -zw$i -zc1-9 -zm1-9 -zs0-3 -f0-5 $file | 193 optipng -q -nb -nc -zw$i -zc1-9 -zm1-9 -zs0-3 -f0-5 $file |
| 180 done | 194 done |
| 181 fi | 195 fi |
| 182 for i in $(seq 1 4); do | 196 for i in $(seq 1 4); do |
| 183 throbber | 197 throbber |
| 184 advdef -q -z -$i $file | 198 advdef -q -z -$i $file |
| 185 done | 199 done |
| 186 echo -ne "\r" | 200 info -ne "\r" |
| 187 } | 201 } |
| 188 | 202 |
| 189 # Usage: get_color_type <file> | 203 # Usage: get_color_type <file> |
| 190 # Returns the color type name of the png file. Here is the list of names | 204 # Returns the color type name of the png file. Here is the list of names |
| 191 # for each color type codes. | 205 # for each color type codes. |
| 192 # 0 : grayscale | 206 # 0 : grayscale |
| 193 # 2 : RGB | 207 # 2 : RGB |
| 194 # 3 : colormap | 208 # 3 : colormap |
| 195 # 4 : gray+alpha | 209 # 4 : gray+alpha |
| 196 # 6 : RGBA | 210 # 6 : RGBA |
| 197 # See http://en.wikipedia.org/wiki/Portable_Network_Graphics#Color_depth | 211 # See http://en.wikipedia.org/wiki/Portable_Network_Graphics#Color_depth |
| 198 # for details about the color type code. | 212 # for details about the color type code. |
| 199 function get_color_type { | 213 function get_color_type { |
| 200 local file=$1 | 214 local file=$1 |
| 201 echo $(file $file | awk -F, '{print $3}' | awk '{print $2}') | 215 echo $(file $file | awk -F, '{print $3}' | awk '{print $2}') |
| 202 } | 216 } |
| 203 | 217 |
| 204 # Usage: optimize_size <file> | 218 # Usage: optimize_size <file> |
| 205 # Performs png file optimization. | 219 # Performs png file optimization. |
| 206 function optimize_size { | 220 function optimize_size { |
| 207 tput el | 221 tput el |
| 208 local file=$1 | 222 local file=$1 |
| 209 echo -n "$file " | 223 info -n "$file " |
| 210 | 224 |
| 211 advdef -q -z -4 $file | 225 advdef -q -z -4 $file |
| 212 | 226 |
| 213 pngout -q -s4 -c0 -force $file $file.tmp.png | 227 pngout -q -s4 -c0 -force $file $file.tmp.png |
| 214 if [ -f $file.tmp.png ]; then | 228 if [ -f $file.tmp.png ]; then |
| 215 rm $file.tmp.png | 229 rm $file.tmp.png |
| 216 process_grayscale $file | 230 process_grayscale $file |
| 217 process_grayscale_alpha $file | 231 process_grayscale_alpha $file |
| 218 else | 232 else |
| 219 pngout -q -s4 -c4 -force $file $file.tmp.png | 233 pngout -q -s4 -c4 -force $file $file.tmp.png |
| 220 if [ -f $file.tmp.png ]; then | 234 if [ -f $file.tmp.png ]; then |
| 221 rm $file.tmp.png | 235 rm $file.tmp.png |
| 222 process_grayscale_alpha $file | 236 process_grayscale_alpha $file |
| 223 else | 237 else |
| 224 process_rgb $file | 238 process_rgb $file |
| 225 fi | 239 fi |
| 226 fi | 240 fi |
| 227 | 241 |
| 228 echo -n "|filter" | 242 info -n "|filter" |
| 229 local old_color_type=$(get_color_type $file) | 243 local old_color_type=$(get_color_type $file) |
| 230 optipng -q -zc9 -zm8 -zs0-3 -f0-5 $file -out $file.tmp.png | 244 optipng -q -zc9 -zm8 -zs0-3 -f0-5 $file -out $file.tmp.png |
| 231 local new_color_type=$(get_color_type $file.tmp.png) | 245 local new_color_type=$(get_color_type $file.tmp.png) |
| 232 # optipng may corrupt a png file when reducing the color type | 246 # optipng may corrupt a png file when reducing the color type |
| 233 # to grayscale/grayscale+alpha. Just skip such cases until | 247 # to grayscale/grayscale+alpha. Just skip such cases until |
| 234 # the bug is fixed. See crbug.com/174505, crbug.com/174084. | 248 # the bug is fixed. See crbug.com/174505, crbug.com/174084. |
| 235 # The issue is reported in | 249 # The issue is reported in |
| 236 # https://sourceforge.net/tracker/?func=detail&aid=3603630&group_id=151404&ati
d=780913 | 250 # 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 | 251 if [[ $old_color_type == "RGBA" && $new_color_type =~ gray.* ]] ; then |
| 238 rm $file.tmp.png | 252 rm $file.tmp.png |
| 239 echo -n "[skip opting]" | 253 info -n "[skip opting]" |
| 240 else | 254 else |
| 241 mv $file.tmp.png $file | 255 mv $file.tmp.png $file |
| 242 fi | 256 fi |
| 243 pngout -q -k1 -s1 $file | 257 pngout -q -k1 -s1 $file |
| 244 | 258 |
| 245 huffman_blocks $file | 259 huffman_blocks $file |
| 246 | 260 |
| 247 # TODO(oshima): Experiment with strategy 1. | 261 # TODO(oshima): Experiment with strategy 1. |
| 248 echo -n "|strategy" | 262 info -n "|strategy" |
| 249 if [ $OPTIMIZE_LEVEL == 2 ]; then | 263 if [ $OPTIMIZE_LEVEL == 2 ]; then |
| 250 for i in 3 2 0; do | 264 for i in 3 2 0; do |
| 251 pngout -q -k1 -ks -s$i $file | 265 pngout -q -k1 -ks -s$i $file |
| 252 done | 266 done |
| 253 else | 267 else |
| 254 pngout -q -k1 -ks -s1 $file | 268 pngout -q -k1 -ks -s1 $file |
| 255 fi | 269 fi |
| 256 | 270 |
| 257 if [ $OPTIMIZE_LEVEL == 2 ]; then | 271 if [ $OPTIMIZE_LEVEL == 2 ]; then |
| 258 random_huffman_table_trial $file | 272 random_huffman_table_trial $file |
| 259 fi | 273 fi |
| 260 | 274 |
| 261 final_compression $file | 275 final_compression $file |
| 262 } | 276 } |
| 263 | 277 |
| 264 # Usage: process_file <file> | 278 # Usage: process_file <file> |
| 265 function process_file { | 279 function process_file { |
| 266 local file=$1 | 280 local file=$1 |
| 267 local name=$(basename $file) | 281 local name=$(basename $file) |
| 268 # -rem alla removes all ancillary chunks except for tRNS | 282 # -rem alla removes all ancillary chunks except for tRNS |
| 269 pngcrush -d $TMP_DIR -brute -reduce -rem alla $file > /dev/null | 283 pngcrush -d $TMP_DIR -brute -reduce -rem alla $file > /dev/null |
| 270 | 284 |
| 271 if [ $OPTIMIZE_LEVEL != 0 ]; then | 285 if [ -f $TMP_DIR/$name -a $OPTIMIZE_LEVEL != 0 ]; then |
| 272 optimize_size $TMP_DIR/$name | 286 optimize_size $TMP_DIR/$name |
| 273 fi | 287 fi |
| 274 } | 288 } |
| 275 | 289 |
| 276 # Usage: optimize_file <file> | 290 # Usage: optimize_file <file> |
| 277 function optimize_file { | 291 function optimize_file { |
| 278 local file=$1 | 292 local file=$1 |
| 279 if $using_cygwin ; then | 293 if $using_cygwin ; then |
| 280 file=$(cygpath -w $file) | 294 file=$(cygpath -w $file) |
| 281 fi | 295 fi |
| 282 | 296 |
| 283 local name=$(basename $file) | 297 local name=$(basename $file) |
| 284 local old=$(stat -c%s $file) | 298 local old=$(stat -c%s $file) |
| 285 local tmp_file=$TMP_DIR/$name | 299 local tmp_file=$TMP_DIR/$name |
| 300 let TOTAL_FILE+=1 |
| 286 | 301 |
| 287 process_file $file | 302 process_file $file |
| 288 | 303 |
| 304 if [ ! -e $tmp_file ] ; then |
| 305 let CORRUPTED_FILE+=1 |
| 306 echo "The png file ($file) may be corrupted. skipping" |
| 307 return |
| 308 fi |
| 309 |
| 289 local new=$(stat -c%s $tmp_file) | 310 local new=$(stat -c%s $tmp_file) |
| 290 let diff=$old-$new | 311 let diff=$old-$new |
| 291 let percent=($diff*100)/$old | 312 let percent=($diff*100)/$old |
| 292 let TOTAL_FILE+=1 | |
| 293 | 313 |
| 294 tput el | 314 tput el |
| 295 if [ $new -lt $old ]; then | 315 if [ $new -lt $old ]; then |
| 296 echo -ne "$file : $old => $new ($diff bytes : $percent %)\n" | 316 echo -ne "$file : $old => $new ($diff bytes : $percent %)\n" |
| 297 mv "$tmp_file" "$file" | 317 mv "$tmp_file" "$file" |
| 298 let TOTAL_OLD_BYTES+=$old | 318 let TOTAL_OLD_BYTES+=$old |
| 299 let TOTAL_NEW_BYTES+=$new | 319 let TOTAL_NEW_BYTES+=$new |
| 300 let PROCESSED_FILE+=1 | 320 let PROCESSED_FILE+=1 |
| 301 else | 321 else |
| 302 if [ $OPTIMIZE_LEVEL == 0 ]; then | 322 if [ $OPTIMIZE_LEVEL == 0 ]; then |
| 303 echo -ne "$file : skipped\r" | 323 info -ne "$file : skipped\r" |
| 304 fi | 324 fi |
| 305 rm $tmp_file | 325 rm $tmp_file |
| 306 fi | 326 fi |
| 307 } | 327 } |
| 308 | 328 |
| 309 function optimize_dir { | 329 function optimize_dir { |
| 310 local dir=$1 | 330 local dir=$1 |
| 311 if $using_cygwin ; then | 331 if $using_cygwin ; then |
| 312 dir=$(cygpath -w $dir) | 332 dir=$(cygpath -w $dir) |
| 313 fi | 333 fi |
| (...skipping 26 matching lines...) Expand all Loading... |
| 340 which $program > /dev/null 2>&1 | 360 which $program > /dev/null 2>&1 |
| 341 if [ $? != 0 ]; then | 361 if [ $? != 0 ]; then |
| 342 echo "Couldn't find $program. Please download and install it from $url ." | 362 echo "Couldn't find $program. Please download and install it from $url ." |
| 343 exit 1 | 363 exit 1 |
| 344 fi | 364 fi |
| 345 } | 365 } |
| 346 | 366 |
| 347 function show_help { | 367 function show_help { |
| 348 local program=$(basename $0) | 368 local program=$(basename $0) |
| 349 echo \ | 369 echo \ |
| 350 "Usage: $program [options] dir ... | 370 "Usage: $program [options] <dir> ... |
| 351 | 371 |
| 352 $program is a utility to reduce the size of png files by removing | 372 $program is a utility to reduce the size of png files by removing |
| 353 unnecessary chunks and compressing the image. | 373 unnecessary chunks and compressing the image. |
| 354 | 374 |
| 355 Options: | 375 Options: |
| 356 -o<optimize_level> Specify optimization level: (default is 1) | 376 -o<optimize_level> Specify optimization level: (default is 1) |
| 357 0 Just run pngcrush. It removes unnecessary chunks and perform basic | 377 0 Just run pngcrush. It removes unnecessary chunks and perform basic |
| 358 optimization on the encoded data. | 378 optimization on the encoded data. |
| 359 1 Optimize png files using pngout/optipng and advdef. This can further | 379 1 Optimize png files using pngout/optipng and advdef. This can further |
| 360 reduce addtional 5~30%. This is the default level. | 380 reduce addtional 5~30%. This is the default level. |
| 361 2 Aggressively optimize the size of png files. This may produce | 381 2 Aggressively optimize the size of png files. This may produce |
| 362 addtional 1%~5% reduction. Warning: this is *VERY* | 382 addtional 1%~5% reduction. Warning: this is *VERY* |
| 363 slow and can take hours to process all files. | 383 slow and can take hours to process all files. |
| 364 -r<revision> If this is specified, the script processes only png files | 384 -r<revision> If this is specified, the script processes only png files |
| 365 changed since this revision. The <dir> options will be used | 385 changed since this revision. The <dir> options will be used |
| 366 to narrow down the files under specific directories. | 386 to narrow down the files under specific directories. |
| 387 -s Run as subprocess. This may be used to parallelize execution. |
| 388 Usage: find <dir> -name \"*.png\" | xargs -n1 -P16 $program -s -o2 |
| 367 -h Print this help text." | 389 -h Print this help text." |
| 368 exit 1 | 390 exit 1 |
| 369 } | 391 } |
| 370 | 392 |
| 371 if [ ! -e ../.gclient ]; then | 393 if [ ! -e ../.gclient ]; then |
| 372 echo "$0 must be run in src directory" | 394 echo "$0 must be run in src directory" |
| 373 exit 1 | 395 exit 1 |
| 374 fi | 396 fi |
| 375 | 397 |
| 376 if [ "$(expr substr $(uname -s) 1 6)" == "CYGWIN" ]; then | 398 if [ "$(expr substr $(uname -s) 1 6)" == "CYGWIN" ]; then |
| 377 using_cygwin=true | 399 using_cygwin=true |
| 378 else | 400 else |
| 379 using_cygwin=false | 401 using_cygwin=false |
| 380 fi | 402 fi |
| 381 | 403 |
| 382 OPTIMIZE_LEVEL=1 | 404 OPTIMIZE_LEVEL=1 |
| 383 # Parse options | 405 # Parse options |
| 384 while getopts o:r:h opts | 406 while getopts o:r:h:s opts |
| 385 do | 407 do |
| 386 case $opts in | 408 case $opts in |
| 387 r) | 409 r) |
| 388 COMMIT=$(git svn find-rev r$OPTARG | tail -1) || exit | 410 COMMIT=$(git svn find-rev r$OPTARG | tail -1) || exit |
| 389 if [ -z "$COMMIT" ] ; then | 411 if [ -z "$COMMIT" ] ; then |
| 390 echo "Revision $OPTARG not found" | 412 echo "Revision $OPTARG not found" |
| 391 show_help | 413 show_help |
| 392 fi | 414 fi |
| 393 ;; | 415 ;; |
| 394 o) | 416 o) |
| 395 if [[ ! "$OPTARG" =~ [012] ]] ; then | 417 if [[ ! "$OPTARG" =~ [012] ]] ; then |
| 396 show_help | 418 show_help |
| 397 fi | 419 fi |
| 398 OPTIMIZE_LEVEL=$OPTARG | 420 OPTIMIZE_LEVEL=$OPTARG |
| 399 ;; | 421 ;; |
| 422 s) |
| 423 let SUBPROCESS=1 |
| 424 ;; |
| 400 [h?]) | 425 [h?]) |
| 401 show_help;; | 426 show_help;; |
| 402 esac | 427 esac |
| 403 done | 428 done |
| 404 | 429 |
| 405 # Remove options from argument list. | 430 # Remove options from argument list. |
| 406 shift $(($OPTIND -1)) | 431 shift $(($OPTIND -1)) |
| 407 | 432 |
| 408 # Make sure we have all necessary commands installed. | 433 # Make sure we have all necessary commands installed. |
| 409 install_if_not_installed pngcrush pngcrush | 434 install_if_not_installed pngcrush pngcrush |
| (...skipping 20 matching lines...) Expand all Loading... |
| 430 TMP_DIR=$(cygpath -w $TMP_DIR) | 455 TMP_DIR=$(cygpath -w $TMP_DIR) |
| 431 fi | 456 fi |
| 432 | 457 |
| 433 # Make sure we cleanup temp dir | 458 # Make sure we cleanup temp dir |
| 434 trap "rm -rf $TMP_DIR" EXIT | 459 trap "rm -rf $TMP_DIR" EXIT |
| 435 | 460 |
| 436 # If no directories are specified, optimize all directories. | 461 # If no directories are specified, optimize all directories. |
| 437 DIRS=$@ | 462 DIRS=$@ |
| 438 set ${DIRS:=$ALL_DIRS} | 463 set ${DIRS:=$ALL_DIRS} |
| 439 | 464 |
| 440 echo "Optimize level=$OPTIMIZE_LEVEL" | 465 info "Optimize level=$OPTIMIZE_LEVEL" |
| 466 |
| 441 if [ -n "$COMMIT" ] ; then | 467 if [ -n "$COMMIT" ] ; then |
| 442 ALL_FILES=$(git diff --name-only $COMMIT HEAD $DIRS | grep "png$") | 468 ALL_FILES=$(git diff --name-only $COMMIT HEAD $DIRS | grep "png$") |
| 443 ALL_FILES_LIST=( $ALL_FILES ) | 469 ALL_FILES_LIST=( $ALL_FILES ) |
| 444 echo "Processing ${#ALL_FILES_LIST[*]} files" | 470 echo "Processing ${#ALL_FILES_LIST[*]} files" |
| 445 for f in $ALL_FILES; do | 471 for f in $ALL_FILES; do |
| 446 if [ -f $f ] ; then | 472 if [ -f $f ] ; then |
| 447 optimize_file $f | 473 optimize_file $f |
| 448 else | 474 else |
| 449 echo "Skipping deleted file: $f"; | 475 echo "Skipping deleted file: $f"; |
| 450 fi | 476 fi |
| 451 done | 477 done |
| 452 else | 478 else |
| 453 for d in $DIRS; do | 479 for d in $DIRS; do |
| 454 echo "Optimizing png files in $d" | 480 info "Optimizing png files in $d" |
| 455 optimize_dir $d | 481 optimize_dir $d |
| 456 echo | 482 echo |
| 457 done | 483 done |
| 458 fi | 484 fi |
| 459 | 485 |
| 460 # Print the results. | 486 # Print the results. |
| 461 if [ $PROCESSED_FILE == 0 ]; then | 487 if [ $PROCESSED_FILE == 0 ]; then |
| 462 echo "Did not find any files (out of $TOTAL_FILE files)" \ | 488 echo "Did not find any files (out of $TOTAL_FILE files)" \ |
| 463 "that could be optimized" \ | 489 "that could be optimized" \ |
| 464 "in $(date -u -d @$SECONDS +%T)s" | 490 "in $(date -u -d @$SECONDS +%T)s" |
| 465 else | 491 else |
| 466 let diff=$TOTAL_OLD_BYTES-$TOTAL_NEW_BYTES | 492 let diff=$TOTAL_OLD_BYTES-$TOTAL_NEW_BYTES |
| 467 let percent=$diff*100/$TOTAL_OLD_BYTES | 493 let percent=$diff*100/$TOTAL_OLD_BYTES |
| 468 echo "Processed $PROCESSED_FILE files (out of $TOTAL_FILE files)" \ | 494 echo "Processed $PROCESSED_FILE files (out of $TOTAL_FILE files)" \ |
| 469 "in $(date -u -d @$SECONDS +%T)s" | 495 "in $(date -u -d @$SECONDS +%T)s" |
| 470 echo "Result : $TOTAL_OLD_BYTES => $TOTAL_NEW_BYTES bytes" \ | 496 echo "Result : $TOTAL_OLD_BYTES => $TOTAL_NEW_BYTES bytes" \ |
| 471 "($diff bytes : $percent %)" | 497 "($diff bytes : $percent %)" |
| 472 fi | 498 fi |
| 499 if [ $CORRUPTED_FILE != 0 ]; then |
| 500 echo "Warning: corrupted files found: $CORRUPTED_FILE" |
| 501 echo "Please contact the author of the CL that landed corrupted png files" |
| 502 fi |
| OLD | NEW |