Chromium Code Reviews| 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 168 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 179 optipng -q -nb -nc -zw$i -zc1-9 -zm1-9 -zs0-3 -f0-5 $file | 179 optipng -q -nb -nc -zw$i -zc1-9 -zm1-9 -zs0-3 -f0-5 $file |
| 180 done | 180 done |
| 181 fi | 181 fi |
| 182 for i in $(seq 1 4); do | 182 for i in $(seq 1 4); do |
| 183 throbber | 183 throbber |
| 184 advdef -q -z -$i $file | 184 advdef -q -z -$i $file |
| 185 done | 185 done |
| 186 echo -ne "\r" | 186 echo -ne "\r" |
| 187 } | 187 } |
| 188 | 188 |
| 189 # Usage: get_color_type <file> | |
| 190 # Returns the color type code of the png file. | |
| 191 # See http://en.wikipedia.org/wiki/Portable_Network_Graphics#Color_depth | |
| 192 # for details about the color type code. | |
| 193 function get_color_type { | |
| 194 local file=$1 | |
| 195 echo $(identify -verbose $file | awk '/IHDR.color_type/ {print $3}') | |
| 196 } | |
| 197 | |
| 189 # Usage: optimize_size <file> | 198 # Usage: optimize_size <file> |
| 190 # Performs png file optimization. | 199 # Performs png file optimization. |
| 191 function optimize_size { | 200 function optimize_size { |
| 192 tput el | 201 tput el |
| 193 local file=$1 | 202 local file=$1 |
| 194 echo -n "$file " | 203 echo -n "$file " |
| 195 | 204 |
| 196 advdef -q -z -4 $file | 205 advdef -q -z -4 $file |
| 197 | 206 |
| 198 pngout -q -s4 -c0 -force $file $file.tmp.png | 207 pngout -q -s4 -c0 -force $file $file.tmp.png |
| 199 if [ -f $file.tmp.png ]; then | 208 if [ -f $file.tmp.png ]; then |
| 200 rm $file.tmp.png | 209 rm $file.tmp.png |
| 201 process_grayscale $file | 210 process_grayscale $file |
| 202 process_grayscale_alpha $file | 211 process_grayscale_alpha $file |
| 203 else | 212 else |
| 204 pngout -q -s4 -c4 -force $file $file.tmp.png | 213 pngout -q -s4 -c4 -force $file $file.tmp.png |
| 205 if [ -f $file.tmp.png ]; then | 214 if [ -f $file.tmp.png ]; then |
| 206 rm $file.tmp.png | 215 rm $file.tmp.png |
| 207 process_grayscale_alpha $file | 216 process_grayscale_alpha $file |
| 208 else | 217 else |
| 209 process_rgb $file | 218 process_rgb $file |
| 210 fi | 219 fi |
| 211 fi | 220 fi |
| 212 | 221 |
| 213 echo -n "|filter" | 222 echo -n "|filter" |
| 214 optipng -q -zc9 -zm8 -zs0-3 -f0-5 $file | 223 local old_color_type=$(get_color_type $file) |
| 224 optipng -q -zc9 -zm8 -zs0-3 -f0-5 $file -out $file.tmp.png | |
| 225 local new_color_type=$(get_color_type $file.tmp.png) | |
| 226 # optipng may corrupt a png file when reducing the color type | |
| 227 # to grayscale/grayscale+alpha. Just skip such cases. | |
| 228 # crbug.com/174505, crbug.com/174084. | |
|
Nico
2013/02/06 21:53:30
Should we fix this in optipng instead? Have you at
oshima
2013/02/06 22:34:09
Filed a bug and added the reference.
| |
| 229 if [[ $old_color_type == 6 && | |
| 230 ($new_color_type == 0 || $new_color_type == 4) ]] ; then | |
| 231 rm $file.tmp.png | |
| 232 echo -n "[skip opting]" | |
| 233 else | |
| 234 mv $file.tmp.png $file | |
| 235 fi | |
| 215 pngout -q -k1 -s1 $file | 236 pngout -q -k1 -s1 $file |
| 216 | 237 |
| 217 huffman_blocks $file | 238 huffman_blocks $file |
| 218 | 239 |
| 219 # TODO(oshima): Experiment with strategy 1. | 240 # TODO(oshima): Experiment with strategy 1. |
| 220 echo -n "|strategy" | 241 echo -n "|strategy" |
| 221 if [ $OPTIMIZE_LEVEL == 2 ]; then | 242 if [ $OPTIMIZE_LEVEL == 2 ]; then |
| 222 for i in 3 2 0; do | 243 for i in 3 2 0; do |
| 223 pngout -q -k1 -ks -s$i $file | 244 pngout -q -k1 -ks -s$i $file |
| 224 done | 245 done |
| (...skipping 145 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 370 done | 391 done |
| 371 | 392 |
| 372 # Print the results. | 393 # Print the results. |
| 373 let diff=$TOTAL_OLD_BYTES-$TOTAL_NEW_BYTES | 394 let diff=$TOTAL_OLD_BYTES-$TOTAL_NEW_BYTES |
| 374 let percent=$diff*100/$TOTAL_OLD_BYTES | 395 let percent=$diff*100/$TOTAL_OLD_BYTES |
| 375 echo "Processed $PROCESSED_FILE files (out of $TOTAL_FILE files)" \ | 396 echo "Processed $PROCESSED_FILE files (out of $TOTAL_FILE files)" \ |
| 376 "in $(date -u -d @$SECONDS +%T)s" | 397 "in $(date -u -d @$SECONDS +%T)s" |
| 377 echo "Result : $TOTAL_OLD_BYTES => $TOTAL_NEW_BYTES bytes" \ | 398 echo "Result : $TOTAL_OLD_BYTES => $TOTAL_NEW_BYTES bytes" \ |
| 378 "($diff bytes : $percent %)" | 399 "($diff bytes : $percent %)" |
| 379 | 400 |
| OLD | NEW |