Files
rosetta/tests/ops/imageops.luau
T
2026-11-01 01:51:09 -05:00

308 lines
13 KiB
Luau

-- ops/ImageOps tests against Pillow (tools/fixtures/imageops.py) and property tests.
local T = require("@tests/T")
local Ops = require("@rosetta/ops/ImageOps")
local Types = require("@rosetta/Types")
local Err = require("@rosetta/core/Err")
local Fx = require("@tests/gen/imageops")
local t = T.suite("imageops")
local function image(w: number, h: number, fmt: string, px: string): any
local img = Types.Image.new(w, h, fmt :: any)
buffer.writestring(img.pixels, 0, px)
return img
end
local function diff(a: buffer, b: string): (number, number)
local n = #b
local mx, se = 0, 0
for i = 0, n - 1 do
local d = math.abs(buffer.readu8(a, i) - string.byte(b, i + 1))
if d > mx then
mx = d
end
se += d * d
end
local mse = se / n
return mx, if mse == 0 then 99 else 10 * math.log10(255 * 255 / mse)
end
local rgb = image(Fx.rgb.w, Fx.rgb.h, "rgb8", Fx.rgb.px)
local rgba = image(Fx.rgba.w, Fx.rgba.h, "rgba8", Fx.rgba.px)
local rgba2 = image(Fx.rgba2.w, Fx.rgba2.h, "rgba8", Fx.rgba2.px)
do
local g = Ops.convert(rgb, "gray8") :: any
t:check(buffer.tostring(g.pixels) == Fx.gray, "rgb8 -> gray8 matches Pillow convert('L')")
local ga = Ops.convert(rgba, "gray8") :: any
t:check(buffer.tostring(ga.pixels) == Fx.graya, "rgba8 -> gray8 matches Pillow (alpha ignored)")
local gray = Ops.convert(rgb, "gray8") :: any
local worst: { [string]: number } = { nearest = 0, bilinear = 0, lanczos = 0 }
for _, c in Fx.resize do
local src = if c.mode == "RGB" then rgb else gray
local r = Ops.resize(src, c.w, c.h, c.filter) :: any
t:check(r and r.width == c.w and r.height == c.h, "resize size")
local mx = diff(r.pixels, c.px)
worst[c.filter] = math.max(worst[c.filter], mx)
t:check(mx <= (if c.filter == "nearest" then 0 else 1), string.format("resize %s %s %dx%d: max error %d", c.filter, c.mode, c.w, c.h, mx))
end
t:note("resize vs Pillow (%d cases): max error nearest %d, bilinear %d, lanczos %d", #Fx.resize, worst.nearest, worst.bilinear, worst.lanczos)
local minPsnr = 99
for _, c in Fx.rotate do
local r = Ops.rotate(rgba, c.angle, { expand = c.expand }) :: any
t:check(r and r.width == c.w and r.height == c.h, string.format("rotate %d: size %dx%d, Pillow %dx%d", c.angle, r.width, r.height, c.w, c.h))
local _, psnr = diff(r.pixels, c.px)
minPsnr = math.min(minPsnr, psnr)
t:check(psnr > 40, string.format("rotate %d: PSNR %.1f", c.angle, psnr))
end
t:note("rotate vs Pillow (%d cases): min PSNR %.1f dB", #Fx.rotate, minPsnr)
local c = Ops.composite(rgba, rgba2) :: any
local mx = diff(c.pixels, Fx.composite)
t:check(mx == 0, "alpha_composite matches Pillow exactly (max error " .. mx .. ")")
end
local FORMATS = { "rgba8", "rgba16", "rgbaf32", "rgb8", "gray8", "gray16", "graya8", "alpha8", "yuv420p" }
do
for _, f in FORMATS do
local c = Ops.convert(rgba, f) :: any
t:check(c and c.format == f and c.width == rgba.width, "convert to " .. f)
local back = Ops.convert(c, "rgba8") :: any
t:check(back and back.format == "rgba8", f .. " back to rgba8")
if f == "rgba16" or f == "rgbaf32" then
t:check(buffer.tostring(back.pixels) == buffer.tostring(rgba.pixels), f .. ": lossless round trip")
end
if f == "yuv420p" then
-- a smooth image (4:2:0 drops chroma detail)
local sm = Types.Image.new(40, 30)
for i = 0, 40 * 30 - 1 do
local x, y = i % 40, i // 40
buffer.writeu32(sm.pixels, i * 4, 0xFF000000 + (x * 6) + (y * 8) * 256 + ((x + y) * 3) * 65536)
end
local yb = Ops.convert(Ops.convert(sm, "yuv420p") :: any, "rgba8") :: any
local _, psnr = diff(yb.pixels, buffer.tostring(sm.pixels))
t:check(psnr > 38, string.format("yuv420p round trip PSNR %.1f", psnr))
end
for _, g in FORMATS do
t:check(Ops.convert(c, g) ~= nil, f .. " -> " .. g)
end
end
local g16 = Ops.convert(Ops.convert(rgb, "gray8") :: any, "gray16") :: any
t:eq(buffer.readu16(g16.pixels, 0), buffer.readu8((Ops.convert(rgb, "gray8") :: any).pixels, 0) * 257, "8 -> 16 bit is * 257")
local f = Types.Image.new(1, 1, "rgbaf32")
buffer.writef32(f.pixels, 0, 1.5)
buffer.writef32(f.pixels, 4, -0.2)
buffer.writef32(f.pixels, 8, 0.5)
buffer.writef32(f.pixels, 12, 1)
local b = Ops.convert(f, "rgba8") :: any
t:check(buffer.readu8(b.pixels, 0) == 255 and buffer.readu8(b.pixels, 1) == 0 and buffer.readu8(b.pixels, 2) == 128, "float -> 8 bit clamps and rounds")
end
do
for _, f in FORMATS do
local img = Ops.convert(rgba, f) :: any
local s = buffer.tostring(img.pixels)
local r = img
for _ = 1, 4 do
r = Ops.rotate90(r, 1)
end
if f ~= "yuv420p" then
t:check(buffer.tostring(r.pixels) == s, f .. ": four quarter turns are the identity")
t:check(buffer.tostring((Ops.flip(Ops.flip(img, "h") :: any, "h") :: any).pixels) == s, f .. ": flip h twice")
t:check(buffer.tostring((Ops.flip(Ops.flip(img, "v") :: any, "v") :: any).pixels) == s, f .. ": flip v twice")
local q = Ops.rotate90(img, 1) :: any
t:check(q.width == img.height and q.height == img.width, f .. ": a quarter turn swaps the size")
t:check(buffer.tostring((Ops.rotate90(img, 2) :: any).pixels) == buffer.tostring((Ops.flip(Ops.flip(img, "h") :: any, "v") :: any).pixels), f .. ": half turn = both flips")
t:check(buffer.tostring((Ops.rotate(img, 90) :: any).pixels) == buffer.tostring(q.pixels), f .. ": rotate 90 = one quarter turn")
end
local c = Ops.crop(img, -5, 10, 20, 100) :: any
t:check(c and c.width == 15 and c.height == img.height - 10, f .. ": crop clipped")
for _, filter in { "nearest", "bilinear", "lanczos" } do
local z = Ops.resize(img, 23, 31, filter) :: any
t:check(z and z.width == 23 and z.height == 31 and z.format == f, f .. ": resize " .. filter)
end
local rr = Ops.rotate(img, 33) :: any
t:check(rr and rr.format == f, f .. ": rotate 33")
end
local c = Ops.crop(rgba, 3, 4, 5, 6) :: any
t:check(buffer.readu32(c.pixels, 0) == buffer.readu32(rgba.pixels, (4 * rgba.width + 3) * 4), "crop origin")
t:check(buffer.tostring((Ops.resize(rgba, rgba.width, rgba.height, "lanczos") :: any).pixels) == buffer.tostring(rgba.pixels), "same-size resize is a copy")
local q = Ops.rotate90(rgba, 1) :: any -- ccw: the top-right pixel goes to the top-left
t:eq(buffer.readu32(q.pixels, 0), buffer.readu32(rgba.pixels, (rgba.width - 1) * 4), "rotate90 is counter-clockwise")
end
do
for _, f in FORMATS do
local img = Ops.new(3, 2, f) :: any
t:check(img and img.format == f, "new " .. f)
if f ~= "yuv420p" then
local max = if f == "rgba16" or f == "gray16" then 65535 elseif f == "rgbaf32" then 1 else 255
t:check(Ops.setPixel(img, 2, 1, max, max, max, max), f .. ": setPixel")
local r, g, b, a = Ops.getPixel(img, 2, 1)
t:check(r == max and g == max and b == max and a == max, f .. ": getPixel after setPixel")
Ops.fill(img, 0, 0, 0, 0)
local r2 = Ops.getPixel(img, 2, 1)
t:check(r2 == (if f == "alpha8" then 255 else 0), f .. ": fill")
else
t:check(Ops.setPixel(img, 0, 0, 1, 2, 3) == nil, "setPixel refuses yuv420p")
t:check(Ops.getPixel(img, 2, 1) ~= nil, "getPixel on yuv420p")
end
end
local p = Ops.premultiply(rgba) :: any
local u = Ops.unpremultiply(p) :: any
local worst = 0
for i = 0, rgba.width * rgba.height - 1 do
local a = buffer.readu8(rgba.pixels, i * 4 + 3)
t:check(buffer.readu8(p.pixels, i * 4) <= a, "premultiplied <= alpha")
if a >= 32 then
for c = 0, 2 do
worst = math.max(worst, math.abs(buffer.readu8(u.pixels, i * 4 + c) - buffer.readu8(rgba.pixels, i * 4 + c)))
end
end
end
t:check(worst <= 4, "premultiply round trip error " .. worst .. " (alpha >= 32)")
t:note("premultiply round trip: max error %d for alpha >= 32", worst)
end
do
local dst = Ops.new(10, 10, "rgba8") :: any
Ops.fill(dst, 10, 20, 30, 255)
local src = Ops.new(4, 4, "rgba8") :: any
Ops.fill(src, 200, 100, 50, 255)
Ops.blit(dst, src, -2, 8)
t:check(select(1, Ops.getPixel(dst, 0, 8)) == 200 and select(1, Ops.getPixel(dst, 1, 9)) == 200, "blit clipped at the left / bottom")
t:check(select(1, Ops.getPixel(dst, 2, 8)) == 10 and select(1, Ops.getPixel(dst, 0, 7)) == 10, "blit stays inside its rectangle")
Ops.fill(src, 0, 0, 0, 0)
Ops.blit(dst, src, 0, 0)
t:check(select(1, Ops.getPixel(dst, 0, 0)) == 10, "over with a transparent source keeps dst")
Ops.blit(dst, src, 0, 0, { blend = "source" })
t:check(select(4, Ops.getPixel(dst, 0, 0)) == 0, "source blend copies alpha")
Ops.fill(src, 255, 255, 255, 255)
Ops.blit(dst, src, 5, 5, { opacity = 0.5 })
local r = Ops.getPixel(dst, 5, 5)
t:check(math.abs(r - 133) <= 1, "opacity 0.5 over (10 -> " .. r .. ")")
local d3 = Ops.convert(dst, "rgb8") :: any
t:check(Ops.blit(d3, Ops.convert(src, "graya8") :: any, 1, 1) == d3, "blit onto rgb8")
local df = Ops.convert(dst, "rgbaf32") :: any
t:check(Ops.blit(df, src, 0, 0, { opacity = 0.25 }) == df, "blit onto rgbaf32")
local dy = Ops.convert(dst, "yuv420p") :: any
t:check(Ops.blit(dy, src, 3, 3) == dy, "blit onto yuv420p")
end
do
local W, H = 6, 4
local function solid(w: number, h: number, r: number, a: number): any
local im = Ops.new(w, h, "rgba8") :: any
Ops.fill(im, r, 0, 0, a)
return im
end
local anim = Ops.new(W, H, "rgba8") :: any
anim.frames = {
{ image = solid(W, H, 10, 255), delay = 0.1, x = 0, y = 0, dispose = "none", blend = "source" },
{ image = solid(2, 2, 20, 255), delay = 0.2, x = 1, y = 1, dispose = "background", blend = "over" },
{ image = solid(2, 2, 30, 255), delay = 0.3, x = 3, y = 1, dispose = "previous", blend = "over" },
{ image = solid(1, 1, 40, 128), delay = 0.4, x = 5, y = 3, dispose = "none", blend = "over" },
}
local fr = Ops.coalesce(anim) :: any
t:eq(#fr, 4, "coalesce frame count")
local function at(i: number, x: number, y: number): (number, number)
local r, _, _, a = Ops.getPixel(fr[i], x, y)
return r, a
end
t:check(at(1, 0, 0) == 10 and at(1, 1, 1) == 10, "frame 1")
t:check(at(2, 1, 1) == 20 and at(2, 2, 2) == 20 and at(2, 0, 0) == 10, "frame 2 drawn at 1, 1")
local r3, a3 = at(3, 1, 1)
t:check(r3 == 0 and a3 == 0, "frame 2 disposed to background before frame 3")
t:check(at(3, 3, 1) == 30, "frame 3 drawn")
t:check(at(4, 3, 1) == 10, "frame 3 disposed to previous before frame 4")
local r4 = at(4, 5, 3)
t:check(r4 > 10 and r4 < 40, "frame 4 blends over")
t:eq(Ops.frameCount(anim), 4, "frameCount")
t:eq(Ops.durations(anim)[3], 0.3, "durations")
t:eq(Ops.frameCount(rgba), 1, "frameCount of a still")
t:check((Ops.coalesce(rgba) :: any)[1] == rgba, "coalesce of a still")
local made = Ops.fromFrames({ fr[1], fr[2], fr[3] }, { 0.5, 0.25 }) :: any
t:check(made and #made.frames == 3 and made.frames[2].delay == 0.25 and made.frames[3].delay == 0.1, "fromFrames delays")
t:check(buffer.tostring(made.pixels) == buffer.tostring(fr[1].pixels), "fromFrames pixels = frame 1")
local again = Ops.coalesce(made) :: any
t:check(buffer.tostring(again[3].pixels) == buffer.tostring(fr[3].pixels), "coalesce(fromFrames) round trip")
t:check(Ops.fromFrames({ fr[1], rgba }) == nil, "fromFrames refuses mixed sizes")
end
do
local rand = T.rng(5)
local junk: { any } = { nil, 0, -1, 1.5, 2 ^ 40, "x", {}, true, 0 / 0, math.huge, -7 }
local imgs: { any } = { rgba, rgb, {}, { pixels = buffer.create(1), width = 5, height = 5, format = "rgba8" }, Types.Image.new(1, 1, "alpha8"), Ops.convert(rgba, "yuv420p") }
local calls = {
function(a: any, b: any, c: any, d: any)
return Ops.convert(a, b)
end,
function(a: any, b: any, c: any, d: any)
return Ops.crop(a, b, c, d, b)
end,
function(a: any, b: any, c: any, _d: any)
return Ops.resize(a, b, c, "lanczos")
end,
function(a: any, b: any, c: any, _d: any)
return Ops.resize(a, b, c, b)
end,
function(a: any, b: any, _c: any, _d: any)
return Ops.rotate(a, b)
end,
function(a: any, b: any, _c: any, _d: any)
return Ops.rotate90(a, b)
end,
function(a: any, b: any, _c: any, _d: any)
return Ops.flip(a, b)
end,
function(a: any, b: any, c: any, d: any)
return Ops.blit(a, d, b, c)
end,
function(a: any, b: any, c: any, d: any)
return Ops.blit(a, a, b, c, { opacity = d })
end,
function(a: any, b: any, c: any, _d: any)
return Ops.getPixel(a, b, c)
end,
function(a: any, b: any, c: any, d: any)
return Ops.setPixel(a, b, c, d, d, d)
end,
function(a: any, b: any, _c: any, _d: any)
return Ops.new(a, b, "rgba8")
end,
function(a: any, _b: any, _c: any, _d: any)
return Ops.coalesce(a)
end,
function(a: any, b: any, _c: any, _d: any)
return Ops.fromFrames({ a, b } :: any)
end,
function(a: any, b: any, c: any, d: any)
return Ops.fill(a, b, c, d)
end,
}
local tries, threw = 0, 0
for _ = 1, 600 do
local f = calls[1 + rand(#calls)]
local a = imgs[1 + rand(#imgs)]
local args = { junk[1 + rand(#junk)], junk[1 + rand(#junk)], junk[1 + rand(#junk)] }
if rand(3) == 0 then
args[3] = imgs[1 + rand(#imgs)]
end
tries += 1
local ok, err = pcall(f, a, args[1], args[2], args[3])
if not ok then
threw += 1
print(" THREW: " .. tostring(err))
end
end
t:check(threw == 0, "bad arguments never throw (" .. threw .. " of " .. tries .. ")")
t:note("bad arguments: %d calls, none threw", tries)
local _, e = Ops.resize(rgba, 100000, 100000)
t:check(e and e.code == Err.LIMIT, "resize over the pixel limit")
local _, e2 = Ops.crop(rgba, 100, 100, 5, 5)
t:check(e2 and e2.code == Err.ARG, "empty crop")
local _, e3 = Ops.convert(rgba, "cmyk")
t:check(e3 and e3.code == Err.ARG, "unknown format")
end
t:done()