home *** CD-ROM | disk | FTP | other *** search
/ Maximum CD 2010 November / maximum-cd-2010-11.iso / DiscContents / calibre-0.7.13.msi / file_2196 (.txt) < prev    next >
Encoding:
Python Compiled Bytecode  |  2010-08-06  |  2.7 KB  |  85 lines

  1. # Source Generated with Decompyle++
  2. # File: in.pyc (Python 2.6)
  3.  
  4. __version__ = '0.2'
  5. import re
  6. import string
  7. import Image
  8. import ImageFile
  9. import ImagePalette
  10. xpm_head = re.compile('"([0-9]*) ([0-9]*) ([0-9]*) ([0-9]*)')
  11.  
  12. def _accept(prefix):
  13.     return prefix[:9] == '/* XPM */'
  14.  
  15.  
  16. class XpmImageFile(ImageFile.ImageFile):
  17.     format = 'XPM'
  18.     format_description = 'X11 Pixel Map'
  19.     
  20.     def _open(self):
  21.         if not _accept(self.fp.read(9)):
  22.             raise SyntaxError, 'not an XPM file'
  23.         _accept(self.fp.read(9))
  24.         while None:
  25.             s = self.fp.readline()
  26.             if not s:
  27.                 raise SyntaxError, 'broken XPM file'
  28.             m = xpm_head.match(s)
  29.             if m:
  30.                 break
  31.                 continue
  32.             continue
  33.             self.size = (int(m.group(1)), int(m.group(2)))
  34.             pal = int(m.group(3))
  35.             bpp = int(m.group(4))
  36.             if pal > 256 or bpp != 1:
  37.                 raise ValueError, 'cannot read this XPM file'
  38.             bpp != 1
  39.             palette = [
  40.                 '\x00\x00\x00'] * 256
  41.             for i in range(pal):
  42.                 s = self.fp.readline()
  43.                 if s[-2:] == '\r\n':
  44.                     s = s[:-2]
  45.                 elif s[-1:] in '\r\n':
  46.                     s = s[:-1]
  47.                 
  48.                 c = ord(s[1])
  49.                 s = string.split(s[2:-2])
  50.                 for i in range(0, len(s), 2):
  51.                     if s[i] == 'c':
  52.                         rgb = s[i + 1]
  53.                         if rgb == 'None':
  54.                             self.info['transparency'] = c
  55.                         elif rgb[0] == '#':
  56.                             rgb = string.atoi(rgb[1:], 16)
  57.                             palette[c] = chr(rgb >> 16 & 255) + chr(rgb >> 8 & 255) + chr(rgb & 255)
  58.                         else:
  59.                             raise ValueError, 'cannot read this XPM file'
  60.                         continue
  61.                     rgb == 'None'
  62.                 else:
  63.                     raise ValueError, 'cannot read this XPM file'
  64.             
  65.         self.mode = 'P'
  66.         self.palette = ImagePalette.raw('RGB', string.join(palette, ''))
  67.         self.tile = [
  68.             ('raw', (0, 0) + self.size, self.fp.tell(), ('P', 0, 1))]
  69.  
  70.     
  71.     def load_read(self, bytes):
  72.         (xsize, ysize) = self.size
  73.         s = [
  74.             None] * ysize
  75.         for i in range(ysize):
  76.             s[i] = string.ljust(self.fp.readline()[1:xsize + 1], xsize)
  77.         
  78.         self.fp = None
  79.         return string.join(s, '')
  80.  
  81.  
  82. Image.register_open('XPM', XpmImageFile, _accept)
  83. Image.register_extension('XPM', '.xpm')
  84. Image.register_mime('XPM', 'image/xpm')
  85.