ws-apifix: F.ops.layers.flatten applies the mask of a passThrough group (codecs/image/Blend lerp takes a mask)
Co-Authored-By: Claude Opus 5.5 <[email protected]>
This commit is contained in:
1 parent
d3dbba500b
commit
90c5886f61
3 files changed
+51
-12
No files matched your search
@@ -58,7 +58,8 @@ assert(flat.width == 48 and F.detect(png) == "png")
|
||||
|
||||
**Groups.** A group with `blend = "passThrough"` blends its children straight onto what is below (as Photoshop's
|
||||
pass-through and Aseprite's plain groups). Any other blend first composites the children on their own transparent
|
||||
canvas, then blends that result with the group's mode and opacity.
|
||||
canvas, then blends that result with the group's mode and opacity. A group's mask applies either way: on a
|
||||
pass-through group it limits, pixel by pixel, how much of the children's effect reaches the canvas.
|
||||
|
||||
**Format extras.**
|
||||
|
||||
@@ -164,7 +165,6 @@ doc.layers = { L.layer("Paper", paper), L.group("Art", { inkLayer, shadeLayer })
|
||||
|
||||
local mask = assert(F.ops.image.new(32, 16, "gray8"))
|
||||
F.ops.image.fill(mask, 255, 255, 255) -- show the top half
|
||||
doc.layers[2].blend = "normal" -- an isolated group (masks need one, see Gotchas)
|
||||
doc.layers[2].mask = { image = mask, x = 0, y = 0, default = 0 } -- hide the rest
|
||||
|
||||
local flat = assert(F.ops.layers.flatten(doc))
|
||||
@@ -244,6 +244,4 @@ See [FORMATS.md](../FORMATS.md) for the capability matrix.
|
||||
not rendered, so the result can differ from Photoshop's `meta.composite`.
|
||||
- Linked Aseprite cels share one Image table between frames: copy it (`F.ops.image.copy`) before editing a single
|
||||
frame.
|
||||
- `flatten` ignores the mask of a `passThrough` group (Photoshop applies it). Give a masked group a real blend mode
|
||||
such as `"normal"` to isolate it.
|
||||
- Pixels are straight (not premultiplied) alpha, like every Image.
|
||||
@@ -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
|
||||
|
||||
@@ -110,6 +110,19 @@ do
|
||||
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)
|
||||
-- a pass-through group's mask limits its effect: black hides it, grey mixes, outside the mask = its default
|
||||
g.opacity = 1
|
||||
local m = Types.Image.new(1, 1, "gray8")
|
||||
g.mask = { image = m, x = 0, y = 0, default = 255 }
|
||||
t:eq((get(LayerOps.flatten(doc({ bottom, g })))), 200, "pass-through: black mask hides the group")
|
||||
buffer.writeu8(m.pixels, 0, 128)
|
||||
local r4 = get(LayerOps.flatten(doc({ bottom, g })))
|
||||
t:check(math.abs(r4 - 150) <= 1, "pass-through: grey mask mixes: " .. r4)
|
||||
g.mask = { image = m, x = 3, y = 3, default = 0 }
|
||||
t:eq((get(LayerOps.flatten(doc({ bottom, g })))), 200, "pass-through: outside the mask, default black")
|
||||
g.mask.disabled = true
|
||||
t:check(math.abs(get(LayerOps.flatten(doc({ bottom, g }))) - 100) <= 1, "pass-through: disabled mask ignored")
|
||||
g.mask = nil
|
||||
end
|
||||
-- frames, leaves, flattenFrames
|
||||
do
|
||||
|
||||
Reference in new issue
Block a user