ws-apifix: F.ops.layers.flatten applies the mask of a passThrough group (codecs/image/Blend lerp takes a mask)

This commit is contained in:
4DBug committed 2026-10-31 13:50:05 -05:00
1 parent de4f14a343
commit 25d8e4ff25
3 files changed
+51 -12

No files matched your search

+36 -8
View File
@@ -273,14 +273,40 @@ local function composite(dst: buffer, dw: number, dh: number, src: buffer, sw: n
end
end
-- lerp(dst, from, t, n): dst = from + (dst - from) * t per byte over n pixels (pass-through group opacity)
local function lerp(dst: buffer, from: buffer, t: number, n: number)
-- lerp(dst, from, t, n, w?, mask?): dst = from + (dst - from) * t per byte over n pixels (pass-through group opacity);
-- with a mask ({ pixels, width, height, x, y, default } as for composite, in the coordinates of a w-wide canvas) t is
-- also scaled by the mask value of each pixel
local function lerp(dst: buffer, from: buffer, t: number, n: number, w: number?, mask: any?)
if blen(dst) < n * 4 or blen(from) < n * 4 then
return
end
for i = 0, n * 4 - 1 do
local a = readu8(from, i)
writeu8(dst, i, floor(a + (readu8(dst, i) - a) * t + 0.5))
if mask == nil or w == nil or w <= 0 then
for i = 0, n * 4 - 1 do
local a = readu8(from, i)
writeu8(dst, i, floor(a + (readu8(dst, i) - a) * t + 0.5))
end
return
end
local mbuf: buffer? = mask.pixels
local mw, mh = floor(tonumber(mask.width) or 0), floor(tonumber(mask.height) or 0)
local mx0, my0 = floor(tonumber(mask.x) or 0), floor(tonumber(mask.y) or 0)
local mdef = (tonumber(mask.default) or 255) / 255
if type(mbuf) ~= "buffer" or mw <= 0 or mh <= 0 or blen(mbuf) < mw * mh then
mbuf = nil
end
for p = 0, n - 1 do
local px, py = p % w, p // w
local mv = mdef
local mxx, myy = px - mx0, py - my0
if mbuf ~= nil and mxx >= 0 and myy >= 0 and mxx < mw and myy < mh then
mv = readu8(mbuf, myy * mw + mxx) / 255
end
local tt = t * mv
local o = p * 4
for k = o, o + 3 do
local a = readu8(from, k)
writeu8(dst, k, floor(a + (readu8(dst, k) - a) * tt + 0.5))
end
end
end
Blend.composite = composite
@@ -330,8 +356,10 @@ local function compose(layers: { any }, canvas: buffer, w: number, h: number, hi
local before = buffer.create(w * h * 4)
buffer.copy(before, 0, canvas, 0, w * h * 4)
compose(l.children, canvas, w, h, hidden)
if opacity < 1 then
lerp(canvas, before, opacity, w * h)
-- the group's opacity and mask limit how much of its effect reaches the canvas (as in Photoshop)
local gm = maskOf(l)
if opacity < 1 or gm ~= nil then
lerp(canvas, before, opacity, w * h, w, gm)
end
continue
end
@@ -360,7 +388,7 @@ end
-- flatten(layers, canvas, w, h, hidden): composites a Types.Layer tree (bottom to top) onto the w x h rgba8 canvas:
-- blend modes, opacity x fillOpacity, groups (isolated, or "passThrough": blended straight onto the canvas, the group
-- opacity then mixing before / after), clipping layers (clip = true: source-atop onto the layer below), grey masks.
-- opacity and mask then mixing before / after), clipping layers (clip = true: source-atop onto the layer below), grey masks.
-- hidden = true includes invisible layers.
function Blend.flatten(layers: { any }, canvas: buffer, w: number, h: number, hidden: boolean)
if blen(canvas) >= w * h * 4 then