Files
rosetta/src/Formats/codecs/image/Texel.luau
T
4DBug 672948f7d7 ws-tex: image/dds (read every DXGI / legacy FourCC / bit-mask format, mips, cube, arrays, volumes; write rgba8 / BC1/3/4/5 / 16-bit / float)
New shared codecs: codecs/image/Texel (uncompressed GPU texel formats incl. packed, shared exponent, YUY2),
codecs/image/Texture (dispatch over Bcn / Etc / Texel, surface assembly, mip halving). Tested against
nvcompress / nvassemble / ImageMagick files (Pillow per surface, Godot for BC6H floats), hand-written
formats, and our files read back by Pillow + nvddsinfo.
2026-10-31 14:21:04 -05:00

399 lines
15 KiB
Luau

--!strict
--!native
--!optimize 2
-- codecs/image/Texel: uncompressed GPU texel formats -> the standard pixel formats, shared by the texture containers
-- (DDS's DXGI and legacy bit-mask formats, KTX's Vulkan / OpenGL formats, ...). New in formats-luau. Formats are named
-- lower case, channels from the lowest address / bit up (DXGI order): r8, rg8, rgb8, bgr8, rgba8, bgra8, bgrx8, a8,
-- l8, la8, r16, rgba16, *s (snorm), *ui / *i (integers), *f (half / float: r16f ... rgba32f), and the packed ones:
-- b5g6r5, r5g6b5, b5g5r5a1, a1b5g5r5, b4g4r4a4, a4b4g4r4, a4r4g4b4, rgb10a2, bgr10a2, r11g11b10f, rgb9e5; any other
-- bit-mask layout through Texel.masked(bytes, rMask, gMask, bMask, aMask, luminance?). Never throws.
--
-- local px, fmt = Texel.decode("bgra8", buf, offset, w, h) -- rgba8 pixels
-- local px, fmt = Texel.decode("rgba16f", buf, offset, w, h, true) -- native: rgbaf32 (float) / rgba16 (16-bit)
-- local desc = Texel.masked(2, 0xF800, 0x07E0, 0x001F, 0) -- a descriptor, accepted by decode
--
-- Channels: one-channel formats (r*, l*) are grey (r = g = b), two-channel ones red + green; missing alpha is opaque.
-- rgba8 output: unorm rescaled with rounding, snorm biased (8-bit: s + 128), floats clamped to 0..1, integers clamped
-- to 0..255. Native output: rgba8 when every channel fits 8 bits unsigned, rgba16 for 9..16-bit unorm, else rgbaf32
-- (snorm -> -1..1, integers as their values).
local Half = require("@formats/codecs/image/Half")
local Texel = {}
export type Channel = { c: string, shift: number, bits: number } -- packed channels (c = r g b a l)
export type Desc = {
bytes: number, -- per texel
kind: "array" | "packed" | "r11g11b10f" | "rgb9e5" | "pair" | "ayuv",
size: number?, -- array: bytes per component (1, 2, 4)
type: string?, -- array: unorm / snorm / uint / sint / float
order: string?, -- array: the components in memory (r g b a l x); pair: the 4 bytes of 2 pixels (yuyv, uyvy, rgbg, grgb)
channels: { Channel }?, -- packed: unorm fields of a little-endian 16 / 32-bit word
}
local readu8, readu16, readu32, readi8, readi16, readi32, readf32 = buffer.readu8, buffer.readu16, buffer.readu32, buffer.readi8, buffer.readi16, buffer.readi32, buffer.readf32
local writeu8, writeu16, writeu32, writef32 = buffer.writeu8, buffer.writeu16, buffer.writeu32, buffer.writef32
local band, rshift, lshift = bit32.band, bit32.rshift, bit32.lshift
local HALF = Half.TABLE
-- ------------------------------------------------------------------------------------------------ formats
local FORMATS: { [string]: Desc } = {}
local function array(name: string, size: number, typ: string, order: string)
FORMATS[name] = { bytes = size * #order, kind = "array", size = size, type = typ, order = order }
end
for _, o in { "r", "rg", "rgb", "rgba" } do
array(o .. "8", 1, "unorm", o)
array(o .. "8s", 1, "snorm", o)
array(o .. "8ui", 1, "uint", o)
array(o .. "8i", 1, "sint", o)
array(o .. "16", 2, "unorm", o)
array(o .. "16s", 2, "snorm", o)
array(o .. "16ui", 2, "uint", o)
array(o .. "16i", 2, "sint", o)
array(o .. "16f", 2, "float", o)
array(o .. "32ui", 4, "uint", o)
array(o .. "32i", 4, "sint", o)
array(o .. "32f", 4, "float", o)
end
array("bgr8", 1, "unorm", "bgr")
array("bgra8", 1, "unorm", "bgra")
array("bgrx8", 1, "unorm", "bgrx")
array("rgbx8", 1, "unorm", "rgbx")
array("a8", 1, "unorm", "a")
array("l8", 1, "unorm", "l")
array("la8", 1, "unorm", "la")
array("l16", 2, "unorm", "l")
array("la16", 2, "unorm", "la")
array("a16", 2, "unorm", "a")
local function packed(name: string, bytes: number, spec: { any })
local ch: { Channel } = {}
for i = 1, #spec, 3 do
table.insert(ch, { c = spec[i], shift = spec[i + 1], bits = spec[i + 2] })
end
FORMATS[name] = { bytes = bytes, kind = "packed", channels = ch }
end
packed("b5g6r5", 2, { "b", 0, 5, "g", 5, 6, "r", 11, 5 })
packed("r5g6b5", 2, { "r", 0, 5, "g", 5, 6, "b", 11, 5 })
packed("b5g5r5a1", 2, { "b", 0, 5, "g", 5, 5, "r", 10, 5, "a", 15, 1 })
packed("b5g5r5x1", 2, { "b", 0, 5, "g", 5, 5, "r", 10, 5 })
packed("a1b5g5r5", 2, { "a", 0, 1, "b", 1, 5, "g", 6, 5, "r", 11, 5 })
packed("a1r5g5b5", 2, { "a", 0, 1, "r", 1, 5, "g", 6, 5, "b", 11, 5 })
packed("b4g4r4a4", 2, { "b", 0, 4, "g", 4, 4, "r", 8, 4, "a", 12, 4 })
packed("r4g4b4a4", 2, { "r", 0, 4, "g", 4, 4, "b", 8, 4, "a", 12, 4 })
packed("a4b4g4r4", 2, { "a", 0, 4, "b", 4, 4, "g", 8, 4, "r", 12, 4 })
packed("a4r4g4b4", 2, { "a", 0, 4, "r", 4, 4, "g", 8, 4, "b", 12, 4 })
packed("rgb10a2", 4, { "r", 0, 10, "g", 10, 10, "b", 20, 10, "a", 30, 2 })
packed("bgr10a2", 4, { "b", 0, 10, "g", 10, 10, "r", 20, 10, "a", 30, 2 })
packed("r3g3b2", 1, { "b", 0, 2, "g", 2, 3, "r", 5, 3 })
FORMATS.r11g11b10f = { bytes = 4, kind = "r11g11b10f" }
FORMATS.rgb9e5 = { bytes = 4, kind = "rgb9e5" }
-- 4:2:2 pairs (2 pixels in 4 bytes; bytes = per pixel) and 4:4:4 AYUV (bytes V U Y A); Y'CbCr is BT.601 limited range
FORMATS.yuy2 = { bytes = 2, kind = "pair", order = "yuyv" }
FORMATS.uyvy = { bytes = 2, kind = "pair", order = "uyvy" }
FORMATS.rgbg = { bytes = 2, kind = "pair", order = "rgbg" }
FORMATS.grgb = { bytes = 2, kind = "pair", order = "grgb" }
FORMATS.ayuv = { bytes = 4, kind = "ayuv" }
Texel.FORMATS = FORMATS
-- masked(bytes, r, g, b, a, luminance?) -> a packed descriptor from bit masks (DDS legacy pixel formats); luminance:
-- the r mask is a luminance channel
function Texel.masked(bytes: number, r: number, g: number, b: number, a: number, luminance: boolean?): Desc?
if bytes < 1 or bytes > 4 then
return nil
end
local ch: { Channel } = {}
for i, m in { r, g, b, a } do
if m ~= 0 then
local shift = 0
while band(m, lshift(1, shift)) == 0 and shift < 31 do
shift += 1
end
local bits = 0
while shift + bits < 32 and band(m, lshift(1, shift + bits)) ~= 0 do
bits += 1
end
local name = if i == 1 then (if luminance then "l" else "r") elseif i == 2 then "g" elseif i == 3 then "b" else "a"
table.insert(ch, { c = name, shift = shift, bits = bits })
end
end
if #ch == 0 then
return nil
end
return { bytes = bytes, kind = "packed", channels = ch }
end
-- size(fmt, w, h) -> bytes (rows tightly packed) | nil
function Texel.size(fmt: string | Desc, w: number, h: number): number?
local d = if type(fmt) == "string" then FORMATS[fmt] else fmt
if not d then
return nil
end
return if d.kind == "pair" then ((w + 1) // 2) * 4 * h else d.bytes * w * h
end
-- ------------------------------------------------------------------------------------------------ helpers
-- the native output of a descriptor: rgba8 / rgba16 / rgbaf32
local function nativeOf(d: Desc): string
if d.kind == "array" then
local t, s = d.type, d.size :: number
if t == "float" or t == "snorm" or t == "sint" or (t == "uint" and s > 1) then
return "rgbaf32"
end
return if s == 1 then "rgba8" else "rgba16"
elseif d.kind == "packed" then
local wide = false
for _, c in d.channels :: { Channel } do
wide = wide or c.bits > 8
end
return if wide then "rgba16" else "rgba8"
end
return "rgbaf32"
end
Texel.nativeOf = nativeOf
local function uf11(v: number): number
local e, m = rshift(v, 6), band(v, 63)
if e == 0 then
return m / 64 * 2 ^ -14
elseif e == 31 then
return if m == 0 then math.huge else 0 / 0
end
return (1 + m / 64) * 2 ^ (e - 15)
end
local function uf10(v: number): number
local e, m = rshift(v, 5), band(v, 31)
if e == 0 then
return m / 32 * 2 ^ -14
elseif e == 31 then
return if m == 0 then math.huge else 0 / 0
end
return (1 + m / 32) * 2 ^ (e - 15)
end
-- one component at q of an array format -> a float (native meaning) and its rgba8 byte
local function component(src: buffer, q: number, size: number, typ: string): (number, number)
if typ == "unorm" then
if size == 1 then
local v = readu8(src, q)
return v / 255, v
elseif size == 2 then
local v = readu16(src, q)
return v / 65535, (v * 255 + 32767) // 65535
end
local v = readu32(src, q)
return v / 4294967295, math.floor(v / 4294967295 * 255 + 0.5)
elseif typ == "snorm" then
if size == 1 then
local v = math.max(readi8(src, q), -127)
return v / 127, v + 128
elseif size == 2 then
local v = math.max(readi16(src, q), -32767)
return v / 32767, math.floor(v * 127 / 32767 + 0.5) + 128
end
local v = math.max(readi32(src, q), -2147483647)
return v / 2147483647, math.floor(v * 127 / 2147483647 + 0.5) + 128
elseif typ == "float" then
local f = if size == 2 then HALF[readu16(src, q)] else readf32(src, q)
local c = if f ~= f then 0 elseif f < 0 then 0 elseif f > 1 then 1 else f
return f, math.floor(c * 255 + 0.5)
elseif typ == "uint" then
local v = if size == 1 then readu8(src, q) elseif size == 2 then readu16(src, q) else readu32(src, q)
return v, math.min(v, 255)
end
local v = if size == 1 then readi8(src, q) elseif size == 2 then readi16(src, q) else readi32(src, q)
return v, math.clamp(v, 0, 255)
end
-- BT.601 limited-range Y'CbCr -> an rgba8 u32
local function ycc(y: number, u: number, v: number, a: number): number
local c, d, e = 1.164 * (y - 16), u - 128, v - 128
local r = math.clamp(math.floor(c + 1.596 * e + 0.5), 0, 255)
local g = math.clamp(math.floor(c - 0.392 * d - 0.813 * e + 0.5), 0, 255)
local b = math.clamp(math.floor(c + 2.017 * d + 0.5), 0, 255)
return r + g * 256 + b * 65536 + a * 16777216
end
-- the 4:2:2 pair formats and AYUV -> rgba8 (out is always rgba8: 8-bit sources)
local function yuv(d: Desc, src: buffer, offset: number, w: number, h: number, pitch: number, dst: buffer): buffer
local n = 0
local order = d.order or ""
for y = 0, h - 1 do
local s = offset + y * pitch
for x = 0, w - 1 do
if d.kind == "ayuv" then
local q = s + x * 4
writeu32(dst, n, ycc(readu8(src, q + 2), readu8(src, q + 1), readu8(src, q), readu8(src, q + 3)))
else
local q = s + (x // 2) * 4
local b0, b1, b2, b3 = readu8(src, q), readu8(src, q + 1), readu8(src, q + 2), readu8(src, q + 3)
local odd = x % 2 == 1
if order == "yuyv" then
writeu32(dst, n, ycc(if odd then b2 else b0, b1, b3, 255))
elseif order == "uyvy" then
writeu32(dst, n, ycc(if odd then b3 else b1, b0, b2, 255))
elseif order == "rgbg" then
writeu32(dst, n, b0 + (if odd then b3 else b1) * 256 + b2 * 65536 + 0xFF000000)
else
writeu32(dst, n, b1 + (if odd then b2 else b0) * 256 + b3 * 65536 + 0xFF000000)
end
end
n += 4
end
end
return dst
end
-- ------------------------------------------------------------------------------------------------ decode
-- decode(fmt, src, offset, w, h, native?, rowPitch?) -> pixels, pixel format ("rgba8" / "rgba16" / "rgbaf32") | nil,
-- message. rowPitch: bytes per source row (default tight).
function Texel.decode(fmt: string | Desc, src: buffer, offset: number, w: number, h: number, native: boolean?, rowPitch: number?): (buffer?, string)
local d = if type(fmt) == "string" then FORMATS[fmt] else fmt
if not d then
return nil, "unsupported: unknown texel format " .. tostring(fmt)
end
local tb = d.bytes
local rowBytes = if d.kind == "pair" then ((w + 1) // 2) * 4 else tb * w
local pitch = rowPitch or rowBytes
if w < 1 or h < 1 or w % 1 ~= 0 or h % 1 ~= 0 or pitch < rowBytes then
return nil, "bad image size"
end
if offset < 0 or offset + pitch * (h - 1) + rowBytes > buffer.len(src) then
return nil, "truncated: texel data runs past the end"
end
local out = if native and d.kind ~= "pair" and d.kind ~= "ayuv" then nativeOf(d) else "rgba8"
local bpp = if out == "rgba8" then 4 elseif out == "rgba16" then 8 else 16
local dst = buffer.create(w * h * bpp)
local n = 0
if d.kind == "array" then
local order, size, typ = d.order :: string, d.size :: number, d.type :: string
-- fast paths
if size == 1 and typ == "unorm" and out == "rgba8" and (order == "rgba" or order == "bgra" or order == "bgrx" or order == "rgbx") then
local swap = order == "bgra" or order == "bgrx"
local opaque = order == "bgrx" or order == "rgbx"
for y = 0, h - 1 do
local s = offset + y * pitch
for x = 0, w - 1 do
local v = readu32(src, s + x * 4)
if swap then
v = band(v, 0xFF00FF00) + band(rshift(v, 16), 0xFF) + lshift(band(v, 0xFF), 16)
end
if opaque then
v = band(v, 0xFFFFFF) + 0xFF000000
end
writeu32(dst, n, v)
n += 4
end
end
return dst, out
end
local nc = #order
local map = table.create(nc, 0) -- component -> output channel 0..3, 4 = grey, -1 = skip
for i = 1, nc do
local c = string.sub(order, i, i)
map[i] = if c == "r" then (if nc == 1 then 4 else 0) elseif c == "g" then 1 elseif c == "b" then 2 elseif c == "a" then 3 elseif c == "l" then 4 else -1
end
local f = table.create(4, 0)
local q8 = table.create(4, 0)
for y = 0, h - 1 do
local s = offset + y * pitch
for _ = 0, w - 1 do
f[1], f[2], f[3], f[4] = 0, 0, 0, 1
q8[1], q8[2], q8[3], q8[4] = 0, 0, 0, 255
for i = 1, nc do
local m = map[i]
if m >= 0 then
local fv, bv = component(src, s, size, typ)
if m == 4 then
f[1], f[2], f[3] = fv, fv, fv
q8[1], q8[2], q8[3] = bv, bv, bv
else
f[m + 1], q8[m + 1] = fv, bv
end
end
s += size
end
if out == "rgba8" then
writeu32(dst, n, q8[1] + q8[2] * 256 + q8[3] * 65536 + q8[4] * 16777216)
elseif out == "rgba16" then
for c = 0, 3 do
writeu16(dst, n + c * 2, math.floor(math.clamp(f[c + 1], 0, 1) * 65535 + 0.5))
end
else
for c = 0, 3 do
writef32(dst, n + c * 4, f[c + 1])
end
end
n += bpp
end
end
return dst, out
elseif d.kind == "packed" then
local chs = d.channels :: { Channel }
for y = 0, h - 1 do
local s = offset + y * pitch
for _ = 0, w - 1 do
local v = if tb == 1 then readu8(src, s) elseif tb == 2 then readu16(src, s) elseif tb == 3 then readu16(src, s) + readu8(src, s + 2) * 65536 else readu32(src, s)
s += tb
local r, g, b, a = 0, 0, 0, 1
for _, c in chs do
local maxv = lshift(1, c.bits) - 1
local x = band(rshift(v, c.shift), maxv) / (if c.bits >= 32 then 4294967295 else maxv)
local k = c.c
if k == "r" then
r = x
elseif k == "g" then
g = x
elseif k == "b" then
b = x
elseif k == "a" then
a = x
else
r, g, b = x, x, x
end
end
if out == "rgba8" then
writeu32(dst, n, math.floor(r * 255 + 0.5) + math.floor(g * 255 + 0.5) * 256 + math.floor(b * 255 + 0.5) * 65536 + math.floor(a * 255 + 0.5) * 16777216)
else
writeu16(dst, n, math.floor(r * 65535 + 0.5))
writeu16(dst, n + 2, math.floor(g * 65535 + 0.5))
writeu16(dst, n + 4, math.floor(b * 65535 + 0.5))
writeu16(dst, n + 6, math.floor(a * 65535 + 0.5))
end
n += bpp
end
end
return dst, out
end
if d.kind == "pair" or d.kind == "ayuv" then
return yuv(d, src, offset, w, h, pitch, dst), out
end
-- shared-exponent / packed floats
local nine = d.kind == "rgb9e5"
for y = 0, h - 1 do
local s = offset + y * pitch
for _ = 0, w - 1 do
local v = readu32(src, s)
s += 4
local sc = if nine then 2 ^ (rshift(v, 27) - 24) else 0
local r: number = if nine then band(v, 511) * sc else uf11(band(v, 2047))
local g: number = if nine then band(rshift(v, 9), 511) * sc else uf11(band(rshift(v, 11), 2047))
local b: number = if nine then band(rshift(v, 18), 511) * sc else uf10(rshift(v, 22))
if out == "rgba8" then
local function q(x: number): number
return if x ~= x or x <= 0 then 0 elseif x >= 1 then 255 else math.floor(x * 255 + 0.5)
end
writeu32(dst, n, q(r) + q(g) * 256 + q(b) * 65536 + 0xFF000000)
else
writef32(dst, n, r)
writef32(dst, n + 4, g)
writef32(dst, n + 8, b)
writef32(dst, n + 12, 1)
end
n += bpp
end
end
return dst, out
end
return Texel