home *** CD-ROM | disk | FTP | other *** search
/ Chip 2011 November / CHIP_2011_11.iso / Programy / Narzedzia / Inkscape / Inkscape-0.48.2-1-win32.exe / python / Lib / site-packages / PIL / WbmpImagePlugin.py < prev    next >
Encoding:
Text File  |  2010-03-06  |  2.7 KB  |  106 lines

  1. #
  2. # The Python Imaging Library.
  3. # $Id$
  4. #
  5. # WBMP file handling
  6. #
  7. # This format is used by mobile phones supporting the WAP protocol.
  8. #
  9.  
  10. __version__ = "0.1"
  11.  
  12. import Image, ImageFile, ImagePalette
  13. import string
  14.  
  15. def _tomb(val):
  16.     ''' Convert val to a multi-byte encoded string. '''
  17.     l = [chr(val & 0x7f)]
  18.     val = val >> 7
  19.     while val != 0:
  20.         l.append(chr((val & 0x7f)|0x80))
  21.         val = val >> 7
  22.     l.reverse()
  23.     return string.join(l, '')
  24.  
  25. def _frommb(s, index = 0):
  26.     ''' Read multibyte value from string, returns value and index to
  27.     rest of string. '''
  28.     val, cont = 0, 1
  29.     while cont:
  30.         ch = ord(s[index])
  31.         index = index + 1
  32.         cont = ch & 0x80
  33.         val = val << 7 | (ch & 0x7f)
  34.     return val, index
  35.  
  36. def _accept(prefix):
  37.     ''' The type field is multibyte, but only type 0 exists. '''
  38.     type, index = _frommb(prefix)
  39.     header = ord(prefix[index])
  40.     if type != 0 or header != 0:
  41.         # Cannot handle extension header fields yet.
  42.         return 0
  43.     return 1
  44.  
  45. ##
  46. # (Internal) Image plugin for the WBMP format.
  47.  
  48. class WbmpImageFile(ImageFile.ImageFile):
  49.  
  50.     format = "WBMP"
  51.     format_description = "Wireless"
  52.  
  53.     def _open(self):
  54.         # Read header...
  55.         # Image type --- must be 0.
  56.         # Fixheader field
  57.         # ExtFields
  58.         # Width Height
  59.         s = self.fp.read(128)
  60.         if not _accept(s):
  61.             raise SyntaxError("not a WBMP file")
  62.  
  63.         # Set size in pixels
  64.         width, index = _frommb(s, 2)
  65.         height, index = _frommb(s, index)
  66.         self.size = width, height
  67.  
  68.         # Set image mode.
  69.         # 1-bit bilevel, stored with the leftmost pixel in the most
  70.         # significant bit. 0 means black, 1 means white.
  71.         self.mode = "1"
  72.  
  73.         # Tell the system how to read the image
  74.         self.tile = [("raw", (0, 0) + self.size, index, (self.mode, 0, 1))]
  75.  
  76. SAVE = {
  77.     # mode: (version, bits, planes, raw mode)
  78.     "1": (2, 1, 1, "1"),
  79. }
  80.  
  81. ##
  82. # (Internal) Image save plugin for the WBMP format.
  83.  
  84. def _save(im, fp, filename, check=0):
  85.     try:
  86.         version, bits, planes, rawmode = SAVE[im.mode]
  87.     except KeyError:
  88.         raise ValueError("Cannot save %s images as WBMP" % im.mode)
  89.  
  90.     if check:
  91.         return check
  92.  
  93.     width, height = im.size
  94.     # Write header
  95.     fp.write (chr(0) + chr(0) + _tomb(width) + _tomb(height))
  96.     # Write data
  97.     ImageFile._save(im, fp, [("raw", (0,0) + im.size, 0, (rawmode, 0, 1))])
  98.  
  99.  
  100. # --------------------------------------------------------------------
  101. # registry
  102.  
  103. Image.register_open("WBMP", WbmpImageFile, _accept)
  104. Image.register_save("WBMP", _save)
  105. Image.register_extension("WBMP", ".wbmp")
  106.