| 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. |
| (...skipping 255 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 266 local file=$1 | 266 local file=$1 |
| 267 local name=$(basename $file) | 267 local name=$(basename $file) |
| 268 # -rem alla removes all ancillary chunks except for tRNS | 268 # -rem alla removes all ancillary chunks except for tRNS |
| 269 pngcrush -d $TMP_DIR -brute -reduce -rem alla $file > /dev/null | 269 pngcrush -d $TMP_DIR -brute -reduce -rem alla $file > /dev/null |
| 270 | 270 |
| 271 if [ $OPTIMIZE_LEVEL != 0 ]; then | 271 if [ $OPTIMIZE_LEVEL != 0 ]; then |
| 272 optimize_size $TMP_DIR/$name | 272 optimize_size $TMP_DIR/$name |
| 273 fi | 273 fi |
| 274 } | 274 } |
| 275 | 275 |
| 276 # Usage: sanitize_file <file> | 276 # Usage: optimize_file <file> |
| 277 function sanitize_file { | 277 function optimize_file { |
| 278 local file=$1 | 278 local file=$1 |
| 279 local name=$(basename $file) | 279 local name=$(basename $file) |
| 280 local old=$(stat -c%s $file) | 280 local old=$(stat -c%s $file) |
| 281 local tmp_file=$TMP_DIR/$name | 281 local tmp_file=$TMP_DIR/$name |
| 282 | 282 |
| 283 process_file $file | 283 process_file $file |
| 284 | 284 |
| 285 local new=$(stat -c%s $tmp_file) | 285 local new=$(stat -c%s $tmp_file) |
| 286 let diff=$old-$new | 286 let diff=$old-$new |
| 287 let percent=($diff*100)/$old | 287 let percent=($diff*100)/$old |
| 288 let TOTAL_FILE+=1 | 288 let TOTAL_FILE+=1 |
| 289 | 289 |
| 290 tput el | 290 tput el |
| 291 if [ $new -lt $old ]; then | 291 if [ $new -lt $old ]; then |
| 292 echo -ne "$file : $old => $new ($diff bytes : $percent %)\n" | 292 echo -ne "$file : $old => $new ($diff bytes : $percent %)\n" |
| 293 mv "$tmp_file" "$file" | 293 mv "$tmp_file" "$file" |
| 294 let TOTAL_OLD_BYTES+=$old | 294 let TOTAL_OLD_BYTES+=$old |
| 295 let TOTAL_NEW_BYTES+=$new | 295 let TOTAL_NEW_BYTES+=$new |
| 296 let PROCESSED_FILE+=1 | 296 let PROCESSED_FILE+=1 |
| 297 else | 297 else |
| 298 if [ $OPTIMIZE_LEVEL == 0 ]; then | 298 if [ $OPTIMIZE_LEVEL == 0 ]; then |
| 299 echo -ne "$file : skipped\r" | 299 echo -ne "$file : skipped\r" |
| 300 fi | 300 fi |
| 301 rm $tmp_file | 301 rm $tmp_file |
| 302 fi | 302 fi |
| 303 } | 303 } |
| 304 | 304 |
| 305 function sanitize_dir { | 305 function optimize_dir { |
| 306 local dir=$1 | 306 local dir=$1 |
| 307 for f in $(find $dir -name "*.png"); do | 307 for f in $(find $dir -name "*.png"); do |
| 308 if $using_cygwin ; then | 308 if $using_cygwin ; then |
| 309 sanitize_file $(cygpath -w $f) | 309 optimize_file $(cygpath -w $f) |
| 310 else | 310 else |
| 311 sanitize_file $f | 311 optimize_file $f |
| 312 fi | 312 fi |
| 313 done | 313 done |
| 314 } | 314 } |
| 315 | 315 |
| 316 function install_if_not_installed { | 316 function install_if_not_installed { |
| 317 local program=$1 | 317 local program=$1 |
| 318 local package=$2 | 318 local package=$2 |
| 319 which $program > /dev/null 2>&1 | 319 which $program > /dev/null 2>&1 |
| 320 if [ "$?" != "0" ]; then | 320 if [ "$?" != "0" ]; then |
| 321 if $using_cygwin ; then | 321 if $using_cygwin ; then |
| 322 echo "Couldn't find $program. Please run setup.exe and install the $packa
ge package." | 322 echo "Couldn't find $program. " \ |
| 323 "Please run cygwin's setup.exe and install the $package package." |
| 323 exit 1 | 324 exit 1 |
| 324 else | 325 else |
| 325 read -p "Couldn't find $program. Do you want to install? (y/n)" | 326 read -p "Couldn't find $program. Do you want to install? (y/n)" |
| 326 [ "$REPLY" == "y" ] && sudo apt-get install $package | 327 [ "$REPLY" == "y" ] && sudo apt-get install $package |
| 327 [ "$REPLY" == "y" ] || exit | 328 [ "$REPLY" == "y" ] || exit |
| 328 fi | 329 fi |
| 329 fi | 330 fi |
| 330 } | 331 } |
| 331 | 332 |
| 332 function fail_if_not_installed { | 333 function fail_if_not_installed { |
| (...skipping 76 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 409 | 410 |
| 410 # Create tmp directory for crushed png file. | 411 # Create tmp directory for crushed png file. |
| 411 TMP_DIR=$(mktemp -d) | 412 TMP_DIR=$(mktemp -d) |
| 412 if $using_cygwin ; then | 413 if $using_cygwin ; then |
| 413 TMP_DIR=$(cygpath -w $TMP_DIR) | 414 TMP_DIR=$(cygpath -w $TMP_DIR) |
| 414 fi | 415 fi |
| 415 | 416 |
| 416 # Make sure we cleanup temp dir | 417 # Make sure we cleanup temp dir |
| 417 trap "rm -rf $TMP_DIR" EXIT | 418 trap "rm -rf $TMP_DIR" EXIT |
| 418 | 419 |
| 419 # If no directories are specified, sanitize all directories. | 420 # If no directories are specified, optimize all directories. |
| 420 DIRS=$@ | 421 DIRS=$@ |
| 421 set ${DIRS:=$ALL_DIRS} | 422 set ${DIRS:=$ALL_DIRS} |
| 422 | 423 |
| 423 echo "Optimize level=$OPTIMIZE_LEVEL" | 424 echo "Optimize level=$OPTIMIZE_LEVEL" |
| 424 for d in $DIRS; do | 425 for d in $DIRS; do |
| 425 if $using_cygwin ; then | 426 if $using_cygwin ; then |
| 426 d=$(cygpath -w $d) | 427 d=$(cygpath -w $d) |
| 427 fi | 428 fi |
| 428 echo "Sanitizing png files in $d" | 429 echo "Optimizing png files in $d" |
| 429 sanitize_dir $d | 430 optimize_dir $d |
| 430 echo | 431 echo |
| 431 done | 432 done |
| 432 | 433 |
| 433 # Print the results. | 434 # Print the results. |
| 434 if [ $PROCESSED_FILE == 0 ]; then | 435 if [ $PROCESSED_FILE == 0 ]; then |
| 435 echo "Did not find any files (out of $TOTAL_FILE files)" \ | 436 echo "Did not find any files (out of $TOTAL_FILE files)" \ |
| 436 "that could be optimized" \ | 437 "that could be optimized" \ |
| 437 "in $(date -u -d @$SECONDS +%T)s" | 438 "in $(date -u -d @$SECONDS +%T)s" |
| 438 else | 439 else |
| 439 let diff=$TOTAL_OLD_BYTES-$TOTAL_NEW_BYTES | 440 let diff=$TOTAL_OLD_BYTES-$TOTAL_NEW_BYTES |
| 440 let percent=$diff*100/$TOTAL_OLD_BYTES | 441 let percent=$diff*100/$TOTAL_OLD_BYTES |
| 441 echo "Processed $PROCESSED_FILE files (out of $TOTAL_FILE files)" \ | 442 echo "Processed $PROCESSED_FILE files (out of $TOTAL_FILE files)" \ |
| 442 "in $(date -u -d @$SECONDS +%T)s" | 443 "in $(date -u -d @$SECONDS +%T)s" |
| 443 echo "Result : $TOTAL_OLD_BYTES => $TOTAL_NEW_BYTES bytes" \ | 444 echo "Result : $TOTAL_OLD_BYTES => $TOTAL_NEW_BYTES bytes" \ |
| 444 "($diff bytes : $percent %)" | 445 "($diff bytes : $percent %)" |
| 445 fi | 446 fi |
| OLD | NEW |