@@ -215,39 +215,44 @@ def image_to_texture2d(
215
215
pil_mode = "RGB"
216
216
# everything else defaulted to RGBA
217
217
218
+ switch_swizzle = None
218
219
if platform == BuildTarget .Switch and platform_blob :
219
220
gobs_per_block = TextureSwizzler .get_switch_gobs_per_block (platform_blob )
220
221
221
- s_tex_format = tex_format
222
222
if tex_format == TextureFormat .RGB24 :
223
- s_tex_format = TextureFormat .RGBA32
223
+ tex_format = TextureFormat .RGBA32
224
224
pil_mode = "RGBA"
225
225
elif tex_format == TextureFormat .BGR24 :
226
- s_tex_format = TextureFormat .BGRA32
226
+ tex_format = TextureFormat .BGRA32
227
227
pil_mode = "BGRA"
228
228
229
- block_size = TextureSwizzler .TEXTUREFORMAT_BLOCK_SIZE_MAP [s_tex_format ]
229
+ block_size = TextureSwizzler .TEXTUREFORMAT_BLOCK_SIZE_MAP .get (tex_format )
230
+ if not block_size :
231
+ raise NotImplementedError (
232
+ f"Not implemented swizzle format: { tex_format .name } "
233
+ )
234
+
230
235
width , height = TextureSwizzler .get_padded_texture_size (
231
236
img .width , img .height , * block_size , gobs_per_block
232
237
)
233
- img = pad_image (img , width , height )
234
- img = Image .frombytes (
235
- pil_mode ,
236
- img .size ,
237
- TextureSwizzler .swizzle (
238
- img .tobytes ("raw" , pil_mode ), width , height , * block_size , gobs_per_block
239
- ),
240
- )
238
+ switch_swizzle = (block_size , gobs_per_block )
239
+ else :
240
+ width , height = get_compressed_image_size (img .width , img .height , tex_format )
241
241
242
+ img = pad_image (img , width , height )
242
243
if compress_func :
243
- width , height = get_compressed_image_size (img .width , img .height , tex_format )
244
- img = pad_image (img , width , height )
245
244
enc_img = compress_func (
246
- img .tobytes ("raw" , "RGBA" ), img .width , img .height , tex_format
245
+ img .tobytes ("raw" , pil_mode ), img .width , img .height , tex_format
247
246
)
248
247
else :
249
248
enc_img = img .tobytes ("raw" , pil_mode )
250
249
250
+ if switch_swizzle is not None :
251
+ block_size , gobs_per_block = switch_swizzle
252
+ enc_img = bytes (
253
+ TextureSwizzler .swizzle (enc_img , width , height , * block_size , gobs_per_block )
254
+ )
255
+
251
256
return enc_img , tex_format
252
257
253
258
@@ -324,7 +329,12 @@ def parse_image_data(
324
329
texture_format = texture_format
325
330
pil_mode = "L"
326
331
327
- block_size = TextureSwizzler .TEXTUREFORMAT_BLOCK_SIZE_MAP [texture_format ]
332
+ block_size = TextureSwizzler .TEXTUREFORMAT_BLOCK_SIZE_MAP .get (texture_format )
333
+ if not block_size :
334
+ raise NotImplementedError (
335
+ f"Not implemented swizzle format: { texture_format .name } "
336
+ )
337
+
328
338
width , height = TextureSwizzler .get_padded_texture_size (
329
339
width , height , * block_size , gobs_per_block
330
340
)
@@ -343,21 +353,20 @@ def parse_image_data(
343
353
else :
344
354
image_data = texture2ddecoder .unpack_crunch (image_data )
345
355
346
- conv_func = CONV_TABLE .get (texture_format )
347
- if not conv_func :
348
- raise NotImplementedError (f"Not implemented texture format: { texture_format } " )
349
-
350
- img = conv_func (image_data , width , height )
351
-
352
356
if switch_swizzle is not None :
353
357
block_size , gobs_per_block , pil_mode = switch_swizzle
354
- swizzle_data = bytes (
358
+ image_data = bytes (
355
359
TextureSwizzler .deswizzle (
356
- img . tobytes ( "raw" , pil_mode ) , width , height , * block_size , gobs_per_block
360
+ image_data , width , height , * block_size , gobs_per_block
357
361
)
358
362
)
359
- img .mode = pil_mode
360
- img = Image .frombytes (img .mode , (width , height ), swizzle_data , "raw" , pil_mode )
363
+
364
+ conv_func = CONV_TABLE .get (texture_format )
365
+ if not conv_func :
366
+ raise NotImplementedError (
367
+ f"Not implemented texture format: { texture_format .name } "
368
+ )
369
+ img = conv_func (image_data , width , height )
361
370
362
371
if original_width != width or original_height != height :
363
372
img = img .crop ((0 , 0 , original_width , original_height ))
0 commit comments