93 lines
3.6 KiB
Luau
93 lines
3.6 KiB
Luau
-- image/tim tests against ImageMagick (tools/fixtures/tim.py).
|
|
local T = require("@tests/T")
|
|
local F = require("@rosetta")
|
|
local Tim = require("@rosetta/image/tim")
|
|
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/tim")
|
|
|
|
local t = T.suite("tim")
|
|
t:eq(Tim.name, "tim", "name")
|
|
|
|
local function rgb(img: any): buffer
|
|
local n = img.width * img.height
|
|
local out = buffer.create(n * 3)
|
|
for i = 0, n - 1 do
|
|
buffer.copy(out, i * 3, img.pixels, i * 4, 3)
|
|
end
|
|
return out
|
|
end
|
|
|
|
for _, c in Fx do
|
|
local img, err = Tim.decode(Bytes.reader(buffer.fromstring(c.data)))
|
|
t:check(img, c.name .. ": " .. tostring(err))
|
|
t:eq(F.detect(c.data), "tim", c.name .. ": F.detect")
|
|
t:check(img.width == c.width and img.height == c.height, c.name .. ": size")
|
|
-- transparent pixels: compare RGB with ImageMagick where opaque (it reads 0x0000 as black)
|
|
t:eq(Crc32.of(rgb(img)), c.rgb[1], c.name .. ": RGB against ImageMagick")
|
|
local tr = 0
|
|
for i = 0, img.width * img.height - 1 do
|
|
if buffer.readu8(img.pixels, i * 4 + 3) == 0 then
|
|
tr += 1
|
|
end
|
|
end
|
|
t:eq(tr, c.transparent, c.name .. ": transparent pixels")
|
|
t:eq(img.meta.source.bpp, c.bpp, c.name .. ": bpp")
|
|
t:eq(img.meta.source.x, 320, c.name .. ": VRAM x")
|
|
if c.cluts > 1 then
|
|
local other = Tim.decode(Bytes.reader(buffer.fromstring(c.data)), { clut = 1 }) :: any
|
|
t:check(Crc32.of(other.pixels) ~= Crc32.of(img.pixels) and other.meta.source.clut.row == 1, c.name .. ": second CLUT row")
|
|
end
|
|
-- decoded 4 / 8 / 15-bit pixels are PSX colours, so every depth that fits is lossless (24-bit only as 24-bit)
|
|
for _, bpp in (if c.bpp == 24 then { 24 } else { 16, 24, 8, 4 }) do
|
|
local enc, eerr = Tim.encode(img, { bpp = bpp })
|
|
if bpp == 24 then
|
|
-- 24-bit drops alpha: compare RGB
|
|
local back = Tim.decode(Bytes.reader(enc :: buffer)) :: any
|
|
t:eq(Crc32.of(rgb(back)), Crc32.of(rgb(img)), c.name .. ": 24-bit round trip (RGB)")
|
|
elseif enc then
|
|
-- 4 / 8-bit rows hold whole 16-bit units: odd widths come back padded on the right
|
|
local back = Tim.decode(Bytes.reader(enc)) :: any
|
|
local same = back.height == img.height and back.width >= img.width
|
|
for y = 0, img.height - 1 do
|
|
for x = 0, img.width - 1 do
|
|
same = same and buffer.readu32(back.pixels, (y * back.width + x) * 4) == buffer.readu32(img.pixels, (y * img.width + x) * 4)
|
|
end
|
|
end
|
|
t:check(same, c.name .. ": " .. bpp .. "-bit round trip")
|
|
else
|
|
t:check(eerr and eerr.code == Err.ARG, c.name .. ": " .. bpp .. "-bit refused (too many colours)")
|
|
end
|
|
end
|
|
end
|
|
|
|
do
|
|
local img = Types.Image.new(3, 1)
|
|
buffer.writeu32(img.pixels, 0, 0xFF000000)
|
|
buffer.writeu32(img.pixels, 4, 0x00FFFFFF)
|
|
buffer.writeu32(img.pixels, 8, 0xFF0000FF)
|
|
local back = Tim.decode(Bytes.reader(Tim.encode(img) :: buffer)) :: any
|
|
t:eq(buffer.readu32(back.pixels, 0), 0xFF000000, "opaque black stays opaque (STP)")
|
|
t:eq(buffer.readu32(back.pixels, 4), 0, "transparent")
|
|
t:eq(buffer.readu32(back.pixels, 8), 0xFF0000FF, "red")
|
|
local _, e1 = Tim.encode(img, { bpp = 12 })
|
|
t:check(e1 and e1.code == Err.ARG, "bpp 12")
|
|
local _, e2 = Tim.decode(Bytes.reader(buffer.fromstring(string.sub(Fx[1].data, 1, 40))))
|
|
t:check(e2 and e2.code == Err.TRUNCATED, "truncated: " .. tostring(e2))
|
|
local mixed = buffer.fromstring(Fx[3].data)
|
|
buffer.writeu8(mixed, 4, 4)
|
|
local _, e3 = Tim.decode(Bytes.reader(mixed))
|
|
t:check(e3 and e3.code == Err.UNSUPPORTED, "mixed mode")
|
|
end
|
|
|
|
local samples = {}
|
|
for _, c in Fx do
|
|
table.insert(samples, c.data)
|
|
end
|
|
t:fuzz("decode", function(b: buffer)
|
|
return Tim.decode(Bytes.reader(b))
|
|
end, samples, { mutations = 60 })
|
|
t:done()
|