Module brewcompressedimage
|
|
1
2
3
4
5
6
7
8
9
10
11
12 """Support for the BCI (Brew Compressed Image) format
13
14 Currently this code can read a BCI file. You should call the
15 L{getimage} function.
16
17 """
18
19 """
20 integers are lsb
21
22 0000 - 0003 BCI\0
23 0004 - 0007 ? (x0844 = 2116) [length of file]
24 0008 - 000b ? (x0434 = 1076) [offset to first image
25 000c - 000d ? (1)
26 000e - 000f width
27 0010 - 0011 height
28 0012 - 0013 ? (1) 2
29 0014 - 0015 ? (1) 2
30 0016 - 0017 ? (1) 2
31 0018 - 0019 ? (0)
32 001a - 001b ? (8) [bits per pixel?]
33 001c - 001d ? (1)
34 001e - 001f ? [0x100 = 256 ] # number of entries in palette
35
36 palette
37 b,g,r,0 (32 bit entry)
38
39 next palette
40 0000 - 0001 ? (2) palette number/id
41 0002 - 0003 number of entries in palette
42
43 image
44 0000 - 0001 data length
45 0002 - 0003 ? (0)
46 0004 - 0005 width
47 0006 - 0007 height
48 0008 - 0009 ? (1)
49 000a - 000b ? (1)
50
51 """
52
53 import common
54 import zlib
55 import cStringIO
56
57 import wx
58
60 """Used for the builtin tester"""
61
63 bmp=wx.BitmapFromImage(wx.Image(file))
64
65 wx.Frame.__init__(self, parent, -1, "Image Display")
66
67 b=wx.StaticBitmap(self, -1, bmp)
68
69 b.SetSize((bmp.GetWidth(), bmp.GetHeight()))
70 self.Fit()
71 self.Show(True)
72
73
75 """An encapsulation of the image"""
76
77 - def __init__(self, width, height, bytes, palette):
78 self.width=width
79 self.height=height
80 offset=0
81 import cStringIO
82 data=cStringIO.StringIO()
83 for row in range(height):
84
85
86 while (offset%4)!=0:
87 offset+=1
88 for col in range(width):
89 v=ord(bytes[offset])
90 offset+=1
91 data.write(palette[v])
92
93 self.data=data.getvalue()
94
95
97 """Converts image to wxImage
98
99 @rtype: wxImage
100 """
101 if img is None:
102 img=wx.EmptyImage(self.width, self.height)
103 else:
104 img.Destroy()
105 img.Create(self.width, self.height)
106 img.SetData(self.data)
107 return img
108
110 """An encapsulation of the palette"""
117
120
125
130
133
213
215 """Read binary data in lsb"""
216 res=0
217 shift=0
218 for i in data:
219 res|=ord(i)<<shift
220 shift+=8
221 return res
222
223 BITMAP_TYPE_BCI=wx.BITMAP_TYPE_ANY+1
224
226
228 super(BCIImageHandler, self).__init__()
229 self.SetName("BREW Compressed Image")
230 self.SetExtension("bci")
231 self.SetType(BITMAP_TYPE_BCI)
232 self.SetMimeType("image/x-brewcompressedimage")
233
237
238 - def LoadFile(self, image, stream, verbose, index):
239 try:
240 getimage(stream, image)
241 return True
242 except:
243 return False
244
245 - def SaveFile(self, image, stream, verbose):
246 raise NotImplementedError
247
249
250 return stream.read(4)=='BCI\x00'
251
252 BITMAP_TYPE_LGBIT=BITMAP_TYPE_BCI+1
259
262
263 - def LoadFile(self, image, stream, verbose, index):
265 - def SaveFile(self, image, stream, verbose):
269
270 wx.Image_AddHandler(BCIImageHandler())
271
272 if __name__=='__main__':
273 import sys
274
275 app=wx.PySimpleApp()
276 if len(sys.argv)==2:
277 f=Display(sys.argv[1])
278 elif len(sys.argv)==3:
279 bciconvert(sys.argv[1], sys.argv[2])
280 f=Display(sys.argv[2])
281 else:
282 assert Exception, "not enough params"
283
284 app.MainLoop()
285