ws-tex: codecs/image/Bcn (BC1-7 decode, BC1/3/4/5 encode) + codecs/image/Half
Decoders ported from Pillow BcnDecode.c (CC0) with D3D11 rules (BC6H +32 rounding, float output, reserved modes, signed RGTC). Tests against Pillow and Godot 4 (bcdec) over random blocks of every BC6H / BC7 mode; encoder PSNR above Pillow's encoder. Bench lines in tools/bench.luau. Co-Authored-By: Claude Opus 5.5 <[email protected]>
This commit is contained in:
1 parent
d3dbba500b
commit
b32d45ef99
10 files changed
+1761
No files matched your search
@@ -0,0 +1,107 @@
|
||||
# Shared by the texture fixture generators (bcn.py, etc.py, dds.py, ktx.py): reference block decoders through nix.
|
||||
# godot(ctx, jobs) Godot 4's Image.decompress (bcdec for BC1-7, etcpak for ETC1 / ETC2 / EAC): each job is
|
||||
# (raw block bytes, w, h, godot format number, float) -> rgba8 bytes, or rgbaf32 little-endian
|
||||
# bytes when float is true
|
||||
# pillow(ctx, jobs) Pillow's DDS reader (BcnDecode.c): each job is (dds bytes) -> (mode, raw bytes)
|
||||
# dds_dx10(w, h, dxgi, data) / dds_fourcc(w, h, fourcc, data): minimal DDS files around block data
|
||||
import os, shutil, struct, tempfile
|
||||
|
||||
# Godot Image.Format numbers (checked against Godot 4.7)
|
||||
GODOT = {"bc1": 17, "bc2": 18, "bc3": 19, "bc4": 20, "bc5": 21, "bc7": 22, "bc6hs": 23, "bc6h": 24, "etc1": 25,
|
||||
"eac_r11": 26, "eac_r11s": 27, "eac_rg11": 28, "eac_rg11s": 29, "etc2_rgb": 30, "etc2_rgba": 31,
|
||||
"etc2_rgba1": 32}
|
||||
|
||||
GD_SCRIPT = r'''extends SceneTree
|
||||
func _init():
|
||||
var dir = OS.get_cmdline_user_args()[0]
|
||||
var lines = FileAccess.get_file_as_string(dir + "/jobs.txt").split("\n", false)
|
||||
for line in lines:
|
||||
var a = line.split(" ")
|
||||
var data = FileAccess.get_file_as_bytes(dir + "/" + a[0])
|
||||
var img = Image.create_from_data(int(a[1]), int(a[2]), false, int(a[3]), data)
|
||||
img.decompress()
|
||||
if a[4] == "1":
|
||||
img.convert(Image.FORMAT_RGBAF)
|
||||
else:
|
||||
img.convert(Image.FORMAT_RGBA8)
|
||||
var f = FileAccess.open(dir + "/" + a[0] + ".out", FileAccess.WRITE)
|
||||
f.store_buffer(img.get_data())
|
||||
f.close()
|
||||
quit()
|
||||
'''
|
||||
|
||||
|
||||
def godot(ctx, jobs):
|
||||
work = tempfile.mkdtemp(prefix="formats-godot-")
|
||||
try:
|
||||
lines = []
|
||||
for i, (data, w, h, fmt, flt) in enumerate(jobs):
|
||||
with open(os.path.join(work, "j%d" % i), "wb") as f:
|
||||
f.write(data)
|
||||
lines.append("j%d %d %d %d %d" % (i, w, h, fmt, 1 if flt else 0))
|
||||
with open(os.path.join(work, "jobs.txt"), "w") as f:
|
||||
f.write("\n".join(lines) + "\n")
|
||||
with open(os.path.join(work, "run.gd"), "w") as f:
|
||||
f.write(GD_SCRIPT)
|
||||
ctx.nix("godot", ["godot", "--headless", "--script", os.path.join(work, "run.gd"), "--", work])
|
||||
out = []
|
||||
for i in range(len(jobs)):
|
||||
with open(os.path.join(work, "j%d.out" % i), "rb") as f:
|
||||
out.append(f.read())
|
||||
return out
|
||||
finally:
|
||||
shutil.rmtree(work, ignore_errors=True)
|
||||
|
||||
|
||||
def pillow(ctx, files):
|
||||
"""files: list of bytes (any format Pillow reads) -> list of (mode, w, h, raw bytes)"""
|
||||
work = tempfile.mkdtemp(prefix="formats-pillow-")
|
||||
try:
|
||||
for i, data in enumerate(files):
|
||||
with open(os.path.join(work, "f%d" % i), "wb") as f:
|
||||
f.write(data)
|
||||
code = ("import os\nfrom PIL import Image\nw=%r\nfor i in range(%d):\n"
|
||||
" im=Image.open(os.path.join(w,'f%%d'%%i)); im.load()\n"
|
||||
" open(os.path.join(w,'f%%d.out'%%i),'wb').write(im.tobytes())\n"
|
||||
" open(os.path.join(w,'f%%d.mode'%%i),'w').write('%%s %%d %%d'%%(im.mode,im.width,im.height))\n") % (work, len(files))
|
||||
ctx.python(["pillow"], code)
|
||||
out = []
|
||||
for i in range(len(files)):
|
||||
mode, w, h = open(os.path.join(work, "f%d.mode" % i)).read().split()
|
||||
out.append((mode, int(w), int(h), open(os.path.join(work, "f%d.out" % i), "rb").read()))
|
||||
return out
|
||||
finally:
|
||||
shutil.rmtree(work, ignore_errors=True)
|
||||
|
||||
|
||||
def dds_header(w, h, pf, caps=0x1000, mips=0, flags=0x1007, pitch=0, depth=0, caps2=0):
|
||||
if mips:
|
||||
flags |= 0x20000
|
||||
return (b"DDS " + struct.pack("<7I", 124, flags, h, w, pitch, depth, mips) + b"\0" * 44 + pf +
|
||||
struct.pack("<4I", caps, caps2, 0, 0) + b"\0" * 4)
|
||||
|
||||
|
||||
def dds_dx10(w, h, dxgi, data, array=1, misc=0, mips=0, caps2=0):
|
||||
pf = struct.pack("<2I", 32, 4) + b"DX10" + b"\0" * 20
|
||||
return dds_header(w, h, pf, mips=mips, caps2=caps2) + struct.pack("<5I", dxgi, 3, misc, array, 0) + data
|
||||
|
||||
|
||||
def dds_fourcc(w, h, fourcc, data, mips=0, caps2=0):
|
||||
pf = struct.pack("<2I", 32, 4) + fourcc + b"\0" * 20
|
||||
return dds_header(w, h, pf, mips=mips, caps2=caps2) + data
|
||||
|
||||
|
||||
def rgba_from(mode, raw):
|
||||
"""Pillow raw bytes of mode L / RGB / RGBA -> rgba8 bytes"""
|
||||
if mode == "RGBA":
|
||||
return raw
|
||||
out = bytearray()
|
||||
if mode == "L":
|
||||
for v in raw:
|
||||
out += bytes((v, v, v, 255))
|
||||
elif mode == "RGB":
|
||||
for i in range(0, len(raw), 3):
|
||||
out += raw[i:i + 3] + b"\xff"
|
||||
else:
|
||||
raise ValueError(mode)
|
||||
return bytes(out)
|
||||
Reference in new issue
Block a user