ws-tex: codecs/image/Etc (ETC1, ETC2 RGB/RGBA/RGB A1, EAC R11/RG11 decode)
Written from the Khronos Data Format Spec 1.3. Exact against Godot 4 (etcpak) for ETC1 / ETC2 in every mode, EAC unsigned within 1 (8-bit rounding); RGB A1 checked as punch-through rules over Godot's ETC2 RGB decode; signed EAC against the spec rule in the fixture generator.
This commit is contained in:
1 parent
f17a54395d
commit
be643a66c8
4 files changed
+575
No files matched your search
@@ -0,0 +1,315 @@
|
||||
--!strict
|
||||
--!native
|
||||
--!optimize 2
|
||||
-- codecs/image/Etc: the Ericsson / Khronos block texture codecs, decode only: ETC1, ETC2 RGB (with the T, H and planar
|
||||
-- modes), ETC2 RGBA (an EAC alpha block + ETC2 colour), ETC2 RGB A1 (punch-through alpha) and EAC R11 / RG11
|
||||
-- (unsigned and signed). Written from the Khronos Data Format Specification 1.3, section 21 (ETC1 / ETC2 / EAC); new
|
||||
-- in formats-luau. Shared by the texture formats (KTX, PVR, ...). Never throws: the caller gets a message instead.
|
||||
--
|
||||
-- local px, err = Etc.decode("etc2_rgba", buf, offset, w, h) -- rgba8 pixels
|
||||
-- local px16 = Etc.decode("eac_rg11", buf, offset, w, h, nil, 0, true) -- rgba16 (EAC keeps its 11 bits)
|
||||
-- Etc.size("etc1", w, h) -- bytes of the block data
|
||||
--
|
||||
-- Kinds: etc1, etc2_rgb, etc2_rgba, etc2_rgba1, eac_r11, eac_r11s, eac_rg11, eac_rg11s. R11 decodes to grey (r = g = b),
|
||||
-- RG11 to red + green (blue 0); signed values are biased (-1 -> 1, 0 -> 128, +1 -> 255; rgba16: 0 / 32768 / 65535).
|
||||
local Etc = {}
|
||||
|
||||
Etc.BLOCK = table.freeze({ etc1 = 8, etc2_rgb = 8, etc2_rgba = 16, etc2_rgba1 = 8, eac_r11 = 8, eac_r11s = 8, eac_rg11 = 16, eac_rg11s = 16 }) :: { [string]: number }
|
||||
|
||||
local readu8, readu32, writeu8, writeu16, writeu32 = buffer.readu8, buffer.readu32, buffer.writeu8, buffer.writeu16, buffer.writeu32
|
||||
local band, rshift, byteswap = bit32.band, bit32.rshift, bit32.byteswap
|
||||
|
||||
-- ------------------------------------------------------------------------------------------------ tables
|
||||
local MOD = { -- the ETC1 intensity modifiers per table: index (msb * 2 + lsb) -> a, b, -a, -b
|
||||
{ 2, 8, -2, -8 },
|
||||
{ 5, 17, -5, -17 },
|
||||
{ 9, 29, -9, -29 },
|
||||
{ 13, 42, -13, -42 },
|
||||
{ 18, 60, -18, -60 },
|
||||
{ 24, 80, -24, -80 },
|
||||
{ 33, 106, -33, -106 },
|
||||
{ 47, 183, -47, -183 },
|
||||
}
|
||||
local DIST = { 3, 6, 11, 16, 23, 32, 41, 64 } -- T / H mode distances
|
||||
local EAC = {
|
||||
{ -3, -6, -9, -15, 2, 5, 8, 14 },
|
||||
{ -3, -7, -10, -13, 2, 6, 9, 12 },
|
||||
{ -2, -5, -8, -13, 1, 4, 7, 12 },
|
||||
{ -2, -4, -6, -13, 1, 3, 5, 12 },
|
||||
{ -3, -6, -8, -12, 2, 5, 7, 11 },
|
||||
{ -3, -7, -9, -11, 2, 6, 8, 10 },
|
||||
{ -4, -7, -8, -11, 3, 6, 7, 10 },
|
||||
{ -3, -5, -8, -11, 2, 4, 7, 10 },
|
||||
{ -2, -6, -8, -10, 1, 5, 7, 9 },
|
||||
{ -2, -5, -8, -10, 1, 4, 7, 9 },
|
||||
{ -2, -4, -8, -10, 1, 3, 7, 9 },
|
||||
{ -2, -5, -7, -10, 1, 4, 6, 9 },
|
||||
{ -3, -4, -7, -10, 2, 3, 6, 9 },
|
||||
{ -1, -2, -3, -10, 0, 1, 2, 9 },
|
||||
{ -4, -6, -8, -9, 3, 5, 7, 8 },
|
||||
{ -3, -5, -7, -9, 2, 4, 6, 8 },
|
||||
}
|
||||
|
||||
local function clamp8(v: number): number
|
||||
return if v < 0 then 0 elseif v > 255 then 255 else v
|
||||
end
|
||||
local function rgb(r: number, g: number, b: number): number
|
||||
return clamp8(r) + clamp8(g) * 256 + clamp8(b) * 65536 + 0xFF000000
|
||||
end
|
||||
|
||||
-- ------------------------------------------------------------------------------------------------ colour blocks
|
||||
-- blk: 16 u32 rgba8 pixels in raster order (y * 4 + x)
|
||||
local P = table.create(4, 0) -- T / H paint colours (rgba8 u32)
|
||||
|
||||
local function be32(src: buffer, p: number): number
|
||||
return byteswap(readu32(src, p))
|
||||
end
|
||||
|
||||
-- the 2-bit pixel index of pixel i (column-major: i = x * 4 + y)
|
||||
local function pix(lo: number, i: number): number
|
||||
return band(rshift(lo, i + 15), 2) + band(rshift(lo, i), 1)
|
||||
end
|
||||
|
||||
-- ETC1 / ETC2 colour block at p into blk. etc2: T / H / planar modes; punch: the RGB A1 variant (bit 33 = opaque).
|
||||
local function colour(src: buffer, p: number, blk: buffer, etc2: boolean, punch: boolean)
|
||||
local hi, lo = be32(src, p), be32(src, p + 4)
|
||||
local diffBit = band(rshift(hi, 1), 1)
|
||||
local flip = band(hi, 1)
|
||||
local opaque = not punch or diffBit == 1
|
||||
local r1, g1, b1, r2, g2, b2
|
||||
if not punch and diffBit == 0 then
|
||||
r1, r2 = band(rshift(hi, 28), 15) * 17, band(rshift(hi, 24), 15) * 17
|
||||
g1, g2 = band(rshift(hi, 20), 15) * 17, band(rshift(hi, 16), 15) * 17
|
||||
b1, b2 = band(rshift(hi, 12), 15) * 17, band(rshift(hi, 8), 15) * 17
|
||||
else
|
||||
local R, G, B = band(rshift(hi, 27), 31), band(rshift(hi, 19), 31), band(rshift(hi, 11), 31)
|
||||
local dR, dG, dB = band(rshift(hi, 24), 7), band(rshift(hi, 16), 7), band(rshift(hi, 8), 7)
|
||||
if dR >= 4 then
|
||||
dR -= 8
|
||||
end
|
||||
if dG >= 4 then
|
||||
dG -= 8
|
||||
end
|
||||
if dB >= 4 then
|
||||
dB -= 8
|
||||
end
|
||||
local R2, G2, B2 = R + dR, G + dG, B + dB
|
||||
if etc2 and (R2 < 0 or R2 > 31) then
|
||||
-- T mode
|
||||
local ra = band(rshift(hi, 27), 3) * 4 + band(rshift(hi, 24), 3)
|
||||
local c1r, c1g, c1b = ra * 17, band(rshift(hi, 20), 15) * 17, band(rshift(hi, 16), 15) * 17
|
||||
local c2r, c2g, c2b = band(rshift(hi, 12), 15) * 17, band(rshift(hi, 8), 15) * 17, band(rshift(hi, 4), 15) * 17
|
||||
local d = DIST[band(rshift(hi, 2), 3) * 2 + band(hi, 1) + 1]
|
||||
P[1] = rgb(c1r, c1g, c1b)
|
||||
P[2] = rgb(c2r + d, c2g + d, c2b + d)
|
||||
P[3] = rgb(c2r, c2g, c2b)
|
||||
P[4] = rgb(c2r - d, c2g - d, c2b - d)
|
||||
elseif etc2 and (G2 < 0 or G2 > 31) then
|
||||
-- H mode
|
||||
local q1r = band(rshift(hi, 27), 15)
|
||||
local q1g = band(rshift(hi, 24), 7) * 2 + band(rshift(hi, 20), 1)
|
||||
local q1b = band(rshift(hi, 19), 1) * 8 + band(rshift(hi, 15), 7)
|
||||
local q2r, q2g, q2b = band(rshift(hi, 11), 15), band(rshift(hi, 7), 15), band(rshift(hi, 3), 15)
|
||||
local order = if q1r * 256 + q1g * 16 + q1b >= q2r * 256 + q2g * 16 + q2b then 1 else 0
|
||||
local d = DIST[band(rshift(hi, 2), 1) * 4 + band(hi, 1) * 2 + order + 1]
|
||||
local c1r, c1g, c1b, c2r, c2g, c2b = q1r * 17, q1g * 17, q1b * 17, q2r * 17, q2g * 17, q2b * 17
|
||||
P[1] = rgb(c1r + d, c1g + d, c1b + d)
|
||||
P[2] = rgb(c1r - d, c1g - d, c1b - d)
|
||||
P[3] = rgb(c2r + d, c2g + d, c2b + d)
|
||||
P[4] = rgb(c2r - d, c2g - d, c2b - d)
|
||||
elseif etc2 and (B2 < 0 or B2 > 31) then
|
||||
-- planar (always opaque)
|
||||
local ro = band(rshift(hi, 25), 63)
|
||||
local go = band(rshift(hi, 24), 1) * 64 + band(rshift(hi, 17), 63)
|
||||
local bo = band(rshift(hi, 16), 1) * 32 + band(rshift(hi, 11), 3) * 8 + band(rshift(hi, 7), 7)
|
||||
local rh = band(rshift(hi, 2), 31) * 2 + band(hi, 1)
|
||||
local gh, bh = band(rshift(lo, 25), 127), band(rshift(lo, 19), 63)
|
||||
local rv, gv, bv = band(rshift(lo, 13), 63), band(rshift(lo, 6), 127), band(lo, 63)
|
||||
ro, rh, rv = ro * 4 + rshift(ro, 4), rh * 4 + rshift(rh, 4), rv * 4 + rshift(rv, 4)
|
||||
go, gh, gv = go * 2 + rshift(go, 6), gh * 2 + rshift(gh, 6), gv * 2 + rshift(gv, 6)
|
||||
bo, bh, bv = bo * 4 + rshift(bo, 4), bh * 4 + rshift(bh, 4), bv * 4 + rshift(bv, 4)
|
||||
for y = 0, 3 do
|
||||
for x = 0, 3 do
|
||||
local r = (x * (rh - ro) + y * (rv - ro) + 4 * ro + 2) // 4
|
||||
local g = (x * (gh - go) + y * (gv - go) + 4 * go + 2) // 4
|
||||
local b = (x * (bh - bo) + y * (bv - bo) + 4 * bo + 2) // 4
|
||||
writeu32(blk, (y * 4 + x) * 4, rgb(r, g, b))
|
||||
end
|
||||
end
|
||||
return
|
||||
else
|
||||
r1, g1, b1 = R * 8 + rshift(R, 2), G * 8 + rshift(G, 2), B * 8 + rshift(B, 2)
|
||||
r2, g2, b2 = R2 * 8 + rshift(R2, 2), G2 * 8 + rshift(G2, 2), B2 * 8 + rshift(B2, 2)
|
||||
end
|
||||
if not r1 then
|
||||
-- T / H: the paint colours by index
|
||||
for i = 0, 15 do
|
||||
local x, y = rshift(i, 2), band(i, 3)
|
||||
local idx = pix(lo, i)
|
||||
local c = if not opaque and idx == 2 then 0 else P[idx + 1]
|
||||
writeu32(blk, (y * 4 + x) * 4, c)
|
||||
end
|
||||
return
|
||||
end
|
||||
end
|
||||
local t1, t2 = MOD[band(rshift(hi, 5), 7) + 1], MOD[band(rshift(hi, 2), 7) + 1]
|
||||
for i = 0, 15 do
|
||||
local x, y = rshift(i, 2), band(i, 3)
|
||||
local second = if flip == 0 then x >= 2 else y >= 2
|
||||
local idx = pix(lo, i)
|
||||
local c
|
||||
if not opaque and idx == 2 then
|
||||
c = 0
|
||||
else
|
||||
local m = if second then t2[idx + 1] else t1[idx + 1]
|
||||
if not opaque and idx == 0 then
|
||||
m = 0
|
||||
end
|
||||
if second then
|
||||
c = rgb((r2 :: number) + m, (g2 :: number) + m, (b2 :: number) + m)
|
||||
else
|
||||
c = rgb((r1 :: number) + m, (g1 :: number) + m, (b1 :: number) + m)
|
||||
end
|
||||
end
|
||||
writeu32(blk, (y * 4 + x) * 4, c)
|
||||
end
|
||||
end
|
||||
|
||||
-- ------------------------------------------------------------------------------------------------ EAC
|
||||
local VALS = table.create(16, 0)
|
||||
|
||||
-- the 16 values of the EAC block at p into VALS (raster order): mode "alpha" (8-bit), "u11" / "s11" (11-bit)
|
||||
local function eac(src: buffer, p: number, mode: string)
|
||||
local base = readu8(src, p)
|
||||
local b1 = readu8(src, p + 1)
|
||||
local mult, tab = rshift(b1, 4), EAC[band(b1, 15) + 1]
|
||||
-- 48 index bits, pixel 0 (column-major) first: bytes 2..7 big-endian, as 24 + 24 bits
|
||||
local hiIdx = readu8(src, p + 2) * 65536 + readu8(src, p + 3) * 256 + readu8(src, p + 4)
|
||||
local loIdx = readu8(src, p + 5) * 65536 + readu8(src, p + 6) * 256 + readu8(src, p + 7)
|
||||
if mode == "s11" then
|
||||
if base >= 128 then
|
||||
base -= 256
|
||||
end
|
||||
if base == -128 then
|
||||
base = -127
|
||||
end
|
||||
end
|
||||
for i = 0, 15 do
|
||||
local idx = if i < 8 then band(rshift(hiIdx, 21 - i * 3), 7) else band(rshift(loIdx, 21 - (i - 8) * 3), 7)
|
||||
local m = tab[idx + 1]
|
||||
local v
|
||||
if mode == "alpha" then
|
||||
v = clamp8(base + m * mult)
|
||||
elseif mode == "u11" then
|
||||
v = if mult == 0 then base * 8 + 4 + m else base * 8 + 4 + m * mult * 8
|
||||
v = if v < 0 then 0 elseif v > 2047 then 2047 else v
|
||||
else
|
||||
v = if mult == 0 then base * 8 + m else base * 8 + m * mult * 8
|
||||
v = if v < -1023 then -1023 elseif v > 1023 then 1023 else v
|
||||
end
|
||||
VALS[band(i, 3) * 4 + rshift(i, 2) + 1] = v
|
||||
end
|
||||
end
|
||||
|
||||
-- 11-bit values -> 8 / 16 bits
|
||||
local function to8(v: number, signed: boolean): number
|
||||
if signed then
|
||||
return math.floor(v * 127 / 1023 + 0.5) + 128
|
||||
end
|
||||
return math.floor(v * 255 / 2047 + 0.5)
|
||||
end
|
||||
local function to16(v: number, signed: boolean): number
|
||||
if signed then
|
||||
return math.floor((v + 1023) * 65535 / 2046 + 0.5)
|
||||
end
|
||||
return v * 32 + rshift(v, 6)
|
||||
end
|
||||
|
||||
-- ------------------------------------------------------------------------------------------------ decode
|
||||
-- size(kind, w, h) -> bytes of the block data | nil for an unknown kind
|
||||
function Etc.size(kind: string, w: number, h: number): number?
|
||||
local b = Etc.BLOCK[kind]
|
||||
return if b then ((w + 3) // 4) * ((h + 3) // 4) * b else nil
|
||||
end
|
||||
|
||||
-- decode(kind, src, offset, w, h, out?, outOffset?, wide?) -> pixels | nil, message. rgba8, or rgba16 for the EAC
|
||||
-- kinds when wide is true.
|
||||
function Etc.decode(kind: string, src: buffer, offset: number, w: number, h: number, out: buffer?, outOffset: number?, wide: boolean?): (buffer?, string?)
|
||||
local bytes = Etc.BLOCK[kind]
|
||||
if not bytes 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 = Etc.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 isEac = string.sub(kind, 1, 3) == "eac"
|
||||
local w16 = isEac and wide == true
|
||||
local bpp = if w16 then 8 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 signed = kind == "eac_r11s" or kind == "eac_rg11s"
|
||||
local two = kind == "eac_rg11" or kind == "eac_rg11s"
|
||||
local mode11 = if signed then "s11" else "u11"
|
||||
local etc2 = kind ~= "etc1"
|
||||
local punch = kind == "etc2_rgba1"
|
||||
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
|
||||
if isEac then
|
||||
eac(src, p, mode11)
|
||||
for i = 0, 15 do
|
||||
local v = VALS[i + 1]
|
||||
if w16 then
|
||||
local q = to16(v, signed)
|
||||
writeu16(blk, i * 8, q)
|
||||
writeu16(blk, i * 8 + 2, if two then 0 else q)
|
||||
writeu16(blk, i * 8 + 4, if two then 0 else q)
|
||||
writeu16(blk, i * 8 + 6, 65535)
|
||||
else
|
||||
local q = to8(v, signed)
|
||||
writeu32(blk, i * 4, if two then q + 0xFF000000 else q + q * 256 + q * 65536 + 0xFF000000)
|
||||
end
|
||||
end
|
||||
if two then
|
||||
eac(src, p + 8, mode11)
|
||||
for i = 0, 15 do
|
||||
local v = VALS[i + 1]
|
||||
if w16 then
|
||||
writeu16(blk, i * 8 + 2, to16(v, signed))
|
||||
else
|
||||
writeu8(blk, i * 4 + 1, to8(v, signed))
|
||||
end
|
||||
end
|
||||
end
|
||||
elseif kind == "etc2_rgba" then
|
||||
colour(src, p + 8, blk, true, false)
|
||||
eac(src, p, "alpha")
|
||||
for i = 0, 15 do
|
||||
writeu8(blk, i * 4 + 3, VALS[i + 1])
|
||||
end
|
||||
else
|
||||
colour(src, p, blk, etc2, punch)
|
||||
end
|
||||
p += bytes
|
||||
local cols = math.min(4, w - bx * 4)
|
||||
local base = oo + (by * 4) * rowBytes + bx * 4 * bpp
|
||||
for y = 0, rows - 1 do
|
||||
buffer.copy(dst, base + y * rowBytes, blk, y * 4 * bpp, cols * bpp)
|
||||
end
|
||||
end
|
||||
end
|
||||
return dst, nil
|
||||
end
|
||||
|
||||
return Etc
|
||||
Reference in new issue
Block a user