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

« back to all changes in this revision

Viewing changes to .pc/git-updates.diff/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:
 
1
#
 
2
# The Python Imaging Library.
 
3
# $Id$
 
4
#
 
5
# TGA file handling
 
6
#
 
7
# History:
 
8
# 95-09-01 fl   created (reads 24-bit files only)
 
9
# 97-01-04 fl   support more TGA versions, including compressed images
 
10
# 98-07-04 fl   fixed orientation and alpha layer bugs
 
11
# 98-09-11 fl   fixed orientation for runlength decoder
 
12
#
 
13
# Copyright (c) Secret Labs AB 1997-98.
 
14
# Copyright (c) Fredrik Lundh 1995-97.
 
15
#
 
16
# See the README file for information on usage and redistribution.
 
17
#
 
18
 
 
19
 
 
20
__version__ = "0.3"
 
21
 
 
22
import Image, ImageFile, ImagePalette
 
23
 
 
24
 
 
25
#
 
26
# --------------------------------------------------------------------
 
27
# Read RGA file
 
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)
 
34
 
 
35
 
 
36
MODES = {
 
37
    # map imagetype/depth to rawmode
 
38
    (1, 8):  "P",
 
39
    (3, 1):  "1",
 
40
    (3, 8):  "L",
 
41
    (2, 16): "BGR;5",
 
42
    (2, 24): "BGR",
 
43
    (2, 32): "BGRA",
 
44
}
 
45
 
 
46
 
 
47
def _accept(prefix):
 
48
    return prefix[0] == "\0"
 
49
 
 
50
##
 
51
# Image plugin for Targa files.
 
52
 
 
53
class TgaImageFile(ImageFile.ImageFile):
 
54
 
 
55
    format = "TGA"
 
56
    format_description = "Targa"
 
57
 
 
58
    def _open(self):
 
59
 
 
60
        # process header
 
61
        s = self.fp.read(18)
 
62
 
 
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])
 
71
 
 
72
        self.size = i16(s[12:]), i16(s[14:])
 
73
 
 
74
        # validate header fields
 
75
        if id != 0 or colormaptype not in (0, 1) or\
 
76
           self.size[0] <= 0 or self.size[1] <= 0 or\
 
77
           depth not in (1, 8, 16, 24, 32):
 
78
            raise SyntaxError, "not a TGA file"
 
79
 
 
80
        # image mode
 
81
        if imagetype in (3, 11):
 
82
            self.mode = "L"
 
83
            if depth == 1:
 
84
                self.mode = "1" # ???
 
85
        elif imagetype in (1, 9):
 
86
            self.mode = "P"
 
87
        elif imagetype in (2, 10):
 
88
            self.mode = "RGB"
 
89
            if depth == 32:
 
90
                self.mode = "RGBA"
 
91
        else:
 
92
            raise SyntaxError, "unknown TGA mode"
 
93
 
 
94
        # orientation
 
95
        orientation = flags & 0x30
 
96
        if orientation == 0x20:
 
97
            orientation = 1
 
98
        elif not orientation:
 
99
            orientation = -1
 
100
        else:
 
101
            raise SyntaxError, "unknown TGA orientation"
 
102
 
 
103
        self.info["orientation"] = orientation
 
104
 
 
105
        if imagetype & 8:
 
106
            self.info["compression"] = "tga_rle"
 
107
 
 
108
        if colormaptype:
 
109
            # read palette
 
110
            start, size, mapdepth = i16(s[3:]), i16(s[5:]), i16(s[7:])
 
111
            if mapdepth == 16:
 
112
                self.palette = ImagePalette.raw("BGR;16",
 
113
                    "\0"*2*start + self.fp.read(2*size))
 
114
            elif mapdepth == 24:
 
115
                self.palette = ImagePalette.raw("BGR",
 
116
                    "\0"*3*start + self.fp.read(3*size))
 
117
            elif mapdepth == 32:
 
118
                self.palette = ImagePalette.raw("BGRA",
 
119
                    "\0"*4*start + self.fp.read(4*size))
 
120
 
 
121
        # setup tile descriptor
 
122
        try:
 
123
            rawmode = MODES[(imagetype&7, depth)]
 
124
            if imagetype & 8:
 
125
                # compressed
 
126
                self.tile = [("tga_rle", (0, 0)+self.size,
 
127
                              self.fp.tell(), (rawmode, orientation, depth))]
 
128
            else:
 
129
                self.tile = [("raw", (0, 0)+self.size,
 
130
                              self.fp.tell(), (rawmode, 0, orientation))]
 
131
        except KeyError:
 
132
            pass # cannot decode
 
133
 
 
134
#
 
135
# --------------------------------------------------------------------
 
136
# Write TGA file
 
137
 
 
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)
 
143
 
 
144
SAVE = {
 
145
    "1": ("1", 1, 0, 3),
 
146
    "L": ("L", 8, 0, 3),
 
147
    "P": ("P", 8, 1, 1),
 
148
    "RGB": ("BGR", 24, 0, 2),
 
149
    "RGBA": ("BGRA", 32, 0, 2),
 
150
}
 
151
 
 
152
def _save(im, fp, filename, check=0):
 
153
 
 
154
    try:
 
155
        rawmode, bits, colormaptype, imagetype = SAVE[im.mode]
 
156
    except KeyError:
 
157
        raise IOError("cannot write mode %s as TGA" % im.mode)
 
158
 
 
159
    if check:
 
160
        return check
 
161
 
 
162
    if colormaptype:
 
163
        colormapfirst, colormaplength, colormapentry = 0, 256, 24
 
164
    else:
 
165
        colormapfirst, colormaplength, colormapentry = 0, 0, 0
 
166
 
 
167
    if im.mode == "RGBA":
 
168
        flags = 8
 
169
    else:
 
170
        flags = 0
 
171
 
 
172
    orientation = im.info.get("orientation", -1)
 
173
    if orientation > 0:
 
174
        flags = flags | 0x20
 
175
 
 
176
    fp.write("\000" +
 
177
             chr(colormaptype) +
 
178
             chr(imagetype) +
 
179
             o16(colormapfirst) +
 
180
             o16(colormaplength) +
 
181
             chr(colormapentry) +
 
182
             o16(0) +
 
183
             o16(0) +
 
184
             o16(im.size[0]) +
 
185
             o16(im.size[1]) +
 
186
             chr(bits) +
 
187
             chr(flags))
 
188
 
 
189
    if colormaptype:
 
190
        fp.write(im.im.getpalette("RGB", "BGR"))
 
191
 
 
192
    ImageFile._save(im, fp, [("raw", (0,0)+im.size, 0, (rawmode, 0, orientation))])
 
193
 
 
194
#
 
195
# --------------------------------------------------------------------
 
196
# Registry
 
197
 
 
198
Image.register_open("TGA", TgaImageFile, _accept)
 
199
Image.register_save("TGA", _save)
 
200
 
 
201
Image.register_extension("TGA", ".tga")