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,18 @@ def image_to_texture2d(
216219 pil_mode = "RGB"
217220 # everything else defaulted to RGBA
218221
219- switch_swizzle = None
220- if platform == BuildTarget .Switch and platform_blob :
221- gobs_per_block = TextureSwizzler .get_switch_gobs_per_block (platform_blob )
222-
222+ if TextureSwizzler .is_switch_swizzled (platform , platform_blob ):
223+ assert platform_blob is not None
223224 if texture_format == TF .RGB24 :
224225 texture_format = TF .RGBA32
225226 pil_mode = "RGBA"
226227 elif texture_format == TF .BGR24 :
227228 texture_format = TF .BGRA32
228229 pil_mode = "BGRA"
229230
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
231+ width , height = TextureSwizzler .get_padded_image_size (
232+ img .width , img .height , texture_format , platform_blob
238233 )
239- switch_swizzle = (block_size , gobs_per_block )
240234 else :
241235 width , height = get_compressed_image_size (img .width , img .height , texture_format )
242236
@@ -248,9 +242,11 @@ def image_to_texture2d(
248242 else :
249243 enc_img = img .tobytes ("raw" , pil_mode )
250244
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 )
245+ if TextureSwizzler .is_switch_swizzled (platform , platform_blob ):
246+ assert platform_blob is not None
247+ enc_img = TextureSwizzler .swizzle (
248+ enc_img , width , height , texture_format , platform_blob
249+ )
254250
255251 return enc_img , texture_format
256252
@@ -285,10 +281,10 @@ def parse_image_data(
285281 image_data : bytes ,
286282 width : int ,
287283 height : int ,
288- texture_format : Union [int , TF ],
284+ texture_format : Union [TF , int ],
289285 version : Tuple [int , int , int , int ],
290286 platform : int ,
291- platform_blob : Optional [bytes ] = None ,
287+ platform_blob : Optional [PlatformBlobType ] = None ,
292288 flip : bool = True ,
293289) -> Image .Image :
294290 if not width or not height :
@@ -304,30 +300,19 @@ def parse_image_data(
304300 if platform == BuildTarget .XBOX360 and texture_format in XBOX_SWAP_FORMATS :
305301 image_data = swap_bytes_for_xbox (image_data )
306302
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 )
303+ ori_width , ori_height = width , height
311304
312- pil_mode = "RGBA"
305+ if TextureSwizzler .is_switch_swizzled (platform , platform_blob ):
306+ assert platform_blob is not None
313307 if texture_format == TF .RGB24 :
314308 texture_format = TF .RGBA32
315309 elif texture_format == TF .BGR24 :
316310 texture_format = TF .BGRA32
317- pil_mode = "BGRA"
318- elif texture_format == TF .Alpha8 :
319- pil_mode = "L"
320-
321- block_size = TextureSwizzler .TEXTUREFORMAT_BLOCK_SIZE_MAP .get (texture_format )
322- if not block_size :
323- raise NotImplementedError (
324- f"Not implemented swizzle format: { texture_format .name } "
325- )
326311
327- width , height = TextureSwizzler .get_padded_texture_size (
328- width , height , * block_size , gobs_per_block
312+ width , height = TextureSwizzler .get_padded_image_size (
313+ width , height , texture_format , platform_blob
329314 )
330- switch_swizzle = ( block_size , gobs_per_block , pil_mode )
315+ image_data = TextureSwizzler . deswizzle ( image_data , width , height , texture_format , platform_blob )
331316 else :
332317 width , height = get_compressed_image_size (width , height , texture_format )
333318
@@ -342,20 +327,15 @@ def parse_image_data(
342327 else :
343328 image_data = texture2ddecoder .unpack_crunch (image_data )
344329
345- if switch_swizzle is not None :
346- block_size , gobs_per_block , pil_mode = switch_swizzle
347- image_data = TextureSwizzler .deswizzle (image_data , width , height , * block_size , gobs_per_block )
348-
349-
350330 conv_func = CONV_TABLE .get (texture_format )
351331 if not conv_func :
352332 raise NotImplementedError (
353333 f"Not implemented texture format: { texture_format .name } "
354334 )
355335 img = conv_func (image_data , width , height )
356336
357- if original_width != width or original_height != height :
358- img = img .crop ((0 , 0 , original_width , original_height ))
337+ if ori_width != width or ori_height != height :
338+ img = img .crop ((0 , 0 , ori_width , ori_height ))
359339
360340 return img .transpose (Image .FLIP_TOP_BOTTOM ) if flip else img
361341
0 commit comments