File: compress_test_float.py

package info (click to toggle)
basis-universal 2.0.2-3
  • links: PTS
  • area: main
  • in suites: forky, sid
  • size: 216,436 kB
  • sloc: cpp: 163,224; ansic: 51,368; python: 2,824; javascript: 2,637; lisp: 1,026; sh: 161; makefile: 17
file content (76 lines) | stat: -rw-r--r-- 1,982 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
# compress_test_float.py

from .basisu_wasm import BasisuWasm, BasisTexFormat, BasisEffort, BasisFlags, BasisQuality
import struct   # for packing floats

# === Load WASM ===
codec = BasisuWasm("basisu_py/wasm/basisu_module_st.wasm")
codec.load()

print("Version =", codec.version())

# === Build a 256x256 FLOAT RGBA image ===
W, H = 256, 256
BYTES_PER_PIXEL = 16  # float32 * 4
pitch = W * BYTES_PER_PIXEL

# Float image stored as bytearray of packed floats
img = bytearray(W * H * BYTES_PER_PIXEL)

for y in range(H):
    for x in range(W):
        # Create some float HDR gradient pattern
        r = float(x) / W               # 0.0 ? 1.0
        g = float(y) / H               # 0.0 ? 1.0
        b = float(x ^ y) / 255.0       # quirky pattern
        a = 1.0

        i = (y * W + x) * 4

        # pack into img bytearray
        struct.pack_into("ffff", img, i*4, r, g, b, a)

print("Created FLOAT RGBA image.")

# === Upload to WASM memory ===
img_ptr = codec.alloc(len(img))
codec.write_bytes(img_ptr, img)
print("Copied float image into WASM heap at", img_ptr)

# === Create params ===
params = codec.new_params()

# === Set FLOAT RGBA image ===
ok = codec.set_image_float_rgba(params, 0, img_ptr, W, H, pitch)
print("Set float RGBA:", ok)

# === Compress using HDR UASTC 4x4 ===
ok = codec.compress(
    params,
    tex_format=BasisTexFormat.cUASTC_HDR_4x4,
    quality=BasisQuality.MAX,
    effort=BasisEffort.DEFAULT,
    flags=BasisFlags.KTX2_OUTPUT | BasisFlags.REC2020,  # optional: HDR color space
    rdo_quality=0.0
)

print("Compression result:", ok)

# === Retrieve compressed HDR KTX2 ===
ofs  = codec.get_comp_data_ofs(params)
size = codec.get_comp_data_size(params)

print("Output size =", size)
data = codec.read_bytes(ofs, size)

print("First 16 bytes:", data[:16])

# === Save to test_hdr.ktx2 ===
with open("test_hdr.ktx2", "wb") as f:
    f.write(data)

print("Wrote test_hdr.ktx2")

# === Cleanup ===
codec.delete_params(params)
codec.free(img_ptr)