]> code.delx.au - refind/blob - images/mkeei.py
Fix for refind-install script to handle a wider range of disk devices,
[refind] / images / mkeei.py
1 #!/usr/bin/python
2
3 import sys
4 import Image
5
6 def enc_backbuffer(backbuffer):
7 """Helper function for RLE compression, encodes a string of uncompressable data."""
8 compdata = []
9 if len(backbuffer) == 0:
10 return compdata
11 while len(backbuffer) > 128:
12 compdata.append(127)
13 compdata.extend(backbuffer[0:128])
14 backbuffer = backbuffer[128:]
15 compdata.append(len(backbuffer)-1)
16 compdata.extend(backbuffer)
17 return compdata
18
19 def compress_rle(rawdata):
20 """Compresses the string using a RLE scheme."""
21 compdata = []
22 backbuffer = []
23
24 while len(rawdata) >= 3:
25 c = rawdata[0]
26 if rawdata[1] == c and rawdata[2] == c:
27 runlength = 3
28 while runlength < 130 and len(rawdata) > runlength:
29 if rawdata[runlength] == c:
30 runlength = runlength + 1
31 else:
32 break
33 compdata.extend(enc_backbuffer(backbuffer))
34 backbuffer = []
35 compdata.append(runlength + 125)
36 compdata.append(c)
37 rawdata = rawdata[runlength:]
38
39 else:
40 backbuffer.append(c)
41 rawdata = rawdata[1:]
42
43 backbuffer.extend(rawdata)
44 compdata.extend(enc_backbuffer(backbuffer))
45
46 return compdata
47
48 def encode_plane(rawdata, identname, planename):
49 """Encodes the data of a single plane."""
50
51 rawlen = len(rawdata)
52 compdata = compress_rle(rawdata)
53 complen = len(compdata)
54 print " plane %s: compressed %d to %d (%.1f%%)" % (planename, rawlen, complen, float(complen) / float(rawlen) * 100.0)
55
56 output = """static const UINT8 eei_%s_planedata_%s[%d] = {
57 """ % (identname, planename, complen)
58 for i in range(0, len(compdata)):
59 output = output + " 0x%02x," % compdata[i]
60 if (i % 12) == 11:
61 output = output + "\n"
62 output = output + """
63 };
64 """
65 return (output, "eei_%s_planedata_%s, %d" % (identname, planename, complen))
66
67
68 ### main loop
69
70 print "mkeei 0.1, Copyright (c) 2006 Christoph Pfisterer"
71
72 planenames = ( "blue", "green", "red", "alpha", "grey" )
73
74 for filename in sys.argv[1:]:
75
76 origimage = Image.open(filename)
77
78 (width, height) = origimage.size
79 mode = origimage.mode
80 data = origimage.getdata()
81
82 print "%s: %d x %d %s" % (filename, width, height, mode)
83
84 basename = filename[:-4] # TODO!!!!!!
85 identname = basename.replace("-", "_")
86
87 planes = [ [], [], [], [] ]
88
89 if mode == "RGB":
90 for pixcount in range(0, width*height):
91 pixeldata = data[pixcount]
92 planes[0].append(pixeldata[2])
93 planes[1].append(pixeldata[1])
94 planes[2].append(pixeldata[0])
95
96 elif mode == "RGBA":
97 for pixcount in range(0, width*height):
98 pixeldata = data[pixcount]
99 planes[0].append(pixeldata[2])
100 planes[1].append(pixeldata[1])
101 planes[2].append(pixeldata[0])
102 planes[3].append(pixeldata[3])
103
104 elif mode == "L":
105 for pixcount in range(0, width*height):
106 pixeldata = data[pixcount]
107 planes[0].append(pixeldata)
108 planes[1].append(pixeldata)
109 planes[2].append(pixeldata)
110
111 else:
112 print " Error: Mode not supported!"
113 continue
114
115 # special treatment for fonts
116
117 if basename[0:4] == "font":
118 if planes[0] != planes[1] or planes[0] != planes[2]:
119 print " Error: Font detected, but it is not greyscale!"
120 continue
121 print " font detected, encoding as alpha-only"
122 # invert greyscale values for use as alpha
123 planes[3] = map(lambda x: 255-x, planes[0])
124 planes[0] = []
125 planes[1] = []
126 planes[2] = []
127
128 # generate optimal output
129
130 output = ""
131 planeinfo = [ "NULL, 0", "NULL, 0", "NULL, 0", "NULL, 0" ]
132
133 if len(planes[0]) > 0 and planes[0] == planes[1] and planes[0] == planes[2]:
134 print " encoding as greyscale"
135 (output_part, planeinfo[0]) = encode_plane(planes[0], identname, planenames[4])
136 output = output + output_part
137 planeinfo[1] = planeinfo[0]
138 planeinfo[2] = planeinfo[0]
139
140 elif len(planes[0]) > 0:
141 print " encoding as true color"
142
143 (output_part, planeinfo[0]) = encode_plane(planes[0], identname, planenames[0])
144 output = output + output_part
145
146 if planes[1] == planes[0]:
147 print " encoding plane 1 is a copy of plane 0"
148 planeinfo[1] = planeinfo[0]
149 else:
150 (output_part, planeinfo[1]) = encode_plane(planes[1], identname, planenames[1])
151 output = output + output_part
152
153 if planes[2] == planes[0]:
154 print " encoding plane 2 is a copy of plane 0"
155 planeinfo[2] = planeinfo[0]
156 elif planes[2] == planes[1]:
157 print " encoding plane 2 is a copy of plane 1"
158 planeinfo[2] = planeinfo[1]
159 else:
160 (output_part, planeinfo[2]) = encode_plane(planes[2], identname, planenames[2])
161 output = output + output_part
162
163 if len(planes[3]) > 0:
164 if reduce(lambda x,y: x+y, planes[3]) == 0:
165 print " skipping alpha plane because it is empty"
166 else:
167 (output_part, planeinfo[3]) = encode_plane(planes[3], identname, planenames[3])
168 output = output + output_part
169
170 output = output + "static EEI_IMAGE eei_%s = { %d, %d, NULL, {\n" % (identname, width, height)
171 for i in range(0,4):
172 output = output + " { %s },\n" % planeinfo[i]
173 output = output + "} };\n"
174
175 f = file("eei_%s.h" % identname, "w")
176 f.write(output)
177 f.close()
178
179 print "Done!"