annotate Lib/plat-mac/PixMapWrapper.py @ 79675:c0a2294de787 2.7

Issue #16199: Backport 2to3 typo fix from 3.2.
author Chris Jerdonek <chris.jerdonek@gmail.com>
date Thu, 11 Oct 2012 16:15:46 -0700
parents e7a6a4515ab0
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
46919
d7c207b80c40 Revert commit r63086.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 46904
diff changeset
1 """PixMapWrapper - defines the PixMapWrapper class, which wraps an opaque
d7c207b80c40 Revert commit r63086.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 46904
diff changeset
2 QuickDraw PixMap data structure in a handy Python class. Also provides
d7c207b80c40 Revert commit r63086.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 46904
diff changeset
3 methods to convert to/from pixel data (from, e.g., the img module) or a
d7c207b80c40 Revert commit r63086.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 46904
diff changeset
4 Python Imaging Library Image object.
d7c207b80c40 Revert commit r63086.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 46904
diff changeset
5
d7c207b80c40 Revert commit r63086.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 46904
diff changeset
6 J. Strout <joe@strout.net> February 1999"""
d7c207b80c40 Revert commit r63086.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 46904
diff changeset
7
46967
6df0394abbf1 Add warnings to and deprecated all those Mac modules
Benjamin Peterson <benjamin@python.org>
parents: 46919
diff changeset
8
6df0394abbf1 Add warnings to and deprecated all those Mac modules
Benjamin Peterson <benjamin@python.org>
parents: 46919
diff changeset
9 from warnings import warnpy3k
47958
e7a6a4515ab0 fix test_py3kwarns
Benjamin Peterson <benjamin@python.org>
parents: 46967
diff changeset
10 warnpy3k("In 3.x, the PixMapWrapper module is removed.", stacklevel=2)
46967
6df0394abbf1 Add warnings to and deprecated all those Mac modules
Benjamin Peterson <benjamin@python.org>
parents: 46919
diff changeset
11
46919
d7c207b80c40 Revert commit r63086.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 46904
diff changeset
12 from Carbon import Qd
d7c207b80c40 Revert commit r63086.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 46904
diff changeset
13 from Carbon import QuickDraw
d7c207b80c40 Revert commit r63086.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 46904
diff changeset
14 import struct
d7c207b80c40 Revert commit r63086.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 46904
diff changeset
15 import MacOS
d7c207b80c40 Revert commit r63086.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 46904
diff changeset
16 import img
d7c207b80c40 Revert commit r63086.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 46904
diff changeset
17 import imgformat
d7c207b80c40 Revert commit r63086.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 46904
diff changeset
18
d7c207b80c40 Revert commit r63086.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 46904
diff changeset
19 # PixMap data structure element format (as used with struct)
d7c207b80c40 Revert commit r63086.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 46904
diff changeset
20 _pmElemFormat = {
d7c207b80c40 Revert commit r63086.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 46904
diff changeset
21 'baseAddr':'l', # address of pixel data
d7c207b80c40 Revert commit r63086.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 46904
diff changeset
22 'rowBytes':'H', # bytes per row, plus 0x8000
d7c207b80c40 Revert commit r63086.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 46904
diff changeset
23 'bounds':'hhhh', # coordinates imposed over pixel data
d7c207b80c40 Revert commit r63086.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 46904
diff changeset
24 'top':'h',
d7c207b80c40 Revert commit r63086.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 46904
diff changeset
25 'left':'h',
d7c207b80c40 Revert commit r63086.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 46904
diff changeset
26 'bottom':'h',
d7c207b80c40 Revert commit r63086.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 46904
diff changeset
27 'right':'h',
d7c207b80c40 Revert commit r63086.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 46904
diff changeset
28 'pmVersion':'h', # flags for Color QuickDraw
d7c207b80c40 Revert commit r63086.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 46904
diff changeset
29 'packType':'h', # format of compression algorithm
d7c207b80c40 Revert commit r63086.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 46904
diff changeset
30 'packSize':'l', # size after compression
d7c207b80c40 Revert commit r63086.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 46904
diff changeset
31 'hRes':'l', # horizontal pixels per inch
d7c207b80c40 Revert commit r63086.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 46904
diff changeset
32 'vRes':'l', # vertical pixels per inch
d7c207b80c40 Revert commit r63086.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 46904
diff changeset
33 'pixelType':'h', # pixel format
d7c207b80c40 Revert commit r63086.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 46904
diff changeset
34 'pixelSize':'h', # bits per pixel
d7c207b80c40 Revert commit r63086.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 46904
diff changeset
35 'cmpCount':'h', # color components per pixel
d7c207b80c40 Revert commit r63086.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 46904
diff changeset
36 'cmpSize':'h', # bits per component
d7c207b80c40 Revert commit r63086.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 46904
diff changeset
37 'planeBytes':'l', # offset in bytes to next plane
d7c207b80c40 Revert commit r63086.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 46904
diff changeset
38 'pmTable':'l', # handle to color table
d7c207b80c40 Revert commit r63086.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 46904
diff changeset
39 'pmReserved':'l' # reserved for future use
d7c207b80c40 Revert commit r63086.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 46904
diff changeset
40 }
d7c207b80c40 Revert commit r63086.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 46904
diff changeset
41
d7c207b80c40 Revert commit r63086.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 46904
diff changeset
42 # PixMap data structure element offset
d7c207b80c40 Revert commit r63086.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 46904
diff changeset
43 _pmElemOffset = {
d7c207b80c40 Revert commit r63086.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 46904
diff changeset
44 'baseAddr':0,
d7c207b80c40 Revert commit r63086.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 46904
diff changeset
45 'rowBytes':4,
d7c207b80c40 Revert commit r63086.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 46904
diff changeset
46 'bounds':6,
d7c207b80c40 Revert commit r63086.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 46904
diff changeset
47 'top':6,
d7c207b80c40 Revert commit r63086.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 46904
diff changeset
48 'left':8,
d7c207b80c40 Revert commit r63086.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 46904
diff changeset
49 'bottom':10,
d7c207b80c40 Revert commit r63086.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 46904
diff changeset
50 'right':12,
d7c207b80c40 Revert commit r63086.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 46904
diff changeset
51 'pmVersion':14,
d7c207b80c40 Revert commit r63086.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 46904
diff changeset
52 'packType':16,
d7c207b80c40 Revert commit r63086.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 46904
diff changeset
53 'packSize':18,
d7c207b80c40 Revert commit r63086.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 46904
diff changeset
54 'hRes':22,
d7c207b80c40 Revert commit r63086.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 46904
diff changeset
55 'vRes':26,
d7c207b80c40 Revert commit r63086.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 46904
diff changeset
56 'pixelType':30,
d7c207b80c40 Revert commit r63086.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 46904
diff changeset
57 'pixelSize':32,
d7c207b80c40 Revert commit r63086.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 46904
diff changeset
58 'cmpCount':34,
d7c207b80c40 Revert commit r63086.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 46904
diff changeset
59 'cmpSize':36,
d7c207b80c40 Revert commit r63086.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 46904
diff changeset
60 'planeBytes':38,
d7c207b80c40 Revert commit r63086.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 46904
diff changeset
61 'pmTable':42,
d7c207b80c40 Revert commit r63086.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 46904
diff changeset
62 'pmReserved':46
d7c207b80c40 Revert commit r63086.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 46904
diff changeset
63 }
d7c207b80c40 Revert commit r63086.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 46904
diff changeset
64
d7c207b80c40 Revert commit r63086.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 46904
diff changeset
65 class PixMapWrapper:
d7c207b80c40 Revert commit r63086.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 46904
diff changeset
66 """PixMapWrapper -- wraps the QD PixMap object in a Python class,
d7c207b80c40 Revert commit r63086.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 46904
diff changeset
67 with methods to easily get/set various pixmap fields. Note: Use the
d7c207b80c40 Revert commit r63086.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 46904
diff changeset
68 PixMap() method when passing to QD calls."""
d7c207b80c40 Revert commit r63086.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 46904
diff changeset
69
d7c207b80c40 Revert commit r63086.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 46904
diff changeset
70 def __init__(self):
d7c207b80c40 Revert commit r63086.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 46904
diff changeset
71 self.__dict__['data'] = ''
d7c207b80c40 Revert commit r63086.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 46904
diff changeset
72 self._header = struct.pack("lhhhhhhhlllhhhhlll",
d7c207b80c40 Revert commit r63086.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 46904
diff changeset
73 id(self.data)+MacOS.string_id_to_buffer,
d7c207b80c40 Revert commit r63086.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 46904
diff changeset
74 0, # rowBytes
d7c207b80c40 Revert commit r63086.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 46904
diff changeset
75 0, 0, 0, 0, # bounds
d7c207b80c40 Revert commit r63086.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 46904
diff changeset
76 0, # pmVersion
d7c207b80c40 Revert commit r63086.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 46904
diff changeset
77 0, 0, # packType, packSize
d7c207b80c40 Revert commit r63086.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 46904
diff changeset
78 72<<16, 72<<16, # hRes, vRes
d7c207b80c40 Revert commit r63086.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 46904
diff changeset
79 QuickDraw.RGBDirect, # pixelType
d7c207b80c40 Revert commit r63086.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 46904
diff changeset
80 16, # pixelSize
d7c207b80c40 Revert commit r63086.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 46904
diff changeset
81 2, 5, # cmpCount, cmpSize,
d7c207b80c40 Revert commit r63086.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 46904
diff changeset
82 0, 0, 0) # planeBytes, pmTable, pmReserved
d7c207b80c40 Revert commit r63086.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 46904
diff changeset
83 self.__dict__['_pm'] = Qd.RawBitMap(self._header)
d7c207b80c40 Revert commit r63086.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 46904
diff changeset
84
d7c207b80c40 Revert commit r63086.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 46904
diff changeset
85 def _stuff(self, element, bytes):
d7c207b80c40 Revert commit r63086.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 46904
diff changeset
86 offset = _pmElemOffset[element]
d7c207b80c40 Revert commit r63086.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 46904
diff changeset
87 fmt = _pmElemFormat[element]
d7c207b80c40 Revert commit r63086.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 46904
diff changeset
88 self._header = self._header[:offset] \
d7c207b80c40 Revert commit r63086.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 46904
diff changeset
89 + struct.pack(fmt, bytes) \
d7c207b80c40 Revert commit r63086.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 46904
diff changeset
90 + self._header[offset + struct.calcsize(fmt):]
d7c207b80c40 Revert commit r63086.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 46904
diff changeset
91 self.__dict__['_pm'] = None
d7c207b80c40 Revert commit r63086.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 46904
diff changeset
92
d7c207b80c40 Revert commit r63086.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 46904
diff changeset
93 def _unstuff(self, element):
d7c207b80c40 Revert commit r63086.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 46904
diff changeset
94 offset = _pmElemOffset[element]
d7c207b80c40 Revert commit r63086.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 46904
diff changeset
95 fmt = _pmElemFormat[element]
d7c207b80c40 Revert commit r63086.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 46904
diff changeset
96 return struct.unpack(fmt, self._header[offset:offset+struct.calcsize(fmt)])[0]
26884
f0f9acca4164 Moved most of Mac/Lib hierarchy to Lib/plat-mac: it can be used both
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff changeset
97
46919
d7c207b80c40 Revert commit r63086.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 46904
diff changeset
98 def __setattr__(self, attr, val):
d7c207b80c40 Revert commit r63086.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 46904
diff changeset
99 if attr == 'baseAddr':
d7c207b80c40 Revert commit r63086.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 46904
diff changeset
100 raise 'UseErr', "don't assign to .baseAddr -- assign to .data instead"
d7c207b80c40 Revert commit r63086.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 46904
diff changeset
101 elif attr == 'data':
d7c207b80c40 Revert commit r63086.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 46904
diff changeset
102 self.__dict__['data'] = val
d7c207b80c40 Revert commit r63086.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 46904
diff changeset
103 self._stuff('baseAddr', id(self.data) + MacOS.string_id_to_buffer)
d7c207b80c40 Revert commit r63086.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 46904
diff changeset
104 elif attr == 'rowBytes':
d7c207b80c40 Revert commit r63086.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 46904
diff changeset
105 # high bit is always set for some odd reason
d7c207b80c40 Revert commit r63086.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 46904
diff changeset
106 self._stuff('rowBytes', val | 0x8000)
d7c207b80c40 Revert commit r63086.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 46904
diff changeset
107 elif attr == 'bounds':
d7c207b80c40 Revert commit r63086.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 46904
diff changeset
108 # assume val is in official Left, Top, Right, Bottom order!
d7c207b80c40 Revert commit r63086.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 46904
diff changeset
109 self._stuff('left',val[0])
d7c207b80c40 Revert commit r63086.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 46904
diff changeset
110 self._stuff('top',val[1])
d7c207b80c40 Revert commit r63086.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 46904
diff changeset
111 self._stuff('right',val[2])
d7c207b80c40 Revert commit r63086.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 46904
diff changeset
112 self._stuff('bottom',val[3])
d7c207b80c40 Revert commit r63086.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 46904
diff changeset
113 elif attr == 'hRes' or attr == 'vRes':
d7c207b80c40 Revert commit r63086.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 46904
diff changeset
114 # 16.16 fixed format, so just shift 16 bits
d7c207b80c40 Revert commit r63086.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 46904
diff changeset
115 self._stuff(attr, int(val) << 16)
d7c207b80c40 Revert commit r63086.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 46904
diff changeset
116 elif attr in _pmElemFormat.keys():
d7c207b80c40 Revert commit r63086.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 46904
diff changeset
117 # any other pm attribute -- just stuff
d7c207b80c40 Revert commit r63086.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 46904
diff changeset
118 self._stuff(attr, val)
d7c207b80c40 Revert commit r63086.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 46904
diff changeset
119 else:
d7c207b80c40 Revert commit r63086.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 46904
diff changeset
120 self.__dict__[attr] = val
d7c207b80c40 Revert commit r63086.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 46904
diff changeset
121
d7c207b80c40 Revert commit r63086.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 46904
diff changeset
122 def __getattr__(self, attr):
d7c207b80c40 Revert commit r63086.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 46904
diff changeset
123 if attr == 'rowBytes':
d7c207b80c40 Revert commit r63086.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 46904
diff changeset
124 # high bit is always set for some odd reason
d7c207b80c40 Revert commit r63086.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 46904
diff changeset
125 return self._unstuff('rowBytes') & 0x7FFF
d7c207b80c40 Revert commit r63086.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 46904
diff changeset
126 elif attr == 'bounds':
d7c207b80c40 Revert commit r63086.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 46904
diff changeset
127 # return bounds in official Left, Top, Right, Bottom order!
d7c207b80c40 Revert commit r63086.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 46904
diff changeset
128 return ( \
d7c207b80c40 Revert commit r63086.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 46904
diff changeset
129 self._unstuff('left'),
d7c207b80c40 Revert commit r63086.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 46904
diff changeset
130 self._unstuff('top'),
d7c207b80c40 Revert commit r63086.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 46904
diff changeset
131 self._unstuff('right'),
d7c207b80c40 Revert commit r63086.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 46904
diff changeset
132 self._unstuff('bottom') )
d7c207b80c40 Revert commit r63086.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 46904
diff changeset
133 elif attr == 'hRes' or attr == 'vRes':
d7c207b80c40 Revert commit r63086.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 46904
diff changeset
134 # 16.16 fixed format, so just shift 16 bits
d7c207b80c40 Revert commit r63086.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 46904
diff changeset
135 return self._unstuff(attr) >> 16
d7c207b80c40 Revert commit r63086.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 46904
diff changeset
136 elif attr in _pmElemFormat.keys():
d7c207b80c40 Revert commit r63086.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 46904
diff changeset
137 # any other pm attribute -- just unstuff
d7c207b80c40 Revert commit r63086.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 46904
diff changeset
138 return self._unstuff(attr)
d7c207b80c40 Revert commit r63086.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 46904
diff changeset
139 else:
d7c207b80c40 Revert commit r63086.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 46904
diff changeset
140 return self.__dict__[attr]
d7c207b80c40 Revert commit r63086.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 46904
diff changeset
141
d7c207b80c40 Revert commit r63086.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 46904
diff changeset
142
d7c207b80c40 Revert commit r63086.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 46904
diff changeset
143 def PixMap(self):
d7c207b80c40 Revert commit r63086.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 46904
diff changeset
144 "Return a QuickDraw PixMap corresponding to this data."
d7c207b80c40 Revert commit r63086.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 46904
diff changeset
145 if not self.__dict__['_pm']:
d7c207b80c40 Revert commit r63086.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 46904
diff changeset
146 self.__dict__['_pm'] = Qd.RawBitMap(self._header)
d7c207b80c40 Revert commit r63086.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 46904
diff changeset
147 return self.__dict__['_pm']
26884
f0f9acca4164 Moved most of Mac/Lib hierarchy to Lib/plat-mac: it can be used both
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff changeset
148
46919
d7c207b80c40 Revert commit r63086.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 46904
diff changeset
149 def blit(self, x1=0,y1=0,x2=None,y2=None, port=None):
d7c207b80c40 Revert commit r63086.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 46904
diff changeset
150 """Draw this pixmap into the given (default current) grafport."""
d7c207b80c40 Revert commit r63086.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 46904
diff changeset
151 src = self.bounds
d7c207b80c40 Revert commit r63086.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 46904
diff changeset
152 dest = [x1,y1,x2,y2]
d7c207b80c40 Revert commit r63086.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 46904
diff changeset
153 if x2 is None:
d7c207b80c40 Revert commit r63086.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 46904
diff changeset
154 dest[2] = x1 + src[2]-src[0]
d7c207b80c40 Revert commit r63086.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 46904
diff changeset
155 if y2 is None:
d7c207b80c40 Revert commit r63086.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 46904
diff changeset
156 dest[3] = y1 + src[3]-src[1]
d7c207b80c40 Revert commit r63086.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 46904
diff changeset
157 if not port: port = Qd.GetPort()
d7c207b80c40 Revert commit r63086.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 46904
diff changeset
158 Qd.CopyBits(self.PixMap(), port.GetPortBitMapForCopyBits(), src, tuple(dest),
d7c207b80c40 Revert commit r63086.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 46904
diff changeset
159 QuickDraw.srcCopy, None)
d7c207b80c40 Revert commit r63086.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 46904
diff changeset
160
d7c207b80c40 Revert commit r63086.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 46904
diff changeset
161 def fromstring(self,s,width,height,format=imgformat.macrgb):
d7c207b80c40 Revert commit r63086.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 46904
diff changeset
162 """Stuff this pixmap with raw pixel data from a string.
d7c207b80c40 Revert commit r63086.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 46904
diff changeset
163 Supply width, height, and one of the imgformat specifiers."""
d7c207b80c40 Revert commit r63086.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 46904
diff changeset
164 # we only support 16- and 32-bit mac rgb...
d7c207b80c40 Revert commit r63086.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 46904
diff changeset
165 # so convert if necessary
d7c207b80c40 Revert commit r63086.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 46904
diff changeset
166 if format != imgformat.macrgb and format != imgformat.macrgb16:
d7c207b80c40 Revert commit r63086.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 46904
diff changeset
167 # (LATER!)
d7c207b80c40 Revert commit r63086.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 46904
diff changeset
168 raise "NotImplementedError", "conversion to macrgb or macrgb16"
d7c207b80c40 Revert commit r63086.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 46904
diff changeset
169 self.data = s
d7c207b80c40 Revert commit r63086.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 46904
diff changeset
170 self.bounds = (0,0,width,height)
d7c207b80c40 Revert commit r63086.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 46904
diff changeset
171 self.cmpCount = 3
d7c207b80c40 Revert commit r63086.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 46904
diff changeset
172 self.pixelType = QuickDraw.RGBDirect
d7c207b80c40 Revert commit r63086.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 46904
diff changeset
173 if format == imgformat.macrgb:
d7c207b80c40 Revert commit r63086.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 46904
diff changeset
174 self.pixelSize = 32
d7c207b80c40 Revert commit r63086.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 46904
diff changeset
175 self.cmpSize = 8
d7c207b80c40 Revert commit r63086.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 46904
diff changeset
176 else:
d7c207b80c40 Revert commit r63086.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 46904
diff changeset
177 self.pixelSize = 16
d7c207b80c40 Revert commit r63086.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 46904
diff changeset
178 self.cmpSize = 5
d7c207b80c40 Revert commit r63086.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 46904
diff changeset
179 self.rowBytes = width*self.pixelSize/8
d7c207b80c40 Revert commit r63086.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 46904
diff changeset
180
d7c207b80c40 Revert commit r63086.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 46904
diff changeset
181 def tostring(self, format=imgformat.macrgb):
d7c207b80c40 Revert commit r63086.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 46904
diff changeset
182 """Return raw data as a string in the specified format."""
d7c207b80c40 Revert commit r63086.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 46904
diff changeset
183 # is the native format requested? if so, just return data
d7c207b80c40 Revert commit r63086.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 46904
diff changeset
184 if (format == imgformat.macrgb and self.pixelSize == 32) or \
d7c207b80c40 Revert commit r63086.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 46904
diff changeset
185 (format == imgformat.macrgb16 and self.pixelsize == 16):
d7c207b80c40 Revert commit r63086.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 46904
diff changeset
186 return self.data
d7c207b80c40 Revert commit r63086.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 46904
diff changeset
187 # otherwise, convert to the requested format
d7c207b80c40 Revert commit r63086.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 46904
diff changeset
188 # (LATER!)
d7c207b80c40 Revert commit r63086.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 46904
diff changeset
189 raise "NotImplementedError", "data format conversion"
d7c207b80c40 Revert commit r63086.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 46904
diff changeset
190
d7c207b80c40 Revert commit r63086.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 46904
diff changeset
191 def fromImage(self,im):
d7c207b80c40 Revert commit r63086.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 46904
diff changeset
192 """Initialize this PixMap from a PIL Image object."""
d7c207b80c40 Revert commit r63086.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 46904
diff changeset
193 # We need data in ARGB format; PIL can't currently do that,
d7c207b80c40 Revert commit r63086.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 46904
diff changeset
194 # but it can do RGBA, which we can use by inserting one null
d7c207b80c40 Revert commit r63086.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 46904
diff changeset
195 # up frontpm =
d7c207b80c40 Revert commit r63086.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 46904
diff changeset
196 if im.mode != 'RGBA': im = im.convert('RGBA')
d7c207b80c40 Revert commit r63086.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 46904
diff changeset
197 data = chr(0) + im.tostring()
d7c207b80c40 Revert commit r63086.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 46904
diff changeset
198 self.fromstring(data, im.size[0], im.size[1])
d7c207b80c40 Revert commit r63086.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 46904
diff changeset
199
d7c207b80c40 Revert commit r63086.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 46904
diff changeset
200 def toImage(self):
d7c207b80c40 Revert commit r63086.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 46904
diff changeset
201 """Return the contents of this PixMap as a PIL Image object."""
d7c207b80c40 Revert commit r63086.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 46904
diff changeset
202 import Image
d7c207b80c40 Revert commit r63086.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 46904
diff changeset
203 # our tostring() method returns data in ARGB format,
d7c207b80c40 Revert commit r63086.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 46904
diff changeset
204 # whereas Image uses RGBA; a bit of slicing fixes this...
d7c207b80c40 Revert commit r63086.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 46904
diff changeset
205 data = self.tostring()[1:] + chr(0)
d7c207b80c40 Revert commit r63086.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 46904
diff changeset
206 bounds = self.bounds
d7c207b80c40 Revert commit r63086.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 46904
diff changeset
207 return Image.fromstring('RGBA',(bounds[2]-bounds[0],bounds[3]-bounds[1]),data)
d7c207b80c40 Revert commit r63086.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 46904
diff changeset
208
d7c207b80c40 Revert commit r63086.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 46904
diff changeset
209 def test():
d7c207b80c40 Revert commit r63086.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 46904
diff changeset
210 import MacOS
d7c207b80c40 Revert commit r63086.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 46904
diff changeset
211 import EasyDialogs
d7c207b80c40 Revert commit r63086.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 46904
diff changeset
212 import Image
d7c207b80c40 Revert commit r63086.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 46904
diff changeset
213 path = EasyDialogs.AskFileForOpen("Image File:")
d7c207b80c40 Revert commit r63086.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 46904
diff changeset
214 if not path: return
d7c207b80c40 Revert commit r63086.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 46904
diff changeset
215 pm = PixMapWrapper()
d7c207b80c40 Revert commit r63086.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 46904
diff changeset
216 pm.fromImage( Image.open(path) )
d7c207b80c40 Revert commit r63086.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 46904
diff changeset
217 pm.blit(20,20)
d7c207b80c40 Revert commit r63086.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 46904
diff changeset
218 return pm