11
11
import astc_decomp
12
12
import liblzfse
13
13
14
- def load_ktx (data ):
15
- header = data [:64 ]
16
- ktx_data = data [64 :]
17
-
18
- if header [12 :16 ] == bytes .fromhex ('01020304' ):
19
- endianness = '<'
20
-
21
- else :
22
- endianness = '>'
23
-
24
- if header [0 :7 ] != b'\xab KTX 11' :
25
- raise TypeError ('Unsupported or unknown KTX version: {}' .format (header [0 :7 ]))
26
-
27
- glInternalFormat , = struct .unpack (endianness + 'I' , header [28 :32 ])
28
- pixelWidth , pixelHeight = struct .unpack (endianness + '2I' , header [36 :44 ])
29
- bytesOfKeyValueData , = struct .unpack (endianness + 'I' , header [60 :64 ])
30
-
31
- if glInternalFormat not in (0x93B0 , 0x93B4 , 0x93B7 ):
32
- raise TypeError ('Unsupported texture format: {}' .format (hex (glInternalFormat )))
33
-
34
- if glInternalFormat == 0x93B0 :
35
- block_width , block_height = 4 , 4
36
-
37
- elif glInternalFormat == 0x93B4 :
38
- block_width , block_height = 6 , 6
39
-
40
- else :
41
- block_width , block_height = 8 , 8
42
-
43
- key_value_data = ktx_data [:bytesOfKeyValueData ]
44
- ktx_data = ktx_data [bytesOfKeyValueData :]
45
-
46
- if b'Compression_APPLE' in key_value_data :
47
- if ktx_data [12 :15 ] == b'bvx' :
48
- image_data = liblzfse .decompress (ktx_data [12 :])
49
-
50
- else :
51
- raise ValueError ('Unsupported compression type: {}' .format (
52
- ktx_data [12 :15 ])
53
- )
54
-
55
- else :
56
- image_data = ktx_data [4 :]
57
-
58
- return Image .frombytes ('RGBA' , (pixelWidth , pixelHeight ), image_data , 'astc' , (block_width , block_height , False ))
59
-
14
+ from System .Ktx import load_ktx
60
15
61
16
def convert_pixel (pixel , type ):
62
17
if type in (0 , 1 ):
@@ -141,7 +96,7 @@ def decompress_data(data, baseName="Unknown"):
141
96
return decompressed
142
97
143
98
144
- def process_sc (baseName , data , path , decompress ):
99
+ def process_sc (texturePath , baseName , data , path , decompress ):
145
100
if decompress :
146
101
decompressed = decompress_data (data , baseName )
147
102
@@ -160,12 +115,21 @@ def process_sc(baseName, data, path, decompress):
160
115
i += 4 # Ignore this uint32, it's basically the fileSize + the size of subType + width + height (9 bytes)
161
116
162
117
fileSize , = struct .unpack ('<I' , decompressed [i + 1 :i + 5 ])
163
- subType , = struct .unpack ('<b' , bytes ([decompressed [i + 5 ]]))
164
- width , = struct .unpack ('<H' , decompressed [i + 6 :i + 8 ])
165
- height , = struct .unpack ('<H' , decompressed [i + 8 :i + 10 ])
166
- i += 10
118
+ i += 5
119
+
120
+ if fileType == 0x2F :
121
+ zktx_path = decompressed [i + 1 : i + 1 + decompressed [i ]].decode ('utf-8' )
122
+ i += decompressed [i ] + 1
167
123
168
- if fileType != 0x2D :
124
+ subType , = struct .unpack ('<b' , bytes ([decompressed [i ]]))
125
+ width , = struct .unpack ('<H' , decompressed [i + 1 :i + 3 ])
126
+ height , = struct .unpack ('<H' , decompressed [i + 3 :i + 5 ])
127
+ i += 5
128
+
129
+ print ('fileType: {}, fileSize: {}, subType: {}, width: {}, '
130
+ 'height: {}' .format (fileType , fileSize , subType , width , height ))
131
+
132
+ if fileType != 0x2D and fileType != 0x2F :
169
133
if subType in (0 , 1 ):
170
134
pixelSize = 4
171
135
elif subType in (2 , 3 , 4 , 6 ):
@@ -219,6 +183,16 @@ def process_sc(baseName, data, path, decompress):
219
183
imgl [h + (width - (width % 32 )), j + (height - (height % 32 ))] = pixels [iSrcPix ]
220
184
iSrcPix += 1
221
185
186
+ elif fileType == 0x2F :
187
+ zktx_path = os .path .join (texturePath , zktx_path )
188
+
189
+ if os .path .isfile (zktx_path ):
190
+ with open (zktx_path , 'rb' ) as f :
191
+ img = load_ktx (zstandard .decompress (f .read ()))
192
+
193
+ else :
194
+ raise Exception ('External KTX texture {} cannot be found !' .format (zktx_path ))
195
+
222
196
else :
223
197
img = load_ktx (decompressed [i :i + fileSize ])
224
198
i += fileSize
0 commit comments