home *** CD-ROM | disk | FTP | other *** search
/ MacHack 2000 / MacHack 2000.toast / pc / The Hacks / MacHacksBug / Python 1.5.2c1 / Extensions / img / Lib / imgpict.py < prev    next >
Encoding:
Python Source  |  2000-06-23  |  3.1 KB  |  119 lines

  1. """A minimal module to write Mac 24-bit RGB PICT files on any machine"""
  2. #
  3. # Macintosh PICT format reader/writer module
  4. #
  5. # Jack Jansen, CWI, 1995
  6. #
  7. import struct
  8. import imgformat
  9. import imgop
  10.  
  11. error = imgformat.error
  12.  
  13. class reader:
  14.     def __init__(self, filename):
  15.     self._filename = filename
  16.     self.width = 0
  17.     self.height = 0
  18.     raise error, 'Reading PICT files not yet supported'
  19.  
  20.     def args(self):
  21.     return self.__dict__
  22.     
  23.     def read(self):
  24.     return ''
  25.  
  26.     def write(self, data):
  27.     raise error, 'Cannot write() to reader'
  28.  
  29. class writer:
  30.     """This object can write to a pict file. The width, height and format
  31.     attributes should be set before calling write()"""
  32.     
  33.     def __init__(self, file):
  34.         if type(file) == type(''):
  35.         self._filename = file
  36.         self._fp = None
  37.     else:
  38.         self._fp = file
  39.         self._filename = "<open file>"
  40.     self.format_choices = (imgformat.macrgb, )
  41.     self.format = imgformat.macrgb
  42.  
  43.     def args(self):
  44.     return self.__dict__
  45.     
  46.     def _get(self, attr):
  47.     try:
  48.         return getattr(self, attr)
  49.     except AttributeError:
  50.         raise error, "Required attribute '%s' missing"%attr
  51.  
  52.     def read(self):
  53.     raise error, 'Cannot read() from writer'
  54.  
  55.  
  56.     def write(self, data):
  57.     "write the actual data"
  58.     
  59.     w = self._get('width')
  60.     h = self._get('height')
  61.     fmt = self._get('format')
  62.     if fmt <> imgformat.macrgb:
  63.         raise error, 'Only macrgb currently supported'
  64.     if w*h*4 != len(data):
  65.         raise error, 'Incorrect datasize'
  66.     # Create the image structure. This is tricky due to possible alignment
  67.     imgstruct = struct.pack('hhhhh', 0x011, 0x02ff, 0x0c00, -2, 0)[:10]
  68.     imgstruct = imgstruct + struct.pack('llhhhh', 0x480000, 0x480000, 0, 0, h, w)
  69.     imgstruct = imgstruct + struct.pack('l', 0)
  70.     imgstruct = imgstruct + struct.pack('h', 0x9a)[:2]
  71.     imgstruct = imgstruct + struct.pack('l', 0xff)
  72.     imgstruct = imgstruct + struct.pack('hhhhhhh', 0x8000|(w*4),
  73.             0, 0, h, w, 0, 1)[:14]
  74.     imgstruct = imgstruct + struct.pack('lll', 0, 0x480000, 0x480000)
  75.     imgstruct = imgstruct + struct.pack('hhhh', 16, 32, 3, 8)
  76. ##    imgstruct = imgstruct + struct.pack('lll', 0, 0, 0)
  77. ##    imgstruct = imgstruct + struct.pack('l', 0x1000000)
  78. ##    imgstruct = imgstruct + struct.pack('hh', 0, 0)
  79. ##    imgstruct = imgstruct + struct.pack('hhhh', -1, 0, 0, 0)
  80.     imgstruct = imgstruct + struct.pack('lll', 0, 0xad892c, 0)  # ????
  81.     imgstruct = imgstruct + struct.pack('hhhh', 0, 0, h, w)
  82.     imgstruct = imgstruct + struct.pack('hhhh', 0, 0, h, w)
  83.     imgstruct = imgstruct + struct.pack('h', 0)[:2]
  84.     
  85.     length = len(imgstruct) + len(data) + 10 + 2
  86.     imghdr = struct.pack('hhhhh', length&0xffff, 0, 0, h, w)[:10]
  87.     imgtrailer = struct.pack('h', 0xff)[:2]
  88.     
  89.     if self._fp:
  90.         fp = self._fp
  91.     else:
  92.         fp = open(self._filename, 'wb')
  93.     fp.write('\0'*512)
  94.     fp.write(imghdr)
  95.     fp.write(imgstruct)
  96.     fp.write(data)
  97.     fp.write(imgtrailer)
  98.     fp.close()
  99.     
  100.     try:
  101.         import macfs
  102.         macfs.FSSpec(self._filename).SetCreatorType('????', 'PICT')
  103.     except ImportError:
  104.         # We're probably not running on a mac
  105.         pass
  106.         
  107. def _test():
  108.     import imgppm
  109.     
  110.     r = imgppm.reader('in-rgb-t2b.ppm')
  111.     w = writer('out-mac.pict')
  112.     w.width, w.height = r.width, r.height
  113.     d = r.read()
  114.     d = imgop.shuffle(d, r.width, r.height, r.format, w.format)
  115.     w.write(d)
  116.  
  117. if __name__ == '__main__':
  118.     _test()    
  119.