@@ -222,36 +222,39 @@ def image_to_texture2d(
222
222
tex_format = TF .RGB24
223
223
pil_mode = "RGB"
224
224
# 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 )
225
233
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
227
238
gobsPerBlock = TextureSwizzler .get_switch_gobs_per_block (platform_blob )
228
239
s_tex_format = tex_format
229
240
if tex_format == TextureFormat .RGB24 :
230
241
s_tex_format = TextureFormat .RGBA32
231
242
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"
234
246
block_size = TextureSwizzler .TEXTUREFORMAT_BLOCK_SIZE_MAP [s_tex_format ]
235
247
width , height = TextureSwizzler .get_padded_texture_size (
236
248
img .width , img .height , * block_size , gobsPerBlock
237
249
)
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 )
246
254
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 )
252
257
)
253
- else :
254
- enc_img = img .tobytes ("raw" , pil_mode )
255
258
256
259
return enc_img , tex_format
257
260
@@ -314,8 +317,11 @@ def parse_image_data(
314
317
image_data = swap_bytes_for_xbox (image_data )
315
318
316
319
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
319
325
gobsPerBlock = TextureSwizzler .get_switch_gobs_per_block (platform_blob )
320
326
if texture_format == TextureFormat .RGB24 :
321
327
texture_format = TextureFormat .RGBA32
@@ -325,7 +331,9 @@ def parse_image_data(
325
331
width , height = TextureSwizzler .get_padded_texture_size (
326
332
width , height , * block_size , gobsPerBlock
327
333
)
328
- switch_swizzle = (block_size , gobsPerBlock )
334
+ image_data = TextureSwizzler .deswizzle (
335
+ image_data , width , height , * block_size , gobsPerBlock
336
+ )
329
337
else :
330
338
width , height = get_compressed_image_size (width , height , texture_format )
331
339
@@ -348,12 +356,6 @@ def parse_image_data(
348
356
349
357
img = selection [0 ](image_data , width , height , * selection [1 :])
350
358
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
-
357
359
if original_width != width or original_height != height :
358
360
img = img .crop ((0 , 0 , original_width , original_height ))
359
361
0 commit comments