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 / DcxImagePlugin.py < prev    next >
Text File  |  2001-05-03  |  2KB  |  77 lines

  1. #
  2. # The Python Imaging Library.
  3. # $Id$
  4. #
  5. # DCX file handling
  6. #
  7. # DCX is a container file format defined by Intel, commonly used
  8. # for fax applications.  Each DCX file consists of a directory
  9. # (a list of file offsets) followed by a set of (usually 1-bit)
  10. # PCX files.
  11. #
  12. # History:
  13. #       95-09-09 fl     Created
  14. #       96-03-20 fl     Properly derived from PcxImageFile.
  15. #       98-07-15 fl     Renamed offset attribute to avoid name clash
  16. #
  17. # Copyright (c) Secret Labs AB 1997-98.
  18. # Copyright (c) Fredrik Lundh 1995-96.
  19. #
  20. # See the README file for information on usage and redistribution.
  21. #
  22.  
  23.  
  24. __version__ = "0.1"
  25.  
  26.  
  27. import Image, ImageFile
  28.  
  29. from PcxImagePlugin import PcxImageFile
  30.  
  31.  
  32. MAGIC = 0x3ADE68B1 # QUIZ: what's this value, then?
  33.  
  34.  
  35. def i32(c):
  36.     return ord(c[0]) + (ord(c[1])<<8) + (ord(c[2])<<16) + (ord(c[3])<<24)
  37.  
  38. def _accept(prefix):
  39.     return i32(prefix) == MAGIC
  40.  
  41. class DcxImageFile(PcxImageFile):
  42.  
  43.     format = "DCX"
  44.     format_description = "Intel DCX"
  45.  
  46.     def _open(self):
  47.  
  48.         # Header
  49.         s = self.fp.read(4)
  50.         if i32(s) != MAGIC:
  51.             raise SyntaxError, "not a DCX file"
  52.  
  53.         # Component directory
  54.         self._offset = []
  55.         for i in range(1024):
  56.             offset = i32(self.fp.read(4))
  57.             if not offset:
  58.                 break
  59.             self._offset.append(offset)
  60.  
  61.         self.seek(0)
  62.  
  63.     def seek(self, frame):
  64.         if frame >= len(self._offset):
  65.             raise IndexError, "attempt to seek outside DCX directory"
  66.         self.frame = frame
  67.         self.fp.seek(self._offset[frame])
  68.         PcxImageFile._open(self)
  69.  
  70.     def tell(self):
  71.         return self.frame
  72.  
  73.  
  74. Image.register_open("DCX", DcxImageFile, _accept)
  75.  
  76. Image.register_extension("DCX", ".dcx")
  77.