3 # Generate Mac OS X .icns files, or at least the simple subformats
4 # that don't involve JPEG encoding and the like.
6 # Sources: https://en.wikipedia.org/wiki/Apple_Icon_Image_format and
7 # some details implicitly documented by the source code of 'libicns'.
13 # The file format has a typical IFF-style (type, length, data) chunk
14 # structure, with one outer chunk containing subchunks for various
15 # different icon sizes and formats.
16 def make_chunk(chunkid, data):
17 assert len(chunkid) == 4
18 return chunkid + struct.pack(">I", len(data) + 8) + data
20 # Monochrome icons: a single chunk containing a 1 bpp image followed
21 # by a 1 bpp transparency mask. Both uncompressed, unless you count
22 # packing the bits into bytes.
23 def make_mono_icon(size, rgba):
24 assert len(rgba) == size * size
26 # We assume our input image was monochrome, so that the R,G,B
27 # channels are all the same; we want the image and then the mask,
28 # so we take the R channel followed by the alpha channel. However,
29 # we have to flip the former, because in the output format the
30 # image has 0=white and 1=black, while the mask has 0=transparent
32 pixels = [rgba[index][chan] ^ flip for (chan, flip) in [(0,0xFF),(3,0)]
33 for index in range(len(rgba))]
35 # Encode in 1-bit big-endian format.
37 for i in range(0, len(pixels), 8):
40 if pixels[i+j] >= 0x80:
44 # This size-32 chunk id is an anomaly in what would otherwise be a
45 # consistent system of using {s,l,h,t} for {16,32,48,128}-pixel
47 chunkid = { 16: "ics#", 32: "ICN#", 48: "ich#" }[size]
48 return make_chunk(chunkid, data)
50 # Mask for full-colour icons: a chunk containing an 8 bpp alpha
51 # bitmap, uncompressed. The RGB data appears in a separate chunk.
52 def make_colour_mask(size, rgba):
53 assert len(rgba) == size * size
55 data = "".join(map(lambda pix: chr(pix[3]), rgba))
57 chunkid = { 16: "s8mk", 32: "l8mk", 48: "h8mk", 128: "t8mk" }[size]
58 return make_chunk(chunkid, data)
60 # Helper routine for deciding when to start and stop run-length
62 def runof3(string, position):
63 return (position < len(string) and
64 string[position:position+3] == string[position] * 3)
66 # RGB data for full-colour icons: a chunk containing 8 bpp red, green
67 # and blue images, each run-length encoded (see comment inside the
68 # function), and then concatenated.
69 def make_colour_icon(size, rgba):
70 assert len(rgba) == size * size
74 # Mysterious extra zero header word appearing only in the size-128
75 # icon chunk. libicns doesn't know what it's for, and neither do
80 # Handle R,G,B channels in sequence. (Ignore the alpha channel; it
81 # goes into the separate mask chunk constructed above.)
83 pixels = "".join([chr(rgba[index][chan])
84 for index in range(len(rgba))])
86 # Run-length encode each channel using the following format:
87 # * byte 0x80-0xFF followed by one literal byte means repeat
88 # that byte 3-130 times
89 # * byte 0x00-0x7F followed by n+1 literal bytes means emit
90 # those bytes once each.
92 while pos < len(pixels):
94 if runof3(pixels, start):
96 pixval = pixels[start]
97 while (pos - start < 130 and
99 pixels[pos] == pixval):
101 data += chr(0x80 + pos-start - 3) + pixval
103 while (pos - start < 128 and
104 pos < len(pixels) and
105 not runof3(pixels, pos)):
107 data += chr(0x00 + pos-start - 1) + pixels[start:pos]
109 chunkid = { 16: "is32", 32: "il32", 48: "ih32", 128: "it32" }[size]
110 return make_chunk(chunkid, data)
112 # Load an image file from disk and turn it into a simple list of
113 # 4-tuples giving 8-bit R,G,B,A values for each pixel.
115 # My icon-building makefile already depends on ImageMagick, so I use
116 # identify and convert here in place of more sensible Python libraries
117 # so as to add no build dependency that wasn't already needed.
118 def load_rgba(filename):
119 size = subprocess.check_output(["identify", "-format", "%wx%h", filename])
120 width, height = map(int, size.split("x"))
121 assert width == height
122 data = subprocess.check_output(["convert", "-depth", "8",
124 assert len(data) == width*height*4
125 rgba = [map(ord, data[i:i+4]) for i in range(0, len(data), 4)]
130 # Trivial argument format: each argument is a filename prefixed with
131 # "mono:", "colour:" or "output:". The first two indicate image files
132 # to use as part of the icon, and the last gives the output file name.
133 # Icon subformat chunks are written out in the order of the arguments.
134 for arg in sys.argv[1:]:
135 kind, filename = arg.split(":", 2)
139 size, rgba = load_rgba(filename)
141 data += make_mono_icon(size, rgba)
142 elif kind == "colour":
143 data += make_colour_icon(size, rgba) + make_colour_mask(size, rgba)
145 assert False, "bad argument '%s'" % arg
147 data = make_chunk("icns", data)
149 with open(outfile, "w") as f: