archive/wad2, image/lmp, image/spr, data/q3shader, data/q3skin

WAD2 / WAD3 with miptex / qpic / conchars / font / palette images and addImage; Quake .lmp pictures, palette and
colormap (palette never embedded); Quake / Half-Life / SPR32 sprites with frame groups; Quake III shader scripts and
.skin files. Miptex gains qpic decode / encode and palette mapping; Pixels.keyed keeps the key colour (alpha 0).
This commit is contained in:
4DBug committed 2026-11-01 19:17:00 -06:00
1 parent 67e116a6c6
commit 175ca9e823
23 files changed
+5380 -1143

No files matched your search

+95
View File
@@ -0,0 +1,95 @@
-- archive/wad2 tests: WAD2 / WAD3 lumps, miptex / qpic / conchars / font / palette images against pixels decoded in
-- the fixture, ericw-tools qbsp's copy of a miptex, write round trips, adding images, errors, the fuzz pass.
-- Fixtures: tools/fixtures/wad2.py.
local T = require("@tests/T")
local F = require("@rosetta")
local Wad2 = require("@rosetta/archive/wad2")
local Miptex = require("@rosetta/codecs/image/Miptex")
local Bytes = require("@rosetta/core/Bytes")
local Crc32 = require("@rosetta/core/Crc32")
local Err = require("@rosetta/core/Err")
local Fx = require("@tests/gen/wad2")
local t = T.suite("wad2")
t:eq(Wad2.name, "wad2", "name")
for _, v in { "wad2", "wad3" } do
local c = Fx[v]
t:eq(F.detect(c.data), "wad2", v .. ": F.detect")
local a, err = Wad2.decode(Bytes.reader(buffer.fromstring(c.data)))
t:check(a, v .. ": " .. tostring(err))
t:eq(a.meta.source.variant, v, v .. ": variant")
local names = {}
for _, e in a:list() do
table.insert(names, e.name)
end
t:eq(table.concat(names, ","), table.concat(c.names, ","), v .. ": lump names in order")
for name, want in c.images do
local img, ierr = Wad2.image(a, name)
t:check(img, v .. ": image " .. name .. ": " .. tostring(ierr))
t:eq(img.width .. "x" .. img.height, want.w .. "x" .. want.h, v .. ": " .. name .. " size")
t:eq(Crc32.of(img.pixels), want.crc, v .. ": " .. name .. " pixels")
t:eq(img.meta.source.kind, want.kind, v .. ": " .. name .. " kind")
if want.mip2 then
local m2 = assert(Wad2.image(a, name, { mip = 2 }))
t:eq(Crc32.of(m2.pixels), want.mip2, v .. ": " .. name .. " mip 2")
end
end
-- write: the same lumps back
local out = assert(F.save(a))
local b = assert(Wad2.decode(Bytes.reader(out)))
t:eq(b.meta.source.variant, v, v .. ": variant kept")
for _, e in a:list() do
t:check(buffer.tostring(assert(a:read(e.name))) == buffer.tostring(assert(b:read(e.name))), v .. ": round trip " .. e.name)
t:eq((b:list()[1] :: any).type, (a:list()[1] :: any).type, v .. ": lump types kept")
end
end
do
local a = assert(Wad2.decode(Bytes.reader(buffer.fromstring(Fx.wad2.data))))
t:eq(Wad2.image(a, "palette").width, 16, "palette lump as a 16 x 16 image")
t:check(Wad2.palette(a) ~= nil, "the WAD's palette")
-- an explicit palette wins over the WAD's
local grey = string.rep("\0", 768)
local g = assert(Wad2.image(a, "brick", { palette = grey }))
t:eq(buffer.readu32(g.pixels, 0), 0xFF000000, "explicit palette")
-- qbsp copied brick into the BSP: same pixels
local m = assert(Miptex.decode(buffer.fromstring(Fx.bsp.miptex), 0, #Fx.bsp.miptex, { palette = Wad2.palette(a) }))
t:eq(Crc32.of(m.pixels), Fx.wad2.images.brick.crc, "qbsp's copy of the miptex")
-- adding images: palette colours map back exactly
local brick = assert(Wad2.image(a, "brick"))
local n = Wad2.new("wad2")
t:check(Wad2.addImage(n, "copy", brick, { palette = Fx.wad2.palette }), "addImage miptex")
t:check(Wad2.addImage(n, "pic", brick, { palette = Fx.wad2.palette, kind = "qpic" }), "addImage qpic")
local _, e1 = Wad2.addImage(n, "odd", F.ops.image.new(10, 10), { palette = Fx.wad2.palette })
t:check(e1 and e1.code == Err.ARG, "miptex sizes: multiples of 16")
local _, e2 = Wad2.addImage(n, "nopal", brick)
t:check(e2 and e2.code == Err.ARG, "WAD2 needs a palette")
local nb = assert(Wad2.decode(Bytes.reader(assert(F.save(n)))))
t:eq(Crc32.of(assert(Wad2.image(nb, "copy", { palette = Fx.wad2.palette })).pixels), Fx.wad2.images.brick.crc, "added miptex decodes the same")
t:eq(Crc32.of(assert(Wad2.image(nb, "pic", { palette = Fx.wad2.palette, keyed = false })).pixels), Fx.wad2.images.brick.crc, "added qpic decodes the same")
-- WAD3: its own palette from median cut (at most 256 colours: exact)
local w3 = assert(Wad2.decode(Bytes.reader(buffer.fromstring(Fx.wad3.data))))
local crete = assert(Wad2.image(w3, "crete"))
local n3 = Wad2.new("wad3")
t:check(Wad2.addImage(n3, "CRETE2", crete), "addImage into WAD3")
local d3 = assert(Wad2.decode(Bytes.reader(assert(F.save(n3)))))
t:eq(d3.meta.source.variant, "wad3", "WAD3 written")
t:eq(Crc32.of(assert(Wad2.image(d3, "CRETE2")).pixels), Fx.wad3.images.CRETE.crc, "WAD3 miptex with a made palette")
t:check(assert(Wad2.image(w3, "CONCHARS")).meta.font.glyphs[66].width == 8, "font glyph table")
local _, e3 = Wad2.image(w3, "missing")
t:check(e3 and e3.code == Err.ARG, "missing lump")
local _, e4 = Wad2.decode(Bytes.reader(buffer.fromstring(string.sub(Fx.wad2.data, 1, 20))))
t:check(e4 and e4.code == Err.TRUNCATED, "truncated directory: " .. tostring(e4))
end
t:fuzz("decode", function(b: buffer)
local a = Wad2.decode(Bytes.reader(b))
if a then
for _, e in a:list() do
Wad2.image(a, e.name)
end
end
return a
end, { Fx.wad2.data, Fx.wad3.data }, { mutations = 80 })
t:done()
+57
View File
@@ -0,0 +1,57 @@
-- data/q3shader tests: shader scripts against an independent tokenizer (tools/fixtures/q3shader.py), write round
-- trips, lookups, errors, the fuzz pass.
local T = require("@tests/T")
local F = require("@rosetta")
local Q3Shader = require("@rosetta/data/q3shader")
local Bytes = require("@rosetta/core/Bytes")
local Err = require("@rosetta/core/Err")
local Fx = require("@tests/gen/q3shader")
local t = T.suite("q3shader")
t:eq(Q3Shader.name, "q3shader", "name")
local function dump(v: any): string
if type(v) ~= "table" then
return tostring(v)
end
local parts = {}
if v.name then
table.insert(parts, "name=" .. v.name)
table.insert(parts, "d=" .. dump(v.directives))
table.insert(parts, "s=" .. dump(v.stages))
elseif v.surface then
table.insert(parts, v.surface .. "=" .. v.shader)
else
for _, x in v do
table.insert(parts, dump(x))
end
end
return "{" .. table.concat(parts, "|") .. "}"
end
local samples = {}
for i, c in Fx.scripts do
t:eq(F.detect(c.text), "q3shader", "script " .. i .. ": F.detect")
local list, err = Q3Shader.decode(Bytes.reader(buffer.fromstring(c.text)))
t:check(list, "script " .. i .. ": " .. tostring(err))
t:eq(dump(list), dump(c.shaders), "script " .. i .. ": structure")
local out = assert(Q3Shader.encode(list :: any))
t:eq(dump(Q3Shader.decode(Bytes.reader(out))), dump(c.shaders), "script " .. i .. ": round trip")
table.insert(samples, c.text)
end
do
local list = assert(Q3Shader.decode(Bytes.reader(buffer.fromstring(Fx.scripts[2].text))))
local sky = assert(Q3Shader.find(list, "TEXTURES/SKIES/HELLSKY"))
t:eq(assert(Q3Shader.directive(sky.directives, "skyparms"))[2], "env/hell sky", "quoted token")
t:eq(#sky.stages, 1, "stage on brace lines")
local again = assert(F.data.load(Fx.scripts[1].text))
t:eq(#again, 2, "F.data.load")
local _, e1 = Q3Shader.decode(Bytes.reader(buffer.fromstring("textures/x\n{\n{\nmap a\n")))
t:check(e1 and e1.code == Err.TRUNCATED, "unclosed shader")
local _, e2 = Q3Shader.encode({ { name = "has space", directives = {}, stages = {} } } :: any)
t:check(e2 and e2.code == Err.ARG, "bad name")
end
t:fuzz("decode", function(b: buffer)
return Q3Shader.decode(Bytes.reader(b))
end, samples)
t:done()
+36
View File
@@ -0,0 +1,36 @@
-- data/q3skin tests: Quake III .skin files against tools/fixtures/q3shader.py's parse, write round trips, lookup,
-- errors, the fuzz pass.
local T = require("@tests/T")
local F = require("@rosetta")
local Q3Skin = require("@rosetta/data/q3skin")
local Bytes = require("@rosetta/core/Bytes")
local Err = require("@rosetta/core/Err")
local Fx = require("@tests/gen/q3shader")
local t = T.suite("q3skin")
t:eq(Q3Skin.name, "q3skin", "name")
local function dump(list: any): string
local parts = {}
for _, e in list do
table.insert(parts, e.surface .. "=" .. e.shader)
end
return table.concat(parts, "|")
end
for i, c in Fx.skins do
local list, err = Q3Skin.decode(Bytes.reader(buffer.fromstring(c.text)))
t:check(list, "skin " .. i .. ": " .. tostring(err))
t:eq(dump(list), dump(c.entries), "skin " .. i .. ": entries")
t:eq(dump(Q3Skin.decode(Bytes.reader(assert(Q3Skin.encode(list :: any))))), dump(c.entries), "skin " .. i .. ": round trip")
t:eq(F.detect(c.text, { name = "x.skin" }), "q3skin", "skin " .. i .. ": F.detect")
end
t:eq(Q3Skin.shader(assert(Q3Skin.decode(Bytes.reader(buffer.fromstring(Fx.skins[1].text)))), "H_HEAD"), "models/players/sarge/band.tga", "skin lookup")
local _, e1 = Q3Skin.encode({ { surface = "a,b", shader = "x" } })
t:check(e1 and e1.code == Err.ARG, "comma in a surface name")
local _, e2 = Q3Skin.decode(Bytes.reader(buffer.fromstring("no comma here\n")))
t:check(e2 and e2.code == Err.CORRUPT, "line without a comma")
t:fuzz("decode", function(b: buffer)
return Q3Skin.decode(Bytes.reader(b))
end, { Fx.skins[1].text, Fx.skins[2].text })
t:done()
+48
View File
@@ -0,0 +1,48 @@
-- image/lmp tests: qpic pictures, palette.lmp and colormap.lmp against Pillow (tools/fixtures/lmp.py), the palette
-- through files, the grey fallback, the qpic writer round trip, errors, the fuzz pass.
local T = require("@tests/T")
local F = require("@rosetta")
local Lmp = require("@rosetta/image/lmp")
local Bytes = require("@rosetta/core/Bytes")
local Crc32 = require("@rosetta/core/Crc32")
local Err = require("@rosetta/core/Err")
local Fx = require("@tests/gen/lmp")
local t = T.suite("lmp")
t:eq(Lmp.name, "lmp", "name")
local samples = {}
for _, p in Fx.pics do
local img, err = Lmp.decode(Bytes.reader(buffer.fromstring(p.data)), { palette = Fx.palette })
t:check(img, p.name .. ": " .. tostring(err))
t:eq(img.width .. "x" .. img.height, p.w .. "x" .. p.h, p.name .. ": size")
t:eq(Crc32.of(img.pixels), p.crc, p.name .. ": pixels = Pillow (index 255 transparent)")
local opaque = assert(Lmp.decode(Bytes.reader(buffer.fromstring(p.data)), { files = { ["gfx/palette.lmp"] = Fx.palette }, keyed = false }))
t:eq(Crc32.of(opaque.pixels), p.crcOpaque, p.name .. ": opaque, palette through files")
t:eq(opaque.meta.source.palette, true, p.name .. ": palette found")
-- write back: the palette colours map to the same indices
local out = assert(Lmp.encode(img, { palette = Fx.palette }))
t:check(buffer.tostring(out) == p.data, p.name .. ": writer gives the same bytes")
table.insert(samples, p.data)
end
do
local p = Fx.pics[2]
t:eq(F.detect(p.data, { name = "conback.lmp" }), "lmp", "F.detect by size + extension")
local grey = assert(Lmp.decode(Bytes.reader(buffer.fromstring(p.data))))
t:eq(grey.meta.source.palette, false, "no palette: grey")
local cm = assert(Lmp.decode(Bytes.reader(buffer.fromstring(Fx.colormap.data)), { palette = Fx.palette }))
t:eq(cm.width .. "x" .. cm.height, "256x64", "colormap size")
t:eq(Crc32.of(cm.pixels), Fx.colormap.crc, "colormap = Pillow")
local pal = assert(Lmp.decode(Bytes.reader(buffer.fromstring(Fx.palette))))
t:eq(Crc32.of(pal.pixels), Fx.palcrc, "palette.lmp swatch = Pillow")
t:eq(#pal.meta.palette, 256, "palette entries")
t:check(buffer.tostring(assert(Lmp.encode(pal, { kind = "palette" }))) == Fx.palette, "palette round trip")
local _, e1 = Lmp.encode(grey)
t:check(e1 and e1.code == Err.ARG, "qpic writer needs the palette")
local _, e2 = Lmp.decode(Bytes.reader(buffer.fromstring(string.sub(p.data, 1, 30))), { kind = "qpic" })
t:check(e2 and e2.code == Err.CORRUPT or e2 and e2.code == Err.TRUNCATED, "truncated qpic: " .. tostring(e2))
end
t:fuzz("decode", function(b: buffer)
return Lmp.decode(Bytes.reader(b), { palette = Fx.palette, kind = "qpic" })
end, samples)
t:done()
+67
View File
@@ -0,0 +1,67 @@
-- image/spr tests: Quake / Half-Life (alpha test, index alpha, normal) / SPR32 sprites against the frames written by
-- tools/fixtures/spr.py (pixels, canvas positions from the origins, group delays), the writer round trips (versions 1,
-- 2 and 32), errors, the fuzz pass.
local T = require("@tests/T")
local F = require("@rosetta")
local Spr = require("@rosetta/image/spr")
local Bytes = require("@rosetta/core/Bytes")
local Crc32 = require("@rosetta/core/Crc32")
local Err = require("@rosetta/core/Err")
local Fx = require("@tests/gen/spr")
local t = T.suite("spr")
t:eq(Spr.name, "spr", "name")
local samples = {}
for _, s in Fx.sprites do
t:eq(F.detect(s.data), "spr", s.name .. ": F.detect")
local img, err = Spr.decode(Bytes.reader(buffer.fromstring(s.data)), { palette = Fx.palette })
t:check(img, s.name .. ": " .. tostring(err))
t:eq(img.width .. "x" .. img.height, s.w .. "x" .. s.h, s.name .. ": canvas")
t:eq(img.meta.origin[1] .. " " .. img.meta.origin[2], s.origin[1] .. " " .. s.origin[2], s.name .. ": origin")
local frames = img.frames or { { image = img, x = 0, y = 0, delay = 0.1 } }
t:eq(#frames, #s.frames, s.name .. ": frames")
for i, want in s.frames do
local f = frames[i]
t:eq(f.x .. "," .. f.y .. " " .. f.image.width .. "x" .. f.image.height, want.x .. "," .. want.y .. " " .. want.w .. "x" .. want.h, s.name .. ": frame " .. i .. " placement")
t:eq(Crc32.of(f.image.pixels), want.crc, s.name .. ": frame " .. i .. " pixels")
t:check(math.abs(f.delay - want.delay) < 1e-6, s.name .. ": frame " .. i .. " delay")
end
t:eq(img.meta.source.version, s.version, s.name .. ": version")
-- write in the same version and read back the same frames
local out, eerr = Spr.encode(img, { version = s.version, palette = if s.version == 2 then Fx.hlpalette else Fx.palette, texFormat = s.tex })
t:check(out, s.name .. ": encode " .. tostring(eerr))
local back = assert(Spr.decode(Bytes.reader(out :: buffer), { palette = Fx.palette }))
local bf = back.frames or { { image = back, x = 0, y = 0 } }
local same = #bf == #frames
for i, f in frames do
local g = bf[i]
same = same and g and g.x == f.x and g.y == f.y
if s.version == 32 or s.version == 1 or s.tex == 0 or s.tex == 3 then
same = same and Crc32.of(g.image.pixels) == Crc32.of(f.image.pixels)
end
end
t:check(same, s.name .. ": round trip keeps frames, placement and pixels")
table.insert(samples, s.data)
end
do
local q = Fx.sprites[1]
local img = assert(Spr.decode(Bytes.reader(buffer.fromstring(q.data)), { palette = Fx.palette }))
-- Half-Life from a Quake sprite: a made palette
local hl = assert(Spr.encode(img))
local back = assert(Spr.decode(Bytes.reader(hl)))
t:eq(back.meta.source.version, 2, "default version 2")
t:eq(back.meta.source.texFormat, "alphatest", "transparent pixels: alpha test")
local grey = assert(Spr.decode(Bytes.reader(buffer.fromstring(q.data))))
t:eq(grey.meta.source.palette, false, "Quake sprite without a palette: grey")
local _, e1 = Spr.encode(img, { version = 1 })
t:check(e1 and e1.code == Err.ARG, "version 1 needs a palette")
local _, e2 = Spr.decode(Bytes.reader(buffer.fromstring("IDS2" .. string.rep("\0", 40))))
t:check(e2 and e2.code == Err.CORRUPT, "Quake II sprite: not IDSP")
local _, e3 = Spr.decode(Bytes.reader(buffer.fromstring(string.sub(q.data, 1, 60))), { palette = Fx.palette })
t:check(e3 and e3.code == Err.TRUNCATED, "truncated: " .. tostring(e3))
end
t:fuzz("decode", function(b: buffer)
return Spr.decode(Bytes.reader(b), { palette = Fx.palette })
end, samples)
t:done()