codecs/image: comments to specs only
This commit is contained in:
1 parent
0bd7b87d2b
commit
95a6b03f70
30 files changed
+224
-770
No files matched your search
@@ -1,16 +1,7 @@
|
||||
--!strict
|
||||
--!native
|
||||
-- codecs/image/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.
|
||||
-- codecs/image/Blend: rgba8 layer compositing (straight alpha) with the W3C Compositing and Blending Level 1 modes.
|
||||
-- Clipping is source-atop (the backdrop keeps its alpha); unknown mode names composite as normal.
|
||||
local Blend = {}
|
||||
|
||||
local readu8, writeu8, readu32, writeu32, blen = buffer.readu8, buffer.writeu8, buffer.readu32, buffer.writeu32, buffer.len
|
||||
@@ -164,9 +155,6 @@ local function setSat(r: number, g: number, b: number, s: number): (number, numb
|
||||
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
|
||||
@@ -216,7 +204,6 @@ local function composite(dst: buffer, dw: number, dh: number, src: buffer, sw: n
|
||||
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
|
||||
@@ -273,9 +260,6 @@ local function composite(dst: buffer, dw: number, dh: number, src: buffer, sw: n
|
||||
end
|
||||
end
|
||||
|
||||
-- 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
|
||||
@@ -312,7 +296,6 @@ 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
|
||||
@@ -324,7 +307,7 @@ local function maskOf(l: any): any
|
||||
end
|
||||
local img = m.image
|
||||
local px = img.pixels
|
||||
if img.format == "rgba8" then -- use the red channel
|
||||
if img.format == "rgba8" then
|
||||
local n = img.width * img.height
|
||||
local g = buffer.create(n)
|
||||
for i = 0, n - 1 do
|
||||
@@ -349,7 +332,6 @@ local function compose(layers: { any }, canvas: buffer, w: number, h: number, hi
|
||||
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
|
||||
@@ -386,10 +368,6 @@ local function compose(layers: { any }, canvas: buffer, w: number, h: number, hi
|
||||
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 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
|
||||
compose(layers, canvas, w, h, hidden)
|
||||
|
||||
Reference in new issue
Block a user