ws-tex: image/pcx (read 1/2/4/8-bit, planes, palettes; write 8-bit palette / 24 / 32-bit RLE)

Tested against netpbm ppmtopcx / ImageMagick / Pillow files decoded by pcxtoppm; our files read by
ImageMagick. Shared test helper tests/image/_raster.luau and fixture helper tools/fixtures/_raster_ref.py.
This commit is contained in:
4DBug committed 2026-10-31 14:48:46 -05:00
1 parent abd01270f0
commit d294c211f9
8 files changed
+614

No files matched your search

+78
View File
@@ -0,0 +1,78 @@
-- tests/image/_raster: the shared checks of the simple raster formats' tests (pcx, sgi, sunras, xbm, xpm, ilbm, icns,
-- ani): decode every case against its reference rgba8 pixels, round-trip images through the encoder, fuzz samples.
local Bytes = require("@formats/core/Bytes")
local Types = require("@formats/Types")
local R = {}
local function maxDiff(a: buffer, b: buffer, n: number): (number, number)
local worst, at = 0, -1
for i = 0, n * 4 - 1 do
local d = math.abs(buffer.readu8(a, i) - buffer.readu8(b, i))
if d > worst then
worst, at = d, i // 4
end
end
return worst, at
end
R.maxDiff = maxDiff
-- cases: { { name, data, w, h, rgba, tol? } } (rgba: the reference decode)
function R.decodes(t: any, Fmt: any, cases: { any }, opts: any?): number
local n = 0
for _, c in cases do
local img, err = Fmt.decode(Bytes.reader(buffer.fromstring(c.data)), opts)
t:check(img, c.name .. ": " .. tostring(err))
if img then
t:check(img.width == c.w and img.height == c.h and img.format == "rgba8", string.format("%s: %d x %d %s", c.name, img.width, img.height, img.format))
if img.width == c.w and img.height == c.h then
local d, at = maxDiff(img.pixels, buffer.fromstring(c.rgba), c.w * c.h)
t:check(d <= (c.tol or 0), string.format("%s: %d off the reference at pixel %d", c.name, d, at))
end
n += 1
end
-- from inside a bigger buffer
local big = buffer.create(#c.data + 9)
buffer.writestring(big, 5, c.data)
local img2 = Fmt.decode(Bytes.reader(big, 5, #c.data), opts)
t:check(img2 and img and buffer.tostring(img2.pixels) == buffer.tostring(img.pixels), c.name .. ": from a sub-range")
end
return n
end
-- image(w, h, rgba string) -> Types.Image
function R.image(w: number, h: number, rgba: string): Types.Image
local img = Types.Image.new(w, h)
img.pixels = buffer.fromstring(rgba)
return img
end
-- roundTrip(t, Fmt, img, encodeOpts?, label, tol?) -> the encoded buffer
function R.roundTrip(t: any, Fmt: any, img: Types.Image, eopts: any?, label: string, tol: number?): buffer?
local enc, err = Fmt.encode(img, eopts)
t:check(enc, label .. ": encode " .. tostring(err))
if not enc then
return nil
end
local back, derr = Fmt.decode(Bytes.reader(enc))
t:check(back, label .. ": decode " .. tostring(derr))
if back then
t:check(back.width == img.width and back.height == img.height, label .. ": size")
local d = maxDiff(back.pixels, img.pixels, img.width * img.height)
t:check(d <= (tol or 0), string.format("%s: round trip %d off", label, d))
end
return enc
end
-- samples(cases, every?) -> the fuzz samples (every n-th case's data)
function R.samples(cases: { any }, every: number?): { string }
local samples = {}
for i, c in cases do
if i % (every or 1) == 0 then
table.insert(samples, c.data)
end
end
return samples
end
return R
+39
View File
@@ -0,0 +1,39 @@
-- image/pcx tests: files from netpbm (24-bit, 8-bit, packed 2 / 4-bit, 3 / 4 planes, mono), ImageMagick and Pillow
-- against pcxtoppm's decode; our files read by ImageMagick; round trips (palette, 24-bit, 32-bit alpha); errors; the
-- fuzz no-throw pass. Fixtures: tools/fixtures/pcx.py.
local T = require("@tests/T")
local Pcx = require("@formats/image/pcx")
local Bytes = require("@formats/core/Bytes")
local Err = require("@formats/core/Err")
local R = require("@tests/image/_raster")
local Fx = require("@tests/gen/pcx")
local t = T.suite("pcx")
t:eq(Pcx.name, "pcx", "name")
t:check(Pcx.caps.decode and Pcx.caps.encode, "caps")
t:note("%d files match pcxtoppm", R.decodes(t, Pcx, Fx.cases))
-- our files: ImageMagick reads what we decode
for _, o in Fx.ours do
local img = Pcx.decode(Bytes.reader(buffer.fromstring(o.data))) :: any
t:eq(R.maxDiff(img.pixels, buffer.fromstring(o.rgba), o.w * o.h), 0, o.name .. ": ImageMagick reads our file")
end
for _, c in Fx.cases do
R.roundTrip(t, Pcx, R.image(c.w, c.h, c.rgba), nil, c.name .. " re-encoded")
R.roundTrip(t, Pcx, R.image(c.w, c.h, c.rgba), { palette = false }, c.name .. " re-encoded 24-bit")
end
do
local img = R.image(3, 2, string.rep("\1\2\3\128", 6))
local enc = R.roundTrip(t, Pcx, img, nil, "alpha: 4 planes") :: buffer
t:eq(buffer.readu8(enc, 65), 4, "alpha -> 4 planes")
t:eq(Pcx.sniff(Bytes.reader(enc)), 0.8, "sniff")
t:eq(Pcx.sniff(Bytes.reader(buffer.fromstring("GIF89a"))), 0, "sniff other")
local _, e = Pcx.decode(Bytes.reader(buffer.fromstring(string.sub(buffer.tostring(enc), 1, 60))))
t:check(e and e.code == Err.TRUNCATED, "short header")
local _, e2 = Pcx.decode(Bytes.reader(enc), { maxPixels = 2 })
t:check(e2 and e2.code == Err.LIMIT, "limit")
end
t:fuzz("decode", function(b: buffer)
return Pcx.decode(Bytes.reader(b))
end, R.samples(Fx.cases), { mutations = 6 })
t:done()