/Mac/Demo/imgbrowse/mac_image.py

http://unladen-swallow.googlecode.com/ · Python · 55 lines · 52 code · 2 blank · 1 comment · 0 complexity · 129b23625d35f6b2e567a074e744c861 MD5 · raw file

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