2
# The Python Imaging Library
8
# 1996-12-14 fl Created
9
# 2004-02-22 fl Turned into a stub driver
10
# 2004-02-23 fl Added EMF support
12
# Copyright (c) Secret Labs AB 1997-2004. All rights reserved.
13
# Copyright (c) Fredrik Lundh 1996.
15
# See the README file for information on usage and redistribution.
20
import Image, ImageFile
25
# Install application-specific WMF image handler.
27
# @param handler Handler object.
29
def register_handler(handler):
33
if hasattr(Image.core, "drawwmf"):
34
# install default handler (windows only)
40
self.bbox = im.info["wmf_bbox"]
43
im.fp.seek(0) # rewind
44
return Image.fromstring(
46
Image.core.drawwmf(im.fp.read(), im.size, self.bbox),
47
"raw", "BGR", (im.size[0]*3 + 3) & -4, -1
50
register_handler(WmfHandler())
52
# --------------------------------------------------------------------
55
return ord(c[o]) + (ord(c[o+1])<<8)
58
v = ord(c[o]) + (ord(c[o+1])<<8)
64
return ord(c[o]) + (ord(c[o+1])<<8) + (ord(c[o+2])<<16) + (ord(c[o+3])<<24)
70
# --------------------------------------------------------------------
75
prefix[:6] == "\xd7\xcd\xc6\x9a\x00\x00" or
76
prefix[:4] == "\x01\x00\x00\x00"
80
# Image plugin for Windows metafiles.
82
class WmfStubImageFile(ImageFile.StubImageFile):
85
format_description = "Windows Metafile"
89
# check placable header
92
if s[:6] == "\xd7\xcd\xc6\x9a\x00\x00":
94
# placeable windows metafile
100
x0 = short(s, 6); y0 = short(s, 8)
101
x1 = short(s, 10); y1 = short(s, 12)
103
# normalize size to 72 dots per inch
104
size = (x1 - x0) * 72 / inch, (y1 - y0) * 72 / inch
106
self.info["wmf_bbox"] = x0, y0, x1, y1
108
self.info["dpi"] = 72
110
# print self.mode, self.size, self.info
112
# sanity check (standard metafile header)
113
if s[22:26] != "\x01\x00\t\x00":
114
raise SyntaxError("Unsupported WMF file format")
116
elif long(s) == 1 and s[40:44] == " EMF":
120
x0 = long(s, 8); y0 = long(s, 12)
121
x1 = long(s, 16); 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
# normalize size to 72 dots per inch
127
size = x1 - x0, y1 - y0
129
# calculate dots per inch from bbox and frame
130
xdpi = 2540 * (x1 - y0) / (frame[2] - frame[0])
131
ydpi = 2540 * (y1 - y0) / (frame[3] - frame[1])
133
self.info["wmf_bbox"] = x0, y0, x1, y1
136
self.info["dpi"] = xdpi
138
self.info["dpi"] = xdpi, ydpi
141
raise SyntaxError("Unsupported file format")
146
loader = self._load()
154
def _save(im, fp, filename):
155
if _handler is None or not hasattr("_handler", "save"):
156
raise IOError("WMF save handler not installed")
157
_handler.save(im, fp, filename)
160
# --------------------------------------------------------------------
163
Image.register_open(WmfStubImageFile.format, WmfStubImageFile, _accept)
164
Image.register_save(WmfStubImageFile.format, _save)
166
Image.register_extension(WmfStubImageFile.format, ".wmf")
167
Image.register_extension(WmfStubImageFile.format, ".emf")