]> code.delx.au - refind/blob - images/mkegemb.py
Fix for refind-install script to handle a wider range of disk devices,
[refind] / images / mkegemb.py
1 #!/usr/bin/env python
2
3 import sys, os.path
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, 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 return compdata
57
58
59 ### main loop
60
61 print "mkegemb 0.1, Copyright (c) 2006 Christoph Pfisterer"
62
63 planenames = ( "blue", "green", "red", "alpha", "grey" )
64
65 for filename in sys.argv[1:]:
66
67 origimage = Image.open(filename)
68
69 (width, height) = origimage.size
70 mode = origimage.mode
71 data = origimage.getdata()
72
73 print "%s: %d x %d %s" % (filename, width, height, mode)
74
75 (basename, extension) = os.path.splitext(filename)
76 identname = basename.replace("-", "_")
77
78 # extract image data from PIL object
79
80 planes = [ [], [], [], [] ]
81
82 if mode == "RGB":
83 for pixcount in range(0, width*height):
84 pixeldata = data[pixcount]
85 planes[0].append(pixeldata[0])
86 planes[1].append(pixeldata[1])
87 planes[2].append(pixeldata[2])
88
89 elif mode == "RGBA":
90 for pixcount in range(0, width*height):
91 pixeldata = data[pixcount]
92 planes[0].append(pixeldata[0])
93 planes[1].append(pixeldata[1])
94 planes[2].append(pixeldata[2])
95 planes[3].append(pixeldata[3])
96
97 elif mode == "L":
98 for pixcount in range(0, width*height):
99 pixeldata = data[pixcount]
100 planes[0].append(pixeldata)
101 planes[1].append(pixeldata)
102 planes[2].append(pixeldata)
103
104 else:
105 print " Error: Mode not supported!"
106 continue
107
108 # special treatment for fonts
109
110 if basename[0:4] == "font":
111 if planes[0] != planes[1] or planes[0] != planes[2]:
112 print " Error: Font detected, but it is not greyscale!"
113 continue
114 print " font detected, encoding as alpha-only"
115 # invert greyscale values for use as alpha
116 planes[3] = map(lambda x: 255-x, planes[0])
117 planes[0] = []
118 planes[1] = []
119 planes[2] = []
120
121 # encode planes
122
123 imagedata = []
124 pixelformat = "EG_EIPIXELMODE"
125
126 if len(planes[0]) > 0 and planes[0] == planes[1] and planes[0] == planes[2]:
127 print " encoding as greyscale"
128 imagedata.extend(encode_plane(planes[0], planenames[4]))
129 pixelformat = pixelformat + "_GRAY"
130
131 elif len(planes[0]) > 0:
132 print " encoding as true color"
133 imagedata.extend(encode_plane(planes[0], planenames[0]))
134 imagedata.extend(encode_plane(planes[1], planenames[1]))
135 imagedata.extend(encode_plane(planes[2], planenames[2]))
136 pixelformat = pixelformat + "_COLOR"
137
138 if len(planes[3]) > 0:
139 if reduce(lambda x,y: x+y, planes[3]) == 0:
140 print " skipping alpha plane because it is empty"
141 else:
142 imagedata.extend(encode_plane(planes[3], planenames[3]))
143 pixelformat = pixelformat + "_ALPHA"
144
145 # generate compilable header file
146
147 output = "static const UINT8 egemb_%s_data[%d] = {\n" % (identname, len(imagedata))
148 for i in range(0, len(imagedata)):
149 output = output + " 0x%02x," % imagedata[i]
150 if (i % 12) == 11:
151 output = output + "\n"
152 output = output + "\n};\n"
153 output = output + "static EG_EMBEDDED_IMAGE egemb_%s = { %d, %d, %s, EG_EICOMPMODE_RLE, egemb_%s_data, %d };\n" % (identname, width, height, pixelformat, identname, len(imagedata))
154
155 f = file("egemb_%s.h" % identname, "w")
156 f.write(output)
157 f.close()
158
159 print "Done!"