ws-game: image/vtf (Valve VTF 7.0-7.5 read: every common texel format incl. DXT / ATIxN via codecs/image/Bcn, halves / floats, frames, mips / faces / slices; checked against srctools)
This commit is contained in:
1 parent
7a86e357ed
commit
3d01293b82
6 files changed
+540
No files matched your search
@@ -158,6 +158,18 @@ local list: { Entry } = {
|
||||
return require("@formats/image/tim")
|
||||
end,
|
||||
},
|
||||
{
|
||||
name = "vtf",
|
||||
category = "image",
|
||||
path = "image/vtf",
|
||||
extensions = { "vtf" },
|
||||
mime = { "image/x-vtf" },
|
||||
deps = { "core/Err", "core/Bytes", "Types", "codecs/image/Bcn", "codecs/image/Half" },
|
||||
caps = { decode = true, encode = false, alpha = true, animation = true, versions = { "7.0", "7.1", "7.2", "7.3", "7.4", "7.5" } },
|
||||
load = function()
|
||||
return require("@formats/image/vtf")
|
||||
end,
|
||||
},
|
||||
{
|
||||
name = "webp",
|
||||
category = "image",
|
||||
|
||||
@@ -0,0 +1,111 @@
|
||||
--!strict
|
||||
--!native
|
||||
-- image/vtf/Texel: the uncompressed VTF texel formats to rgba8 (native). Each format is a byte size per texel plus
|
||||
-- where its channels sit: whole bytes (RGBA8888, ABGR8888, BGR888, ...), 16-bit packed fields (RGB565, BGR565,
|
||||
-- BGRA5551, BGRA4444, ...), 16-bit integers / halves and 32-bit floats per channel. Packed fields expand by bit
|
||||
-- replication (5 bits: v << 3 | v >> 2); floats clamp to 0..1. Never throws: the caller checks that the source
|
||||
-- holds w * h texels.
|
||||
local Half = require("@formats/codecs/image/Half")
|
||||
|
||||
local Texel = {}
|
||||
|
||||
local readu8, readu16, readf32, writeu32 = buffer.readu8, buffer.readu16, buffer.readf32, buffer.writeu32
|
||||
local band, rshift = bit32.band, bit32.rshift
|
||||
|
||||
-- kind: "bytes" (offsets of r, g, b, a in the texel; -1 = absent: 0 for colour, 255 for alpha; grey = r copied),
|
||||
-- "packed" (16-bit little-endian: { shift, bits } per channel), "u16" / "f16" / "f32" (channels in order)
|
||||
export type Spec = {
|
||||
size: number,
|
||||
kind: string,
|
||||
at: { number }?, -- bytes: r, g, b, a offsets
|
||||
grey: boolean?, -- bytes: r is copied to g and b
|
||||
fields: { { number } }?, -- packed: { shift, bits } for r, g, b, a (bits 0 = absent)
|
||||
channels: number?, -- u16 / f16 / f32: how many channels are stored
|
||||
bluescreen: boolean?, -- pure blue (0, 0, 255) is transparent
|
||||
}
|
||||
|
||||
local function expand(v: number, bits: number): number
|
||||
if bits == 8 then
|
||||
return v
|
||||
elseif bits == 5 then
|
||||
return v * 8 + v // 4
|
||||
elseif bits == 6 then
|
||||
return v * 4 + v // 16
|
||||
elseif bits == 4 then
|
||||
return v * 17
|
||||
elseif bits == 1 then
|
||||
return v * 255
|
||||
end
|
||||
return (v * 255 + (2 ^ bits - 1) // 2) // (2 ^ bits - 1)
|
||||
end
|
||||
|
||||
local function clamp8(f: number): number
|
||||
if f ~= f or f <= 0 then
|
||||
return 0
|
||||
elseif f >= 1 then
|
||||
return 255
|
||||
end
|
||||
return math.floor(f * 255 + 0.5)
|
||||
end
|
||||
|
||||
-- convert(spec, src, at, w, h, dst): w * h texels at src[at..] -> rgba8 dst
|
||||
function Texel.convert(spec: Spec, src: buffer, at: number, w: number, h: number, dst: buffer)
|
||||
local n = w * h
|
||||
local size = spec.size
|
||||
if spec.kind == "bytes" then
|
||||
local o = spec.at :: { number }
|
||||
local ro, go, bo, ao = o[1], o[2], o[3], o[4]
|
||||
local grey, blue = spec.grey == true, spec.bluescreen == true
|
||||
for i = 0, n - 1 do
|
||||
local p = at + i * size
|
||||
local r = if ro >= 0 then readu8(src, p + ro) else 0
|
||||
local g = if go >= 0 then readu8(src, p + go) else 0
|
||||
local b = if bo >= 0 then readu8(src, p + bo) else 0
|
||||
local a = if ao >= 0 then readu8(src, p + ao) else 255
|
||||
if grey then
|
||||
g, b = r, r
|
||||
end
|
||||
if blue and r == 0 and g == 0 and b == 255 then
|
||||
b, a = 0, 0
|
||||
end
|
||||
writeu32(dst, i * 4, r + g * 256 + b * 65536 + a * 16777216)
|
||||
end
|
||||
elseif spec.kind == "packed" then
|
||||
local f = spec.fields :: { { number } }
|
||||
for i = 0, n - 1 do
|
||||
local v = readu16(src, at + i * 2)
|
||||
local c = { 0, 0, 0, 255 }
|
||||
for k = 1, 4 do
|
||||
local fk = f[k]
|
||||
if fk[2] > 0 then
|
||||
c[k] = expand(band(rshift(v, fk[1]), 2 ^ fk[2] - 1), fk[2])
|
||||
end
|
||||
end
|
||||
writeu32(dst, i * 4, c[1] + c[2] * 256 + c[3] * 65536 + c[4] * 16777216)
|
||||
end
|
||||
else
|
||||
local ch = spec.channels or 4
|
||||
local kind = spec.kind
|
||||
for i = 0, n - 1 do
|
||||
local p = at + i * size
|
||||
local c = { 0, 0, 0, 255 }
|
||||
for k = 1, ch do
|
||||
local v
|
||||
if kind == "u16" then
|
||||
v = (readu16(src, p + (k - 1) * 2) + 128) // 257
|
||||
elseif kind == "f16" then
|
||||
v = clamp8(Half.toFloat(readu16(src, p + (k - 1) * 2)))
|
||||
else
|
||||
v = clamp8(readf32(src, p + (k - 1) * 4))
|
||||
end
|
||||
c[k] = v
|
||||
end
|
||||
if ch == 1 then
|
||||
c[2], c[3] = c[1], c[1]
|
||||
end
|
||||
writeu32(dst, i * 4, c[1] + c[2] * 256 + c[3] * 65536 + c[4] * 16777216)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
return Texel
|
||||
@@ -0,0 +1,242 @@
|
||||
--!strict
|
||||
-- image/vtf: Valve Texture Format (VTF 7.0 - 7.5: Source engine textures), read. Written from the Valve Developer
|
||||
-- Community "VTF (Valve Texture Format)" page; no code taken.
|
||||
-- Decode: the high-resolution image (located by the 7.3+ resource directory or after the low-resolution thumbnail
|
||||
-- for 7.0 - 7.2), mip 0 of slice 0 and face 0 (opts.mip / opts.face / opts.slice pick others), frame 1 as the Image
|
||||
-- and every frame of an animated texture in img.frames (opts.frames ~= false; delay opts.delay, default 0.1 s), to
|
||||
-- rgba8. Texel formats: RGBA8888, ABGR8888, RGB888, BGR888, RGB565, I8, IA88, A8, RGB888 / BGR888 bluescreen (pure
|
||||
-- blue transparent), ARGB8888, BGRA8888, BGRX8888, BGR565, BGRX5551, BGRA5551, BGRA4444, UV88, UVWQ8888, UVLX8888,
|
||||
-- RGBA16161616 (integers) / RGBA16161616F (halves, clamped), R32F, RGB323232F, RGBA32323232F, DXT1 (+ one-bit alpha),
|
||||
-- DXT3, DXT5, ATI1N, ATI2N (ids 35 / 34 since Alien Swarm, 38 / 37 in Source 2013) through codecs/image/Bcn. meta.source = { format, version, flags, frames, faces, depth, mips,
|
||||
-- texel, reflectivity, bumpScale }.
|
||||
-- Not supported: P8 (never implemented by Valve), the newer Strata / Source 2 formats, writing.
|
||||
|
||||
-- ------------------------------------------------------------------------------------------------ requires
|
||||
local Err = require("@formats/core/Err")
|
||||
local Bytes = require("@formats/core/Bytes")
|
||||
local Types = require("@formats/Types")
|
||||
local Bcn = require("@formats/codecs/image/Bcn")
|
||||
local Texel = require("@formats/image/vtf/Texel")
|
||||
|
||||
-- ------------------------------------------------------------------------------------------------ contract
|
||||
local Vtf = {
|
||||
name = "vtf",
|
||||
category = "image",
|
||||
extensions = { "vtf" },
|
||||
mime = { "image/x-vtf" },
|
||||
deps = { "core/Err", "core/Bytes", "Types", "codecs/image/Bcn", "codecs/image/Half" },
|
||||
caps = { decode = true, encode = false, alpha = true, animation = true, versions = { "7.0", "7.1", "7.2", "7.3", "7.4", "7.5" } },
|
||||
}
|
||||
|
||||
-- ------------------------------------------------------------------------------------------------ types
|
||||
export type DecodeOptions = {
|
||||
mip: number?, -- mip level (0 = full size, default)
|
||||
face: number?, -- cube map face (0-based, default 0)
|
||||
slice: number?, -- volume slice (0-based, default 0)
|
||||
frames: boolean?, -- decode every frame into img.frames (default true)
|
||||
delay: number?, -- frame delay in seconds (default 0.1)
|
||||
maxPixels: number?, -- default 2^26
|
||||
}
|
||||
|
||||
-- ------------------------------------------------------------------------------------------------ constants
|
||||
local B = function(size: number, r: number, g: number, b: number, a: number, extra: any?): Texel.Spec
|
||||
local s: any = { size = size, kind = "bytes", at = { r, g, b, a } }
|
||||
if extra then
|
||||
for k, v in extra do
|
||||
s[k] = v
|
||||
end
|
||||
end
|
||||
return s
|
||||
end
|
||||
local P = function(r: { number }, g: { number }, b: { number }, a: { number }): Texel.Spec
|
||||
return { size = 2, kind = "packed", fields = { r, g, b, a } }
|
||||
end
|
||||
-- format id -> texel spec, or a BCn kind
|
||||
local FORMATS: { [number]: any } = {
|
||||
[0] = B(4, 0, 1, 2, 3), -- RGBA8888
|
||||
[1] = B(4, 3, 2, 1, 0), -- ABGR8888
|
||||
[2] = B(3, 0, 1, 2, -1), -- RGB888
|
||||
[3] = B(3, 2, 1, 0, -1), -- BGR888
|
||||
[4] = P({ 0, 5 }, { 5, 6 }, { 11, 5 }, { 0, 0 }), -- RGB565: red in the low bits (as VTFLib / srctools read it)
|
||||
[5] = B(1, 0, -1, -1, -1, { grey = true }), -- I8
|
||||
[6] = B(2, 0, -1, -1, 1, { grey = true }), -- IA88
|
||||
[8] = B(1, -1, -1, -1, 0), -- A8
|
||||
[9] = B(3, 0, 1, 2, -1, { bluescreen = true }), -- RGB888_BLUESCREEN
|
||||
[10] = B(3, 2, 1, 0, -1, { bluescreen = true }), -- BGR888_BLUESCREEN
|
||||
[11] = B(4, 3, 0, 1, 2), -- ARGB8888: VTFLib's layout (G, B, A, R in memory), what the tools write
|
||||
[12] = B(4, 2, 1, 0, 3), -- BGRA8888
|
||||
[13] = "bc1", -- DXT1
|
||||
[14] = "bc2", -- DXT3
|
||||
[15] = "bc3", -- DXT5
|
||||
[16] = B(4, 2, 1, 0, -1), -- BGRX8888
|
||||
[17] = P({ 11, 5 }, { 5, 6 }, { 0, 5 }, { 0, 0 }), -- BGR565: blue in the low bits
|
||||
[18] = P({ 10, 5 }, { 5, 5 }, { 0, 5 }, { 0, 0 }), -- BGRX5551
|
||||
[19] = P({ 8, 4 }, { 4, 4 }, { 0, 4 }, { 12, 4 }), -- BGRA4444
|
||||
[20] = "bc1a", -- DXT1_ONEBITALPHA
|
||||
[21] = P({ 10, 5 }, { 5, 5 }, { 0, 5 }, { 15, 1 }), -- BGRA5551
|
||||
[22] = B(2, 0, 1, -1, -1), -- UV88
|
||||
[23] = B(4, 0, 1, 2, 3), -- UVWQ8888
|
||||
[24] = { size = 8, kind = "f16", channels = 4 }, -- RGBA16161616F
|
||||
[25] = { size = 8, kind = "u16", channels = 4 }, -- RGBA16161616
|
||||
[26] = B(4, 0, 1, 2, 3), -- UVLX8888
|
||||
[27] = { size = 4, kind = "f32", channels = 1 }, -- R32F
|
||||
[28] = { size = 12, kind = "f32", channels = 3 }, -- RGB323232F
|
||||
[29] = { size = 16, kind = "f32", channels = 4 }, -- RGBA32323232F
|
||||
[34] = "bc5", -- ATI2N (Alien Swarm and later)
|
||||
[35] = "bc4", -- ATI1N (Alien Swarm and later)
|
||||
[37] = "bc5", -- ATI2N (Source 2013)
|
||||
[38] = "bc4", -- ATI1N (Source 2013)
|
||||
}
|
||||
local NAMES: { [number]: string } = { [0] = "RGBA8888", "ABGR8888", "RGB888", "BGR888", "RGB565", "I8", "IA88", "P8", "A8", "RGB888_BLUESCREEN",
|
||||
"BGR888_BLUESCREEN", "ARGB8888", "BGRA8888", "DXT1", "DXT3", "DXT5", "BGRX8888", "BGR565", "BGRX5551", "BGRA4444",
|
||||
"DXT1_ONEBITALPHA", "BGRA5551", "UV88", "UVWQ8888", "RGBA16161616F", "RGBA16161616", "UVLX8888", "R32F", "RGB323232F",
|
||||
"RGBA32323232F", "NV_DST16", "NV_DST24", "NV_INTZ", "NV_RAWZ", "ATI2N", "ATI1N", "NV_NULL", "ATI2N", "ATI1N" }
|
||||
local ENVMAP = 0x4000
|
||||
|
||||
-- ------------------------------------------------------------------------------------------------ sniff
|
||||
function Vtf.sniff(r: Bytes.Reader): number
|
||||
return if r:peek(4) == "VTF\0" and r:peekU8(4) == 7 then 1 else 0
|
||||
end
|
||||
|
||||
-- ------------------------------------------------------------------------------------------------ decode
|
||||
-- bytes of one w x h image in format f
|
||||
local function imageSize(f: any, w: number, h: number): number
|
||||
if type(f) == "string" then
|
||||
local kind = if f == "bc1a" then "bc1" else f
|
||||
return Bcn.size(kind, w, h) or 0
|
||||
end
|
||||
return w * h * f.size
|
||||
end
|
||||
|
||||
function Vtf.decode(r: Bytes.Reader, opts: DecodeOptions?): (Types.Image?, Err.Err?)
|
||||
local size = r.limit - r.base
|
||||
local b, base = r.buf, r.base
|
||||
if size < 64 then
|
||||
return nil, Err.new(Err.TRUNCATED, "header", 0)
|
||||
end
|
||||
if buffer.readstring(b, base, 4) ~= "VTF\0" then
|
||||
return nil, Err.new(Err.CORRUPT, "bad magic", 0)
|
||||
end
|
||||
local major, minor = buffer.readu32(b, base + 4), buffer.readu32(b, base + 8)
|
||||
if major ~= 7 or minor > 5 then
|
||||
return nil, Err.new(Err.UNSUPPORTED, "VTF version " .. major .. "." .. minor, 4)
|
||||
end
|
||||
local headerSize = buffer.readu32(b, base + 12)
|
||||
local w, h = buffer.readu16(b, base + 16), buffer.readu16(b, base + 18)
|
||||
local flags = buffer.readu32(b, base + 20)
|
||||
local frames, firstFrame = buffer.readu16(b, base + 24), buffer.readu16(b, base + 26)
|
||||
local refl = { buffer.readf32(b, base + 32), buffer.readf32(b, base + 36), buffer.readf32(b, base + 40) }
|
||||
local bump = buffer.readf32(b, base + 48)
|
||||
local fmtId = buffer.readi32(b, base + 52)
|
||||
local mips = buffer.readu8(b, base + 56)
|
||||
local lowFmt = buffer.readi32(b, base + 57)
|
||||
local lowW, lowH = buffer.readu8(b, base + 61), buffer.readu8(b, base + 62)
|
||||
local depth = if minor >= 2 and size >= 65 then math.max(1, buffer.readu16(b, base + 63)) else 1
|
||||
if w == 0 or h == 0 then
|
||||
return nil, Err.new(Err.CORRUPT, "zero size", 16)
|
||||
end
|
||||
if w * h > (opts and opts.maxPixels or 2 ^ 26) then
|
||||
return nil, Err.new(Err.LIMIT, "image too big")
|
||||
end
|
||||
local f: any = FORMATS[fmtId]
|
||||
if not f then
|
||||
return nil, Err.new(Err.UNSUPPORTED, "texel format " .. (NAMES[fmtId] or tostring(fmtId)), 52)
|
||||
end
|
||||
frames = math.max(1, frames)
|
||||
mips = math.max(1, mips)
|
||||
local faces = 1
|
||||
if bit32.band(flags, ENVMAP) ~= 0 then
|
||||
faces = if minor < 5 and firstFrame ~= 0xFFFF then 7 else 6
|
||||
end
|
||||
-- where the high-resolution data starts
|
||||
local dataAt: number? = nil
|
||||
if minor >= 3 then
|
||||
if size < 80 then
|
||||
return nil, Err.new(Err.TRUNCATED, "resource directory", 68)
|
||||
end
|
||||
local nres = buffer.readu32(b, base + 68)
|
||||
if nres > 32 or 80 + nres * 8 > size then
|
||||
return nil, Err.new(Err.CORRUPT, "bad resource count", 68)
|
||||
end
|
||||
for i = 0, nres - 1 do
|
||||
local o = base + 80 + i * 8
|
||||
if buffer.readu8(b, o) == 0x30 and buffer.readu8(b, o + 1) == 0 and buffer.readu8(b, o + 2) == 0 then
|
||||
dataAt = buffer.readu32(b, o + 4)
|
||||
end
|
||||
end
|
||||
if not dataAt then
|
||||
return nil, Err.new(Err.CORRUPT, "no high-resolution image resource", 80)
|
||||
end
|
||||
else
|
||||
local lf = FORMATS[lowFmt]
|
||||
local lowSize = if lowFmt >= 0 and lowW > 0 and lowH > 0 and lf then imageSize(lf, lowW, lowH) else 0
|
||||
dataAt = headerSize + lowSize
|
||||
end
|
||||
-- the chain: mips smallest first; per mip: frames, faces, slices
|
||||
local mip = math.clamp(math.floor(opts and opts.mip or 0), 0, mips - 1)
|
||||
local face = math.clamp(math.floor(opts and opts.face or 0), 0, faces - 1)
|
||||
local slice = math.clamp(math.floor(opts and opts.slice or 0), 0, depth - 1)
|
||||
local off = dataAt :: number
|
||||
for m = mips - 1, mip + 1, -1 do
|
||||
local mw, mh, md = math.max(1, w // 2 ^ m), math.max(1, h // 2 ^ m), math.max(1, depth // 2 ^ m)
|
||||
off += imageSize(f, mw, mh) * md * faces * frames
|
||||
end
|
||||
local mw, mh, md = math.max(1, w // 2 ^ mip), math.max(1, h // 2 ^ mip), math.max(1, depth // 2 ^ mip)
|
||||
local one = imageSize(f, mw, mh)
|
||||
local function image(frame: number): (Types.Image?, Err.Err?)
|
||||
local at = off + ((frame * faces + face) * md + math.min(slice, md - 1)) * one
|
||||
if at + one > size or at < 0 then
|
||||
return nil, Err.new(Err.TRUNCATED, "image data past the end", at)
|
||||
end
|
||||
local img = Types.Image.new(mw, mh)
|
||||
if type(f) == "string" then
|
||||
local kind = if f == "bc1a" then "bc1" else f
|
||||
local px, e = Bcn.decode(kind, b, base + at, mw, mh)
|
||||
if not px then
|
||||
return nil, Err.from(e)
|
||||
end
|
||||
buffer.copy(img.pixels, 0, px, 0, mw * mh * 4)
|
||||
if f == "bc1" then
|
||||
-- plain DXT1 carries no alpha: the transparent colour of 3-colour blocks reads as opaque black
|
||||
for i = 0, mw * mh - 1 do
|
||||
buffer.writeu8(img.pixels, i * 4 + 3, 255)
|
||||
end
|
||||
end
|
||||
else
|
||||
Texel.convert(f, b, base + at, mw, mh, img.pixels)
|
||||
end
|
||||
return img, nil
|
||||
end
|
||||
local img, err = image(0)
|
||||
if not img then
|
||||
return nil, err
|
||||
end
|
||||
if frames > 1 and not (opts and opts.frames == false) then
|
||||
local delay = opts and opts.delay or 0.1
|
||||
local list: { Types.Frame } = { { image = img, delay = delay, x = 0, y = 0, dispose = "none", blend = "source" } :: any }
|
||||
for fr = 1, frames - 1 do
|
||||
local fi, ferr = image(fr)
|
||||
if not fi then
|
||||
return nil, ferr
|
||||
end
|
||||
table.insert(list, { image = fi, delay = delay, x = 0, y = 0, dispose = "none", blend = "source" } :: any)
|
||||
end
|
||||
img.frames = list
|
||||
img.loops = 0
|
||||
end
|
||||
img.meta.source = {
|
||||
format = "vtf",
|
||||
version = major .. "." .. minor,
|
||||
flags = flags,
|
||||
frames = frames,
|
||||
faces = faces,
|
||||
depth = depth,
|
||||
mips = mips,
|
||||
texel = NAMES[fmtId],
|
||||
reflectivity = refl,
|
||||
bumpScale = bump,
|
||||
}
|
||||
return img, nil
|
||||
end
|
||||
|
||||
return Vtf
|
||||
Reference in new issue
Block a user