-- ops/LayerOps + codecs/Blend tests: blend mode formulas on single pixels against hand-computed W3C values, opacity, -- masks, clipping, isolated vs pass-through groups, hidden layers, frames, leaves, bad arguments. (Whole-document -- flattening against psd-tools is in tests/layered/psd.luau.) local T = require("@tests/T") local LayerOps = require("@formats/ops/LayerOps") local Blend = require("@formats/codecs/Blend") local Types = require("@formats/Types") local Err = require("@formats/core/Err") local t = T.suite("layerops") local function px(r: number, g: number, b: number, a: number): Types.Image local img = Types.Image.new(1, 1) buffer.writeu8(img.pixels, 0, r) buffer.writeu8(img.pixels, 1, g) buffer.writeu8(img.pixels, 2, b) buffer.writeu8(img.pixels, 3, a) return img end local function get(img: any): (number, number, number, number) local p = img.pixels return buffer.readu8(p, 0), buffer.readu8(p, 1), buffer.readu8(p, 2), buffer.readu8(p, 3) end local function doc(layers: { any }): any local d = Types.Layered.new(1, 1) d.layers = layers return d end local L = Types.Layered.layer -- separable modes on opaque pixels: backdrop (200, 100, 50), source (100, 150, 250); values from the W3C formulas local cases = { normal = { 100, 150, 250 }, multiply = { 78, 59, 49 }, screen = { 222, 191, 251 }, darken = { 100, 100, 50 }, lighten = { 200, 150, 250 }, difference = { 100, 50, 200 }, exclusion = { 143, 132, 202 }, linearDodge = { 255, 250, 255 }, linearBurn = { 45, 0, 45 }, subtract = { 100, 0, 0 }, overlay = { 188, 118, 98 }, hardLight = { 157, 127, 247 }, colorDodge = { 255, 243, 255 }, colorBurn = { 115, 0, 46 }, hardMix = { 255, 0, 255 }, } for mode, want in cases do local top = L("top", px(100, 150, 250, 255)) top.blend = mode local img = LayerOps.flatten(doc({ L("bottom", px(200, 100, 50, 255)), top })) :: any local r, g, b, a = get(img) t:check(math.abs(r - want[1]) <= 1 and math.abs(g - want[2]) <= 1 and math.abs(b - want[3]) <= 1 and a == 255, string.format("%s: got %d %d %d, want %d %d %d", mode, r, g, b, want[1], want[2], want[3])) end -- non-separable: luminosity of the source on the backdrop's hue do local top = L("top", px(255, 255, 255, 255)) top.blend = "luminosity" local r, g, b = get(LayerOps.flatten(doc({ L("bottom", px(255, 0, 0, 255)), top }))) t:check(r == 255 and g == 255 and b == 255, "luminosity of white") top.blend = "color" local r2, g2, b2 = get(LayerOps.flatten(doc({ L("bottom", px(128, 128, 128, 255)), top }))) t:check(r2 == g2 and g2 == b2 and math.abs(r2 - 128) <= 1, "colour of a grey source keeps the grey") end -- opacity and alpha: 50% red over opaque blue do local top = L("top", px(255, 0, 0, 255)) top.opacity = 0.5 local r, g, b, a = get(LayerOps.flatten(doc({ L("bottom", px(0, 0, 255, 255)), top }))) t:check(math.abs(r - 128) <= 1 and g == 0 and math.abs(b - 128) <= 1 and a == 255, "opacity mix") local r2, _, _, a2 = get(LayerOps.flatten(doc({ top }))) t:check(r2 == 255 and math.abs(a2 - 128) <= 1, "over transparent: alpha scales") end -- hidden layers, masks, clipping do local top = L("top", px(255, 0, 0, 255)) top.visible = false local r = get(LayerOps.flatten(doc({ L("bottom", px(0, 0, 255, 255)), top }))) t:eq(r, 0, "hidden layer skipped") local r2 = get(LayerOps.flatten(doc({ L("bottom", px(0, 0, 255, 255)), top }), { hidden = true })) t:eq(r2, 255, "hidden = true includes it") top.visible = true local m = Types.Image.new(1, 1, "gray8") top.mask = { image = m, x = 0, y = 0, default = 255 } t:eq((get(LayerOps.flatten(doc({ L("bottom", px(0, 0, 255, 255)), top })))), 0, "black mask hides") top.mask = { image = m, x = 5, y = 5, default = 255 } t:eq((get(LayerOps.flatten(doc({ L("bottom", px(0, 0, 255, 255)), top })))), 255, "outside the mask: default white") top.mask = nil local base = L("base", px(0, 255, 0, 0)) -- transparent base: the clipped layer shows nowhere top.clip = true local r3, g3, b3, a3 = get(LayerOps.flatten(doc({ base, top }))) t:check(a3 == 0, "clipped to a transparent base: " .. r3 .. g3 .. b3 .. a3) base = L("base", px(0, 255, 0, 255)) local r4, g4 = get(LayerOps.flatten(doc({ base, top }))) t:check(r4 == 255 and g4 == 0, "clipped to an opaque base") end -- groups: an isolated multiply group multiplies its composite; pass-through multiplies each child onto the backdrop do local child = L("child", px(128, 128, 128, 255)) child.blend = "multiply" local g = Types.Layered.group("g", { child }) local bottom = L("bottom", px(200, 200, 200, 255)) local r = get(LayerOps.flatten(doc({ bottom, g }))) t:check(math.abs(r - 100) <= 1, "pass-through: child multiplies the backdrop: " .. r) g.blend = "normal" local r2 = get(LayerOps.flatten(doc({ bottom, g }))) t:eq(r2, 128, "isolated group: the child multiplies nothing") g.blend = "passThrough" g.opacity = 0.5 local r3 = get(LayerOps.flatten(doc({ bottom, g }))) t:check(math.abs(r3 - 150) <= 1, "pass-through opacity mixes before / after: " .. r3) end -- frames, leaves, flattenFrames do local d = doc({ L("a", px(1, 2, 3, 255)) }) d.frames = { { layers = d.layers, delay = 0.1 }, { layers = { L("b", px(9, 8, 7, 255)) }, delay = 0.2 } } t:eq((get(LayerOps.flatten(d, { frame = 2 }))), 9, "frame 2") local anim = LayerOps.flattenFrames(d) :: any t:check(anim.frames and #anim.frames == 2 and anim.frames[2].delay == 0.2, "flattenFrames") local _, e = LayerOps.flatten(d, { frame = 5 }) t:check(e and e.code == Err.ARG, "missing frame") local names = {} for l, depth in LayerOps.leaves({ L("x", px(0, 0, 0, 0)), Types.Layered.group("g", { L("y", px(0, 0, 0, 0)), Types.Layered.group("h", { L("z", px(0, 0, 0, 0)) }) }) }) do table.insert(names, (l :: any).name .. depth) end t:eq(table.concat(names, " "), "x0 y1 z2", "leaves with depth") local _, e2 = LayerOps.flatten(5 :: any) t:check(e2 and e2.code == Err.ARG, "flatten of a non-Layered") end -- Blend.composite clips its rectangles (no throw, nothing outside) do local dst = buffer.create(4 * 4 * 4) local src = buffer.create(3 * 3 * 4) buffer.fill(src, 0, 255) for _, pos in { { -2, -2 }, { 3, 3 }, { -10, 0 }, { 100, 100 } } do Blend.composite(dst, 4, 4, src, 3, 3, pos[1], pos[2], 1, "multiply") end Blend.composite(dst, 4, 4, src, 3, 3, 0, 0, 1, "nope") Blend.composite(dst, 4, 4, src, 30, 30, 0, 0, 1, "normal") -- src too short: ignored t:eq(buffer.readu8(dst, 0), 255, "composite clipping") t:check(Blend.MODES.softLight and not Blend.MODES.nope, "MODES") end t:done()