Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(1)

Side by Side Diff: tools/resources/optimize-png-files.sh

Issue 62873002: Skip corrupted png file (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Created 7 years, 1 month ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « no previous file | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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
msw 2013/11/06 21:44:12 Perhaps limit this to content/public/android/java/
oshima 2014/01/09 19:39:25 Done.
25 content/renderer/resources
26 content/shell/resources
27 remoting/resources
28 ui/resources
29 ui/webui/resources
msw 2013/11/06 21:44:12 perhaps limit this to ui/webui/resources/images?
oshima 2014/01/09 19:39:25 Done.
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
52 # Show throbber character at current cursor position. 57 # Show throbber character at current cursor position.
53 function throbber { 58 function throbber {
54 echo -ne "${THROBBER_STR[$THROBBER_COUNT]}\b" 59 echo -ne "${THROBBER_STR[$THROBBER_COUNT]}\b"
55 let THROBBER_COUNT=($THROBBER_COUNT+1)%4 60 let THROBBER_COUNT=($THROBBER_COUNT+1)%4
56 } 61 }
(...skipping 204 matching lines...) Expand 10 before | Expand all | Expand 10 after
261 final_compression $file 266 final_compression $file
262 } 267 }
263 268
264 # Usage: process_file <file> 269 # Usage: process_file <file>
265 function process_file { 270 function process_file {
266 local file=$1 271 local file=$1
267 local name=$(basename $file) 272 local name=$(basename $file)
268 # -rem alla removes all ancillary chunks except for tRNS 273 # -rem alla removes all ancillary chunks except for tRNS
269 pngcrush -d $TMP_DIR -brute -reduce -rem alla $file > /dev/null 274 pngcrush -d $TMP_DIR -brute -reduce -rem alla $file > /dev/null
270 275
271 if [ $OPTIMIZE_LEVEL != 0 ]; then 276 if [ -f $TMP_DIR/$name -a $OPTIMIZE_LEVEL != 0 ]; then
msw 2013/11/06 21:44:12 Wow, shell scripting is archaic... I need to look
272 optimize_size $TMP_DIR/$name 277 optimize_size $TMP_DIR/$name
273 fi 278 fi
274 } 279 }
275 280
276 # Usage: optimize_file <file> 281 # Usage: optimize_file <file>
277 function optimize_file { 282 function optimize_file {
278 local file=$1 283 local file=$1
279 if $using_cygwin ; then 284 if $using_cygwin ; then
280 file=$(cygpath -w $file) 285 file=$(cygpath -w $file)
281 fi 286 fi
282 287
283 local name=$(basename $file) 288 local name=$(basename $file)
284 local old=$(stat -c%s $file) 289 local old=$(stat -c%s $file)
285 local tmp_file=$TMP_DIR/$name 290 local tmp_file=$TMP_DIR/$name
291 let TOTAL_FILE+=1
286 292
287 process_file $file 293 process_file $file
288 294
295 if [ ! -e $tmp_file ] ; then
296 let CORRUPTED_FILE+=1
297 echo "The png file ($file) may be corrupted. skipping"
msw 2013/11/06 21:44:12 Interesting, have you encountered this case at all
oshima 2014/01/09 19:39:25 Yes, I had. I think this happened because someone
298 return
299 fi
300
289 local new=$(stat -c%s $tmp_file) 301 local new=$(stat -c%s $tmp_file)
290 let diff=$old-$new 302 let diff=$old-$new
291 let percent=($diff*100)/$old 303 let percent=($diff*100)/$old
292 let TOTAL_FILE+=1
293 304
294 tput el 305 tput el
295 if [ $new -lt $old ]; then 306 if [ $new -lt $old ]; then
296 echo -ne "$file : $old => $new ($diff bytes : $percent %)\n" 307 echo -ne "$file : $old => $new ($diff bytes : $percent %)\n"
297 mv "$tmp_file" "$file" 308 mv "$tmp_file" "$file"
298 let TOTAL_OLD_BYTES+=$old 309 let TOTAL_OLD_BYTES+=$old
299 let TOTAL_NEW_BYTES+=$new 310 let TOTAL_NEW_BYTES+=$new
300 let PROCESSED_FILE+=1 311 let PROCESSED_FILE+=1
301 else 312 else
302 if [ $OPTIMIZE_LEVEL == 0 ]; then 313 if [ $OPTIMIZE_LEVEL == 0 ]; then
(...skipping 152 matching lines...) Expand 10 before | Expand all | Expand 10 after
455 optimize_dir $d 466 optimize_dir $d
456 echo 467 echo
457 done 468 done
458 fi 469 fi
459 470
460 # Print the results. 471 # Print the results.
461 if [ $PROCESSED_FILE == 0 ]; then 472 if [ $PROCESSED_FILE == 0 ]; then
462 echo "Did not find any files (out of $TOTAL_FILE files)" \ 473 echo "Did not find any files (out of $TOTAL_FILE files)" \
463 "that could be optimized" \ 474 "that could be optimized" \
464 "in $(date -u -d @$SECONDS +%T)s" 475 "in $(date -u -d @$SECONDS +%T)s"
476 if [ $CORRUPTED_FILE != 0 ]; then
msw 2013/11/06 21:44:12 nit: just print this after everything else outside
oshima 2014/01/09 19:39:25 Done.
477 echo "Warning: corrupted files found: $CORRUPTED_FILE"
msw 2013/11/06 21:44:12 Would it help to print a list of these file names
oshima 2014/01/09 19:39:25 File names are printed in #297. I added "contact
478 fi
465 else 479 else
466 let diff=$TOTAL_OLD_BYTES-$TOTAL_NEW_BYTES 480 let diff=$TOTAL_OLD_BYTES-$TOTAL_NEW_BYTES
467 let percent=$diff*100/$TOTAL_OLD_BYTES 481 let percent=$diff*100/$TOTAL_OLD_BYTES
468 echo "Processed $PROCESSED_FILE files (out of $TOTAL_FILE files)" \ 482 echo "Processed $PROCESSED_FILE files (out of $TOTAL_FILE files)" \
469 "in $(date -u -d @$SECONDS +%T)s" 483 "in $(date -u -d @$SECONDS +%T)s"
484 if [ $CORRUPTED_FILE != 0 ]; then
485 echo "Warning: corrupted files found: $CORRUPTED_FILE"
486 fi
470 echo "Result : $TOTAL_OLD_BYTES => $TOTAL_NEW_BYTES bytes" \ 487 echo "Result : $TOTAL_OLD_BYTES => $TOTAL_NEW_BYTES bytes" \
471 "($diff bytes : $percent %)" 488 "($diff bytes : $percent %)"
472 fi 489 fi
OLDNEW
« no previous file with comments | « no previous file | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698