Skip to content

Commit

Permalink
1.20.16 - Texture2DConverter - astc decompress - fix decompress impor…
Browse files Browse the repository at this point in the history
…t by checking length
  • Loading branch information
K0lb3 committed Nov 20, 2024
1 parent 03098aa commit 13e102b
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 2 deletions.
2 changes: 1 addition & 1 deletion UnityPy/__init__.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
__version__ = "1.20.15"
__version__ = "1.20.16"

from .environment import Environment as Environment
from .helpers.ArchiveStorageManager import (
Expand Down
17 changes: 16 additions & 1 deletion UnityPy/export/Texture2DConverter.py
Original file line number Diff line number Diff line change
Expand Up @@ -268,11 +268,26 @@ def astc(image_data: bytes, width: int, height: int, block_size: tuple) -> Image
context = ASTC_CONTEXTS[block_size] = astc_encoder.ASTCContext(config)

image = astc_encoder.ASTCImage(astc_encoder.ASTCType.U8, width, height, 1)
context.decompress(image_data, image, astc_encoder.ASTCSwizzle.from_str("RGBA"))
texture_size = calculate_astc_compressed_size(width, height, block_size)
if len(image_data) < texture_size:
raise ValueError(f"Invalid ASTC data size: {len(image_data)} < {texture_size}")
context.decompress(
image_data[:texture_size], image, astc_encoder.ASTCSwizzle.from_str("RGBA")
)

return Image.frombytes("RGBA", (width, height), image.data, "raw", "RGBA")


def calculate_astc_compressed_size(width: int, height: int, block_size: tuple) -> int:
"""Calculate the size of the compressed data for ASTC."""
# calculate the number of blocks
block_count_x = (width + block_size[0] - 1) // block_size[0]
block_count_y = (height + block_size[1] - 1) // block_size[1]
# ignore depth for 2D textures
# calculate the size of the compressed data
return block_count_x * block_count_y * 16


def pvrtc(image_data: bytes, width: int, height: int, fmt: bool) -> Image.Image:
image_data = texture2ddecoder.decode_pvrtc(image_data, width, height, fmt)
return Image.frombytes("RGBA", (width, height), image_data, "raw", "BGRA")
Expand Down

0 comments on commit 13e102b

Please sign in to comment.