ws-game: image/vtf decodes its uncompressed texel formats through the shared codecs/image/Texel (image/vtf/Texel removed); packed 5/6-bit formats within 1 of srctools' bit replication

This commit is contained in:
4DBug committed 2026-10-31 15:53:12 -05:00
1 parent 43df95f6e0
commit 862f8e459a
4 files changed
+55 -161

No files matched your search

+1 -1
View File
@@ -272,7 +272,7 @@ local list: { Entry } = {
path = "image/vtf",
extensions = { "vtf" },
mime = { "image/x-vtf" },
deps = { "core/Err", "core/Bytes", "Types", "codecs/image/Bcn", "codecs/image/Half" },
deps = { "core/Err", "core/Bytes", "Types", "codecs/image/Bcn", "codecs/image/Texel" },
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")
-111
View File
@@ -1,111 +0,0 @@
--!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
+50 -47
View File
@@ -6,7 +6,8 @@
-- 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),
-- RGBA16161616 (integers) / RGBA16161616F (halves, clamped), R32F, RGB323232F, RGBA32323232F (all through
-- codecs/image/Texel), 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.
@@ -16,7 +17,7 @@ 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")
local Texel = require("@formats/codecs/image/Texel")
-- ------------------------------------------------------------------------------------------------ contract
local Vtf = {
@@ -24,7 +25,7 @@ local Vtf = {
category = "image",
extensions = { "vtf" },
mime = { "image/x-vtf" },
deps = { "core/Err", "core/Bytes", "Types", "codecs/image/Bcn", "codecs/image/Half" },
deps = { "core/Err", "core/Bytes", "Types", "codecs/image/Bcn", "codecs/image/Texel" },
caps = { decode = true, encode = false, alpha = true, animation = true, versions = { "7.0", "7.1", "7.2", "7.3", "7.4", "7.5" } },
}
@@ -39,54 +40,44 @@ export type DecodeOptions = {
}
-- ------------------------------------------------------------------------------------------------ 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 M = Texel.masked
-- format id -> a codecs/image/Texel format (name or bit-mask descriptor) | { texel, bluescreen = true } | 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
[0] = "rgba8", -- RGBA8888
[1] = M(4, 0xFF000000, 0x00FF0000, 0x0000FF00, 0x000000FF), -- ABGR8888
[2] = "rgb8", -- RGB888
[3] = "bgr8", -- BGR888
[4] = "r5g6b5", -- RGB565: red in the low bits (as VTFLib / srctools read it)
[5] = "l8", -- I8
[6] = "la8", -- IA88
[8] = "a8", -- A8
[9] = { texel = "rgb8", bluescreen = true }, -- RGB888_BLUESCREEN
[10] = { texel = "bgr8", bluescreen = true }, -- BGR888_BLUESCREEN
[11] = M(4, 0xFF000000, 0x000000FF, 0x0000FF00, 0x00FF0000), -- ARGB8888: VTFLib's layout (G, B, A, R in memory)
[12] = "bgra8", -- 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
[16] = "bgrx8", -- BGRX8888
[17] = "b5g6r5", -- BGR565: blue in the low bits
[18] = "b5g5r5x1", -- BGRX5551
[19] = "b4g4r4a4", -- 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
[21] = "b5g5r5a1", -- BGRA5551
[22] = "rg8", -- UV88
[23] = "rgba8", -- UVWQ8888
[24] = "rgba16f", -- RGBA16161616F
[25] = "rgba16", -- RGBA16161616
[26] = "rgba8", -- UVLX8888
[27] = "r32f", -- R32F
[28] = "rgb32f", -- RGB323232F
[29] = "rgba32f", -- 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 BCN: { [string]: boolean } = { bc1 = true, bc1a = true, bc2 = true, bc3 = true, bc4 = true, bc5 = true }
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",
@@ -101,11 +92,10 @@ 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
if type(f) == "string" and BCN[f] then
return Bcn.size(if f == "bc1a" then "bc1" else f, w, h) or 0
end
return w * h * f.size
return Texel.size(if type(f) == "table" and f.texel then f.texel else f, w, h) or 0
end
function Vtf.decode(r: Bytes.Reader, opts: DecodeOptions?): (Types.Image?, Err.Err?)
@@ -189,7 +179,7 @@ function Vtf.decode(r: Bytes.Reader, opts: DecodeOptions?): (Types.Image?, Err.E
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
if type(f) == "string" and BCN[f] 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
@@ -203,7 +193,20 @@ function Vtf.decode(r: Bytes.Reader, opts: DecodeOptions?): (Types.Image?, Err.E
end
end
else
Texel.convert(f, b, base + at, mw, mh, img.pixels)
local blue = type(f) == "table" and f.bluescreen == true
local px, e = Texel.decode(if blue then f.texel else f, 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 blue then
-- pure blue is the transparent key
for i = 0, mw * mh - 1 do
if buffer.readu32(img.pixels, i * 4) == 0xFFFF0000 then
buffer.writeu32(img.pixels, i * 4, 0)
end
end
end
end
return img, nil
end
+4 -2
View File
@@ -45,7 +45,9 @@ for i, name in enumerate(FMTS):
print(json.dumps(out))
"""
TOL = {"DXT1": 8, "DXT3": 8, "DXT5": 8, "DXT1_ONEBITALPHA": 8, "ATI1N": 2, "ATI2N": 2, "RGBA16161616F": 1, "RGBA16161616": 1}
# codecs/image/Texel rescales 5 / 6-bit fields with rounding (round(v * 255 / 31)); srctools replicates bits
# (v << 3 | v >> 2): within 1
TOL = {"RGB565": 1, "BGR565": 1, "BGRX5551": 1, "BGRA5551": 1, "DXT1": 8, "DXT3": 8, "DXT5": 8, "DXT1_ONEBITALPHA": 8, "ATI1N": 2, "ATI2N": 2, "RGBA16161616F": 1, "RGBA16161616": 1}
def own(rng):
@@ -80,7 +82,7 @@ def own(rng):
px[1] = px[2] = px[0]
rgba += bytes(px)
data = header(fmt) + struct.pack("<%d%s" % (len(vals), kind), *vals)
out.append({"name": name, "data": data, "width": w, "height": h, "frames": 1, "version": "7.2", "rgba": bytes(rgba), "tol": 0, "mips": 1})
out.append({"name": name, "data": data, "width": w, "height": h, "frames": 1, "version": "7.2", "rgba": bytes(rgba), "tol": 1 if kind == "H" else 0, "mips": 1})
return out