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

« back to all changes in this revision

Viewing changes to PIL/PsdImagePlugin.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
 
19
19
__version__ = "0.4"
20
20
 
21
 
import Image, ImageFile, ImagePalette
 
21
from . import Image, ImageFile, ImagePalette, _binary
22
22
 
23
23
MODES = {
24
24
    # (photoshop mode, bits) -> (pil mode, required channels)
36
36
#
37
37
# helpers
38
38
 
39
 
def i16(c):
40
 
    return ord(c[1]) + (ord(c[0])<<8)
41
 
 
42
 
def i32(c):
43
 
    return ord(c[3]) + (ord(c[2])<<8) + (ord(c[1])<<16) + (ord(c[0])<<24)
 
39
i8 = _binary.i8
 
40
i16 = _binary.i16be
 
41
i32 = _binary.i32be
44
42
 
45
43
# --------------------------------------------------------------------.
46
44
# read PSD images
47
45
 
48
46
def _accept(prefix):
49
 
    return prefix[:4] == "8BPS"
 
47
    return prefix[:4] == b"8BPS"
50
48
 
51
49
##
52
50
# Image plugin for Photoshop images.
57
55
    format_description = "Adobe Photoshop"
58
56
 
59
57
    def _open(self):
60
 
 
 
58
        
61
59
        read = self.fp.read
62
60
 
63
61
        #
64
62
        # header
65
63
 
66
64
        s = read(26)
67
 
        if s[:4] != "8BPS" or i16(s[4:]) != 1:
68
 
            raise SyntaxError, "not a PSD file"
 
65
        if s[:4] != b"8BPS" or i16(s[4:]) != 1:
 
66
            raise SyntaxError("not a PSD file")
69
67
 
70
68
        psd_bits = i16(s[22:])
71
69
        psd_channels = i16(s[12:])
74
72
        mode, channels = MODES[(psd_mode, psd_bits)]
75
73
 
76
74
        if channels > psd_channels:
77
 
            raise IOError, "not enough channels"
 
75
            raise IOError("not enough channels")
78
76
 
79
77
        self.mode = mode
80
78
        self.size = i32(s[18:]), i32(s[14:])
100
98
            while self.fp.tell() < end:
101
99
                signature = read(4)
102
100
                id = i16(read(2))
103
 
                name = read(ord(read(1)))
 
101
                name = read(i8(read(1)))
104
102
                if not (len(name) & 1):
105
103
                    read(1) # padding
106
104
                data = read(i32(read(4)))
146
144
            self.fp = self._fp
147
145
            return name, bbox
148
146
        except IndexError:
149
 
            raise EOFError, "no such layer"
 
147
            raise EOFError("no such layer")
150
148
 
151
149
    def tell(self):
152
150
        # return layer number (0=image, 1..max=layers)
165
163
    # read layerinfo block
166
164
    layers = []
167
165
    read = file.read
168
 
 
169
166
    for i in range(abs(i16(read(2)))):
170
167
 
171
168
        # bounding box
175
172
        # image info
176
173
        info = []
177
174
        mode = []
178
 
        for i in range(i16(read(2))):
 
175
        types = list(range(i16(read(2))))
 
176
        if len(types) > 4:
 
177
            continue
 
178
 
 
179
        for i in types:
179
180
            type = i16(read(2))
 
181
 
180
182
            if type == 65535:
181
183
                m = "A"
182
184
            else:
183
 
                m = "RGB"[type]
 
185
                m = "RGBA"[type]
 
186
 
184
187
            mode.append(m)
185
188
            size = i32(read(4))
186
189
            info.append((m, size))
214
217
                file.seek(length, 1)
215
218
            combined += length + 4
216
219
 
217
 
            length = ord(read(1))
 
220
            length = i8(read(1))
218
221
            if length:
219
 
                name = read(length)
 
222
                # Don't know the proper encoding, Latin-1 should be a good guess
 
223
                name = read(length).decode('latin-1', 'replace')
220
224
            combined += length + 1
221
225
 
222
226
        file.seek(size - combined, 1)