fix cards css generator
[brisk-img.git] / bin / spriter.sh
1 #!/bin/bash
2 #
3 #  Max height in play area 121 px
4 #
5 # set -x
6
7
8 if [ $# -ne 3 -o "$1" = "-h" -o "$1" = "--help" ]; then
9     echo "$0 <folder_in> <suffix> <file_out_without_extension>"
10     exit 1
11 fi
12 fold=$1
13 sfx=$2
14 fou=$3
15
16 w=$(identify -format "%w" ${fold}/00${sfx}.png)
17 h=$(identify -format "%h" ${fold}/00${sfx}.png)
18
19 convert -size $((w * 9))x$((h * 5)) xc:none ${fou}${sfx}_bg.png
20 convert -size $((h * 9))x$((w * 5)) xc:none ${fou}${sfx}_oriz_bg.png
21
22 convert -size ${w}x${h} xc:none ${fou}${sfx}_empty.png
23 convert -size ${h}x${w} xc:none ${fou}${sfx}_empty_ea.png
24 cp ${fou}${sfx}_empty_ea.png ${fou}${sfx}_empty_we.png
25 cssname="${fou}${sfx}.css"
26 rm -f $cssname
27 touch $cssname
28
29 for direction in "" "_ea" "_we"; do
30     horiz_sfx=""
31     if [ "$direction" != "" ]; then
32         horiz_sfx="_oriz"
33     fi
34     argz="${fou}${sfx}${horiz_sfx}_bg.png"
35
36     for seed in 0 1 2 3 4; do
37         for card in $(seq 0 9); do
38             if [ "$direction" = "" ]; then
39                 x=$(((seed * 2 + card / 5) * w)) 
40                 y=$(((card % 5) * h))
41             else
42                 x=$(((seed * 2 + card / 5) * h)) 
43                 y=$(((card % 5) * w))
44             fi
45
46             card_id=$(printf "%02d" "$((seed * 10 + card))")
47             if [ "$card_id" = "40" ]; then
48                 card_id="cover"
49             fi
50             card_url=$(echo "$fou" | sed 's@.*/\([^/]\+/[^/]\+$\)@\1@g')
51             cat <<EOF >> $cssname
52 img[data-card-id="${card_id}${direction}"] {
53     background: url('${card_url}${direction}.png') -${x}px -${y}px;
54 }
55
56 EOF
57             
58             fin_no=$(printf "%s/%02d%s.png" "$fold" "$((seed * 10 + card))" "$sfx")
59             fin=$(printf "%s/%02d%s%s.png" "$fold" "$((seed * 10 + card))" "$sfx" "$direction")
60             if [ $seed -lt 4 ]; then
61                 if [ "$direction" == "" ]; then
62                     :
63                 elif [ "$direction" == "_ea" ]; then
64                     convert -rotate 270 "$fin_no" "$fin"
65                 elif [ "$direction" == "_we" ]; then
66                     convert -rotate 90 "$fin_no" "$fin"                
67                 else
68                     exit 1
69                 fi
70             fi
71             
72             argz="$argz $fin -geometry +${x}+${y} -composite "
73             if [ $seed -eq 4 ]; then
74                 break
75             fi
76         done
77     done
78     # full color: argz="$argz ${fou}${sfx}.png"
79     argz="$argz +dither -colors 255 ${fou}${sfx}${direction}.png"
80     convert $argz
81     echo "Created ${fou}${sfx}${direction}.png"
82 done
83
84 rm  ${fold}/[0-3][0-9]${sfx}*.png ${fou}${sfx}_bg.png ${fou}${sfx}_oriz_bg.png