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

« back to all changes in this revision

Viewing changes to PIL/ArgImagePlugin.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
# See the README file for information on usage and redistribution.
19
19
#
20
20
 
 
21
from __future__ import print_function
 
22
 
21
23
__version__ = "0.4"
22
24
 
23
 
import Image, ImageFile, ImagePalette
24
 
 
25
 
from PngImagePlugin import i16, i32, ChunkStream, _MODES
26
 
 
27
 
MAGIC = "\212ARG\r\n\032\n"
 
25
from . import Image, ImageFile, ImagePalette
 
26
 
 
27
from .PngImagePlugin import i8, i16, i32, ChunkStream, _MODES
 
28
 
 
29
MAGIC = b"\212ARG\r\n\032\n"
28
30
 
29
31
# --------------------------------------------------------------------
30
32
# ARG parser
60
62
 
61
63
        # assertions
62
64
        if self.count != 0:
63
 
            raise SyntaxError, "misplaced AHDR chunk"
 
65
            raise SyntaxError("misplaced AHDR chunk")
64
66
 
65
67
        s = self.fp.read(bytes)
66
68
        self.size = i32(s), i32(s[4:])
67
69
        try:
68
 
            self.mode, self.rawmode = _MODES[(ord(s[8]), ord(s[9]))]
 
70
            self.mode, self.rawmode = _MODES[(i8(s[8]), i8(s[9]))]
69
71
        except:
70
 
            raise SyntaxError, "unknown ARG mode"
 
72
            raise SyntaxError("unknown ARG mode")
71
73
 
72
74
        if Image.DEBUG:
73
 
            print "AHDR size", self.size
74
 
            print "AHDR mode", self.mode, self.rawmode
 
75
            print("AHDR size", self.size)
 
76
            print("AHDR mode", self.mode, self.rawmode)
75
77
 
76
78
        return s
77
79
 
80
82
 
81
83
        # assertions
82
84
        if self.count != 0:
83
 
            raise SyntaxError, "misplaced AFRM chunk"
 
85
            raise SyntaxError("misplaced AFRM chunk")
84
86
 
85
87
        self.show = 1
86
88
        self.id = 0
98
100
                    self.repair = None
99
101
 
100
102
        if Image.DEBUG:
101
 
            print "AFRM", self.id, self.count
 
103
            print("AFRM", self.id, self.count)
102
104
 
103
105
        return s
104
106
 
107
109
 
108
110
        # assertions
109
111
        if self.count != 0:
110
 
            raise SyntaxError, "misplaced ADEF chunk"
 
112
            raise SyntaxError("misplaced ADEF chunk")
111
113
 
112
114
        self.show = 0
113
115
        self.id = 0
121
123
                self.count = i16(s[2:4])
122
124
 
123
125
        if Image.DEBUG:
124
 
            print "ADEF", self.id, self.count
 
126
            print("ADEF", self.id, self.count)
125
127
 
126
128
        return s
127
129
 
130
132
 
131
133
        # assertions
132
134
        if self.count == 0:
133
 
            raise SyntaxError, "misplaced NAME chunk"
 
135
            raise SyntaxError("misplaced NAME chunk")
134
136
 
135
137
        name = self.fp.read(bytes)
136
138
        self.names[self.id] = name
141
143
        "AEND -- end of animation"
142
144
 
143
145
        if Image.DEBUG:
144
 
            print "AEND"
 
146
            print("AEND")
145
147
 
146
148
        self.eof = 1
147
149
 
148
 
        raise EOFError, "end of ARG file"
 
150
        raise EOFError("end of ARG file")
149
151
 
150
152
    def __getmodesize(self, s, full=1):
151
153
 
152
154
        size = i32(s), i32(s[4:])
153
155
 
154
156
        try:
155
 
            mode, rawmode = _MODES[(ord(s[8]), ord(s[9]))]
 
157
            mode, rawmode = _MODES[(i8(s[8]), i8(s[9]))]
156
158
        except:
157
 
            raise SyntaxError, "unknown image mode"
 
159
            raise SyntaxError("unknown image mode")
158
160
 
159
161
        if full:
160
 
            if ord(s[12]):
 
162
            if i8(s[12]):
161
163
                pass # interlace not yet supported
162
 
            if ord(s[11]):
163
 
                raise SyntaxError, "unknown filter category"
 
164
            if i8(s[11]):
 
165
                raise SyntaxError("unknown filter category")
164
166
 
165
167
        return size, mode, rawmode
166
168
 
169
171
 
170
172
        # assertions
171
173
        if self.count == 0:
172
 
            raise SyntaxError, "misplaced PAST chunk"
 
174
            raise SyntaxError("misplaced PAST chunk")
173
175
 
174
176
        if self.repair is not None:
175
177
            # we must repair the target image before we
206
208
 
207
209
        # assertions
208
210
        if self.count == 0:
209
 
            raise SyntaxError, "misplaced BLNK chunk"
 
211
            raise SyntaxError("misplaced BLNK chunk")
210
212
 
211
213
        s = self.fp.read(bytes)
212
214
        size, mode, rawmode = self.__getmodesize(s, 0)
223
225
 
224
226
        # assertions
225
227
        if self.count == 0:
226
 
            raise SyntaxError, "misplaced IHDR chunk"
 
228
            raise SyntaxError("misplaced IHDR chunk")
227
229
 
228
230
        # image header
229
231
        s = self.fp.read(bytes)
234
236
        self.im = Image.core.new(mode, size)
235
237
        self.decoder = Image.core.zip_decoder(rawmode)
236
238
        self.decoder.setimage(self.im, (0,0) + size)
237
 
        self.data = ""
 
239
        self.data = b""
238
240
 
239
241
        return s
240
242
 
243
245
 
244
246
        # assertions
245
247
        if self.count == 0:
246
 
            raise SyntaxError, "misplaced DHDR chunk"
 
248
            raise SyntaxError("misplaced DHDR chunk")
247
249
 
248
250
        s = self.fp.read(bytes)
249
251
 
250
252
        size, mode, rawmode = self.__getmodesize(s)
251
253
 
252
254
        # delta header
253
 
        diff = ord(s[13])
 
255
        diff = i8(s[13])
254
256
        offs = i32(s[14:18]), i32(s[18:22])
255
257
 
256
258
        bbox = offs + (offs[0]+size[0], offs[1]+size[1])
257
259
 
258
260
        if Image.DEBUG:
259
 
            print "DHDR", diff, bbox
 
261
            print("DHDR", diff, bbox)
260
262
 
261
263
        # FIXME: decode and apply image
262
264
        self.action = ("DHDR", diff, bbox)
267
269
        self.decoder = Image.core.zip_decoder(rawmode)
268
270
        self.decoder.setimage(self.im, (0,0) + size)
269
271
 
270
 
        self.data = ""
 
272
        self.data = b""
271
273
 
272
274
        return s
273
275
 
276
278
 
277
279
        # assertions
278
280
        if self.count == 0:
279
 
            raise SyntaxError, "misplaced JHDR chunk"
 
281
            raise SyntaxError("misplaced JHDR chunk")
280
282
 
281
283
        # image header
282
284
        s = self.fp.read(bytes)
287
289
        self.im = Image.core.new(mode, size)
288
290
        self.decoder = Image.core.jpeg_decoder(rawmode)
289
291
        self.decoder.setimage(self.im, (0,0) + size)
290
 
        self.data = ""
 
292
        self.data = b""
291
293
 
292
294
        return s
293
295
 
296
298
 
297
299
        # assertions
298
300
        if self.count == 0:
299
 
            raise SyntaxError, "misplaced UHDR chunk"
 
301
            raise SyntaxError("misplaced UHDR chunk")
300
302
 
301
303
        # image header
302
304
        s = self.fp.read(bytes)
307
309
        self.im = Image.core.new(mode, size)
308
310
        self.decoder = Image.core.raw_decoder(rawmode)
309
311
        self.decoder.setimage(self.im, (0,0) + size)
310
 
        self.data = ""
 
312
        self.data = b""
311
313
 
312
314
        return s
313
315
 
321
323
        if n < 0:
322
324
            # end of image
323
325
            if e < 0:
324
 
                raise IOError, "decoder error %d" % e
 
326
                raise IOError("decoder error %d" % e)
325
327
        else:
326
328
            self.data = self.data[n:]
327
329
 
386
388
        "SYNC -- reset decoder"
387
389
 
388
390
        if self.count != 0:
389
 
            raise SyntaxError, "misplaced sYNC chunk"
 
391
            raise SyntaxError("misplaced sYNC chunk")
390
392
 
391
393
        s = self.fp.read(bytes)
392
394
        self.__reset()
418
420
                )
419
421
 
420
422
        if self.fp.read(8) != MAGIC:
421
 
            raise SyntaxError, "not an ARG file"
 
423
            raise SyntaxError("not an ARG file")
422
424
 
423
425
        self.arg = ArgStream(self.fp)
424
426
 
427
429
        cid, offset, bytes = self.arg.read()
428
430
 
429
431
        if cid != "AHDR":
430
 
            raise SyntaxError, "expected an AHDR chunk"
 
432
            raise SyntaxError("expected an AHDR chunk")
431
433
 
432
434
        s = self.arg.call(cid, offset, bytes)
433
435
 
452
454
    def seek(self, frame):
453
455
 
454
456
        if self.arg.eof:
455
 
            raise EOFError, "end of animation"
 
457
            raise EOFError("end of animation")
456
458
 
457
459
        self.fp = self.arg.fp
458
460
 
459
 
        while 1:
 
461
        while True:
460
462
 
461
463
            #
462
464
            # process chunks
464
466
            cid, offset, bytes = self.arg.read()
465
467
 
466
468
            if self.arg.eof:
467
 
                raise EOFError, "end of animation"
 
469
                raise EOFError("end of animation")
468
470
 
469
471
            try:
470
472
                s = self.arg.call(cid, offset, bytes)
473
475
 
474
476
            except "glurk": # AttributeError
475
477
                if Image.DEBUG:
476
 
                    print cid, bytes, "(unknown)"
 
478
                    print(cid, bytes, "(unknown)")
477
479
                s = self.fp.read(bytes)
478
480
 
479
481
            self.arg.crc(cid, s)