WmfImagePlugin.py 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  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. # get bounding box
  74. x0 = short(s, 6)
  75. y0 = short(s, 8)
  76. x1 = short(s, 10)
  77. y1 = short(s, 12)
  78. # normalize size to 72 dots per inch
  79. self.info["dpi"] = 72
  80. size = (
  81. (x1 - x0) * self.info["dpi"] // self._inch,
  82. (y1 - y0) * self.info["dpi"] // self._inch,
  83. )
  84. self.info["wmf_bbox"] = x0, y0, x1, y1
  85. # sanity check (standard metafile header)
  86. if s[22:26] != b"\x01\x00\t\x00":
  87. msg = "Unsupported WMF file format"
  88. raise SyntaxError(msg)
  89. elif s[:4] == b"\x01\x00\x00\x00" and s[40:44] == b" EMF":
  90. # enhanced metafile
  91. # get bounding box
  92. x0 = _long(s, 8)
  93. y0 = _long(s, 12)
  94. x1 = _long(s, 16)
  95. y1 = _long(s, 20)
  96. # get frame (in 0.01 millimeter units)
  97. frame = _long(s, 24), _long(s, 28), _long(s, 32), _long(s, 36)
  98. size = x1 - x0, y1 - y0
  99. # calculate dots per inch from bbox and frame
  100. xdpi = 2540.0 * (x1 - y0) / (frame[2] - frame[0])
  101. ydpi = 2540.0 * (y1 - y0) / (frame[3] - frame[1])
  102. self.info["wmf_bbox"] = x0, y0, x1, y1
  103. if xdpi == ydpi:
  104. self.info["dpi"] = xdpi
  105. else:
  106. self.info["dpi"] = xdpi, ydpi
  107. else:
  108. msg = "Unsupported file format"
  109. raise SyntaxError(msg)
  110. self._mode = "RGB"
  111. self._size = size
  112. loader = self._load()
  113. if loader:
  114. loader.open(self)
  115. def _load(self) -> ImageFile.StubHandler | None:
  116. return _handler
  117. def load(self, dpi=None):
  118. if dpi is not None and self._inch is not None:
  119. self.info["dpi"] = dpi
  120. x0, y0, x1, y1 = self.info["wmf_bbox"]
  121. self._size = (
  122. (x1 - x0) * self.info["dpi"] // self._inch,
  123. (y1 - y0) * self.info["dpi"] // self._inch,
  124. )
  125. return super().load()
  126. def _save(im: Image.Image, fp: IO[bytes], filename: str | bytes) -> None:
  127. if _handler is None or not hasattr(_handler, "save"):
  128. msg = "WMF save handler not installed"
  129. raise OSError(msg)
  130. _handler.save(im, fp, filename)
  131. #
  132. # --------------------------------------------------------------------
  133. # Registry stuff
  134. Image.register_open(WmfStubImageFile.format, WmfStubImageFile, _accept)
  135. Image.register_save(WmfStubImageFile.format, _save)
  136. Image.register_extensions(WmfStubImageFile.format, [".wmf", ".emf"])