Files
rosetta/src/Formats/codecs/image/Texture.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

163 lines
5.8 KiB
Luau

--!strict
-- codecs/image/Texture: one entry point over the texture pixel codecs for the texture containers (DDS, KTX, VTF, ...):
-- a format name is a BCn kind (codecs/image/Bcn), an ETC / EAC kind (codecs/image/Etc) or a texel format
-- (codecs/image/Texel); decode / size dispatch on it, surfaces() assembles mip levels / faces / layers into one
-- Types.Image, halve() makes the next mip level of an image for writers. New in formats-luau. Never throws.
--
-- local px, pf = Texture.decode("bc7", buf, offset, w, h, native) -- pixels + their pixel format
-- Texture.size("etc2_rgb", w, h) -- bytes of one surface
-- local img = Texture.image(px, pf, w, h) -- a Types.Image around them
-- local smaller = Texture.halve(img) -- box-filtered next mip level
local Types = require("@formats/Types")
local Bcn = require("@formats/codecs/image/Bcn")
local Etc = require("@formats/codecs/image/Etc")
local Texel = require("@formats/codecs/image/Texel")
local Half = require("@formats/codecs/image/Half")
local Texture = {}
-- known(name) -> "bcn" | "etc" | "texel" | nil
function Texture.known(name: string): string?
if Bcn.BLOCK[name] then
return "bcn"
elseif Etc.BLOCK[name] then
return "etc"
elseif Texel.FORMATS[name] then
return "texel"
end
return nil
end
-- size(name, w, h) -> bytes of a w x h surface | nil
function Texture.size(name: string, w: number, h: number): number?
return Bcn.size(name, w, h) or Etc.size(name, w, h) or Texel.size(name, w, h)
end
-- block(name) -> block width, height, bytes (1, 1, texel bytes for texel formats) | nil
function Texture.block(name: string): (number?, number?, number?)
local b = Bcn.BLOCK[name] or Etc.BLOCK[name]
if b then
return 4, 4, b
end
local d = Texel.FORMATS[name]
if d then
return 1, 1, d.bytes
end
return nil, nil, nil
end
-- decode(name, src, offset, w, h, native?) -> pixels, pixel format | nil, message. Without native the result is
-- rgba8 (BC6H and float texels clamped to 0..1); native keeps rgbaf32 (BC6H, floats) / rgba16 (EAC, 16-bit unorm).
function Texture.decode(name: string, src: buffer, offset: number, w: number, h: number, native: boolean?): (buffer?, string)
if Bcn.BLOCK[name] then
local px, err = Bcn.decode(name, src, offset, w, h)
if not px then
return nil, err or "bad block data"
end
if Bcn.FLOAT[name] then
if native then
return px, "rgbaf32"
end
local out = buffer.create(w * h * 4)
for i = 0, w * h - 1 do
for c = 0, 3 do
local v = buffer.readf32(px, i * 16 + c * 4)
buffer.writeu8(out, i * 4 + c, if v ~= v or v <= 0 then 0 elseif v >= 1 then 255 else math.floor(v * 255 + 0.5))
end
end
return out, "rgba8"
end
return px, "rgba8"
elseif Etc.BLOCK[name] then
local wide = native == true and string.sub(name, 1, 3) == "eac"
local px, err = Etc.decode(name, src, offset, w, h, nil, 0, wide)
if not px then
return nil, err or "bad block data"
end
return px, if wide then "rgba16" else "rgba8"
end
return Texel.decode(name, src, offset, w, h, native)
end
-- image(pixels, pixel format, w, h) -> Types.Image
function Texture.image(px: buffer, pf: string, w: number, h: number): Types.Image
local img = Types.Image.new(0, 0, pf :: any)
img.width, img.height, img.pixels = w, h, px
return img
end
export type Surface = { level: number, layer: number, face: number, slice: number, image: Types.Image }
-- assemble(surfaces, layout) -> the Image of the first surface with img.frames = every level-0 surface (faces /
-- layers / depth slices in order, when more than one) and img.meta.mipmaps = the smaller levels of the first surface
function Texture.assemble(list: { Surface }, layout: string): Types.Image?
local first: Types.Image? = nil
local frames: { Types.Frame } = {}
local mips: { Types.Image } = {}
for _, s in list do
if s.level == 0 then
first = first or s.image
local fr: Types.Frame = { image = s.image, delay = 0, x = 0, y = 0, dispose = "none", blend = "source" }
table.insert(frames, fr)
elseif s.layer == 0 and s.face == 0 and s.slice == 0 then
mips[s.level] = s.image
end
end
if not first then
return nil
end
local img = Texture.image(first.pixels, first.format, first.width, first.height)
if #frames > 1 then
img.frames = frames
end
if #mips > 0 then
img.meta.mipmaps = mips
end
img.meta.layout = layout
return img
end
-- halve(img) -> the next mip level (each dimension halved, at least 1; 2 x 2 box filter), same pixel format
-- (rgba8 / rgba16 / rgbaf32)
function Texture.halve(img: Types.Image): Types.Image
local w, h = img.width, img.height
local nw, nh = math.max(1, w // 2), math.max(1, h // 2)
local fmt = img.format
local out = Types.Image.new(nw, nh, fmt)
local src, dst = img.pixels, out.pixels
local cs = if fmt == "rgba8" then 1 elseif fmt == "rgba16" then 2 else 4
local function get(x: number, y: number, c: number): number
local o = ((math.min(y, h - 1) * w + math.min(x, w - 1)) * 4 + c) * cs
return if cs == 1 then buffer.readu8(src, o) elseif cs == 2 then buffer.readu16(src, o) else buffer.readf32(src, o)
end
for y = 0, nh - 1 do
for x = 0, nw - 1 do
for c = 0, 3 do
local x0, y0 = x * 2, y * 2
local v = (get(x0, y0, c) + get(x0 + 1, y0, c) + get(x0, y0 + 1, c) + get(x0 + 1, y0 + 1, c)) / 4
local o = ((y * nw + x) * 4 + c) * cs
if cs == 1 then
buffer.writeu8(dst, o, math.floor(v + 0.5))
elseif cs == 2 then
buffer.writeu16(dst, o, math.floor(v + 0.5))
else
buffer.writef32(dst, o, v)
end
end
end
end
return out
end
-- toHalf(rgbaf32 pixels, n) -> rgba16f texels (for writers)
function Texture.toHalf(px: buffer, n: number): buffer
local out = buffer.create(n * 8)
local from = Half.fromFloat
for i = 0, n * 4 - 1 do
buffer.writeu16(out, i * 2, from(buffer.readf32(px, i * 4)))
end
return out
end
return Texture