85 lines
3.4 KiB
Luau
85 lines
3.4 KiB
Luau
-- image/icns tests: a libicns file (RLE entries + masks, JPEG 2000 entries skipped), a Pillow file (PNG entries through
|
|
-- the decodeImage hook) and a hand-written one (ics#, raw is32, ic04 ARGB) against icns2png's / the written pixels;
|
|
-- our RLE file read back by icns2png; PNG entries through encodeImage; errors; the fuzz no-throw pass. Fixtures:
|
|
-- tools/fixtures/icns.py.
|
|
local T = require("@tests/T")
|
|
local Icns = require("@rosetta/image/icns")
|
|
local Png = require("@rosetta/image/png")
|
|
local Bytes = require("@rosetta/core/Bytes")
|
|
local Err = require("@rosetta/core/Err")
|
|
local Types = require("@rosetta/Types")
|
|
local R = require("@tests/image/_raster")
|
|
local Fx = require("@tests/gen/icns")
|
|
|
|
local t = T.suite("icns")
|
|
t:eq(Icns.name, "icns", "name")
|
|
t:check(Icns.caps.decode and Icns.caps.encode, "caps")
|
|
|
|
local hooks = {
|
|
decodeImage = function(b: buffer): (any, any)
|
|
return Png.decode(Bytes.reader(b))
|
|
end,
|
|
encodeImage = function(img: any, _fmt: string): (any, any)
|
|
return Png.encode(img)
|
|
end,
|
|
}
|
|
|
|
local function check(c: any)
|
|
local img, err = Icns.decode(Bytes.reader(buffer.fromstring(c.data)), hooks)
|
|
t:check(img, c.name .. ": " .. tostring(err))
|
|
local frames = img.frames or { { image = img } }
|
|
t:check(#frames >= #c.entries, c.name .. ": entries") -- icns2png writes one file per size (@2x entries collide)
|
|
for _, e in c.entries do
|
|
local found = nil
|
|
for _, f in frames do
|
|
local im = f.image
|
|
if im.width == e.w and im.height == e.h and (e.type == "" or im.meta.source.type == e.type) then
|
|
found = im
|
|
end
|
|
end
|
|
t:check(found, c.name .. ": entry " .. e.type .. " " .. e.w)
|
|
if found then
|
|
local d = R.maxDiff(found.pixels, buffer.fromstring(e.rgba), e.w * e.h)
|
|
t:eq(d, 0, string.format("%s: %s %dx%d pixels", c.name, e.type, e.w, e.h))
|
|
end
|
|
end
|
|
t:eq(if img.meta.skipped then #img.meta.skipped else 0, c.skipped or 0, c.name .. ": skipped entries")
|
|
return img
|
|
end
|
|
for _, c in Fx.cases do
|
|
check(c)
|
|
end
|
|
for _, o in Fx.ours do
|
|
check(o)
|
|
end
|
|
t:note("%d files and our encoder's file match", #Fx.cases)
|
|
|
|
do
|
|
-- PNG entries through the hooks, legacy entries without them
|
|
local img = Types.Image.new(64, 64)
|
|
for i = 0, 64 * 64 - 1 do
|
|
buffer.writeu32(img.pixels, i * 4, i * 2654435761 % 4294967296)
|
|
end
|
|
local small = Types.Image.new(16, 16)
|
|
buffer.fill(small.pixels, 0, 200)
|
|
img.frames = { { image = img, delay = 0, x = 0, y = 0, dispose = "none", blend = "source" }, { image = small, delay = 0, x = 0, y = 0, dispose = "none", blend = "source" } }
|
|
local enc = Icns.encode(img, hooks) :: buffer
|
|
local back = Icns.decode(Bytes.reader(enc), hooks) :: any
|
|
t:check(back and back.width == 64 and buffer.tostring(back.pixels) == buffer.tostring(img.pixels), "PNG entries round trip")
|
|
t:eq(#back.frames, 2, "two sizes")
|
|
local _, e = Icns.encode(img)
|
|
t:check(e and e.code == Err.ARG, "64 x 64 needs the PNG hook")
|
|
local leg = Icns.encode(small) :: buffer
|
|
local lb = Icns.decode(Bytes.reader(leg)) :: any
|
|
t:eq(buffer.tostring(lb.pixels), buffer.tostring(small.pixels), "legacy round trip")
|
|
local _, e2 = Icns.decode(Bytes.reader(enc))
|
|
t:check(e2 and e2.code == Err.UNSUPPORTED, "PNG entries without the hook: UNSUPPORTED")
|
|
t:eq(Icns.sniff(Bytes.reader(enc)), 1, "sniff")
|
|
local _, e3 = Icns.encode(Types.Image.new(10, 12))
|
|
t:check(e3 and e3.code == Err.ARG, "not square")
|
|
end
|
|
t:fuzz("decode", function(b: buffer)
|
|
return Icns.decode(Bytes.reader(b), hooks)
|
|
end, R.samples(Fx.cases), { mutations = 6 })
|
|
t:done()
|