| OLD | NEW |
| 1 #!/bin/bash | 1 #!/bin/bash -i |
| 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. |
| (...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 47 # Global variables for stats | 47 # Global variables for stats |
| 48 TOTAL_OLD_BYTES=0 | 48 TOTAL_OLD_BYTES=0 |
| 49 TOTAL_NEW_BYTES=0 | 49 TOTAL_NEW_BYTES=0 |
| 50 TOTAL_FILE=0 | 50 TOTAL_FILE=0 |
| 51 CORRUPTED_FILE=0 | 51 CORRUPTED_FILE=0 |
| 52 PROCESSED_FILE=0 | 52 PROCESSED_FILE=0 |
| 53 | 53 |
| 54 declare -a THROBBER_STR=('-' '\\' '|' '/') | 54 declare -a THROBBER_STR=('-' '\\' '|' '/') |
| 55 THROBBER_COUNT=0 | 55 THROBBER_COUNT=0 |
| 56 | 56 |
| 57 VERBOSE=0 | 57 VERBOSE=false |
| 58 | 58 |
| 59 # Echo only if verbose option is set. | 59 # Echo only if verbose option is set. |
| 60 function info { | 60 function info { |
| 61 if [ $VERBOSE -eq 1 ]; then | 61 if $VERBOSE ; then |
| 62 echo $@ | 62 echo $@ |
| 63 fi | 63 fi |
| 64 } | 64 } |
| 65 | 65 |
| 66 # Show throbber character at current cursor position. | 66 # Show throbber character at current cursor position. |
| 67 function throbber { | 67 function throbber { |
| 68 info -ne "${THROBBER_STR[$THROBBER_COUNT]}\b" | 68 info -ne "${THROBBER_STR[$THROBBER_COUNT]}\b" |
| 69 let THROBBER_COUNT=($THROBBER_COUNT+1)%4 | 69 let THROBBER_COUNT=$THROBBER_COUNT+1 |
| 70 let THROBBER_COUNT=$THROBBER_COUNT%4 |
| 70 } | 71 } |
| 71 | 72 |
| 72 # Usage: pngout_loop <file> <png_out_options> ... | 73 # Usage: pngout_loop <file> <png_out_options> ... |
| 73 # Optimize the png file using pngout with the given options | 74 # Optimize the png file using pngout with the given options |
| 74 # using various block split thresholds and filter types. | 75 # using various block split thresholds and filter types. |
| 75 function pngout_loop { | 76 function pngout_loop { |
| 76 local file=$1 | 77 local file=$1 |
| 77 shift | 78 shift |
| 78 local opts=$* | 79 local opts=$* |
| 79 if [ $OPTIMIZE_LEVEL == 1 ]; then | 80 if [ $OPTIMIZE_LEVEL == 1 ]; then |
| 80 for j in $(seq 0 5); do | 81 for j in $(eval echo {0..5}); do |
| 81 throbber | 82 throbber |
| 82 pngout -q -k1 -s1 -f$j $opts $file | 83 pngout -q -k1 -s1 -f$j $opts $file |
| 83 done | 84 done |
| 84 else | 85 else |
| 85 for i in 0 128 256 512; do | 86 for i in 0 128 256 512; do |
| 86 for j in $(seq 0 5); do | 87 for j in $(eval echo {0..5}); do |
| 87 throbber | 88 throbber |
| 88 pngout -q -k1 -s1 -b$i -f$j $opts $file | 89 pngout -q -k1 -s1 -b$i -f$j $opts $file |
| 89 done | 90 done |
| 90 done | 91 done |
| 91 fi | 92 fi |
| 92 } | 93 } |
| 93 | 94 |
| 94 # Usage: get_color_depth_list | 95 # Usage: get_color_depth_list |
| 95 # Returns the list of color depth options for current optimization level. | 96 # Returns the list of color depth options for current optimization level. |
| 96 function get_color_depth_list { | 97 function get_color_depth_list { |
| 97 if [ $OPTIMIZE_LEVEL == 1 ]; then | 98 if [ $OPTIMIZE_LEVEL == 1 ]; then |
| 98 echo "-d0" | 99 echo "-d0" |
| 99 else | 100 else |
| 100 echo "-d1 -d2 -d4 -d8" | 101 echo "-d1 -d2 -d4 -d8" |
| 101 fi | 102 fi |
| 102 } | 103 } |
| 103 | 104 |
| 104 # Usage: process_grayscale <file> | 105 # Usage: process_grayscale <file> |
| 105 # Optimize grayscale images for all color bit depths. | 106 # Optimize grayscale images for all color bit depths. |
| 106 # | 107 # |
| 107 # TODO(oshima): Experiment with -d0 w/o -c0. | 108 # TODO(oshima): Experiment with -d0 w/o -c0. |
| 108 function process_grayscale { | 109 function process_grayscale { |
| 109 info -n "|gray" | 110 info -ne "\b\b\b\b\b\b\b\bgray...." |
| 110 for opt in $(get_color_depth_list); do | 111 for opt in $(get_color_depth_list); do |
| 111 pngout_loop $file -c0 $opt | 112 pngout_loop $file -c0 $opt |
| 112 done | 113 done |
| 113 } | 114 } |
| 114 | 115 |
| 115 # Usage: process_grayscale_alpha <file> | 116 # Usage: process_grayscale_alpha <file> |
| 116 # Optimize grayscale images with alpha for all color bit depths. | 117 # Optimize grayscale images with alpha for all color bit depths. |
| 117 function process_grayscale_alpha { | 118 function process_grayscale_alpha { |
| 118 info -n "|gray-a" | 119 info -ne "\b\b\b\b\b\b\b\bgray-a.." |
| 119 pngout_loop $file -c4 | 120 pngout_loop $file -c4 |
| 120 for opt in $(get_color_depth_list); do | 121 for opt in $(get_color_depth_list); do |
| 121 pngout_loop $file -c3 $opt | 122 pngout_loop $file -c3 $opt |
| 122 done | 123 done |
| 123 } | 124 } |
| 124 | 125 |
| 125 # Usage: process_rgb <file> | 126 # Usage: process_rgb <file> |
| 126 # Optimize rgb images with or without alpha for all color bit depths. | 127 # Optimize rgb images with or without alpha for all color bit depths. |
| 127 function process_rgb { | 128 function process_rgb { |
| 128 info -n "|rgb" | 129 info -ne "\b\b\b\b\b\b\b\brgb....." |
| 129 for opt in $(get_color_depth_list); do | 130 for opt in $(get_color_depth_list); do |
| 130 pngout_loop $file -c3 $opt | 131 pngout_loop $file -c3 $opt |
| 131 done | 132 done |
| 132 pngout_loop $file -c2 | 133 pngout_loop $file -c2 |
| 133 pngout_loop $file -c6 | 134 pngout_loop $file -c6 |
| 134 } | 135 } |
| 135 | 136 |
| 136 # Usage: huffman_blocks <file> | 137 # Usage: huffman_blocks <file> |
| 137 # Optimize the huffman blocks. | 138 # Optimize the huffman blocks. |
| 138 function huffman_blocks { | 139 function huffman_blocks { |
| 140 info -ne "\b\b\b\b\b\b\b\bhuffman." |
| 139 local file=$1 | 141 local file=$1 |
| 140 info -n "|huffman" | |
| 141 local size=$(stat -c%s $file) | 142 local size=$(stat -c%s $file) |
| 142 local min_block_size=$DEFAULT_MIN_BLOCK_SIZE | 143 local min_block_size=$DEFAULT_MIN_BLOCK_SIZE |
| 143 local limit_blocks=$DEFAULT_LIMIT_BLOCKS | 144 local limit_blocks=$DEFAULT_LIMIT_BLOCKS |
| 144 | 145 |
| 145 if [ $size -gt $LARGE_FILE_THRESHOLD ]; then | 146 if [ $size -gt $LARGE_FILE_THRESHOLD ]; then |
| 146 min_block_size=$LARGE_MIN_BLOCK_SIZE | 147 min_block_size=$LARGE_MIN_BLOCK_SIZE |
| 147 limit_blocks=$LARGE_LIMIT_BLOCKS | 148 limit_blocks=$LARGE_LIMIT_BLOCKS |
| 148 fi | 149 fi |
| 149 let max_blocks=$size/$min_block_size | 150 let max_blocks=$size/$min_block_size |
| 150 if [ $max_blocks -gt $limit_blocks ]; then | 151 if [ $max_blocks -gt $limit_blocks ]; then |
| 151 max_blocks=$limit_blocks | 152 max_blocks=$limit_blocks |
| 152 fi | 153 fi |
| 153 | 154 |
| 154 for i in $(seq 2 $max_blocks); do | 155 for i in $(eval echo {2..$max_blocks}); do |
| 155 throbber | 156 throbber |
| 156 pngout -q -k1 -ks -s1 -n$i $file | 157 pngout -q -k1 -ks -s1 -n$i $file |
| 157 done | 158 done |
| 158 } | 159 } |
| 159 | 160 |
| 160 # Usage: random_huffman_table_trial <file> | 161 # Usage: random_huffman_table_trial <file> |
| 161 # Try compressing by randomizing the initial huffman table. | 162 # Try compressing by randomizing the initial huffman table. |
| 162 # | 163 # |
| 163 # TODO(oshima): Try adjusting different parameters for large files to | 164 # TODO(oshima): Try adjusting different parameters for large files to |
| 164 # reduce runtime. | 165 # reduce runtime. |
| 165 function random_huffman_table_trial { | 166 function random_huffman_table_trial { |
| 166 info -n "|random" | 167 info -ne "\b\b\b\b\b\b\b\brandom.." |
| 167 local file=$1 | 168 local file=$1 |
| 168 local old_size=$(stat -c%s $file) | 169 local old_size=$(stat -c%s $file) |
| 169 local trials_count=$DEFAULT_RANDOM_TRIALS | 170 local trials_count=$DEFAULT_RANDOM_TRIALS |
| 170 | 171 |
| 171 if [ $old_size -gt $LARGE_FILE_THRESHOLD ]; then | 172 if [ $old_size -gt $LARGE_FILE_THRESHOLD ]; then |
| 172 trials_count=$LARGE_RANDOM_TRIALS | 173 trials_count=$LARGE_RANDOM_TRIALS |
| 173 fi | 174 fi |
| 174 for i in $(seq 1 $trials_count); do | 175 for i in $(eval echo {1..$trials_count}); do |
| 175 throbber | 176 throbber |
| 176 pngout -q -k1 -ks -s0 -r $file | 177 pngout -q -k1 -ks -s0 -r $file |
| 177 done | 178 done |
| 178 local new_size=$(stat -c%s $file) | 179 local new_size=$(stat -c%s $file) |
| 179 if [ $new_size -lt $old_size ]; then | 180 if [ $new_size -lt $old_size ]; then |
| 180 random_huffman_table_trial $file | 181 random_huffman_table_trial $file |
| 181 fi | 182 fi |
| 182 } | 183 } |
| 183 | 184 |
| 184 # Usage: final_comprssion <file> | 185 # Usage: final_comprssion <file> |
| 185 # Further compress using optipng and advdef. | 186 # Further compress using optipng and advdef. |
| 186 # TODO(oshima): Experiment with 256. | 187 # TODO(oshima): Experiment with 256. |
| 187 function final_compression { | 188 function final_compression { |
| 188 info -n "|final" | 189 info -ne "\b\b\b\b\b\b\b\bfinal..." |
| 189 local file=$1 | 190 local file=$1 |
| 190 if [ $OPTIMIZE_LEVEL == 2 ]; then | 191 if [ $OPTIMIZE_LEVEL == 2 ]; then |
| 191 for i in 32k 16k 8k 4k 2k 1k 512; do | 192 for i in 32k 16k 8k 4k 2k 1k 512; do |
| 192 throbber | 193 throbber |
| 193 optipng -q -nb -nc -zw$i -zc1-9 -zm1-9 -zs0-3 -f0-5 $file | 194 optipng -q -nb -nc -zw$i -zc1-9 -zm1-9 -zs0-3 -f0-5 $file |
| 194 done | 195 done |
| 195 fi | 196 fi |
| 196 for i in $(seq 1 4); do | 197 for i in $(eval echo {1..4}); do |
| 197 throbber | 198 throbber |
| 198 advdef -q -z -$i $file | 199 advdef -q -z -$i $file |
| 199 done | 200 done |
| 200 info -ne "\r" | 201 |
| 202 # Clear the current line. |
| 203 if $VERBOSE ; then |
| 204 printf "\033[0G\033[K" |
| 205 fi |
| 201 } | 206 } |
| 202 | 207 |
| 203 # Usage: get_color_type <file> | 208 # Usage: get_color_type <file> |
| 204 # Returns the color type name of the png file. Here is the list of names | 209 # Returns the color type name of the png file. Here is the list of names |
| 205 # for each color type codes. | 210 # for each color type codes. |
| 206 # 0 : grayscale | 211 # 0: grayscale |
| 207 # 2 : RGB | 212 # 2: RGB |
| 208 # 3 : colormap | 213 # 3: colormap |
| 209 # 4 : gray+alpha | 214 # 4: gray+alpha |
| 210 # 6 : RGBA | 215 # 6: RGBA |
| 211 # See http://en.wikipedia.org/wiki/Portable_Network_Graphics#Color_depth | 216 # See http://en.wikipedia.org/wiki/Portable_Network_Graphics#Color_depth |
| 212 # for details about the color type code. | 217 # for details about the color type code. |
| 213 function get_color_type { | 218 function get_color_type { |
| 214 local file=$1 | 219 local file=$1 |
| 215 echo $(file $file | awk -F, '{print $3}' | awk '{print $2}') | 220 echo $(file $file | awk -F, '{print $3}' | awk '{print $2}') |
| 216 } | 221 } |
| 217 | 222 |
| 218 # Usage: optimize_size <file> | 223 # Usage: optimize_size <file> |
| 219 # Performs png file optimization. | 224 # Performs png file optimization. |
| 220 function optimize_size { | 225 function optimize_size { |
| 221 tput el | 226 # Print filename, trimmed to ensure it + status don't take more than 1 line |
| 227 local filename_length=${#file} |
| 228 local -i allowed_length=$COLUMNS-11 |
| 229 local -i trimmed_length=$filename_length-$COLUMNS+14 |
| 230 if [ "$filename_length" -lt "$allowed_length" ]; then |
| 231 info -n "$file|........" |
| 232 else |
| 233 info -n "...${file:$trimmed_length}|........" |
| 234 fi |
| 235 |
| 222 local file=$1 | 236 local file=$1 |
| 223 info -n "$file " | |
| 224 | 237 |
| 225 advdef -q -z -4 $file | 238 advdef -q -z -4 $file |
| 226 | 239 |
| 227 pngout -q -s4 -c0 -force $file $file.tmp.png | 240 pngout -q -s4 -c0 -force $file $file.tmp.png |
| 228 if [ -f $file.tmp.png ]; then | 241 if [ -f $file.tmp.png ]; then |
| 229 rm $file.tmp.png | 242 rm $file.tmp.png |
| 230 process_grayscale $file | 243 process_grayscale $file |
| 231 process_grayscale_alpha $file | 244 process_grayscale_alpha $file |
| 232 else | 245 else |
| 233 pngout -q -s4 -c4 -force $file $file.tmp.png | 246 pngout -q -s4 -c4 -force $file $file.tmp.png |
| 234 if [ -f $file.tmp.png ]; then | 247 if [ -f $file.tmp.png ]; then |
| 235 rm $file.tmp.png | 248 rm $file.tmp.png |
| 236 process_grayscale_alpha $file | 249 process_grayscale_alpha $file |
| 237 else | 250 else |
| 238 process_rgb $file | 251 process_rgb $file |
| 239 fi | 252 fi |
| 240 fi | 253 fi |
| 241 | 254 |
| 242 info -n "|filter" | 255 info -ne "\b\b\b\b\b\b\b\bfilter.." |
| 243 local old_color_type=$(get_color_type $file) | 256 local old_color_type=$(get_color_type $file) |
| 244 optipng -q -zc9 -zm8 -zs0-3 -f0-5 $file -out $file.tmp.png | 257 optipng -q -zc9 -zm8 -zs0-3 -f0-5 $file -out $file.tmp.png |
| 245 local new_color_type=$(get_color_type $file.tmp.png) | 258 local new_color_type=$(get_color_type $file.tmp.png) |
| 246 # optipng may corrupt a png file when reducing the color type | 259 # optipng may corrupt a png file when reducing the color type |
| 247 # to grayscale/grayscale+alpha. Just skip such cases until | 260 # to grayscale/grayscale+alpha. Just skip such cases until |
| 248 # the bug is fixed. See crbug.com/174505, crbug.com/174084. | 261 # the bug is fixed. See crbug.com/174505, crbug.com/174084. |
| 249 # The issue is reported in | 262 # The issue is reported in |
| 250 # https://sourceforge.net/tracker/?func=detail&aid=3603630&group_id=151404&ati
d=780913 | 263 # https://sourceforge.net/tracker/?func=detail&aid=3603630&group_id=151404&ati
d=780913 |
| 251 if [[ $old_color_type == "RGBA" && $new_color_type =~ gray.* ]] ; then | 264 if [[ $old_color_type == "RGBA" && $new_color_type == gray* ]] ; then |
| 252 rm $file.tmp.png | 265 rm $file.tmp.png |
| 253 info -n "[skip opting]" | |
| 254 else | 266 else |
| 255 mv $file.tmp.png $file | 267 mv $file.tmp.png $file |
| 256 fi | 268 fi |
| 257 pngout -q -k1 -s1 $file | 269 pngout -q -k1 -s1 $file |
| 258 | 270 |
| 259 huffman_blocks $file | 271 huffman_blocks $file |
| 260 | 272 |
| 261 # TODO(oshima): Experiment with strategy 1. | 273 # TODO(oshima): Experiment with strategy 1. |
| 262 info -n "|strategy" | 274 info -ne "\b\b\b\b\b\b\b\bstrategy" |
| 263 if [ $OPTIMIZE_LEVEL == 2 ]; then | 275 if [ $OPTIMIZE_LEVEL == 2 ]; then |
| 264 for i in 3 2 0; do | 276 for i in 3 2 0; do |
| 265 pngout -q -k1 -ks -s$i $file | 277 pngout -q -k1 -ks -s$i $file |
| 266 done | 278 done |
| 267 else | 279 else |
| 268 pngout -q -k1 -ks -s1 $file | 280 pngout -q -k1 -ks -s1 $file |
| 269 fi | 281 fi |
| 270 | 282 |
| 271 if [ $OPTIMIZE_LEVEL == 2 ]; then | 283 if [ $OPTIMIZE_LEVEL == 2 ]; then |
| 272 random_huffman_table_trial $file | 284 random_huffman_table_trial $file |
| 273 fi | 285 fi |
| 274 | 286 |
| 275 final_compression $file | 287 final_compression $file |
| 276 } | 288 } |
| 277 | 289 |
| 278 # Usage: process_file <file> | 290 # Usage: process_file <file> |
| 279 function process_file { | 291 function process_file { |
| 280 local file=$1 | 292 local file=$1 |
| 281 local name=$(basename $file) | 293 local name=$(basename $file) |
| 282 # -rem alla removes all ancillary chunks except for tRNS | 294 # -rem alla removes all ancillary chunks except for tRNS |
| 283 pngcrush -d $TMP_DIR -brute -reduce -rem alla $file > /dev/null | 295 pngcrush -d $TMP_DIR -brute -reduce -rem alla $file > /dev/null 2>&1 |
| 284 | 296 |
| 285 if [ -f $TMP_DIR/$name -a $OPTIMIZE_LEVEL != 0 ]; then | 297 if [ -f $TMP_DIR/$name -a $OPTIMIZE_LEVEL != 0 ]; then |
| 286 optimize_size $TMP_DIR/$name | 298 optimize_size $TMP_DIR/$name |
| 287 fi | 299 fi |
| 288 } | 300 } |
| 289 | 301 |
| 290 # Usage: optimize_file <file> | 302 # Usage: optimize_file <file> |
| 291 function optimize_file { | 303 function optimize_file { |
| 292 local file=$1 | 304 local file=$1 |
| 293 if $using_cygwin ; then | 305 if $using_cygwin ; then |
| 294 file=$(cygpath -w $file) | 306 file=$(cygpath -w $file) |
| 295 fi | 307 fi |
| 296 | 308 |
| 297 local name=$(basename $file) | 309 local name=$(basename $file) |
| 298 local old=$(stat -c%s $file) | 310 local old=$(stat -c%s $file) |
| 299 local tmp_file=$TMP_DIR/$name | 311 local tmp_file=$TMP_DIR/$name |
| 300 let TOTAL_FILE+=1 | 312 let TOTAL_FILE+=1 |
| 301 | 313 |
| 302 process_file $file | 314 process_file $file |
| 303 | 315 |
| 304 if [ ! -e $tmp_file ] ; then | 316 if [ ! -e $tmp_file ] ; then |
| 305 let CORRUPTED_FILE+=1 | 317 let CORRUPTED_FILE+=1 |
| 306 echo "The png file ($file) may be corrupted. skipping" | 318 echo "$file may be corrupted; skipping\n" |
| 307 return | 319 return |
| 308 fi | 320 fi |
| 309 | 321 |
| 310 local new=$(stat -c%s $tmp_file) | 322 local new=$(stat -c%s $tmp_file) |
| 311 let diff=$old-$new | 323 let diff=$old-$new |
| 312 let percent=($diff*100)/$old | 324 let percent=$diff*100 |
| 325 let percent=$percent/$old |
| 313 | 326 |
| 314 tput el | |
| 315 if [ $new -lt $old ]; then | 327 if [ $new -lt $old ]; then |
| 316 echo -ne "$file : $old => $new ($diff bytes : $percent %)\n" | 328 info "$file: $old => $new ($diff bytes: $percent%)" |
| 317 mv "$tmp_file" "$file" | 329 cp "$tmp_file" "$file" |
| 318 let TOTAL_OLD_BYTES+=$old | 330 let TOTAL_OLD_BYTES+=$old |
| 319 let TOTAL_NEW_BYTES+=$new | 331 let TOTAL_NEW_BYTES+=$new |
| 320 let PROCESSED_FILE+=1 | 332 let PROCESSED_FILE+=1 |
| 321 else | 333 else |
| 322 if [ $OPTIMIZE_LEVEL == 0 ]; then | 334 if [ $OPTIMIZE_LEVEL == 0 ]; then |
| 323 info -ne "$file : skipped\r" | 335 info "$file: Skipped" |
| 336 else |
| 337 info "$file: Unable to reduce size" |
| 324 fi | 338 fi |
| 325 rm $tmp_file | 339 rm $tmp_file |
| 326 fi | 340 fi |
| 327 } | 341 } |
| 328 | 342 |
| 329 function optimize_dir { | 343 function optimize_dir { |
| 330 local dir=$1 | 344 local dir=$1 |
| 331 if $using_cygwin ; then | 345 if $using_cygwin ; then |
| 332 dir=$(cygpath -w $dir) | 346 dir=$(cygpath -w $dir) |
| 333 fi | 347 fi |
| (...skipping 59 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 393 echo "$0 must be run in src directory" | 407 echo "$0 must be run in src directory" |
| 394 exit 1 | 408 exit 1 |
| 395 fi | 409 fi |
| 396 | 410 |
| 397 if [ "$(expr substr $(uname -s) 1 6)" == "CYGWIN" ]; then | 411 if [ "$(expr substr $(uname -s) 1 6)" == "CYGWIN" ]; then |
| 398 using_cygwin=true | 412 using_cygwin=true |
| 399 else | 413 else |
| 400 using_cygwin=false | 414 using_cygwin=false |
| 401 fi | 415 fi |
| 402 | 416 |
| 417 # The -i in the shebang line should result in $COLUMNS being set on newer |
| 418 # versions of bash. If it's not set yet, attempt to set it. |
| 419 if [ -z $COLUMNS ]; then |
| 420 which tput > /dev/null 2>&1 |
| 421 if [ "$?" == "0" ]; then |
| 422 COLUMNS=$(tput cols) |
| 423 else |
| 424 # No tput either... give up and just guess 80 columns. |
| 425 COLUMNS=80 |
| 426 fi |
| 427 export COLUMNS |
| 428 fi |
| 429 |
| 403 OPTIMIZE_LEVEL=1 | 430 OPTIMIZE_LEVEL=1 |
| 404 # Parse options | 431 # Parse options |
| 405 while getopts o:r:h:v opts | 432 while getopts o:r:h:v opts |
| 406 do | 433 do |
| 407 case $opts in | 434 case $opts in |
| 408 r) | 435 r) |
| 409 COMMIT=$(git svn find-rev r$OPTARG | tail -1) || exit | 436 COMMIT=$(git svn find-rev r$OPTARG | tail -1) || exit |
| 410 if [ -z "$COMMIT" ] ; then | 437 if [ -z "$COMMIT" ] ; then |
| 411 echo "Revision $OPTARG not found" | 438 echo "Revision $OPTARG not found" |
| 412 show_help | 439 show_help |
| 413 fi | 440 fi |
| 414 ;; | 441 ;; |
| 415 o) | 442 o) |
| 416 if [[ ! "$OPTARG" =~ [012] ]] ; then | 443 if [[ "$OPTARG" != 0 && "$OPTARG" != 1 && "$OPTARG" != 2 ]] ; then |
| 417 show_help | 444 show_help |
| 418 fi | 445 fi |
| 419 OPTIMIZE_LEVEL=$OPTARG | 446 OPTIMIZE_LEVEL=$OPTARG |
| 420 ;; | 447 ;; |
| 421 v) | 448 v) |
| 422 let VERBOSE=1 | 449 VERBOSE=true |
| 423 ;; | 450 ;; |
| 424 [h?]) | 451 [h?]) |
| 425 show_help;; | 452 show_help;; |
| 426 esac | 453 esac |
| 427 done | 454 done |
| 428 | 455 |
| 429 # Remove options from argument list. | 456 # Remove options from argument list. |
| 430 shift $(($OPTIND -1)) | 457 shift $(($OPTIND -1)) |
| 431 | 458 |
| 432 # Make sure we have all necessary commands installed. | 459 # Make sure we have all necessary commands installed. |
| (...skipping 15 matching lines...) Expand all Loading... |
| 448 fail_if_not_installed pngout $pngout_url | 475 fail_if_not_installed pngout $pngout_url |
| 449 fi | 476 fi |
| 450 | 477 |
| 451 # Create tmp directory for crushed png file. | 478 # Create tmp directory for crushed png file. |
| 452 TMP_DIR=$(mktemp -d) | 479 TMP_DIR=$(mktemp -d) |
| 453 if $using_cygwin ; then | 480 if $using_cygwin ; then |
| 454 TMP_DIR=$(cygpath -w $TMP_DIR) | 481 TMP_DIR=$(cygpath -w $TMP_DIR) |
| 455 fi | 482 fi |
| 456 | 483 |
| 457 # Make sure we cleanup temp dir | 484 # Make sure we cleanup temp dir |
| 458 trap "rm -rf $TMP_DIR" EXIT | 485 #trap "rm -rf $TMP_DIR" EXIT |
| 459 | 486 |
| 460 # If no directories are specified, optimize all directories. | 487 # If no directories are specified, optimize all directories. |
| 461 DIRS=$@ | 488 DIRS=$@ |
| 462 set ${DIRS:=$ALL_DIRS} | 489 set ${DIRS:=$ALL_DIRS} |
| 463 | 490 |
| 464 info "Optimize level=$OPTIMIZE_LEVEL" | 491 info "Optimize level=$OPTIMIZE_LEVEL" |
| 465 | 492 |
| 466 if [ -n "$COMMIT" ] ; then | 493 if [ -n "$COMMIT" ] ; then |
| 467 ALL_FILES=$(git diff --name-only $COMMIT HEAD $DIRS | grep "png$") | 494 ALL_FILES=$(git diff --name-only $COMMIT HEAD $DIRS | grep "png$") |
| 468 ALL_FILES_LIST=( $ALL_FILES ) | 495 ALL_FILES_LIST=( $ALL_FILES ) |
| (...skipping 13 matching lines...) Expand all Loading... |
| 482 info "" | 509 info "" |
| 483 elif [ -f $d ] ; then | 510 elif [ -f $d ] ; then |
| 484 optimize_file $d | 511 optimize_file $d |
| 485 else | 512 else |
| 486 echo "Not a file or directory: $d"; | 513 echo "Not a file or directory: $d"; |
| 487 fi | 514 fi |
| 488 done | 515 done |
| 489 fi | 516 fi |
| 490 | 517 |
| 491 # Print the results. | 518 # Print the results. |
| 492 if [ $PROCESSED_FILE == 0 ]; then | 519 echo "Optimized $PROCESSED_FILE/$TOTAL_FILE files in" \ |
| 493 echo "Did not find any files (out of $TOTAL_FILE files)" \ | 520 "$(date -d "0 + $SECONDS sec" +%Ts)" |
| 494 "that could be optimized" \ | 521 if [ $PROCESSED_FILE != 0 ]; then |
| 495 "in $(date -u -d @$SECONDS +%T)s" | |
| 496 else | |
| 497 let diff=$TOTAL_OLD_BYTES-$TOTAL_NEW_BYTES | 522 let diff=$TOTAL_OLD_BYTES-$TOTAL_NEW_BYTES |
| 498 let percent=$diff*100/$TOTAL_OLD_BYTES | 523 let percent=$diff*100/$TOTAL_OLD_BYTES |
| 499 echo "Processed $PROCESSED_FILE files (out of $TOTAL_FILE files)" \ | 524 echo "Result: $TOTAL_OLD_BYTES => $TOTAL_NEW_BYTES bytes" \ |
| 500 "in $(date -u -d @$SECONDS +%T)s" | 525 "($diff bytes: $percent%)" |
| 501 echo "Result : $TOTAL_OLD_BYTES => $TOTAL_NEW_BYTES bytes" \ | |
| 502 "($diff bytes : $percent %)" | |
| 503 fi | 526 fi |
| 504 if [ $CORRUPTED_FILE != 0 ]; then | 527 if [ $CORRUPTED_FILE != 0 ]; then |
| 505 echo "Warning: corrupted files found: $CORRUPTED_FILE" | 528 echo "Warning: corrupted files found: $CORRUPTED_FILE" |
| 506 echo "Please contact the author of the CL that landed corrupted png files" | 529 echo "Please contact the author of the CL that landed corrupted png files" |
| 507 fi | 530 fi |
| OLD | NEW |