Coverage for /pythoncovmergedfiles/medio/medio/usr/local/lib/python3.11/site-packages/PIL/TgaImagePlugin.py: 64%

Shortcuts on this page

r m x   toggle line displays

j k   next/prev highlighted chunk

0   (zero) top of page

1   (one) first highlighted chunk

136 statements  

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# 

18from __future__ import annotations 

19 

20__lazy_modules__ = {"PIL._binary", "warnings"} 

21 

22import os 

23import warnings 

24 

25from . import Image, ImageFile, ImagePalette 

26from ._binary import i16le as i16 

27from ._binary import i32le as i32 

28from ._binary import o8 

29from ._binary import o16le as o16 

30 

31TYPE_CHECKING = False 

32if TYPE_CHECKING: 

33 from typing import IO 

34 

35# 

36# -------------------------------------------------------------------- 

37# Read RGA file 

38 

39 

40MODES = { 

41 # map imagetype/depth to rawmode 

42 (1, 8): "P", 

43 (3, 1): "1", 

44 (3, 8): "L", 

45 (3, 16): "LA", 

46 (2, 16): "BGRA;15Z", 

47 (2, 24): "BGR", 

48 (2, 32): "BGRA", 

49} 

50 

51 

52## 

53# Image plugin for Targa files. 

54 

55 

56class TgaImageFile(ImageFile.ImageFile): 

57 format = "TGA" 

58 format_description = "Targa" 

59 

60 def _open(self) -> None: 

61 # process header 

62 assert self.fp is not None 

63 

64 s = self.fp.read(18) 

65 

66 id_len = s[0] 

67 

68 colormaptype = s[1] 

69 imagetype = s[2] 

70 

71 depth = s[16] 

72 

73 flags = s[17] 

74 

75 self._size = i16(s, 12), i16(s, 14) 

76 

77 # validate header fields 

78 if ( 

79 colormaptype not in (0, 1) 

80 or self.size[0] <= 0 

81 or self.size[1] <= 0 

82 or depth not in (1, 8, 16, 24, 32) 

83 ): 

84 msg = "not a TGA file" 

85 raise SyntaxError(msg) 

86 

87 # image mode 

88 if imagetype in (3, 11): 

89 self._mode = "L" 

90 if depth == 1: 

91 self._mode = "1" # ??? 

92 elif depth == 16: 

93 self._mode = "LA" 

94 elif imagetype in (1, 9): 

95 self._mode = "P" if colormaptype else "L" 

96 elif imagetype in (2, 10): 

97 self._mode = "RGB" if depth == 24 else "RGBA" 

98 else: 

99 msg = "unknown TGA mode" 

100 raise SyntaxError(msg) 

101 

102 # orientation 

103 orientation = flags & 0x30 

104 self._flip_horizontally = orientation in [0x10, 0x30] 

105 if orientation in [0x20, 0x30]: 

106 orientation = 1 

107 elif orientation in [0, 0x10]: 

108 orientation = -1 

109 else: 

110 msg = "unknown TGA orientation" 

111 raise SyntaxError(msg) 

112 

113 self.info["orientation"] = orientation 

114 

115 if imagetype & 8: 

116 self.info["compression"] = "tga_rle" 

117 

118 if id_len: 

119 self.info["id_section"] = self.fp.read(id_len) 

120 

121 if colormaptype: 

122 # read palette 

123 start, size, mapdepth = i16(s, 3), i16(s, 5), s[7] 

124 if mapdepth == 16: 

125 self.palette = ImagePalette.raw( 

126 "BGRA;15Z", bytes(2 * start) + self.fp.read(2 * size) 

127 ) 

128 self.palette.mode = "RGBA" 

129 elif mapdepth == 24: 

130 self.palette = ImagePalette.raw( 

131 "BGR", bytes(3 * start) + self.fp.read(3 * size) 

132 ) 

133 elif mapdepth == 32: 

134 self.palette = ImagePalette.raw( 

135 "BGRA", bytes(4 * start) + self.fp.read(4 * size) 

136 ) 

137 self.palette.mode = "RGBA" 

138 else: 

139 msg = "unknown TGA map depth" 

140 raise SyntaxError(msg) 

141 

142 # setup tile descriptor 

143 try: 

144 rawmode = MODES[(imagetype & 7, depth)] 

145 if imagetype & 8: 

146 # compressed 

147 self.tile = [ 

148 ImageFile._Tile( 

149 "tga_rle", 

150 None, 

151 self.fp.tell(), 

152 (rawmode, orientation, depth), 

153 ) 

154 ] 

155 else: 

156 self.tile = [ 

157 ImageFile._Tile( 

158 "raw", 

159 None, 

160 self.fp.tell(), 

161 (rawmode, 0, orientation), 

162 ) 

163 ] 

164 except KeyError: 

165 pass # cannot decode 

166 

167 def load_end(self) -> None: 

168 if self.mode == "RGBA": 

169 assert self.fp is not None 

170 self.fp.seek(-26, os.SEEK_END) 

171 footer = self.fp.read(26) 

172 if footer.endswith(b"TRUEVISION-XFILE.\x00"): 

173 # version 2 

174 extension_offset = i32(footer) 

175 if extension_offset: 

176 self.fp.seek(extension_offset + 494) 

177 attributes_type = self.fp.read(1) 

178 if attributes_type == b"\x00": 

179 # No alpha 

180 self.im.fillband(3, 255) 

181 

182 if self._flip_horizontally: 

183 self.im = self.im.transpose(Image.Transpose.FLIP_LEFT_RIGHT) 

184 

185 

186# 

187# -------------------------------------------------------------------- 

188# Write TGA file 

189 

190 

191SAVE = { 

192 "1": ("1", 1, 0, 3), 

193 "L": ("L", 8, 0, 3), 

194 "LA": ("LA", 16, 0, 3), 

195 "P": ("P", 8, 1, 1), 

196 "RGB": ("BGR", 24, 0, 2), 

197 "RGBA": ("BGRA", 32, 0, 2), 

198} 

199 

200 

201def _save(im: Image.Image, fp: IO[bytes], filename: str | bytes) -> None: 

202 try: 

203 rawmode, bits, colormaptype, imagetype = SAVE[im.mode] 

204 except KeyError as e: 

205 msg = f"cannot write mode {im.mode} as TGA" 

206 raise OSError(msg) from e 

207 

208 if "rle" in im.encoderinfo: 

209 rle = im.encoderinfo["rle"] 

210 else: 

211 compression = im.encoderinfo.get("compression", im.info.get("compression")) 

212 rle = compression == "tga_rle" 

213 if rle: 

214 if im.mode == "1": 

215 msg = f"cannot write mode {im.mode} as TGA with run-length encoding" 

216 raise OSError(msg) 

217 

218 imagetype += 8 

219 

220 id_section = im.encoderinfo.get("id_section", im.info.get("id_section", "")) 

221 id_len = len(id_section) 

222 if id_len > 255: 

223 id_len = 255 

224 id_section = id_section[:255] 

225 warnings.warn("id_section has been trimmed to 255 characters") 

226 

227 if colormaptype: 

228 assert im.palette is not None 

229 if im.palette.mode == "RGBA": 

230 palette = im.im.getpalette("BGRA") 

231 colormaplength, colormapentry = len(palette) // 4, 32 

232 else: 

233 palette = im.im.getpalette("BGR") 

234 colormaplength, colormapentry = len(palette) // 3, 24 

235 else: 

236 colormaplength, colormapentry = 0, 0 

237 

238 if im.mode in ("LA", "RGBA"): 

239 flags = 8 

240 else: 

241 flags = 0 

242 

243 orientation = im.encoderinfo.get("orientation", im.info.get("orientation", -1)) 

244 if orientation > 0: 

245 flags = flags | 0x20 

246 

247 fp.write( 

248 o8(id_len) 

249 + o8(colormaptype) 

250 + o8(imagetype) 

251 + o16(0) # colormapfirst 

252 + o16(colormaplength) 

253 + o8(colormapentry) 

254 + o16(0) 

255 + o16(0) 

256 + o16(im.size[0]) 

257 + o16(im.size[1]) 

258 + o8(bits) 

259 + o8(flags) 

260 ) 

261 

262 if id_section: 

263 fp.write(id_section) 

264 

265 if colormaptype: 

266 fp.write(palette) 

267 

268 if rle: 

269 ImageFile._save( 

270 im, 

271 fp, 

272 [ImageFile._Tile("tga_rle", args=(rawmode, orientation))], 

273 ) 

274 else: 

275 ImageFile._save( 

276 im, 

277 fp, 

278 [ImageFile._Tile("raw", args=(rawmode, 0, orientation))], 

279 ) 

280 

281 # write targa version 2 footer 

282 fp.write(b"\000" * 8 + b"TRUEVISION-XFILE." + b"\000") 

283 

284 

285# 

286# -------------------------------------------------------------------- 

287# Registry 

288 

289 

290Image.register_open(TgaImageFile.format, TgaImageFile) 

291Image.register_save(TgaImageFile.format, _save) 

292 

293Image.register_extensions(TgaImageFile.format, [".tga", ".icb", ".vda", ".vst"]) 

294 

295Image.register_mime(TgaImageFile.format, "image/x-tga")