33import struct
44from io import BytesIO
55from 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
77
88import astc_encoder
99import texture2ddecoder
1717 from ..classes import Texture2D
1818
1919
20+ PlatformBlobType = Union [bytes , Sequence [int ]]
21+
22+
2023TEXTURE_FORMAT_BLOCK_SIZE_TABLE : Dict [TF , Optional [Tuple [int , int ]]] = {}
2124for tf in TF :
2225 if tf .name .startswith ("ASTC" ):
@@ -134,7 +137,7 @@ def image_to_texture2d(
134137 img : Image .Image ,
135138 target_texture_format : Union [TF , int ],
136139 platform : int = 0 ,
137- platform_blob : Optional [bytes ] = None ,
140+ platform_blob : Optional [PlatformBlobType ] = None ,
138141 flip : bool = True ,
139142) -> Tuple [bytes , TF ]:
140143 if not isinstance (target_texture_format , TF ):
@@ -216,27 +219,17 @@ def image_to_texture2d(
216219 pil_mode = "RGB"
217220 # everything else defaulted to RGBA
218221
219- switch_swizzle = None
220222 if platform == BuildTarget .Switch and platform_blob :
221- gobs_per_block = TextureSwizzler .get_switch_gobs_per_block (platform_blob )
222-
223223 if texture_format == TF .RGB24 :
224224 texture_format = TF .RGBA32
225225 pil_mode = "RGBA"
226226 elif texture_format == TF .BGR24 :
227227 texture_format = TF .BGRA32
228228 pil_mode = "BGRA"
229229
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
238232 )
239- switch_swizzle = (block_size , gobs_per_block )
240233 else :
241234 width , height = get_compressed_image_size (img .width , img .height , texture_format )
242235
@@ -248,9 +241,10 @@ def image_to_texture2d(
248241 else :
249242 enc_img = img .tobytes ("raw" , pil_mode )
250243
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+ )
254248
255249 return enc_img , texture_format
256250
@@ -285,10 +279,10 @@ def parse_image_data(
285279 image_data : bytes ,
286280 width : int ,
287281 height : int ,
288- texture_format : Union [int , TF ],
282+ texture_format : Union [TF , int ],
289283 version : Tuple [int , int , int , int ],
290284 platform : int ,
291- platform_blob : Optional [bytes ] = None ,
285+ platform_blob : Optional [PlatformBlobType ] = None ,
292286 flip : bool = True ,
293287) -> Image .Image :
294288 if not width or not height :
@@ -304,31 +298,20 @@ def parse_image_data(
304298 if platform == BuildTarget .XBOX360 and texture_format in XBOX_SWAP_FORMATS :
305299 image_data = swap_bytes_for_xbox (image_data )
306300
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
311302
312- pil_mode = "RGBA"
303+ if platform == BuildTarget . Switch and platform_blob :
313304 if texture_format == TF .RGB24 :
314305 texture_format = TF .RGBA32
315306 elif texture_format == TF .BGR24 :
316307 texture_format = TF .BGRA32
317- pil_mode = "BGRA"
318308 elif texture_format == TF .Alpha8 :
319309 texture_format = texture_format
320- pil_mode = "L"
321310
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
330313 )
331- switch_swizzle = ( block_size , gobs_per_block , pil_mode )
314+ image_data = TextureSwizzler . deswizzle ( image_data , width , height , texture_format , platform_blob )
332315 else :
333316 width , height = get_compressed_image_size (width , height , texture_format )
334317
@@ -343,20 +326,15 @@ def parse_image_data(
343326 else :
344327 image_data = texture2ddecoder .unpack_crunch (image_data )
345328
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-
351329 conv_func = CONV_TABLE .get (texture_format )
352330 if not conv_func :
353331 raise NotImplementedError (
354332 f"Not implemented texture format: { texture_format .name } "
355333 )
356334 img = conv_func (image_data , width , height )
357335
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 ))
360338
361339 return img .transpose (Image .FLIP_TOP_BOTTOM ) if flip else img
362340
0 commit comments