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.
This commit is contained in:
1 parent
be643a66c8
commit
672948f7d7
8 files changed
+1691
No files matched your search
@@ -26,6 +26,18 @@ local list: { Entry } = {
|
||||
return require("@formats/image/bmp")
|
||||
end,
|
||||
},
|
||||
{
|
||||
name = "dds",
|
||||
category = "image",
|
||||
path = "image/dds",
|
||||
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" } },
|
||||
load = function()
|
||||
return require("@formats/image/dds")
|
||||
end,
|
||||
},
|
||||
{
|
||||
name = "gif",
|
||||
category = "image",
|
||||
|
||||
@@ -0,0 +1,398 @@
|
||||
--!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
|
||||
@@ -0,0 +1,162 @@
|
||||
--!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
|
||||
@@ -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("@formats/core/Err")
|
||||
local Bytes = require("@formats/core/Bytes")
|
||||
local Sniff = require("@formats/core/Sniff")
|
||||
local Types = require("@formats/Types")
|
||||
local Bcn = require("@formats/codecs/image/Bcn")
|
||||
local Texel = require("@formats/codecs/image/Texel")
|
||||
local Texture = require("@formats/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 = {
|
||||
format: ("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.format) 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 format " .. 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("format %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
|
||||
Reference in new issue
Block a user