move sections
[python/dscho.git] / Mac / Demo / imgbrowse / mac_image.py
blobb6fb274427edd92edc394f6095d81d0b072a8b70
1 """mac_image - Helper routines (hacks) for images"""
2 import imgformat
3 from Carbon import Qd
4 import struct
5 import MacOS
7 _fmt_to_mac = {
8 imgformat.macrgb16 : (16, 16, 3, 5),
11 def mkpixmap(w, h, fmt, data):
12 """kludge a pixmap together"""
13 fmtinfo = _fmt_to_mac[fmt]
15 rv = struct.pack("lHhhhhhhlllhhhhlll",
16 id(data)+MacOS.string_id_to_buffer, # HACK HACK!!
17 w*2 + 0x8000,
18 0, 0, h, w,
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)
29 def 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