Never been to DZone Snippets before?

Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world

Reading a 24-bit icon in an mbm file (See related posts)

Here's an example how I read the icon and display it on canvas
It's a continued part of my previous mbm hack.
>>> from appuifw import *
>>> from struct import unpack
>>> def readL(f, pos=None):     # helping function
...     if pos is not None:
...         f.seek(pos)
...     return unpack('L', f.read(4))[0]
...
>>> fxmbm = 'E:\\system\\apps\\FExplorer\\FExplorer.mbm'
>>> f = open(fxmbm, 'rb')
>>> trailer = readL(f, 16)
>>> num = readL(f, trailer)   # 19 icons
>>> offset = []
>>> for i in range(num):
...   offset.append(readL(f))
...
>>> offset
[20L, 68L, 116L, 474L, 588L, 1038L, 1228L, 1636L, 1864L, 2277L, 2430L, 3735L, 41
69L, 4569L, 4697L, 5197L, 5370L, 5897L, 6062L]
>>> start = offset[2]  # folder icon
>>> f.seek(start)
>>> length = readL(f) - readL(f)    # length of data section
>>> width, height = readL(f), readL(f)  # 16 x 13
>>> f.seek(start+0x28)  # start of data section
>>> data_enc = f.read(length)   # got the data
>>> def rle24_decode(bytes):
...     out = []
...     i = 0
...     while i < len(bytes):
...         n = ord(bytes[i])
...         i += 1
...         if n < 0x80:
...             out.append( bytes[i:i+3] * (n+1) )
...             i += 3
...         else:
...             n = 0x100 - n
...             out.append( bytes[i:i+3*n] )
...             i += 3*n
...     return ''.join(out)
...
>>> data = rel24_decode(data_enc)
>>> app.body = canvas = Canvas()
>>> for j in range(height):
...     for i in range(width):
...         p = 3*(j*width+i)
...         color = [ord(data[p+k]) for k in (2,1,0)]  # It's BGR
...         canvas.point((i,j), tuple(color))
...
>>>  # folder icon is drawn on screen

You need to create an account or log in to post comments to this site.


Click here to browse all 5140 code snippets

Related Posts