Coverage for /pythoncovmergedfiles/medio/medio/usr/local/lib/python3.11/site-packages/PIL/FtexImagePlugin.py: 84%

Shortcuts on this page

r m x   toggle line displays

j k   next/prev highlighted chunk

0   (zero) top of page

1   (one) first highlighted chunk

38 statements  

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 

56__lazy_modules__ = {"io", "struct"} 

57 

58import struct 

59from enum import IntEnum 

60 

61from . import Image, ImageFile 

62 

63MAGIC = b"FTEX" 

64 

65 

66class Format(IntEnum): 

67 DXT1 = 0 

68 UNCOMPRESSED = 1 

69 

70 

71class FtexImageFile(ImageFile.ImageFile): 

72 format = "FTEX" 

73 format_description = "Texture File Format (IW2:EOC)" 

74 

75 def _open(self) -> None: 

76 assert self.fp is not None 

77 if not _accept(self.fp.read(4)): 

78 msg = "not an FTEX file" 

79 raise SyntaxError(msg) 

80 self.fp.read(4) # version 

81 self._size = struct.unpack("<2i", self.fp.read(8)) 

82 mipmap_count, format_count = struct.unpack("<2i", self.fp.read(8)) 

83 

84 # Only support single-format files. 

85 # I don't know of any multi-format file. 

86 if format_count != 1: 

87 msg = f"Unsupported number of texture formats: {format_count}" 

88 raise ValueError(msg) 

89 

90 format, where = struct.unpack("<2i", self.fp.read(8)) 

91 

92 offset = where + 4 

93 if format == Format.DXT1: 

94 self._mode = "RGBA" 

95 self.tile = [ImageFile._Tile("bcn", None, offset, (1,))] 

96 elif format == Format.UNCOMPRESSED: 

97 self._mode = "RGB" 

98 self.tile = [ImageFile._Tile("raw", None, offset, "RGB")] 

99 else: 

100 msg = f"Invalid texture compression format: {repr(format)}" 

101 raise ValueError(msg) 

102 

103 

104def _accept(prefix: bytes) -> bool: 

105 return prefix.startswith(MAGIC) 

106 

107 

108Image.register_open(FtexImageFile.format, FtexImageFile, _accept) 

109Image.register_extensions(FtexImageFile.format, [".ftc", ".ftu"])