Coverage for /var/srv/projects/api.amasfac.comuna18.com/tmp/venv/lib/python3.9/site-packages/PIL/FtexImagePlugin.py: 35%

46 statements  

« prev     ^ index     » next       coverage.py v6.4.4, created at 2023-07-17 14:22 -0600

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 

54import struct 

55from enum import IntEnum 

56from io import BytesIO 

57 

58from . import Image, ImageFile 

59from ._deprecate import deprecate 

60 

61MAGIC = b"FTEX" 

62 

63 

64class Format(IntEnum): 

65 DXT1 = 0 

66 UNCOMPRESSED = 1 

67 

68 

69def __getattr__(name): 

70 for enum, prefix in {Format: "FORMAT_"}.items(): 

71 if name.startswith(prefix): 

72 name = name[len(prefix) :] 

73 if name in enum.__members__: 

74 deprecate(f"{prefix}{name}", 10, f"{enum.__name__}.{name}") 

75 return enum[name] 

76 raise AttributeError(f"module '{__name__}' has no attribute '{name}'") 

77 

78 

79class FtexImageFile(ImageFile.ImageFile): 

80 format = "FTEX" 

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

82 

83 def _open(self): 

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

85 raise SyntaxError("not an FTEX file") 

86 struct.unpack("<i", self.fp.read(4)) # version 

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

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

89 

90 self.mode = "RGB" 

91 

92 # Only support single-format files. 

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

94 assert format_count == 1 

95 

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

97 self.fp.seek(where) 

98 (mipmap_size,) = struct.unpack("<i", self.fp.read(4)) 

99 

100 data = self.fp.read(mipmap_size) 

101 

102 if format == Format.DXT1: 

103 self.mode = "RGBA" 

104 self.tile = [("bcn", (0, 0) + self.size, 0, 1)] 

105 elif format == Format.UNCOMPRESSED: 

106 self.tile = [("raw", (0, 0) + self.size, 0, ("RGB", 0, 1))] 

107 else: 

108 raise ValueError(f"Invalid texture compression format: {repr(format)}") 

109 

110 self.fp.close() 

111 self.fp = BytesIO(data) 

112 

113 def load_seek(self, pos): 

114 pass 

115 

116 

117def _accept(prefix): 

118 return prefix[:4] == MAGIC 

119 

120 

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

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