~ubuntu-branches/ubuntu/saucy/python-imaging/saucy-proposed

« back to all changes in this revision

Viewing changes to PIL/XpmImagePlugin.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:
18
18
__version__ = "0.2"
19
19
 
20
20
 
21
 
import re, string
22
 
import Image, ImageFile, ImagePalette
 
21
import re
 
22
from . import Image, ImageFile, ImagePalette
 
23
from ._binary import i8, o8
23
24
 
24
25
# XPM header
25
 
xpm_head = re.compile("\"([0-9]*) ([0-9]*) ([0-9]*) ([0-9]*)")
 
26
xpm_head = re.compile(b"\"([0-9]*) ([0-9]*) ([0-9]*) ([0-9]*)")
26
27
 
27
28
 
28
29
def _accept(prefix):
29
 
    return prefix[:9] == "/* XPM */"
 
30
    return prefix[:9] == b"/* XPM */"
30
31
 
31
32
##
32
33
# Image plugin for X11 pixel maps.
39
40
    def _open(self):
40
41
 
41
42
        if not _accept(self.fp.read(9)):
42
 
            raise SyntaxError, "not an XPM file"
 
43
            raise SyntaxError("not an XPM file")
43
44
 
44
45
        # skip forward to next string
45
 
        while 1:
 
46
        while True:
46
47
            s = self.fp.readline()
47
48
            if not s:
48
 
                raise SyntaxError, "broken XPM file"
 
49
                raise SyntaxError("broken XPM file")
49
50
            m = xpm_head.match(s)
50
51
            if m:
51
52
                break
56
57
        bpp = int(m.group(4))
57
58
 
58
59
        if pal > 256 or bpp != 1:
59
 
            raise ValueError, "cannot read this XPM file"
 
60
            raise ValueError("cannot read this XPM file")
60
61
 
61
62
        #
62
63
        # load palette description
63
64
 
64
 
        palette = ["\0\0\0"] * 256
 
65
        palette = [b"\0\0\0"] * 256
65
66
 
66
67
        for i in range(pal):
67
68
 
68
69
            s = self.fp.readline()
69
 
            if s[-2:] == '\r\n':
 
70
            if s[-2:] == b'\r\n':
70
71
                s = s[:-2]
71
 
            elif s[-1:] in '\r\n':
 
72
            elif s[-1:] in b'\r\n':
72
73
                s = s[:-1]
73
74
 
74
 
            c = ord(s[1])
75
 
            s = string.split(s[2:-2])
 
75
            c = i8(s[1])
 
76
            s = s[2:-2].split()
76
77
 
77
78
            for i in range(0, len(s), 2):
78
79
 
79
 
                if s[i] == "c":
 
80
                if s[i] == b"c":
80
81
 
81
82
                    # process colour key
82
83
                    rgb = s[i+1]
83
 
                    if rgb == "None":
 
84
                    if rgb == b"None":
84
85
                        self.info["transparency"] = c
85
 
                    elif rgb[0] == "#":
 
86
                    elif rgb[0:1] == b"#":
86
87
                        # FIXME: handle colour names (see ImagePalette.py)
87
 
                        rgb = string.atoi(rgb[1:], 16)
88
 
                        palette[c] = chr((rgb >> 16) & 255) +\
89
 
                                     chr((rgb >> 8) & 255) +\
90
 
                                     chr(rgb & 255)
 
88
                        rgb = int(rgb[1:], 16)
 
89
                        palette[c] = o8((rgb >> 16) & 255) +\
 
90
                                     o8((rgb >> 8) & 255) +\
 
91
                                     o8(rgb & 255)
91
92
                    else:
92
93
                        # unknown colour
93
 
                        raise ValueError, "cannot read this XPM file"
 
94
                        raise ValueError("cannot read this XPM file")
94
95
                    break
95
96
 
96
97
            else:
97
98
 
98
99
                # missing colour key
99
 
                raise ValueError, "cannot read this XPM file"
 
100
                raise ValueError("cannot read this XPM file")
100
101
 
101
102
        self.mode = "P"
102
 
        self.palette = ImagePalette.raw("RGB", string.join(palette, ""))
 
103
        self.palette = ImagePalette.raw("RGB", b"".join(palette))
103
104
 
104
105
        self.tile = [("raw", (0, 0)+self.size, self.fp.tell(), ("P", 0, 1))]
105
106
 
113
114
        s = [None] * ysize
114
115
 
115
116
        for i in range(ysize):
116
 
            s[i] = string.ljust(self.fp.readline()[1:xsize+1], xsize)
 
117
            s[i] = self.fp.readline()[1:xsize+1].ljust(xsize)
117
118
 
118
119
        self.fp = None
119
120
 
120
 
        return string.join(s, "")
 
121
        return b"".join(s)
121
122
 
122
123
#
123
124
# Registry