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}') | |
Nico
2013/02/06 22:46:23
can you base this on the output of `file` instead
oshima
2013/02/06 23:30:37
Done.
| |
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 until | |
228 # the bug is fixed. See crbug.com/174505, crbug.com/174084. | |
229 # The issue is reported in | |
230 # https://sourceforge.net/tracker/?func=detail&aid=3603630&group_id=151404&ati d=780913 | |
231 if [[ $old_color_type == 6 && | |
232 ($new_color_type == 0 || $new_color_type == 4) ]] ; then | |
233 rm $file.tmp.png | |
234 echo -n "[skip opting]" | |
235 else | |
236 mv $file.tmp.png $file | |
237 fi | |
215 pngout -q -k1 -s1 $file | 238 pngout -q -k1 -s1 $file |
216 | 239 |
217 huffman_blocks $file | 240 huffman_blocks $file |
218 | 241 |
219 # TODO(oshima): Experiment with strategy 1. | 242 # TODO(oshima): Experiment with strategy 1. |
220 echo -n "|strategy" | 243 echo -n "|strategy" |
221 if [ $OPTIMIZE_LEVEL == 2 ]; then | 244 if [ $OPTIMIZE_LEVEL == 2 ]; then |
222 for i in 3 2 0; do | 245 for i in 3 2 0; do |
223 pngout -q -k1 -ks -s$i $file | 246 pngout -q -k1 -ks -s$i $file |
224 done | 247 done |
(...skipping 145 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
370 done | 393 done |
371 | 394 |
372 # Print the results. | 395 # Print the results. |
373 let diff=$TOTAL_OLD_BYTES-$TOTAL_NEW_BYTES | 396 let diff=$TOTAL_OLD_BYTES-$TOTAL_NEW_BYTES |
374 let percent=$diff*100/$TOTAL_OLD_BYTES | 397 let percent=$diff*100/$TOTAL_OLD_BYTES |
375 echo "Processed $PROCESSED_FILE files (out of $TOTAL_FILE files)" \ | 398 echo "Processed $PROCESSED_FILE files (out of $TOTAL_FILE files)" \ |
376 "in $(date -u -d @$SECONDS +%T)s" | 399 "in $(date -u -d @$SECONDS +%T)s" |
377 echo "Result : $TOTAL_OLD_BYTES => $TOTAL_NEW_BYTES bytes" \ | 400 echo "Result : $TOTAL_OLD_BYTES => $TOTAL_NEW_BYTES bytes" \ |
378 "($diff bytes : $percent %)" | 401 "($diff bytes : $percent %)" |
379 | 402 |
OLD | NEW |