DcxImagePlugin.py 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  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. # 1995-09-09 fl Created
  14. # 1996-03-20 fl Properly derived from PcxImageFile.
  15. # 1998-07-15 fl Renamed offset attribute to avoid name clash
  16. # 2002-07-30 fl Fixed file handling
  17. #
  18. # Copyright (c) 1997-98 by Secret Labs AB.
  19. # Copyright (c) 1995-96 by Fredrik Lundh.
  20. #
  21. # See the README file for information on usage and redistribution.
  22. #
  23. from . import Image
  24. from ._binary import i32le as i32
  25. from .PcxImagePlugin import PcxImageFile
  26. # __version__ is deprecated and will be removed in a future version. Use
  27. # PIL.__version__ instead.
  28. __version__ = "0.2"
  29. MAGIC = 0x3ADE68B1 # QUIZ: what's this value, then?
  30. def _accept(prefix):
  31. return len(prefix) >= 4 and i32(prefix) == MAGIC
  32. ##
  33. # Image plugin for the Intel DCX format.
  34. class DcxImageFile(PcxImageFile):
  35. format = "DCX"
  36. format_description = "Intel DCX"
  37. _close_exclusive_fp_after_loading = False
  38. def _open(self):
  39. # Header
  40. s = self.fp.read(4)
  41. if i32(s) != MAGIC:
  42. raise SyntaxError("not a DCX file")
  43. # Component directory
  44. self._offset = []
  45. for i in range(1024):
  46. offset = i32(self.fp.read(4))
  47. if not offset:
  48. break
  49. self._offset.append(offset)
  50. self.__fp = self.fp
  51. self.frame = None
  52. self.seek(0)
  53. @property
  54. def n_frames(self):
  55. return len(self._offset)
  56. @property
  57. def is_animated(self):
  58. return len(self._offset) > 1
  59. def seek(self, frame):
  60. if not self._seek_check(frame):
  61. return
  62. self.frame = frame
  63. self.fp = self.__fp
  64. self.fp.seek(self._offset[frame])
  65. PcxImageFile._open(self)
  66. def tell(self):
  67. return self.frame
  68. def _close__fp(self):
  69. try:
  70. if self.__fp != self.fp:
  71. self.__fp.close()
  72. except AttributeError:
  73. pass
  74. finally:
  75. self.__fp = None
  76. Image.register_open(DcxImageFile.format, DcxImageFile, _accept)
  77. Image.register_extension(DcxImageFile.format, ".dcx")