Files

119 lines
4.8 KiB
Luau

-- image/ico tests against Pillow's decode of every entry (tools/fixtures/ico.py).
local T = require("@tests/T")
local Ico = require("@rosetta/image/ico")
local Png = require("@rosetta/image/png")
local Bytes = require("@rosetta/core/Bytes")
local Err = require("@rosetta/core/Err")
local Types = require("@rosetta/Types")
local Fx = require("@tests/gen/ico")
local t = T.suite("ico")
t:eq(Ico.name, "ico", "name")
t:check(Ico.caps.decode and Ico.caps.encode, "caps")
local hooks = {
decodeImage = function(b: buffer)
return Png.decode(Bytes.reader(b))
end,
encodeImage = function(img: any, _fmt: string)
return Png.encode(img)
end,
}
local function dec(data: any, opts: any?): (any, any)
return Ico.decode(Bytes.reader(if type(data) == "string" then buffer.fromstring(data) else data), opts)
end
local entries = 0
for _, c in Fx do
local img, err = dec(c.data, hooks)
t:check(img, c.name .. ": " .. tostring(err))
local frames = img.frames or { { image = img } }
t:eq(#frames, #c.entries, c.name .. ": entry count")
local biggest = 0
for i, e in c.entries do
local f = frames[i].image
t:check(f.width == e.width and f.height == e.height, string.format("%s entry %d: size %dx%d, want %dx%d", c.name, i, f.width, f.height, e.width, e.height))
local worst = 0
for k = 0, #e.rgba - 1 do
worst = math.max(worst, math.abs(buffer.readu8(f.pixels, k) - string.byte(e.rgba, k + 1)))
end
t:check(worst == 0, string.format("%s entry %d: differs from Pillow by %d", c.name, i, worst))
biggest = math.max(biggest, e.width * e.height)
entries += 1
end
t:eq(img.width * img.height, biggest, c.name .. ": the main image is the largest entry")
local enc, eerr = Ico.encode(img, hooks)
t:check(enc, c.name .. ": encode: " .. tostring(eerr))
local back = dec(enc, hooks)
local bf = back.frames or { { image = back } }
t:eq(#bf, #frames, c.name .. ": round trip entries")
for i, f in frames do
local a, b = f.image, bf[i].image
local same = a.width == b.width and a.height == b.height
if same then
-- alpha-0 pixels keep their colour in 32-bit DIBs too
same = buffer.tostring(a.pixels) == buffer.tostring(b.pixels)
end
t:check(same, string.format("%s: round trip entry %d", c.name, i))
end
end
t:note("%d icons, %d entries match Pillow and round-trip", #Fx, entries)
do
local byName = {}
for _, c in Fx do
byName[c.name] = c
end
-- without the hook: PNG entries are skipped, BMP entries still decode
local big, sm = Types.Image.new(256, 256), Types.Image.new(16, 16)
buffer.fill(big.pixels, 0, 77, 256 * 256 * 4)
buffer.fill(sm.pixels, 0, 99, 16 * 16 * 4)
local two = Types.Image.new(256, 256)
two.frames = { { image = big, delay = 0, x = 0, y = 0, dispose = "none", blend = "source" }, { image = sm, delay = 0, x = 0, y = 0, dispose = "none", blend = "source" } }
local mixed = Ico.encode(two, hooks) :: buffer
t:eq(buffer.readstring(mixed, 6 + 32, 4), "\137PNG", "the 256 entry is written as PNG through the hook")
local im = dec(mixed)
t:check(im and im.width == 16 and im.meta.source.skipped == 1, "no hook: the PNG entry is skipped")
t:eq(dec(mixed, hooks).width, 256, "with the hook: the PNG entry is the main image")
local _, e = dec(byName["pil-png"].data)
t:check(e and e.code == Err.UNSUPPORTED, "PNG-only icon without the hook: " .. tostring(e))
local cur = dec(byName["hand-cur"].data)
t:check(cur.meta.source.type == "cur" and cur.meta.hotspot and cur.meta.hotspot.x == 3 and cur.meta.hotspot.y == 4, "cursor hotspot")
t:eq(cur.frames[2].image.meta.hotspot.x, 5, "second hotspot")
local cenc = Ico.encode(cur) :: buffer
t:eq(buffer.readu16(cenc, 2), 2, "cursor written as CUR")
local cb = dec(cenc)
t:check(cb.meta.hotspot.x == 3 and cb.frames[2].image.meta.hotspot.y == 6, "hotspots round-trip")
local small = Types.Image.new(16, 16)
buffer.fill(small.pixels, 0, 200, 16 * 16 * 4)
local pe = Ico.encode(small, { png = true, encodeImage = hooks.encodeImage }) :: buffer
t:eq(buffer.readstring(pe, 22, 4), "\137PNG", "opts.png writes a PNG entry")
local _, e2 = Ico.encode(small, { png = true })
t:check(e2 and e2.code == Err.ARG, "opts.png without a hook")
local _, e3 = Ico.encode(Types.Image.new(300, 10))
t:check(e3 and e3.code == Err.ARG, "over 256 pixels")
t:eq(Ico.sniff(Bytes.reader(buffer.fromstring(byName["pil-bmp"].data))), 0.8, "sniff")
t:eq(Ico.sniff(Bytes.reader(buffer.fromstring(string.rep("\0", 30)))), 0, "sniff zeros")
local _, e4 = dec(string.sub(byName["pil-bmp"].data, 1, 20))
t:check(e4 and e4.code == Err.TRUNCATED, "truncated directory")
end
do
local samples = {}
for _, c in Fx do
if #c.data < 40000 then
table.insert(samples, c.data)
end
end
t:fuzz("decode", function(b: buffer)
return Ico.decode(Bytes.reader(b), hooks)
end, samples)
t:fuzz("decode without hooks", function(b: buffer)
return Ico.decode(Bytes.reader(b))
end, samples, { mutations = 3, garbage = 50 })
end
t:done()