]>
code.delx.au - refind/blob - images/mkeei.py
6 def enc_backbuffer(backbuffer
):
7 """Helper function for RLE compression, encodes a string of uncompressable data."""
9 if len(backbuffer
) == 0:
11 while len(backbuffer
) > 128:
13 compdata
.extend(backbuffer
[0:128])
14 backbuffer
= backbuffer
[128:]
15 compdata
.append(len(backbuffer
)-1)
16 compdata
.extend(backbuffer
)
19 def compress_rle(rawdata
):
20 """Compresses the string using a RLE scheme."""
24 while len(rawdata
) >= 3:
26 if rawdata
[1] == c
and rawdata
[2] == c
:
28 while runlength
< 130 and len(rawdata
) > runlength
:
29 if rawdata
[runlength
] == c
:
30 runlength
= runlength
+ 1
33 compdata
.extend(enc_backbuffer(backbuffer
))
35 compdata
.append(runlength
+ 125)
37 rawdata
= rawdata
[runlength
:]
43 backbuffer
.extend(rawdata
)
44 compdata
.extend(enc_backbuffer(backbuffer
))
48 def encode_plane(rawdata
, identname
, planename
):
49 """Encodes the data of a single plane."""
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)
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
]
61 output
= output
+ "\n"
65 return (output
, "eei_%s_planedata_%s, %d" % (identname
, planename
, complen
))
70 print "mkeei 0.1, Copyright (c) 2006 Christoph Pfisterer"
72 planenames
= ( "blue", "green", "red", "alpha", "grey" )
74 for filename
in sys
.argv
[1:]:
76 origimage
= Image
.open(filename
)
78 (width
, height
) = origimage
.size
80 data
= origimage
.getdata()
82 print "%s: %d x %d %s" % (filename
, width
, height
, mode
)
84 basename
= filename
[:-4] # TODO!!!!!!
85 identname
= basename
.replace("-", "_")
87 planes
= [ [], [], [], [] ]
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])
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])
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
)
112 print " Error: Mode not supported!"
115 # special treatment for fonts
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!"
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])
128 # generate optimal output
131 planeinfo
= [ "NULL, 0", "NULL, 0", "NULL, 0", "NULL, 0" ]
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]
140 elif len(planes
[0]) > 0:
141 print " encoding as true color"
143 (output_part
, planeinfo
[0]) = encode_plane(planes
[0], identname
, planenames
[0])
144 output
= output
+ output_part
146 if planes
[1] == planes
[0]:
147 print " encoding plane 1 is a copy of plane 0"
148 planeinfo
[1] = planeinfo
[0]
150 (output_part
, planeinfo
[1]) = encode_plane(planes
[1], identname
, planenames
[1])
151 output
= output
+ output_part
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]
160 (output_part
, planeinfo
[2]) = encode_plane(planes
[2], identname
, planenames
[2])
161 output
= output
+ output_part
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"
167 (output_part
, planeinfo
[3]) = encode_plane(planes
[3], identname
, planenames
[3])
168 output
= output
+ output_part
170 output
= output
+ "static EEI_IMAGE eei_%s = { %d, %d, NULL, {\n" % (identname
, width
, height
)
172 output
= output
+ " { %s },\n" % planeinfo
[i
]
173 output
= output
+ "} };\n"
175 f
= file("eei_%s.h" % identname
, "w")