Coverage for /pythoncovmergedfiles/medio/medio/usr/local/lib/python3.11/site-packages/PIL/WmfImagePlugin.py: 70%
Shortcuts on this page
r m x toggle line displays
j k next/prev highlighted chunk
0 (zero) top of page
1 (one) first highlighted chunk
Shortcuts on this page
r m x toggle line displays
j k next/prev highlighted chunk
0 (zero) top of page
1 (one) first highlighted chunk
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
21from __future__ import annotations
23from typing import IO
25from . import Image, ImageFile
26from ._binary import i16le as word
27from ._binary import si16le as short
28from ._binary import si32le as _long
30_handler = None
33def register_handler(handler: ImageFile.StubHandler | None) -> None:
34 """
35 Install application-specific WMF image handler.
37 :param handler: Handler object.
38 """
39 global _handler
40 _handler = handler
43if hasattr(Image.core, "drawwmf"):
44 # install default handler (windows only)
46 class WmfHandler(ImageFile.StubHandler):
47 def open(self, im: ImageFile.StubImageFile) -> None:
48 self.bbox = im.info["wmf_bbox"]
50 def load(self, im: ImageFile.StubImageFile) -> Image.Image:
51 assert im.fp is not None
52 im.fp.seek(0) # rewind
53 return Image.frombytes(
54 "RGB",
55 im.size,
56 Image.core.drawwmf(im.fp.read(), im.size, self.bbox),
57 "raw",
58 "BGR",
59 (im.size[0] * 3 + 3) & -4,
60 -1,
61 )
63 register_handler(WmfHandler())
65#
66# --------------------------------------------------------------------
67# Read WMF file
70def _accept(prefix: bytes) -> bool:
71 return prefix.startswith((b"\xd7\xcd\xc6\x9a\x00\x00", b"\x01\x00\x00\x00"))
74##
75# Image plugin for Windows metafiles.
78class WmfStubImageFile(ImageFile.StubImageFile):
79 format = "WMF"
80 format_description = "Windows Metafile"
82 def _open(self) -> None:
83 # check placeable header
84 assert self.fp is not None
85 s = self.fp.read(44)
87 if s.startswith(b"\xd7\xcd\xc6\x9a\x00\x00"):
88 # placeable windows metafile
90 # get units per inch
91 inch = word(s, 14)
92 if inch == 0:
93 msg = "Invalid inch"
94 raise ValueError(msg)
95 self._inch: tuple[float, float] = inch, inch
97 # get bounding box
98 x0 = short(s, 6)
99 y0 = short(s, 8)
100 x1 = short(s, 10)
101 y1 = short(s, 12)
103 # normalize size to 72 dots per inch
104 self.info["dpi"] = 72
105 size = (
106 (x1 - x0) * self.info["dpi"] // inch,
107 (y1 - y0) * self.info["dpi"] // inch,
108 )
110 self.info["wmf_bbox"] = x0, y0, x1, y1
112 # sanity check (standard metafile header)
113 if s[22:26] != b"\x01\x00\t\x00":
114 msg = "Unsupported WMF file format"
115 raise SyntaxError(msg)
117 elif s.startswith(b"\x01\x00\x00\x00") and s[40:44] == b" EMF":
118 # enhanced metafile
120 # get bounding box
121 x0 = _long(s, 8)
122 y0 = _long(s, 12)
123 x1 = _long(s, 16)
124 y1 = _long(s, 20)
126 # get frame (in 0.01 millimeter units)
127 frame = _long(s, 24), _long(s, 28), _long(s, 32), _long(s, 36)
129 size = x1 - x0, y1 - y0
131 # calculate dots per inch from bbox and frame
132 xdpi = 2540.0 * (x1 - x0) / (frame[2] - frame[0])
133 ydpi = 2540.0 * (y1 - y0) / (frame[3] - frame[1])
135 self.info["wmf_bbox"] = x0, y0, x1, y1
137 if xdpi == ydpi:
138 self.info["dpi"] = xdpi
139 else:
140 self.info["dpi"] = xdpi, ydpi
141 self._inch = xdpi, ydpi
143 else:
144 msg = "Unsupported file format"
145 raise SyntaxError(msg)
147 self._mode = "RGB"
148 self._size = size
150 loader = self._load()
151 if loader:
152 loader.open(self)
154 def _load(self) -> ImageFile.StubHandler | None:
155 return _handler
157 def load(
158 self, dpi: float | tuple[float, float] | None = None
159 ) -> Image.core.PixelAccess | None:
160 if dpi is not None:
161 self.info["dpi"] = dpi
162 x0, y0, x1, y1 = self.info["wmf_bbox"]
163 if not isinstance(dpi, tuple):
164 dpi = dpi, dpi
165 self._size = (
166 int((x1 - x0) * dpi[0] / self._inch[0]),
167 int((y1 - y0) * dpi[1] / self._inch[1]),
168 )
169 return super().load()
172def _save(im: Image.Image, fp: IO[bytes], filename: str | bytes) -> None:
173 if _handler is None or not hasattr(_handler, "save"):
174 msg = "WMF save handler not installed"
175 raise OSError(msg)
176 _handler.save(im, fp, filename)
179#
180# --------------------------------------------------------------------
181# Registry stuff
184Image.register_open(WmfStubImageFile.format, WmfStubImageFile, _accept)
185Image.register_save(WmfStubImageFile.format, _save)
187Image.register_extensions(WmfStubImageFile.format, [".wmf", ".emf"])