# BCn fixtures (tools/fixtures.py): random BC1..BC7 blocks (every BC7 mode and BC6H mode forced in turn, the BC1 # 3-colour mode, odd image sizes) decoded by two references: Pillow's DDS reader and Godot 4 (bcdec); plus encoder # baselines: test images and the PSNR Pillow's own BC1 / BC3 / BC5 encoder reaches on them. # tests/gen/bcn.luau { cases = { { name, kind, w, h, data, pillow?, godot?, tol } }, enc = { { name, w, h, # rgba, psnr = { bc1, bc3, bc5 } } } } (pillow / godot: rgba8 bytes, or rgbaf32 for BC6H) # tests/gen/bcn_bench.luau a 256 x 256 image and random BC1 / BC6H / BC7 blocks for tools/bench.luau import importlib.util, math, os, random, struct _spec = importlib.util.spec_from_file_location("_tex_reference", os.path.join(os.path.dirname(os.path.abspath(__file__)), "_tex_reference.py")) R = importlib.util.module_from_spec(_spec) _spec.loader.exec_module(R) DXGI = {"bc1": 71, "bc2": 74, "bc3": 77, "bc4": 80, "bc5": 83, "bc5s": 84, "bc6h": 95, "bc6hs": 96, "bc7": 98} BLOCK = {"bc1": 8, "bc2": 16, "bc3": 16, "bc4": 8, "bc5": 16, "bc5s": 16, "bc6h": 16, "bc6hs": 16, "bc7": 16} BC6_HEADERS = [0, 1, 2, 6, 10, 14, 18, 22, 26, 30, 3, 7, 11, 15, 19, 23, 27, 31] def blocks(rng, kind, n, mode=None): out = bytearray() for _ in range(n): b = bytearray(rng.getrandbits(8) for _ in range(BLOCK[kind])) if kind == "bc7" and mode is not None: b[0] = (b[0] & ~((2 << mode) - 1) & 0xFF) | (1 << mode) elif kind.startswith("bc6h") and mode is not None: bitsn = 2 if mode < 2 else 5 b[0] = (b[0] & ~((1 << bitsn) - 1) & 0xFF) | mode out += b return bytes(out) def test_image(w, h, rng, alpha): px = bytearray() for y in range(h): for x in range(w): r = int(128 + 100 * math.sin(x / 7.0) * math.cos(y / 11.0)) g = int(255 * x / (w - 1)) b = int(255 * y / (h - 1)) if (x // 16 + y // 16) % 2 else 40 n = rng.randrange(-12, 13) r, g, b = [max(0, min(255, v + n)) for v in (r, g, b)] a = 255 if alpha: a = 0 if (x - w / 2) ** 2 + (y - h / 2) ** 2 < (w / 4) ** 2 else 255 px += bytes((r, g, b, a)) return bytes(px) def psnr(a, b, channels): se, n = 0, 0 for i in range(0, len(a), 4): for c in channels: d = a[i + c] - b[i + c] se += d * d n += 1 return 99.0 if se == 0 else 10 * math.log10(255 * 255 * n / se) def generate(ctx): rng = random.Random(7) cases = [] def add(name, kind, w, h, data): cases.append({"name": name, "kind": kind, "w": w, "h": h, "data": data}) for kind in ("bc1", "bc2", "bc3", "bc4", "bc5", "bc5s"): add(kind + " 32x32", kind, 32, 32, blocks(rng, kind, 64)) add(kind + " 13x7", kind, 13, 7, blocks(rng, kind, 8)) for m in range(8): add("bc7 mode %d" % m, "bc7", 32, 16, blocks(rng, "bc7", 32, m)) add("bc7 15x9", "bc7", 15, 9, blocks(rng, "bc7", 12)) for kind in ("bc6h", "bc6hs"): for m in BC6_HEADERS: add("%s mode %d" % (kind, m), kind, 16, 16, blocks(rng, kind, 16, m)) # Pillow (BC1..BC5, BC5S, BC7, BC6H as clamped 8-bit) files = [R.dds_dx10(c["w"], c["h"], DXGI[c["kind"]], c["data"]) for c in cases] for c, (mode, w, h, raw) in zip(cases, R.pillow(ctx, files)): c["pillow"] = R.rgba_from(mode, raw) # Godot (bcdec): everything but the signed BC5 (no Godot format); BC6H as floats gj, gi = [], [] for i, c in enumerate(cases): if c["kind"] in R.GODOT: gj.append((c["data"], c["w"], c["h"], R.GODOT[c["kind"]], c["kind"].startswith("bc6h"))) gi.append(i) for i, out in zip(gi, R.godot(ctx, gj)): cases[i]["godot"] = out # encoder baselines: Pillow's encoder on the same images, decoded by Pillow enc = [] for name, w, h, alpha in (("photo 64x64", 64, 64, False), ("alpha 64x48", 64, 48, True), ("odd 37x21", 37, 21, False)): px = test_image(w, h, rng, alpha) e = {"name": name, "w": w, "h": h, "rgba": px, "psnr": {}} enc.append(e) import tempfile, shutil work = tempfile.mkdtemp(prefix="formats-bcn-") try: for i, e in enumerate(enc): open(os.path.join(work, "src%d" % i), "wb").write(e["rgba"]) code = ("import os\nfrom PIL import Image\nw=%r\n" "for i, (W, H) in enumerate(%r):\n" " im=Image.frombytes('RGBA',(W,H),open(os.path.join(w,'src%%d'%%i),'rb').read())\n" " for pf in ('DXT1','DXT5','BC5'):\n" " p=os.path.join(w,'e%%d_%%s.dds'%%(i,pf))\n" " (im if pf!='BC5' else im.convert('RGB')).save(p,pixel_format=pf)\n" " back=Image.open(p).convert('RGBA')\n" " open(p+'.rgba','wb').write(back.tobytes())\n") % (work, [(e["w"], e["h"]) for e in enc]) ctx.python(["pillow"], code) for i, e in enumerate(enc): for pf, kind, ch in (("DXT1", "bc1", (0, 1, 2)), ("DXT5", "bc3", (0, 1, 2, 3)), ("BC5", "bc5", (0, 1))): back = open(os.path.join(work, "e%d_%s.dds.rgba" % (i, pf)), "rb").read() e["psnr"][kind] = psnr(e["rgba"], back, ch) finally: shutil.rmtree(work, ignore_errors=True) ctx.emit("bcn", {"cases": cases, "enc": enc}) # bench: a 512 x 512 image, and random BC7 blocks of every mode w = h = 256 px = test_image(w, h, rng, True) bench = {"w": w, "h": h, "rgba": px, "bc7": blocks(rng, "bc7", (w // 4) * (h // 4)), "bc1": blocks(rng, "bc1", (w // 4) * (h // 4)), "bc6h": blocks(rng, "bc6h", (w // 4) * (h // 4), 3)} ctx.emit("bcn_bench", bench)