Created
January 3, 2026 12:39
-
-
Save chayleaf/e996659f2da3baa949605cbb000f3181 to your computer and use it in GitHub Desktop.
decompress dxt5 (s3) compressed textures in python
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| def undxt5(w, h, data): | |
| file = io.BytesIO(data) | |
| ret = [] | |
| rows = [[], [], [], []] | |
| for row in range(h // 4): | |
| for x in rows: | |
| x.clear() | |
| for col in range(w // 4): | |
| a0, a1, at0, at1, c0, c1, ct = struct.unpack("<BBIHHHI", file.read(16)) | |
| a = [a0, a1] | |
| at = at0 + (at1 << 32) | |
| num = 7 if a0 > a1 else 5 | |
| a.extend((a0 * (num - i) + a1 * i) // num for i in range(1, num)) | |
| if len(a) < 8: | |
| a.extend([0, 255]) | |
| c = [ | |
| ( | |
| ((x >> 11) * 527 + 23) >> 6, | |
| (((x >> 5) & 0x3F) * 259 + 33) >> 6, | |
| ((x & 0x1F) * 527 + 23) >> 6, | |
| ) | |
| for x in (c0, c1) | |
| ] | |
| num = 3 if c0 > c1 else 2 | |
| c.extend( | |
| tuple( | |
| (d * (num - i) + e * i) // num | |
| for d, e in zip(c[0], c[1]) | |
| ) | |
| for i in range(1, num) | |
| ) | |
| if len(c) < 4: | |
| c.append((0, 0, 0)) | |
| for i in range(16): | |
| rows[i >> 2].extend(c[(ct >> (i * 2)) & 0b11]) | |
| rows[i >> 2].append(a[(at >> (i * 3)) & 0b111]) | |
| for row in rows: | |
| ret.extend(row) | |
| return bytes(ret) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment