Coverage for /var/srv/projects/api.amasfac.comuna18.com/tmp/venv/lib/python3.9/site-packages/PIL/WmfImagePlugin.py: 24%
69 statements
« prev ^ index » next coverage.py v6.4.4, created at 2023-07-17 14:22 -0600
« prev ^ index » next coverage.py v6.4.4, created at 2023-07-17 14:22 -0600
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
22from . import Image, ImageFile
23from ._binary import i16le as word
24from ._binary import si16le as short
25from ._binary import si32le as _long
27_handler = None
30def register_handler(handler):
31 """
32 Install application-specific WMF image handler.
34 :param handler: Handler object.
35 """
36 global _handler
37 _handler = handler
40if hasattr(Image.core, "drawwmf"): 40 ↛ 43line 40 didn't jump to line 43, because the condition on line 40 was never true
41 # install default handler (windows only)
43 class WmfHandler:
44 def open(self, im):
45 im.mode = "RGB"
46 self.bbox = im.info["wmf_bbox"]
48 def load(self, im):
49 im.fp.seek(0) # rewind
50 return Image.frombytes(
51 "RGB",
52 im.size,
53 Image.core.drawwmf(im.fp.read(), im.size, self.bbox),
54 "raw",
55 "BGR",
56 (im.size[0] * 3 + 3) & -4,
57 -1,
58 )
60 register_handler(WmfHandler())
62#
63# --------------------------------------------------------------------
64# Read WMF file
67def _accept(prefix):
68 return (
69 prefix[:6] == b"\xd7\xcd\xc6\x9a\x00\x00" or prefix[:4] == b"\x01\x00\x00\x00"
70 )
73##
74# Image plugin for Windows metafiles.
77class WmfStubImageFile(ImageFile.StubImageFile):
79 format = "WMF"
80 format_description = "Windows Metafile"
82 def _open(self):
83 self._inch = None
85 # check placable header
86 s = self.fp.read(80)
88 if s[:6] == b"\xd7\xcd\xc6\x9a\x00\x00":
90 # placeable windows metafile
92 # get units per inch
93 self._inch = word(s, 14)
95 # get bounding box
96 x0 = short(s, 6)
97 y0 = short(s, 8)
98 x1 = short(s, 10)
99 y1 = short(s, 12)
101 # normalize size to 72 dots per inch
102 self.info["dpi"] = 72
103 size = (
104 (x1 - x0) * self.info["dpi"] // self._inch,
105 (y1 - y0) * self.info["dpi"] // self._inch,
106 )
108 self.info["wmf_bbox"] = x0, y0, x1, y1
110 # sanity check (standard metafile header)
111 if s[22:26] != b"\x01\x00\t\x00":
112 raise SyntaxError("Unsupported WMF file format")
114 elif s[:4] == b"\x01\x00\x00\x00" and s[40:44] == b" EMF":
115 # enhanced metafile
117 # get bounding box
118 x0 = _long(s, 8)
119 y0 = _long(s, 12)
120 x1 = _long(s, 16)
121 y1 = _long(s, 20)
123 # get frame (in 0.01 millimeter units)
124 frame = _long(s, 24), _long(s, 28), _long(s, 32), _long(s, 36)
126 size = x1 - x0, y1 - y0
128 # calculate dots per inch from bbox and frame
129 xdpi = 2540.0 * (x1 - y0) / (frame[2] - frame[0])
130 ydpi = 2540.0 * (y1 - y0) / (frame[3] - frame[1])
132 self.info["wmf_bbox"] = x0, y0, x1, y1
134 if xdpi == ydpi:
135 self.info["dpi"] = xdpi
136 else:
137 self.info["dpi"] = xdpi, ydpi
139 else:
140 raise SyntaxError("Unsupported file format")
142 self.mode = "RGB"
143 self._size = size
145 loader = self._load()
146 if loader:
147 loader.open(self)
149 def _load(self):
150 return _handler
152 def load(self, dpi=None):
153 if dpi is not None and self._inch is not None:
154 self.info["dpi"] = dpi
155 x0, y0, x1, y1 = self.info["wmf_bbox"]
156 self._size = (
157 (x1 - x0) * self.info["dpi"] // self._inch,
158 (y1 - y0) * self.info["dpi"] // self._inch,
159 )
160 return super().load()
163def _save(im, fp, filename):
164 if _handler is None or not hasattr(_handler, "save"):
165 raise OSError("WMF save handler not installed")
166 _handler.save(im, fp, filename)
169#
170# --------------------------------------------------------------------
171# Registry stuff
174Image.register_open(WmfStubImageFile.format, WmfStubImageFile, _accept)
175Image.register_save(WmfStubImageFile.format, _save)
177Image.register_extensions(WmfStubImageFile.format, [".wmf", ".emf"])