1"""
2A Pillow loader for .ftc and .ftu files (FTEX)
3Jerome Leclanche <jerome@leclan.ch>
4
5The contents of this file are hereby released in the public domain (CC0)
6Full text of the CC0 license:
7 https://creativecommons.org/publicdomain/zero/1.0/
8
9Independence War 2: Edge Of Chaos - Texture File Format - 16 October 2001
10
11The textures used for 3D objects in Independence War 2: Edge Of Chaos are in a
12packed custom format called FTEX. This file format uses file extensions FTC
13and FTU.
14* FTC files are compressed textures (using standard texture compression).
15* FTU files are not compressed.
16Texture File Format
17The FTC and FTU texture files both use the same format. This
18has the following structure:
19{header}
20{format_directory}
21{data}
22Where:
23{header} = {
24 u32:magic,
25 u32:version,
26 u32:width,
27 u32:height,
28 u32:mipmap_count,
29 u32:format_count
30}
31
32* The "magic" number is "FTEX".
33* "width" and "height" are the dimensions of the texture.
34* "mipmap_count" is the number of mipmaps in the texture.
35* "format_count" is the number of texture formats (different versions of the
36same texture) in this file.
37
38{format_directory} = format_count * { u32:format, u32:where }
39
40The format value is 0 for DXT1 compressed textures and 1 for 24-bit RGB
41uncompressed textures.
42The texture data for a format starts at the position "where" in the file.
43
44Each set of texture data in the file has the following structure:
45{data} = format_count * { u32:mipmap_size, mipmap_size * { u8 } }
46* "mipmap_size" is the number of bytes in that mip level. For compressed
47textures this is the size of the texture data compressed with DXT1. For 24 bit
48uncompressed textures, this is 3 * width * height. Following this are the image
49bytes for that mipmap level.
50
51Note: All data is stored in little-Endian (Intel) byte order.
52"""
53
54from __future__ import annotations
55
56import struct
57from enum import IntEnum
58from io import BytesIO
59
60from . import Image, ImageFile
61
62MAGIC = b"FTEX"
63
64
65class Format(IntEnum):
66 DXT1 = 0
67 UNCOMPRESSED = 1
68
69
70class FtexImageFile(ImageFile.ImageFile):
71 format = "FTEX"
72 format_description = "Texture File Format (IW2:EOC)"
73
74 def _open(self) -> None:
75 if not _accept(self.fp.read(4)):
76 msg = "not an FTEX file"
77 raise SyntaxError(msg)
78 struct.unpack("<i", self.fp.read(4)) # version
79 self._size = struct.unpack("<2i", self.fp.read(8))
80 mipmap_count, format_count = struct.unpack("<2i", self.fp.read(8))
81
82 # Only support single-format files.
83 # I don't know of any multi-format file.
84 assert format_count == 1
85
86 format, where = struct.unpack("<2i", self.fp.read(8))
87 self.fp.seek(where)
88 (mipmap_size,) = struct.unpack("<i", self.fp.read(4))
89
90 data = self.fp.read(mipmap_size)
91
92 if format == Format.DXT1:
93 self._mode = "RGBA"
94 self.tile = [ImageFile._Tile("bcn", (0, 0) + self.size, 0, (1,))]
95 elif format == Format.UNCOMPRESSED:
96 self._mode = "RGB"
97 self.tile = [ImageFile._Tile("raw", (0, 0) + self.size, 0, "RGB")]
98 else:
99 msg = f"Invalid texture compression format: {repr(format)}"
100 raise ValueError(msg)
101
102 self.fp.close()
103 self.fp = BytesIO(data)
104
105 def load_seek(self, pos: int) -> None:
106 pass
107
108
109def _accept(prefix: bytes) -> bool:
110 return prefix.startswith(MAGIC)
111
112
113Image.register_open(FtexImageFile.format, FtexImageFile, _accept)
114Image.register_extensions(FtexImageFile.format, [".ftc", ".ftu"])