| 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. |
| (...skipping 36 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 SUBPROCESS=0 | 57 VERBOSE=0 |
| 58 | 58 |
| 59 # NoOp in subprocess, echo otherwise. | 59 # Echo only if verbose option is set. |
| 60 function info { | 60 function info { |
| 61 if [ $SUBPROCESS -eq 0 ]; then | 61 if [ $VERBOSE -eq 1 ]; 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)%4 |
| 70 } | 70 } |
| 71 | 71 |
| (...skipping 305 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 377 0 Just run pngcrush. It removes unnecessary chunks and perform basic | 377 0 Just run pngcrush. It removes unnecessary chunks and perform basic |
| 378 optimization on the encoded data. | 378 optimization on the encoded data. |
| 379 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 |
| 380 reduce addtional 5~30%. This is the default level. | 380 reduce addtional 5~30%. This is the default level. |
| 381 2 Aggressively optimize the size of png files. This may produce | 381 2 Aggressively optimize the size of png files. This may produce |
| 382 addtional 1%~5% reduction. Warning: this is *VERY* | 382 addtional 1%~5% reduction. Warning: this is *VERY* |
| 383 slow and can take hours to process all files. | 383 slow and can take hours to process all files. |
| 384 -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 |
| 385 changed since this revision. The <dir> options will be used | 385 changed since this revision. The <dir> options will be used |
| 386 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. | 387 -v Shows optimization process for each file. |
| 388 Usage: find <dir> -name \"*.png\" | xargs -n1 -P16 $program -s -o2 | |
| 389 -h Print this help text." | 388 -h Print this help text." |
| 390 exit 1 | 389 exit 1 |
| 391 } | 390 } |
| 392 | 391 |
| 393 if [ ! -e ../.gclient ]; then | 392 if [ ! -e ../.gclient ]; then |
| 394 echo "$0 must be run in src directory" | 393 echo "$0 must be run in src directory" |
| 395 exit 1 | 394 exit 1 |
| 396 fi | 395 fi |
| 397 | 396 |
| 398 if [ "$(expr substr $(uname -s) 1 6)" == "CYGWIN" ]; then | 397 if [ "$(expr substr $(uname -s) 1 6)" == "CYGWIN" ]; then |
| 399 using_cygwin=true | 398 using_cygwin=true |
| 400 else | 399 else |
| 401 using_cygwin=false | 400 using_cygwin=false |
| 402 fi | 401 fi |
| 403 | 402 |
| 404 OPTIMIZE_LEVEL=1 | 403 OPTIMIZE_LEVEL=1 |
| 405 # Parse options | 404 # Parse options |
| 406 while getopts o:r:h:s opts | 405 while getopts o:r:h:v opts |
| 407 do | 406 do |
| 408 case $opts in | 407 case $opts in |
| 409 r) | 408 r) |
| 410 COMMIT=$(git svn find-rev r$OPTARG | tail -1) || exit | 409 COMMIT=$(git svn find-rev r$OPTARG | tail -1) || exit |
| 411 if [ -z "$COMMIT" ] ; then | 410 if [ -z "$COMMIT" ] ; then |
| 412 echo "Revision $OPTARG not found" | 411 echo "Revision $OPTARG not found" |
| 413 show_help | 412 show_help |
| 414 fi | 413 fi |
| 415 ;; | 414 ;; |
| 416 o) | 415 o) |
| 417 if [[ ! "$OPTARG" =~ [012] ]] ; then | 416 if [[ ! "$OPTARG" =~ [012] ]] ; then |
| 418 show_help | 417 show_help |
| 419 fi | 418 fi |
| 420 OPTIMIZE_LEVEL=$OPTARG | 419 OPTIMIZE_LEVEL=$OPTARG |
| 421 ;; | 420 ;; |
| 422 s) | 421 v) |
| 423 let SUBPROCESS=1 | 422 let VERBOSE=1 |
| 424 ;; | 423 ;; |
| 425 [h?]) | 424 [h?]) |
| 426 show_help;; | 425 show_help;; |
| 427 esac | 426 esac |
| 428 done | 427 done |
| 429 | 428 |
| 430 # Remove options from argument list. | 429 # Remove options from argument list. |
| 431 shift $(($OPTIND -1)) | 430 shift $(($OPTIND -1)) |
| 432 | 431 |
| 433 # Make sure we have all necessary commands installed. | 432 # Make sure we have all necessary commands installed. |
| (...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 472 if [ -f $f ] ; then | 471 if [ -f $f ] ; then |
| 473 optimize_file $f | 472 optimize_file $f |
| 474 else | 473 else |
| 475 echo "Skipping deleted file: $f"; | 474 echo "Skipping deleted file: $f"; |
| 476 fi | 475 fi |
| 477 done | 476 done |
| 478 else | 477 else |
| 479 for d in $DIRS; do | 478 for d in $DIRS; do |
| 480 info "Optimizing png files in $d" | 479 info "Optimizing png files in $d" |
| 481 optimize_dir $d | 480 optimize_dir $d |
| 482 echo | 481 info "" |
| 483 done | 482 done |
| 484 fi | 483 fi |
| 485 | 484 |
| 486 # Print the results. | 485 # Print the results. |
| 487 if [ $PROCESSED_FILE == 0 ]; then | 486 if [ $PROCESSED_FILE == 0 ]; then |
| 488 echo "Did not find any files (out of $TOTAL_FILE files)" \ | 487 echo "Did not find any files (out of $TOTAL_FILE files)" \ |
| 489 "that could be optimized" \ | 488 "that could be optimized" \ |
| 490 "in $(date -u -d @$SECONDS +%T)s" | 489 "in $(date -u -d @$SECONDS +%T)s" |
| 491 else | 490 else |
| 492 let diff=$TOTAL_OLD_BYTES-$TOTAL_NEW_BYTES | 491 let diff=$TOTAL_OLD_BYTES-$TOTAL_NEW_BYTES |
| 493 let percent=$diff*100/$TOTAL_OLD_BYTES | 492 let percent=$diff*100/$TOTAL_OLD_BYTES |
| 494 echo "Processed $PROCESSED_FILE files (out of $TOTAL_FILE files)" \ | 493 echo "Processed $PROCESSED_FILE files (out of $TOTAL_FILE files)" \ |
| 495 "in $(date -u -d @$SECONDS +%T)s" | 494 "in $(date -u -d @$SECONDS +%T)s" |
| 496 echo "Result : $TOTAL_OLD_BYTES => $TOTAL_NEW_BYTES bytes" \ | 495 echo "Result : $TOTAL_OLD_BYTES => $TOTAL_NEW_BYTES bytes" \ |
| 497 "($diff bytes : $percent %)" | 496 "($diff bytes : $percent %)" |
| 498 fi | 497 fi |
| 499 if [ $CORRUPTED_FILE != 0 ]; then | 498 if [ $CORRUPTED_FILE != 0 ]; then |
| 500 echo "Warning: corrupted files found: $CORRUPTED_FILE" | 499 echo "Warning: corrupted files found: $CORRUPTED_FILE" |
| 501 echo "Please contact the author of the CL that landed corrupted png files" | 500 echo "Please contact the author of the CL that landed corrupted png files" |
| 502 fi | 501 fi |
| OLD | NEW |