Files
rosetta/tests/image/tga.luau
T

92 lines
3.7 KiB
Luau

-- image/tga tests against Pillow's and ImageMagick's decodes (tools/fixtures/tga.py).
local T = require("@tests/T")
local Tga = require("@rosetta/image/tga")
local Bytes = require("@rosetta/core/Bytes")
local Err = require("@rosetta/core/Err")
local Types = require("@rosetta/Types")
local Fx = require("@tests/gen/tga")
local t = T.suite("tga")
t:eq(Tga.name, "tga", "name")
t:check(Tga.caps.decode and Tga.caps.encode, "caps")
local function dec(data: any, opts: any?): (any, any)
return Tga.decode(Bytes.reader(if type(data) == "string" then buffer.fromstring(data) else data), opts)
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, c.name .. ": size")
local worst, at = 0, -1
for i = 0, #c.rgba - 1 do
local d = math.abs(buffer.readu8(img.pixels, i) - string.byte(c.rgba, i + 1))
if d > worst then
worst, at = d, i
end
end
t:check(worst == 0, string.format("%s: differs from the reference by %d at byte %d", c.name, worst, at))
t:check(not img.meta.truncated, c.name .. ": not truncated")
for _, rle in { false, true } do
for _, origin in { "bottom-left", "top-left" } do
local enc = Tga.encode(img, { rle = rle, origin = origin }) :: buffer
local back = dec(enc)
t:check(back and buffer.tostring(back.pixels) == buffer.tostring(img.pixels), string.format("%s: round trip rle=%s %s", c.name, tostring(rle), origin))
end
end
end
t:note("%d files match the reference decodes and round-trip", #Fx)
do
local byName = {}
for _, c in Fx do
byName[c.name] = c
end
local id = dec(byName["hand-id-top"].data)
t:check(id.meta.text and id.meta.text.id == "hello", "image ID")
local g = dec(byName["pil-L-raw-top"].data, { native = true })
t:eq(g.format, "gray8", "native grey")
t:eq(Tga.encode(g) and buffer.readu8(Tga.encode(g) :: buffer, 2), 3, "gray8 encodes as type 3")
local rgb = dec(byName["pil-RGB-raw-top"].data, { native = true })
t:eq(rgb.format, "rgb8", "native rgb8")
local p = dec(byName["pil-P-raw-top"].data)
t:check(p.meta.palette and #p.meta.palette > 0, "palette in meta")
-- sniff: low without a footer, higher with ours, never for other formats
t:eq(Tga.sniff(Bytes.reader(buffer.fromstring(byName["hand-16bit"].data))), 0.2, "sniff without a footer")
t:eq(Tga.sniff(Bytes.reader(Tga.encode(p) :: buffer)), 0.9, "sniff with the footer")
t:eq(Tga.sniff(Bytes.reader(buffer.fromstring("\0\0\1\0\1\0\16\16\0\0\1\0\32\0\0\0\0\0\0\0\0\0"))), 0, "an ICO header is not a TGA")
t:eq(Tga.sniff(Bytes.reader(buffer.fromstring("\137PNG\r\n\26\n\0\0\0\13IHDR\0\0\0\0\0\0"))), 0, "a PNG is not a TGA")
local img = Types.Image.new(3, 2)
buffer.fill(img.pixels, 0, 255, 24)
t:eq(buffer.readu8(Tga.encode(img) :: buffer, 16), 24, "opaque rgba8 -> 24-bit")
buffer.writeu8(img.pixels, 3, 0)
t:eq(buffer.readu8(Tga.encode(img) :: buffer, 16), 32, "alpha -> 32-bit")
local _, e = Tga.encode(img, { origin = "middle" } :: any)
t:check(e and e.code == Err.ARG, "bad origin")
local _, e2 = Tga.encode(Types.Image.new(2, 2, "gray16"))
t:check(e2 and e2.code == Err.ARG, "gray16 refused")
-- truncation keeps what is there
local s = byName["pil-RGBA-raw-top"].data
local tr = dec(string.sub(s, 1, #s // 2))
t:check(tr and tr.meta.truncated, "truncated pixels flagged")
local _, e3 = dec(string.sub(s, 1, 10))
t:check(e3 and e3.code == Err.TRUNCATED, "short header")
local _, e4 = dec(s, { maxPixels = 10 })
t:check(e4 and e4.code == Err.LIMIT, "maxPixels")
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 Tga.decode(Bytes.reader(b))
end, samples)
end
t:done()