home *** CD-ROM | disk | FTP | other *** search
/ Maximum CD 2010 May / maximum-cd-2010-05.iso / DiscContents / boxee-0.9.20.10711.exe / system / python / Lib / plat-mac / PixMapWrapper.pyo (.txt) < prev    next >
Encoding:
Python Compiled Bytecode  |  2009-07-20  |  6.6 KB  |  201 lines

  1. # Source Generated with Decompyle++
  2. # File: in.pyo (Python 2.4)
  3.  
  4. '''PixMapWrapper - defines the PixMapWrapper class, which wraps an opaque
  5. QuickDraw PixMap data structure in a handy Python class.  Also provides
  6. methods to convert to/from pixel data (from, e.g., the img module) or a
  7. Python Imaging Library Image object.
  8.  
  9. J. Strout <joe@strout.net>  February 1999'''
  10. from Carbon import Qd
  11. from Carbon import QuickDraw
  12. import struct
  13. import MacOS
  14. import img
  15. import imgformat
  16. _pmElemFormat = {
  17.     'baseAddr': 'l',
  18.     'rowBytes': 'H',
  19.     'bounds': 'hhhh',
  20.     'top': 'h',
  21.     'left': 'h',
  22.     'bottom': 'h',
  23.     'right': 'h',
  24.     'pmVersion': 'h',
  25.     'packType': 'h',
  26.     'packSize': 'l',
  27.     'hRes': 'l',
  28.     'vRes': 'l',
  29.     'pixelType': 'h',
  30.     'pixelSize': 'h',
  31.     'cmpCount': 'h',
  32.     'cmpSize': 'h',
  33.     'planeBytes': 'l',
  34.     'pmTable': 'l',
  35.     'pmReserved': 'l' }
  36. _pmElemOffset = {
  37.     'baseAddr': 0,
  38.     'rowBytes': 4,
  39.     'bounds': 6,
  40.     'top': 6,
  41.     'left': 8,
  42.     'bottom': 10,
  43.     'right': 12,
  44.     'pmVersion': 14,
  45.     'packType': 16,
  46.     'packSize': 18,
  47.     'hRes': 22,
  48.     'vRes': 26,
  49.     'pixelType': 30,
  50.     'pixelSize': 32,
  51.     'cmpCount': 34,
  52.     'cmpSize': 36,
  53.     'planeBytes': 38,
  54.     'pmTable': 42,
  55.     'pmReserved': 46 }
  56.  
  57. class PixMapWrapper:
  58.     '''PixMapWrapper -- wraps the QD PixMap object in a Python class,
  59.     with methods to easily get/set various pixmap fields.  Note: Use the
  60.     PixMap() method when passing to QD calls.'''
  61.     
  62.     def __init__(self):
  63.         self.__dict__['data'] = ''
  64.         self._header = struct.pack('lhhhhhhhlllhhhhlll', id(self.data) + MacOS.string_id_to_buffer, 0, 0, 0, 0, 0, 0, 0, 0, 72 << 16, 72 << 16, QuickDraw.RGBDirect, 16, 2, 5, 0, 0, 0)
  65.         self.__dict__['_pm'] = Qd.RawBitMap(self._header)
  66.  
  67.     
  68.     def _stuff(self, element, bytes):
  69.         offset = _pmElemOffset[element]
  70.         fmt = _pmElemFormat[element]
  71.         self._header = self._header[:offset] + struct.pack(fmt, bytes) + self._header[offset + struct.calcsize(fmt):]
  72.         self.__dict__['_pm'] = None
  73.  
  74.     
  75.     def _unstuff(self, element):
  76.         offset = _pmElemOffset[element]
  77.         fmt = _pmElemFormat[element]
  78.         return struct.unpack(fmt, self._header[offset:offset + struct.calcsize(fmt)])[0]
  79.  
  80.     
  81.     def __setattr__(self, attr, val):
  82.         if attr == 'baseAddr':
  83.             raise 'UseErr', "don't assign to .baseAddr -- assign to .data instead"
  84.         elif attr == 'data':
  85.             self.__dict__['data'] = val
  86.             self._stuff('baseAddr', id(self.data) + MacOS.string_id_to_buffer)
  87.         elif attr == 'rowBytes':
  88.             self._stuff('rowBytes', val | 32768)
  89.         elif attr == 'bounds':
  90.             self._stuff('left', val[0])
  91.             self._stuff('top', val[1])
  92.             self._stuff('right', val[2])
  93.             self._stuff('bottom', val[3])
  94.         elif attr == 'hRes' or attr == 'vRes':
  95.             self._stuff(attr, int(val) << 16)
  96.         elif attr in _pmElemFormat.keys():
  97.             self._stuff(attr, val)
  98.         else:
  99.             self.__dict__[attr] = val
  100.  
  101.     
  102.     def __getattr__(self, attr):
  103.         if attr == 'rowBytes':
  104.             return self._unstuff('rowBytes') & 32767
  105.         elif attr == 'bounds':
  106.             return (self._unstuff('left'), self._unstuff('top'), self._unstuff('right'), self._unstuff('bottom'))
  107.         elif attr == 'hRes' or attr == 'vRes':
  108.             return self._unstuff(attr) >> 16
  109.         elif attr in _pmElemFormat.keys():
  110.             return self._unstuff(attr)
  111.         else:
  112.             return self.__dict__[attr]
  113.  
  114.     
  115.     def PixMap(self):
  116.         '''Return a QuickDraw PixMap corresponding to this data.'''
  117.         if not self.__dict__['_pm']:
  118.             self.__dict__['_pm'] = Qd.RawBitMap(self._header)
  119.         
  120.         return self.__dict__['_pm']
  121.  
  122.     
  123.     def blit(self, x1 = 0, y1 = 0, x2 = None, y2 = None, port = None):
  124.         '''Draw this pixmap into the given (default current) grafport.'''
  125.         src = self.bounds
  126.         dest = [
  127.             x1,
  128.             y1,
  129.             x2,
  130.             y2]
  131.         if x2 == None:
  132.             dest[2] = x1 + src[2] - src[0]
  133.         
  134.         if y2 == None:
  135.             dest[3] = y1 + src[3] - src[1]
  136.         
  137.         if not port:
  138.             port = Qd.GetPort()
  139.         
  140.         Qd.CopyBits(self.PixMap(), port.GetPortBitMapForCopyBits(), src, tuple(dest), QuickDraw.srcCopy, None)
  141.  
  142.     
  143.     def fromstring(self, s, width, height, format = imgformat.macrgb):
  144.         '''Stuff this pixmap with raw pixel data from a string.
  145.         Supply width, height, and one of the imgformat specifiers.'''
  146.         if format != imgformat.macrgb and format != imgformat.macrgb16:
  147.             raise 'NotImplementedError', 'conversion to macrgb or macrgb16'
  148.         
  149.         self.data = s
  150.         self.bounds = (0, 0, width, height)
  151.         self.cmpCount = 3
  152.         self.pixelType = QuickDraw.RGBDirect
  153.         if format == imgformat.macrgb:
  154.             self.pixelSize = 32
  155.             self.cmpSize = 8
  156.         else:
  157.             self.pixelSize = 16
  158.             self.cmpSize = 5
  159.         self.rowBytes = width * self.pixelSize / 8
  160.  
  161.     
  162.     def tostring(self, format = imgformat.macrgb):
  163.         '''Return raw data as a string in the specified format.'''
  164.         if (format == imgformat.macrgb or self.pixelSize == 32 or format == imgformat.macrgb16) and self.pixelsize == 16:
  165.             return self.data
  166.             raise 'NotImplementedError', 'data format conversion'
  167.         
  168.  
  169.     
  170.     def fromImage(self, im):
  171.         '''Initialize this PixMap from a PIL Image object.'''
  172.         if im.mode != 'RGBA':
  173.             im = im.convert('RGBA')
  174.         
  175.         data = chr(0) + im.tostring()
  176.         self.fromstring(data, im.size[0], im.size[1])
  177.  
  178.     
  179.     def toImage(self):
  180.         '''Return the contents of this PixMap as a PIL Image object.'''
  181.         import Image
  182.         data = self.tostring()[1:] + chr(0)
  183.         bounds = self.bounds
  184.         return Image.fromstring('RGBA', (bounds[2] - bounds[0], bounds[3] - bounds[1]), data)
  185.  
  186.  
  187.  
  188. def test():
  189.     import MacOS
  190.     import EasyDialogs
  191.     import Image
  192.     path = EasyDialogs.AskFileForOpen('Image File:')
  193.     if not path:
  194.         return None
  195.     
  196.     pm = PixMapWrapper()
  197.     pm.fromImage(Image.open(path))
  198.     pm.blit(20, 20)
  199.     return pm
  200.  
  201.