DcxImagePlugin.py 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  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 __future__ import annotations
  24. from . import Image
  25. from ._binary import i32le as i32
  26. from ._util import DeferredError
  27. from .PcxImagePlugin import PcxImageFile
  28. MAGIC = 0x3ADE68B1 # QUIZ: what's this value, then?
  29. def _accept(prefix: bytes) -> bool:
  30. return len(prefix) >= 4 and i32(prefix) == MAGIC
  31. ##
  32. # Image plugin for the Intel DCX format.
  33. class DcxImageFile(PcxImageFile):
  34. format = "DCX"
  35. format_description = "Intel DCX"
  36. _close_exclusive_fp_after_loading = False
  37. def _open(self) -> None:
  38. # Header
  39. assert self.fp is not None
  40. s = self.fp.read(4)
  41. if not _accept(s):
  42. msg = "not a DCX file"
  43. raise SyntaxError(msg)
  44. # Component directory
  45. self._offset = []
  46. for i in range(1024):
  47. offset = i32(self.fp.read(4))
  48. if not offset:
  49. break
  50. self._offset.append(offset)
  51. self._fp = self.fp
  52. self.frame = -1
  53. self.n_frames = len(self._offset)
  54. self.is_animated = self.n_frames > 1
  55. self.seek(0)
  56. def seek(self, frame: int) -> None:
  57. if not self._seek_check(frame):
  58. return
  59. if isinstance(self._fp, DeferredError):
  60. raise self._fp.ex
  61. self.frame = frame
  62. self.fp = self._fp
  63. self.fp.seek(self._offset[frame])
  64. PcxImageFile._open(self)
  65. def tell(self) -> int:
  66. return self.frame
  67. Image.register_open(DcxImageFile.format, DcxImageFile, _accept)
  68. Image.register_extension(DcxImageFile.format, ".dcx")