TGA (AKA Truevision TGA, AKA TARGA) raster image file format: Python parsing library

TGA (AKA Truevision TGA, AKA TARGA), is a raster image file format created by Truevision. It supports up to 32 bits per pixel (three 8-bit RGB channels + 8-bit alpha channel), color mapping and optional lossless RLE compression.

File extension

["tga", "icb", "vda", "vst"]

KS implementation details

License: CC0-1.0

This page hosts a formal specification of TGA (AKA Truevision TGA, AKA TARGA) raster image file format using Kaitai Struct. This specification can be automatically translated into a variety of programming languages to get a parsing library.

Usage

Parse a local file and get structure in memory:

data = Tga.from_file("path/to/local/file.["tga", "icb", "vda", "vst"]")

Or parse structure from a bytes:

from kaitaistruct import KaitaiStream, BytesIO

raw = b"\x00\x01\x02..."
data = Tga(KaitaiStream(BytesIO(raw)))

After that, one can get various attributes from the structure by invoking getter methods like:

data.numColorMap # => Number of entries in a color map

Python source code to parse TGA (AKA Truevision TGA, AKA TARGA) raster image file format

tga.py

# This is a generated file! Please edit source .ksy file and use kaitai-struct-compiler to rebuild

from pkg_resources import parse_version
from kaitaistruct import __version__ as ks_version, KaitaiStruct, KaitaiStream, BytesIO
from enum import Enum
import struct


if parse_version(ks_version) < parse_version('0.7'):
    raise Exception("Incompatible Kaitai Struct Python API: 0.7 or later is required, but you have %s" % (ks_version))

class Tga(KaitaiStruct):
    """TGA (AKA Truevision TGA, AKA TARGA), is a raster image file format created by Truevision. It supports up to 32 bits per pixel (three 8-bit RGB channels + 8-bit alpha channel), color mapping and optional lossless RLE compression.
    
    .. seealso::
       Source - http://www.dca.fee.unicamp.br/~martino/disciplinas/ea978/tgaffs.pdf
    """

    class ColorMapEnum(Enum):
        no_color_map = 0
        has_color_map = 1

    class ImageTypeEnum(Enum):
        no_image_data = 0
        uncomp_color_mapped = 1
        uncomp_true_color = 2
        uncomp_bw = 3
        rle_color_mapped = 9
        rle_true_color = 10
        rle_bw = 11
    def __init__(self, _io, _parent=None, _root=None):
        self._io = _io
        self._parent = _parent
        self._root = _root if _root else self
        self._read()

    def _read(self):
        self.image_id_len = self._io.read_u1()
        self.color_map_type = self._root.ColorMapEnum(self._io.read_u1())
        self.image_type = self._root.ImageTypeEnum(self._io.read_u1())
        self.color_map_ofs = self._io.read_u2le()
        self.num_color_map = self._io.read_u2le()
        self.color_map_depth = self._io.read_u1()
        self.x_offset = self._io.read_u2le()
        self.y_offset = self._io.read_u2le()
        self.width = self._io.read_u2le()
        self.height = self._io.read_u2le()
        self.image_depth = self._io.read_u1()
        self.img_descriptor = self._io.read_u1()
        self.image_id = self._io.read_bytes(self.image_id_len)
        if self.color_map_type == self._root.ColorMapEnum.has_color_map:
            self.color_map = [None] * (self.num_color_map)
            for i in range(self.num_color_map):
                self.color_map[i] = self._io.read_bytes((self.color_map_depth + 7) // 8)



    class TgaFooter(KaitaiStruct):
        def __init__(self, _io, _parent=None, _root=None):
            self._io = _io
            self._parent = _parent
            self._root = _root if _root else self
            self._read()

        def _read(self):
            self.ext_area_ofs = self._io.read_u4le()
            self.dev_dir_ofs = self._io.read_u4le()
            self.version_magic = self._io.read_bytes(18)

        @property
        def is_valid(self):
            if hasattr(self, '_m_is_valid'):
                return self._m_is_valid if hasattr(self, '_m_is_valid') else None

            self._m_is_valid = self.version_magic == struct.pack('18b', 84, 82, 85, 69, 86, 73, 83, 73, 79, 78, 45, 88, 70, 73, 76, 69, 46, 0)
            return self._m_is_valid if hasattr(self, '_m_is_valid') else None

        @property
        def ext_area(self):
            if hasattr(self, '_m_ext_area'):
                return self._m_ext_area if hasattr(self, '_m_ext_area') else None

            if self.is_valid:
                _pos = self._io.pos()
                self._io.seek(self.ext_area_ofs)
                self._m_ext_area = self._root.TgaExtArea(self._io, self, self._root)
                self._io.seek(_pos)

            return self._m_ext_area if hasattr(self, '_m_ext_area') else None


    class TgaExtArea(KaitaiStruct):
        def __init__(self, _io, _parent=None, _root=None):
            self._io = _io
            self._parent = _parent
            self._root = _root if _root else self
            self._read()

        def _read(self):
            self.ext_area_size = self._io.read_u2le()
            self.author_name = (self._io.read_bytes(41)).decode(u"ASCII")
            self.comments = [None] * (4)
            for i in range(4):
                self.comments[i] = (self._io.read_bytes(81)).decode(u"ASCII")

            self.timestamp = self._io.read_bytes(12)
            self.job_id = (self._io.read_bytes(41)).decode(u"ASCII")
            self.job_time = (self._io.read_bytes(6)).decode(u"ASCII")
            self.software_id = (self._io.read_bytes(41)).decode(u"ASCII")
            self.software_version = self._io.read_bytes(3)
            self.key_color = self._io.read_u4le()
            self.pixel_aspect_ratio = self._io.read_u4le()
            self.gamma_value = self._io.read_u4le()
            self.color_corr_ofs = self._io.read_u4le()
            self.postage_stamp_ofs = self._io.read_u4le()
            self.scan_line_ofs = self._io.read_u4le()
            self.attributes = self._io.read_u1()


    @property
    def footer(self):
        if hasattr(self, '_m_footer'):
            return self._m_footer if hasattr(self, '_m_footer') else None

        _pos = self._io.pos()
        self._io.seek((self._io.size() - 26))
        self._m_footer = self._root.TgaFooter(self._io, self, self._root)
        self._io.seek(_pos)
        return self._m_footer if hasattr(self, '_m_footer') else None