]>
code.delx.au - refind/blob - images/mkegemb.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
, 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)
61 print "mkegemb 0.1, Copyright (c) 2006 Christoph Pfisterer"
63 planenames
= ( "blue", "green", "red", "alpha", "grey" )
65 for filename
in sys
.argv
[1:]:
67 origimage
= Image
.open(filename
)
69 (width
, height
) = origimage
.size
71 data
= origimage
.getdata()
73 print "%s: %d x %d %s" % (filename
, width
, height
, mode
)
75 (basename
, extension
) = os
.path
.splitext(filename
)
76 identname
= basename
.replace("-", "_")
78 # extract image data from PIL object
80 planes
= [ [], [], [], [] ]
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])
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])
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
)
105 print " Error: Mode not supported!"
108 # special treatment for fonts
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!"
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])
124 pixelformat
= "EG_EIPIXELMODE"
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"
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"
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"
142 imagedata
.extend(encode_plane(planes
[3], planenames
[3]))
143 pixelformat
= pixelformat
+ "_ALPHA"
145 # generate compilable header file
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
]
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
))
155 f
= file("egemb_%s.h" % identname
, "w")