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:
1 parent
de4f14a343
commit
25d8e4ff25
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
|
**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
|
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.**
|
**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"))
|
local mask = assert(F.ops.image.new(32, 16, "gray8"))
|
||||||
F.ops.image.fill(mask, 255, 255, 255) -- show the top half
|
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
|
doc.layers[2].mask = { image = mask, x = 0, y = 0, default = 0 } -- hide the rest
|
||||||
|
|
||||||
local flat = assert(F.ops.layers.flatten(doc))
|
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`.
|
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
|
- Linked Aseprite cels share one Image table between frames: copy it (`F.ops.image.copy`) before editing a single
|
||||||
frame.
|
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.
|
- 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
|
||||||
end
|
end
|
||||||
|
|
||||||
-- lerp(dst, from, t, n): dst = from + (dst - from) * t per byte over n pixels (pass-through group opacity)
|
-- lerp(dst, from, t, n, w?, mask?): 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)
|
-- 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
|
if blen(dst) < n * 4 or blen(from) < n * 4 then
|
||||||
return
|
return
|
||||||
end
|
end
|
||||||
for i = 0, n * 4 - 1 do
|
if mask == nil or w == nil or w <= 0 then
|
||||||
local a = readu8(from, i)
|
for i = 0, n * 4 - 1 do
|
||||||
writeu8(dst, i, floor(a + (readu8(dst, i) - a) * t + 0.5))
|
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
|
||||||
end
|
end
|
||||||
Blend.composite = composite
|
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)
|
local before = buffer.create(w * h * 4)
|
||||||
buffer.copy(before, 0, canvas, 0, w * h * 4)
|
buffer.copy(before, 0, canvas, 0, w * h * 4)
|
||||||
compose(l.children, canvas, w, h, hidden)
|
compose(l.children, canvas, w, h, hidden)
|
||||||
if opacity < 1 then
|
-- the group's opacity and mask limit how much of its effect reaches the canvas (as in Photoshop)
|
||||||
lerp(canvas, before, opacity, w * h)
|
local gm = maskOf(l)
|
||||||
|
if opacity < 1 or gm ~= nil then
|
||||||
|
lerp(canvas, before, opacity, w * h, w, gm)
|
||||||
end
|
end
|
||||||
continue
|
continue
|
||||||
end
|
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:
|
-- 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
|
-- 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.
|
-- hidden = true includes invisible layers.
|
||||||
function Blend.flatten(layers: { any }, canvas: buffer, w: number, h: number, hidden: boolean)
|
function Blend.flatten(layers: { any }, canvas: buffer, w: number, h: number, hidden: boolean)
|
||||||
if blen(canvas) >= w * h * 4 then
|
if blen(canvas) >= w * h * 4 then
|
||||||
|
|||||||
@@ -110,6 +110,19 @@ do
|
|||||||
g.opacity = 0.5
|
g.opacity = 0.5
|
||||||
local r3 = get(LayerOps.flatten(doc({ bottom, g })))
|
local r3 = get(LayerOps.flatten(doc({ bottom, g })))
|
||||||
t:check(math.abs(r3 - 150) <= 1, "pass-through opacity mixes before / after: " .. r3)
|
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
|
end
|
||||||
-- frames, leaves, flattenFrames
|
-- frames, leaves, flattenFrames
|
||||||
do
|
do
|
||||||
|
|||||||
Reference in new issue
Block a user