~ubuntu-branches/debian/stretch/electrum/stretch

« back to all changes in this revision

Viewing changes to gui/kivy/tools/.buildozer/android/platform/python-for-android/dist/kivy/python-install/lib/python2.7/site-packages/PIL/WmfImagePlugin.py

  • Committer: Package Import Robot
  • Author(s): Tristan Seligmann
  • Date: 2016-04-04 03:02:39 UTC
  • mfrom: (1.1.10)
  • Revision ID: package-import@ubuntu.com-20160404030239-0szgkio8yryjv7c9
Tags: 2.6.3-1
* New upstream release.
  - Drop backported install-wizard-connect.patch.
* Add Suggests: python-zbar and update the installation hint to suggest
  apt-get instead of pip (closes: #819517).
* Bump Standards-Version to 3.9.7 (no changes).
* Update Vcs-* links.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
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
 
 
18
__version__ = "0.2"
 
19
 
 
20
import Image, ImageFile
 
21
 
 
22
_handler = None
 
23
 
 
24
##
 
25
# Install application-specific WMF image handler.
 
26
#
 
27
# @param handler Handler object.
 
28
 
 
29
def register_handler(handler):
 
30
    global _handler
 
31
    _handler = handler
 
32
 
 
33
if hasattr(Image.core, "drawwmf"):
 
34
    # install default handler (windows only)
 
35
 
 
36
    class WmfHandler:
 
37
 
 
38
        def open(self, im):
 
39
            im.mode = "RGB"
 
40
            self.bbox = im.info["wmf_bbox"]
 
41
 
 
42
        def load(self, im):
 
43
            im.fp.seek(0) # rewind
 
44
            return Image.fromstring(
 
45
                "RGB", im.size,
 
46
                Image.core.drawwmf(im.fp.read(), im.size, self.bbox),
 
47
                "raw", "BGR", (im.size[0]*3 + 3) & -4, -1
 
48
                )
 
49
 
 
50
    register_handler(WmfHandler())
 
51
 
 
52
# --------------------------------------------------------------------
 
53
 
 
54
def word(c, o=0):
 
55
    return ord(c[o]) + (ord(c[o+1])<<8)
 
56
 
 
57
def short(c, o=0):
 
58
    v = ord(c[o]) + (ord(c[o+1])<<8)
 
59
    if v >= 32768:
 
60
        v = v - 65536
 
61
    return v
 
62
 
 
63
def dword(c, o=0):
 
64
    return ord(c[o]) + (ord(c[o+1])<<8) + (ord(c[o+2])<<16) + (ord(c[o+3])<<24)
 
65
 
 
66
def long(c, o=0):
 
67
    return dword(c, o)
 
68
 
 
69
#
 
70
# --------------------------------------------------------------------
 
71
# Read WMF file
 
72
 
 
73
def _accept(prefix):
 
74
    return (
 
75
        prefix[:6] == "\xd7\xcd\xc6\x9a\x00\x00" or
 
76
        prefix[:4] == "\x01\x00\x00\x00"
 
77
        )
 
78
 
 
79
##
 
80
# Image plugin for Windows metafiles.
 
81
 
 
82
class WmfStubImageFile(ImageFile.StubImageFile):
 
83
 
 
84
    format = "WMF"
 
85
    format_description = "Windows Metafile"
 
86
 
 
87
    def _open(self):
 
88
 
 
89
        # check placable header
 
90
        s = self.fp.read(80)
 
91
 
 
92
        if s[:6] == "\xd7\xcd\xc6\x9a\x00\x00":
 
93
 
 
94
            # placeable windows metafile
 
95
 
 
96
            # get units per inch
 
97
            inch = word(s, 14)
 
98
 
 
99
            # get bounding box
 
100
            x0 = short(s, 6); y0 = short(s, 8)
 
101
            x1 = short(s, 10); y1 = short(s, 12)
 
102
 
 
103
            # normalize size to 72 dots per inch
 
104
            size = (x1 - x0) * 72 / inch, (y1 - y0) * 72 / inch
 
105
 
 
106
            self.info["wmf_bbox"] = x0, y0, x1, y1
 
107
 
 
108
            self.info["dpi"] = 72
 
109
 
 
110
            # print self.mode, self.size, self.info
 
111
 
 
112
            # sanity check (standard metafile header)
 
113
            if s[22:26] != "\x01\x00\t\x00":
 
114
                raise SyntaxError("Unsupported WMF file format")
 
115
 
 
116
        elif long(s) == 1 and s[40:44] == " EMF":
 
117
            # enhanced metafile
 
118
 
 
119
            # get bounding box
 
120
            x0 = long(s, 8); y0 = long(s, 12)
 
121
            x1 = long(s, 16); 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
            # normalize size to 72 dots per inch
 
127
            size = x1 - x0, y1 - y0
 
128
 
 
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])
 
132
 
 
133
            self.info["wmf_bbox"] = x0, y0, x1, y1
 
134
 
 
135
            if xdpi == ydpi:
 
136
                self.info["dpi"] = xdpi
 
137
            else:
 
138
                self.info["dpi"] = xdpi, ydpi
 
139
 
 
140
        else:
 
141
            raise SyntaxError("Unsupported file format")
 
142
 
 
143
        self.mode = "RGB"
 
144
        self.size = size
 
145
 
 
146
        loader = self._load()
 
147
        if loader:
 
148
            loader.open(self)
 
149
 
 
150
    def _load(self):
 
151
        return _handler
 
152
 
 
153
 
 
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)
 
158
 
 
159
#
 
160
# --------------------------------------------------------------------
 
161
# Registry stuff
 
162
 
 
163
Image.register_open(WmfStubImageFile.format, WmfStubImageFile, _accept)
 
164
Image.register_save(WmfStubImageFile.format, _save)
 
165
 
 
166
Image.register_extension(WmfStubImageFile.format, ".wmf")
 
167
Image.register_extension(WmfStubImageFile.format, ".emf")