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).
49 lines
2.6 KiB
Luau
49 lines
2.6 KiB
Luau
-- 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()
|