~ubuntu-branches/ubuntu/raring/python-imaging/raring-updates

« back to all changes in this revision

Viewing changes to PIL/IptcImagePlugin.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
Rewrite build dependencies to allow cross builds.

Show diffs side-by-side

added added

removed removed

Lines of Context:
15
15
# See the README file for information on usage and redistribution.
16
16
#
17
17
 
 
18
from __future__ import print_function
18
19
 
19
20
__version__ = "0.3"
20
21
 
21
22
 
22
 
import Image, ImageFile
 
23
from . import Image, ImageFile, _binary
23
24
import os, tempfile
24
25
 
 
26
i8 = _binary.i8
 
27
i16 = _binary.i16be
 
28
i32 = _binary.i32be
 
29
o8 = _binary.o8
25
30
 
26
31
COMPRESSION = {
27
32
    1: "raw",
28
33
    5: "jpeg"
29
34
}
30
35
 
31
 
PAD = chr(0) * 4
 
36
PAD = o8(0) * 4
32
37
 
33
38
#
34
39
# Helpers
35
40
 
36
 
def i16(c):
37
 
    return ord(c[1]) + (ord(c[0])<<8)
38
 
 
39
 
def i32(c):
40
 
    return ord(c[3]) + (ord(c[2])<<8) + (ord(c[1])<<16) + (ord(c[0])<<24)
41
 
 
42
41
def i(c):
43
42
    return i32((PAD + c)[-4:])
44
43
 
45
44
def dump(c):
46
45
    for i in c:
47
 
        print "%02x" % ord(i),
48
 
    print
 
46
        print("%02x" % i8(i), end=' ')
 
47
    print()
49
48
 
50
49
##
51
50
# Image plugin for IPTC/NAA datastreams.  To read IPTC/NAA fields
66
65
        if not len(s):
67
66
            return None, 0
68
67
 
69
 
        tag = ord(s[1]), ord(s[2])
 
68
        tag = i8(s[1]), i8(s[2])
70
69
 
71
70
        # syntax
72
 
        if ord(s[0]) != 0x1C or tag[0] < 1 or tag[0] > 9:
73
 
            raise SyntaxError, "invalid IPTC/NAA file"
 
71
        if i8(s[0]) != 0x1C or tag[0] < 1 or tag[0] > 9:
 
72
            raise SyntaxError("invalid IPTC/NAA file")
74
73
 
75
74
        # field size
76
 
        size = ord(s[3])
 
75
        size = i8(s[3])
77
76
        if size > 132:
78
 
            raise IOError, "illegal field length in IPTC/NAA file"
 
77
            raise IOError("illegal field length in IPTC/NAA file")
79
78
        elif size == 128:
80
79
            size = 0
81
80
        elif size > 128:
97
96
        if sz != size[0]:
98
97
            return 0
99
98
        y = 1
100
 
        while 1:
 
99
        while True:
101
100
            self.fp.seek(sz, 1)
102
101
            t, s = self.field()
103
102
            if t != (8, 10):
110
109
    def _open(self):
111
110
 
112
111
        # load descriptive fields
113
 
        while 1:
 
112
        while True:
114
113
            offset = self.fp.tell()
115
114
            tag, size = self.field()
116
115
            if not tag or tag == (8,10):
119
118
                tagdata = self.fp.read(size)
120
119
            else:
121
120
                tagdata = None
122
 
            if tag in self.info.keys():
 
121
            if tag in list(self.info.keys()):
123
122
                if isinstance(self.info[tag], list):
124
123
                    self.info[tag].append(tagdata)
125
124
                else:
130
129
            # print tag, self.info[tag]
131
130
 
132
131
        # mode
133
 
        layers = ord(self.info[(3,60)][0])
134
 
        component = ord(self.info[(3,60)][1])
135
 
        if self.info.has_key((3,65)):
136
 
            id = ord(self.info[(3,65)][0])-1
 
132
        layers = i8(self.info[(3,60)][0])
 
133
        component = i8(self.info[(3,60)][1])
 
134
        if (3,65) in self.info:
 
135
            id = i8(self.info[(3,65)][0])-1
137
136
        else:
138
137
            id = 0
139
138
        if layers == 1 and not component:
150
149
        try:
151
150
            compression = COMPRESSION[self.getint((3,120))]
152
151
        except KeyError:
153
 
            raise IOError, "Unknown IPTC image compression"
 
152
            raise IOError("Unknown IPTC image compression")
154
153
 
155
154
        # tile
156
155
        if tag == (8,10):
179
178
            # To simplify access to the extracted file,
180
179
            # prepend a PPM header
181
180
            o.write("P5\n%d %d\n255\n" % self.size)
182
 
        while 1:
 
181
        while True:
183
182
            type, size = self.field()
184
183
            if type != (8, 10):
185
184
                break
218
217
 
219
218
def getiptcinfo(im):
220
219
 
221
 
    import TiffImagePlugin, JpegImagePlugin
222
 
    import StringIO
 
220
    from . import TiffImagePlugin, JpegImagePlugin
 
221
    import io
223
222
 
224
223
    data = None
225
224
 
241
240
                    code = JpegImagePlugin.i16(app, offset)
242
241
                    offset = offset + 2
243
242
                    # resource name (usually empty)
244
 
                    name_len = ord(app[offset])
 
243
                    name_len = i8(app[offset])
245
244
                    name = app[offset+1:offset+1+name_len]
246
245
                    offset = 1 + offset + name_len
247
246
                    if offset & 1:
278
277
 
279
278
    # parse the IPTC information chunk
280
279
    im.info = {}
281
 
    im.fp = StringIO.StringIO(data)
 
280
    im.fp = io.BytesIO(data)
282
281
 
283
282
    try:
284
283
        im._open()