Coverage for /pythoncovmergedfiles/medio/medio/usr/local/lib/python3.8/site-packages/pillow-10.4.0-py3.8-linux-x86_64.egg/PIL/FtexImagePlugin.py: 45%

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

40 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 

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 self._mode = "RGB" 

83 

84 # Only support single-format files. 

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

86 assert format_count == 1 

87 

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

89 self.fp.seek(where) 

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

91 

92 data = self.fp.read(mipmap_size) 

93 

94 if format == Format.DXT1: 

95 self._mode = "RGBA" 

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

97 elif format == Format.UNCOMPRESSED: 

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

99 else: 

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

101 raise ValueError(msg) 

102 

103 self.fp.close() 

104 self.fp = BytesIO(data) 

105 

106 def load_seek(self, pos: int) -> None: 

107 pass 

108 

109 

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

111 return prefix[:4] == MAGIC 

112 

113 

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

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