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

« back to all changes in this revision

Viewing changes to PIL/TgaImagePlugin.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
__version__ = "0.3"
21
21
 
22
 
import Image, ImageFile, ImagePalette
 
22
from . import Image, ImageFile, ImagePalette, _binary
23
23
 
24
24
 
25
25
#
26
26
# --------------------------------------------------------------------
27
27
# Read RGA file
28
28
 
29
 
def i16(c):
30
 
    return ord(c[0]) + (ord(c[1])<<8)
31
 
 
32
 
def i32(c):
33
 
    return ord(c[0]) + (ord(c[1])<<8) + (ord(c[2])<<16) + (ord(c[3])<<24)
 
29
i8 = _binary.i8
 
30
i16 = _binary.i16le
 
31
i32 = _binary.i32le
34
32
 
35
33
 
36
34
MODES = {
45
43
 
46
44
 
47
45
def _accept(prefix):
48
 
    return prefix[0] == "\0"
 
46
    return prefix[0:1] == b"\0"
49
47
 
50
48
##
51
49
# Image plugin for Targa files.
60
58
        # process header
61
59
        s = self.fp.read(18)
62
60
 
63
 
        id = ord(s[0])
64
 
 
65
 
        colormaptype = ord(s[1])
66
 
        imagetype = ord(s[2])
67
 
 
68
 
        depth = ord(s[16])
69
 
 
70
 
        flags = ord(s[17])
 
61
        id = i8(s[0])
 
62
 
 
63
        colormaptype = i8(s[1])
 
64
        imagetype = i8(s[2])
 
65
 
 
66
        depth = i8(s[16])
 
67
 
 
68
        flags = i8(s[17])
71
69
 
72
70
        self.size = i16(s[12:]), i16(s[14:])
73
71
 
75
73
        if id != 0 or colormaptype not in (0, 1) or\
76
74
           self.size[0] <= 0 or self.size[1] <= 0 or\
77
75
           depth not in (1, 8, 16, 24, 32):
78
 
            raise SyntaxError, "not a TGA file"
 
76
            raise SyntaxError("not a TGA file")
79
77
 
80
78
        # image mode
81
79
        if imagetype in (3, 11):
89
87
            if depth == 32:
90
88
                self.mode = "RGBA"
91
89
        else:
92
 
            raise SyntaxError, "unknown TGA mode"
 
90
            raise SyntaxError("unknown TGA mode")
93
91
 
94
92
        # orientation
95
93
        orientation = flags & 0x30
98
96
        elif not orientation:
99
97
            orientation = -1
100
98
        else:
101
 
            raise SyntaxError, "unknown TGA orientation"
 
99
            raise SyntaxError("unknown TGA orientation")
102
100
 
103
101
        self.info["orientation"] = orientation
104
102
 
110
108
            start, size, mapdepth = i16(s[3:]), i16(s[5:]), i16(s[7:])
111
109
            if mapdepth == 16:
112
110
                self.palette = ImagePalette.raw("BGR;16",
113
 
                    "\0"*2*start + self.fp.read(2*size))
 
111
                    b"\0"*2*start + self.fp.read(2*size))
114
112
            elif mapdepth == 24:
115
113
                self.palette = ImagePalette.raw("BGR",
116
 
                    "\0"*3*start + self.fp.read(3*size))
 
114
                    b"\0"*3*start + self.fp.read(3*size))
117
115
            elif mapdepth == 32:
118
116
                self.palette = ImagePalette.raw("BGRA",
119
 
                    "\0"*4*start + self.fp.read(4*size))
 
117
                    b"\0"*4*start + self.fp.read(4*size))
120
118
 
121
119
        # setup tile descriptor
122
120
        try:
135
133
# --------------------------------------------------------------------
136
134
# Write TGA file
137
135
 
138
 
def o16(i):
139
 
    return chr(i&255) + chr(i>>8&255)
140
 
 
141
 
def o32(i):
142
 
    return chr(i&255) + chr(i>>8&255) + chr(i>>16&255) + chr(i>>24&255)
 
136
o8 = _binary.o8
 
137
o16 = _binary.o16le
 
138
o32 = _binary.o32le
143
139
 
144
140
SAVE = {
145
141
    "1": ("1", 1, 0, 3),
173
169
    if orientation > 0:
174
170
        flags = flags | 0x20
175
171
 
176
 
    fp.write("\000" +
177
 
             chr(colormaptype) +
178
 
             chr(imagetype) +
 
172
    fp.write(b"\000" +
 
173
             o8(colormaptype) +
 
174
             o8(imagetype) +
179
175
             o16(colormapfirst) +
180
176
             o16(colormaplength) +
181
 
             chr(colormapentry) +
 
177
             o8(colormapentry) +
182
178
             o16(0) +
183
179
             o16(0) +
184
180
             o16(im.size[0]) +
185
181
             o16(im.size[1]) +
186
 
             chr(bits) +
187
 
             chr(flags))
 
182
             o8(bits) +
 
183
             o8(flags))
188
184
 
189
185
    if colormaptype:
190
186
        fp.write(im.im.getpalette("RGB", "BGR"))