File: mkegemb.py

package info (click to toggle)
refit 0.7-3
  • links: PTS
  • area: main
  • in suites: etch, etch-m68k
  • size: 580 kB
  • ctags: 688
  • sloc: ansic: 5,556; python: 167; sh: 107; makefile: 64
file content (159 lines) | stat: -rw-r--r-- 5,031 bytes parent folder | download | duplicates (8)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
#!/usr/bin/env python

import sys, os.path
import Image

def enc_backbuffer(backbuffer):
    """Helper function for RLE compression, encodes a string of uncompressable data."""
    compdata = []
    if len(backbuffer) == 0:
        return compdata
    while len(backbuffer) > 128:
        compdata.append(127)
        compdata.extend(backbuffer[0:128])
        backbuffer = backbuffer[128:]
    compdata.append(len(backbuffer)-1)
    compdata.extend(backbuffer)
    return compdata

def compress_rle(rawdata):
    """Compresses the string using a RLE scheme."""
    compdata = []
    backbuffer = []
    
    while len(rawdata) >= 3:
        c = rawdata[0]
        if rawdata[1] == c and rawdata[2] == c:
            runlength = 3
            while runlength < 130 and len(rawdata) > runlength:
                if rawdata[runlength] == c:
                    runlength = runlength + 1
                else:
                    break
            compdata.extend(enc_backbuffer(backbuffer))
            backbuffer = []
            compdata.append(runlength + 125)
            compdata.append(c)
            rawdata = rawdata[runlength:]
        
        else:
            backbuffer.append(c)
            rawdata = rawdata[1:]
    
    backbuffer.extend(rawdata)
    compdata.extend(enc_backbuffer(backbuffer))
    
    return compdata

def encode_plane(rawdata, planename):
    """Encodes the data of a single plane."""
    
    rawlen = len(rawdata)
    compdata = compress_rle(rawdata)
    complen = len(compdata)
    print "  plane %s: compressed %d to %d (%.1f%%)" % (planename, rawlen, complen, float(complen) / float(rawlen) * 100.0)
    
    return compdata


### main loop

print "mkegemb 0.1, Copyright (c) 2006 Christoph Pfisterer"

planenames = ( "blue", "green", "red", "alpha", "grey" )

for filename in sys.argv[1:]:
    
    origimage = Image.open(filename)
    
    (width, height) = origimage.size
    mode = origimage.mode
    data = origimage.getdata()
    
    print "%s: %d x %d %s" % (filename, width, height, mode)
    
    (basename, extension) = os.path.splitext(filename)
    identname = basename.replace("-", "_")
    
    # extract image data from PIL object
    
    planes = [ [], [], [], [] ]
    
    if mode == "RGB":
        for pixcount in range(0, width*height):
            pixeldata = data[pixcount]
            planes[0].append(pixeldata[0])
            planes[1].append(pixeldata[1])
            planes[2].append(pixeldata[2])
    
    elif mode == "RGBA":
        for pixcount in range(0, width*height):
            pixeldata = data[pixcount]
            planes[0].append(pixeldata[0])
            planes[1].append(pixeldata[1])
            planes[2].append(pixeldata[2])
            planes[3].append(pixeldata[3])
    
    elif mode == "L":
        for pixcount in range(0, width*height):
            pixeldata = data[pixcount]
            planes[0].append(pixeldata)
            planes[1].append(pixeldata)
            planes[2].append(pixeldata)
    
    else:
        print " Error: Mode not supported!"
        continue
    
    # special treatment for fonts
    
    if basename[0:4] == "font":
        if planes[0] != planes[1] or planes[0] != planes[2]:
            print " Error: Font detected, but it is not greyscale!"
            continue
        print " font detected, encoding as alpha-only"
        # invert greyscale values for use as alpha
        planes[3] = map(lambda x: 255-x, planes[0])
        planes[0] = []
        planes[1] = []
        planes[2] = []
    
    # encode planes
    
    imagedata = []
    pixelformat = "EG_EIPIXELMODE"
    
    if len(planes[0]) > 0 and planes[0] == planes[1] and planes[0] == planes[2]:
        print " encoding as greyscale"
        imagedata.extend(encode_plane(planes[0], planenames[4]))
        pixelformat = pixelformat + "_GRAY"
    
    elif len(planes[0]) > 0:
        print " encoding as true color"
        imagedata.extend(encode_plane(planes[0], planenames[0]))
        imagedata.extend(encode_plane(planes[1], planenames[1]))
        imagedata.extend(encode_plane(planes[2], planenames[2]))
        pixelformat = pixelformat + "_COLOR"
    
    if len(planes[3]) > 0:
        if reduce(lambda x,y: x+y, planes[3]) == 0:
            print " skipping alpha plane because it is empty"
        else:
            imagedata.extend(encode_plane(planes[3], planenames[3]))
            pixelformat = pixelformat + "_ALPHA"
    
    # generate compilable header file
    
    output = "static const UINT8 egemb_%s_data[%d] = {\n" % (identname, len(imagedata))
    for i in range(0, len(imagedata)):
        output = output + " 0x%02x," % imagedata[i]
        if (i % 12) == 11:
            output = output + "\n"
    output = output + "\n};\n"
    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))
    
    f = file("egemb_%s.h" % identname, "w")
    f.write(output)
    f.close()

print "Done!"