126 lines
5.2 KiB
Luau
126 lines
5.2 KiB
Luau
-- image/pnm tests: ImageMagick-written P1-P7 / PFM files (ASCII and binary, 8 / 16-bit, PAM tuple types, both PFM
|
|
-- byte orders) and hand-written edge cases (comments, packed P1 digits, odd maxvals) against ImageMagick's RGBA
|
|
-- decodes (8-bit; 16-bit and float natively); round trips of every subtype through our encoder, binary and ASCII;
|
|
-- errors; the fuzz no-throw pass. Fixtures: tools/fixtures/pnm.py.
|
|
local T = require("@tests/T")
|
|
local Pnm = require("@rosetta/image/pnm")
|
|
local Bytes = require("@rosetta/core/Bytes")
|
|
local Err = require("@rosetta/core/Err")
|
|
local Types = require("@rosetta/Types")
|
|
local Fx = require("@tests/gen/pnm")
|
|
|
|
local t = T.suite("pnm")
|
|
|
|
t:eq(Pnm.name, "pnm", "name")
|
|
t:check(Pnm.caps.decode and Pnm.caps.encode, "caps")
|
|
|
|
local function dec(data: any, opts: any?): (any, any)
|
|
return Pnm.decode(Bytes.reader(if type(data) == "string" then buffer.fromstring(data) else data), opts)
|
|
end
|
|
|
|
-- channel c (0..3) of pixel i of a native image, in 16-bit scale
|
|
local function chan16(img: any, i: number, c: number): number
|
|
local px, f = img.pixels, img.format
|
|
if f == "gray16" then
|
|
return if c == 3 then 65535 else buffer.readu16(px, i * 2)
|
|
elseif f == "rgba16" then
|
|
return buffer.readu16(px, i * 8 + c * 2)
|
|
elseif f == "gray8" then
|
|
return if c == 3 then 65535 else buffer.readu8(px, i) * 257
|
|
elseif f == "graya8" then
|
|
return buffer.readu8(px, i * 2 + (if c == 3 then 1 else 0)) * 257
|
|
elseif f == "rgb8" then
|
|
return if c == 3 then 65535 else buffer.readu8(px, i * 3 + c) * 257
|
|
end
|
|
return buffer.readu8(px, i * 4 + c) * 257
|
|
end
|
|
|
|
for _, c in Fx do
|
|
local img, err = dec(c.data)
|
|
t:check(img, c.name .. ": " .. tostring(err))
|
|
t:check(img.width == c.width and img.height == c.height and img.format == "rgba8", c.name .. ": size")
|
|
local worst = 0
|
|
for i = 0, c.width * c.height * 4 - 1 do
|
|
worst = math.max(worst, math.abs(buffer.readu8(img.pixels, i) - string.byte(c.rgba8, i + 1)))
|
|
end
|
|
t:check(worst <= c.tol, string.format("%s: max difference %d against ImageMagick (allowed %d)", c.name, worst, c.tol))
|
|
local nat = dec(c.data, { native = true })
|
|
if c.rgba16 then
|
|
local w16 = 0
|
|
for i = 0, c.width * c.height - 1 do
|
|
for k = 0, 3 do
|
|
local o = (i * 4 + k) * 2 + 1
|
|
local want = string.byte(c.rgba16, o) * 256 + string.byte(c.rgba16, o + 1)
|
|
w16 = math.max(w16, math.abs(chan16(nat, i, k) - want))
|
|
end
|
|
end
|
|
t:check(w16 <= c.tol * 66, string.format("%s: 16-bit max difference %d", c.name, w16))
|
|
end
|
|
if c.floats then
|
|
t:eq(nat.format, "rgbaf32", c.name .. ": native float")
|
|
local fb = buffer.fromstring(c.floats)
|
|
local fw = 0
|
|
for i = 0, c.width * c.height * 4 - 1 do
|
|
fw = math.max(fw, math.abs(buffer.readf32(nat.pixels, i * 4) - buffer.readf32(fb, i * 4)))
|
|
end
|
|
t:check(fw < 1e-6, c.name .. ": floats")
|
|
end
|
|
-- round trips through our encoder: the same subtype, binary and ASCII
|
|
local ext = string.match(c.name, "%.(%a+)$")
|
|
for _, ascii in { false, true } do
|
|
local enc, eerr = Pnm.encode(nat, { format = ext, ascii = ascii })
|
|
t:check(enc, c.name .. ": encode: " .. tostring(eerr))
|
|
local back = dec(enc, { native = true })
|
|
t:check(back and back.format == nat.format and buffer.tostring(back.pixels) == buffer.tostring(nat.pixels), string.format("%s: round trip (ascii %s)", c.name, tostring(ascii)))
|
|
end
|
|
end
|
|
t:note("%d files match ImageMagick and round-trip", #Fx)
|
|
|
|
-- encoder choices, conversions, options
|
|
do
|
|
local img = Types.Image.new(3, 2)
|
|
for i = 0, 23 do
|
|
buffer.writeu8(img.pixels, i, (i * 40) % 256)
|
|
end
|
|
for i = 0, 5 do
|
|
buffer.writeu8(img.pixels, i * 4 + 3, 255)
|
|
end
|
|
local s = buffer.tostring(Pnm.encode(img) :: buffer)
|
|
t:eq(string.sub(s, 1, 2), "P6", "opaque rgba8 -> P6")
|
|
buffer.writeu8(img.pixels, 3, 7)
|
|
t:eq(string.sub(buffer.tostring(Pnm.encode(img) :: buffer), 1, 2), "P7", "alpha -> PAM")
|
|
t:eq(string.sub(buffer.tostring(Pnm.encode(img, { format = "pgm" }) :: buffer), 1, 2), "P5", "pgm by name")
|
|
t:eq(string.sub(buffer.tostring(Pnm.encode(img, { format = "pbm", ascii = true }) :: buffer), 1, 2), "P1", "ascii pbm")
|
|
t:eq(string.sub(buffer.tostring(Pnm.encode(img, { format = "pfm" }) :: buffer), 1, 2), "PF", "pfm by name")
|
|
local g = Types.Image.new(2, 1, "gray8")
|
|
t:eq(string.sub(buffer.tostring(Pnm.encode(g) :: buffer), 1, 2), "P5", "gray8 -> P5")
|
|
local m = Pnm.encode(g, { maxval = 15, ascii = true }) :: buffer
|
|
t:check(string.find(buffer.tostring(m), "\n15\n", 1, true) ~= nil, "maxval option")
|
|
local _, e = Pnm.encode(img, { maxval = 0 })
|
|
t:check(e and e.code == Err.ARG, "bad maxval")
|
|
local _, e2 = Pnm.encode({} :: any)
|
|
t:check(e2 and e2.code == Err.ARG, "not an image")
|
|
t:eq(Pnm.sniff(Bytes.reader(buffer.fromstring("P6\n1 1\n255\n..."))), 0.9, "sniff")
|
|
t:eq(Pnm.sniff(Bytes.reader(buffer.fromstring("PNG"))), 0, "sniff other")
|
|
local _, e3 = dec("P6\n100 100\n255\n" .. string.rep("x", 50))
|
|
t:check(e3 and e3.code == Err.TRUNCATED, "truncated")
|
|
local _, e4 = dec("P6\n10 10\n255\n", { maxPixels = 50 })
|
|
t:check(e4 and e4.code == Err.LIMIT, "maxPixels")
|
|
local _, e5 = dec("P3\n1 1\n255\n1 x 3\n")
|
|
t:check(e5 and e5.code == Err.CORRUPT, "bad ascii")
|
|
end
|
|
|
|
do
|
|
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 Pnm.decode(Bytes.reader(b))
|
|
end, samples)
|
|
end
|
|
|
|
t:done()
|