86 lines
3.7 KiB
Luau
86 lines
3.7 KiB
Luau
-- image/qoi tests: Pillow-written QOI files (RGB / RGBA, noise / gradients / runs / small deltas, 1x1 to 500x1)
|
|
-- decode to Pillow's pixels; our encoder writes the same bytes as Pillow for the same pixels and round-trips exactly;
|
|
-- native rgb8 output, the colour space tag, errors, and the fuzz no-throw pass. Fixtures: tools/fixtures/qoi.py.
|
|
local T = require("@tests/T")
|
|
local Qoi = require("@rosetta/image/qoi")
|
|
local Bytes = require("@rosetta/core/Bytes")
|
|
local Crc32 = require("@rosetta/core/Crc32")
|
|
local Err = require("@rosetta/core/Err")
|
|
local Types = require("@rosetta/Types")
|
|
local Fx = require("@tests/gen/qoi")
|
|
|
|
local t = T.suite("qoi")
|
|
|
|
t:eq(Qoi.name, "qoi", "name")
|
|
t:check(Qoi.caps.decode and Qoi.caps.encode, "caps")
|
|
|
|
local same = 0
|
|
for _, c in Fx do
|
|
local img, err = Qoi.decode(Bytes.reader(buffer.fromstring(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")
|
|
t:eq(Crc32.of(img.pixels), c.crc, c.name .. ": pixels against Pillow")
|
|
-- encode the source pixels: the same bytes as Pillow's writer
|
|
local src = Types.Image.new(c.width, c.height, if c.channels == 4 then "rgba8" else "rgb8")
|
|
src.pixels = buffer.fromstring(c.pixels)
|
|
local enc = Qoi.encode(src, { channels = c.channels, linear = string.byte(c.data, 14) == 1 }) :: buffer -- Pillow tags its files linear
|
|
if buffer.tostring(enc) == c.data then
|
|
same += 1
|
|
end
|
|
local back = Qoi.decode(Bytes.reader(enc))
|
|
t:eq(Crc32.of(back.pixels), c.crc, c.name .. ": our encoding round-trips")
|
|
-- native rgb8
|
|
if c.channels == 3 then
|
|
local n = Qoi.decode(Bytes.reader(buffer.fromstring(c.data)), { native = true })
|
|
t:check(n.format == "rgb8" and buffer.tostring(n.pixels) == c.pixels, c.name .. ": native rgb8")
|
|
end
|
|
end
|
|
t:eq(same, #Fx, "byte-identical to Pillow's encoder")
|
|
t:note("%d Pillow files decode; %d of our encodings are byte-identical to Pillow's", #Fx, same)
|
|
|
|
-- round trips of random images, a sub-range reader, colour space, errors
|
|
do
|
|
local rand = T.rng(5)
|
|
for _, s in { { 1, 1 }, { 70, 3 }, { 33, 33 } } do
|
|
local img = Types.Image.new(s[1], s[2])
|
|
for i = 0, s[1] * s[2] * 4 - 1 do
|
|
buffer.writeu8(img.pixels, i, if rand(3) == 0 then rand(256) else (i // 4) % 256)
|
|
end
|
|
local back = Qoi.decode(Bytes.reader(Qoi.encode(img) :: buffer))
|
|
t:check(buffer.tostring(back.pixels) == buffer.tostring(img.pixels), "random round trip")
|
|
end
|
|
local c = Fx[4]
|
|
local big = buffer.create(#c.data + 9)
|
|
buffer.writestring(big, 5, c.data)
|
|
local img = Qoi.decode(Bytes.reader(big, 5, #c.data))
|
|
t:check(img and Crc32.of(img.pixels) == c.crc, "decode from a sub-range")
|
|
local lin = Types.Image.new(2, 2)
|
|
local d = Qoi.encode(lin, { linear = true }) :: buffer
|
|
t:eq(buffer.readu8(d, 13), 1, "linear tag")
|
|
t:eq(Qoi.decode(Bytes.reader(d)).meta.source.colorSpace, "linear", "linear read back")
|
|
t:eq(Qoi.sniff(Bytes.reader(buffer.fromstring(c.data))), 1, "sniff")
|
|
t:eq(Qoi.sniff(Bytes.reader(buffer.fromstring("GIF8"))), 0, "sniff other")
|
|
local _, e = Qoi.decode(Bytes.reader(buffer.fromstring(c.data)), { maxPixels = 10 })
|
|
t:check(e and e.code == Err.LIMIT, "maxPixels")
|
|
local _, e2 = Qoi.decode(Bytes.reader(buffer.fromstring(string.sub(c.data, 1, 40))))
|
|
t:check(e2 and e2.code == Err.TRUNCATED, "truncated: " .. tostring(e2))
|
|
local _, e3 = Qoi.decode(Bytes.reader(buffer.fromstring("qoix" .. string.rep("\0", 20))))
|
|
t:check(e3 and e3.code == Err.CORRUPT, "bad magic")
|
|
local _, e4 = Qoi.encode(Types.Image.new(2, 2, "gray8"))
|
|
t:check(e4 and e4.code == Err.ARG, "encode gray8 -> ARG")
|
|
end
|
|
|
|
do
|
|
local samples = {}
|
|
for i, c in Fx do
|
|
if i % 2 == 0 then
|
|
table.insert(samples, c.data)
|
|
end
|
|
end
|
|
t:fuzz("decode", function(b: buffer)
|
|
return Qoi.decode(Bytes.reader(b))
|
|
end, samples, { keepHeader = 4 })
|
|
end
|
|
|
|
t:done()
|