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.
This commit is contained in:
4DBug committed 2026-10-31 14:05:10 -05:00
1 parent de4f14a343
commit f17a54395d
10 files changed
+1761

No files matched your search

+498
View File
@@ -0,0 +1,498 @@
--!strict
--!native
--!optimize 2
-- codecs/image/Bcn/Decode: the BC1..BC7 block decoders (S3TC / DXT1-5, RGTC, BPTC). Ported from Pillow's
-- src/libImaging/BcnDecode.c (CC0, licenses/pillow-bcn-CC0.txt) and changed to follow the D3D11 functional spec where
-- Pillow does not: BC6H interpolation rounds ((a * (64 - w) + b * w + 32) >> 6), BC6H keeps its float values (no 8-bit
-- clamp), BC7's reserved mode 8 and BC6H's reserved modes decode to transparent / zero, BC4 / BC5 SNORM interpolate
-- the signed values (-128 reads as -127). Each decoder writes one 4x4 block into a 64-byte (rgba8) or 256-byte
-- (rgbaf32) scratch block; the caller clips it into the image. Never throws: the caller checks the source size.
local Half = require("@formats/codecs/image/Half")
local Decode = {}
local readu8, readu16, readu32, writeu8, writeu32, writef32 = buffer.readu8, buffer.readu16, buffer.readu32, buffer.writeu8, buffer.writeu32, buffer.writef32
local band, bor, rshift, lshift = bit32.band, bit32.bor, bit32.rshift, bit32.lshift
local HALF = Half.TABLE
-- ------------------------------------------------------------------------------------------------ BC1 / BC2 / BC3
local pal = buffer.create(16) -- 4 rgba8 colours
local function c565(c: number): (number, number, number)
local r = rshift(band(c, 0xF800), 8)
local g = rshift(band(c, 0x7E0), 3)
local b = lshift(band(c, 0x1F), 3)
return bor(r, rshift(r, 5)), bor(g, rshift(g, 6)), bor(b, rshift(b, 5))
end
-- the BC1 colour block at p into blk (64 bytes); fourColour = BC2 / BC3 (always the 4-colour palette)
local function colour(src: buffer, p: number, blk: buffer, fourColour: boolean)
local c0, c1 = readu16(src, p), readu16(src, p + 2)
local r0, g0, b0 = c565(c0)
local r1, g1, b1 = c565(c1)
writeu32(pal, 0, r0 + g0 * 256 + b0 * 65536 + 0xFF000000)
writeu32(pal, 4, r1 + g1 * 256 + b1 * 65536 + 0xFF000000)
if c0 > c1 or fourColour then
writeu32(pal, 8, (2 * r0 + r1) // 3 + ((2 * g0 + g1) // 3) * 256 + ((2 * b0 + b1) // 3) * 65536 + 0xFF000000)
writeu32(pal, 12, (r0 + 2 * r1) // 3 + ((g0 + 2 * g1) // 3) * 256 + ((b0 + 2 * b1) // 3) * 65536 + 0xFF000000)
else
writeu32(pal, 8, (r0 + r1) // 2 + ((g0 + g1) // 2) * 256 + ((b0 + b1) // 2) * 65536 + 0xFF000000)
writeu32(pal, 12, 0)
end
local idx = readu32(src, p + 4)
for i = 0, 15 do
writeu32(blk, i * 4, readu32(pal, band(rshift(idx, i * 2), 3) * 4))
end
end
-- the 8 values of a BC3 alpha / BC4 block (unsigned) into vals
local vals = table.create(8, 0)
local function ramp8(a0: number, a1: number)
vals[1], vals[2] = a0, a1
if a0 > a1 then
for i = 1, 6 do
vals[i + 2] = ((7 - i) * a0 + i * a1) // 7
end
else
for i = 1, 4 do
vals[i + 2] = ((5 - i) * a0 + i * a1) // 5
end
vals[7], vals[8] = 0, 255
end
end
-- signed (SNORM) ramp, values -127..127 rounded to the nearest integer, biased by 128 into 1..255
local function ramp8s(u0: number, u1: number)
local s0: number = math.max(if u0 >= 128 then u0 - 256 else u0, -127)
local s1: number = math.max(if u1 >= 128 then u1 - 256 else u1, -127)
vals[1], vals[2] = s0 + 128, s1 + 128
if s0 > s1 then
for i = 1, 6 do
vals[i + 2] = math.floor(((7 - i) * s0 + i * s1) / 7 + 0.5) + 128
end
else
for i = 1, 4 do
vals[i + 2] = math.floor(((5 - i) * s0 + i * s1) / 5 + 0.5) + 128
end
vals[7], vals[8] = 1, 255
end
end
-- one 8-byte BC3 alpha / BC4 block at p into byte `channel` of every blk pixel (and the next ones when spread)
local function single(src: buffer, p: number, blk: buffer, channel: number, signed: boolean, spread: boolean)
if signed then
ramp8s(readu8(src, p), readu8(src, p + 1))
else
ramp8(readu8(src, p), readu8(src, p + 1))
end
local lo = readu8(src, p + 2) + readu8(src, p + 3) * 256 + readu8(src, p + 4) * 65536
local hi = readu8(src, p + 5) + readu8(src, p + 6) * 256 + readu8(src, p + 7) * 65536
for i = 0, 15 do
local sel = if i < 8 then band(rshift(lo, i * 3), 7) else band(rshift(hi, (i - 8) * 3), 7)
local v = vals[sel + 1]
local o = i * 4
if spread then
writeu32(blk, o, v + v * 256 + v * 65536 + 0xFF000000)
else
writeu8(blk, o + channel, v)
end
end
end
function Decode.bc1(src: buffer, p: number, blk: buffer)
colour(src, p, blk, false)
end
function Decode.bc2(src: buffer, p: number, blk: buffer)
colour(src, p + 8, blk, true)
for i = 0, 15 do
local a = band(rshift(readu8(src, p + rshift(i, 1)), band(i, 1) * 4), 15)
writeu8(blk, i * 4 + 3, a * 17)
end
end
function Decode.bc3(src: buffer, p: number, blk: buffer)
colour(src, p + 8, blk, true)
single(src, p, blk, 3, false, false)
end
function Decode.bc4(src: buffer, p: number, blk: buffer)
single(src, p, blk, 0, false, true)
end
function Decode.bc4s(src: buffer, p: number, blk: buffer)
single(src, p, blk, 0, true, true)
end
local function bc5(src: buffer, p: number, blk: buffer, signed: boolean)
for i = 0, 15 do
writeu32(blk, i * 4, 0xFF000000)
end
single(src, p, blk, 0, signed, false)
single(src, p + 8, blk, 1, signed, false)
end
function Decode.bc5(src: buffer, p: number, blk: buffer)
bc5(src, p, blk, false)
end
function Decode.bc5s(src: buffer, p: number, blk: buffer)
bc5(src, p, blk, true)
end
-- ------------------------------------------------------------------------------------------------ BPTC tables
-- partition tables (BC7 Table.P2 1 bit per pixel, Table.P3 2 bits per pixel) and the anchor indices
local SI2 = {
0xcccc, 0x8888, 0xeeee, 0xecc8, 0xc880, 0xfeec, 0xfec8, 0xec80, 0xc800, 0xffec, 0xfe80, 0xe800, 0xffe8, 0xff00, 0xfff0, 0xf000,
0xf710, 0x008e, 0x7100, 0x08ce, 0x008c, 0x7310, 0x3100, 0x8cce, 0x088c, 0x3110, 0x6666, 0x366c, 0x17e8, 0x0ff0, 0x718e, 0x399c,
0xaaaa, 0xf0f0, 0x5a5a, 0x33cc, 0x3c3c, 0x55aa, 0x9696, 0xa55a, 0x73ce, 0x13c8, 0x324c, 0x3bdc, 0x6996, 0xc33c, 0x9966, 0x0660,
0x0272, 0x04e4, 0x4e40, 0x2720, 0xc936, 0x936c, 0x39c6, 0x639c, 0x9336, 0x9cc6, 0x817e, 0xe718, 0xccf0, 0x0fcc, 0x7744, 0xee22,
}
local SI3 = {
0xaa685050, 0x6a5a5040, 0x5a5a4200, 0x5450a0a8, 0xa5a50000, 0xa0a05050, 0x5555a0a0, 0x5a5a5050, 0xaa550000, 0xaa555500,
0xaaaa5500, 0x90909090, 0x94949494, 0xa4a4a4a4, 0xa9a59450, 0x2a0a4250, 0xa5945040, 0x0a425054, 0xa5a5a500, 0x55a0a0a0,
0xa8a85454, 0x6a6a4040, 0xa4a45000, 0x1a1a0500, 0x0050a4a4, 0xaaa59090, 0x14696914, 0x69691400, 0xa08585a0, 0xaa821414,
0x50a4a450, 0x6a5a0200, 0xa9a58000, 0x5090a0a8, 0xa8a09050, 0x24242424, 0x00aa5500, 0x24924924, 0x24499224, 0x50a50a50,
0x500aa550, 0xaaaa4444, 0x66660000, 0xa5a0a5a0, 0x50a050a0, 0x69286928, 0x44aaaa44, 0x66666600, 0xaa444444, 0x54a854a8,
0x95809580, 0x96969600, 0xa85454a8, 0x80959580, 0xaa141414, 0x96960000, 0xaaaa1414, 0xa05050a0, 0xa0a5a5a0, 0x96000000,
0x40804080, 0xa9a8a9a8, 0xaaaaaa44, 0x2a4a5254,
}
local AI0 = {
15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 2, 8, 2, 2, 8, 8, 15, 2, 8, 2, 2, 8, 8, 2, 2,
15, 15, 6, 8, 2, 8, 15, 15, 2, 8, 2, 2, 2, 15, 15, 6, 6, 2, 6, 8, 15, 15, 2, 2, 15, 15, 15, 15, 15, 2, 2, 15,
}
local AI1 = {
3, 3, 15, 15, 8, 3, 15, 15, 8, 8, 6, 6, 6, 5, 3, 3, 3, 3, 8, 15, 3, 3, 6, 10, 5, 8, 8, 6, 8, 5, 15, 15,
8, 15, 3, 5, 6, 10, 8, 15, 15, 3, 15, 5, 15, 15, 15, 15, 3, 15, 5, 5, 5, 8, 5, 10, 5, 10, 8, 13, 15, 12, 3, 3,
}
local AI2 = {
15, 8, 8, 3, 15, 15, 3, 8, 15, 15, 15, 15, 15, 15, 15, 8, 15, 8, 15, 3, 15, 8, 15, 8, 3, 15, 6, 10, 15, 15, 10, 8,
15, 3, 15, 10, 10, 8, 9, 10, 6, 15, 8, 15, 3, 6, 6, 8, 15, 3, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 3, 15, 15, 8,
}
local W2 = { [0] = 0, 21, 43, 64 }
local W3 = { [0] = 0, 9, 18, 27, 37, 46, 55, 64 }
local W4 = { [0] = 0, 4, 9, 13, 17, 21, 26, 30, 34, 38, 43, 47, 51, 55, 60, 64 }
local WEIGHTS = { [2] = W2, [3] = W3, [4] = W4 }
Decode.SI2, Decode.SI3, Decode.AI0, Decode.AI1, Decode.AI2, Decode.WEIGHTS = SI2, SI3, AI0, AI1, AI2, WEIGHTS
local function subset(ns: number, part: number, i: number): number
if ns == 2 then
return band(rshift(SI2[part + 1], i), 1)
elseif ns == 3 then
return band(rshift(SI3[part + 1], 2 * i), 3)
end
return 0
end
-- the 128-bit block as four u32 words: bits(at, n) for n <= 16
local w0, w1, w2, w3 = 0, 0, 0, 0
local function load(src: buffer, p: number)
w0, w1, w2, w3 = readu32(src, p), readu32(src, p + 4), readu32(src, p + 8), readu32(src, p + 12)
end
local function word(i: number): number
return if i == 0 then w0 elseif i == 1 then w1 elseif i == 2 then w2 else w3
end
local function bits(at: number, n: number): number
if n == 0 then
return 0
end
local wi, sh = rshift(at, 5), band(at, 31)
local v = rshift(word(wi), sh)
if sh + n > 32 and wi < 3 then
v = bor(v, lshift(word(wi + 1), 32 - sh))
end
return band(v, lshift(1, n) - 1)
end
-- ------------------------------------------------------------------------------------------------ BC7
-- ns, pb, rb, isb, cb, ab, epb, spb, ib, ib2
local MODES7 = {
{ 3, 4, 0, 0, 4, 0, 1, 0, 3, 0 },
{ 2, 6, 0, 0, 6, 0, 0, 1, 3, 0 },
{ 3, 6, 0, 0, 5, 0, 0, 0, 2, 0 },
{ 2, 6, 0, 0, 7, 0, 1, 0, 2, 0 },
{ 1, 0, 2, 1, 5, 6, 0, 0, 2, 3 },
{ 1, 0, 2, 0, 7, 8, 0, 0, 2, 2 },
{ 1, 0, 0, 0, 7, 7, 1, 0, 4, 0 },
{ 2, 6, 0, 0, 5, 5, 1, 0, 2, 0 },
}
local er, eg, eb, ea = table.create(6, 0), table.create(6, 0), table.create(6, 0), table.create(6, 0)
local function expand(v: number, n: number): number
v = band(lshift(v, 8 - n), 255)
return bor(v, rshift(v, n))
end
function Decode.bc7(src: buffer, p: number, blk: buffer)
local m0 = readu8(src, p)
if m0 == 0 then
buffer.fill(blk, 0, 0, 64)
return
end
load(src, p)
local mode = 0
while band(m0, lshift(1, mode)) == 0 do
mode += 1
end
local info = MODES7[mode + 1]
local ns, pb, rb, isb, cb, ab, epb, spb, ib, ib2 = info[1], info[2], info[3], info[4], info[5], info[6], info[7], info[8], info[9], info[10]
local bit = mode + 1
local part = bits(bit, pb)
bit += pb
local rot = bits(bit, rb)
bit += rb
local sel = bits(bit, isb)
bit += isb
local nep = ns * 2
for i = 1, nep do
er[i] = bits(bit, cb)
bit += cb
end
for i = 1, nep do
eg[i] = bits(bit, cb)
bit += cb
end
for i = 1, nep do
eb[i] = bits(bit, cb)
bit += cb
end
for i = 1, nep do
if ab > 0 then
ea[i] = bits(bit, ab)
bit += ab
else
ea[i] = 255
end
end
if epb == 1 then
cb += 1
if ab > 0 then
ab += 1
end
for i = 1, nep do
local v = bits(bit, 1)
bit += 1
er[i], eg[i], eb[i] = er[i] * 2 + v, eg[i] * 2 + v, eb[i] * 2 + v
if ab > 0 then
ea[i] = ea[i] * 2 + v
end
end
end
if spb == 1 then
cb += 1
if ab > 0 then
ab += 1
end
for i = 1, nep, 2 do
local v = bits(bit, 1)
bit += 1
for j = i, i + 1 do
er[j], eg[j], eb[j] = er[j] * 2 + v, eg[j] * 2 + v, eb[j] * 2 + v
if ab > 0 then
ea[j] = ea[j] * 2 + v
end
end
end
end
for i = 1, nep do
er[i], eg[i], eb[i] = expand(er[i], cb), expand(eg[i], cb), expand(eb[i], cb)
if ab > 0 then
ea[i] = expand(ea[i], ab)
end
end
local cw = WEIGHTS[ib]
local aw = if ab > 0 and ib2 > 0 then WEIGHTS[ib2] else cw
local cbit = bit
local abit = cbit + 16 * ib - ns
local a1 = if ns == 2 then AI0[part + 1] elseif ns == 3 then AI1[part + 1] else -1
local a2 = if ns == 3 then AI2[part + 1] else -1
for i = 0, 15 do
local s = subset(ns, part, i) * 2 + 1
local n = ib
if i == 0 or i == a1 or i == a2 then
n -= 1
end
local i0 = bits(cbit, n)
cbit += n
local wc, wa
if ab > 0 and ib2 > 0 then
local n2 = if i == 0 then ib2 - 1 else ib2
local i1 = bits(abit, n2)
abit += n2
if sel == 1 then
wc, wa = aw[i1], cw[i0]
else
wc, wa = cw[i0], aw[i1]
end
else
wc, wa = cw[i0], cw[i0]
end
local tc, ta = 64 - wc, 64 - wa
local r = rshift(tc * er[s] + wc * er[s + 1] + 32, 6)
local g = rshift(tc * eg[s] + wc * eg[s + 1] + 32, 6)
local b = rshift(tc * eb[s] + wc * eb[s + 1] + 32, 6)
local a = rshift(ta * ea[s] + wa * ea[s + 1] + 32, 6)
if rot == 1 then
r, a = a, r
elseif rot == 2 then
g, a = a, g
elseif rot == 3 then
b, a = a, b
end
writeu32(blk, i * 4, r + g * 256 + b * 65536 + a * 16777216)
end
end
-- ------------------------------------------------------------------------------------------------ BC6H
-- ns, transformed, pb, epb, rb, gb, bb
local MODES6 = {
{ 2, 1, 5, 10, 5, 5, 5 },
{ 2, 1, 5, 7, 6, 6, 6 },
{ 2, 1, 5, 11, 5, 4, 4 },
{ 2, 1, 5, 11, 4, 5, 4 },
{ 2, 1, 5, 11, 4, 4, 5 },
{ 2, 1, 5, 9, 5, 5, 5 },
{ 2, 1, 5, 8, 6, 5, 5 },
{ 2, 1, 5, 8, 5, 6, 5 },
{ 2, 1, 5, 8, 5, 5, 6 },
{ 2, 0, 5, 6, 6, 6, 6 },
{ 1, 0, 0, 10, 10, 10, 10 },
{ 1, 1, 0, 11, 9, 9, 9 },
{ 1, 1, 0, 12, 8, 8, 8 },
{ 1, 1, 0, 16, 4, 4, 4 },
}
-- Table.F: for each endpoint bit in stream order, endpoint * 16 + bit (endpoints r0 g0 b0 r1 g1 b1 r2 ... b3)
local PACK6 = {
{ 116, 132, 180, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 48, 49, 50, 51, 52, 164, 112, 113, 114, 115, 64, 65, 66, 67, 68, 176, 160, 161, 162, 163, 80, 81, 82, 83, 84, 177, 128, 129, 130, 131, 96, 97, 98, 99, 100, 178, 144, 145, 146, 147, 148, 179 },
{ 117, 164, 165, 0, 1, 2, 3, 4, 5, 6, 176, 177, 132, 16, 17, 18, 19, 20, 21, 22, 133, 178, 116, 32, 33, 34, 35, 36, 37, 38, 179, 181, 180, 48, 49, 50, 51, 52, 53, 112, 113, 114, 115, 64, 65, 66, 67, 68, 69, 160, 161, 162, 163, 80, 81, 82, 83, 84, 85, 128, 129, 130, 131, 96, 97, 98, 99, 100, 101, 144, 145, 146, 147, 148, 149 },
{ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 48, 49, 50, 51, 52, 10, 112, 113, 114, 115, 64, 65, 66, 67, 26, 176, 160, 161, 162, 163, 80, 81, 82, 83, 42, 177, 128, 129, 130, 131, 96, 97, 98, 99, 100, 178, 144, 145, 146, 147, 148, 179 },
{ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 48, 49, 50, 51, 10, 164, 112, 113, 114, 115, 64, 65, 66, 67, 68, 26, 160, 161, 162, 163, 80, 81, 82, 83, 42, 177, 128, 129, 130, 131, 96, 97, 98, 99, 176, 178, 144, 145, 146, 147, 116, 179 },
{ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 48, 49, 50, 51, 10, 132, 112, 113, 114, 115, 64, 65, 66, 67, 26, 176, 160, 161, 162, 163, 80, 81, 82, 83, 84, 42, 128, 129, 130, 131, 96, 97, 98, 99, 177, 178, 144, 145, 146, 147, 180, 179 },
{ 0, 1, 2, 3, 4, 5, 6, 7, 8, 132, 16, 17, 18, 19, 20, 21, 22, 23, 24, 116, 32, 33, 34, 35, 36, 37, 38, 39, 40, 180, 48, 49, 50, 51, 52, 164, 112, 113, 114, 115, 64, 65, 66, 67, 68, 176, 160, 161, 162, 163, 80, 81, 82, 83, 84, 177, 128, 129, 130, 131, 96, 97, 98, 99, 100, 178, 144, 145, 146, 147, 148, 179 },
{ 0, 1, 2, 3, 4, 5, 6, 7, 164, 132, 16, 17, 18, 19, 20, 21, 22, 23, 178, 116, 32, 33, 34, 35, 36, 37, 38, 39, 179, 180, 48, 49, 50, 51, 52, 53, 112, 113, 114, 115, 64, 65, 66, 67, 68, 176, 160, 161, 162, 163, 80, 81, 82, 83, 84, 177, 128, 129, 130, 131, 96, 97, 98, 99, 100, 101, 144, 145, 146, 147, 148, 149 },
{ 0, 1, 2, 3, 4, 5, 6, 7, 176, 132, 16, 17, 18, 19, 20, 21, 22, 23, 117, 116, 32, 33, 34, 35, 36, 37, 38, 39, 165, 180, 48, 49, 50, 51, 52, 164, 112, 113, 114, 115, 64, 65, 66, 67, 68, 69, 160, 161, 162, 163, 80, 81, 82, 83, 84, 177, 128, 129, 130, 131, 96, 97, 98, 99, 100, 178, 144, 145, 146, 147, 148, 179 },
{ 0, 1, 2, 3, 4, 5, 6, 7, 177, 132, 16, 17, 18, 19, 20, 21, 22, 23, 133, 116, 32, 33, 34, 35, 36, 37, 38, 39, 181, 180, 48, 49, 50, 51, 52, 164, 112, 113, 114, 115, 64, 65, 66, 67, 68, 176, 160, 161, 162, 163, 80, 81, 82, 83, 84, 85, 128, 129, 130, 131, 96, 97, 98, 99, 100, 178, 144, 145, 146, 147, 148, 179 },
{ 0, 1, 2, 3, 4, 5, 164, 176, 177, 132, 16, 17, 18, 19, 20, 21, 117, 133, 178, 116, 32, 33, 34, 35, 36, 37, 165, 179, 181, 180, 48, 49, 50, 51, 52, 53, 112, 113, 114, 115, 64, 65, 66, 67, 68, 69, 160, 161, 162, 163, 80, 81, 82, 83, 84, 85, 128, 129, 130, 131, 96, 97, 98, 99, 100, 101, 144, 145, 146, 147, 148, 149 },
{ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89 },
{ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 48, 49, 50, 51, 52, 53, 54, 55, 56, 10, 64, 65, 66, 67, 68, 69, 70, 71, 72, 26, 80, 81, 82, 83, 84, 85, 86, 87, 88, 42 },
{ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 48, 49, 50, 51, 52, 53, 54, 55, 11, 10, 64, 65, 66, 67, 68, 69, 70, 71, 27, 26, 80, 81, 82, 83, 84, 85, 86, 87, 43, 42 },
{ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 48, 49, 50, 51, 15, 14, 13, 12, 11, 10, 64, 65, 66, 67, 31, 30, 29, 28, 27, 26, 80, 81, 82, 83, 47, 46, 45, 44, 43, 42 },
}
local ep = table.create(12, 0)
local uq = table.create(12, 0)
local function signExtend(v: number, n: number): number
v = band(v, lshift(1, n) - 1)
return if v >= lshift(1, n - 1) then v - lshift(1, n) else v
end
local function unquantize(x: number, n: number, signed: boolean): number
if not signed then
if n >= 15 or x == 0 then
return x
elseif x == lshift(1, n) - 1 then
return 0xFFFF
end
return rshift(x * 32768 + 0x4000, n - 1)
end
if n >= 16 then
return x
end
local neg = x < 0
if neg then
x = -x
end
if x ~= 0 then
if x >= lshift(1, n - 1) - 1 then
x = 0x7FFF
else
x = rshift(x * 32768 + 0x4000, n - 1)
end
end
return if neg then -x else x
end
local function finish(v: number, signed: boolean): number
if signed then
if v < 0 then
return -HALF[(-v * 31) // 32]
end
return HALF[(v * 31) // 32]
end
return HALF[(v * 31) // 64]
end
-- one BC6H block into blk as rgbaf32 (256 bytes, alpha 1)
function Decode.bc6h(src: buffer, p: number, blk: buffer, signed: boolean)
load(src, p)
local m = band(w0, 31)
local mode, bit, epbits, ib
local low = band(m, 3)
if low < 2 then
mode, bit, epbits, ib = low, 2, 75, 3
elseif low == 2 then
mode, bit, epbits, ib = 2 + rshift(m, 2), 5, 72, 3
else
mode, bit, epbits, ib = 10 + rshift(m, 2), 5, 60, 4
end
if mode >= 14 then
for i = 0, 15 do
local o = i * 16
writef32(blk, o, 0)
writef32(blk, o + 4, 0)
writef32(blk, o + 8, 0)
writef32(blk, o + 12, 1)
end
return
end
local info = MODES6[mode + 1]
local ns, tr, pb, epb, rbits, gbits, bbits = info[1], info[2], info[3], info[4], info[5], info[6], info[7]
local nep = if ns == 2 then 12 else 6
for i = 1, 12 do
ep[i] = 0
end
local pack = PACK6[mode + 1]
for i = 1, epbits do
local d = pack[i]
local e = rshift(d, 4) + 1
ep[e] = bor(ep[e], lshift(bits(bit + i - 1, 1), band(d, 15)))
end
bit += epbits
local part = bits(bit, pb)
bit += pb
local mask = lshift(1, epb) - 1
if signed then
ep[1], ep[2], ep[3] = signExtend(ep[1], epb), signExtend(ep[2], epb), signExtend(ep[3], epb)
end
if signed or tr == 1 then
for i = 4, nep, 3 do
ep[i], ep[i + 1], ep[i + 2] = signExtend(ep[i], rbits), signExtend(ep[i + 1], gbits), signExtend(ep[i + 2], bbits)
end
end
if tr == 1 then
for i = 4, nep, 3 do
for c = 0, 2 do
local v = band(ep[i + c] + ep[1 + c], mask)
ep[i + c] = if signed then signExtend(v, epb) else v
end
end
end
for i = 1, nep do
uq[i] = unquantize(ep[i], epb, signed)
end
local w = WEIGHTS[ib]
local a1 = if ns == 2 then AI0[part + 1] else -1
for i = 0, 15 do
local s = subset(ns, part, i) * 6 + 1
local n = if i == 0 or i == a1 then ib - 1 else ib
local wt = w[bits(bit, n)]
bit += n
local t = 64 - wt
local o = i * 16
-- floor((a * t + b * w + 32) / 64): the products may be negative (signed) - // floors like >> would
writef32(blk, o, finish((uq[s] * t + uq[s + 3] * wt + 32) // 64, signed))
writef32(blk, o + 4, finish((uq[s + 1] * t + uq[s + 4] * wt + 32) // 64, signed))
writef32(blk, o + 8, finish((uq[s + 2] * t + uq[s + 5] * wt + 32) // 64, signed))
writef32(blk, o + 12, 1)
end
end
return Decode
+442
View File
@@ -0,0 +1,442 @@
--!strict
--!native
--!optimize 2
-- codecs/image/Bcn/Encode: BC1 / BC3 / BC4 / BC5 block encoders (new in formats-luau; the approach follows stb_dxt by
-- Fabian Giesen / Sean Barrett, public domain: the principal axis of the block's colours by power iteration, the
-- extreme pixels along it as endpoints, then least-squares refinement of the endpoints for the chosen indices, keeping
-- the best; single-colour blocks use exact endpoint tables). Indices are chosen against the palette exactly as
-- Bcn/Decode rebuilds it. BC4 / BC5 channels try both the 8-value and the 6-value (+ 0 / 255) ramps. Never throws.
local Encode = {}
local readu8, writeu8, writeu16, writeu32 = buffer.readu8, buffer.writeu8, buffer.writeu16, buffer.writeu32
local band, bor, rshift, lshift = bit32.band, bit32.bor, bit32.rshift, bit32.lshift
local function exp5(v: number): number
return bor(lshift(v, 3), rshift(v, 2))
end
local function exp6(v: number): number
return bor(lshift(v, 2), rshift(v, 4))
end
-- ------------------------------------------------------------------------------------------------ single colours
-- OM[k][v + 1] = e0 * 256 + e1 (quantized) whose colour-2 value (4-colour: (2 e0 + e1) // 3; 3-colour: (e0 + e1)
-- // 2) is closest to v; k = 1 / 2 for 5-bit (4 / 3 colour), 3 / 4 for 6-bit
local OM: { { number } }? = nil
local function singleTables(): { { number } }
local om = OM
if om then
return om
end
local t: { { number } } = {}
for k = 1, 4 do
local bitsN = if k <= 2 then 5 else 6
local maxq = lshift(1, bitsN) - 1
local exp: (number) -> number = if bitsN == 5 then exp5 else exp6
local best = table.create(256, 1e9)
local pick = table.create(256, 0)
for a = 0, maxq do
local ea = exp(a)
for b = 0, maxq do
local eb = exp(b)
local v: number = if k % 2 == 1 then (2 * ea + eb) // 3 else (ea + eb) // 2
-- prefer endpoints close to each other (less error if the colour shifts)
local e = math.abs(ea - eb) * 0.01
for target = math.max(0, v - 8), math.min(255, v + 8) do
local err = (target - v) ^ 2 + e
if err < best[target + 1] then
best[target + 1] = err
pick[target + 1] = a * 256 + b
end
end
end
end
t[k] = pick
end
OM = t
return t
end
-- ------------------------------------------------------------------------------------------------ BC1 colour
local R, G, B, A = table.create(16, 0), table.create(16, 0), table.create(16, 0), table.create(16, 0)
local USE = table.create(16, true)
local PR, PG, PB = table.create(4, 0), table.create(4, 0), table.create(4, 0)
local IDX = table.create(16, 0)
local BEST = table.create(16, 0)
local function palette(c0: number, c1: number, four: boolean)
local r0, g0, b0 = exp5(rshift(c0, 11)), exp6(band(rshift(c0, 5), 63)), exp5(band(c0, 31))
local r1, g1, b1 = exp5(rshift(c1, 11)), exp6(band(rshift(c1, 5), 63)), exp5(band(c1, 31))
PR[1], PG[1], PB[1] = r0, g0, b0
PR[2], PG[2], PB[2] = r1, g1, b1
if four then
PR[3], PG[3], PB[3] = (2 * r0 + r1) // 3, (2 * g0 + g1) // 3, (2 * b0 + b1) // 3
PR[4], PG[4], PB[4] = (r0 + 2 * r1) // 3, (g0 + 2 * g1) // 3, (b0 + 2 * b1) // 3
else
PR[3], PG[3], PB[3] = (r0 + r1) // 2, (g0 + g1) // 2, (b0 + b1) // 2
PR[4], PG[4], PB[4] = 0, 0, 0
end
end
-- indices for the palette into IDX (0-based); returns the squared error of the used pixels
local function assign(four: boolean): number
local n = if four then 4 else 3
local total = 0
for i = 1, 16 do
if USE[i] then
local r, g, b = R[i], G[i], B[i]
local bi, be = 0, math.huge
for k = 1, n do
local dr, dg, db = r - PR[k], g - PG[k], b - PB[k]
local e = dr * dr + dg * dg + db * db
if e < be then
be, bi = e, k - 1
end
end
IDX[i] = bi
total += be
else
IDX[i] = 3
end
end
return total
end
local function q565(r: number, g: number, b: number): number
local qr = math.clamp(math.floor(r * 31 / 255 + 0.5), 0, 31)
local qg = math.clamp(math.floor(g * 63 / 255 + 0.5), 0, 63)
local qb = math.clamp(math.floor(b * 31 / 255 + 0.5), 0, 31)
return qr * 2048 + qg * 32 + qb
end
-- the endpoint weights of index k (the e0 share)
local W4 = { 1, 0, 2 / 3, 1 / 3 }
local W3 = { 1, 0, 0.5, 0 }
-- least squares endpoints for the current IDX: c0, c1 or nil when degenerate
local function refine(four: boolean): (number?, number?)
local W = if four then W4 else W3
local aa, bb, ab = 0, 0, 0
local ar, ag, ab_, br, bg, bb_ = 0, 0, 0, 0, 0, 0
for i = 1, 16 do
if USE[i] then
local w = W[IDX[i] + 1]
local v = 1 - w
aa += w * w
bb += v * v
ab += w * v
ar += w * R[i]
ag += w * G[i]
ab_ += w * B[i]
br += v * R[i]
bg += v * G[i]
bb_ += v * B[i]
end
end
local det = aa * bb - ab * ab
if math.abs(det) < 1e-9 then
return nil, nil
end
local f = 1 / det
local r0, g0, b0 = (ar * bb - br * ab) * f, (ag * bb - bg * ab) * f, (ab_ * bb - bb_ * ab) * f
local r1, g1, b1 = (br * aa - ar * ab) * f, (bg * aa - ag * ab) * f, (bb_ * aa - ab_ * ab) * f
return q565(r0, g0, b0), q565(r1, g1, b1)
end
-- encodes R / G / B (USE marks the pixels that count) as a BC1 colour block at out[o]; four = 4-colour mode
-- (opaque), else the 3-colour mode with index 3 for the unused (transparent) pixels
local function colourBlock(out: buffer, o: number, four: boolean)
local n, mr, mg, mb = 0, 0, 0, 0
for i = 1, 16 do
if USE[i] then
n += 1
mr += R[i]
mg += G[i]
mb += B[i]
end
end
if n == 0 then
writeu16(out, o, 0)
writeu16(out, o + 2, 0)
writeu32(out, o + 4, 0xFFFFFFFF)
return
end
mr, mg, mb = mr / n, mg / n, mb / n
local crr, cgg, cbb, crg, crb, cgb = 0, 0, 0, 0, 0, 0
local same = true
local fr, fg, fb = -1, -1, -1
for i = 1, 16 do
if USE[i] then
if fr < 0 then
fr, fg, fb = R[i], G[i], B[i]
elseif R[i] ~= fr or G[i] ~= fg or B[i] ~= fb then
same = false
end
local dr, dg, db = R[i] - mr, G[i] - mg, B[i] - mb
crr += dr * dr
cgg += dg * dg
cbb += db * db
crg += dr * dg
crb += dr * db
cgb += dg * db
end
end
local c0, c1
if same then
local t = singleTables()
local k = if four then 0 else 1
local pr, pg, pb = t[1 + k][fr + 1], t[3 + k][fg + 1], t[1 + k][fb + 1]
c0 = rshift(pr, 8) * 2048 + rshift(pg, 8) * 32 + rshift(pb, 8)
c1 = band(pr, 255) * 2048 + band(pg, 255) * 32 + band(pb, 255)
local i2 = 2
if c0 == c1 then
i2 = 0
end
-- index 2 everywhere (or 0 for an exact endpoint); order fixed below
for i = 1, 16 do
IDX[i] = if USE[i] then i2 else 3
end
else
-- principal axis
local vr, vg, vb = 1, 1, 1
for _ = 1, 8 do
local nr = crr * vr + crg * vg + crb * vb
local ng = crg * vr + cgg * vg + cgb * vb
local nb = crb * vr + cgb * vg + cbb * vb
local m = math.max(math.abs(nr), math.abs(ng), math.abs(nb))
if m < 1e-9 then
break
end
vr, vg, vb = nr / m, ng / m, nb / m
end
local lo, hi, li, hi_ = math.huge, -math.huge, 1, 1
for i = 1, 16 do
if USE[i] then
local d = R[i] * vr + G[i] * vg + B[i] * vb
if d < lo then
lo, li = d, i
end
if d > hi then
hi, hi_ = d, i
end
end
end
c0 = q565(R[hi_], G[hi_], B[hi_])
c1 = q565(R[li], G[li], B[li])
palette(c0, c1, four)
local err = assign(four)
for i = 1, 16 do
BEST[i] = IDX[i]
end
for _ = 1, 2 do
local n0, n1 = refine(four)
if not n0 or not n1 then
break
end
palette(n0, n1, four)
local e2 = assign(four)
if e2 < err then
err, c0, c1 = e2, n0, n1
for i = 1, 16 do
BEST[i] = IDX[i]
end
else
break
end
end
for i = 1, 16 do
IDX[i] = BEST[i]
end
end
-- the mode is set by the endpoint order: 4-colour c0 > c1, 3-colour c0 <= c1
if four then
if c0 < c1 then
c0, c1 = c1, c0
for i = 1, 16 do
local v = IDX[i]
IDX[i] = if v == 0 then 1 elseif v == 1 then 0 elseif v == 2 then 3 else 2
end
elseif c0 == c1 then
for i = 1, 16 do
IDX[i] = 0
end
end
elseif c0 > c1 then
c0, c1 = c1, c0
for i = 1, 16 do
local v = IDX[i]
IDX[i] = if v == 0 then 1 elseif v == 1 then 0 else v
end
end
writeu16(out, o, c0)
writeu16(out, o + 2, c1)
local bitsv = 0
for i = 16, 1, -1 do
bitsv = bitsv * 4 + IDX[i]
end
writeu32(out, o + 4, bitsv)
end
-- ------------------------------------------------------------------------------------------------ BC4 channel
local V = table.create(16, 0)
local RAMP = table.create(8, 0)
local SEL = table.create(16, 0)
local SELB = table.create(16, 0)
local function ramp(a0: number, a1: number)
RAMP[1], RAMP[2] = a0, a1
if a0 > a1 then
for i = 1, 6 do
RAMP[i + 2] = ((7 - i) * a0 + i * a1) // 7
end
else
for i = 1, 4 do
RAMP[i + 2] = ((5 - i) * a0 + i * a1) // 5
end
RAMP[7], RAMP[8] = 0, 255
end
end
local function pick(): number
local total = 0
for i = 1, 16 do
local v = V[i]
local bi, be = 0, math.huge
for k = 1, 8 do
local d = v - RAMP[k]
local e = d * d
if e < be then
be, bi = e, k - 1
end
end
SEL[i] = bi
total += be
end
return total
end
-- encodes V[1..16] (0..255) as an 8-byte BC4 block at out[o]
local function channelBlock(out: buffer, o: number)
local lo, hi = 255, 0
local lo2, hi2 = 255, 0
for i = 1, 16 do
local v = V[i]
lo, hi = math.min(lo, v), math.max(hi, v)
if v > 0 and v < 255 then
lo2, hi2 = math.min(lo2, v), math.max(hi2, v)
end
end
local a0, a1
if lo == hi then
a0, a1 = lo, lo
ramp(a0, a1)
pick()
else
-- 8-value ramp, then a few nudged endpoints
local bestErr, b0, b1 = math.huge, hi, lo
for d0 = 0, 2 do
for d1 = 0, 2 do
local x0, x1 = hi - d0, lo + d1
if x0 > x1 then
ramp(x0, x1)
local e = pick()
if e < bestErr then
bestErr, b0, b1 = e, x0, x1
for i = 1, 16 do
SELB[i] = SEL[i]
end
end
end
end
end
-- 6-value ramp with the 0 / 255 extremes
if lo2 > hi2 then
lo2, hi2 = lo, lo
end
ramp(lo2, hi2)
local e = pick()
if e < bestErr then
bestErr, b0, b1 = e, lo2, hi2
for i = 1, 16 do
SELB[i] = SEL[i]
end
end
a0, a1 = b0, b1
for i = 1, 16 do
SEL[i] = SELB[i]
end
end
writeu8(out, o, a0)
writeu8(out, o + 1, a1)
local lo24, hi24 = 0, 0
for i = 8, 1, -1 do
lo24 = lo24 * 8 + SEL[i]
hi24 = hi24 * 8 + SEL[i + 8]
end
writeu8(out, o + 2, band(lo24, 255))
writeu8(out, o + 3, band(rshift(lo24, 8), 255))
writeu8(out, o + 4, rshift(lo24, 16))
writeu8(out, o + 5, band(hi24, 255))
writeu8(out, o + 6, band(rshift(hi24, 8), 255))
writeu8(out, o + 7, rshift(hi24, 16))
end
-- ------------------------------------------------------------------------------------------------ whole images
-- the 4x4 block at bx, by of an rgba8 image into R / G / B / A (edge pixels repeat)
local function gather(px: buffer, w: number, h: number, bx: number, by: number)
for y = 0, 3 do
local sy = math.min(by * 4 + y, h - 1)
for x = 0, 3 do
local sx = math.min(bx * 4 + x, w - 1)
local s = (sy * w + sx) * 4
local i = y * 4 + x + 1
R[i], G[i], B[i], A[i] = readu8(px, s), readu8(px, s + 1), readu8(px, s + 2), readu8(px, s + 3)
end
end
end
-- encode(kind, rgba8 pixels, w, h, alphaThreshold) -> the block data (kind = bc1 / bc3 / bc4 / bc5)
function Encode.run(kind: string, px: buffer, w: number, h: number, threshold: number): buffer
local bw, bh = (w + 3) // 4, (h + 3) // 4
local bytes = if kind == "bc1" or kind == "bc4" then 8 else 16
local out = buffer.create(bw * bh * bytes)
local o = 0
for by = 0, bh - 1 do
for bx = 0, bw - 1 do
gather(px, w, h, bx, by)
if kind == "bc1" then
local any = false
for i = 1, 16 do
local u = A[i] >= threshold
USE[i] = u
any = any or not u
end
colourBlock(out, o, not any)
elseif kind == "bc3" then
for i = 1, 16 do
USE[i] = true
V[i] = A[i]
end
channelBlock(out, o)
colourBlock(out, o + 8, true)
elseif kind == "bc4" then
for i = 1, 16 do
V[i] = R[i]
end
channelBlock(out, o)
else
for i = 1, 16 do
V[i] = R[i]
end
channelBlock(out, o)
for i = 1, 16 do
V[i] = G[i]
end
channelBlock(out, o + 8)
end
o += bytes
end
end
return out
end
return Encode
+121
View File
@@ -0,0 +1,121 @@
--!strict
--!native
--!optimize 2
-- codecs/image/Bcn: the BCn block-compressed texture codecs, shared by the texture formats (DDS, KTX, VTF, BLP, ...):
-- decode of BC1 (DXT1, with the 3-colour + transparent mode), BC2 (DXT3), BC3 (DXT5), BC4 / BC5 (RGTC / ATI1 / ATI2,
-- UNORM and SNORM), BC6H (UF16 / SF16, to rgbaf32) and BC7 (all 8 modes); encode of BC1 / BC3 / BC4 / BC5 (UNORM).
-- Decoders ported from Pillow's BcnDecode.c (CC0) with the D3D11 rules; encoders new (stb_dxt-style). Never throws.
--
-- local px, err = Bcn.decode("bc7", buf, offset, w, h) -- rgba8 pixels (rgbaf32 for bc6h / bc6hs)
-- local blocks = Bcn.encode("bc1", rgba8Pixels, w, h) -- the block data (row-major blocks)
-- Bcn.size("bc3", w, h) -- bytes of a w x h image (whole 4x4 blocks)
--
-- Channels: BC4 decodes to grey (r = g = b, opaque), BC5 to red + green (blue 0, opaque); SNORM values are biased by
-- 128 into rgba8 (-127 -> 1, 0 -> 128, 127 -> 255). BC1 / BC3 / BC5 encode reads R / G (BC4: R), BC1 alpha below
-- opts.alphaThreshold (default 128) selects the transparent 3-colour mode.
local Decode = require("@formats/codecs/image/Bcn/Decode")
local Encode = require("@formats/codecs/image/Bcn/Encode")
local Bcn = {}
-- kind -> bytes per 4x4 block
Bcn.BLOCK = table.freeze({ bc1 = 8, bc2 = 16, bc3 = 16, bc4 = 8, bc4s = 8, bc5 = 16, bc5s = 16, bc6h = 16, bc6hs = 16, bc7 = 16 }) :: { [string]: number }
-- kinds that decode to rgbaf32
Bcn.FLOAT = table.freeze({ bc6h = true, bc6hs = true }) :: { [string]: boolean }
local readu32, writeu32 = buffer.readu32, buffer.writeu32
local BLOCKS: { [string]: (buffer, number, buffer) -> () } = {
bc1 = Decode.bc1,
bc2 = Decode.bc2,
bc3 = Decode.bc3,
bc4 = Decode.bc4,
bc4s = Decode.bc4s,
bc5 = Decode.bc5,
bc5s = Decode.bc5s,
bc7 = Decode.bc7,
bc6h = function(src: buffer, p: number, blk: buffer)
Decode.bc6h(src, p, blk, false)
end,
bc6hs = function(src: buffer, p: number, blk: buffer)
Decode.bc6h(src, p, blk, true)
end,
}
-- size(kind, w, h) -> bytes of the block data (nil for an unknown kind)
function Bcn.size(kind: string, w: number, h: number): number?
local b = Bcn.BLOCK[kind]
if not b then
return nil
end
return ((w + 3) // 4) * ((h + 3) // 4) * b
end
-- decode(kind, src, offset, w, h, out?, outOffset?) -> pixels (rgba8, or rgbaf32 for BC6H) | nil, message.
-- out (optional) receives the pixels at outOffset (default 0) instead of a new buffer.
function Bcn.decode(kind: string, src: buffer, offset: number, w: number, h: number, out: buffer?, outOffset: number?): (buffer?, string?)
local fn = BLOCKS[kind]
if not fn then
return nil, "unsupported: unknown block format " .. tostring(kind)
end
if w < 1 or h < 1 or w % 1 ~= 0 or h % 1 ~= 0 then
return nil, "bad image size"
end
local need = Bcn.size(kind, w, h) :: number
if offset < 0 or offset + need > buffer.len(src) then
return nil, "truncated: block data runs past the end"
end
local float = Bcn.FLOAT[kind] == true
local bpp = if float then 16 else 4
local oo = outOffset or 0
local dst = out or buffer.create(w * h * bpp)
if oo + w * h * bpp > buffer.len(dst) then
return nil, "output buffer too small"
end
local bytes = Bcn.BLOCK[kind]
local blk = buffer.create(16 * bpp)
local bw, bh = (w + 3) // 4, (h + 3) // 4
local p = offset
local rowBytes = w * bpp
for by = 0, bh - 1 do
local rows = math.min(4, h - by * 4)
for bx = 0, bw - 1 do
fn(src, p, blk)
p += bytes
local cols = math.min(4, w - bx * 4)
local base = oo + (by * 4) * rowBytes + bx * 4 * bpp
if cols == 4 and not float then
for y = 0, rows - 1 do
local d, s = base + y * rowBytes, y * 16
writeu32(dst, d, readu32(blk, s))
writeu32(dst, d + 4, readu32(blk, s + 4))
writeu32(dst, d + 8, readu32(blk, s + 8))
writeu32(dst, d + 12, readu32(blk, s + 12))
end
else
for y = 0, rows - 1 do
buffer.copy(dst, base + y * rowBytes, blk, y * 4 * bpp, cols * bpp)
end
end
end
end
return dst, nil
end
export type EncodeOptions = {
alphaThreshold: number?, -- BC1: alpha below this is transparent (default 128; 0 = always opaque)
}
-- encode(kind, pixels, w, h, opts?) -> block data | nil, message. pixels: rgba8, w x h; kind = bc1 / bc3 / bc4 / bc5.
function Bcn.encode(kind: string, px: buffer, w: number, h: number, opts: EncodeOptions?): (buffer?, string?)
if kind ~= "bc1" and kind ~= "bc3" and kind ~= "bc4" and kind ~= "bc5" then
return nil, "unsupported: no encoder for " .. tostring(kind)
end
if type(w) ~= "number" or type(h) ~= "number" or w < 1 or h < 1 or w % 1 ~= 0 or h % 1 ~= 0 or buffer.len(px) < w * h * 4 then
return nil, "needs w x h rgba8 pixels"
end
local t = opts and opts.alphaThreshold or 128
return Encode.run(kind, px, w, h, t), nil
end
return Bcn
+74
View File
@@ -0,0 +1,74 @@
--!strict
--!native
--!optimize 2
-- codecs/image/Half: IEEE 754 binary16 ("half") <-> number, for the float texel formats (DDS / KTX R16F..., BC6H,
-- OpenEXR HALF channels). New in formats-luau. Never throws.
--
-- local f = Half.TABLE[h] -- the number of every half bit pattern 0..65535 (a 65536-entry table)
-- local f = Half.toFloat(h) -- the same, computed
-- local h = Half.fromFloat(f) -- round to nearest even; overflow -> infinity, NaN -> a quiet NaN
local Half = {}
local band, bor, rshift, lshift = bit32.band, bit32.bor, bit32.rshift, bit32.lshift
-- toFloat(h) -> number (h a 16-bit pattern)
local function toFloat(h: number): number
local s = if h >= 32768 then -1 else 1
local e = band(rshift(h, 10), 31)
local m = band(h, 1023)
if e == 0 then
return s * m * 2 ^ -24
elseif e == 31 then
return if m == 0 then s * math.huge else 0 / 0
end
return s * (1024 + m) * 2 ^ (e - 25)
end
Half.toFloat = toFloat
local TABLE: { [number]: number } = table.create(65536, 0)
for h = 0, 65535 do
TABLE[h] = toFloat(h)
end
Half.TABLE = TABLE
local scratch = buffer.create(4)
-- fromFloat(f) -> the nearest half bit pattern (ties to even)
function Half.fromFloat(f: number): number
if f ~= f then
return 0x7E00
end
buffer.writef32(scratch, 0, f)
local x = buffer.readu32(scratch, 0)
local sign = band(rshift(x, 16), 0x8000)
local e = band(rshift(x, 23), 0xFF)
local m = band(x, 0x7FFFFF)
if e == 255 then
return bor(sign, 0x7C00)
end
local ne = e - 112
if ne >= 31 then
return bor(sign, 0x7C00)
elseif ne <= 0 then
if ne < -10 then
return sign
end
m = bor(m, 0x800000)
local shift = 14 - ne
local hm = rshift(m, shift)
local rem = band(m, lshift(1, shift) - 1)
local halfway = lshift(1, shift - 1)
if rem > halfway or (rem == halfway and band(hm, 1) == 1) then
hm += 1
end
return sign + hm
end
local hm = rshift(m, 13)
local rem = band(m, 0x1FFF)
if rem > 0x1000 or (rem == 0x1000 and band(hm, 1) == 1) then
hm += 1 -- a carry out of the mantissa bumps the exponent (up to infinity), as it should
end
return sign + ne * 1024 + hm
end
return Half