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

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

HTML - AddIcon

<head>
    <link rel="shortcut icon" href="img.ico" />
</head>

Show all icons in avkon.mbm

There are 2 type of mbm files. The following code work with
ROM-type (most mbm on Z:\) only.
>>> from appuifw import *
>>> avkon = u'z:\\system\\data\\avkon.mbm'
>>> def showicon(id_list, maskfunc=lambda x:x):
...     if type(id_list) == int:
...         id_list = [id_list] # one item
...     entries = []
...     if type(maskfunc) == dict:  # allow dict as a func
...         func = lambda id: maskfunc.get(id,id)
...     else:
...         func = maskfunc
...     for id in id_list:
...         item = u'%s, %s' % (id, func(id))
...         icon = Icon(avkon, id, func(id))
...         entries.append((item, u'', icon))
...     app.body = Listbox(entries, lambda: None)
...
>>> showicon(range(503))  # all icons, including mask
>>> showicon(range(0,100,2), lambda x:x+1)  # first 50 icons, with their mask brother

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.
>>> 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

Counting number of bitmaps in an mbm file

I continue my hacking on symbian mbm(multi-bitmap) format.
# a helping function
from struct import unpack
def readL(f, pos=None):
    if pos is not None:
        f.seek(pos)
    return unpack('L', f.read(4))[0]

# the real counting function
def mbm_count(mbm_file):
    f = open(mbm_file, 'rb')
    mbm_type = readL(f)
    if mbm_type == 0x10000041:  # mbm on ROM (Z:)
        return readL(f) 
    elif mbm_type == 0x10000037L:  # mbm on file (normal)
        return readL(f, readL(f, 16)) # read at trailer
    else:       # what type is it?
        return None

For example (you may try this on your phone)
>>> mbm_count('z:\\system\\data\\avkon.mbm')
503L
>>> mbm_count('E:\\system\\apps\\FExplorer\\FExplorer.mbm')
19L
>>>

Using Icon as Image

Pys60 1.1.3 have 2 differnt classes for graphics data.
Icon class represents icon (typically from .mbm file)
which can be put in ListBox for selection.
Image class represents a bigger image which can be
drawn upon. These 2 classes can't convert to/from
each other.

So, I create a library that read .mbm file and draw
an Image from the data there. So, you can make
an icon into an image (now only 1-bit icons).
the module can be downloaded from here
http://larndham.net/service/pys60/icon_image.py
from appuifw import *
import icon_image, e32

app.body = c = Canvas()
# choose a bitmap from plenty inside mbm (multi-bitmap)
icon = icon_image('z:\\system\\data\\avkon.mbm', 28) 
c.blit(icon)

e32.ao_sleep(10)  # 10 sec to end program

Look here for more information.
http://discussion.forum.nokia.com/forum/showthread.php?s=&threadid=63608

Using icons in Listbox

pys60 1.1.3 provide some graphics capabilitis.
One of them is using icons in Listbox
(code taken from pys60 API reference)
icon1 = appuifw.Icon(u"z:\\system\\data\\avkon.mbm", 28, 29)
icon2 = appuifw.Icon(u"z:\\system\\data\\avkon.mbm ", 40, 41)
entries = [(u"Signal", icon1),
           (u"Battery", icon2)]
lb = appuifw.Listbox(entries, lbox_observe)

Listbox is one of the 3 types that can be assigned to app.body
(i.e. Text, Listbox, Canvas)
So, to make code above able to run, you must add
appuifw.app.body = lb

and the function lbox_observe that will handle the selection
need to be defined.
« Newer Snippets
Older Snippets »
Showing 1-6 of 6 total  RSS