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 287 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
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 sanitize_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 sanitize_file $f | 308 if $using_cygwin ; then |
| 309 sanitize_file $(cygpath -w $f) |
| 310 else |
| 311 sanitize_file $f |
| 312 fi |
309 done | 313 done |
310 } | 314 } |
311 | 315 |
312 function install_if_not_installed { | 316 function install_if_not_installed { |
313 local program=$1 | 317 local program=$1 |
314 dpkg -s $program > /dev/null 2>&1 | 318 local package=$2 |
| 319 which $program > /dev/null 2>&1 |
315 if [ "$?" != "0" ]; then | 320 if [ "$?" != "0" ]; then |
316 read -p "Couldn't find $program. Do you want to install? (y/n)" | 321 if $using_cygwin ; then |
317 [ "$REPLY" == "y" ] && sudo apt-get install $program | 322 echo "Couldn't find $program. Please run setup.exe and install the $packa
ge package." |
318 [ "$REPLY" == "y" ] || exit | 323 exit 1 |
| 324 else |
| 325 read -p "Couldn't find $program. Do you want to install? (y/n)" |
| 326 [ "$REPLY" == "y" ] && sudo apt-get install $package |
| 327 [ "$REPLY" == "y" ] || exit |
| 328 fi |
319 fi | 329 fi |
320 } | 330 } |
321 | 331 |
322 function fail_if_not_installed { | 332 function fail_if_not_installed { |
323 local program=$1 | 333 local program=$1 |
324 local url=$2 | 334 local url=$2 |
325 which $program > /dev/null | 335 which $program > /dev/null 2>&1 |
326 if [ $? != 0 ]; then | 336 if [ $? != 0 ]; then |
327 echo "Couldn't find $program. Please download and install it from $url" | 337 echo "Couldn't find $program. Please download and install it from $url ." |
328 exit 1 | 338 exit 1 |
329 fi | 339 fi |
330 } | 340 } |
331 | 341 |
332 function show_help { | 342 function show_help { |
333 local program=$(basename $0) | 343 local program=$(basename $0) |
334 echo \ | 344 echo \ |
335 "Usage: $program [options] dir ... | 345 "Usage: $program [options] dir ... |
336 | 346 |
337 $program is a utility to reduce the size of png files by removing | 347 $program is a utility to reduce the size of png files by removing |
(...skipping 10 matching lines...) Expand all Loading... |
348 slow and can take hours to process all files. | 358 slow and can take hours to process all files. |
349 -h Print this help text." | 359 -h Print this help text." |
350 exit 1 | 360 exit 1 |
351 } | 361 } |
352 | 362 |
353 if [ ! -e ../.gclient ]; then | 363 if [ ! -e ../.gclient ]; then |
354 echo "$0 must be run in src directory" | 364 echo "$0 must be run in src directory" |
355 exit 1 | 365 exit 1 |
356 fi | 366 fi |
357 | 367 |
| 368 if [ "$(expr substr $(uname -s) 1 6)" == "CYGWIN" ]; then |
| 369 using_cygwin=true |
| 370 else |
| 371 using_cygwin=false |
| 372 fi |
| 373 |
358 OPTIMIZE_LEVEL=1 | 374 OPTIMIZE_LEVEL=1 |
359 # Parse options | 375 # Parse options |
360 while getopts o:h opts | 376 while getopts o:h opts |
361 do | 377 do |
362 case $opts in | 378 case $opts in |
363 o) | 379 o) |
364 if [[ ! "$OPTARG" =~ [012] ]]; then | 380 if [[ ! "$OPTARG" =~ [012] ]]; then |
365 show_help | 381 show_help |
366 fi | 382 fi |
367 OPTIMIZE_LEVEL=$OPTARG | 383 OPTIMIZE_LEVEL=$OPTARG |
368 [ "$1" == "-o" ] && shift | 384 [ "$1" == "-o" ] && shift |
369 shift;; | 385 shift;; |
370 [h?]) | 386 [h?]) |
371 show_help;; | 387 show_help;; |
372 esac | 388 esac |
373 done | 389 done |
374 | 390 |
375 # Make sure we have all necessary commands installed. | 391 # Make sure we have all necessary commands installed. |
376 install_if_not_installed pngcrush | 392 install_if_not_installed pngcrush pngcrush |
377 if [ $OPTIMIZE_LEVEL == 2 ]; then | 393 if [ $OPTIMIZE_LEVEL == 2 ]; then |
378 install_if_not_installed optipng | 394 install_if_not_installed optipng optipng |
379 | 395 |
380 install_if_not_installed advancecomp | 396 if $using_cygwin ; then |
381 fail_if_not_installed advdef "http://advancemame.sourceforge.net/comp-download
.html" | 397 fail_if_not_installed advdef "http://advancemame.sourceforge.net/comp-readme
.html" |
| 398 else |
| 399 install_if_not_installed advdef advancecomp |
| 400 fi |
382 | 401 |
383 fail_if_not_installed pngout "http://www.jonof.id.au/kenutils" | 402 if $using_cygwin ; then |
| 403 pngout_url="http://www.advsys.net/ken/utils.htm" |
| 404 else |
| 405 pngout_url="http://www.jonof.id.au/kenutils" |
| 406 fi |
| 407 fail_if_not_installed pngout $pngout_url |
384 fi | 408 fi |
385 | 409 |
386 # Create tmp directory for crushed png file. | 410 # Create tmp directory for crushed png file. |
387 TMP_DIR=$(mktemp -d) | 411 TMP_DIR=$(mktemp -d) |
| 412 if $using_cygwin ; then |
| 413 TMP_DIR=$(cygpath -w $TMP_DIR) |
| 414 fi |
388 | 415 |
389 # Make sure we cleanup temp dir | 416 # Make sure we cleanup temp dir |
390 trap "rm -rf $TMP_DIR" EXIT | 417 trap "rm -rf $TMP_DIR" EXIT |
391 | 418 |
392 # If no directories are specified, sanitize all directories. | 419 # If no directories are specified, sanitize all directories. |
393 DIRS=$@ | 420 DIRS=$@ |
394 set ${DIRS:=$ALL_DIRS} | 421 set ${DIRS:=$ALL_DIRS} |
395 | 422 |
396 echo "Optimize level=$OPTIMIZE_LEVEL" | 423 echo "Optimize level=$OPTIMIZE_LEVEL" |
397 for d in $DIRS; do | 424 for d in $DIRS; do |
| 425 if $using_cygwin ; then |
| 426 d=$(cygpath -w $d) |
| 427 fi |
398 echo "Sanitizing png files in $d" | 428 echo "Sanitizing png files in $d" |
399 sanitize_dir $d | 429 sanitize_dir $d |
400 echo | 430 echo |
401 done | 431 done |
402 | 432 |
403 # Print the results. | 433 # Print the results. |
404 if [ $PROCESSED_FILE == 0 ]; then | 434 if [ $PROCESSED_FILE == 0 ]; then |
405 echo "Did not find any files (out of $TOTAL_FILE files)" \ | 435 echo "Did not find any files (out of $TOTAL_FILE files)" \ |
406 "that could be optimized" \ | 436 "that could be optimized" \ |
407 "in $(date -u -d @$SECONDS +%T)s" | 437 "in $(date -u -d @$SECONDS +%T)s" |
408 else | 438 else |
409 let diff=$TOTAL_OLD_BYTES-$TOTAL_NEW_BYTES | 439 let diff=$TOTAL_OLD_BYTES-$TOTAL_NEW_BYTES |
410 let percent=$diff*100/$TOTAL_OLD_BYTES | 440 let percent=$diff*100/$TOTAL_OLD_BYTES |
411 echo "Processed $PROCESSED_FILE files (out of $TOTAL_FILE files)" \ | 441 echo "Processed $PROCESSED_FILE files (out of $TOTAL_FILE files)" \ |
412 "in $(date -u -d @$SECONDS +%T)s" | 442 "in $(date -u -d @$SECONDS +%T)s" |
413 echo "Result : $TOTAL_OLD_BYTES => $TOTAL_NEW_BYTES bytes" \ | 443 echo "Result : $TOTAL_OLD_BYTES => $TOTAL_NEW_BYTES bytes" \ |
414 "($diff bytes : $percent %)" | 444 "($diff bytes : $percent %)" |
415 fi | 445 fi |
OLD | NEW |