WmfImagePlugin.py 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  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 print_function
  22. from . import Image, ImageFile
  23. from ._binary import i16le as word, i32le as dword, si16le as short, si32le as _long
  24. from ._util import py3
  25. # __version__ is deprecated and will be removed in a future version. Use
  26. # PIL.__version__ instead.
  27. __version__ = "0.2"
  28. _handler = None
  29. if py3:
  30. long = int
  31. def register_handler(handler):
  32. """
  33. Install application-specific WMF image handler.
  34. :param handler: Handler object.
  35. """
  36. global _handler
  37. _handler = handler
  38. if hasattr(Image.core, "drawwmf"):
  39. # install default handler (windows only)
  40. class WmfHandler(object):
  41. def open(self, im):
  42. im.mode = "RGB"
  43. self.bbox = im.info["wmf_bbox"]
  44. def load(self, im):
  45. im.fp.seek(0) # rewind
  46. return Image.frombytes(
  47. "RGB",
  48. im.size,
  49. Image.core.drawwmf(im.fp.read(), im.size, self.bbox),
  50. "raw",
  51. "BGR",
  52. (im.size[0] * 3 + 3) & -4,
  53. -1,
  54. )
  55. register_handler(WmfHandler())
  56. #
  57. # --------------------------------------------------------------------
  58. # Read WMF file
  59. def _accept(prefix):
  60. return (
  61. prefix[:6] == b"\xd7\xcd\xc6\x9a\x00\x00" or prefix[:4] == b"\x01\x00\x00\x00"
  62. )
  63. ##
  64. # Image plugin for Windows metafiles.
  65. class WmfStubImageFile(ImageFile.StubImageFile):
  66. format = "WMF"
  67. format_description = "Windows Metafile"
  68. def _open(self):
  69. # check placable header
  70. s = self.fp.read(80)
  71. if s[:6] == b"\xd7\xcd\xc6\x9a\x00\x00":
  72. # placeable windows metafile
  73. # get units per inch
  74. inch = word(s, 14)
  75. # get bounding box
  76. x0 = short(s, 6)
  77. y0 = short(s, 8)
  78. x1 = short(s, 10)
  79. y1 = short(s, 12)
  80. # normalize size to 72 dots per inch
  81. size = (x1 - x0) * 72 // inch, (y1 - y0) * 72 // inch
  82. self.info["wmf_bbox"] = x0, y0, x1, y1
  83. self.info["dpi"] = 72
  84. # sanity check (standard metafile header)
  85. if s[22:26] != b"\x01\x00\t\x00":
  86. raise SyntaxError("Unsupported WMF file format")
  87. elif dword(s) == 1 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. # normalize size to 72 dots per inch
  97. size = x1 - x0, y1 - y0
  98. # calculate dots per inch from bbox and frame
  99. xdpi = int(2540.0 * (x1 - y0) / (frame[2] - frame[0]) + 0.5)
  100. ydpi = int(2540.0 * (y1 - y0) / (frame[3] - frame[1]) + 0.5)
  101. self.info["wmf_bbox"] = x0, y0, x1, y1
  102. if xdpi == ydpi:
  103. self.info["dpi"] = xdpi
  104. else:
  105. self.info["dpi"] = xdpi, ydpi
  106. else:
  107. raise SyntaxError("Unsupported file format")
  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 _save(im, fp, filename):
  116. if _handler is None or not hasattr(_handler, "save"):
  117. raise IOError("WMF save handler not installed")
  118. _handler.save(im, fp, filename)
  119. #
  120. # --------------------------------------------------------------------
  121. # Registry stuff
  122. Image.register_open(WmfStubImageFile.format, WmfStubImageFile, _accept)
  123. Image.register_save(WmfStubImageFile.format, _save)
  124. Image.register_extensions(WmfStubImageFile.format, [".wmf", ".emf"])