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

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 

22from . import Image, ImageFile 

23from ._binary import i16le as word 

24from ._binary import si16le as short 

25from ._binary import si32le as _long 

26 

27_handler = None 

28 

29 

30def register_handler(handler): 

31 """ 

32 Install application-specific WMF image handler. 

33 

34 :param handler: Handler object. 

35 """ 

36 global _handler 

37 _handler = handler 

38 

39 

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) 

42 

43 class WmfHandler: 

44 def open(self, im): 

45 im.mode = "RGB" 

46 self.bbox = im.info["wmf_bbox"] 

47 

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 ) 

59 

60 register_handler(WmfHandler()) 

61 

62# 

63# -------------------------------------------------------------------- 

64# Read WMF file 

65 

66 

67def _accept(prefix): 

68 return ( 

69 prefix[:6] == b"\xd7\xcd\xc6\x9a\x00\x00" or prefix[:4] == b"\x01\x00\x00\x00" 

70 ) 

71 

72 

73## 

74# Image plugin for Windows metafiles. 

75 

76 

77class WmfStubImageFile(ImageFile.StubImageFile): 

78 

79 format = "WMF" 

80 format_description = "Windows Metafile" 

81 

82 def _open(self): 

83 self._inch = None 

84 

85 # check placable header 

86 s = self.fp.read(80) 

87 

88 if s[:6] == b"\xd7\xcd\xc6\x9a\x00\x00": 

89 

90 # placeable windows metafile 

91 

92 # get units per inch 

93 self._inch = word(s, 14) 

94 

95 # get bounding box 

96 x0 = short(s, 6) 

97 y0 = short(s, 8) 

98 x1 = short(s, 10) 

99 y1 = short(s, 12) 

100 

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 ) 

107 

108 self.info["wmf_bbox"] = x0, y0, x1, y1 

109 

110 # sanity check (standard metafile header) 

111 if s[22:26] != b"\x01\x00\t\x00": 

112 raise SyntaxError("Unsupported WMF file format") 

113 

114 elif s[:4] == b"\x01\x00\x00\x00" and s[40:44] == b" EMF": 

115 # enhanced metafile 

116 

117 # get bounding box 

118 x0 = _long(s, 8) 

119 y0 = _long(s, 12) 

120 x1 = _long(s, 16) 

121 y1 = _long(s, 20) 

122 

123 # get frame (in 0.01 millimeter units) 

124 frame = _long(s, 24), _long(s, 28), _long(s, 32), _long(s, 36) 

125 

126 size = x1 - x0, y1 - y0 

127 

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]) 

131 

132 self.info["wmf_bbox"] = x0, y0, x1, y1 

133 

134 if xdpi == ydpi: 

135 self.info["dpi"] = xdpi 

136 else: 

137 self.info["dpi"] = xdpi, ydpi 

138 

139 else: 

140 raise SyntaxError("Unsupported file format") 

141 

142 self.mode = "RGB" 

143 self._size = size 

144 

145 loader = self._load() 

146 if loader: 

147 loader.open(self) 

148 

149 def _load(self): 

150 return _handler 

151 

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() 

161 

162 

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) 

167 

168 

169# 

170# -------------------------------------------------------------------- 

171# Registry stuff 

172 

173 

174Image.register_open(WmfStubImageFile.format, WmfStubImageFile, _accept) 

175Image.register_save(WmfStubImageFile.format, _save) 

176 

177Image.register_extensions(WmfStubImageFile.format, [".wmf", ".emf"])