2010-07-31 06:52:47 +04:00
|
|
|
#
|
|
|
|
# The Python Imaging Library.
|
|
|
|
# $Id$
|
|
|
|
#
|
|
|
|
# TGA file handling
|
|
|
|
#
|
|
|
|
# History:
|
|
|
|
# 95-09-01 fl created (reads 24-bit files only)
|
|
|
|
# 97-01-04 fl support more TGA versions, including compressed images
|
|
|
|
# 98-07-04 fl fixed orientation and alpha layer bugs
|
|
|
|
# 98-09-11 fl fixed orientation for runlength decoder
|
|
|
|
#
|
|
|
|
# Copyright (c) Secret Labs AB 1997-98.
|
|
|
|
# Copyright (c) Fredrik Lundh 1995-97.
|
|
|
|
#
|
|
|
|
# See the README file for information on usage and redistribution.
|
|
|
|
#
|
|
|
|
|
|
|
|
|
2019-07-06 23:40:53 +03:00
|
|
|
import warnings
|
|
|
|
|
2017-01-17 16:22:18 +03:00
|
|
|
from . import Image, ImageFile, ImagePalette
|
2017-03-03 13:32:31 +03:00
|
|
|
from ._binary import i8, i16le as i16, o8, o16le as o16
|
2010-07-31 06:52:47 +04:00
|
|
|
|
|
|
|
#
|
|
|
|
# --------------------------------------------------------------------
|
|
|
|
# Read RGA file
|
|
|
|
|
|
|
|
|
|
|
|
MODES = {
|
|
|
|
# map imagetype/depth to rawmode
|
2019-03-21 16:28:20 +03:00
|
|
|
(1, 8): "P",
|
|
|
|
(3, 1): "1",
|
|
|
|
(3, 8): "L",
|
2018-06-14 12:18:08 +03:00
|
|
|
(3, 16): "LA",
|
2010-07-31 06:52:47 +04:00
|
|
|
(2, 16): "BGR;5",
|
|
|
|
(2, 24): "BGR",
|
|
|
|
(2, 32): "BGRA",
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
##
|
|
|
|
# Image plugin for Targa files.
|
|
|
|
|
2019-03-21 16:28:20 +03:00
|
|
|
|
2010-07-31 06:52:47 +04:00
|
|
|
class TgaImageFile(ImageFile.ImageFile):
|
|
|
|
|
|
|
|
format = "TGA"
|
|
|
|
format_description = "Targa"
|
|
|
|
|
|
|
|
def _open(self):
|
|
|
|
|
|
|
|
# process header
|
|
|
|
s = self.fp.read(18)
|
|
|
|
|
2018-09-08 12:02:03 +03:00
|
|
|
id_len = i8(s[0])
|
2010-07-31 06:52:47 +04:00
|
|
|
|
py3k: The big push
There are two main issues fixed with this commit:
* bytes vs. str: All file, image, and palette data are now handled as
bytes. A new _binary module consolidates the hacks needed to do this
across Python versions. tostring/fromstring methods have been renamed to
tobytes/frombytes, but the Python 2.6/2.7 versions alias them to the old
names for compatibility. Users should move to tobytes/frombytes.
One other potentially-breaking change is that text data in image files
(such as tags, comments) are now explicitly handled with a specific
character encoding in mind. This works well with the Unicode str in
Python 3, but may trip up old code expecting a straight byte-for-byte
translation to a Python string. This also required a change to Gohlke's
tags tests (in Tests/test_file_png.py) to expect Unicode strings from
the code.
* True div vs. floor div: Many division operations used the "/" operator
to do floor division, which is now the "//" operator in Python 3. These
were fixed.
As of this commit, on the first pass, I have one failing test (improper
handling of a slice object in a C module, test_imagepath.py) in Python 3,
and three that that I haven't tried running yet (test_imagegl,
test_imagegrab, and test_imageqt). I also haven't tested anything on
Windows. All but the three skipped tests run flawlessly against Pythons
2.6 and 2.7.
2012-10-21 01:01:53 +04:00
|
|
|
colormaptype = i8(s[1])
|
|
|
|
imagetype = i8(s[2])
|
2010-07-31 06:52:47 +04:00
|
|
|
|
py3k: The big push
There are two main issues fixed with this commit:
* bytes vs. str: All file, image, and palette data are now handled as
bytes. A new _binary module consolidates the hacks needed to do this
across Python versions. tostring/fromstring methods have been renamed to
tobytes/frombytes, but the Python 2.6/2.7 versions alias them to the old
names for compatibility. Users should move to tobytes/frombytes.
One other potentially-breaking change is that text data in image files
(such as tags, comments) are now explicitly handled with a specific
character encoding in mind. This works well with the Unicode str in
Python 3, but may trip up old code expecting a straight byte-for-byte
translation to a Python string. This also required a change to Gohlke's
tags tests (in Tests/test_file_png.py) to expect Unicode strings from
the code.
* True div vs. floor div: Many division operations used the "/" operator
to do floor division, which is now the "//" operator in Python 3. These
were fixed.
As of this commit, on the first pass, I have one failing test (improper
handling of a slice object in a C module, test_imagepath.py) in Python 3,
and three that that I haven't tried running yet (test_imagegl,
test_imagegrab, and test_imageqt). I also haven't tested anything on
Windows. All but the three skipped tests run flawlessly against Pythons
2.6 and 2.7.
2012-10-21 01:01:53 +04:00
|
|
|
depth = i8(s[16])
|
2010-07-31 06:52:47 +04:00
|
|
|
|
py3k: The big push
There are two main issues fixed with this commit:
* bytes vs. str: All file, image, and palette data are now handled as
bytes. A new _binary module consolidates the hacks needed to do this
across Python versions. tostring/fromstring methods have been renamed to
tobytes/frombytes, but the Python 2.6/2.7 versions alias them to the old
names for compatibility. Users should move to tobytes/frombytes.
One other potentially-breaking change is that text data in image files
(such as tags, comments) are now explicitly handled with a specific
character encoding in mind. This works well with the Unicode str in
Python 3, but may trip up old code expecting a straight byte-for-byte
translation to a Python string. This also required a change to Gohlke's
tags tests (in Tests/test_file_png.py) to expect Unicode strings from
the code.
* True div vs. floor div: Many division operations used the "/" operator
to do floor division, which is now the "//" operator in Python 3. These
were fixed.
As of this commit, on the first pass, I have one failing test (improper
handling of a slice object in a C module, test_imagepath.py) in Python 3,
and three that that I haven't tried running yet (test_imagegl,
test_imagegrab, and test_imageqt). I also haven't tested anything on
Windows. All but the three skipped tests run flawlessly against Pythons
2.6 and 2.7.
2012-10-21 01:01:53 +04:00
|
|
|
flags = i8(s[17])
|
2010-07-31 06:52:47 +04:00
|
|
|
|
2018-09-30 05:58:02 +03:00
|
|
|
self._size = i16(s[12:]), i16(s[14:])
|
2010-07-31 06:52:47 +04:00
|
|
|
|
|
|
|
# validate header fields
|
2019-03-21 16:28:20 +03:00
|
|
|
if (
|
|
|
|
colormaptype not in (0, 1)
|
|
|
|
or self.size[0] <= 0
|
|
|
|
or self.size[1] <= 0
|
|
|
|
or depth not in (1, 8, 16, 24, 32)
|
|
|
|
):
|
2012-10-11 07:52:53 +04:00
|
|
|
raise SyntaxError("not a TGA file")
|
2010-07-31 06:52:47 +04:00
|
|
|
|
|
|
|
# image mode
|
|
|
|
if imagetype in (3, 11):
|
|
|
|
self.mode = "L"
|
|
|
|
if depth == 1:
|
2014-08-20 10:32:06 +04:00
|
|
|
self.mode = "1" # ???
|
2018-06-14 12:18:08 +03:00
|
|
|
elif depth == 16:
|
|
|
|
self.mode = "LA"
|
2010-07-31 06:52:47 +04:00
|
|
|
elif imagetype in (1, 9):
|
|
|
|
self.mode = "P"
|
|
|
|
elif imagetype in (2, 10):
|
|
|
|
self.mode = "RGB"
|
|
|
|
if depth == 32:
|
|
|
|
self.mode = "RGBA"
|
|
|
|
else:
|
2012-10-11 07:52:53 +04:00
|
|
|
raise SyntaxError("unknown TGA mode")
|
2010-07-31 06:52:47 +04:00
|
|
|
|
|
|
|
# orientation
|
|
|
|
orientation = flags & 0x30
|
|
|
|
if orientation == 0x20:
|
|
|
|
orientation = 1
|
|
|
|
elif not orientation:
|
|
|
|
orientation = -1
|
|
|
|
else:
|
2012-10-11 07:52:53 +04:00
|
|
|
raise SyntaxError("unknown TGA orientation")
|
2010-07-31 06:52:47 +04:00
|
|
|
|
|
|
|
self.info["orientation"] = orientation
|
|
|
|
|
|
|
|
if imagetype & 8:
|
|
|
|
self.info["compression"] = "tga_rle"
|
|
|
|
|
2018-09-08 12:02:03 +03:00
|
|
|
if id_len:
|
|
|
|
self.info["id_section"] = self.fp.read(id_len)
|
2014-08-19 17:53:51 +04:00
|
|
|
|
2010-07-31 06:52:47 +04:00
|
|
|
if colormaptype:
|
|
|
|
# read palette
|
|
|
|
start, size, mapdepth = i16(s[3:]), i16(s[5:]), i16(s[7:])
|
|
|
|
if mapdepth == 16:
|
2014-08-20 10:32:06 +04:00
|
|
|
self.palette = ImagePalette.raw(
|
2019-03-21 16:28:20 +03:00
|
|
|
"BGR;16", b"\0" * 2 * start + self.fp.read(2 * size)
|
|
|
|
)
|
2010-07-31 06:52:47 +04:00
|
|
|
elif mapdepth == 24:
|
2014-08-20 10:32:06 +04:00
|
|
|
self.palette = ImagePalette.raw(
|
2019-03-21 16:28:20 +03:00
|
|
|
"BGR", b"\0" * 3 * start + self.fp.read(3 * size)
|
|
|
|
)
|
2010-07-31 06:52:47 +04:00
|
|
|
elif mapdepth == 32:
|
2014-08-20 10:32:06 +04:00
|
|
|
self.palette = ImagePalette.raw(
|
2019-03-21 16:28:20 +03:00
|
|
|
"BGRA", b"\0" * 4 * start + self.fp.read(4 * size)
|
|
|
|
)
|
2010-07-31 06:52:47 +04:00
|
|
|
|
|
|
|
# setup tile descriptor
|
|
|
|
try:
|
2014-08-20 10:32:06 +04:00
|
|
|
rawmode = MODES[(imagetype & 7, depth)]
|
2010-07-31 06:52:47 +04:00
|
|
|
if imagetype & 8:
|
|
|
|
# compressed
|
2019-03-21 16:28:20 +03:00
|
|
|
self.tile = [
|
|
|
|
(
|
|
|
|
"tga_rle",
|
|
|
|
(0, 0) + self.size,
|
|
|
|
self.fp.tell(),
|
|
|
|
(rawmode, orientation, depth),
|
|
|
|
)
|
|
|
|
]
|
2010-07-31 06:52:47 +04:00
|
|
|
else:
|
2019-03-21 16:28:20 +03:00
|
|
|
self.tile = [
|
|
|
|
(
|
|
|
|
"raw",
|
|
|
|
(0, 0) + self.size,
|
|
|
|
self.fp.tell(),
|
|
|
|
(rawmode, 0, orientation),
|
|
|
|
)
|
|
|
|
]
|
2010-07-31 06:52:47 +04:00
|
|
|
except KeyError:
|
2014-08-20 10:32:06 +04:00
|
|
|
pass # cannot decode
|
2010-07-31 06:52:47 +04:00
|
|
|
|
2019-03-21 16:28:20 +03:00
|
|
|
|
2010-07-31 06:52:47 +04:00
|
|
|
#
|
|
|
|
# --------------------------------------------------------------------
|
|
|
|
# Write TGA file
|
|
|
|
|
2018-03-03 12:54:00 +03:00
|
|
|
|
2010-07-31 06:52:47 +04:00
|
|
|
SAVE = {
|
|
|
|
"1": ("1", 1, 0, 3),
|
|
|
|
"L": ("L", 8, 0, 3),
|
2018-06-14 12:18:08 +03:00
|
|
|
"LA": ("LA", 16, 0, 3),
|
2010-07-31 06:52:47 +04:00
|
|
|
"P": ("P", 8, 1, 1),
|
|
|
|
"RGB": ("BGR", 24, 0, 2),
|
|
|
|
"RGBA": ("BGRA", 32, 0, 2),
|
|
|
|
}
|
|
|
|
|
2014-08-20 10:32:06 +04:00
|
|
|
|
2017-09-01 13:04:22 +03:00
|
|
|
def _save(im, fp, filename):
|
2010-07-31 06:52:47 +04:00
|
|
|
|
|
|
|
try:
|
|
|
|
rawmode, bits, colormaptype, imagetype = SAVE[im.mode]
|
|
|
|
except KeyError:
|
2019-09-30 17:56:31 +03:00
|
|
|
raise OSError("cannot write mode %s as TGA" % im.mode)
|
2010-07-31 06:52:47 +04:00
|
|
|
|
2018-09-01 16:31:39 +03:00
|
|
|
if "rle" in im.encoderinfo:
|
|
|
|
rle = im.encoderinfo["rle"]
|
|
|
|
else:
|
2019-03-21 16:28:20 +03:00
|
|
|
compression = im.encoderinfo.get("compression", im.info.get("compression"))
|
2018-09-01 16:31:39 +03:00
|
|
|
rle = compression == "tga_rle"
|
2018-06-15 23:01:06 +03:00
|
|
|
if rle:
|
|
|
|
imagetype += 8
|
|
|
|
|
2019-03-21 16:28:20 +03:00
|
|
|
id_section = im.encoderinfo.get("id_section", im.info.get("id_section", ""))
|
2018-09-08 12:02:03 +03:00
|
|
|
id_len = len(id_section)
|
|
|
|
if id_len > 255:
|
|
|
|
id_len = 255
|
2018-09-08 01:26:32 +03:00
|
|
|
id_section = id_section[:255]
|
|
|
|
warnings.warn("id_section has been trimmed to 255 characters")
|
2018-09-01 16:31:39 +03:00
|
|
|
|
2010-07-31 06:52:47 +04:00
|
|
|
if colormaptype:
|
|
|
|
colormapfirst, colormaplength, colormapentry = 0, 256, 24
|
|
|
|
else:
|
|
|
|
colormapfirst, colormaplength, colormapentry = 0, 0, 0
|
|
|
|
|
2018-06-14 12:18:08 +03:00
|
|
|
if im.mode in ("LA", "RGBA"):
|
2010-07-31 06:52:47 +04:00
|
|
|
flags = 8
|
|
|
|
else:
|
|
|
|
flags = 0
|
|
|
|
|
2019-03-21 16:28:20 +03:00
|
|
|
orientation = im.encoderinfo.get("orientation", im.info.get("orientation", -1))
|
2010-07-31 06:52:47 +04:00
|
|
|
if orientation > 0:
|
|
|
|
flags = flags | 0x20
|
|
|
|
|
2019-03-21 16:28:20 +03:00
|
|
|
fp.write(
|
|
|
|
o8(id_len)
|
|
|
|
+ o8(colormaptype)
|
|
|
|
+ o8(imagetype)
|
|
|
|
+ o16(colormapfirst)
|
|
|
|
+ o16(colormaplength)
|
|
|
|
+ o8(colormapentry)
|
|
|
|
+ o16(0)
|
|
|
|
+ o16(0)
|
|
|
|
+ o16(im.size[0])
|
|
|
|
+ o16(im.size[1])
|
|
|
|
+ o8(bits)
|
|
|
|
+ o8(flags)
|
|
|
|
)
|
2010-07-31 06:52:47 +04:00
|
|
|
|
2018-09-01 16:31:39 +03:00
|
|
|
if id_section:
|
|
|
|
fp.write(id_section)
|
|
|
|
|
2010-07-31 06:52:47 +04:00
|
|
|
if colormaptype:
|
|
|
|
fp.write(im.im.getpalette("RGB", "BGR"))
|
|
|
|
|
2018-06-15 23:01:06 +03:00
|
|
|
if rle:
|
|
|
|
ImageFile._save(
|
2019-03-21 16:28:20 +03:00
|
|
|
im, fp, [("tga_rle", (0, 0) + im.size, 0, (rawmode, orientation))]
|
|
|
|
)
|
2018-06-15 23:01:06 +03:00
|
|
|
else:
|
|
|
|
ImageFile._save(
|
2019-03-21 16:28:20 +03:00
|
|
|
im, fp, [("raw", (0, 0) + im.size, 0, (rawmode, 0, orientation))]
|
|
|
|
)
|
2010-07-31 06:52:47 +04:00
|
|
|
|
2017-07-24 23:49:18 +03:00
|
|
|
# write targa version 2 footer
|
2017-09-06 02:13:31 +03:00
|
|
|
fp.write(b"\000" * 8 + b"TRUEVISION-XFILE." + b"\000")
|
2010-07-31 06:52:47 +04:00
|
|
|
|
2019-03-21 16:28:20 +03:00
|
|
|
|
2010-07-31 06:52:47 +04:00
|
|
|
#
|
|
|
|
# --------------------------------------------------------------------
|
|
|
|
# Registry
|
|
|
|
|
2018-03-03 12:54:00 +03:00
|
|
|
|
2015-07-04 16:29:58 +03:00
|
|
|
Image.register_open(TgaImageFile.format, TgaImageFile)
|
|
|
|
Image.register_save(TgaImageFile.format, _save)
|
2010-07-31 06:52:47 +04:00
|
|
|
|
2018-12-31 09:41:41 +03:00
|
|
|
Image.register_extensions(TgaImageFile.format, [".tga", ".icb", ".vda", ".vst"])
|
|
|
|
|
|
|
|
Image.register_mime(TgaImageFile.format, "image/x-tga")
|