webperimental: killstack decides stack protects.
[freeciv.git] / data / flags / convert_png
blob2637f828e30f6e99c3420b5504afa2f35807bd5e
1 #!/bin/bash
3 files="$@"
5 if test "x$1" = "x-h" || test "x$1" = "x--help" ; then
6 echo "Usage: $(basename $0) [list of svg-files]"
7 exit 0
8 fi
10 # Renderer: inkscape or sodipodi. Inkscape seems slightly better.
11 #SODIPODI="sodipodi"
12 RENDERER="inkscape"
14 # Target width: 29px is regular, 44px is large (height is then calculated
15 # automatically).
16 TARGET_WIDTH=29
17 TARGET_WIDTH_LARGE=44
19 SHIELD_WIDTH=14
20 SHIELD_HEIGHT=14
21 SHIELD_WIDTH_LARGE=19
22 SHIELD_HEIGHT_LARGE=19
24 which $RENDERER >/dev/null \
25 || (echo "You need $RENDERER to render the SVG files.")
26 (which convert >/dev/null && which composite >/dev/null) \
27 || (echo "You need ImageMagick to manipulate the images."; err=yes)
29 for file0 in $files; do
30 # Generate the regular images
31 filebase=`echo $file0 | sed 's/.[Ss][Vv][Gg]$//'`
33 file1="$filebase-1.png"
34 file2="$filebase-2.png"
35 file3="$filebase-3.png"
36 file4="$filebase.png"
37 echo "Converting $file0 to $file4"
39 # This is actually necessary for rendering
40 rm -f $file1 $file2 $file3 $file4
42 $RENDERER -f "$file0" -w $(($TARGET_WIDTH - 2)) -e "$file1" >/dev/null
44 # Ugly way to determine width and height. There must be a better way!
45 SIZE=`identify "$file1" | sed "s/^.*PNG //" | sed "s/ .*\$//"`
46 WIDTH=`echo $SIZE | sed "s/x.*\$//"`
47 HEIGHT=`echo $SIZE | sed "s/^.*x//"`
49 # This complicated code puts a 1-pixel black border around the image.
50 convert -resize $((2*$WIDTH))x$((2*$HEIGHT)) -fill black -draw "rectangle 0,0 $((2*$WIDTH)),$((2*$HEIGHT))" $file1 $file2
51 convert -crop $((2+$WIDTH))x$((2+$HEIGHT))+0x0 $file2 $file3
52 composite -gravity center -compose src-over $file1 $file3 $file4
54 rm -f $file1 $file2 $file3 "$filebase-4-fs8.png"
56 file1="$filebase-1.png"
57 file2="$filebase-2.png"
58 file3="$filebase-3.png"
59 file4="$filebase-shield.png"
61 WIDTH=$(($SHIELD_WIDTH-2))
62 HEIGHT=$(($SHIELD_HEIGHT-2))
64 # The following assumes that the drawing has the same dimensions as the page
65 DRAWING_WIDTH=$($RENDERER -f $file0 -W)
66 DRAWING_HEIGHT=$($RENDERER -f $file0 -H)
67 X0=$(awk -v drawing_width=${DRAWING_WIDTH} 'BEGIN { print int((drawing_width / 6) + 0.5) }')
68 X1=$(awk -v x0=${X0} 'BEGIN{ print int((x0 * 5) + 0.5) }')
69 Y0=0
70 Y1=$(awk -v drawing_height=${DRAWING_HEIGHT} 'BEGIN { print int(drawing_height + 0.5) }')
71 AREA=$X0":"$Y0":"$X1":"$Y1
73 $RENDERER -f $file0 -w $WIDTH -h $HEIGHT -e $file1 -a=$AREA > /dev/null
75 composite -gravity center -compose src-over $file1 mask.png $file2
76 composite -gravity center -compose src-over mask.png $file2 $file3
77 convert -transparent magenta $file3 $file4
79 rm -f $file1 $file2 $file3 "$filebase-4-fs8.png"
81 # Generate the large images
82 filebase=`echo $file0 | sed 's/.[Ss][Vv][Gg]$//'`
84 file1="$filebase-1.png"
85 file2="$filebase-2.png"
86 file3="$filebase-3.png"
87 file4="$filebase-large.png"
88 echo "Converting $file0 to $file4"
90 # This is actually necessary for rendering
91 rm -f $file1 $file2 $file3 $file4
93 $RENDERER -f "$file0" -w $(($TARGET_WIDTH_LARGE - 2)) -e "$file1" >/dev/null
95 # Ugly way to determine width and height. There must be a better way!
96 SIZE=`identify "$file1" | sed "s/^.*PNG //" | sed "s/ .*\$//"`
97 WIDTH=`echo $SIZE | sed "s/x.*\$//"`
98 HEIGHT=`echo $SIZE | sed "s/^.*x//"`
100 # This complicated code puts a 1-pixel black border around the image.
101 convert -resize $((2*$WIDTH))x$((2*$HEIGHT)) -fill black -draw "rectangle 0,0 $((2*$WIDTH)),$((2*$HEIGHT))" $file1 $file2
102 convert -crop $((2+$WIDTH))x$((2+$HEIGHT))+0x0 $file2 $file3
103 composite -gravity center -compose src-over $file1 $file3 $file4
105 rm -f $file1 $file2 $file3 "$filebase-4-fs8.png"
107 file1="$filebase-1.png"
108 file2="$filebase-2.png"
109 file3="$filebase-3.png"
110 file4="$filebase-shield-large.png"
112 WIDTH=$(($SHIELD_WIDTH_LARGE-2))
113 HEIGHT=$(($SHIELD_HEIGHT_LARGE-2))
115 # The following assumes that the drawing has the same dimensions as the page
116 DRAWING_WIDTH=$($RENDERER -f $file0 -W)
117 DRAWING_HEIGHT=$($RENDERER -f $file0 -H)
118 X0=$(awk -v drawing_width=${DRAWING_WIDTH} 'BEGIN { print int((drawing_width / 6) + 0.5) }')
119 X1=$(awk -v x0=${X0} 'BEGIN{ print int((x0 * 5) + 0.5) }')
120 Y0=0
121 Y1=$(awk -v drawing_height=${DRAWING_HEIGHT} 'BEGIN { print int(drawing_height + 0.5) }')
122 AREA=$X0":"$Y0":"$X1":"$Y1
124 $RENDERER -f $file0 -w $WIDTH -h $HEIGHT -e $file1 -a=$AREA > /dev/null
126 composite -gravity center -compose src-over $file1 mask-large.png $file2
127 composite -gravity center -compose src-over mask-large.png $file2 $file3
128 convert -transparent magenta $file3 $file4
130 rm -f $file1 $file2 $file3 "$filebase-4-fs8.png"
131 done