| 1 | """PixMapWrapper - defines the PixMapWrapper class, which wraps an opaque
|
|---|
| 2 | QuickDraw PixMap data structure in a handy Python class. Also provides
|
|---|
| 3 | methods to convert to/from pixel data (from, e.g., the img module) or a
|
|---|
| 4 | Python Imaging Library Image object.
|
|---|
| 5 |
|
|---|
| 6 | J. Strout <[email protected]> February 1999"""
|
|---|
| 7 |
|
|---|
| 8 | from Carbon import Qd
|
|---|
| 9 | from Carbon import QuickDraw
|
|---|
| 10 | import struct
|
|---|
| 11 | import MacOS
|
|---|
| 12 | import img
|
|---|
| 13 | import imgformat
|
|---|
| 14 |
|
|---|
| 15 | # PixMap data structure element format (as used with struct)
|
|---|
| 16 | _pmElemFormat = {
|
|---|
| 17 | 'baseAddr':'l', # address of pixel data
|
|---|
| 18 | 'rowBytes':'H', # bytes per row, plus 0x8000
|
|---|
| 19 | 'bounds':'hhhh', # coordinates imposed over pixel data
|
|---|
| 20 | 'top':'h',
|
|---|
| 21 | 'left':'h',
|
|---|
| 22 | 'bottom':'h',
|
|---|
| 23 | 'right':'h',
|
|---|
| 24 | 'pmVersion':'h', # flags for Color QuickDraw
|
|---|
| 25 | 'packType':'h', # format of compression algorithm
|
|---|
| 26 | 'packSize':'l', # size after compression
|
|---|
| 27 | 'hRes':'l', # horizontal pixels per inch
|
|---|
| 28 | 'vRes':'l', # vertical pixels per inch
|
|---|
| 29 | 'pixelType':'h', # pixel format
|
|---|
| 30 | 'pixelSize':'h', # bits per pixel
|
|---|
| 31 | 'cmpCount':'h', # color components per pixel
|
|---|
| 32 | 'cmpSize':'h', # bits per component
|
|---|
| 33 | 'planeBytes':'l', # offset in bytes to next plane
|
|---|
| 34 | 'pmTable':'l', # handle to color table
|
|---|
| 35 | 'pmReserved':'l' # reserved for future use
|
|---|
| 36 | }
|
|---|
| 37 |
|
|---|
| 38 | # PixMap data structure element offset
|
|---|
| 39 | _pmElemOffset = {
|
|---|
| 40 | 'baseAddr':0,
|
|---|
| 41 | 'rowBytes':4,
|
|---|
| 42 | 'bounds':6,
|
|---|
| 43 | 'top':6,
|
|---|
| 44 | 'left':8,
|
|---|
| 45 | 'bottom':10,
|
|---|
| 46 | 'right':12,
|
|---|
| 47 | 'pmVersion':14,
|
|---|
| 48 | 'packType':16,
|
|---|
| 49 | 'packSize':18,
|
|---|
| 50 | 'hRes':22,
|
|---|
| 51 | 'vRes':26,
|
|---|
| 52 | 'pixelType':30,
|
|---|
| 53 | 'pixelSize':32,
|
|---|
| 54 | 'cmpCount':34,
|
|---|
| 55 | 'cmpSize':36,
|
|---|
| 56 | 'planeBytes':38,
|
|---|
| 57 | 'pmTable':42,
|
|---|
| 58 | 'pmReserved':46
|
|---|
| 59 | }
|
|---|
| 60 |
|
|---|
| 61 | class PixMapWrapper:
|
|---|
| 62 | """PixMapWrapper -- wraps the QD PixMap object in a Python class,
|
|---|
| 63 | with methods to easily get/set various pixmap fields. Note: Use the
|
|---|
| 64 | PixMap() method when passing to QD calls."""
|
|---|
| 65 |
|
|---|
| 66 | def __init__(self):
|
|---|
| 67 | self.__dict__['data'] = ''
|
|---|
| 68 | self._header = struct.pack("lhhhhhhhlllhhhhlll",
|
|---|
| 69 | id(self.data)+MacOS.string_id_to_buffer,
|
|---|
| 70 | 0, # rowBytes
|
|---|
| 71 | 0, 0, 0, 0, # bounds
|
|---|
| 72 | 0, # pmVersion
|
|---|
| 73 | 0, 0, # packType, packSize
|
|---|
| 74 | 72<<16, 72<<16, # hRes, vRes
|
|---|
| 75 | QuickDraw.RGBDirect, # pixelType
|
|---|
| 76 | 16, # pixelSize
|
|---|
| 77 | 2, 5, # cmpCount, cmpSize,
|
|---|
| 78 | 0, 0, 0) # planeBytes, pmTable, pmReserved
|
|---|
| 79 | self.__dict__['_pm'] = Qd.RawBitMap(self._header)
|
|---|
| 80 |
|
|---|
| 81 | def _stuff(self, element, bytes):
|
|---|
| 82 | offset = _pmElemOffset[element]
|
|---|
| 83 | fmt = _pmElemFormat[element]
|
|---|
| 84 | self._header = self._header[:offset] \
|
|---|
| 85 | + struct.pack(fmt, bytes) \
|
|---|
| 86 | + self._header[offset + struct.calcsize(fmt):]
|
|---|
| 87 | self.__dict__['_pm'] = None
|
|---|
| 88 |
|
|---|
| 89 | def _unstuff(self, element):
|
|---|
| 90 | offset = _pmElemOffset[element]
|
|---|
| 91 | fmt = _pmElemFormat[element]
|
|---|
| 92 | return struct.unpack(fmt, self._header[offset:offset+struct.calcsize(fmt)])[0]
|
|---|
| 93 |
|
|---|
| 94 | def __setattr__(self, attr, val):
|
|---|
| 95 | if attr == 'baseAddr':
|
|---|
| 96 | raise 'UseErr', "don't assign to .baseAddr -- assign to .data instead"
|
|---|
| 97 | elif attr == 'data':
|
|---|
| 98 | self.__dict__['data'] = val
|
|---|
| 99 | self._stuff('baseAddr', id(self.data) + MacOS.string_id_to_buffer)
|
|---|
| 100 | elif attr == 'rowBytes':
|
|---|
| 101 | # high bit is always set for some odd reason
|
|---|
| 102 | self._stuff('rowBytes', val | 0x8000)
|
|---|
| 103 | elif attr == 'bounds':
|
|---|
| 104 | # assume val is in official Left, Top, Right, Bottom order!
|
|---|
| 105 | self._stuff('left',val[0])
|
|---|
| 106 | self._stuff('top',val[1])
|
|---|
| 107 | self._stuff('right',val[2])
|
|---|
| 108 | self._stuff('bottom',val[3])
|
|---|
| 109 | elif attr == 'hRes' or attr == 'vRes':
|
|---|
| 110 | # 16.16 fixed format, so just shift 16 bits
|
|---|
| 111 | self._stuff(attr, int(val) << 16)
|
|---|
| 112 | elif attr in _pmElemFormat.keys():
|
|---|
| 113 | # any other pm attribute -- just stuff
|
|---|
| 114 | self._stuff(attr, val)
|
|---|
|
|---|