Files
rosetta/src/Formats/codecs/image/Bcn/Encode.luau
T

443 lines
12 KiB
Luau

--!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, 1 do
for d1 = 0, 1 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