3 # Take nine input image files and convert them into a
4 # multi-resolution Windows .ICO icon file. The nine files should
7 # - 48x48 icons at 24-bit, 8-bit and 4-bit colour depth respectively
8 # - 32x32 icons at 24-bit, 8-bit and 4-bit colour depth respectively
9 # - 16x16 icons at 24-bit, 8-bit and 4-bit colour depth respectively
11 # ICO files support a 1-bit alpha channel on all these image types.
13 # TODO: it would be nice if we could extend this icon builder to
14 # support monochrome icons and a user-specified subset of the
15 # available formats. None of that should be too hard: the
16 # monochrome raster data has the same format as the alpha channel,
17 # monochrome images have a 2-colour palette containing 000000 and
18 # FFFFFF respectively, and really the biggest problem is designing
19 # a sensible command-line syntax!
22 "\x00\x00\x00\x00" => 0,
23 "\x00\x00\x80\x00" => 1,
24 "\x00\x80\x00\x00" => 2,
25 "\x00\x80\x80\x00" => 3,
26 "\x80\x00\x00\x00" => 4,
27 "\x80\x00\x80\x00" => 5,
28 "\x80\x80\x00\x00" => 6,
29 "\xC0\xC0\xC0\x00" => 7,
30 "\x80\x80\x80\x00" => 8,
31 "\x00\x00\xFF\x00" => 9,
32 "\x00\xFF\x00\x00" => 10,
33 "\x00\xFF\xFF\x00" => 11,
34 "\xFF\x00\x00\x00" => 12,
35 "\xFF\x00\xFF\x00" => 13,
36 "\xFF\xFF\x00\x00" => 14,
37 "\xFF\xFF\xFF\x00" => 15,
39 @win16pal = sort { $win16pal{$a} <=> $win16pal{$b} } keys %win16pal;
44 &readicon($ARGV[0], 48, 48, 24);
45 &readicon($ARGV[1], 48, 48, 8);
46 &readicon($ARGV[2], 48, 48, 4);
47 &readicon($ARGV[3], 32, 32, 24);
48 &readicon($ARGV[4], 32, 32, 8);
49 &readicon($ARGV[5], 32, 32, 4);
50 &readicon($ARGV[6], 16, 16, 24);
51 &readicon($ARGV[7], 16, 16, 8);
52 &readicon($ARGV[8], 16, 16, 4);
54 # Now write out the output icon file.
55 print pack "vvv", 0, 1, scalar @hdr; # file-level header
56 $filepos = 6 + 16 * scalar @hdr;
57 for ($i = 0; $i < scalar @hdr; $i++) {
59 print pack "V", $filepos;
60 $filepos += length($dat[$i]);
62 for ($i = 0; $i < scalar @hdr; $i++) {
67 my $filename = shift @_;
75 # Read the file in as RGBA data. We flip vertically at this
76 # point, to avoid having to do it ourselves (.BMP and hence
77 # .ICO are bottom-up).
79 open IDATA, "convert -flip -depth 8 $filename rgba:- |";
80 push @$data, $rgb while (read IDATA,$rgb,4,0) == 4;
82 # Check we have the right amount of data.
85 die "wrong amount of image data ($al, expected $xl) from $filename\n"
88 # Build the alpha channel now, so we can exclude transparent
89 # pixels from the palette analysis. We replace transparent
90 # pixels with undef in the data array.
92 # We quantise the alpha channel half way up, so that alpha of
93 # 0x80 or more is taken to be fully opaque and 0x7F or less is
94 # fully transparent. Nasty, but the best we can do without
95 # dithering (and don't even suggest we do that!).
100 for ($y = 0; $y < $h; $y++) {
101 my $currbyte = 0, $currbits = 0;
102 for ($x = 0; $x < (($w+31)|31)-31; $x++) {
103 $pix = ($x < $w ? $data->[$y*$w+$x] : "\x00\x00\x00\xFF");
104 my @rgba = unpack "CCCC", $pix;
107 if ($rgba[3] < 0x80) {
109 $data->[$y*$w+$x] = undef;
111 $currbyte |= 1; # MS has the alpha channel inverted :-)
113 # Might as well flip RGBA into BGR0 while we're here.
115 $data->[$y*$w+$x] = pack "CCCC",
116 $rgba[2], $rgba[1], $rgba[0], 0;
119 if ($currbits >= 8) {
120 $alpha .= pack "C", $currbyte;
126 # For an 8-bit image, check we have at most 256 distinct
127 # colours, and build the palette.
131 foreach $pix (@$data) {
132 next unless defined $pix;
133 $pal{$pix} = $palindex++ unless defined $pal{$pix};
135 die "too many colours in 8-bit image $filename\n" unless $palindex <= 256;
136 } elsif ($depth == 4) {
142 # For a non-24-bit image, flatten the image into one palette
144 my $currbyte = 0, $currbits = 0;
145 for ($i = 0; $i < scalar @$data; $i++) {
147 $currbyte <<= $depth;
150 if (!defined $pal{$pix}) {
151 die "illegal colour value $pix at pixel $i in $filename\n";
153 $currbyte |= $pal{$pix};
157 if ($currbits >= 8) {
158 $raster .= pack "C", $currbyte;
163 # For a 24-bit image, reverse the order of the R,G,B values
164 # and stick a padding zero on the end.
165 for ($i = 0; $i < scalar @$data; $i++) {
166 if (defined $data->[$i]) {
167 $raster .= $data->[$i];
169 $raster .= "\x00\x00\x00\x00";
172 $depth = 32; # and adjust this
175 # Prepare the icon data. First the header...
176 my $data = pack "VVVvvVVVVVV",
177 40, # size of bitmap info header
179 $h*2, # icon height (x2 to indicate the subsequent alpha channel)
180 1, # 1 plane (common to all MS image formats)
181 $depth, # bits per pixel
183 length $raster, # image size
184 0, 0, 0, 0; # resolution, colours used, colours important (ignored)
185 # ... then the palette ...
187 my $ncols = (1 << $depth);
188 my $palette = "\x00\x00\x00\x00" x $ncols;
189 foreach $i (keys %pal) {
190 substr($palette, $pal{$i}*4, 4) = $i;
194 # ... the raster data we already had ready ...
196 # ... and the alpha channel we already had as well.
199 # Prepare the header which will represent this image in the
201 my $header = pack "CCCCvvV",
202 $w, $h, # width and height (this time the real height)
203 1 << $depth, # number of colours, if less than 256
206 $depth, # bits per pixel
207 length $data; # size of real icon data