annotate Mac/Lib/PixMapWrapper.py @ 25838:8540887bcec6 2.2

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