Skip to content

Commit ae0d37f

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

File tree

2 files changed

+92
-87
lines changed

2 files changed

+92
-87
lines changed

UnityPy/export/Texture2DConverter.py

Lines changed: 56 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -9,15 +9,14 @@
99
import texture2ddecoder
1010
from PIL import Image
1111

12-
from ..enums import BuildTarget, TextureFormat
12+
from ..enums import BuildTarget
13+
from ..enums import TextureFormat as TF
1314
from ..helpers import TextureSwizzler
1415

1516
if TYPE_CHECKING:
1617
from ..classes import Texture2D
1718

1819

19-
TF = TextureFormat
20-
2120
TEXTURE_FORMAT_BLOCK_SIZE_TABLE: Dict[TF, Optional[Tuple[int, int]]] = {}
2221
for tf in TF:
2322
if tf.name.startswith("ASTC"):
@@ -32,7 +31,7 @@
3231
TEXTURE_FORMAT_BLOCK_SIZE_TABLE[tf] = block_size
3332

3433

35-
def get_compressed_image_size(width: int, height: int, texture_format: TextureFormat):
34+
def get_compressed_image_size(width: int, height: int, texture_format: TF):
3635
block_size = TEXTURE_FORMAT_BLOCK_SIZE_TABLE[texture_format]
3736
if block_size is None:
3837
return (width, height)
@@ -88,7 +87,7 @@ def pad_image(img: Image.Image, pad_width: int, pad_height: int) -> Image.Image:
8887

8988

9089
def compress_etcpak(
91-
data: bytes, width: int, height: int, target_texture_format: TextureFormat
90+
data: bytes, width: int, height: int, target_texture_format: TF
9291
) -> bytes:
9392
import etcpak
9493

@@ -115,7 +114,7 @@ def compress_etcpak(
115114

116115

117116
def compress_astc(
118-
data: bytes, width: int, height: int, target_texture_format: TextureFormat
117+
data: bytes, width: int, height: int, target_texture_format: TF
119118
) -> bytes:
120119
astc_image = astc_encoder.ASTCImage(
121120
astc_encoder.ASTCType.U8, width, height, 1, data
@@ -139,24 +138,24 @@ def image_to_texture2d(
139138
platform: int = 0,
140139
platform_blob: Optional[bytes] = None,
141140
flip: bool = True,
142-
) -> Tuple[bytes, TextureFormat]:
143-
if not isinstance(target_texture_format, TextureFormat):
144-
target_texture_format = TextureFormat(target_texture_format)
141+
) -> Tuple[bytes, TF]:
142+
if not isinstance(target_texture_format, TF):
143+
target_texture_format = TF(target_texture_format)
145144

146145
if flip:
147146
img = img.transpose(Image.FLIP_TOP_BOTTOM)
148147

149148
# defaults
150149
compress_func = None
151-
tex_format = TF.RGBA32
150+
texture_format = TF.RGBA32
152151
pil_mode = "RGBA"
153152

154153
# DXT / BC
155154
if target_texture_format in [TF.DXT1, TF.DXT1Crunched]:
156-
tex_format = TF.DXT1
155+
texture_format = TF.DXT1
157156
compress_func = compress_etcpak
158157
elif target_texture_format in [TF.DXT5, TF.DXT5Crunched]:
159-
tex_format = TF.DXT5
158+
texture_format = TF.DXT5
160159
compress_func = compress_etcpak
161160
elif target_texture_format in [TF.BC4, TF.BC5, TF.BC7]:
162161
compress_func = compress_etcpak
@@ -166,28 +165,32 @@ def image_to_texture2d(
166165
block_size = TEXTURE_FORMAT_BLOCK_SIZE_TABLE[target_texture_format]
167166
assert block_size is not None
168167
if img.mode == "RGB":
169-
tex_format = getattr(TF, f"ASTC_RGB_{block_size[0]}x{block_size[1]}")
168+
texture_format = getattr(
169+
TF, f"ASTC_RGB_{block_size[0]}x{block_size[1]}"
170+
)
170171
else:
171-
tex_format = getattr(TF, f"ASTC_RGBA_{block_size[0]}x{block_size[1]}")
172+
texture_format = getattr(
173+
TF, f"ASTC_RGBA_{block_size[0]}x{block_size[1]}"
174+
)
172175
else:
173-
tex_format = target_texture_format
176+
texture_format = target_texture_format
174177
compress_func = compress_astc
175178
# ETC
176179
elif target_texture_format in [TF.ETC_RGB4, TF.ETC_RGB4Crunched, TF.ETC_RGB4_3DS]:
177180
if target_texture_format == TF.ETC_RGB4_3DS:
178-
tex_format = TF.ETC_RGB4_3DS
181+
texture_format = TF.ETC_RGB4_3DS
179182
else:
180-
tex_format = target_texture_format
183+
texture_format = target_texture_format
181184
compress_func = compress_etcpak
182185
elif target_texture_format == TF.ETC2_RGB:
183-
tex_format = TF.ETC2_RGB
186+
texture_format = TF.ETC2_RGB
184187
compress_func = compress_etcpak
185188
elif target_texture_format in [TF.ETC2_RGBA8, TF.ETC2_RGBA8Crunched, TF.ETC2_RGBA1]:
186-
tex_format = TF.ETC2_RGBA8
189+
texture_format = TF.ETC2_RGBA8
187190
compress_func = compress_etcpak
188191
# L
189192
elif target_texture_format == TF.Alpha8:
190-
tex_format = TF.Alpha8
193+
texture_format = TF.Alpha8
191194
pil_mode = "L"
192195
# R - should probably be merged into #L, as pure R is used as Alpha
193196
# but need test data for this first
@@ -199,7 +202,7 @@ def image_to_texture2d(
199202
TF.EAC_R,
200203
TF.EAC_R_SIGNED,
201204
]:
202-
tex_format = TF.R8
205+
texture_format = TF.R8
203206
pil_mode = "R"
204207
# RGBA
205208
elif target_texture_format in [
@@ -211,52 +214,50 @@ def image_to_texture2d(
211214
TF.PVRTC_RGB4,
212215
TF.ATC_RGB4,
213216
]:
214-
tex_format = TF.RGB24
217+
texture_format = TF.RGB24
215218
pil_mode = "RGB"
216219
# everything else defaulted to RGBA
217220

218221
switch_swizzle = None
219222
if platform == BuildTarget.Switch and platform_blob:
220223
gobs_per_block = TextureSwizzler.get_switch_gobs_per_block(platform_blob)
221224

222-
if tex_format == TextureFormat.RGB24:
223-
tex_format = TextureFormat.RGBA32
225+
if texture_format == TF.RGB24:
226+
texture_format = TF.RGBA32
224227
pil_mode = "RGBA"
225-
elif tex_format == TextureFormat.BGR24:
226-
tex_format = TextureFormat.BGRA32
228+
elif texture_format == TF.BGR24:
229+
texture_format = TF.BGRA32
227230
pil_mode = "BGRA"
228231

229-
block_size = TextureSwizzler.TEXTUREFORMAT_BLOCK_SIZE_MAP.get(tex_format)
232+
block_size = TextureSwizzler.TEXTUREFORMAT_BLOCK_SIZE_MAP.get(texture_format)
230233
if not block_size:
231234
raise NotImplementedError(
232-
f"Not implemented swizzle format: {tex_format.name}"
235+
f"Not implemented swizzle format: {texture_format.name}"
233236
)
234237

235238
width, height = TextureSwizzler.get_padded_texture_size(
236239
img.width, img.height, *block_size, gobs_per_block
237240
)
238241
switch_swizzle = (block_size, gobs_per_block)
239242
else:
240-
width, height = get_compressed_image_size(img.width, img.height, tex_format)
243+
width, height = get_compressed_image_size(img.width, img.height, texture_format)
241244

242245
img = pad_image(img, width, height)
243246
if compress_func:
244247
enc_img = compress_func(
245-
img.tobytes("raw", pil_mode), img.width, img.height, tex_format
248+
img.tobytes("raw", pil_mode), img.width, img.height, texture_format
246249
)
247250
else:
248251
enc_img = img.tobytes("raw", pil_mode)
249252

250253
if switch_swizzle is not None:
251254
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+
enc_img = TextureSwizzler.swizzle(enc_img, width, height, *block_size, gobs_per_block)
255256

256-
return enc_img, tex_format
257+
return enc_img, texture_format
257258

258259

259-
def assert_rgba(img: Image.Image, target_texture_format: TextureFormat) -> Image.Image:
260+
def assert_rgba(img: Image.Image, target_texture_format: TF) -> Image.Image:
260261
if img.mode == "RGB":
261262
img = img.convert("RGBA")
262263
assert (
@@ -295,7 +296,7 @@ def parse_image_data(
295296
image_data: bytes,
296297
width: int,
297298
height: int,
298-
texture_format: Union[int, TextureFormat],
299+
texture_format: Union[int, TF],
299300
version: Tuple[int, int, int, int],
300301
platform: int,
301302
platform_blob: Optional[bytes] = None,
@@ -308,8 +309,8 @@ def parse_image_data(
308309
if not image_data:
309310
raise ValueError("Texture2D has no image data")
310311

311-
if not isinstance(texture_format, TextureFormat):
312-
texture_format = TextureFormat(texture_format)
312+
if not isinstance(texture_format, TF):
313+
texture_format = TF(texture_format)
313314

314315
if platform == BuildTarget.XBOX360 and texture_format in XBOX_SWAP_FORMATS:
315316
image_data = swap_bytes_for_xbox(image_data)
@@ -320,12 +321,12 @@ def parse_image_data(
320321
gobs_per_block = TextureSwizzler.get_switch_gobs_per_block(platform_blob)
321322

322323
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
324+
if texture_format == TF.RGB24:
325+
texture_format = TF.RGBA32
326+
elif texture_format == TF.BGR24:
327+
texture_format = TF.BGRA32
327328
pil_mode = "BGRA"
328-
elif texture_format == TextureFormat.Alpha8:
329+
elif texture_format == TF.Alpha8:
329330
texture_format = texture_format
330331
pil_mode = "L"
331332

@@ -355,11 +356,8 @@ def parse_image_data(
355356

356357
if switch_swizzle is not None:
357358
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-
)
359+
image_data = TextureSwizzler.deswizzle(image_data, width, height, *block_size, gobs_per_block)
360+
363361

364362
conv_func = CONV_TABLE.get(texture_format)
365363
if not conv_func:
@@ -553,13 +551,17 @@ def rgb9e5float(image_data: bytes, width: int, height: int) -> Image.Image:
553551
return Image.frombytes("RGB", (width, height), rgb, "raw", "RGB")
554552

555553

556-
CONV_TABLE: Dict[TextureFormat, Callable[[bytes, int, int], Image.Image]] = {
554+
CONV_TABLE: Dict[TF, Callable[[bytes, int, int], Image.Image]] = {
557555
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)),
556+
TF.ARGB4444: lambda data, w, h: pillow(
557+
data, w, h, "RGBA", "raw", "RGBA;4B", (2, 1, 0, 3)
558+
),
559559
TF.RGB24: lambda data, w, h: pillow(data, w, h, "RGB", "raw", "RGB"),
560560
TF.RGBA32: lambda data, w, h: pillow(data, w, h, "RGBA", "raw", "RGBA"),
561561
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)),
562+
TF.ARGBFloat: lambda data, w, h: pillow(
563+
data, w, h, "RGBA", "raw", "RGBAF", (2, 1, 0, 3)
564+
),
563565
TF.RGB565: lambda data, w, h: pillow(data, w, h, "RGB", "raw", "BGR;16"),
564566
TF.BGR24: lambda data, w, h: pillow(data, w, h, "RGB", "raw", "BGR"),
565567
TF.R8: lambda data, w, h: pillow(data, w, h, "RGB", "raw", "R"),
@@ -568,7 +570,9 @@ def rgb9e5float(image_data: bytes, width: int, height: int) -> Image.Image:
568570
TF.DXT1: lambda data, w, h: pillow(data, w, h, "RGBA", "bcn", 1),
569571
TF.DXT3: lambda data, w, h: pillow(data, w, h, "RGBA", "bcn", 2),
570572
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)),
573+
TF.RGBA4444: lambda data, w, h: pillow(
574+
data, w, h, "RGBA", "raw", "RGBA;4B", (3, 2, 1, 0)
575+
),
572576
TF.BGRA32: lambda data, w, h: pillow(data, w, h, "RGBA", "raw", "BGRA"),
573577
TF.RHalf: lambda data, w, h: half(data, w, h, "R", "raw", "R"),
574578
TF.RGHalf: lambda data, w, h: rg(data, w, h, "RGB", "raw", "RGE"),

UnityPy/helpers/TextureSwizzler.py

Lines changed: 36 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
# based on https://github.com/nesrak1/UABEA/blob/master/TexturePlugin/Texture2DSwitchDeswizzler.cs
22
from typing import Dict, Tuple
33

4-
from ..enums import TextureFormat
4+
from ..enums import TextureFormat as TF
55

66
GOB_X_TEXEL_COUNT = 4
77
GOB_Y_TEXEL_COUNT = 8
@@ -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,40 +80,40 @@ 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
86-
TEXTUREFORMAT_BLOCK_SIZE_MAP: Dict[TextureFormat, Tuple[int, int]] = {
87-
TextureFormat.Alpha8: (16, 1), # 1 byte per pixel
88-
TextureFormat.ARGB4444: (8, 1), # 2 bytes per pixel
89-
TextureFormat.RGBA32: (4, 1), # 4 bytes per pixel
90-
TextureFormat.ARGB32: (4, 1), # 4 bytes per pixel
91-
TextureFormat.ARGBFloat: (1, 1), # 16 bytes per pixel (?)
92-
TextureFormat.RGB565: (8, 1), # 2 bytes per pixel
93-
TextureFormat.R16: (8, 1), # 2 bytes per pixel
94-
TextureFormat.DXT1: (8, 4), # 8 bytes per 4x4=16 pixels
95-
TextureFormat.DXT5: (4, 4), # 16 bytes per 4x4=16 pixels
96-
TextureFormat.RGBA4444: (8, 1), # 2 bytes per pixel
97-
TextureFormat.BGRA32: (4, 1), # 4 bytes per pixel
98-
TextureFormat.BC6H: (4, 4), # 16 bytes per 4x4=16 pixels
99-
TextureFormat.BC7: (4, 4), # 16 bytes per 4x4=16 pixels
100-
TextureFormat.BC4: (8, 4), # 8 bytes per 4x4=16 pixels
101-
TextureFormat.BC5: (4, 4), # 16 bytes per 4x4=16 pixels
102-
TextureFormat.ASTC_RGB_4x4: (4, 4), # 16 bytes per 4x4=16 pixels
103-
TextureFormat.ASTC_RGB_5x5: (5, 5), # 16 bytes per 5x5=25 pixels
104-
TextureFormat.ASTC_RGB_6x6: (6, 6), # 16 bytes per 6x6=36 pixels
105-
TextureFormat.ASTC_RGB_8x8: (8, 8), # 16 bytes per 8x8=64 pixels
106-
TextureFormat.ASTC_RGB_10x10: (10, 10), # 16 bytes per 10x10=100 pixels
107-
TextureFormat.ASTC_RGB_12x12: (12, 12), # 16 bytes per 12x12=144 pixels
108-
TextureFormat.ASTC_RGBA_4x4: (4, 4), # 16 bytes per 4x4=16 pixels
109-
TextureFormat.ASTC_RGBA_5x5: (5, 5), # 16 bytes per 5x5=25 pixels
110-
TextureFormat.ASTC_RGBA_6x6: (6, 6), # 16 bytes per 6x6=36 pixels
111-
TextureFormat.ASTC_RGBA_8x8: (8, 8), # 16 bytes per 8x8=64 pixels
112-
TextureFormat.ASTC_RGBA_10x10: (10, 10), # 16 bytes per 10x10=100 pixels
113-
TextureFormat.ASTC_RGBA_12x12: (12, 12), # 16 bytes per 12x12=144 pixels
114-
TextureFormat.RG16: (8, 1), # 2 bytes per pixel
115-
TextureFormat.R8: (16, 1), # 1 byte per pixel
87+
TEXTUREFORMAT_BLOCK_SIZE_MAP: Dict[TF, Tuple[int, int]] = {
88+
TF.Alpha8: (16, 1), # 1 byte per pixel
89+
TF.ARGB4444: (8, 1), # 2 bytes per pixel
90+
TF.RGBA32: (4, 1), # 4 bytes per pixel
91+
TF.ARGB32: (4, 1), # 4 bytes per pixel
92+
TF.ARGBFloat: (1, 1), # 16 bytes per pixel (?)
93+
TF.RGB565: (8, 1), # 2 bytes per pixel
94+
TF.R16: (8, 1), # 2 bytes per pixel
95+
TF.DXT1: (8, 4), # 8 bytes per 4x4=16 pixels
96+
TF.DXT5: (4, 4), # 16 bytes per 4x4=16 pixels
97+
TF.RGBA4444: (8, 1), # 2 bytes per pixel
98+
TF.BGRA32: (4, 1), # 4 bytes per pixel
99+
TF.BC6H: (4, 4), # 16 bytes per 4x4=16 pixels
100+
TF.BC7: (4, 4), # 16 bytes per 4x4=16 pixels
101+
TF.BC4: (8, 4), # 8 bytes per 4x4=16 pixels
102+
TF.BC5: (4, 4), # 16 bytes per 4x4=16 pixels
103+
TF.ASTC_RGB_4x4: (4, 4), # 16 bytes per 4x4=16 pixels
104+
TF.ASTC_RGB_5x5: (5, 5), # 16 bytes per 5x5=25 pixels
105+
TF.ASTC_RGB_6x6: (6, 6), # 16 bytes per 6x6=36 pixels
106+
TF.ASTC_RGB_8x8: (8, 8), # 16 bytes per 8x8=64 pixels
107+
TF.ASTC_RGB_10x10: (10, 10), # 16 bytes per 10x10=100 pixels
108+
TF.ASTC_RGB_12x12: (12, 12), # 16 bytes per 12x12=144 pixels
109+
TF.ASTC_RGBA_4x4: (4, 4), # 16 bytes per 4x4=16 pixels
110+
TF.ASTC_RGBA_5x5: (5, 5), # 16 bytes per 5x5=25 pixels
111+
TF.ASTC_RGBA_6x6: (6, 6), # 16 bytes per 6x6=36 pixels
112+
TF.ASTC_RGBA_8x8: (8, 8), # 16 bytes per 8x8=64 pixels
113+
TF.ASTC_RGBA_10x10: (10, 10), # 16 bytes per 10x10=100 pixels
114+
TF.ASTC_RGBA_12x12: (12, 12), # 16 bytes per 12x12=144 pixels
115+
TF.RG16: (8, 1), # 2 bytes per pixel
116+
TF.R8: (16, 1), # 1 byte per pixel
116117
}
117118

118119

0 commit comments

Comments
 (0)