2013-10-11 17:28:32 +00: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.
|
|
|
|
#
|
|
|
|
|
|
|
|
|
2014-09-30 16:15:32 +00:00
|
|
|
from PIL import Image, ImageFile, ImagePalette, _binary
|
2013-10-11 17:28:32 +00:00
|
|
|
|
2015-11-25 01:25:01 +00:00
|
|
|
__version__ = "0.3"
|
|
|
|
|
2013-10-11 17:28:32 +00:00
|
|
|
|
|
|
|
#
|
|
|
|
# --------------------------------------------------------------------
|
|
|
|
# Read RGA file
|
|
|
|
|
2014-09-30 16:15:32 +00:00
|
|
|
i8 = _binary.i8
|
|
|
|
i16 = _binary.i16le
|
2013-10-11 17:28:32 +00:00
|
|
|
|
|
|
|
|
|
|
|
MODES = {
|
|
|
|
# map imagetype/depth to rawmode
|
|
|
|
(1, 8): "P",
|
|
|
|
(3, 1): "1",
|
|
|
|
(3, 8): "L",
|
|
|
|
(2, 16): "BGR;5",
|
|
|
|
(2, 24): "BGR",
|
|
|
|
(2, 32): "BGRA",
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
##
|
|
|
|
# Image plugin for Targa files.
|
|
|
|
|
|
|
|
class TgaImageFile(ImageFile.ImageFile):
|
|
|
|
|
|
|
|
format = "TGA"
|
|
|
|
format_description = "Targa"
|
|
|
|
|
|
|
|
def _open(self):
|
|
|
|
|
|
|
|
# process header
|
|
|
|
s = self.fp.read(18)
|
|
|
|
|
2015-11-25 01:25:01 +00:00
|
|
|
idlen = i8(s[0])
|
2013-10-11 17:28:32 +00:00
|
|
|
|
2014-09-30 16:15:32 +00:00
|
|
|
colormaptype = i8(s[1])
|
|
|
|
imagetype = i8(s[2])
|
2013-10-11 17:28:32 +00:00
|
|
|
|
2014-09-30 16:15:32 +00:00
|
|
|
depth = i8(s[16])
|
2013-10-11 17:28:32 +00:00
|
|
|
|
2014-09-30 16:15:32 +00:00
|
|
|
flags = i8(s[17])
|
2013-10-11 17:28:32 +00:00
|
|
|
|
|
|
|
self.size = i16(s[12:]), i16(s[14:])
|
|
|
|
|
|
|
|
# validate header fields
|
2015-11-25 01:25:01 +00:00
|
|
|
if colormaptype not in (0, 1) or\
|
2013-10-11 17:28:32 +00:00
|
|
|
self.size[0] <= 0 or self.size[1] <= 0 or\
|
|
|
|
depth not in (1, 8, 16, 24, 32):
|
2014-09-30 16:15:32 +00:00
|
|
|
raise SyntaxError("not a TGA file")
|
2013-10-11 17:28:32 +00:00
|
|
|
|
|
|
|
# image mode
|
|
|
|
if imagetype in (3, 11):
|
|
|
|
self.mode = "L"
|
|
|
|
if depth == 1:
|
2015-11-25 01:25:01 +00:00
|
|
|
self.mode = "1" # ???
|
2013-10-11 17:28:32 +00:00
|
|
|
elif imagetype in (1, 9):
|
|
|
|
self.mode = "P"
|
|
|
|
elif imagetype in (2, 10):
|
|
|
|
self.mode = "RGB"
|
|
|
|
if depth == 32:
|
|
|
|
self.mode = "RGBA"
|
|
|
|
else:
|
2014-09-30 16:15:32 +00:00
|
|
|
raise SyntaxError("unknown TGA mode")
|
2013-10-11 17:28:32 +00:00
|
|
|
|
|
|
|
# orientation
|
|
|
|
orientation = flags & 0x30
|
|
|
|
if orientation == 0x20:
|
|
|
|
orientation = 1
|
|
|
|
elif not orientation:
|
|
|
|
orientation = -1
|
|
|
|
else:
|
2014-09-30 16:15:32 +00:00
|
|
|
raise SyntaxError("unknown TGA orientation")
|
2013-10-11 17:28:32 +00:00
|
|
|
|
|
|
|
self.info["orientation"] = orientation
|
|
|
|
|
|
|
|
if imagetype & 8:
|
|
|
|
self.info["compression"] = "tga_rle"
|
|
|
|
|
2015-11-25 01:25:01 +00:00
|
|
|
if idlen:
|
|
|
|
self.info["id_section"] = self.fp.read(idlen)
|
|
|
|
|
2013-10-11 17:28:32 +00:00
|
|
|
if colormaptype:
|
|
|
|
# read palette
|
|
|
|
start, size, mapdepth = i16(s[3:]), i16(s[5:]), i16(s[7:])
|
|
|
|
if mapdepth == 16:
|
2015-11-25 01:25:01 +00:00
|
|
|
self.palette = ImagePalette.raw(
|
|
|
|
"BGR;16", b"\0"*2*start + self.fp.read(2*size))
|
2013-10-11 17:28:32 +00:00
|
|
|
elif mapdepth == 24:
|
2015-11-25 01:25:01 +00:00
|
|
|
self.palette = ImagePalette.raw(
|
|
|
|
"BGR", b"\0"*3*start + self.fp.read(3*size))
|
2013-10-11 17:28:32 +00:00
|
|
|
elif mapdepth == 32:
|
2015-11-25 01:25:01 +00:00
|
|
|
self.palette = ImagePalette.raw(
|
|
|
|
"BGRA", b"\0"*4*start + self.fp.read(4*size))
|
2013-10-11 17:28:32 +00:00
|
|
|
|
|
|
|
# setup tile descriptor
|
|
|
|
try:
|
2015-11-25 01:25:01 +00:00
|
|
|
rawmode = MODES[(imagetype & 7, depth)]
|
2013-10-11 17:28:32 +00:00
|
|
|
if imagetype & 8:
|
|
|
|
# compressed
|
|
|
|
self.tile = [("tga_rle", (0, 0)+self.size,
|
|
|
|
self.fp.tell(), (rawmode, orientation, depth))]
|
|
|
|
else:
|
|
|
|
self.tile = [("raw", (0, 0)+self.size,
|
|
|
|
self.fp.tell(), (rawmode, 0, orientation))]
|
|
|
|
except KeyError:
|
2015-11-25 01:25:01 +00:00
|
|
|
pass # cannot decode
|
2013-10-11 17:28:32 +00:00
|
|
|
|
|
|
|
#
|
|
|
|
# --------------------------------------------------------------------
|
|
|
|
# Write TGA file
|
|
|
|
|
2014-09-30 16:15:32 +00:00
|
|
|
o8 = _binary.o8
|
|
|
|
o16 = _binary.o16le
|
|
|
|
o32 = _binary.o32le
|
2013-10-11 17:28:32 +00:00
|
|
|
|
|
|
|
SAVE = {
|
|
|
|
"1": ("1", 1, 0, 3),
|
|
|
|
"L": ("L", 8, 0, 3),
|
|
|
|
"P": ("P", 8, 1, 1),
|
|
|
|
"RGB": ("BGR", 24, 0, 2),
|
|
|
|
"RGBA": ("BGRA", 32, 0, 2),
|
|
|
|
}
|
|
|
|
|
2015-11-25 01:25:01 +00:00
|
|
|
|
2013-10-11 17:28:32 +00:00
|
|
|
def _save(im, fp, filename, check=0):
|
|
|
|
|
|
|
|
try:
|
|
|
|
rawmode, bits, colormaptype, imagetype = SAVE[im.mode]
|
|
|
|
except KeyError:
|
|
|
|
raise IOError("cannot write mode %s as TGA" % im.mode)
|
|
|
|
|
|
|
|
if check:
|
|
|
|
return check
|
|
|
|
|
|
|
|
if colormaptype:
|
|
|
|
colormapfirst, colormaplength, colormapentry = 0, 256, 24
|
|
|
|
else:
|
|
|
|
colormapfirst, colormaplength, colormapentry = 0, 0, 0
|
|
|
|
|
|
|
|
if im.mode == "RGBA":
|
|
|
|
flags = 8
|
|
|
|
else:
|
|
|
|
flags = 0
|
|
|
|
|
|
|
|
orientation = im.info.get("orientation", -1)
|
|
|
|
if orientation > 0:
|
|
|
|
flags = flags | 0x20
|
|
|
|
|
2014-09-30 16:15:32 +00:00
|
|
|
fp.write(b"\000" +
|
|
|
|
o8(colormaptype) +
|
|
|
|
o8(imagetype) +
|
2013-10-11 17:28:32 +00:00
|
|
|
o16(colormapfirst) +
|
|
|
|
o16(colormaplength) +
|
2014-09-30 16:15:32 +00:00
|
|
|
o8(colormapentry) +
|
2013-10-11 17:28:32 +00:00
|
|
|
o16(0) +
|
|
|
|
o16(0) +
|
|
|
|
o16(im.size[0]) +
|
|
|
|
o16(im.size[1]) +
|
2014-09-30 16:15:32 +00:00
|
|
|
o8(bits) +
|
|
|
|
o8(flags))
|
2013-10-11 17:28:32 +00:00
|
|
|
|
|
|
|
if colormaptype:
|
|
|
|
fp.write(im.im.getpalette("RGB", "BGR"))
|
|
|
|
|
2015-11-25 01:25:01 +00:00
|
|
|
ImageFile._save(
|
|
|
|
im, fp, [("raw", (0, 0) + im.size, 0, (rawmode, 0, orientation))])
|
2013-10-11 17:28:32 +00:00
|
|
|
|
|
|
|
#
|
|
|
|
# --------------------------------------------------------------------
|
|
|
|
# Registry
|
|
|
|
|
2015-11-25 01:25:01 +00:00
|
|
|
Image.register_open(TgaImageFile.format, TgaImageFile)
|
|
|
|
Image.register_save(TgaImageFile.format, _save)
|
2013-10-11 17:28:32 +00:00
|
|
|
|
2015-11-25 01:25:01 +00:00
|
|
|
Image.register_extension(TgaImageFile.format, ".tga")
|