Skip to content

Commit 5a6017a

Browse files
committed
chore: Texture2DConverter - unify names
1 parent 0c24f42 commit 5a6017a

File tree

2 files changed

+48
-42
lines changed

2 files changed

+48
-42
lines changed

UnityPy/export/Texture2DConverter.py

Lines changed: 43 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -148,15 +148,15 @@ def image_to_texture2d(
148148

149149
# defaults
150150
compress_func = None
151-
tex_format = TF.RGBA32
151+
texture_format = TF.RGBA32
152152
pil_mode = "RGBA"
153153

154154
# DXT / BC
155155
if target_texture_format in [TF.DXT1, TF.DXT1Crunched]:
156-
tex_format = TF.DXT1
156+
texture_format = TF.DXT1
157157
compress_func = compress_etcpak
158158
elif target_texture_format in [TF.DXT5, TF.DXT5Crunched]:
159-
tex_format = TF.DXT5
159+
texture_format = TF.DXT5
160160
compress_func = compress_etcpak
161161
elif target_texture_format in [TF.BC4, TF.BC5, TF.BC7]:
162162
compress_func = compress_etcpak
@@ -166,28 +166,32 @@ def image_to_texture2d(
166166
block_size = TEXTURE_FORMAT_BLOCK_SIZE_TABLE[target_texture_format]
167167
assert block_size is not None
168168
if img.mode == "RGB":
169-
tex_format = getattr(TF, f"ASTC_RGB_{block_size[0]}x{block_size[1]}")
169+
texture_format = getattr(
170+
TF, f"ASTC_RGB_{block_size[0]}x{block_size[1]}"
171+
)
170172
else:
171-
tex_format = getattr(TF, f"ASTC_RGBA_{block_size[0]}x{block_size[1]}")
173+
texture_format = getattr(
174+
TF, f"ASTC_RGBA_{block_size[0]}x{block_size[1]}"
175+
)
172176
else:
173-
tex_format = target_texture_format
177+
texture_format = target_texture_format
174178
compress_func = compress_astc
175179
# ETC
176180
elif target_texture_format in [TF.ETC_RGB4, TF.ETC_RGB4Crunched, TF.ETC_RGB4_3DS]:
177181
if target_texture_format == TF.ETC_RGB4_3DS:
178-
tex_format = TF.ETC_RGB4_3DS
182+
texture_format = TF.ETC_RGB4_3DS
179183
else:
180-
tex_format = target_texture_format
184+
texture_format = target_texture_format
181185
compress_func = compress_etcpak
182186
elif target_texture_format == TF.ETC2_RGB:
183-
tex_format = TF.ETC2_RGB
187+
texture_format = TF.ETC2_RGB
184188
compress_func = compress_etcpak
185189
elif target_texture_format in [TF.ETC2_RGBA8, TF.ETC2_RGBA8Crunched, TF.ETC2_RGBA1]:
186-
tex_format = TF.ETC2_RGBA8
190+
texture_format = TF.ETC2_RGBA8
187191
compress_func = compress_etcpak
188192
# L
189193
elif target_texture_format == TF.Alpha8:
190-
tex_format = TF.Alpha8
194+
texture_format = TF.Alpha8
191195
pil_mode = "L"
192196
# R - should probably be merged into #L, as pure R is used as Alpha
193197
# but need test data for this first
@@ -199,7 +203,7 @@ def image_to_texture2d(
199203
TF.EAC_R,
200204
TF.EAC_R_SIGNED,
201205
]:
202-
tex_format = TF.R8
206+
texture_format = TF.R8
203207
pil_mode = "R"
204208
# RGBA
205209
elif target_texture_format in [
@@ -211,49 +215,47 @@ def image_to_texture2d(
211215
TF.PVRTC_RGB4,
212216
TF.ATC_RGB4,
213217
]:
214-
tex_format = TF.RGB24
218+
texture_format = TF.RGB24
215219
pil_mode = "RGB"
216220
# everything else defaulted to RGBA
217221

218222
switch_swizzle = None
219223
if platform == BuildTarget.Switch and platform_blob:
220224
gobs_per_block = TextureSwizzler.get_switch_gobs_per_block(platform_blob)
221225

222-
if tex_format == TextureFormat.RGB24:
223-
tex_format = TextureFormat.RGBA32
226+
if texture_format == TF.RGB24:
227+
texture_format = TF.RGBA32
224228
pil_mode = "RGBA"
225-
elif tex_format == TextureFormat.BGR24:
226-
tex_format = TextureFormat.BGRA32
229+
elif texture_format == TF.BGR24:
230+
texture_format = TF.BGRA32
227231
pil_mode = "BGRA"
228232

229-
block_size = TextureSwizzler.TEXTUREFORMAT_BLOCK_SIZE_MAP.get(tex_format)
233+
block_size = TextureSwizzler.TEXTUREFORMAT_BLOCK_SIZE_MAP.get(texture_format)
230234
if not block_size:
231235
raise NotImplementedError(
232-
f"Not implemented swizzle format: {tex_format.name}"
236+
f"Not implemented swizzle format: {texture_format.name}"
233237
)
234238

235239
width, height = TextureSwizzler.get_padded_texture_size(
236240
img.width, img.height, *block_size, gobs_per_block
237241
)
238242
switch_swizzle = (block_size, gobs_per_block)
239243
else:
240-
width, height = get_compressed_image_size(img.width, img.height, tex_format)
244+
width, height = get_compressed_image_size(img.width, img.height, texture_format)
241245

242246
img = pad_image(img, width, height)
243247
if compress_func:
244248
enc_img = compress_func(
245-
img.tobytes("raw", pil_mode), img.width, img.height, tex_format
249+
img.tobytes("raw", pil_mode), img.width, img.height, texture_format
246250
)
247251
else:
248252
enc_img = img.tobytes("raw", pil_mode)
249253

250254
if switch_swizzle is not None:
251255
block_size, gobs_per_block = switch_swizzle
252-
enc_img = bytes(
253-
TextureSwizzler.swizzle(enc_img, width, height, *block_size, gobs_per_block)
254-
)
256+
enc_img = TextureSwizzler.swizzle(enc_img, width, height, *block_size, gobs_per_block)
255257

256-
return enc_img, tex_format
258+
return enc_img, texture_format
257259

258260

259261
def assert_rgba(img: Image.Image, target_texture_format: TextureFormat) -> Image.Image:
@@ -320,12 +322,12 @@ def parse_image_data(
320322
gobs_per_block = TextureSwizzler.get_switch_gobs_per_block(platform_blob)
321323

322324
pil_mode = "RGBA"
323-
if texture_format == TextureFormat.RGB24:
324-
texture_format = TextureFormat.RGBA32
325-
elif texture_format == TextureFormat.BGR24:
326-
texture_format = TextureFormat.BGRA32
325+
if texture_format == TF.RGB24:
326+
texture_format = TF.RGBA32
327+
elif texture_format == TF.BGR24:
328+
texture_format = TF.BGRA32
327329
pil_mode = "BGRA"
328-
elif texture_format == TextureFormat.Alpha8:
330+
elif texture_format == TF.Alpha8:
329331
texture_format = texture_format
330332
pil_mode = "L"
331333

@@ -355,11 +357,8 @@ def parse_image_data(
355357

356358
if switch_swizzle is not None:
357359
block_size, gobs_per_block, pil_mode = switch_swizzle
358-
image_data = bytes(
359-
TextureSwizzler.deswizzle(
360-
image_data, width, height, *block_size, gobs_per_block
361-
)
362-
)
360+
image_data = TextureSwizzler.deswizzle(image_data, width, height, *block_size, gobs_per_block)
361+
363362

364363
conv_func = CONV_TABLE.get(texture_format)
365364
if not conv_func:
@@ -555,11 +554,15 @@ def rgb9e5float(image_data: bytes, width: int, height: int) -> Image.Image:
555554

556555
CONV_TABLE: Dict[TextureFormat, Callable[[bytes, int, int], Image.Image]] = {
557556
TF.Alpha8: lambda data, w, h: pillow(data, w, h, "L", "raw", "L"),
558-
TF.ARGB4444: lambda data, w, h: pillow(data, w, h, "RGBA", "raw", "RGBA;4B", (2, 1, 0, 3)),
557+
TF.ARGB4444: lambda data, w, h: pillow(
558+
data, w, h, "RGBA", "raw", "RGBA;4B", (2, 1, 0, 3)
559+
),
559560
TF.RGB24: lambda data, w, h: pillow(data, w, h, "RGB", "raw", "RGB"),
560561
TF.RGBA32: lambda data, w, h: pillow(data, w, h, "RGBA", "raw", "RGBA"),
561562
TF.ARGB32: lambda data, w, h: pillow(data, w, h, "RGBA", "raw", "ARGB"),
562-
TF.ARGBFloat: lambda data, w, h: pillow(data, w, h, "RGBA", "raw", "RGBAF", (2, 1, 0, 3)),
563+
TF.ARGBFloat: lambda data, w, h: pillow(
564+
data, w, h, "RGBA", "raw", "RGBAF", (2, 1, 0, 3)
565+
),
563566
TF.RGB565: lambda data, w, h: pillow(data, w, h, "RGB", "raw", "BGR;16"),
564567
TF.BGR24: lambda data, w, h: pillow(data, w, h, "RGB", "raw", "BGR"),
565568
TF.R8: lambda data, w, h: pillow(data, w, h, "RGB", "raw", "R"),
@@ -568,7 +571,9 @@ def rgb9e5float(image_data: bytes, width: int, height: int) -> Image.Image:
568571
TF.DXT1: lambda data, w, h: pillow(data, w, h, "RGBA", "bcn", 1),
569572
TF.DXT3: lambda data, w, h: pillow(data, w, h, "RGBA", "bcn", 2),
570573
TF.DXT5: lambda data, w, h: pillow(data, w, h, "RGBA", "bcn", 3),
571-
TF.RGBA4444: lambda data, w, h: pillow(data, w, h, "RGBA", "raw", "RGBA;4B", (3, 2, 1, 0)),
574+
TF.RGBA4444: lambda data, w, h: pillow(
575+
data, w, h, "RGBA", "raw", "RGBA;4B", (3, 2, 1, 0)
576+
),
572577
TF.BGRA32: lambda data, w, h: pillow(data, w, h, "RGBA", "raw", "BGRA"),
573578
TF.RHalf: lambda data, w, h: half(data, w, h, "R", "raw", "R"),
574579
TF.RGHalf: lambda data, w, h: rg(data, w, h, "RGB", "raw", "RGE"),

UnityPy/helpers/TextureSwizzler.py

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ def deswizzle(
2424
block_width: int,
2525
block_height: int,
2626
texels_per_block: int,
27-
) -> bytearray:
27+
) -> bytes:
2828
block_count_x = ceil_divide(width, block_width)
2929
block_count_y = ceil_divide(height, block_height)
3030
gob_count_x = block_count_x // GOB_X_TEXEL_COUNT
@@ -46,7 +46,8 @@ def deswizzle(
4646
:TEXEL_BYTE_SIZE
4747
]
4848
data_view = data_view[TEXEL_BYTE_SIZE:]
49-
return new_data
49+
50+
return bytes(new_data)
5051

5152

5253
def swizzle(
@@ -56,7 +57,7 @@ def swizzle(
5657
block_width: int,
5758
block_height: int,
5859
texels_per_block: int,
59-
) -> bytearray:
60+
) -> bytes:
6061
block_count_x = ceil_divide(width, block_width)
6162
block_count_y = ceil_divide(height, block_height)
6263
gob_count_x = block_count_x // GOB_X_TEXEL_COUNT
@@ -79,7 +80,7 @@ def swizzle(
7980
]
8081
data_view = data_view[TEXEL_BYTE_SIZE:]
8182

82-
return new_data
83+
return bytes(new_data)
8384

8485

8586
# this should be the amount of pixels that can fit 16 bytes

0 commit comments

Comments
 (0)