home *** CD-ROM | disk | FTP | other *** search
/ MacHack 2000 / MacHack 2000.toast / pc / The Hacks / MacHacksBug / Python 1.5.2c1 / Mac / Demo / imgbrowse / mac_image.py < prev   
Encoding:
Python Source  |  2000-06-23  |  1.5 KB  |  57 lines

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