Files
rosetta/src/Formats/codecs/Blend.luau
T

372 lines
13 KiB
Luau

--!strict
--!native
-- codecs/Blend: layer compositing on rgba8 buffers (straight alpha): source-over with the separable and non-separable
-- blend modes of the W3C Compositing and Blending Level 1 spec (the Photoshop / Aseprite set), opacity, a grey layer
-- mask, clipping (source-atop: the backdrop keeps its alpha) and dissolve. Shared by ops/LayerOps (flatten) and the
-- layered formats that must write a composite (layered/psd). Never throws: rectangles are clipped to both buffers.
--
-- Blend.composite(dst, dw, dh, src, sw, sh, x, y, opacity, "multiply", { clip = false, mask = m })
-- Blend.MODES.multiply -> true
--
-- Mode names: normal dissolve darken multiply colorBurn linearBurn darkerColor lighten screen colorDodge linearDodge
-- (= add) lighterColor overlay softLight hardLight vividLight linearLight pinLight hardMix difference exclusion
-- subtract divide hue saturation color luminosity. Unknown names composite as normal.
local Blend = {}
local readu8, writeu8, readu32, writeu32, blen = buffer.readu8, buffer.writeu8, buffer.readu32, buffer.writeu32, buffer.len
local min, max, floor, sqrt, abs = math.min, math.max, math.floor, math.sqrt, math.abs
local IDS: { [string]: number } = {
normal = 0,
dissolve = 1,
darken = 2,
multiply = 3,
colorBurn = 4,
linearBurn = 5,
darkerColor = 6,
lighten = 7,
screen = 8,
colorDodge = 9,
linearDodge = 10,
add = 10,
lighterColor = 11,
overlay = 12,
softLight = 13,
hardLight = 14,
vividLight = 15,
linearLight = 16,
pinLight = 17,
hardMix = 18,
difference = 19,
exclusion = 20,
subtract = 21,
divide = 22,
hue = 23,
saturation = 24,
color = 25,
luminosity = 26,
}
local MODES: { [string]: boolean } = {}
for k in IDS do
MODES[k] = true
end
Blend.MODES = table.freeze(MODES)
local function burn(cb: number, cs: number): number
if cb >= 1 then
return 1
elseif cs <= 0 then
return 0
end
return 1 - min(1, (1 - cb) / cs)
end
local function dodge(cb: number, cs: number): number
if cb <= 0 then
return 0
elseif cs >= 1 then
return 1
end
return min(1, cb / (1 - cs))
end
-- B(cb, cs) of a separable mode
local function sep(m: number, cb: number, cs: number): number
if m == 3 then
return cb * cs
elseif m == 8 then
return cb + cs - cb * cs
elseif m == 2 then
return min(cb, cs)
elseif m == 7 then
return max(cb, cs)
elseif m == 12 then -- overlay = hardLight(cs, cb)
if cb <= 0.5 then
return 2 * cb * cs
end
local c = 2 * cb - 1
return cs + c - cs * c
elseif m == 14 then
if cs <= 0.5 then
return 2 * cb * cs
end
local c = 2 * cs - 1
return cb + c - cb * c
elseif m == 13 then
if cs <= 0.5 then
return cb - (1 - 2 * cs) * cb * (1 - cb)
end
local d = if cb <= 0.25 then ((16 * cb - 12) * cb + 4) * cb else sqrt(cb)
return cb + (2 * cs - 1) * (d - cb)
elseif m == 9 then
return dodge(cb, cs)
elseif m == 4 then
return burn(cb, cs)
elseif m == 5 then
return max(0, cb + cs - 1)
elseif m == 10 then
return min(1, cb + cs)
elseif m == 15 then
if cs <= 0.5 then
return burn(cb, 2 * cs)
end
return dodge(cb, 2 * cs - 1)
elseif m == 16 then
return max(0, min(1, cb + 2 * cs - 1))
elseif m == 17 then
if cs <= 0.5 then
return min(cb, 2 * cs)
end
return max(cb, 2 * cs - 1)
elseif m == 18 then
return if cb + cs >= 1 then 1 else 0
elseif m == 19 then
return abs(cb - cs)
elseif m == 20 then
return cb + cs - 2 * cb * cs
elseif m == 21 then
return max(0, cb - cs)
elseif m == 22 then
if cs <= 0 then
return if cb <= 0 then 0 else 1
end
return min(1, cb / cs)
end
return cs
end
-- non-separable helpers (W3C): lum, clip, setLum, sat, setSat
local function lum(r: number, g: number, b: number): number
return 0.3 * r + 0.59 * g + 0.11 * b
end
local function clipColor(r: number, g: number, b: number): (number, number, number)
local l = lum(r, g, b)
local n = min(r, g, b)
local x = max(r, g, b)
if n < 0 and l - n > 1e-12 then
r, g, b = l + (r - l) * l / (l - n), l + (g - l) * l / (l - n), l + (b - l) * l / (l - n)
end
if x > 1 and x - l > 1e-12 then
r, g, b = l + (r - l) * (1 - l) / (x - l), l + (g - l) * (1 - l) / (x - l), l + (b - l) * (1 - l) / (x - l)
end
return r, g, b
end
local function setLum(r: number, g: number, b: number, l: number): (number, number, number)
local d = l - lum(r, g, b)
return clipColor(r + d, g + d, b + d)
end
local function setSat(r: number, g: number, b: number, s: number): (number, number, number)
local mx = max(r, g, b)
local mn = min(r, g, b)
if mx - mn <= 1e-12 then
return 0, 0, 0
end
local k = s / (mx - mn)
return (r - mn) * k, (g - mn) * k, (b - mn) * k
end
-- composite(dst, dw, dh, src, sw, sh, x, y, opacity, mode, opts?): blends the sw x sh rgba8 src onto the dw x dh rgba8
-- dst with its top-left at (x, y). opacity 0..1. opts.clip: keep the backdrop's alpha (clipping masks). opts.mask:
-- { pixels: buffer (1 byte per pixel), width, height, x, y, default: 0..255 } in dst coordinates (outside it: default).
local function composite(dst: buffer, dw: number, dh: number, src: buffer, sw: number, sh: number, x: number, y: number, opacity: number, mode: string?, opts: { clip: boolean?, mask: any? }?)
x, y = floor(x), floor(y)
if opacity <= 0 or dw <= 0 or dh <= 0 or sw <= 0 or sh <= 0 then
return
end
if blen(dst) < dw * dh * 4 or blen(src) < sw * sh * 4 then
return
end
local op = min(1, opacity)
local m = IDS[mode or "normal"] or 0
local clip = opts ~= nil and opts.clip == true
local mask = if opts ~= nil then opts.mask else nil
local mbuf: buffer? = nil
local mx0, my0, mw, mh, mdef = 0, 0, 0, 0, 1
if type(mask) == "table" and type(mask.pixels) == "buffer" and type(mask.width) == "number" and type(mask.height) == "number" then
mw, mh = floor(mask.width), floor(mask.height)
if mw > 0 and mh > 0 and blen(mask.pixels) >= mw * mh then
mbuf = mask.pixels
end
mx0, my0 = floor(tonumber(mask.x) or 0), floor(tonumber(mask.y) or 0)
mdef = (tonumber(mask.default) or 255) / 255
elseif type(mask) == "table" then
mdef = (tonumber(mask.default) or 255) / 255
end
local x0, y0 = max(0, x), max(0, y)
local x1, y1 = min(dw, x + sw), min(dh, y + sh)
for py = y0, y1 - 1 do
local so = ((py - y) * sw + (x0 - x)) * 4
local do_ = (py * dw + x0) * 4
for px = x0, x1 - 1 do
local sa8 = readu8(src, so + 3)
if sa8 ~= 0 then
local sa = sa8 / 255 * op
if mbuf ~= nil or mdef ~= 1 then
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
sa *= mv
end
if m == 1 then -- dissolve: a hash threshold per pixel, full alpha where it shows
local h = (px * 73856093 + py * 19349663) % 65536 / 65536
sa = if h < sa then 1 else 0
end
local ba8 = readu8(dst, do_ + 3)
if sa > 0 and not (clip and ba8 == 0) then
local sr, sg, sb = readu8(src, so) / 255, readu8(src, so + 1) / 255, readu8(src, so + 2) / 255
if ba8 == 0 then
-- empty backdrop: the source as it is (alpha scaled)
writeu32(dst, do_, readu32(src, so))
writeu8(dst, do_ + 3, floor(sa * 255 + 0.5))
else
local ba = ba8 / 255
local br, bg, bb = readu8(dst, do_) / 255, readu8(dst, do_ + 1) / 255, readu8(dst, do_ + 2) / 255
local mr, mg, mb = sr, sg, sb
if m >= 2 and m ~= 6 and m ~= 11 and m < 23 then
mr, mg, mb = sep(m, br, sr), sep(m, bg, sg), sep(m, bb, sb)
elseif m == 6 then
if lum(sr, sg, sb) >= lum(br, bg, bb) then
mr, mg, mb = br, bg, bb
end
elseif m == 11 then
if lum(sr, sg, sb) < lum(br, bg, bb) then
mr, mg, mb = br, bg, bb
end
elseif m == 23 then
local s = max(br, bg, bb) - min(br, bg, bb)
local r1, g1, b1 = setSat(sr, sg, sb, s)
mr, mg, mb = setLum(r1, g1, b1, lum(br, bg, bb))
elseif m == 24 then
local s = max(sr, sg, sb) - min(sr, sg, sb)
local r1, g1, b1 = setSat(br, bg, bb, s)
mr, mg, mb = setLum(r1, g1, b1, lum(br, bg, bb))
elseif m == 25 then
mr, mg, mb = setLum(sr, sg, sb, lum(br, bg, bb))
elseif m == 26 then
mr, mg, mb = setLum(br, bg, bb, lum(sr, sg, sb))
end
local orr, og, ob, oa
if clip then
-- source-atop: the backdrop's alpha; the mixed colour where the backdrop is opaque
orr = sa * mr + (1 - sa) * br
og = sa * mg + (1 - sa) * bg
ob = sa * mb + (1 - sa) * bb
oa = ba
else
oa = sa + ba * (1 - sa)
local ws, wm, wb = sa * (1 - ba), sa * ba, (1 - sa) * ba
orr = (ws * sr + wm * mr + wb * br) / oa
og = (ws * sg + wm * mg + wb * bg) / oa
ob = (ws * sb + wm * mb + wb * bb) / oa
end
writeu8(dst, do_, floor(max(0, min(1, orr)) * 255 + 0.5))
writeu8(dst, do_ + 1, floor(max(0, min(1, og)) * 255 + 0.5))
writeu8(dst, do_ + 2, floor(max(0, min(1, ob)) * 255 + 0.5))
writeu8(dst, do_ + 3, floor(max(0, min(1, oa)) * 255 + 0.5))
end
end
end
so += 4
do_ += 4
end
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)
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))
end
end
Blend.composite = composite
Blend.lerp = lerp
-- ------------------------------------------------------------------------------------------------ layer trees
local function isImage(v: any): boolean
return type(v) == "table" and v.format == "rgba8" and type(v.pixels) == "buffer" and type(v.width) == "number" and type(v.height) == "number" and buffer.len(v.pixels) >= v.width * v.height * 4
end
local function maskOf(l: any): any
local m = l.mask
if type(m) ~= "table" or m.disabled or type(m.image) ~= "table" or type(m.image.pixels) ~= "buffer" then
return nil
end
local img = m.image
local px = img.pixels
if img.format == "rgba8" then -- use the red channel
local n = img.width * img.height
local g = buffer.create(n)
for i = 0, n - 1 do
buffer.writeu8(g, i, buffer.readu8(px, i * 4))
end
px = g
end
return { pixels = px, width = img.width, height = img.height, x = m.x or 0, y = m.y or 0, default = m.default or 255 }
end
local function compose(layers: { any }, canvas: buffer, w: number, h: number, hidden: boolean)
local i = 1
while i <= #layers do
local l = layers[i]
i += 1
if type(l) ~= "table" or (l.visible == false and not hidden) then
-- clipped layers above a hidden base are hidden too
while i <= #layers and type(layers[i]) == "table" and layers[i].clip do
i += 1
end
continue
end
local opacity = (tonumber(l.opacity) or 1) * (tonumber(l.fillOpacity) or 1)
local mode = if type(l.blend) == "string" then l.blend else "normal"
-- the layer's own pixels, as an isolated w x h buffer
local src: buffer? = nil
if type(l.children) == "table" then
if mode == "passThrough" then
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)
end
continue
end
src = buffer.create(w * h * 4)
compose(l.children, src :: buffer, w, h, hidden)
elseif isImage(l.image) then
if l.image.width > 0 and l.image.height > 0 then
local b = buffer.create(w * h * 4)
composite(b, w, h, l.image.pixels, l.image.width, l.image.height, tonumber(l.x) or 0, tonumber(l.y) or 0, 1, "normal")
src = b
end
end
-- clipping layers above this one: composited onto it with their alpha limited to it
while i <= #layers and type(layers[i]) == "table" and layers[i].clip do
local c = layers[i]
i += 1
if src and (c.visible ~= false or hidden) and isImage(c.image) then
composite(src :: buffer, w, h, c.image.pixels, c.image.width, c.image.height, tonumber(c.x) or 0, tonumber(c.y) or 0, (tonumber(c.opacity) or 1) * (tonumber(c.fillOpacity) or 1), c.blend, { clip = true, mask = maskOf(c) })
end
end
if src then
composite(canvas, w, h, src :: buffer, w, h, 0, 0, opacity, mode, { mask = maskOf(l) })
end
end
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.
-- 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
compose(layers, canvas, w, h, hidden)
end
end
return Blend