@@ -222,36 +222,39 @@ def image_to_texture2d(
222222 tex_format = TF .RGB24
223223 pil_mode = "RGB"
224224 # everything else defaulted to RGBA
225+ if compress_func :
226+ width , height = get_compressed_image_size (img .width , img .height , tex_format )
227+ img = pad_image (img , width , height )
228+ enc_img = compress_func (
229+ img .tobytes ("raw" , "RGBA" ), img .width , img .height , tex_format
230+ )
231+ else :
232+ enc_img = img .tobytes ("raw" , pil_mode )
225233
226- if platform == BuildTarget .Switch and platform_blob is not None :
234+ if TextureSwizzler .is_switch_swizzled (platform , platform_blob ):
235+ if TYPE_CHECKING :
236+ # due to earlier check platform_blob can't be None
237+ assert platform_blob is not None
227238 gobsPerBlock = TextureSwizzler .get_switch_gobs_per_block (platform_blob )
228239 s_tex_format = tex_format
229240 if tex_format == TextureFormat .RGB24 :
230241 s_tex_format = TextureFormat .RGBA32
231242 pil_mode = "RGBA"
232- # elif tex_format == TextureFormat.BGR24:
233- # s_tex_format = TextureFormat.BGRA32
243+ elif tex_format == TextureFormat .BGR24 :
244+ s_tex_format = TextureFormat .BGRA32
245+ pil_mode = "BGRA"
234246 block_size = TextureSwizzler .TEXTUREFORMAT_BLOCK_SIZE_MAP [s_tex_format ]
235247 width , height = TextureSwizzler .get_padded_texture_size (
236248 img .width , img .height , * block_size , gobsPerBlock
237249 )
238- img = pad_image (img , width , height )
239- img = Image .frombytes (
240- "RGBA" ,
241- img .size ,
242- TextureSwizzler .swizzle (
243- img .tobytes ("raw" , "RGBA" ), width , height , * block_size , gobsPerBlock
244- ),
245- )
250+ if not compress_func :
251+ # recompress with padding and corrected image mode
252+ img = pad_image (img , width , height )
253+ enc_img = img .tobytes ("raw" , pil_mode )
246254
247- if compress_func :
248- width , height = get_compressed_image_size (img .width , img .height , tex_format )
249- img = pad_image (img , width , height )
250- enc_img = compress_func (
251- img .tobytes ("raw" , "RGBA" ), img .width , img .height , tex_format
255+ enc_img = bytes (
256+ TextureSwizzler .swizzle (enc_img , width , height , * block_size , gobsPerBlock )
252257 )
253- else :
254- enc_img = img .tobytes ("raw" , pil_mode )
255258
256259 return enc_img , tex_format
257260
@@ -314,8 +317,11 @@ def parse_image_data(
314317 image_data = swap_bytes_for_xbox (image_data )
315318
316319 original_width , original_height = (width , height )
317- switch_swizzle = None
318- if platform == BuildTarget .Switch and platform_blob is not None :
320+
321+ if TextureSwizzler .is_switch_swizzled (platform , platform_blob ):
322+ if TYPE_CHECKING :
323+ # due to earlier check platform_blob can't be None
324+ assert platform_blob is not None
319325 gobsPerBlock = TextureSwizzler .get_switch_gobs_per_block (platform_blob )
320326 if texture_format == TextureFormat .RGB24 :
321327 texture_format = TextureFormat .RGBA32
@@ -325,7 +331,9 @@ def parse_image_data(
325331 width , height = TextureSwizzler .get_padded_texture_size (
326332 width , height , * block_size , gobsPerBlock
327333 )
328- switch_swizzle = (block_size , gobsPerBlock )
334+ image_data = TextureSwizzler .deswizzle (
335+ image_data , width , height , * block_size , gobsPerBlock
336+ )
329337 else :
330338 width , height = get_compressed_image_size (width , height , texture_format )
331339
@@ -348,12 +356,6 @@ def parse_image_data(
348356
349357 img = selection [0 ](image_data , width , height , * selection [1 :])
350358
351- if switch_swizzle is not None :
352- image_data = TextureSwizzler .deswizzle (
353- img .tobytes ("raw" , "RGBA" ), width , height , * block_size , gobsPerBlock
354- )
355- img = Image .frombytes (img .mode , (width , height ), image_data , "raw" , "RGBA" )
356-
357359 if original_width != width or original_height != height :
358360 img = img .crop ((0 , 0 , original_width , original_height ))
359361
0 commit comments