home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / pypil112.zip / PIL-1.1.2.zip / Lib / site-packages / PIL / PcxImagePlugin.pyc (.txt) < prev    next >
Python Compiled Bytecode  |  2001-12-25  |  5KB  |  113 lines

  1. # Source Generated with Decompyle++
  2. # File: in.pyc (Python 2.2)
  3.  
  4. __version__ = '0.4'
  5. import Image
  6. import ImageFile
  7. import ImagePalette
  8.  
  9. def i16(c):
  10.     return ord(c[0]) + (ord(c[1]) << 8)
  11.  
  12.  
  13. def _accept(prefix):
  14.     if ord(prefix[0]) == 10:
  15.         pass
  16.     return ord(prefix[1]) in [
  17.         0,
  18.         2,
  19.         3,
  20.         5]
  21.  
  22.  
  23. class PcxImageFile(ImageFile.ImageFile):
  24.     format = 'PCX'
  25.     format_description = 'Paintbrush'
  26.     
  27.     def _open(self):
  28.         s = self.fp.read(128)
  29.         if not _accept(s):
  30.             raise SyntaxError, 'not a PCX file'
  31.         
  32.         bbox = (i16(s[4:]), i16(s[6:]), i16(s[8:]) + 1, i16(s[10:]) + 1)
  33.         if bbox[2] <= bbox[0] or bbox[3] <= bbox[1]:
  34.             raise SyntaxError, 'bad PCX image size'
  35.         
  36.         version = ord(s[1])
  37.         bits = ord(s[3])
  38.         planes = ord(s[65])
  39.         stride = i16(s[66:])
  40.         if bits == 1 and planes == 1:
  41.             mode = rawmode = '1'
  42.         elif bits == 1 and planes in (2, 4):
  43.             mode = 'P'
  44.             rawmode = 'P;%dL' % planes
  45.             self.palette = ImagePalette.raw('RGB', s[16:64])
  46.         elif version == 5 and bits == 8 and planes == 1:
  47.             mode = rawmode = 'L'
  48.             self.fp.seek(-769, 2)
  49.             s = self.fp.read(769)
  50.             if len(s) == 769 and ord(s[0]) == 12:
  51.                 for i in range(256):
  52.                     if s[i * 3 + 1:i * 3 + 4] != chr(i) * 3:
  53.                         mode = rawmode = 'P'
  54.                         break
  55.                     
  56.                 
  57.                 if mode == 'P':
  58.                     self.palette = ImagePalette.raw('RGB', s[1:])
  59.                 
  60.             
  61.         elif version == 5 and bits == 8 and planes == 3:
  62.             mode = 'RGB'
  63.             rawmode = 'RGB;L'
  64.         else:
  65.             raise IOError, 'unknown PCX mode'
  66.         self.mode = mode
  67.         self.size = (bbox[2] - bbox[0], bbox[3] - bbox[1])
  68.         self.tile = [
  69.             ('pcx', bbox, 128, rawmode)]
  70.  
  71.  
  72. SAVE = {
  73.     '1': (2, 1, 1, '1'),
  74.     'L': (5, 8, 1, 'L'),
  75.     'P': (5, 8, 1, 'P'),
  76.     'RGB': (5, 8, 3, 'RGB;L') }
  77.  
  78. def o16(i):
  79.     return chr(i & 255) + chr(i >> 8 & 255)
  80.  
  81.  
  82. def _save(im, fp, filename, check = 0):
  83.     
  84.     try:
  85.         (version, bits, planes, rawmode) = SAVE[im.mode]
  86.     except KeyError:
  87.         raise ValueError, 'Cannot save %s images as PCX' % im.mode
  88.  
  89.     if check:
  90.         return check
  91.     
  92.     stride = (im.size[0] * bits + 7) / 8
  93.     screen = im.size
  94.     dpi = (100, 100)
  95.     fp.write(chr(10) + chr(version) + chr(1) + chr(bits) + o16(0) + o16(0) + o16(im.size[0] - 1) + o16(im.size[1] - 1) + o16(dpi[0]) + o16(dpi[1]) + chr(0) * 24 + chr(255) * 24 + chr(0) + chr(planes) + o16(stride) + o16(1) + o16(screen[0]) + o16(screen[1]) + chr(0) * 54)
  96.     if not __debug__ and fp.tell() == 128:
  97.         raise AssertionError
  98.     ImageFile._save(im, fp, [
  99.         ('pcx', (0, 0) + im.size, 0, (rawmode, bits * planes))])
  100.     if im.mode == 'P':
  101.         fp.write(chr(12))
  102.         fp.write(im.im.getpalette('RGB', 'RGB'))
  103.     elif im.mode == 'L':
  104.         fp.write(chr(12))
  105.         for i in range(256):
  106.             fp.write(chr(i) * 3)
  107.         
  108.     
  109.  
  110. Image.register_open('PCX', PcxImageFile, _accept)
  111. Image.register_save('PCX', _save)
  112. Image.register_extension('PCX', '.pcx')
  113.