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

« back to all changes in this revision

Viewing changes to PIL/PpmImagePlugin.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:
19
19
 
20
20
import string
21
21
 
22
 
import Image, ImageFile
 
22
from . import Image, ImageFile
23
23
 
24
24
#
25
25
# --------------------------------------------------------------------
26
26
 
 
27
b_whitespace = string.whitespace.encode()
 
28
 
27
29
MODES = {
28
30
    # standard
29
 
    "P4": "1",
30
 
    "P5": "L",
31
 
    "P6": "RGB",
 
31
    b"P4": "1",
 
32
    b"P5": "L",
 
33
    b"P6": "RGB",
32
34
    # extensions
33
 
    "P0CMYK": "CMYK",
 
35
    b"P0CMYK": "CMYK",
34
36
    # PIL extensions (for test purposes only)
35
 
    "PyP": "P",
36
 
    "PyRGBA": "RGBA",
37
 
    "PyCMYK": "CMYK"
 
37
    b"PyP": "P",
 
38
    b"PyRGBA": "RGBA",
 
39
    b"PyCMYK": "CMYK"
38
40
}
39
41
 
40
42
def _accept(prefix):
41
 
    return prefix[0] == "P" and prefix[1] in "0456y"
 
43
    return prefix[0:1] == b"P" and prefix[1] in b"0456y"
42
44
 
43
45
##
44
46
# Image plugin for PBM, PGM, and PPM images.
48
50
    format = "PPM"
49
51
    format_description = "Pbmplus image"
50
52
 
51
 
    def _token(self, s = ""):
52
 
        while 1: # read until next whitespace
 
53
    def _token(self, s = b""):
 
54
        while True: # read until next whitespace
53
55
            c = self.fp.read(1)
54
 
            if not c or c in string.whitespace:
 
56
            if not c or c in b_whitespace:
55
57
                break
56
58
            s = s + c
57
59
        return s
60
62
 
61
63
        # check magic
62
64
        s = self.fp.read(1)
63
 
        if s != "P":
64
 
            raise SyntaxError, "not a PPM file"
 
65
        if s != b"P":
 
66
            raise SyntaxError("not a PPM file")
65
67
        mode = MODES[self._token(s)]
66
68
 
67
69
        if mode == "1":
71
73
            self.mode = rawmode = mode
72
74
 
73
75
        for ix in range(3):
74
 
            while 1:
75
 
                while 1:
 
76
            while True:
 
77
                while True:
76
78
                    s = self.fp.read(1)
77
 
                    if s not in string.whitespace:
 
79
                    if s not in b_whitespace:
78
80
                        break
79
 
                if s != "#":
 
81
                if s != b"#":
80
82
                    break
81
83
                s = self.fp.readline()
82
84
            s = int(self._token(s))
103
105
 
104
106
def _save(im, fp, filename):
105
107
    if im.mode == "1":
106
 
        rawmode, head = "1;I", "P4"
 
108
        rawmode, head = "1;I", b"P4"
107
109
    elif im.mode == "L":
108
 
        rawmode, head = "L", "P5"
 
110
        rawmode, head = "L", b"P5"
109
111
    elif im.mode == "RGB":
110
 
        rawmode, head = "RGB", "P6"
 
112
        rawmode, head = "RGB", b"P6"
111
113
    elif im.mode == "RGBA":
112
 
        rawmode, head = "RGB", "P6"
 
114
        rawmode, head = "RGB", b"P6"
113
115
    else:
114
 
        raise IOError, "cannot write mode %s as PPM" % im.mode
115
 
    fp.write(head + "\n%d %d\n" % im.size)
116
 
    if head != "P4":
117
 
        fp.write("255\n")
 
116
        raise IOError("cannot write mode %s as PPM" % im.mode)
 
117
    fp.write(head + ("\n%d %d\n" % im.size).encode('ascii'))
 
118
    if head != b"P4":
 
119
        fp.write(b"255\n")
118
120
    ImageFile._save(im, fp, [("raw", (0,0)+im.size, 0, (rawmode, 0, 1))])
119
121
 
120
122
    # ALTERNATIVE: save via builtin debug function