rename: src/Formats -> src/Rosetta, @formats -> @rosetta requires, build/luajit/rosetta + rosetta._compat, build/roblox/Rosetta, ROSETTA_* env/globals, writer vendor strings

This commit is contained in:
4DBug committed 2026-11-01 01:19:16 -05:00
1 parent 2956576d7f
commit 22ddcd78fa
941 files changed
+3364 -3364

No files matched your search

+497
View File
@@ -0,0 +1,497 @@
--!strict
-- image/dds: DirectDraw Surface (Microsoft "DDS" programming guide: DDS_HEADER, DDS_PIXELFORMAT, DDS_HEADER_DXT10; new
-- code). Decode: every DXGI format with a pixel layout (BC1..BC7 through codecs/image/Bcn; the uncompressed UNORM /
-- SNORM / UINT / SINT / FLOAT / packed / shared-exponent ones and YUY2 / AYUV / RGBG through codecs/image/Texel), the
-- legacy FourCC codes (DXT1..DXT5, ATI1 / BC4U / BC4S, ATI2 / BC5U / BC5S, RXGB, YUY2 / UYVY / RGBG / GRGB, the
-- D3DFMT float / 16-bit numbers) and legacy bit-mask RGB / luminance / alpha formats; mip chains, cube maps (all six
-- faces or a subset), texture arrays (DX10 arraySize) and volume textures. The first surface is the image; when there
-- are more (faces, layers, depth slices) img.frames lists every one of them (meta.layout = "2d" / "cube" / "array" /
-- "volume", meta.faces), and with opts.mips the smaller levels of the first surface go to img.meta.mipmaps. rgba8 by
-- default (BC6H and float formats clamped to 0..1); opts.native keeps rgbaf32 / rgba16 where the source has more.
-- Encode: rgba8 as uncompressed (R8G8B8A8 / B8G8R8A8), BC1 / BC3 / BC4 / BC5 (legacy FourCC headers), rgba16 / rgbaf32
-- as DX10 R16G16B16A16_UNORM / R32G32B32A32_FLOAT / R16G16B16A16_FLOAT; optional mip chains (box filter) and the
-- frames as a texture array or a cube map. Not supported: planar video formats (NV12, P010, ...), palettes (P8),
-- 1-bit R1_UNORM, the depth-stencil formats with stencil channels, BC encoders other than 1 / 3 / 4 / 5.
-- ------------------------------------------------------------------------------------------------ requires
local Err = require("@rosetta/core/Err")
local Bytes = require("@rosetta/core/Bytes")
local Sniff = require("@rosetta/core/Sniff")
local Types = require("@rosetta/Types")
local Bcn = require("@rosetta/codecs/image/Bcn")
local Texel = require("@rosetta/codecs/image/Texel")
local Texture = require("@rosetta/codecs/image/Texture")
-- ------------------------------------------------------------------------------------------------ contract
local Dds = {
name = "dds",
category = "image",
extensions = { "dds" },
mime = { "image/vnd-ms.dds", "image/x-dds" },
deps = { "core/Err", "core/Bytes", "core/Sniff", "Types", "codecs/image/Bcn", "codecs/image/Texel", "codecs/image/Texture" },
caps = { decode = true, encode = true, alpha = true, float = true, mipmaps = true, cubemap = true, compressed = { "bc1", "bc2", "bc3", "bc4", "bc5", "bc6h", "bc7" } },
}
-- ------------------------------------------------------------------------------------------------ types
export type DecodeOptions = {
native: boolean?, -- keep rgbaf32 / rgba16 for float / 16-bit / BC6H sources (default false: rgba8)
mips: boolean?, -- decode the smaller mip levels of the first surface into img.meta.mipmaps (default false)
surfaces: boolean?, -- decode every face / layer / slice into img.frames (default true)
maxPixels: number?, -- refuse larger images (default 2^26, counted over every decoded surface)
}
export type EncodeOptions = {
pixelFormat: ("rgba8" | "bgra8" | "bc1" | "bc3" | "bc4" | "bc5" | "rgba16" | "rgba16f" | "rgba32f")?, -- default:
-- rgba8 for rgba8 images, rgba16 / rgba32f for rgba16 / rgbaf32 ones
mips: boolean?, -- write a full mip chain (default false)
dx10: boolean?, -- always write the DX10 header (default: only when the format needs it, or for arrays)
cube: boolean?, -- write img.frames (6) as a cube map (default: when img.meta.layout == "cube")
alphaThreshold: number?, -- BC1: alpha below this is transparent (default 128)
}
-- ------------------------------------------------------------------------------------------------ constants
-- DXGI_FORMAT -> texture format name, sRGB
local DXGI: { [number]: string } = {
[1] = "rgba32f", [2] = "rgba32f", [3] = "rgba32ui", [4] = "rgba32i",
[5] = "rgb32f", [6] = "rgb32f", [7] = "rgb32ui", [8] = "rgb32i",
[9] = "rgba16", [10] = "rgba16f", [11] = "rgba16", [12] = "rgba16ui", [13] = "rgba16s", [14] = "rgba16i",
[15] = "rg32f", [16] = "rg32f", [17] = "rg32ui", [18] = "rg32i",
[23] = "rgb10a2", [24] = "rgb10a2", [26] = "r11g11b10f",
[27] = "rgba8", [28] = "rgba8", [29] = "rgba8", [30] = "rgba8ui", [31] = "rgba8s", [32] = "rgba8i",
[33] = "rg16", [34] = "rg16f", [35] = "rg16", [36] = "rg16ui", [37] = "rg16s", [38] = "rg16i",
[39] = "r32f", [40] = "r32f", [41] = "r32f", [42] = "r32ui", [43] = "r32i",
[48] = "rg8", [49] = "rg8", [50] = "rg8ui", [51] = "rg8s", [52] = "rg8i",
[53] = "r16", [54] = "r16f", [55] = "r16", [56] = "r16", [57] = "r16ui", [58] = "r16s", [59] = "r16i",
[60] = "r8", [61] = "r8", [62] = "r8ui", [63] = "r8s", [64] = "r8i", [65] = "a8",
[67] = "rgb9e5", [68] = "rgbg", [69] = "grgb",
[70] = "bc1", [71] = "bc1", [72] = "bc1", [73] = "bc2", [74] = "bc2", [75] = "bc2", [76] = "bc3", [77] = "bc3", [78] = "bc3",
[79] = "bc4", [80] = "bc4", [81] = "bc4s", [82] = "bc5", [83] = "bc5", [84] = "bc5s",
[85] = "b5g6r5", [86] = "b5g5r5a1", [87] = "bgra8", [88] = "bgrx8", [90] = "bgra8", [91] = "bgra8", [92] = "bgrx8", [93] = "bgrx8",
[94] = "bc6h", [95] = "bc6h", [96] = "bc6hs", [97] = "bc7", [98] = "bc7", [99] = "bc7",
[100] = "ayuv", [107] = "yuy2", [115] = "b4g4r4a4", [191] = "a4b4g4r4",
}
local SRGB = { [29] = true, [72] = true, [75] = true, [78] = true, [91] = true, [93] = true, [99] = true }
-- legacy FourCC codes (strings and D3DFMT numbers)
local FOURCC: { [string]: string } = {
DXT1 = "bc1", DXT2 = "bc2", DXT3 = "bc2", DXT4 = "bc3", DXT5 = "bc3", RXGB = "bc3",
ATI1 = "bc4", BC4U = "bc4", BC4S = "bc4s", ATI2 = "bc5", BC5U = "bc5", BC5S = "bc5s",
YUY2 = "yuy2", UYVY = "uyvy", RGBG = "rgbg", GRGB = "grgb",
}
local D3DFMT: { [number]: string } = {
[36] = "rgba16", [110] = "rgba16s", [111] = "r16f", [112] = "rg16f", [113] = "rgba16f",
[114] = "r32f", [115] = "rg32f", [116] = "rgba32f", [117] = "rg8s", [60] = "rg8s", [63] = "rgba8s", [64] = "rg16s",
}
local FACES = { "+x", "-x", "+y", "-y", "+z", "-z" }
local MAGIC = "DDS "
local DDSD_MIPMAPCOUNT, DDSD_DEPTH = 0x20000, 0x800000
local DDPF_ALPHAPIXELS, DDPF_ALPHA, DDPF_FOURCC, DDPF_RGB, DDPF_LUMINANCE, DDPF_BUMPDUDV = 0x1, 0x2, 0x4, 0x40, 0x20000, 0x80000
local CAPS2_CUBEMAP, CAPS2_VOLUME = 0x200, 0x200000
local MISC_CUBE = 0x4
local band = bit32.band
-- ------------------------------------------------------------------------------------------------ sniff
function Dds.sniff(r: Bytes.Reader): number
return math.max(Sniff.magic(r, MAGIC .. "\124\0\0\0"), Sniff.magic(r, MAGIC, 0, 0.6))
end
-- ------------------------------------------------------------------------------------------------ decode
export type Info = {
width: number,
height: number,
depth: number,
mips: number,
format: string, -- the texture format name (codecs/image/Texture)
layout: string, -- "2d" | "cube" | "array" | "volume"
layers: number,
faces: { string }, -- cube faces present (in file order), else { }
srgb: boolean,
dxgi: number?,
fourcc: string?,
dataOffset: number, -- relative to the file start
alphaMode: number?,
premultiplied: boolean?,
masked: Texel.Desc?, -- legacy bit-mask formats (format = "masked" unless the masks have a name)
}
local function u32(b: buffer, o: number): number
return buffer.readu32(b, o)
end
-- info(r) -> Info | nil, Err (the headers only)
function Dds.info(r: Bytes.Reader): (Info?, Err.Err?)
local b, o, n = r.buf, r.base, r.limit - r.base
if n < 128 or buffer.readstring(b, o, 4) ~= MAGIC then
return nil, Err.new(if n < 128 and n >= 4 and buffer.readstring(b, o, 4) == MAGIC then Err.TRUNCATED else Err.CORRUPT, "not a DDS file", o)
end
if u32(b, o + 4) ~= 124 then
return nil, Err.new(Err.CORRUPT, "bad header size " .. u32(b, o + 4), o + 4)
end
local flags = u32(b, o + 8)
local h, w = u32(b, o + 12), u32(b, o + 16)
local depth = u32(b, o + 24)
local mips = u32(b, o + 28)
local pfFlags, fourccNum = u32(b, o + 80), u32(b, o + 84)
local bitCount = u32(b, o + 88)
local rm, gm, bm, am = u32(b, o + 92), u32(b, o + 96), u32(b, o + 100), u32(b, o + 104)
local caps2 = u32(b, o + 112)
if w == 0 or h == 0 or w > 65536 or h > 65536 then
return nil, Err.new(Err.CORRUPT, string.format("bad size %d x %d", w, h), o + 12)
end
if band(flags, DDSD_MIPMAPCOUNT) == 0 and mips > 1 and band(u32(b, o + 108), 0x400000) == 0 then
mips = 1 -- a count without the flag or the caps bit: some writers leave garbage
end
mips = math.max(1, mips)
local maxMips = 1
do
local m = math.max(w, h, depth)
while m > 1 do
m //= 2
maxMips += 1
end
end
if mips > maxMips then
return nil, Err.new(Err.CORRUPT, "more mip levels than the size allows", o + 28)
end
local info: Info = {
width = w,
height = h,
depth = 1,
mips = mips,
format = "",
layout = "2d",
layers = 1,
faces = {},
srgb = false,
dataOffset = 128,
}
local volume = band(caps2, CAPS2_VOLUME) ~= 0 or band(flags, DDSD_DEPTH) ~= 0 and depth > 1
if band(pfFlags, DDPF_FOURCC) ~= 0 then
local fourcc = buffer.readstring(b, o + 84, 4)
if fourcc == "DX10" then
if n < 148 then
return nil, Err.new(Err.TRUNCATED, "missing DX10 header", o + 128)
end
local dxgi, dim, misc, arr, misc2 = u32(b, o + 128), u32(b, o + 132), u32(b, o + 136), u32(b, o + 140), u32(b, o + 144)
info.dxgi = dxgi
info.dataOffset = 148
local name = DXGI[dxgi]
if not name then
return nil, Err.new(Err.UNSUPPORTED, "DXGI format " .. dxgi, o + 128)
end
info.format = name
info.srgb = SRGB[dxgi] == true
info.alphaMode = band(misc2, 7)
info.premultiplied = band(misc2, 7) == 2
info.layers = math.max(1, arr)
if info.layers > 4096 then
return nil, Err.new(Err.CORRUPT, "array size " .. arr, o + 140)
end
if dim == 4 then
volume = true
elseif dim ~= 2 and dim ~= 3 and dim ~= 0 then
return nil, Err.new(Err.CORRUPT, "bad resource dimension " .. dim, o + 132)
else
volume = false
end
if band(misc, MISC_CUBE) ~= 0 then
info.layout = "cube"
info.faces = table.clone(FACES)
elseif info.layers > 1 then
info.layout = "array"
end
else
info.fourcc = fourcc
local name = FOURCC[fourcc] or D3DFMT[fourccNum]
if not name then
return nil, Err.new(Err.UNSUPPORTED, "FourCC " .. string.format("%q", fourcc), o + 84)
end
info.format = name
info.premultiplied = fourcc == "DXT2" or fourcc == "DXT4"
end
elseif band(pfFlags, DDPF_RGB + DDPF_LUMINANCE + DDPF_ALPHA + DDPF_BUMPDUDV + 0x200) ~= 0 then
local bytes = bitCount // 8
if bitCount % 8 ~= 0 or bytes < 1 or bytes > 4 then
return nil, Err.new(Err.UNSUPPORTED, bitCount .. "-bit pixels", o + 88)
end
local lum = band(pfFlags, DDPF_LUMINANCE) ~= 0
local alphaOnly = band(pfFlags, DDPF_ALPHA) ~= 0 and band(pfFlags, DDPF_RGB + DDPF_LUMINANCE) == 0
local a = if band(pfFlags, DDPF_ALPHAPIXELS + DDPF_ALPHA) ~= 0 then am else 0
local d
if alphaOnly then
d = Texel.masked(bytes, 0, 0, 0, if am ~= 0 then am else 0xFF)
elseif band(pfFlags, DDPF_BUMPDUDV) ~= 0 then
d = nil -- signed bump maps: handled by the D3DFMT names when they come as FourCC
else
d = Texel.masked(bytes, rm, if lum then 0 else gm, if lum then 0 else bm, a, lum)
end
if not d then
return nil, Err.new(Err.UNSUPPORTED, "pixel format masks", o + 92)
end
-- the common masks map onto named texel formats (fast paths, sane names)
local key = string.format("%d:%x:%x:%x:%x:%s", bitCount, rm, gm, bm, a, tostring(lum))
local named = ({
["32:ff:ff00:ff0000:ff000000:false"] = "rgba8",
["32:ff0000:ff00:ff:ff000000:false"] = "bgra8",
["32:ff0000:ff00:ff:0:false"] = "bgrx8",
["32:ff:ff00:ff0000:0:false"] = "rgbx8",
["24:ff0000:ff00:ff:0:false"] = "bgr8",
["24:ff:ff00:ff0000:0:false"] = "rgb8",
["8:ff:0:0:0:true"] = "l8",
["16:ffff:0:0:0:true"] = "l16",
} :: { [string]: string })[key]
info.format = named or "masked"
info.masked = d
else
return nil, Err.new(Err.UNSUPPORTED, string.format("pixel format flags 0x%x", pfFlags), o + 80)
end
if volume then
info.layout = "volume"
info.depth = math.max(1, depth)
if info.depth > 2048 then
return nil, Err.new(Err.CORRUPT, "depth " .. depth, o + 24)
end
elseif band(caps2, CAPS2_CUBEMAP) ~= 0 and not info.dxgi then
info.layout = "cube"
for i, f in FACES do
if band(caps2, bit32.lshift(0x400, i - 1)) ~= 0 then
table.insert(info.faces, f)
end
end
if #info.faces == 0 then
info.faces = table.clone(FACES)
end
end
return info, nil
end
-- the bytes of one surface of format f
local function surfaceSize(info: any, w: number, h: number): number
if info.format == "masked" then
return (info.masked :: Texel.Desc).bytes * w * h
end
return Texture.size(info.format, w, h) :: number
end
function Dds.decode(r: Bytes.Reader, opts: DecodeOptions?): (Types.Image?, Err.Err?)
local info, ierr = Dds.info(r)
if not info then
return nil, ierr
end
local native = opts and opts.native or false
local wantMips = opts and opts.mips or false
local wantAll = not (opts and opts.surfaces == false)
local maxPixels = opts and opts.maxPixels or 2 ^ 26
local b, base = r.buf, r.base
local p = base + info.dataOffset
local limit = r.limit
local count = if info.layout == "cube" then #info.faces * info.layers else info.layers
local list: { Texture.Surface } = {}
local pixels = 0
local stop = false
for layer = 0, count - 1 do
local w, h, d = info.width, info.height, info.depth
for level = 0, info.mips - 1 do
local size = surfaceSize(info, w, h)
for slice = 0, d - 1 do
local want = (level == 0 and (wantAll or (layer == 0 and slice == 0))) or (wantMips and layer == 0 and slice == 0)
if want then
if p + size > limit then
if level > 0 or layer > 0 or slice > 0 then
stop = true -- a cut mip chain / later surface: keep what decoded
break
end
return nil, Err.new(Err.TRUNCATED, "surface data runs past the end", p)
end
pixels += w * h
if pixels > maxPixels then
return nil, Err.new(Err.LIMIT, string.format("over the %d pixel limit", maxPixels))
end
local px, pf
if info.format == "masked" then
px, pf = Texel.decode(info.masked :: Texel.Desc, b, p, w, h, native)
else
px, pf = Texture.decode(info.format, b, p, w, h, native)
end
if not px then
return nil, Err.from(pf, p)
end
local face = if info.layout == "cube" then layer % #info.faces else 0
table.insert(list, { level = level, layer = layer, face = face, slice = slice, image = Texture.image(px, pf, w, h) })
end
p += size
end
if stop then
break
end
w, h, d = math.max(1, w // 2), math.max(1, h // 2), math.max(1, d // 2)
end
if stop then
break
end
end
local img = Texture.assemble(list, info.layout)
if not img then
return nil, Err.new(Err.TRUNCATED, "no surface data", base + info.dataOffset)
end
if info.layout == "cube" then
img.meta.faces = info.faces
end
if info.srgb then
img.meta.colorSpace = "srgb"
elseif string.find(info.format, "f$") or info.format == "bc6h" or info.format == "bc6hs" or info.format == "rgb9e5" then
img.meta.colorSpace = "linear"
end
img.meta.source = {
format = "dds",
pixelFormat = info.format,
dxgi = info.dxgi,
fourcc = info.fourcc,
mips = info.mips,
layers = info.layers,
depth = info.depth,
premultiplied = info.premultiplied,
}
return img, nil
end
-- ------------------------------------------------------------------------------------------------ encode
local BYTES_PF: { [string]: number } = { rgba8 = 4, bgra8 = 4, rgba16 = 8, rgba16f = 8, rgba32f = 16 }
local DX10_OUT: { [string]: number } = { rgba8 = 28, bgra8 = 87, bc1 = 71, bc3 = 77, bc4 = 80, bc5 = 83, rgba16 = 11, rgba16f = 10, rgba32f = 2 }
-- the surface bytes of one image in format f
local function surface(img: Types.Image, f: string, opts: EncodeOptions?): (buffer?, Err.Err?)
local w, h, px = img.width, img.height, img.pixels
if f == "bc1" or f == "bc3" or f == "bc4" or f == "bc5" then
local blocks, err = Bcn.encode(f, px, w, h, opts and { alphaThreshold = opts.alphaThreshold } or nil)
if not blocks then
return nil, Err.new(Err.ARG, tostring(err))
end
return blocks, nil
elseif f == "bgra8" then
local out = buffer.create(w * h * 4)
for i = 0, w * h - 1 do
local v = buffer.readu32(px, i * 4)
buffer.writeu32(out, i * 4, band(v, 0xFF00FF00) + band(bit32.rshift(v, 16), 0xFF) + bit32.lshift(band(v, 0xFF), 16))
end
return out, nil
elseif f == "rgba16f" then
return Texture.toHalf(px, w * h), nil
end
local out = buffer.create(w * h * (BYTES_PF[f] :: number))
buffer.copy(out, 0, px, 0, buffer.len(out))
return out, nil
end
local function writeHeader(out: Bytes.Writer, w: number, h: number, f: string, mips: number, dx10: boolean, layers: number, cube: boolean)
local blockFmt = f == "bc1" or f == "bc3" or f == "bc4" or f == "bc5"
out:string(MAGIC)
out:u32le(124)
local flags = 0x1007 + (if mips > 1 then DDSD_MIPMAPCOUNT else 0) + (if blockFmt then 0x80000 else 0x8)
out:u32le(flags)
out:u32le(h)
out:u32le(w)
out:u32le(if blockFmt then Texture.size(f, w, h) :: number else w * (BYTES_PF[f] :: number))
out:u32le(0)
out:u32le(if mips > 1 then mips else 0)
out:fill(0, 44)
-- DDS_PIXELFORMAT
out:u32le(32)
if dx10 then
out:u32le(DDPF_FOURCC)
out:string("DX10")
out:fill(0, 20)
elseif blockFmt then
out:u32le(DDPF_FOURCC)
out:string(({ bc1 = "DXT1", bc3 = "DXT5", bc4 = "ATI1", bc5 = "ATI2" } :: { [string]: string })[f])
out:fill(0, 20)
else
out:u32le(DDPF_RGB + DDPF_ALPHAPIXELS)
out:u32le(0)
out:u32le(32)
if f == "bgra8" then
out:u32le(0xFF0000)
out:u32le(0xFF00)
out:u32le(0xFF)
else
out:u32le(0xFF)
out:u32le(0xFF00)
out:u32le(0xFF0000)
end
out:u32le(0xFF000000)
end
out:u32le(0x1000 + (if mips > 1 then 0x400008 else 0) + (if cube then 0x8 else 0))
out:u32le(if cube then 0xFE00 else 0)
out:fill(0, 12)
if dx10 then
out:u32le(DX10_OUT[f])
out:u32le(3)
out:u32le(if cube then MISC_CUBE else 0)
out:u32le(if cube then layers // 6 else layers)
out:u32le(0)
end
end
function Dds.encode(img: Types.Image, opts: EncodeOptions?): (buffer?, Err.Err?)
if type(img) ~= "table" or type(img.pixels) ~= "buffer" or type(img.width) ~= "number" or img.width < 1 or img.height < 1 then
return nil, Err.new(Err.ARG, "needs an Image")
end
local pf = img.format
if pf ~= "rgba8" and pf ~= "rgba16" and pf ~= "rgbaf32" then
return nil, Err.new(Err.ARG, "needs an rgba8, rgba16 or rgbaf32 Image (got " .. tostring(pf) .. ")")
end
local f: string = (opts and opts.pixelFormat) or (if pf == "rgba8" then "rgba8" elseif pf == "rgba16" then "rgba16" else "rgba32f")
if not DX10_OUT[f] then
return nil, Err.new(Err.ARG, "unknown pixelFormat " .. tostring(f))
end
local needs = if f == "rgba16" then "rgba16" elseif f == "rgba16f" or f == "rgba32f" then "rgbaf32" else "rgba8"
if pf ~= needs then
return nil, Err.new(Err.ARG, string.format("pixelFormat %s needs an %s Image", f, needs))
end
-- surfaces: the frames (array / cube faces) or the image
local images: { Types.Image } = { img }
if img.frames and #img.frames > 1 then
images = {}
for i, fr in img.frames do
local fi = fr.image
if type(fi) ~= "table" or fi.width ~= img.width or fi.height ~= img.height or fi.format ~= pf then
return nil, Err.new(Err.ARG, "frame " .. i .. ": every surface needs the image's size and pixel format")
end
images[i] = fi
end
end
local cube = if opts and opts.cube ~= nil then opts.cube else (img.meta and img.meta.layout == "cube")
if cube and #images % 6 ~= 0 then
return nil, Err.new(Err.ARG, "a cube map needs 6 faces per layer")
end
local dx10 = (opts and opts.dx10) or f == "rgba16" or f == "rgba16f" or f == "rgba32f" or (#images > 1 and not (cube and #images == 6))
local mips = 1
if opts and opts.mips then
local m = math.max(img.width, img.height)
while m > 1 do
m //= 2
mips += 1
end
end
local out = Bytes.writer(img.width * img.height * 4 * #images + 200)
writeHeader(out, img.width, img.height, f, mips, dx10, #images, cube == true)
for _, im in images do
local level = im
for m = 1, mips do
local data, err = surface(level, f, opts)
if not data then
return nil, err
end
out:bytes(data)
if m < mips then
level = Texture.halve(level)
end
end
end
return out:finish(), nil
end
-- ------------------------------------------------------------------------------------------------ extras
Dds.DXGI = DXGI
return Dds