Skip to content

Commit 59586b5

Browse files
committed
TextureSwizzler - implement swizzle
1 parent 0557cd5 commit 59586b5

File tree

1 file changed

+31
-32
lines changed

1 file changed

+31
-32
lines changed

UnityPy/helpers/TextureSwizzler.py

Lines changed: 31 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -49,38 +49,37 @@ def deswizzle(
4949
return new_data
5050

5151

52-
# def switch_swizzle(
53-
# src_image: Image.Image, block_size: Tuple[int, int], texels_per_block: int
54-
# ) -> Image.Image:
55-
# dstImage = Image.new(src_image.mode, src_image.size)
56-
57-
# blockCountX, blockCountY = map(ceil_divide, zip(src_image.size, block_size))
58-
59-
# gob_count_x = blockCountX / GOB_X_BLOCK_COUNT
60-
# gob_count_y = blockCountY / GOB_Y_BLOCK_COUNT
61-
62-
# dstX = 0
63-
# dstY = 0
64-
65-
# for i in range(gob_count_y):
66-
# for j in range(gob_count_x):
67-
# for k in range(texels_per_block):
68-
# for l in range(BLOCKS_IN_GOB):
69-
# # todo: use table for speedy boi
70-
# gobX = ((l >> 3) & 0b10) | ((l >> 1) & 0b1)
71-
# gobY = ((l >> 1) & 0b110) | (l & 0b1)
72-
# gobSrcX = j * GOB_X_BLOCK_COUNT + gobX
73-
# gobSrcY = (i * texels_per_block + k) * GOB_Y_BLOCK_COUNT + gobY
74-
# CopyBlock(
75-
# src_image, dstImage, gobSrcX, gobSrcY, dstX, dstY, *block_size
76-
# )
77-
78-
# dstX += 1
79-
# if dstX >= blockCountX:
80-
# dstX = 0
81-
# dstY += 1
82-
83-
# return dstImage
52+
def swizzle(
53+
data: bytes,
54+
width: int,
55+
height: int,
56+
block_width: int,
57+
block_height: int,
58+
texels_per_block: int,
59+
) -> bytearray:
60+
block_count_x = ceil_divide(width, block_width)
61+
block_count_y = ceil_divide(height, block_height)
62+
gob_count_x = block_count_x // GOB_X_TEXEL_COUNT
63+
gob_count_y = block_count_y // GOB_Y_TEXEL_COUNT
64+
new_data = bytearray(len(data))
65+
data_view = memoryview(new_data)
66+
67+
for i in range(gob_count_y // texels_per_block):
68+
for j in range(gob_count_x):
69+
base_gob_dst_x = j * 4
70+
for k in range(texels_per_block):
71+
base_gob_dst_y = (i * texels_per_block + k) * GOB_Y_TEXEL_COUNT
72+
for gob_x, gob_y in GOB_MAP:
73+
src_offset = (
74+
(base_gob_dst_y + gob_y) * block_count_x
75+
+ (base_gob_dst_x + gob_x)
76+
) * TEXEL_BYTE_SIZE
77+
data_view[:TEXEL_BYTE_SIZE] = data[
78+
src_offset : src_offset + TEXEL_BYTE_SIZE
79+
]
80+
data_view = data_view[TEXEL_BYTE_SIZE:]
81+
82+
return new_data
8483

8584

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

0 commit comments

Comments
 (0)