TgaImagePlugin.py 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248
  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. import warnings
  19. from . import Image, ImageFile, ImagePalette
  20. from ._binary import i16le as i16
  21. from ._binary import o8
  22. from ._binary import o16le as o16
  23. #
  24. # --------------------------------------------------------------------
  25. # Read RGA file
  26. MODES = {
  27. # map imagetype/depth to rawmode
  28. (1, 8): "P",
  29. (3, 1): "1",
  30. (3, 8): "L",
  31. (3, 16): "LA",
  32. (2, 16): "BGR;5",
  33. (2, 24): "BGR",
  34. (2, 32): "BGRA",
  35. }
  36. ##
  37. # Image plugin for Targa files.
  38. class TgaImageFile(ImageFile.ImageFile):
  39. format = "TGA"
  40. format_description = "Targa"
  41. def _open(self):
  42. # process header
  43. s = self.fp.read(18)
  44. id_len = s[0]
  45. colormaptype = s[1]
  46. imagetype = s[2]
  47. depth = s[16]
  48. flags = s[17]
  49. self._size = i16(s, 12), i16(s, 14)
  50. # validate header fields
  51. if (
  52. colormaptype not in (0, 1)
  53. or self.size[0] <= 0
  54. or self.size[1] <= 0
  55. or depth not in (1, 8, 16, 24, 32)
  56. ):
  57. raise SyntaxError("not a TGA file")
  58. # image mode
  59. if imagetype in (3, 11):
  60. self.mode = "L"
  61. if depth == 1:
  62. self.mode = "1" # ???
  63. elif depth == 16:
  64. self.mode = "LA"
  65. elif imagetype in (1, 9):
  66. self.mode = "P"
  67. elif imagetype in (2, 10):
  68. self.mode = "RGB"
  69. if depth == 32:
  70. self.mode = "RGBA"
  71. else:
  72. raise SyntaxError("unknown TGA mode")
  73. # orientation
  74. orientation = flags & 0x30
  75. if orientation == 0x20:
  76. orientation = 1
  77. elif not orientation:
  78. orientation = -1
  79. else:
  80. raise SyntaxError("unknown TGA orientation")
  81. self.info["orientation"] = orientation
  82. if imagetype & 8:
  83. self.info["compression"] = "tga_rle"
  84. if id_len:
  85. self.info["id_section"] = self.fp.read(id_len)
  86. if colormaptype:
  87. # read palette
  88. start, size, mapdepth = i16(s, 3), i16(s, 5), s[7]
  89. if mapdepth == 16:
  90. self.palette = ImagePalette.raw(
  91. "BGR;15", b"\0" * 2 * start + self.fp.read(2 * size)
  92. )
  93. elif mapdepth == 24:
  94. self.palette = ImagePalette.raw(
  95. "BGR", b"\0" * 3 * start + self.fp.read(3 * size)
  96. )
  97. elif mapdepth == 32:
  98. self.palette = ImagePalette.raw(
  99. "BGRA", b"\0" * 4 * start + self.fp.read(4 * size)
  100. )
  101. # setup tile descriptor
  102. try:
  103. rawmode = MODES[(imagetype & 7, depth)]
  104. if imagetype & 8:
  105. # compressed
  106. self.tile = [
  107. (
  108. "tga_rle",
  109. (0, 0) + self.size,
  110. self.fp.tell(),
  111. (rawmode, orientation, depth),
  112. )
  113. ]
  114. else:
  115. self.tile = [
  116. (
  117. "raw",
  118. (0, 0) + self.size,
  119. self.fp.tell(),
  120. (rawmode, 0, orientation),
  121. )
  122. ]
  123. except KeyError:
  124. pass # cannot decode
  125. #
  126. # --------------------------------------------------------------------
  127. # Write TGA file
  128. SAVE = {
  129. "1": ("1", 1, 0, 3),
  130. "L": ("L", 8, 0, 3),
  131. "LA": ("LA", 16, 0, 3),
  132. "P": ("P", 8, 1, 1),
  133. "RGB": ("BGR", 24, 0, 2),
  134. "RGBA": ("BGRA", 32, 0, 2),
  135. }
  136. def _save(im, fp, filename):
  137. try:
  138. rawmode, bits, colormaptype, imagetype = SAVE[im.mode]
  139. except KeyError as e:
  140. raise OSError(f"cannot write mode {im.mode} as TGA") from e
  141. if "rle" in im.encoderinfo:
  142. rle = im.encoderinfo["rle"]
  143. else:
  144. compression = im.encoderinfo.get("compression", im.info.get("compression"))
  145. rle = compression == "tga_rle"
  146. if rle:
  147. imagetype += 8
  148. id_section = im.encoderinfo.get("id_section", im.info.get("id_section", ""))
  149. id_len = len(id_section)
  150. if id_len > 255:
  151. id_len = 255
  152. id_section = id_section[:255]
  153. warnings.warn("id_section has been trimmed to 255 characters")
  154. if colormaptype:
  155. colormapfirst, colormaplength, colormapentry = 0, 256, 24
  156. else:
  157. colormapfirst, colormaplength, colormapentry = 0, 0, 0
  158. if im.mode in ("LA", "RGBA"):
  159. flags = 8
  160. else:
  161. flags = 0
  162. orientation = im.encoderinfo.get("orientation", im.info.get("orientation", -1))
  163. if orientation > 0:
  164. flags = flags | 0x20
  165. fp.write(
  166. o8(id_len)
  167. + o8(colormaptype)
  168. + o8(imagetype)
  169. + o16(colormapfirst)
  170. + o16(colormaplength)
  171. + o8(colormapentry)
  172. + o16(0)
  173. + o16(0)
  174. + o16(im.size[0])
  175. + o16(im.size[1])
  176. + o8(bits)
  177. + o8(flags)
  178. )
  179. if id_section:
  180. fp.write(id_section)
  181. if colormaptype:
  182. fp.write(im.im.getpalette("RGB", "BGR"))
  183. if rle:
  184. ImageFile._save(
  185. im, fp, [("tga_rle", (0, 0) + im.size, 0, (rawmode, orientation))]
  186. )
  187. else:
  188. ImageFile._save(
  189. im, fp, [("raw", (0, 0) + im.size, 0, (rawmode, 0, orientation))]
  190. )
  191. # write targa version 2 footer
  192. fp.write(b"\000" * 8 + b"TRUEVISION-XFILE." + b"\000")
  193. #
  194. # --------------------------------------------------------------------
  195. # Registry
  196. Image.register_open(TgaImageFile.format, TgaImageFile)
  197. Image.register_save(TgaImageFile.format, _save)
  198. Image.register_extensions(TgaImageFile.format, [".tga", ".icb", ".vda", ".vst"])
  199. Image.register_mime(TgaImageFile.format, "image/x-tga")