WmfImagePlugin.py 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  1. #
  2. # The Python Imaging Library
  3. # $Id$
  4. #
  5. # WMF stub codec
  6. #
  7. # history:
  8. # 1996-12-14 fl Created
  9. # 2004-02-22 fl Turned into a stub driver
  10. # 2004-02-23 fl Added EMF support
  11. #
  12. # Copyright (c) Secret Labs AB 1997-2004. All rights reserved.
  13. # Copyright (c) Fredrik Lundh 1996.
  14. #
  15. # See the README file for information on usage and redistribution.
  16. #
  17. # WMF/EMF reference documentation:
  18. # https://winprotocoldoc.blob.core.windows.net/productionwindowsarchives/MS-WMF/[MS-WMF].pdf
  19. # http://wvware.sourceforge.net/caolan/index.html
  20. # http://wvware.sourceforge.net/caolan/ora-wmf.html
  21. from __future__ import annotations
  22. from typing import IO
  23. from . import Image, ImageFile
  24. from ._binary import i16le as word
  25. from ._binary import si16le as short
  26. from ._binary import si32le as _long
  27. _handler = None
  28. def register_handler(handler: ImageFile.StubHandler | None) -> None:
  29. """
  30. Install application-specific WMF image handler.
  31. :param handler: Handler object.
  32. """
  33. global _handler
  34. _handler = handler
  35. if hasattr(Image.core, "drawwmf"):
  36. # install default handler (windows only)
  37. class WmfHandler(ImageFile.StubHandler):
  38. def open(self, im: ImageFile.StubImageFile) -> None:
  39. im._mode = "RGB"
  40. self.bbox = im.info["wmf_bbox"]
  41. def load(self, im: ImageFile.StubImageFile) -> Image.Image:
  42. im.fp.seek(0) # rewind
  43. return Image.frombytes(
  44. "RGB",
  45. im.size,
  46. Image.core.drawwmf(im.fp.read(), im.size, self.bbox),
  47. "raw",
  48. "BGR",
  49. (im.size[0] * 3 + 3) & -4,
  50. -1,
  51. )
  52. register_handler(WmfHandler())
  53. #
  54. # --------------------------------------------------------------------
  55. # Read WMF file
  56. def _accept(prefix: bytes) -> bool:
  57. return (
  58. prefix[:6] == b"\xd7\xcd\xc6\x9a\x00\x00" or prefix[:4] == b"\x01\x00\x00\x00"
  59. )
  60. ##
  61. # Image plugin for Windows metafiles.
  62. class WmfStubImageFile(ImageFile.StubImageFile):
  63. format = "WMF"
  64. format_description = "Windows Metafile"
  65. def _open(self) -> None:
  66. self._inch = None
  67. # check placable header
  68. s = self.fp.read(80)
  69. if s[:6] == b"\xd7\xcd\xc6\x9a\x00\x00":
  70. # placeable windows metafile
  71. # get units per inch
  72. self._inch = word(s, 14)
  73. if self._inch == 0:
  74. msg = "Invalid inch"
  75. raise ValueError(msg)
  76. # get bounding box
  77. x0 = short(s, 6)
  78. y0 = short(s, 8)
  79. x1 = short(s, 10)
  80. y1 = short(s, 12)
  81. # normalize size to 72 dots per inch
  82. self.info["dpi"] = 72
  83. size = (
  84. (x1 - x0) * self.info["dpi"] // self._inch,
  85. (y1 - y0) * self.info["dpi"] // self._inch,
  86. )
  87. self.info["wmf_bbox"] = x0, y0, x1, y1
  88. # sanity check (standard metafile header)
  89. if s[22:26] != b"\x01\x00\t\x00":
  90. msg = "Unsupported WMF file format"
  91. raise SyntaxError(msg)
  92. elif s[:4] == b"\x01\x00\x00\x00" and s[40:44] == b" EMF":
  93. # enhanced metafile
  94. # get bounding box
  95. x0 = _long(s, 8)
  96. y0 = _long(s, 12)
  97. x1 = _long(s, 16)
  98. y1 = _long(s, 20)
  99. # get frame (in 0.01 millimeter units)
  100. frame = _long(s, 24), _long(s, 28), _long(s, 32), _long(s, 36)
  101. size = x1 - x0, y1 - y0
  102. # calculate dots per inch from bbox and frame
  103. xdpi = 2540.0 * (x1 - x0) / (frame[2] - frame[0])
  104. ydpi = 2540.0 * (y1 - y0) / (frame[3] - frame[1])
  105. self.info["wmf_bbox"] = x0, y0, x1, y1
  106. if xdpi == ydpi:
  107. self.info["dpi"] = xdpi
  108. else:
  109. self.info["dpi"] = xdpi, ydpi
  110. else:
  111. msg = "Unsupported file format"
  112. raise SyntaxError(msg)
  113. self._mode = "RGB"
  114. self._size = size
  115. loader = self._load()
  116. if loader:
  117. loader.open(self)
  118. def _load(self) -> ImageFile.StubHandler | None:
  119. return _handler
  120. def load(self, dpi: int | None = None) -> Image.core.PixelAccess | None:
  121. if dpi is not None and self._inch is not None:
  122. self.info["dpi"] = dpi
  123. x0, y0, x1, y1 = self.info["wmf_bbox"]
  124. self._size = (
  125. (x1 - x0) * self.info["dpi"] // self._inch,
  126. (y1 - y0) * self.info["dpi"] // self._inch,
  127. )
  128. return super().load()
  129. def _save(im: Image.Image, fp: IO[bytes], filename: str | bytes) -> None:
  130. if _handler is None or not hasattr(_handler, "save"):
  131. msg = "WMF save handler not installed"
  132. raise OSError(msg)
  133. _handler.save(im, fp, filename)
  134. #
  135. # --------------------------------------------------------------------
  136. # Registry stuff
  137. Image.register_open(WmfStubImageFile.format, WmfStubImageFile, _accept)
  138. Image.register_save(WmfStubImageFile.format, _save)
  139. Image.register_extensions(WmfStubImageFile.format, [".wmf", ".emf"])