PageRenderTime 34ms CodeModel.GetById 11ms app.highlight 19ms RepoModel.GetById 1ms app.codeStats 0ms

/Mac/Demo/imgbrowse/mac_image.py

http://unladen-swallow.googlecode.com/
Python | 55 lines | 47 code | 4 blank | 4 comment | 5 complexity | 129b23625d35f6b2e567a074e744c861 MD5 | raw file
 1"""mac_image - Helper routines (hacks) for images"""
 2import imgformat
 3from Carbon import Qd
 4import struct
 5import MacOS
 6
 7_fmt_to_mac = {
 8        imgformat.macrgb16 : (16, 16, 3, 5),
 9}
10
11def mkpixmap(w, h, fmt, data):
12    """kludge a pixmap together"""
13    fmtinfo = _fmt_to_mac[fmt]
14
15    rv = struct.pack("lHhhhhhhlllhhhhlll",
16            id(data)+MacOS.string_id_to_buffer, # HACK HACK!!
17            w*2 + 0x8000,
18            0, 0, h, w,
19            0,
20            0, 0, # XXXX?
21            72<<16, 72<<16,
22            fmtinfo[0], fmtinfo[1],
23            fmtinfo[2], fmtinfo[3],
24            0, 0, 0)
25##      print 'Our pixmap, size %d:'%len(rv)
26##      dumppixmap(rv)
27    return Qd.RawBitMap(rv)
28
29def dumppixmap(data):
30    baseAddr, \
31            rowBytes, \
32            t, l, b, r, \
33            pmVersion, \
34            packType, packSize, \
35            hRes, vRes, \
36            pixelType, pixelSize, \
37            cmpCount, cmpSize, \
38            planeBytes, pmTable, pmReserved \
39                    = struct.unpack("lhhhhhhhlllhhhhlll", data)
40    print 'Base:       0x%x'%baseAddr
41    print 'rowBytes:   %d (0x%x)'%(rowBytes&0x3fff, rowBytes)
42    print 'rect:       %d, %d, %d, %d'%(t, l, b, r)
43    print 'pmVersion:  0x%x'%pmVersion
44    print 'packing:    %d %d'%(packType, packSize)
45    print 'resolution: %f x %f'%(float(hRes)/0x10000, float(vRes)/0x10000)
46    print 'pixeltype:  %d, size %d'%(pixelType, pixelSize)
47    print 'components: %d, size %d'%(cmpCount, cmpSize)
48    print 'planeBytes: %d (0x%x)'%(planeBytes, planeBytes)
49    print 'pmTable:    0x%x'%pmTable
50    print 'pmReserved: 0x%x'%pmReserved
51    for i in range(0, len(data), 16):
52        for j in range(16):
53            if i + j < len(data):
54                print '%02.2x'%ord(data[i+j]),
55        print