pyffi.formats.tga — Targa (.tga)

Last Built: Mar 06, 2020

pyffi.formats.tga — Targa (.tga)

Implementation

class pyffi.formats.tga.TgaFormat[source]

Bases: pyffi.object_models.xml.FileFormat

This class implements the TGA format.

class ColorMapType(**kwargs)

Bases: pyffi.object_models.xml.enum.EnumBase

An unsigned 8-bit integer, describing the color map type.

class Data[source]

Bases: pyffi.object_models.Data

get_global_child_nodes(edge_filter=(True, True))[source]

Generator which yields all children of this item in the global view, of given edge type (default is edges of type 0).

Override this method.

Returns

Generator for global node children.

inspect(stream)[source]

Quick heuristic check if stream contains Targa data, by looking at the first 18 bytes.

Parameters

stream (file) – The stream to inspect.

read(stream)[source]

Read a tga file.

Parameters

stream (file) – The stream from which to read.

write(stream)[source]

Write a tga file.

Parameters

stream (file) – The stream to write to.

class FooterString(template=None, argument=None, parent=None)[source]

Bases: pyffi.object_models.xml.basic.BasicBase

The Targa footer signature.

get_hash(data=None)[source]

Return a hash value for the signature.

Returns

An immutable object that can be used as a hash.

get_size(data=None)[source]

Return number of bytes that the signature occupies in a file.

Returns

Number of bytes.

get_value()[source]

Get signature.

Returns

The signature.

read(stream, data)[source]

Read signature from stream.

Parameters

stream (file) – The stream to read from.

set_value(value)[source]

Set signature.

Parameters

value (str) – The value to assign.

write(stream, data)[source]

Write signature to stream.

Parameters

stream (file) – The stream to read from.

class Image[source]

Bases: pyffi.utils.graph.GlobalNode

get_detail_child_names(edge_filter=(True, True))[source]

Generator which yields all child names of this item in the detail view.

Override this method if the node has children.

Returns

Generator for detail tree child names.

Return type

generator yielding strs

get_detail_child_nodes(edge_filter=(True, True))[source]

Generator which yields all children of this item in the detail view (by default, all acyclic and active ones).

Override this method if the node has children.

Parameters

edge_filter (EdgeFilter or type(None)) – The edge type to include.

Returns

Generator for detail tree child nodes.

Return type

generator yielding DetailNodes

class ImageType(**kwargs)

Bases: pyffi.object_models.xml.enum.EnumBase

An unsigned 8-bit integer, describing the image type.

PixelData

alias of pyffi.object_models.common.UndecodedData

byte

alias of pyffi.object_models.common.Byte

char

alias of pyffi.object_models.common.Char

float

alias of pyffi.object_models.common.Float

int

alias of pyffi.object_models.common.Int

short

alias of pyffi.object_models.common.Short

ubyte

alias of pyffi.object_models.common.UByte

uint

alias of pyffi.object_models.common.UInt

ushort

alias of pyffi.object_models.common.UShort

Regression tests

Read a TGA file

>>> # check and read tga file
>>> import os
>>> from os.path import dirname
>>> dirpath = __file__
>>> for i in range(4): #recurse up to root repo dir
...     dirpath = dirname(dirpath)
>>> repo_root = dirpath
>>> format_root = os.path.join(repo_root, 'tests', 'formats', 'tga')
>>> file = os.path.join(format_root, 'test.tga').replace("\\", "/")
>>> stream = open(file, 'rb')
>>> data = TgaFormat.Data()
>>> data.inspect(stream)
>>> data.read(stream)
>>> stream.close()
>>> data.header.width
60
>>> data.header.height
20

Parse all TGA files in a directory tree

>>> for stream, data in TgaFormat.walkData(format_root):
...     try:
...         # the replace call makes the doctest also pass on windows
...         os_path = stream.name
...         split = (os_path.split(os.sep))[-4:]
...         rejoin = os.path.join(*split).replace("\\", "/")
...         print("reading %s" % rejoin)
...     except Exception:
...         print(
...             "Warning: read failed due corrupt file,"
...             " corrupt format description, or bug.") 
reading tests/formats/tga/test.tga
reading tests/formats/tga/test_footer.tga

Create a TGA file from scratch and write to file

>>> data = TgaFormat.Data()
>>> from tempfile import TemporaryFile
>>> stream = TemporaryFile()
>>> data.write(stream)
>>> stream.close()