winemac: Make macdrv_event structs heap-allocated and reference-counted.
[wine] / tools / buildimage
1 #! /usr/bin/perl -w
2 #
3 # Render SVG files containing one or more images into an ICO or BMP.
4 #
5 # Copyright (C) 2010 Joel Holdsworth
6 #
7 # This library is free software; you can redistribute it and/or
8 # modify it under the terms of the GNU Lesser General Public
9 # License as published by the Free Software Foundation; either
10 # version 2.1 of the License, or (at your option) any later version.
11 #
12 # This library is distributed in the hope that it will be useful,
13 # but WITHOUT ANY WARRANTY; without even the implied warranty of
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15 # Lesser General Public License for more details.
16 #
17 # You should have received a copy of the GNU Lesser General Public
18 # License along with this library; if not, write to the Free Software
19 # Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
20
21 use strict;
22 use warnings;
23 use XML::Parser;
24 use MIME::Base64;
25 use File::Copy;
26
27 # Parse the parameters
28 my $svgFileName = $ARGV[0];
29 my $outFileName = $ARGV[1];
30
31 die "Cannot open SVG file" unless defined($svgFileName);
32 die "Cannot open output file" unless defined($outFileName);
33
34 $outFileName =~ m/(.*)\.(.*)/;
35 my $outName = $1;
36 my $ext = lc($2);
37 die "Only BMP and ICO outputs are supported" unless $ext eq "bmp" or $ext eq "ico";
38
39 my $renderedSVGFileName = "$svgFileName.png";
40 my @pngFiles;
41 my @pngFilesRaw;
42
43 # Get the programs from the environment variables
44 my $convert = $ENV{"CONVERT"} || "convert";
45 my $rsvg = $ENV{"RSVG"} || "rsvg";
46 my $icotool = $ENV{"ICOTOOL"} || "icotool";
47
48 # Be ready to abort
49 sub cleanup()
50 {
51     unlink $renderedSVGFileName;
52     unlink $_ foreach(@pngFiles);
53     unlink $_ foreach(@pngFilesRaw);
54 }
55
56 $SIG{"INT"} = "cleanup";
57 $SIG{"HUP"} = "cleanup";
58 $SIG{"TERM"} = "cleanup";
59 $SIG{"__DIE__"} = "cleanup";
60
61 # run a shell command and die on error
62 sub shell(@)
63 {
64     my @args = @_;
65     system(@args) == 0 or die "@args failed: $?";
66 }
67
68 sub svg_element_start
69 {
70     my($expat, $element, %attr) = @_;
71
72     # Parse the id for icon/bitmap render directives
73     my $id = $attr{'id'};
74     return unless defined($id);
75
76     my $size = 0;
77     my $depth = 0;
78     my $width = 0;
79     my $height = 0;
80
81     if($ext eq "ico") {
82         return unless $id =~ /icon:(\d*)-(\d*)/;
83         $size = $1;
84         $depth = $2;
85     } elsif($ext eq "bmp") {
86         return unless $id =~ /bitmap:(\d*)-(\d*)/;
87         $size = $1;
88         $depth = $2;
89     }
90
91     return unless defined($size) and defined($depth);
92
93     warn "Unexpected icon depth" unless
94         $depth == 4 or $depth == 8 or $depth == 24 or $depth == 32;
95     my $pngFileName = "$outName-$size-$depth.png";
96
97     if($element eq "svg") {
98
99         # The whole file is tagged
100         copy($renderedSVGFileName, $pngFileName) or die "File could not be copied";
101
102     } elsif($element eq "rect") {
103
104         # Extract SVG vector images
105         my $x = $attr{'x'};
106         my $y = $attr{'y'};
107         $width = $attr{'width'};
108         $height = $attr{'height'};
109
110         if(defined($x) and defined($x)) {
111             if($x =~ /\d*/ and $y =~ /\d*/) {
112                 shell $convert, $renderedSVGFileName, "-crop", "${width}x${height}+$x+$y", $pngFileName;
113             }
114         }
115
116     } elsif($element eq "image" ) {
117
118         # Extract Base64 encoded PNG data to files
119         my $xlinkHref = $attr{'xlink:href'};
120         if(defined($xlinkHref)) {
121             $xlinkHref =~ /data:image\/png;base64(.*)/;
122             my $imageEncodedData = $1;
123             if(defined $imageEncodedData) {
124                 open(FILE, '>' . $pngFileName) or die "$!";
125                 print FILE decode_base64($imageEncodedData);
126                 close FILE;
127             }
128         }
129     } else {
130         return;
131     }
132
133     if ($width >= 128 && $height >= 128)
134     {
135         push(@pngFilesRaw, $pngFileName);
136     }
137     else
138     {
139         push(@pngFiles, $pngFileName);
140     }
141 }
142
143 # Render the SVG image
144 shell $rsvg, $svgFileName, $renderedSVGFileName;
145
146 # Render the images in the SVG
147 my $parser = new XML::Parser(
148     Handlers => {Start => \&svg_element_start});
149 $parser->parsefile("$svgFileName");
150
151 # If no render directives were found, take the full image as-is
152 unless (@pngFiles || @pngFilesRaw) {
153     my $pngFileName = "bmp$renderedSVGFileName";
154     copy($renderedSVGFileName, $pngFileName) or die "File could not be copied";
155     push(@pngFiles, $pngFileName);
156 }
157
158 # Combine the renderings into the output file
159 if($ext eq "ico") {
160
161     # Place images into the ICO
162     shell $icotool, "-c", "-o", $outFileName, @pngFiles, map { "--raw=$_"; } @pngFilesRaw;
163
164 } elsif($ext eq "bmp") {
165
166     # Only the first image becomes the final BMP
167     my $pngFile = $pngFiles[0];
168     $pngFile =~ /.*-\d*-(\d*)\.png/;
169     my $depth = $1;
170
171     # Convert it into a bmp
172     if($depth == 24) {
173         shell $convert, "png:$pngFile", "+matte", $outFileName;
174     } else {
175         shell $convert, "png:$pngFile", $outFileName;
176     }
177
178 }
179
180 # Delete the intermediate images
181 cleanup();