--!strict --!native -- image/webp/Anim: animation compositing. coalesce() reproduces libwebp's WebPAnimDecoder (src/demux/anim_decode.c, -- BSD-3-Clause, licenses/libwebp-LICENSE.txt) exactly: key frame detection, transparent canvas (the background colour -- is ignored, as the spec recommends and libwebp does), non-premultiplied "over" blending of the pixels with alpha -- < 255, dispose to background. composite() is the generic Types.Frame model (also dispose "previous") used by the -- encoder to flatten frames WebP cannot store as they are. Pixels are rgba8; never throws. local band, rshift = bit32.band, bit32.rshift local readu32, writeu32 = buffer.readu32, buffer.writeu32 local bcreate, bcopy, bfill = buffer.create, buffer.copy, buffer.fill local Anim = {} export type Frame = { rgba: buffer, -- w * h rgba8 x: number, y: number, w: number, h: number, blend: boolean, -- alpha-blend over the canvas (false: overwrite) disposeBg: boolean, -- after display, clear the rectangle to transparent disposePrev: boolean?, -- after display, restore the canvas as it was before the frame (composite() only) hasAlpha: boolean, } -- blend(src, dst) -> src over dst, both non-premultiplied 0xAABBGGRR (libwebp BlendPixelNonPremult) local function blendPixel(src: number, dst: number): number local sa = rshift(src, 24) if sa == 0 then return dst end local da = rshift(dst, 24) local dfa = rshift(da * (256 - sa), 8) local ba = sa + dfa local scale = 16777216 // ba local r = ((band(src, 255) * sa + band(dst, 255) * dfa) * scale) // 16777216 local g = ((band(rshift(src, 8), 255) * sa + band(rshift(dst, 8), 255) * dfa) * scale) // 16777216 local b = ((band(rshift(src, 16), 255) * sa + band(rshift(dst, 16), 255) * dfa) * scale) // 16777216 return r + g * 256 + b * 65536 + ba * 16777216 end Anim.blendPixel = blendPixel -- blendRow(cur, prev, offset, n): pixels of cur with alpha < 255 blended over prev (pixel offsets) local function blendRow(cur: buffer, prev: buffer, o: number, n: number) for i = o, o + n - 1 do local s = readu32(cur, i * 4) if s < 0xff000000 then writeu32(cur, i * 4, blendPixel(s, readu32(prev, i * 4))) end end end -- draw(canvas, cw, f): copy the frame rectangle into the canvas local function draw(canvas: buffer, cw: number, f: Frame) for j = 0, f.h - 1 do bcopy(canvas, ((f.y + j) * cw + f.x) * 4, f.rgba, j * f.w * 4, f.w * 4) end end local function clearRect(canvas: buffer, cw: number, x: number, y: number, w: number, h: number) for j = 0, h - 1 do bfill(canvas, ((y + j) * cw + x) * 4, 0, w * 4) end end -- coalesce(cw, ch, frames) -> one full canvas (rgba8) per frame, as WebPAnimDecoder returns them function Anim.coalesce(cw: number, ch: number, frames: { Frame }): { buffer } local size = cw * ch * 4 local out: { buffer } = {} local disposed = bcreate(size) local prev: Frame? = nil local prevKey = false for n, f in frames do local full = f.w == cw and f.h == ch local key if n == 1 then key = true elseif (not f.hasAlpha or not f.blend) and full then key = true else local p = prev :: Frame key = p.disposeBg and ((p.w == cw and p.h == ch) or prevKey) end local cur = bcreate(size) if not key then bcopy(cur, 0, disposed, 0, size) end draw(cur, cw, f) if n > 1 and f.blend and not key then local p = prev :: Frame if not p.disposeBg then for j = 0, f.h - 1 do blendRow(cur, disposed, (f.y + j) * cw + f.x, f.w) end else -- the previous rectangle was cleared: blending with transparent there is a no-op for libwebp local pmx, pmy = p.x + p.w, p.y + p.h local smx = f.x + f.w for j = 0, f.h - 1 do local cy = f.y + j if cy < p.y or cy >= pmy or f.x >= pmx or smx <= p.x then blendRow(cur, disposed, cy * cw + f.x, f.w) else if f.x < p.x then blendRow(cur, disposed, cy * cw + f.x, p.x - f.x) end if smx > pmx then blendRow(cur, disposed, cy * cw + pmx, smx - pmx) end end end end end table.insert(out, cur) prev = f prevKey = key bcopy(disposed, 0, cur, 0, size) if f.disposeBg then clearRect(disposed, cw, f.x, f.y, f.w, f.h) end end return out end -- composite(cw, ch, frames) -> one full canvas per frame in the generic model: start transparent, draw (blend or -- overwrite; "over" a fully transparent pixel copies it), show, then dispose (background: clear the rectangle; -- previous: restore) function Anim.composite(cw: number, ch: number, frames: { Frame }): { buffer } local size = cw * ch * 4 local canvas = bcreate(size) local out: { buffer } = {} for _, f in frames do local saved: buffer? = nil if f.disposePrev then saved = bcreate(size) bcopy(saved :: buffer, 0, canvas, 0, size) end if f.blend then for j = 0, f.h - 1 do local co = (f.y + j) * cw + f.x for i = 0, f.w - 1 do local s = readu32(f.rgba, (j * f.w + i) * 4) local d = readu32(canvas, (co + i) * 4) writeu32(canvas, (co + i) * 4, if s >= 0xff000000 or d < 0x01000000 then s else blendPixel(s, d)) end end else draw(canvas, cw, f) end local shown = bcreate(size) bcopy(shown, 0, canvas, 0, size) table.insert(out, shown) if saved then canvas = saved elseif f.disposeBg then clearRect(canvas, cw, f.x, f.y, f.w, f.h) end end return out end return Anim