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
@@ -17,6 +17,7 @@ is tested under both runtimes (luau `--codegen -O2` and LuaJIT through the build
|
||||
| image | tga | .tga .icb .vda .vst .tpic | yes | yes | alpha: true, palette: true, depths: 8, 15, 16, 24, 32 | `tests/image/tga.luau` |
|
||||
| image | tiff | .tif .tiff | yes | yes | alpha: true, pages: true, depths: 1, 2, 4, 8, 16, 32, float: true | `tests/image/tiff.luau` |
|
||||
| image | tim | .tim | yes | yes | alpha: true, palette: true, depths: 4, 8, 16, 24 | `tests/image/tim.luau` |
|
||||
| image | vtf | .vtf | yes | - | alpha: true, animation: true, versions: "7.0", "7.1", "7.2", "7.3", "7.4", "7.5" | `tests/image/vtf.luau` |
|
||||
| image | webp | .webp | yes | yes | animation: true, alpha: true, lossy: true, lossless: true, depths: 8 | `tests/image/webp.luau` |
|
||||
| audio | aac | .aac .adts | yes | - | codecs: "aac", streaming: true | `tests/audio/aac.luau` |
|
||||
| audio | aiff | .aiff .aif .aifc .afc | yes | yes | streaming: true, depths: 8, 16, 24, 32, float: true, lossy: false | `tests/audio/aiff.luau` |
|
||||
|
||||
@@ -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
|
||||
@@ -0,0 +1,74 @@
|
||||
-- image/vtf tests: VTFs written by srctools (tools/fixtures/vtf.py: every texel format it writes, versions 7.2 / 7.4
|
||||
-- / 7.5, mips, two-frame animations) decode to srctools' own RGBA (DXT within its libsquish rounding), frames, the
|
||||
-- mip / thumbnail layout; the half / float / 16-bit formats srctools cannot write against the fixture writer; errors;
|
||||
-- the fuzz pass.
|
||||
local T = require("@tests/T")
|
||||
local F = require("@formats")
|
||||
local Vtf = require("@formats/image/vtf")
|
||||
local Bytes = require("@formats/core/Bytes")
|
||||
local Err = require("@formats/core/Err")
|
||||
local Fx = require("@tests/gen/vtf")
|
||||
|
||||
local t = T.suite("vtf")
|
||||
t:eq(Vtf.name, "vtf", "name")
|
||||
|
||||
local function compare(px: buffer, want: string, tol: number): (number, number)
|
||||
local worst, at = 0, -1
|
||||
for i = 0, #want - 1 do
|
||||
local d = math.abs(buffer.readu8(px, i) - string.byte(want, i + 1))
|
||||
if d > worst then
|
||||
worst, at = d, i
|
||||
end
|
||||
end
|
||||
return worst, at
|
||||
end
|
||||
|
||||
local exact = 0
|
||||
for _, c in Fx do
|
||||
local img, err = Vtf.decode(Bytes.reader(buffer.fromstring(c.data)))
|
||||
t:check(img, c.name .. ": " .. tostring(err))
|
||||
t:eq(F.detect(c.data), "vtf", c.name .. ": F.detect")
|
||||
t:check(img.width == c.width and img.height == c.height, c.name .. ": size")
|
||||
local worst, at = compare(img.pixels, c.rgba, c.tol)
|
||||
t:check(worst <= c.tol, string.format("%s (%s): within %d of srctools (worst %d at byte %d)", c.name, c.version, c.tol, worst, at))
|
||||
if worst == 0 then
|
||||
exact += 1
|
||||
end
|
||||
t:eq(img.meta.source.texel, c.name, c.name .. ": texel format")
|
||||
t:check(img.meta.source.mips > 1 or c.mips == 1, c.name .. ": mips counted")
|
||||
if c.frames > 1 then
|
||||
t:eq(#img.frames, c.frames, c.name .. ": frames")
|
||||
local lw = compare(img.frames[c.frames].image.pixels, c.last, c.tol)
|
||||
t:check(lw <= c.tol, c.name .. ": last frame")
|
||||
end
|
||||
if c.mips ~= 1 then
|
||||
local small = Vtf.decode(Bytes.reader(buffer.fromstring(c.data)), { mip = 1 }) :: any
|
||||
t:check(small and small.width == math.max(1, c.width // 2), c.name .. ": mip 1")
|
||||
end
|
||||
end
|
||||
t:note("%d of %d files exactly equal to their reference (srctools or the fixture writer)", exact, #Fx)
|
||||
|
||||
do
|
||||
local c = Fx[1]
|
||||
local _, e1 = Vtf.decode(Bytes.reader(buffer.fromstring(string.sub(c.data, 1, #c.data - 20))))
|
||||
t:check(e1 and e1.code == Err.TRUNCATED, "truncated: " .. tostring(e1))
|
||||
local bad = buffer.fromstring(c.data)
|
||||
buffer.writeu32(bad, 8, 9)
|
||||
local _, e2 = Vtf.decode(Bytes.reader(bad))
|
||||
t:check(e2 and e2.code == Err.UNSUPPORTED, "version 7.9")
|
||||
local p8 = buffer.fromstring(c.data)
|
||||
buffer.writei32(p8, 52, 7)
|
||||
local _, e3 = Vtf.decode(Bytes.reader(p8))
|
||||
t:check(e3 and e3.code == Err.UNSUPPORTED, "P8")
|
||||
end
|
||||
|
||||
local samples = {}
|
||||
for i, c in Fx do
|
||||
if i % 3 == 0 then
|
||||
table.insert(samples, c.data)
|
||||
end
|
||||
end
|
||||
t:fuzz("decode", function(b: buffer)
|
||||
return Vtf.decode(Bytes.reader(b))
|
||||
end, samples, { mutations = 30 })
|
||||
t:done()
|
||||
@@ -0,0 +1,100 @@
|
||||
# Valve VTF fixtures (tools/fixtures.py). srctools (nix python3Packages.srctools: a VTF reader / writer with
|
||||
# libsquish for DXT) writes VTFs of random pixels in every texel format it supports, in versions 7.2 / 7.4 / 7.5,
|
||||
# with mips and several frames; its reader decodes them back -> the RGBA of frame 0, mip 0 (and the last frame) as
|
||||
# reference. Formats whose conversion differs from the DXT rule (srctools / libsquish round 5 / 6-bit endpoints
|
||||
# differently) are compared within a tolerance.
|
||||
# tests/gen/vtf.luau { { name, data, width, height, frames, version, fmt, rgba, last?, tol } }
|
||||
import json
|
||||
|
||||
CODE = r"""
|
||||
import io, json, random, sys
|
||||
import srctools.vtf as V
|
||||
args = json.loads(sys.stdin.read())
|
||||
rng = random.Random(args["seed"])
|
||||
out = []
|
||||
FMTS = ["RGBA8888", "ABGR8888", "RGB888", "BGR888", "RGB565", "I8", "IA88", "A8", "RGB888_BLUESCREEN", "BGR888_BLUESCREEN",
|
||||
"ARGB8888", "BGRA8888", "DXT1", "DXT3", "DXT5", "BGRX8888", "BGR565", "BGRX5551", "BGRA4444", "DXT1_ONEBITALPHA",
|
||||
"BGRA5551", "UV88", "UVWQ8888", "RGBA16161616F", "RGBA16161616", "UVLX8888", "ATI1N", "ATI2N"]
|
||||
for i, name in enumerate(FMTS):
|
||||
fmt = V.ImageFormats[name]
|
||||
version = [(7, 2), (7, 4), (7, 5)][i % 3]
|
||||
w, h = [(16, 8), (8, 8), (4, 16)][i % 3] if not name.startswith(("DXT", "ATI")) else (16, 8)
|
||||
frames = 2 if i % 4 == 0 else 1
|
||||
try:
|
||||
vtf = V.VTF(w, h, version, fmt=fmt, frames=frames)
|
||||
for f in range(frames):
|
||||
px = bytes(rng.randrange(256) for _ in range(w * h * 4))
|
||||
if "BLUESCREEN" in name:
|
||||
px = bytearray(px)
|
||||
for k in range(0, len(px), 16):
|
||||
px[k:k + 4] = b"\x00\x00\xff\xff"
|
||||
px = bytes(px)
|
||||
vtf.get(frame=f).copy_from(px)
|
||||
b = io.BytesIO()
|
||||
vtf.save(b)
|
||||
data = b.getvalue()
|
||||
back = V.VTF.read(io.BytesIO(data))
|
||||
back.load()
|
||||
rgba = bytes(back.get(frame=0)._data)
|
||||
e = {"name": name, "data": data.hex(), "width": w, "height": h, "frames": frames, "version": "%d.%d" % version, "rgba": rgba.hex()}
|
||||
if frames > 1:
|
||||
e["last"] = bytes(back.get(frame=frames - 1)._data).hex()
|
||||
out.append(e)
|
||||
except Exception as ex:
|
||||
out.append({"name": name, "failed": repr(ex)})
|
||||
print(json.dumps(out))
|
||||
"""
|
||||
|
||||
TOL = {"DXT1": 8, "DXT3": 8, "DXT5": 8, "DXT1_ONEBITALPHA": 8, "ATI1N": 2, "ATI2N": 2, "RGBA16161616F": 1, "RGBA16161616": 1}
|
||||
|
||||
|
||||
def own(rng):
|
||||
# 7.2 files written here for the formats srctools cannot write (one mip, no thumbnail); expected RGBA by the
|
||||
# documented rules: halves / floats clamped to 0..1 and rounded, 16-bit integers (v + 128) // 257
|
||||
import struct, math
|
||||
out = []
|
||||
w, h = 4, 4
|
||||
def header(fmt):
|
||||
hd = bytearray(80)
|
||||
struct.pack_into("<4sIIIHHIHH", hd, 0, b"VTF\0", 7, 2, 80, w, h, 0, 1, 0)
|
||||
struct.pack_into("<f", hd, 48, 1.0)
|
||||
struct.pack_into("<iBiBBH", hd, 52, fmt, 1, -1, 0, 0, 1)
|
||||
return bytes(hd)
|
||||
def c8(f):
|
||||
return 0 if f != f or f <= 0 else 255 if f >= 1 else math.floor(f * 255 + 0.5)
|
||||
for name, fmt, kind, ch in (("RGBA16161616F", 24, "e", 4), ("RGBA16161616", 25, "H", 4), ("R32F", 27, "f", 1),
|
||||
("RGB323232F", 28, "f", 3), ("RGBA32323232F", 29, "f", 4)):
|
||||
vals, rgba = [], bytearray()
|
||||
for i in range(w * h):
|
||||
px = [0, 0, 0, 255]
|
||||
for k in range(ch):
|
||||
if kind == "H":
|
||||
v = rng.randrange(65536)
|
||||
px[k] = (v + 128) // 257
|
||||
else:
|
||||
v = rng.uniform(-0.5, 1.5)
|
||||
v = struct.unpack("<" + kind, struct.pack("<" + kind, v))[0]
|
||||
px[k] = c8(v)
|
||||
vals.append(v)
|
||||
if ch == 1:
|
||||
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})
|
||||
return out
|
||||
|
||||
|
||||
def generate(ctx):
|
||||
res = json.loads(ctx.python(["srctools", "pillow"], CODE, input=json.dumps({"seed": 7}).encode()))
|
||||
out = []
|
||||
for e in res:
|
||||
if "failed" in e:
|
||||
print("fixtures: vtf: srctools cannot write %s: %s" % (e["name"], e["failed"]))
|
||||
continue
|
||||
c = {"name": e["name"], "data": bytes.fromhex(e["data"]), "width": e["width"], "height": e["height"], "frames": e["frames"],
|
||||
"version": e["version"], "rgba": bytes.fromhex(e["rgba"]), "tol": TOL.get(e["name"], 0)}
|
||||
if "last" in e:
|
||||
c["last"] = bytes.fromhex(e["last"])
|
||||
out.append(c)
|
||||
out += own(ctx.rng(9))
|
||||
ctx.emit("vtf", out)
|
||||
Reference in new issue
Block a user