generate-jpeg: minor fixes
[xonotic/xonotic.git] / misc / tools / generate-jpeg.sh
1 #!/bin/sh
2
3 set -e
4
5 : ${qual:=95}
6 : ${qual_alpha:=99}
7
8 for X in "$@"; do
9         case "$X" in
10                 *.jpg)
11                         jpegoptim --strip-all -m$qual "$X"
12                         ;;
13                 *.png|*.tga)
14                         if convert "$X" -depth 16 RGBA:- | perl -e 'while(read STDIN, $_, 8) { substr($_, 6, 2) eq "\xFF\xFF" or exit 1; ++$pix; } exit not $pix;'; then
15                                 echo "$X has no alpha, converting"
16                                 convert "$X" -quality 100 "${X%.*}.jpg"
17                                 jpegoptim --strip-all -m$qual "${X%.*}.jpg"
18                                 rm -f "$X"
19                         else
20                                 echo "$X has alpha, converting twice"
21                                 convert "$X" -alpha extract -quality 100 "${X%.*}.jpg"
22                                 convert "$X" -alpha off     -quality 100 "${X%.*}_alpha.jpg"
23                                 jpegoptim --strip-all -m$qual "${X%.*}.jpg"
24                                 jpegoptim --strip-all -m$qual_alpha "${X%.*}_alpha.jpg"
25                                 rm -f "$X"
26                         fi
27                         ;;
28         esac
29 done