Funktionierender Prototyp des Serious Games zur Vermittlung von Wissen zu Software-Engineering-Arbeitsmodellen.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

WmfImagePlugin.py 4.6KB

1 year ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  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 . import Image, ImageFile
  22. from ._binary import i16le as word
  23. from ._binary import si16le as short
  24. from ._binary import si32le as _long
  25. _handler = None
  26. def register_handler(handler):
  27. """
  28. Install application-specific WMF image handler.
  29. :param handler: Handler object.
  30. """
  31. global _handler
  32. _handler = handler
  33. if hasattr(Image.core, "drawwmf"):
  34. # install default handler (windows only)
  35. class WmfHandler:
  36. def open(self, im):
  37. im.mode = "RGB"
  38. self.bbox = im.info["wmf_bbox"]
  39. def load(self, im):
  40. im.fp.seek(0) # rewind
  41. return Image.frombytes(
  42. "RGB",
  43. im.size,
  44. Image.core.drawwmf(im.fp.read(), im.size, self.bbox),
  45. "raw",
  46. "BGR",
  47. (im.size[0] * 3 + 3) & -4,
  48. -1,
  49. )
  50. register_handler(WmfHandler())
  51. #
  52. # --------------------------------------------------------------------
  53. # Read WMF file
  54. def _accept(prefix):
  55. return (
  56. prefix[:6] == b"\xd7\xcd\xc6\x9a\x00\x00" or prefix[:4] == b"\x01\x00\x00\x00"
  57. )
  58. ##
  59. # Image plugin for Windows metafiles.
  60. class WmfStubImageFile(ImageFile.StubImageFile):
  61. format = "WMF"
  62. format_description = "Windows Metafile"
  63. def _open(self):
  64. self._inch = None
  65. # check placable header
  66. s = self.fp.read(80)
  67. if s[:6] == b"\xd7\xcd\xc6\x9a\x00\x00":
  68. # placeable windows metafile
  69. # get units per inch
  70. self._inch = word(s, 14)
  71. # get bounding box
  72. x0 = short(s, 6)
  73. y0 = short(s, 8)
  74. x1 = short(s, 10)
  75. y1 = short(s, 12)
  76. # normalize size to 72 dots per inch
  77. self.info["dpi"] = 72
  78. size = (
  79. (x1 - x0) * self.info["dpi"] // self._inch,
  80. (y1 - y0) * self.info["dpi"] // self._inch,
  81. )
  82. self.info["wmf_bbox"] = x0, y0, x1, y1
  83. # sanity check (standard metafile header)
  84. if s[22:26] != b"\x01\x00\t\x00":
  85. msg = "Unsupported WMF file format"
  86. raise SyntaxError(msg)
  87. elif s[:4] == b"\x01\x00\x00\x00" and s[40:44] == b" EMF":
  88. # enhanced metafile
  89. # get bounding box
  90. x0 = _long(s, 8)
  91. y0 = _long(s, 12)
  92. x1 = _long(s, 16)
  93. y1 = _long(s, 20)
  94. # get frame (in 0.01 millimeter units)
  95. frame = _long(s, 24), _long(s, 28), _long(s, 32), _long(s, 36)
  96. size = x1 - x0, y1 - y0
  97. # calculate dots per inch from bbox and frame
  98. xdpi = 2540.0 * (x1 - y0) / (frame[2] - frame[0])
  99. ydpi = 2540.0 * (y1 - y0) / (frame[3] - frame[1])
  100. self.info["wmf_bbox"] = x0, y0, x1, y1
  101. if xdpi == ydpi:
  102. self.info["dpi"] = xdpi
  103. else:
  104. self.info["dpi"] = xdpi, ydpi
  105. else:
  106. msg = "Unsupported file format"
  107. raise SyntaxError(msg)
  108. self.mode = "RGB"
  109. self._size = size
  110. loader = self._load()
  111. if loader:
  112. loader.open(self)
  113. def _load(self):
  114. return _handler
  115. def load(self, dpi=None):
  116. if dpi is not None and self._inch is not None:
  117. self.info["dpi"] = dpi
  118. x0, y0, x1, y1 = self.info["wmf_bbox"]
  119. self._size = (
  120. (x1 - x0) * self.info["dpi"] // self._inch,
  121. (y1 - y0) * self.info["dpi"] // self._inch,
  122. )
  123. return super().load()
  124. def _save(im, fp, filename):
  125. if _handler is None or not hasattr(_handler, "save"):
  126. msg = "WMF save handler not installed"
  127. raise OSError(msg)
  128. _handler.save(im, fp, filename)
  129. #
  130. # --------------------------------------------------------------------
  131. # Registry stuff
  132. Image.register_open(WmfStubImageFile.format, WmfStubImageFile, _accept)
  133. Image.register_save(WmfStubImageFile.format, _save)
  134. Image.register_extensions(WmfStubImageFile.format, [".wmf", ".emf"])