|
3 | 3 | import struct |
4 | 4 | from copy import copy |
5 | 5 | from io import BytesIO |
6 | | -from typing import TYPE_CHECKING, Tuple, Union |
| 6 | +from typing import TYPE_CHECKING, Dict, Tuple, Union |
7 | 7 |
|
| 8 | +import astc_encoder |
8 | 9 | import texture2ddecoder |
9 | 10 | from PIL import Image |
10 | 11 |
|
@@ -68,8 +69,6 @@ def image_to_texture2d( |
68 | 69 | tex_format = TF.ETC2_RGBA8 |
69 | 70 | # ASTC |
70 | 71 | elif target_texture_format.name.startswith("ASTC"): |
71 | | - import astc_encoder |
72 | | - |
73 | 72 | raw_img = img.tobytes("raw", "RGBA") |
74 | 73 |
|
75 | 74 | block_size = tuple( |
@@ -253,9 +252,25 @@ def atc(image_data: bytes, width: int, height: int, alpha: bool) -> Image.Image: |
253 | 252 | return Image.frombytes("RGBA", (width, height), image_data, "raw", "BGRA") |
254 | 253 |
|
255 | 254 |
|
| 255 | +ASTC_CONTEXTS: Dict[Tuple[int, int], astc_encoder.ASTCContext] = {} |
| 256 | + |
| 257 | + |
256 | 258 | def astc(image_data: bytes, width: int, height: int, block_size: tuple) -> Image.Image: |
257 | | - image_data = texture2ddecoder.decode_astc(image_data, width, height, *block_size) |
258 | | - return Image.frombytes("RGBA", (width, height), image_data, "raw", "BGRA") |
| 259 | + context = ASTC_CONTEXTS.get(block_size) |
| 260 | + if context is None: |
| 261 | + config = astc_encoder.ASTCConfig( |
| 262 | + astc_encoder.ASTCProfile.LDR, |
| 263 | + *block_size, |
| 264 | + 1, |
| 265 | + 100, |
| 266 | + astc_encoder.ASTCConfigFlags.USE_DECODE_UNORM8, |
| 267 | + ) |
| 268 | + context = ASTC_CONTEXTS[block_size] = astc_encoder.ASTCContext(config) |
| 269 | + |
| 270 | + image = astc_encoder.ASTCImage(astc_encoder.ASTCType.U8, width, height, 1) |
| 271 | + context.decompress(image_data, image, astc_encoder.ASTCSwizzle.from_str("RGBA")) |
| 272 | + |
| 273 | + return Image.frombytes("RGBA", (width, height), image.data, "raw", "RGBA") |
259 | 274 |
|
260 | 275 |
|
261 | 276 | def pvrtc(image_data: bytes, width: int, height: int, fmt: bool) -> Image.Image: |
|
0 commit comments