Skip to content

Commit c731cb0

Browse files
authored
Switch Swizzle Support (K0lb3#219)
* Add integer texture formats, rg decoder and rgb9e5float decoder * Create SwitchDeswizzle.py * Texture2DDecoder - fix typo * implement Switch Texture Deswizzling * hotfix TextureSwizzler.c
1 parent 1704fac commit c731cb0

7 files changed

Lines changed: 382 additions & 16 deletions

File tree

‎UnityPy/enums/TextureFormat.py‎

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,12 @@ class TextureFormat(IntEnum):
77
RGB24 = 3
88
RGBA32 = 4
99
ARGB32 = 5
10+
ARGBFloat = 6
1011
RGB565 = 7
12+
BGR24 = 8
1113
R16 = 9
1214
DXT1 = 10
15+
DXT3 = 11
1316
DXT5 = 12
1417
RGBA4444 = 13
1518
BGRA32 = 14
@@ -21,10 +24,11 @@ class TextureFormat(IntEnum):
2124
RGBAFloat = 20
2225
YUY2 = 21
2326
RGB9e5Float = 22
24-
BC4 = 26
25-
BC5 = 27
27+
RGBFloat = 23
2628
BC6H = 24
2729
BC7 = 25
30+
BC4 = 26
31+
BC5 = 27
2832
DXT1Crunched = 28
2933
DXT5Crunched = 29
3034
PVRTC_RGB2 = 30
@@ -65,3 +69,14 @@ class TextureFormat(IntEnum):
6569
ASTC_HDR_8x8 = 69
6670
ASTC_HDR_10x10 = 70
6771
ASTC_HDR_12x12 = 71
72+
RG32 = 72
73+
RGB48 = 73
74+
RGBA64 = 74
75+
R8_SIGNED = 75
76+
RG16_SIGNED = 76
77+
RGB24_SIGNED = 77
78+
RGBA32_SIGNED = 78
79+
R16_SIGNED = 79
80+
RG32_SIGNED = 80
81+
RGB48_SIGNED = 81
82+
RGBA64_SIGNED = 82

‎UnityPy/export/Texture2DConverter.py‎

Lines changed: 75 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
from io import BytesIO
66
import struct
77
from ..enums import TextureFormat, BuildTarget
8+
from ..helpers import TextureSwizzler
89

910
TF = TextureFormat
1011

@@ -133,8 +134,20 @@ def get_image_from_texture2d(texture_2d, flip=True) -> Image.Image:
133134
image_data, texture_2d.m_Width, texture_2d.m_Height, *selection[1:]
134135
)
135136

137+
if texture_2d.platform == BuildTarget.Switch and getattr(
138+
texture_2d, "m_PlatformBlob", None
139+
):
140+
gobsPerBlock = TextureSwizzler.get_switch_gobs_per_block(
141+
texture_2d.m_PlatformBlob
142+
)
143+
blockSize = TextureSwizzler.TEXTUREFORMAT_BLOCK_SIZE_MAP[
144+
texture_2d.m_TextureFormat
145+
]
146+
img = TextureSwizzler.switch_deswizzle(img, blockSize, gobsPerBlock)
147+
136148
if img and flip:
137149
return img.transpose(Image.FLIP_TOP_BOTTOM)
150+
138151
return img
139152

140153

@@ -241,6 +254,48 @@ def half(
241254
return pillow(image_data, width, height, mode, codec, args, swap)
242255

243256

257+
RG_PADDING_MAP = {
258+
"RGE": 16,
259+
"RGF": 32,
260+
"RG;16": 16,
261+
"RG;16s": 16,
262+
"RG;8s": 8,
263+
}
264+
265+
266+
def rg(
267+
image_data: bytes, width: int, height: int, mode: str, codec: str, args
268+
) -> Image.Image:
269+
# convert rg to rgb by adding in zeroes
270+
padding_size = RG_PADDING_MAP[codec]
271+
stream = BytesIO(image_data)
272+
padding = bytes(padding_size)
273+
rgb_data = b"".join(
274+
stream.read(padding_size * 2) + padding
275+
for _ in range(image_data / (2 * padding_size))
276+
)
277+
if codec == "RGE":
278+
return half(rgb_data, width, height, mode, "RGB", args)
279+
else:
280+
return pillow(rgb_data, width, height, mode, codec.replace("RG", "RGB"), args)
281+
282+
283+
def rgb9e5float(image_data: bytes, width: int, height: int):
284+
rgb = bytearray(width * height * 3)
285+
for i, (n,) in enumerate(struct.iter_unpack("<i", image_data)):
286+
scale = n >> 27 & 0x1F
287+
scalef = 2 ** (scale - 24)
288+
scaleb = scalef * 255.0
289+
b = (n >> 18 & 0x1FF) * scaleb
290+
g = (n >> 9 & 0x1FF) * scaleb
291+
r = (n & 0x1FF) * scaleb
292+
293+
offset = i * 3
294+
rgb[offset : offset + 3] = [r, g, b]
295+
296+
return Image.frombytes("RGB", (width, height), rgb, "raw", "RGB")
297+
298+
244299
CONV_TABLE = {
245300
# FORMAT FUNC #ARGS.....
246301
#----------------------- -------- -------- ------------ ----------------- ------------ ----------
@@ -249,32 +304,35 @@ def half(
249304
( TF.RGB24, pillow, "RGB", "raw", "RGB" ),
250305
( TF.RGBA32, pillow, "RGBA", "raw", "RGBA" ),
251306
( TF.ARGB32, pillow, "RGBA", "raw", "ARGB" ),
307+
( TF.ARGBFloat, pillow, "RGBA", "raw", "RGBAF", (2,1,0,3) ),
252308
( TF.RGB565, pillow, "RGB", "raw", "BGR;16" ),
309+
( TF.BGR24, pillow, "RGB", "raw", "BGR" ),
253310
( TF.R8, pillow, "RGB", "raw", "R" ),
254311
( TF.R16, pillow, "RGB", "raw", "R;16" ),
255-
( TF.RG16, ),
312+
( TF.RG16, rg, "RGB", "raw", "RG" ),
256313
( TF.DXT1, pillow, "RGBA", "bcn", 1 ),
314+
( TF.DXT3, pillow, "RGBA", "bcn", 2 ),
257315
( TF.DXT5, pillow, "RGBA", "bcn", 3 ),
258316
( TF.RGBA4444, pillow, "RGBA", "raw", 'RGBA;4B', (3,2,1,0) ),
259317
( TF.BGRA32, pillow, "RGBA", "raw", "BGRA" ),
260318
( TF.RHalf, half, "R", "raw", "R" ),
261-
( TF.RGHalf, ),
319+
( TF.RGHalf, rg, "RGB", "raw", "RGE" ),
262320
( TF.RGBAHalf, half, "RGB", "raw", "RGB" ),
263321
( TF.RFloat, pillow, "RGB", "raw", "RF" ),
264-
( TF.RGFloat, ),
322+
( TF.RGFloat, rg, "RGB", "raw", "RGF" ),
265323
( TF.RGBAFloat, pillow, "RGBA", "raw", "RGBAF" ),
266324
( TF.YUY2, ),
267-
( TF.RGB9e5Float, ),
325+
( TF.RGB9e5Float, rgb9e5float ),
268326
( TF.BC4, pillow, "L", "bcn", 4 ),
269327
( TF.BC5, pillow, "RGB", "bcn", 5 ),
270328
( TF.BC6H, pillow, "RGBA", "bcn", 6 ),
271329
( TF.BC7, pillow, "RGBA", "bcn", 7 ),
272330
( TF.DXT1Crunched, pillow, "RGBA", "bcn", 1 ),
273331
( TF.DXT5Crunched, pillow, "RGBA", "bcn", 3 ),
274-
( TF.PVRTC_RGB2, pvrtc, True ),
332+
( TF.PVRTC_RGB2, pvrtc, True ),
275333
( TF.PVRTC_RGBA2, pvrtc, True ),
276334
( TF.PVRTC_RGB4, pvrtc, False ),
277-
( TF.PVRTC_RGBA4, pvrtc, False ),
335+
( TF.PVRTC_RGBA4, pvrtc, False ),
278336
( TF.ETC_RGB4, etc, (1,) ),
279337
( TF.ATC_RGB4, atc, False ),
280338
( TF.ATC_RGBA8, atc, True ),
@@ -307,6 +365,17 @@ def half(
307365
( TF.ASTC_HDR_8x8, astc, (8,8) ),
308366
( TF.ASTC_HDR_10x10, astc, (10,10) ),
309367
( TF.ASTC_HDR_12x12, astc, (12,12) ),
368+
( TF.RG32, rg, "RGB", "raw", "RG;16" ),
369+
( TF.RGB48, pillow, "RGB", "raw", "RGB;16" ),
370+
( TF.RGBA64, pillow, "RGBA", "raw", "RGBA;16" ),
371+
( TF.R8_SIGNED, pillow, "R", "raw", "R;8s" ),
372+
( TF.RG16_SIGNED, rg, "RGB", "raw", "RG;8s" ),
373+
( TF.RGB24_SIGNED, pillow, "RGB", "raw", "RGB;8s" ),
374+
( TF.RGBA32_SIGNED, pillow, "RGBA", "raw", "RGBA;8s" ),
375+
( TF.R16_SIGNED, pillow, "R", "raw", "R;16s" ),
376+
( TF.RG32_SIGNED, rg, "RGB", "raw", "RG;16s" ),
377+
( TF.RGB48_SIGNED, pillow, "RGB", "raw", "RGB;16s" ),
378+
( TF.RGBA64_SIGNED, pillow, "RGBA", "raw", "RGBA;16s" ),
310379
}
311380

312381
# format conv_table to a dict

‎UnityPy/helpers/TextureSwizzler.py‎

Lines changed: 180 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,180 @@
1+
# based on https://github.com/nesrak1/UABEA/blob/master/TexturePlugin/Texture2DSwitchDeswizzler.cs
2+
from typing import Dict, Tuple
3+
4+
from PIL import Image
5+
6+
from ..enums import TextureFormat
7+
8+
try:
9+
from ..UnityPyBoost import switch_deswizzle as switch_deswizzle_c
10+
except:
11+
switch_deswizzle_c = None
12+
13+
14+
GOB_X_BLOCK_COUNT = 4
15+
GOB_Y_BLOCK_COUNT = 8
16+
BLOCKS_IN_GOB = GOB_X_BLOCK_COUNT * GOB_Y_BLOCK_COUNT
17+
18+
19+
def ceil_divide(a: int, b: int) -> int:
20+
return (a + b - 1) / b
21+
22+
23+
def switch_deswizzle(
24+
src_image: Image.Image, block_size: Tuple[int, int], gobs_per_block: int
25+
) -> Image.Image:
26+
src_bytes = src_image.tobytes()
27+
pixel_width = len(src_image.mode)
28+
29+
if switch_deswizzle_c:
30+
dst_bytes = switch_deswizzle_c(
31+
src_bytes, pixel_width, *src_image.size, *block_size, gobs_per_block
32+
)
33+
return Image.frombytes(src_image.mode, src_image.size, dst_bytes)
34+
35+
dst_bytes = bytearray(len(src_bytes))
36+
37+
width, height = src_image.size
38+
block_width, block_height = block_size
39+
block_count_x = width // block_width
40+
block_count_y = height // block_height
41+
42+
gob_count_x = block_count_x // GOB_X_BLOCK_COUNT
43+
gob_count_y = block_count_y // GOB_Y_BLOCK_COUNT
44+
45+
src_x = 0
46+
src_y = 0
47+
48+
block_pixel_width = block_width * pixel_width
49+
row_pixel_width = width * pixel_width
50+
data_size = len(src_bytes)
51+
52+
GOB_MAP = [
53+
(((l >> 3) & 0b10) | ((l >> 1) & 0b1), ((l >> 1) & 0b110) | (l & 0b1))
54+
for l in range(BLOCKS_IN_GOB)
55+
]
56+
57+
for y in range(gob_count_y):
58+
for x in range(gob_count_x):
59+
for k in range(gobs_per_block):
60+
for gob_x, gob_y in GOB_MAP:
61+
gob_dst_x = x * GOB_X_BLOCK_COUNT + gob_x
62+
gob_dst_y = (y * gobs_per_block + k) * GOB_Y_BLOCK_COUNT + gob_y
63+
64+
src_offset = (
65+
src_x * block_width + (src_y * block_height) * width
66+
) * pixel_width
67+
dst_offset = (
68+
gob_dst_x * block_width + (gob_dst_y * block_height) * width
69+
) * pixel_width
70+
71+
for by in range(block_height):
72+
if dst_offset > data_size or src_offset > data_size:
73+
break
74+
copy_width = block_pixel_width
75+
if (data_size - src_offset) < block_pixel_width:
76+
copy_width = data_size - src_offset
77+
elif (data_size - dst_offset) < block_pixel_width:
78+
copy_width = data_size - dst_offset
79+
80+
dst_bytes[dst_offset : dst_offset + copy_width] = src_bytes[
81+
src_offset : src_offset + copy_width
82+
]
83+
src_offset += row_pixel_width
84+
dst_offset += row_pixel_width
85+
86+
src_x += 1
87+
if src_x >= block_count_x:
88+
src_x = 0
89+
src_y += 1
90+
91+
return Image.frombytes(src_image.mode, src_image.size, dst_bytes)
92+
93+
94+
def switch_swizzle(
95+
src_image: Image.Image, block_size: Tuple[int, int], gobs_per_block: int
96+
) -> Image.Image:
97+
dstImage = Image.new(src_image.mode, src_image.size)
98+
99+
blockCountX, blockCountY = map(ceil_divide, zip(src_image.size, block_size))
100+
101+
gob_count_x = blockCountX / GOB_X_BLOCK_COUNT
102+
gob_count_y = blockCountY / GOB_Y_BLOCK_COUNT
103+
104+
dstX = 0
105+
dstY = 0
106+
107+
for i in range(gob_count_y):
108+
for j in range(gob_count_x):
109+
for k in range(gobs_per_block):
110+
for l in range(BLOCKS_IN_GOB):
111+
# todo: use table for speedy boi
112+
gobX = ((l >> 3) & 0b10) | ((l >> 1) & 0b1)
113+
gobY = ((l >> 1) & 0b110) | (l & 0b1)
114+
gobSrcX = j * GOB_X_BLOCK_COUNT + gobX
115+
gobSrcY = (i * gobs_per_block + k) * GOB_Y_BLOCK_COUNT + gobY
116+
CopyBlock(
117+
src_image, dstImage, gobSrcX, gobSrcY, dstX, dstY, *block_size
118+
)
119+
120+
dstX += 1
121+
if dstX >= blockCountX:
122+
dstX = 0
123+
dstY += 1
124+
125+
return dstImage
126+
127+
128+
# this should be the amount of pixels that can fit 16 bytes
129+
TEXTUREFORMAT_BLOCK_SIZE_MAP: Dict[TextureFormat, Tuple[int, int]] = {
130+
TextureFormat.Alpha8: [16, 1], # 1 byte per pixel
131+
TextureFormat.ARGB4444: [8, 1], # 2 bytes per pixel
132+
TextureFormat.RGBA32: [4, 1], # 4 bytes per pixel
133+
TextureFormat.ARGB32: [4, 1], # 4 bytes per pixel
134+
TextureFormat.ARGBFloat: [1, 1], # 16 bytes per pixel (?)
135+
TextureFormat.RGB565: [8, 1], # 2 bytes per pixel
136+
TextureFormat.R16: [8, 1], # 2 bytes per pixel
137+
TextureFormat.DXT1: [8, 4], # 8 bytes per 4x4=16 pixels
138+
TextureFormat.DXT5: [4, 4], # 16 bytes per 4x4=16 pixels
139+
TextureFormat.RGBA4444: [8, 1], # 2 bytes per pixel
140+
TextureFormat.BGRA32: [4, 1], # 4 bytes per pixel
141+
TextureFormat.BC6H: [4, 4], # 16 bytes per 4x4=16 pixels
142+
TextureFormat.BC7: [4, 4], # 16 bytes per 4x4=16 pixels
143+
TextureFormat.BC4: [8, 4], # 8 bytes per 4x4=16 pixels
144+
TextureFormat.BC5: [4, 4], # 16 bytes per 4x4=16 pixels
145+
TextureFormat.ASTC_RGB_4x4: [4, 4], # 16 bytes per 4x4=16 pixels
146+
TextureFormat.ASTC_RGB_5x5: [5, 5], # 16 bytes per 5x5=25 pixels
147+
TextureFormat.ASTC_RGB_6x6: [6, 6], # 16 bytes per 6x6=36 pixels
148+
TextureFormat.ASTC_RGB_8x8: [8, 8], # 16 bytes per 8x8=64 pixels
149+
TextureFormat.ASTC_RGB_10x10: [10, 10], # 16 bytes per 10x10=100 pixels
150+
TextureFormat.ASTC_RGB_12x12: [12, 12], # 16 bytes per 12x12=144 pixels
151+
TextureFormat.ASTC_RGBA_4x4: [4, 4], # 16 bytes per 4x4=16 pixels
152+
TextureFormat.ASTC_RGBA_5x5: [5, 5], # 16 bytes per 5x5=25 pixels
153+
TextureFormat.ASTC_RGBA_6x6: [6, 6], # 16 bytes per 6x6=36 pixels
154+
TextureFormat.ASTC_RGBA_8x8: [8, 8], # 16 bytes per 8x8=64 pixels
155+
TextureFormat.ASTC_RGBA_10x10: [10, 10], # 16 bytes per 10x10=100 pixels
156+
TextureFormat.ASTC_RGBA_12x12: [12, 12], # 16 bytes per 12x12=144 pixels
157+
TextureFormat.RG16: [8, 1], # 2 bytes per pixel
158+
TextureFormat.R8: [16, 1], # 1 byte per pixel
159+
}
160+
161+
162+
def get_padded_texture_size(
163+
width: int, height: int, block_width: int, block_height: int, gobs_per_block: int
164+
):
165+
width = (
166+
ceil_divide(width, block_width * GOB_X_BLOCK_COUNT)
167+
* block_width
168+
* GOB_X_BLOCK_COUNT
169+
)
170+
height = (
171+
ceil_divide(height, block_height * GOB_Y_BLOCK_COUNT * gobs_per_block)
172+
* block_height
173+
* GOB_Y_BLOCK_COUNT
174+
* gobs_per_block
175+
)
176+
return width, height
177+
178+
179+
def get_switch_gobs_per_block(platform_blob: bytes) -> int:
180+
return 1 << int.from_bytes(platform_blob[8:12], "little")

0 commit comments

Comments
 (0)