~ubuntu-branches/ubuntu/wily/python-imaging/wily

« back to all changes in this revision

Viewing changes to PIL/EpsImagePlugin.py

  • Committer: Package Import Robot
  • Author(s): Matthias Klose
  • Date: 2013-01-31 20:49:20 UTC
  • mfrom: (27.1.1 raring-proposed)
  • Revision ID: package-import@ubuntu.com-20130131204920-b5zshy6vgfvdionl
Tags: 1.1.7+1.7.8-1ubuntu1
Rewrite build dependencies to allow cross builds.

Show diffs side-by-side

added added

removed removed

Lines of Context:
20
20
 
21
21
__version__ = "0.5"
22
22
 
23
 
import re, string
24
 
import Image, ImageFile
 
23
import re
 
24
import io
 
25
from . import Image, ImageFile, _binary
25
26
 
26
27
#
27
28
# --------------------------------------------------------------------
28
29
 
29
 
def i32(c):
30
 
    return ord(c[0]) + (ord(c[1])<<8) + (ord(c[2])<<16) + (ord(c[3])<<24)
31
 
 
32
 
def o32(i):
33
 
    return chr(i&255) + chr(i>>8&255) + chr(i>>16&255) + chr(i>>24&255)
 
30
i32 = _binary.i32le
 
31
o32 = _binary.o32le
34
32
 
35
33
split = re.compile(r"^%%([^:]*):[ \t]*(.*)[ \t]*$")
36
34
field = re.compile(r"^%[%!\w]([^:]*)[ \t]*$")
55
53
               "-sOutputFile=%s" % file,# output file
56
54
               "- >/dev/null 2>/dev/null"]
57
55
 
58
 
    command = string.join(command)
 
56
    command = " ".join(command)
59
57
 
60
58
    # push data through ghostscript
61
59
    try:
93
91
    def seek(self, offset, whence=0):
94
92
        self.char = None
95
93
        self.fp.seek(offset, whence)
 
94
    def read(self, count):
 
95
        return self.fp.read(count).decode('latin-1')
96
96
    def tell(self):
97
97
        pos = self.fp.tell()
98
98
        if self.char:
99
99
            pos = pos - 1
100
100
        return pos
101
101
    def readline(self):
102
 
        s = ""
 
102
        s = b""
103
103
        if self.char:
104
104
            c = self.char
105
105
            self.char = None
106
106
        else:
107
107
            c = self.fp.read(1)
108
 
        while c not in "\r\n":
 
108
        while c not in b"\r\n":
109
109
            s = s + c
110
110
            c = self.fp.read(1)
111
 
        if c == "\r":
 
111
        if c == b"\r":
112
112
            self.char = self.fp.read(1)
113
 
            if self.char == "\n":
 
113
            if self.char == b"\n":
114
114
                self.char = None
115
 
        return s + "\n"
 
115
        return s.decode('latin-1') + "\n"
116
116
 
117
117
 
118
118
def _accept(prefix):
119
 
    return prefix[:4] == "%!PS" or i32(prefix) == 0xC6D3D0C5L
 
119
    return prefix[:4] == b"%!PS" or i32(prefix) == 0xC6D3D0C5
120
120
 
121
121
##
122
122
# Image plugin for Encapsulated Postscript.  This plugin supports only
141
141
            offset = 0
142
142
            fp.seek(0, 2)
143
143
            length = fp.tell()
144
 
        elif i32(s) == 0xC6D3D0C5L:
 
144
        elif i32(s) == 0xC6D3D0C5:
145
145
            offset = i32(s[4:])
146
146
            length = i32(s[8:])
147
147
            fp.seek(offset)
148
148
        else:
149
 
            raise SyntaxError, "not an EPS file"
 
149
            raise SyntaxError("not an EPS file")
150
150
 
151
151
        fp.seek(offset)
152
152
 
163
163
        while s:
164
164
 
165
165
            if len(s) > 255:
166
 
                raise SyntaxError, "not an EPS file"
 
166
                raise SyntaxError("not an EPS file")
167
167
 
168
168
            if s[-2:] == '\r\n':
169
169
                s = s[:-2]
172
172
 
173
173
            try:
174
174
                m = split.match(s)
175
 
            except re.error, v:
176
 
                raise SyntaxError, "not an EPS file"
 
175
            except re.error as v:
 
176
                raise SyntaxError("not an EPS file")
177
177
 
178
178
            if m:
179
179
                k, v = m.group(1, 2)
183
183
                        # Note: The DSC spec says that BoundingBox
184
184
                        # fields should be integers, but some drivers
185
185
                        # put floating point values there anyway.
186
 
                        box = map(int, map(float, string.split(v)))
 
186
                        box = [int(float(s)) for s in v.split()]
187
187
                        self.size = box[2] - box[0], box[3] - box[1]
188
188
                        self.tile = [("eps", (0,0) + self.size, offset,
189
189
                                      (length, box))]
196
196
 
197
197
                if m:
198
198
                    k = m.group(1)
 
199
 
199
200
                    if k == "EndComments":
200
201
                        break
201
202
                    if k[:8] == "PS-Adobe":
202
203
                        self.info[k[:8]] = k[9:]
203
204
                    else:
204
205
                        self.info[k] = ""
 
206
                elif s[0:1] == '%':
 
207
                    # handle non-DSC Postscript comments that some
 
208
                    # tools mistakenly put in the Comments section
 
209
                    pass
205
210
                else:
206
 
                    raise IOError, "bad EPS header"
 
211
                    raise IOError("bad EPS header")
207
212
 
208
213
            s = fp.readline()
209
214
 
217
222
        while s[0] == "%":
218
223
 
219
224
            if len(s) > 255:
220
 
                raise SyntaxError, "not an EPS file"
 
225
                raise SyntaxError("not an EPS file")
221
226
 
222
227
            if s[-2:] == '\r\n':
223
228
                s = s[:-2]
227
232
            if s[:11] == "%ImageData:":
228
233
 
229
234
                [x, y, bi, mo, z3, z4, en, id] =\
230
 
                    string.split(s[11:], maxsplit=7)
 
235
                    s[11:].split(None, 7)
231
236
 
232
237
                x = int(x); y = int(y)
233
238
 
257
262
                    id = id[1:-1]
258
263
 
259
264
                # Scan forward to the actual image data
260
 
                while 1:
 
265
                while True:
261
266
                    s = fp.readline()
262
267
                    if not s:
263
268
                        break
274
279
                break
275
280
 
276
281
        if not box:
277
 
            raise IOError, "cannot determine EPS bounding box"
 
282
            raise IOError("cannot determine EPS bounding box")
278
283
 
279
284
    def load(self):
280
285
        # Load EPS via Ghostscript
304
309
    elif im.mode == "CMYK":
305
310
        operator = (8, 4, "false 4 colorimage")
306
311
    else:
307
 
        raise ValueError, "image mode is not supported"
 
312
        raise ValueError("image mode is not supported")
 
313
 
 
314
    class NoCloseStream:
 
315
        def __init__(self, fp):
 
316
            self.fp = fp
 
317
        def __getattr__(self, name):
 
318
            return getattr(self.fp, name)
 
319
        def close(self):
 
320
            pass
 
321
 
 
322
    base_fp = fp
 
323
    fp = io.TextIOWrapper(NoCloseStream(fp), encoding='latin-1')
308
324
 
309
325
    if eps:
310
326
        #
328
344
    fp.write("%d %d 8\n" % im.size) # <= bits
329
345
    fp.write("[%d 0 0 -%d 0 %d]\n" % (im.size[0], im.size[1], im.size[1]))
330
346
    fp.write("{ currentfile buf readhexstring pop } bind\n")
331
 
    fp.write("%s\n" % operator[2])
 
347
    fp.write(operator[2] + "\n")
 
348
    fp.flush()
332
349
 
333
 
    ImageFile._save(im, fp, [("eps", (0,0)+im.size, 0, None)])
 
350
    ImageFile._save(im, base_fp, [("eps", (0,0)+im.size, 0, None)])
334
351
 
335
352
    fp.write("\n%%%%EndBinary\n")
336
353
    fp.write("grestore end\n")