Upstream version 9.38.198.0
[platform/framework/web/crosswalk.git] / src / tools / resources / optimize-png-files.sh
1 #!/bin/bash -i
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
4 # found in the LICENSE file.
5
6 # The optimization code is based on pngslim (http://goo.gl/a0XHg)
7 # and executes a similar pipleline to optimize the png file size.
8 # The steps that require pngoptimizercl/pngrewrite/deflopt are omitted,
9 # but this runs all other processes, including:
10 # 1) various color-dependent optimizations using optipng.
11 # 2) optimize the number of huffman blocks.
12 # 3) randomize the huffman table.
13 # 4) Further optimize using optipng and advdef (zlib stream).
14 # Due to the step 3), each run may produce slightly different results.
15 #
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.
18
19 readonly ALL_DIRS="
20 ash/resources
21 chrome/android/java/res
22 chrome/app/theme
23 chrome/browser/resources
24 chrome/renderer/resources
25 content/public/android/java/res
26 content/renderer/resources
27 content/shell/resources
28 remoting/resources
29 ui/resources
30 ui/chromeos/resources
31 ui/webui/resources/images
32 webkit/glue/resources
33 win8/metro_driver/resources
34 "
35
36 # Files larger than this file size (in bytes) will
37 # use the optimization parameters tailored for large files.
38 LARGE_FILE_THRESHOLD=3000
39
40 # Constants used for optimization
41 readonly DEFAULT_MIN_BLOCK_SIZE=128
42 readonly DEFAULT_LIMIT_BLOCKS=256
43 readonly DEFAULT_RANDOM_TRIALS=100
44 # Taken from the recommendation in the pngslim's readme.txt.
45 readonly LARGE_MIN_BLOCK_SIZE=1
46 readonly LARGE_LIMIT_BLOCKS=2
47 readonly LARGE_RANDOM_TRIALS=1
48
49 # Global variables for stats
50 TOTAL_OLD_BYTES=0
51 TOTAL_NEW_BYTES=0
52 TOTAL_FILE=0
53 CORRUPTED_FILE=0
54 PROCESSED_FILE=0
55
56 declare -a THROBBER_STR=('-' '\\' '|' '/')
57 THROBBER_COUNT=0
58
59 VERBOSE=false
60
61 # Echo only if verbose option is set.
62 function info {
63   if $VERBOSE ; then
64     echo $@
65   fi
66 }
67
68 # Show throbber character at current cursor position.
69 function throbber {
70   info -ne "${THROBBER_STR[$THROBBER_COUNT]}\b"
71   let THROBBER_COUNT=$THROBBER_COUNT+1
72   let THROBBER_COUNT=$THROBBER_COUNT%4
73 }
74
75 # Usage: pngout_loop <file> <png_out_options> ...
76 # Optimize the png file using pngout with the given options
77 # using various block split thresholds and filter types.
78 function pngout_loop {
79   local file=$1
80   shift
81   local opts=$*
82   if [ $OPTIMIZE_LEVEL == 1 ]; then
83     for j in $(eval echo {0..5}); do
84       throbber
85       pngout -q -k1 -s1 -f$j $opts $file
86     done
87   else
88     for i in 0 128 256 512; do
89       for j in $(eval echo {0..5}); do
90         throbber
91         pngout -q -k1 -s1 -b$i -f$j $opts $file
92       done
93     done
94   fi
95 }
96
97 # Usage: get_color_depth_list
98 # Returns the list of color depth options for current optimization level.
99 function get_color_depth_list {
100   if [ $OPTIMIZE_LEVEL == 1 ]; then
101     echo "-d0"
102   else
103     echo "-d1 -d2 -d4 -d8"
104   fi
105 }
106
107 # Usage: process_grayscale <file>
108 # Optimize grayscale images for all color bit depths.
109 #
110 # TODO(oshima): Experiment with -d0 w/o -c0.
111 function process_grayscale {
112   info -ne "\b\b\b\b\b\b\b\bgray...."
113   for opt in $(get_color_depth_list); do
114     pngout_loop $file -c0 $opt
115   done
116 }
117
118 # Usage: process_grayscale_alpha <file>
119 # Optimize grayscale images with alpha for all color bit depths.
120 function process_grayscale_alpha {
121   info -ne "\b\b\b\b\b\b\b\bgray-a.."
122   pngout_loop $file -c4
123   for opt in $(get_color_depth_list); do
124     pngout_loop $file -c3 $opt
125   done
126 }
127
128 # Usage: process_rgb <file>
129 # Optimize rgb images with or without alpha for all color bit depths.
130 function process_rgb {
131   info -ne "\b\b\b\b\b\b\b\brgb....."
132   for opt in $(get_color_depth_list); do
133     pngout_loop $file -c3 $opt
134   done
135   pngout_loop $file -c2
136   pngout_loop $file -c6
137 }
138
139 # Usage: huffman_blocks <file>
140 # Optimize the huffman blocks.
141 function huffman_blocks {
142   info -ne "\b\b\b\b\b\b\b\bhuffman."
143   local file=$1
144   local size=$(stat -c%s $file)
145   local min_block_size=$DEFAULT_MIN_BLOCK_SIZE
146   local limit_blocks=$DEFAULT_LIMIT_BLOCKS
147
148   if [ $size -gt $LARGE_FILE_THRESHOLD ]; then
149     min_block_size=$LARGE_MIN_BLOCK_SIZE
150     limit_blocks=$LARGE_LIMIT_BLOCKS
151   fi
152   let max_blocks=$size/$min_block_size
153   if [ $max_blocks -gt $limit_blocks ]; then
154     max_blocks=$limit_blocks
155   fi
156
157   for i in $(eval echo {2..$max_blocks}); do
158     throbber
159     pngout -q -k1 -ks -s1 -n$i $file
160   done
161 }
162
163 # Usage: random_huffman_table_trial <file>
164 # Try compressing by randomizing the initial huffman table.
165 #
166 # TODO(oshima): Try adjusting different parameters for large files to
167 # reduce runtime.
168 function random_huffman_table_trial {
169   info -ne "\b\b\b\b\b\b\b\brandom.."
170   local file=$1
171   local old_size=$(stat -c%s $file)
172   local trials_count=$DEFAULT_RANDOM_TRIALS
173
174   if [ $old_size -gt $LARGE_FILE_THRESHOLD ]; then
175     trials_count=$LARGE_RANDOM_TRIALS
176   fi
177   for i in $(eval echo {1..$trials_count}); do
178     throbber
179     pngout -q -k1 -ks -s0 -r $file
180   done
181   local new_size=$(stat -c%s $file)
182   if [ $new_size -lt $old_size ]; then
183     random_huffman_table_trial $file
184   fi
185 }
186
187 # Usage: final_comprssion <file>
188 # Further compress using optipng and advdef.
189 # TODO(oshima): Experiment with 256.
190 function final_compression {
191   info -ne "\b\b\b\b\b\b\b\bfinal..."
192   local file=$1
193   if [ $OPTIMIZE_LEVEL == 2 ]; then
194     for i in 32k 16k 8k 4k 2k 1k 512; do
195       throbber
196       optipng -q -nb -nc -zw$i -zc1-9 -zm1-9 -zs0-3 -f0-5 $file
197     done
198   fi
199   for i in $(eval echo {1..4}); do
200     throbber
201     advdef -q -z -$i $file
202   done
203
204   # Clear the current line.
205   if $VERBOSE ; then
206     printf "\033[0G\033[K"
207   fi
208 }
209
210 # Usage: get_color_type <file>
211 # Returns the color type name of the png file. Here is the list of names
212 # for each color type codes.
213 # 0: grayscale
214 # 2: RGB
215 # 3: colormap
216 # 4: gray+alpha
217 # 6: RGBA
218 # See http://en.wikipedia.org/wiki/Portable_Network_Graphics#Color_depth
219 # for details about the color type code.
220 function get_color_type {
221   local file=$1
222   echo $(file $file | awk -F, '{print $3}' | awk '{print $2}')
223 }
224
225 # Usage: optimize_size <file>
226 # Performs png file optimization.
227 function optimize_size {
228   # Print filename, trimmed to ensure it + status don't take more than 1 line
229   local filename_length=${#file}
230   local -i allowed_length=$COLUMNS-11
231   local -i trimmed_length=$filename_length-$COLUMNS+14
232   if [ "$filename_length" -lt "$allowed_length" ]; then
233     info -n "$file|........"
234   else
235     info -n "...${file:$trimmed_length}|........"
236   fi
237
238   local file=$1
239
240   advdef -q -z -4 $file
241
242   pngout -q -s4 -c0 -force $file $file.tmp.png
243   if [ -f $file.tmp.png ]; then
244     rm $file.tmp.png
245     process_grayscale $file
246     process_grayscale_alpha $file
247   else
248     pngout -q -s4 -c4 -force $file $file.tmp.png
249     if [ -f $file.tmp.png ]; then
250       rm $file.tmp.png
251       process_grayscale_alpha $file
252     else
253       process_rgb $file
254     fi
255   fi
256
257   info -ne "\b\b\b\b\b\b\b\bfilter.."
258   local old_color_type=$(get_color_type $file)
259   optipng -q -zc9 -zm8 -zs0-3 -f0-5 $file -out $file.tmp.png
260   local new_color_type=$(get_color_type $file.tmp.png)
261   # optipng may corrupt a png file when reducing the color type
262   # to grayscale/grayscale+alpha. Just skip such cases until
263   # the bug is fixed. See crbug.com/174505, crbug.com/174084.
264   # The issue is reported in
265   # https://sourceforge.net/tracker/?func=detail&aid=3603630&group_id=151404&atid=780913
266   if [[ $old_color_type == "RGBA" && $new_color_type == gray* ]] ; then
267     rm $file.tmp.png
268   else
269     mv $file.tmp.png $file
270   fi
271   pngout -q -k1 -s1 $file
272
273   huffman_blocks $file
274
275   # TODO(oshima): Experiment with strategy 1.
276   info -ne "\b\b\b\b\b\b\b\bstrategy"
277   if [ $OPTIMIZE_LEVEL == 2 ]; then
278     for i in 3 2 0; do
279       pngout -q -k1 -ks -s$i $file
280     done
281   else
282     pngout -q -k1 -ks -s1 $file
283   fi
284
285   if [ $OPTIMIZE_LEVEL == 2 ]; then
286     random_huffman_table_trial $file
287   fi
288
289   final_compression $file
290 }
291
292 # Usage: process_file <file>
293 function process_file {
294   local file=$1
295   local name=$(basename $file)
296   # -rem alla removes all ancillary chunks except for tRNS
297   pngcrush -d $TMP_DIR -brute -reduce -rem alla $file > /dev/null 2>&1
298
299   if [ -f $TMP_DIR/$name -a $OPTIMIZE_LEVEL != 0 ]; then
300     optimize_size $TMP_DIR/$name
301   fi
302 }
303
304 # Usage: optimize_file <file>
305 function optimize_file {
306   local file=$1
307   if $using_cygwin ; then
308     file=$(cygpath -w $file)
309   fi
310
311   local name=$(basename $file)
312   local old=$(stat -c%s $file)
313   local tmp_file=$TMP_DIR/$name
314   let TOTAL_FILE+=1
315
316   process_file $file
317
318   if [ ! -e $tmp_file ] ; then
319     let CORRUPTED_FILE+=1
320     echo "$file may be corrupted; skipping\n"
321     return
322   fi
323
324   local new=$(stat -c%s $tmp_file)
325   let diff=$old-$new
326   let percent=$diff*100
327   let percent=$percent/$old
328
329   if [ $new -lt $old ]; then
330     info "$file: $old => $new ($diff bytes: $percent%)"
331     cp "$tmp_file" "$file"
332     let TOTAL_OLD_BYTES+=$old
333     let TOTAL_NEW_BYTES+=$new
334     let PROCESSED_FILE+=1
335   else
336     if [ $OPTIMIZE_LEVEL == 0 ]; then
337       info "$file: Skipped"
338     else
339       info "$file: Unable to reduce size"
340     fi
341     rm $tmp_file
342   fi
343 }
344
345 function optimize_dir {
346   local dir=$1
347   if $using_cygwin ; then
348     dir=$(cygpath -w $dir)
349   fi
350
351   for f in $(find $dir -name "*.png"); do
352     optimize_file $f
353   done
354 }
355
356 function install_if_not_installed {
357   local program=$1
358   local package=$2
359   which $program > /dev/null 2>&1
360   if [ "$?" != "0" ]; then
361     if $using_cygwin ; then
362       echo "Couldn't find $program. " \
363            "Please run cygwin's setup.exe and install the $package package."
364       exit 1
365     else
366       read -p "Couldn't find $program. Do you want to install? (y/n)"
367       [ "$REPLY" == "y" ] && sudo apt-get install $package
368       [ "$REPLY" == "y" ] || exit
369     fi
370   fi
371 }
372
373 function fail_if_not_installed {
374   local program=$1
375   local url=$2
376   which $program > /dev/null 2>&1
377   if [ $? != 0 ]; then
378     echo "Couldn't find $program. Please download and install it from $url ."
379     exit 1
380   fi
381 }
382
383 function show_help {
384   local program=$(basename $0)
385   echo \
386 "Usage: $program [options] <dir> ...
387
388 $program is a utility to reduce the size of png files by removing
389 unnecessary chunks and compressing the image.
390
391 Options:
392   -o<optimize_level>  Specify optimization level: (default is 1)
393       0  Just run pngcrush. It removes unnecessary chunks and perform basic
394          optimization on the encoded data.
395       1  Optimize png files using pngout/optipng and advdef. This can further
396          reduce addtional 5~30%. This is the default level.
397       2  Aggressively optimize the size of png files. This may produce
398          addtional 1%~5% reduction.  Warning: this is *VERY*
399          slow and can take hours to process all files.
400   -r<revision> If this is specified, the script processes only png files
401                changed since this revision. The <dir> options will be used
402                to narrow down the files under specific directories.
403   -v  Shows optimization process for each file.
404   -h  Print this help text."
405   exit 1
406 }
407
408 if [ ! -e ../.gclient ]; then
409   echo "$0 must be run in src directory"
410   exit 1
411 fi
412
413 if [ "$(expr substr $(uname -s) 1 6)" == "CYGWIN" ]; then
414   using_cygwin=true
415 else
416   using_cygwin=false
417 fi
418
419 # The -i in the shebang line should result in $COLUMNS being set on newer
420 # versions of bash.  If it's not set yet, attempt to set it.
421 if [ -z $COLUMNS ]; then
422   which tput > /dev/null 2>&1
423   if [ "$?" == "0" ]; then
424     COLUMNS=$(tput cols)
425   else
426     # No tput either... give up and just guess 80 columns.
427     COLUMNS=80
428   fi
429   export COLUMNS
430 fi
431
432 OPTIMIZE_LEVEL=1
433 # Parse options
434 while getopts o:r:h:v opts
435 do
436   case $opts in
437     r)
438       COMMIT=$(git svn find-rev r$OPTARG | tail -1) || exit
439       if [ -z "$COMMIT" ] ; then
440         echo "Revision $OPTARG not found"
441         show_help
442       fi
443       ;;
444     o)
445       if [[ "$OPTARG" != 0 && "$OPTARG" != 1 && "$OPTARG" != 2 ]] ; then
446         show_help
447       fi
448       OPTIMIZE_LEVEL=$OPTARG
449       ;;
450     v)
451       VERBOSE=true
452       ;;
453     [h?])
454       show_help;;
455   esac
456 done
457
458 # Remove options from argument list.
459 shift $(($OPTIND -1))
460
461 # Make sure we have all necessary commands installed.
462 install_if_not_installed pngcrush pngcrush
463 if [ $OPTIMIZE_LEVEL -ge 1 ]; then
464   install_if_not_installed optipng optipng
465
466   if $using_cygwin ; then
467     fail_if_not_installed advdef "http://advancemame.sourceforge.net/comp-readme.html"
468   else
469     install_if_not_installed advdef advancecomp
470   fi
471
472   if $using_cygwin ; then
473     pngout_url="http://www.advsys.net/ken/utils.htm"
474   else
475     pngout_url="http://www.jonof.id.au/kenutils"
476   fi
477   fail_if_not_installed pngout $pngout_url
478 fi
479
480 # Create tmp directory for crushed png file.
481 TMP_DIR=$(mktemp -d)
482 if $using_cygwin ; then
483   TMP_DIR=$(cygpath -w $TMP_DIR)
484 fi
485
486 # Make sure we cleanup temp dir
487 #trap "rm -rf $TMP_DIR" EXIT
488
489 # If no directories are specified, optimize all directories.
490 DIRS=$@
491 set ${DIRS:=$ALL_DIRS}
492
493 info "Optimize level=$OPTIMIZE_LEVEL"
494
495 if [ -n "$COMMIT" ] ; then
496  ALL_FILES=$(git diff --name-only $COMMIT HEAD $DIRS | grep "png$")
497  ALL_FILES_LIST=( $ALL_FILES )
498  echo "Processing ${#ALL_FILES_LIST[*]} files"
499  for f in $ALL_FILES; do
500    if [ -f $f ] ; then
501      optimize_file $f
502    else
503      echo "Skipping deleted file: $f";
504    fi
505  done
506 else
507   for d in $DIRS; do
508     if [ -d $d ] ; then
509       info "Optimizing png files in $d"
510       optimize_dir $d
511       info ""
512     elif [ -f $d ] ; then
513       optimize_file $d
514     else
515       echo "Not a file or directory: $d";
516     fi
517   done
518 fi
519
520 # Print the results.
521 echo "Optimized $PROCESSED_FILE/$TOTAL_FILE files in" \
522      "$(date -d "0 + $SECONDS sec" +%Ts)"
523 if [ $PROCESSED_FILE != 0 ]; then
524   let diff=$TOTAL_OLD_BYTES-$TOTAL_NEW_BYTES
525   let percent=$diff*100/$TOTAL_OLD_BYTES
526   echo "Result: $TOTAL_OLD_BYTES => $TOTAL_NEW_BYTES bytes" \
527        "($diff bytes: $percent%)"
528 fi
529 if [ $CORRUPTED_FILE != 0 ]; then
530   echo "Warning: corrupted files found: $CORRUPTED_FILE"
531   echo "Please contact the author of the CL that landed corrupted png files"
532 fi