3
3
import struct
4
4
from io import BytesIO
5
5
from threading import Lock
6
- from typing import TYPE_CHECKING , Callable , Dict , Optional , Tuple , Union
6
+ from typing import TYPE_CHECKING , Callable , Dict , Optional , Sequence , Tuple , Union
7
7
8
8
import astc_encoder
9
9
import texture2ddecoder
17
17
from ..classes import Texture2D
18
18
19
19
20
+ PlatformBlobType = Union [bytes , Sequence [int ]]
21
+
22
+
20
23
TEXTURE_FORMAT_BLOCK_SIZE_TABLE : Dict [TF , Optional [Tuple [int , int ]]] = {}
21
24
for tf in TF :
22
25
if tf .name .startswith ("ASTC" ):
@@ -134,7 +137,7 @@ def image_to_texture2d(
134
137
img : Image .Image ,
135
138
target_texture_format : Union [TF , int ],
136
139
platform : int = 0 ,
137
- platform_blob : Optional [bytes ] = None ,
140
+ platform_blob : Optional [PlatformBlobType ] = None ,
138
141
flip : bool = True ,
139
142
) -> Tuple [bytes , TF ]:
140
143
if not isinstance (target_texture_format , TF ):
@@ -216,27 +219,17 @@ def image_to_texture2d(
216
219
pil_mode = "RGB"
217
220
# everything else defaulted to RGBA
218
221
219
- switch_swizzle = None
220
222
if platform == BuildTarget .Switch and platform_blob :
221
- gobs_per_block = TextureSwizzler .get_switch_gobs_per_block (platform_blob )
222
-
223
223
if texture_format == TF .RGB24 :
224
224
texture_format = TF .RGBA32
225
225
pil_mode = "RGBA"
226
226
elif texture_format == TF .BGR24 :
227
227
texture_format = TF .BGRA32
228
228
pil_mode = "BGRA"
229
229
230
- block_size = TextureSwizzler .TEXTUREFORMAT_BLOCK_SIZE_MAP .get (texture_format )
231
- if not block_size :
232
- raise NotImplementedError (
233
- f"Not implemented swizzle format: { texture_format .name } "
234
- )
235
-
236
- width , height = TextureSwizzler .get_padded_texture_size (
237
- img .width , img .height , * block_size , gobs_per_block
230
+ width , height = TextureSwizzler .get_padded_image_size (
231
+ img .width , img .height , texture_format , platform_blob
238
232
)
239
- switch_swizzle = (block_size , gobs_per_block )
240
233
else :
241
234
width , height = get_compressed_image_size (img .width , img .height , texture_format )
242
235
@@ -248,9 +241,10 @@ def image_to_texture2d(
248
241
else :
249
242
enc_img = img .tobytes ("raw" , pil_mode )
250
243
251
- if switch_swizzle is not None :
252
- block_size , gobs_per_block = switch_swizzle
253
- enc_img = TextureSwizzler .swizzle (enc_img , width , height , * block_size , gobs_per_block )
244
+ if platform == BuildTarget .Switch and platform_blob :
245
+ enc_img = TextureSwizzler .swizzle (
246
+ enc_img , width , height , texture_format , platform_blob
247
+ )
254
248
255
249
return enc_img , texture_format
256
250
@@ -285,10 +279,10 @@ def parse_image_data(
285
279
image_data : bytes ,
286
280
width : int ,
287
281
height : int ,
288
- texture_format : Union [int , TF ],
282
+ texture_format : Union [TF , int ],
289
283
version : Tuple [int , int , int , int ],
290
284
platform : int ,
291
- platform_blob : Optional [bytes ] = None ,
285
+ platform_blob : Optional [PlatformBlobType ] = None ,
292
286
flip : bool = True ,
293
287
) -> Image .Image :
294
288
if not width or not height :
@@ -304,31 +298,20 @@ def parse_image_data(
304
298
if platform == BuildTarget .XBOX360 and texture_format in XBOX_SWAP_FORMATS :
305
299
image_data = swap_bytes_for_xbox (image_data )
306
300
307
- original_width , original_height = width , height
308
- switch_swizzle = None
309
- if platform == BuildTarget .Switch and platform_blob :
310
- gobs_per_block = TextureSwizzler .get_switch_gobs_per_block (platform_blob )
301
+ ori_width , ori_height = width , height
311
302
312
- pil_mode = "RGBA"
303
+ if platform == BuildTarget . Switch and platform_blob :
313
304
if texture_format == TF .RGB24 :
314
305
texture_format = TF .RGBA32
315
306
elif texture_format == TF .BGR24 :
316
307
texture_format = TF .BGRA32
317
- pil_mode = "BGRA"
318
308
elif texture_format == TF .Alpha8 :
319
309
texture_format = texture_format
320
- pil_mode = "L"
321
310
322
- block_size = TextureSwizzler .TEXTUREFORMAT_BLOCK_SIZE_MAP .get (texture_format )
323
- if not block_size :
324
- raise NotImplementedError (
325
- f"Not implemented swizzle format: { texture_format .name } "
326
- )
327
-
328
- width , height = TextureSwizzler .get_padded_texture_size (
329
- width , height , * block_size , gobs_per_block
311
+ width , height = TextureSwizzler .get_padded_image_size (
312
+ width , height , texture_format , platform_blob
330
313
)
331
- switch_swizzle = ( block_size , gobs_per_block , pil_mode )
314
+ image_data = TextureSwizzler . deswizzle ( image_data , width , height , texture_format , platform_blob )
332
315
else :
333
316
width , height = get_compressed_image_size (width , height , texture_format )
334
317
@@ -343,20 +326,15 @@ def parse_image_data(
343
326
else :
344
327
image_data = texture2ddecoder .unpack_crunch (image_data )
345
328
346
- if switch_swizzle is not None :
347
- block_size , gobs_per_block , pil_mode = switch_swizzle
348
- image_data = TextureSwizzler .deswizzle (image_data , width , height , * block_size , gobs_per_block )
349
-
350
-
351
329
conv_func = CONV_TABLE .get (texture_format )
352
330
if not conv_func :
353
331
raise NotImplementedError (
354
332
f"Not implemented texture format: { texture_format .name } "
355
333
)
356
334
img = conv_func (image_data , width , height )
357
335
358
- if original_width != width or original_height != height :
359
- img = img .crop ((0 , 0 , original_width , original_height ))
336
+ if ori_width != width or ori_height != height :
337
+ img = img .crop ((0 , 0 , ori_width , ori_height ))
360
338
361
339
return img .transpose (Image .FLIP_TOP_BOTTOM ) if flip else img
362
340
0 commit comments