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

About this user

Korakot Chaovavanich http://korakot.stumbleupon.com

« Newer Snippets
Older Snippets »
Showing 1-1 of 1 total  RSS 

Reading a 24-bit icon in an mbm file

Here's an example how I read the icon and display it on canvas
It's a continued part of my previous mbm hack.
   1  
   2  >>> from appuifw import *
   3  >>> from struct import unpack
   4  >>> def readL(f, pos=None):     # helping function
   5  ...     if pos is not None:
   6  ...         f.seek(pos)
   7  ...     return unpack('L', f.read(4))[0]
   8  ...
   9  >>> fxmbm = 'E:\\system\\apps\\FExplorer\\FExplorer.mbm'
  10  >>> f = open(fxmbm, 'rb')
  11  >>> trailer = readL(f, 16)
  12  >>> num = readL(f, trailer)   # 19 icons
  13  >>> offset = []
  14  >>> for i in range(num):
  15  ...   offset.append(readL(f))
  16  ...
  17  >>> offset
  18  [20L, 68L, 116L, 474L, 588L, 1038L, 1228L, 1636L, 1864L, 2277L, 2430L, 3735L, 41
  19  69L, 4569L, 4697L, 5197L, 5370L, 5897L, 6062L]
  20  >>> start = offset[2]  # folder icon
  21  >>> f.seek(start)
  22  >>> length = readL(f) - readL(f)    # length of data section
  23  >>> width, height = readL(f), readL(f)  # 16 x 13
  24  >>> f.seek(start+0x28)  # start of data section
  25  >>> data_enc = f.read(length)   # got the data
  26  >>> def rle24_decode(bytes):
  27  ...     out = []
  28  ...     i = 0
  29  ...     while i < len(bytes):
  30  ...         n = ord(bytes[i])
  31  ...         i += 1
  32  ...         if n < 0x80:
  33  ...             out.append( bytes[i:i+3] * (n+1) )
  34  ...             i += 3
  35  ...         else:
  36  ...             n = 0x100 - n
  37  ...             out.append( bytes[i:i+3*n] )
  38  ...             i += 3*n
  39  ...     return ''.join(out)
  40  ...
  41  >>> data = rel24_decode(data_enc)
  42  >>> app.body = canvas = Canvas()
  43  >>> for j in range(height):
  44  ...     for i in range(width):
  45  ...         p = 3*(j*width+i)
  46  ...         color = [ord(data[p+k]) for k in (2,1,0)]  # It's BGR
  47  ...         canvas.point((i,j), tuple(color))
  48  ...
  49  >>>  # folder icon is drawn on screen
« Newer Snippets
Older Snippets »
Showing 1-1 of 1 total  RSS