-- codecs/image/Raster tests: exact coverage of axis-aligned rectangles at fractional positions; areas of circles; random -- polygons and curves under random transforms against an independent 12 x 12 supersampled winding-number reference -- (both fill rules); clipping, the stroker (width, caps, joins against analytic areas), fill() compositing and paint -- functions; malformed paths never throw. Glyph rendering against FreeType is in tests/font/ttf.luau. local T = require("@tests/T") local R = require("@formats/codecs/image/Raster") local Types = require("@formats/Types") local t = T.suite("raster") local function sum(m: any): number if not m then return 0 end local s = 0 for i = 0, m.width * m.height - 1 do s += buffer.readu8(m.coverage, i) end return s / 255 end local function at(m: any, x: number, y: number): number local i, j = x - m.x, y - m.y if i < 0 or j < 0 or i >= m.width or j >= m.height then return 0 end return buffer.readu8(m.coverage, j * m.width + i) end -- 1. rectangles: exact per-pixel coverage do local p = R.path():rect(1.25, 2.5, 3.5, 2) local m = R.mask(p) :: any t:check(m, "rect mask") t:eq(m.x, 1, "rect x") t:eq(m.y, 2, "rect y") t:eq(m.width, 4, "rect width") t:eq(m.height, 3, "rect height") -- row 2 covers y 2.5..3 (half), columns: 1 covers 0.75, 2-3 full, 4 covers 0.75 t:eq(at(m, 1, 2), math.floor(0.75 * 0.5 * 255 + 0.5), "corner pixel") t:eq(at(m, 2, 2), math.floor(0.5 * 255 + 0.5), "top edge pixel") t:eq(at(m, 2, 3), 255, "interior pixel") t:eq(at(m, 4, 3), math.floor(0.75 * 255 + 0.5), "right edge pixel") t:check(math.abs(sum(m) - 7) < 0.02, "rect area 7: " .. sum(m)) -- reversed orientation gives the same coverage local q = R.path():moveTo(1.25, 2.5):lineTo(1.25, 4.5):lineTo(4.75, 4.5):lineTo(4.75, 2.5):close() t:check(math.abs(sum(R.mask(q)) - 7) < 0.02, "reversed rect") end -- 2. circles: area pi r^2 (cubic approximation error ~ 2.7e-4 r) for _, r in { 3, 10.5, 40 } do local m = R.mask(R.path():ellipse(50.3, 50.7, r, r)) local a = sum(m) t:check(math.abs(a - math.pi * r * r) < 0.01 * math.pi * r * r + 1, string.format("circle r=%g area %.2f", r, a)) end -- 3. random paths against a supersampled winding reference local function wind(polys: { { number } }, px: number, py: number): number local w = 0 for _, poly in polys do local n = #poly local x0, y0 = poly[n - 1], poly[n] for i = 1, n - 1, 2 do local x1, y1 = poly[i], poly[i + 1] if y0 <= py then if y1 > py and (x1 - x0) * (py - y0) - (px - x0) * (y1 - y0) > 0 then w += 1 end elseif y1 <= py and (x1 - x0) * (py - y0) - (px - x0) * (y1 - y0) < 0 then w -= 1 end x0, y0 = x1, y1 end end return w end local rand = T.rng(7) local function rf(a: number, b: number): number return a + (b - a) * rand(100000) / 100000 end local worst, total, count = 0, 0, 0 for case = 1, 24 do local p = R.path() for _ = 1, 1 + rand(3) do p:moveTo(rf(0, 28), rf(0, 28)) for _ = 1, 2 + rand(5) do local k = rand(3) if k == 0 then p:lineTo(rf(0, 28), rf(0, 28)) elseif k == 1 then p:quadTo(rf(0, 28), rf(0, 28), rf(0, 28), rf(0, 28)) else p:cubicTo(rf(0, 28), rf(0, 28), rf(0, 28), rf(0, 28), rf(0, 28), rf(0, 28)) end end p:close() end local ang = rf(0, 6.28) local tr = R.multiply(R.translate(rf(-5, 5), rf(-5, 5)), R.multiply(R.rotate(ang), R.scale(rf(0.5, 1.2), rf(0.5, 1.2)))) local rule = if case % 2 == 0 then "evenodd" else "nonzero" local clip = { 0, 0, 32, 32 } local m = R.mask(p, { transform = tr, rule = rule :: any, clip = clip, tolerance = 0.05 }) local polys = R.flatten(p, tr, 0.01) local err, n, mx = 0, 0, 0 for y = 0, 31 do for x = 0, 31 do local inside = 0 for sy = 0, 11 do for sx = 0, 11 do local w = wind(polys, x + (sx + 0.5) / 12, y + (sy + 0.5) / 12) if (rule == "nonzero" and w ~= 0) or (rule == "evenodd" and w % 2 == 1) then inside += 1 end end end local want = inside / 144 local got = if m then at(m, x, y) / 255 else 0 local d = math.abs(got - want) err += d n += 1 if d > mx then mx = d end end end total += err count += n worst = math.max(worst, err / n) -- the area rules approximate where edges of opposite winding meet inside one pixel; on average they agree t:check(err / n < 0.015, string.format("case %d (%s): mean abs error %.4f (max %.3f)", case, rule, err / n, mx)) end t:note("random paths: mean abs coverage error %.4f (worst case mean %.4f) vs 12x12 supersampling", total / count, worst) -- 4. even-odd vs nonzero on two nested squares of the same orientation do local p = R.path():rect(0, 0, 10, 10):rect(3, 3, 4, 4) t:check(math.abs(sum(R.mask(p)) - 100) < 0.01, "nonzero fills the hole") t:check(math.abs(sum(R.mask(p, { rule = "evenodd" })) - 84) < 0.01, "evenodd leaves the hole") -- opposite orientation: a hole under both rules local q = R.path():rect(0, 0, 10, 10):moveTo(3, 3):lineTo(3, 7):lineTo(7, 7):lineTo(7, 3):close() t:check(math.abs(sum(R.mask(q)) - 84) < 0.01, "nonzero hole with opposite winding") end -- 5. clipping: a shape larger than the clip, partly outside on every side do local m = R.mask(R.path():rect(-20.5, -3.25, 100, 50), { clip = { 0, 0, 16, 8 } }) :: any t:check(m and m.x == 0 and m.y == 0 and m.width == 16 and m.height == 8, "clipped bounds") t:check(math.abs(sum(m) - 128) < 0.01, "clipped area " .. sum(m)) local m2 = R.mask(R.path():moveTo(-50, 4):lineTo(60, 4.5):lineTo(60, 6):lineTo(-50, 5):close(), { clip = { 0, 0, 10, 10 } }) t:check(m2 ~= nil, "a sliver crossing the clip") t:check(R.mask(R.path():rect(20, 20, 5, 5), { clip = { 0, 0, 10, 10 } }) == nil, "outside the clip is empty") end -- 6. strokes: a line of length L and width w covers L*w (butt), L*w + w^2 (square), L*w + pi w^2/4 (round) do local line = R.path():moveTo(5, 10):lineTo(35, 10) local a = sum(R.mask(R.stroke(line, { width = 4 }))) t:check(math.abs(a - 120) < 0.05, "butt stroke area " .. a) a = sum(R.mask(R.stroke(line, { width = 4, cap = "square" }))) t:check(math.abs(a - 136) < 0.05, "square cap area " .. a) a = sum(R.mask(R.stroke(line, { width = 4, cap = "round", tolerance = 0.01 }))) t:check(math.abs(a - (120 + math.pi * 4)) < 0.3, "round cap area " .. a) -- a closed square outline of side 20, width 2, miter joins: outer 22^2 - inner 18^2 local sq = R.path():rect(10, 10, 20, 20) a = sum(R.mask(R.stroke(sq, { width = 2 }))) t:check(math.abs(a - (484 - 324)) < 0.1, "mitered square outline " .. a) a = sum(R.mask(R.stroke(sq, { width = 2, join = "bevel" }))) t:check(math.abs(a - (160 - 4 * 0.5)) < 0.1, "bevelled square outline " .. a) a = sum(R.mask(R.stroke(sq, { width = 2, join = "round", tolerance = 0.01 }))) t:check(math.abs(a - (160 - 4 * (1 - math.pi / 4))) < 0.15, "rounded square outline " .. a) -- a sharp zig-zag past the miter limit falls back to bevels; still covered without holes local zig = R.path():moveTo(0, 0):lineTo(20, 2):lineTo(0, 4) t:check(sum(R.mask(R.stroke(zig, { width = 1, miterLimit = 4 }))) > 30, "zig-zag stroke") -- a curve stroke: a circle outline of radius 10, width 1: area 2 pi r w a = sum(R.mask(R.stroke(R.path():ellipse(20, 20, 10, 10), { width = 1, join = "round", tolerance = 0.01 }))) t:check(math.abs(a - 2 * math.pi * 10) < 1, "circle outline " .. a) end -- 7. fill onto an image: colours, opacity, paint functions, compositing over existing pixels do local img = Types.Image.new(8, 8) t:check(R.fill(img, R.path():rect(0, 0, 4, 8), { 255, 0, 0, 255 }), "fill red") t:eq(buffer.readu32(img.pixels, 0), 0xFF0000FF, "opaque red pixel") t:eq(buffer.readu32(img.pixels, 4 * 5), 0, "untouched pixel") R.fill(img, R.path():rect(2, 0, 4, 8), { 0, 0, 255, 255 }, { opacity = 0.5 }) local p = 4 * 3 t:check(buffer.readu8(img.pixels, p) == 128 and buffer.readu8(img.pixels, p + 2) == 128 and buffer.readu8(img.pixels, p + 3) == 255, "half blue over red") p = 4 * 5 t:check(buffer.readu8(img.pixels, p + 2) == 255 and buffer.readu8(img.pixels, p + 3) == 128, "half blue over transparent") local calls = 0 R.fill(img, R.path():rect(0, 7, 8, 1), function(x: number, _y: number) calls += 1 return x * 30, 0, 0, 255 end) t:eq(calls, 8, "paint function per covered pixel") t:eq(buffer.readu8(img.pixels, (7 * 8 + 2) * 4), math.floor(2.5 * 30 + 0.5), "paint at the pixel centre") -- a transform scaling a unit square to the whole image local img2 = Types.Image.new(4, 4) R.fill(img2, R.path():rect(0, 0, 1, 1), { 0, 255, 0, 255 }, { transform = R.scale(4) }) t:eq(buffer.readu32(img2.pixels, 4 * 15), 0xFF00FF00, "transformed fill") local inv = R.invert({ 2, 0, 0, 4, 3, 5 }) :: any local x, y = R.apply(inv, R.apply({ 2, 0, 0, 4, 3, 5 }, 7, 11)) t:check(math.abs(x - 7) < 1e-9 and math.abs(y - 11) < 1e-9, "invert") t:check(R.fill(img2, R.path():rect(0, 0, 1, 1), function() error("boom") end) == false, "a failing paint function returns false") end -- 8. malformed paths and huge / non-finite coordinates never throw do local bad: { any } = { { { op = "moveTo" }, { op = "lineTo", 1 }, { op = "quadTo", 1, 2, "x", 4 }, { op = "close" } }, { { op = "lineTo", 0 / 0, 1 }, { op = "lineTo", 5, 5 }, { op = "lineTo", 0, 5 } }, { { op = "moveTo", 0, 0 }, { op = "lineTo", math.huge, 1 }, { op = "lineTo", 1, 1e300 } }, { { op = "moveTo", -1e6, -1e6 }, { op = "lineTo", 1e6, -1e6 }, { op = "lineTo", 1e6, 1e6 } }, { 5, "x", { op = "cubicTo" }, { op = 3 } }, {}, } local threw = 0 for i, p in bad do for _, rule in { "nonzero", "evenodd" } do local ok = pcall(function() R.mask(p, { rule = rule :: any, clip = { 0, 0, 64, 64 } }) R.mask(p, { transform = { 0 / 0, 0, 0, 1, 0, 0 } }) R.stroke(p, { width = 3, cap = "round", join = "round" }) R.fill(Types.Image.new(16, 16), p, { 1, 2, 3, 4 }, { transform = { 1e9, 0, 0, 1e9, 0, 0 } }) end) if not ok then threw += 1 print(" threw on bad path " .. i) end end end local _, why = R.mask(R.path():rect(0, 0, 5000, 5000), { maxPixels = 1000 }) t:eq(why, "limit", "over maxPixels") t:check(R.mask("nope") == nil and R.mask(nil) == nil, "non-paths") -- random command soup through the fuzz helper: bytes -> commands t:fuzz("mask", function(b: buffer) local cmds = {} local ops = { "moveTo", "lineTo", "quadTo", "cubicTo", "close" } local i = 0 while i + 1 < buffer.len(b) do local c: any = { op = ops[buffer.readu8(b, i) % 5 + 1] } for k = 1, 6 do local q = i + k c[k] = if q < buffer.len(b) then (buffer.readi8(b, q)) * 0.73 else 0 end table.insert(cmds, c) i += 7 end R.mask(cmds, { rule = if buffer.len(b) % 2 == 0 then "evenodd" else "nonzero", clip = { -10, -10, 40, 40 } }) return R.stroke(cmds, { width = 2.5, cap = "square", join = "miter" }) end, { "\0\1\2\3\4\5\6\1\9\9\9\9\9\9\2\1\2\3\4\5\6\3\1\2\3\4\5\6\4" }, { garbage = 400 }) t:eq(threw, 0, "malformed paths never throw") end t:done()