Files
rosetta/src/Formats/ops/ImageOps/Kernels.luau
T

521 lines
17 KiB
Luau

--!strict
--!native
--!optimize 2
-- ops/ImageOps/Kernels: the hot loops behind ops/ImageOps (native; never throws). Every function trusts its caller
-- (ops/ImageOps validates sizes, formats and buffer lengths first), so all buffer indexes are in range by
-- construction. Samples are addressed by kind: 1 = u8, 2 = u16, 4 = f32 (little endian, as Types.Image buffers).
-- The resampling follows Pillow's Resample.c (separable convolution, horizontal pass first, filter support scaled by
-- the downscale ratio, 22-bit fixed-point coefficients for 8-bit samples) and Geometry.c (affine transforms through
-- pixel centres, bilinear with clamped neighbours); the alpha compositing is Pillow's AlphaComposite.c.
local readu8, writeu8, readu16, writeu16, readf32, writef32 = buffer.readu8, buffer.writeu8, buffer.readu16, buffer.writeu16, buffer.readf32, buffer.writef32
local floor, clamp = math.floor, math.clamp
local K = {}
-- format ids: rgba8 1, rgba16 2, rgbaf32 3, rgb8 4, gray8 5, gray16 6, graya8 7, alpha8 8
K.ID = { rgba8 = 1, rgba16 = 2, rgbaf32 = 3, rgb8 = 4, gray8 = 5, gray16 = 6, graya8 = 7, alpha8 = 8 } :: { [string]: number }
K.KIND = { rgba8 = 1, rgba16 = 2, rgbaf32 = 4, rgb8 = 1, gray8 = 1, gray16 = 2, graya8 = 1, alpha8 = 1 } :: { [string]: number }
K.CHANNELS = { rgba8 = 4, rgba16 = 4, rgbaf32 = 4, rgb8 = 3, gray8 = 1, gray16 = 1, graya8 = 2, alpha8 = 1 } :: { [string]: number }
local function rd(b: buffer, o: number, kind: number): number
if kind == 1 then
return readu8(b, o)
elseif kind == 2 then
return readu16(b, o)
end
return readf32(b, o)
end
local function wr(b: buffer, o: number, kind: number, v: number)
if kind == 1 then
writeu8(b, o, clamp(floor(v + 0.5), 0, 255))
elseif kind == 2 then
writeu16(b, o, clamp(floor(v + 0.5), 0, 65535))
else
writef32(b, o, v)
end
end
-- ------------------------------------------------------------------------------------------------ convert
-- depth: 8 -> 0..255, 16 -> 0..65535, 0 -> float 0..1
local DEPTH = { 8, 16, 0, 8, 8, 16, 8, 8 }
local function depthConv(v: number, from: number, to: number): number
if from == to then
return v
end
if from == 8 then
if to == 16 then
return v * 257
end
return v / 255
elseif from == 16 then
if to == 8 then
return (v * 255 + 32767) // 65535
end
return v / 65535
end
-- float
if v ~= v then
v = 0
end
if to == 8 then
return clamp(floor(v * 255 + 0.5), 0, 255)
end
return clamp(floor(v * 65535 + 0.5), 0, 65535)
end
-- Pillow's L24 luma (19595 R + 38470 G + 7471 B + 0x8000) >> 16, in the source depth
local function luma(r: number, g: number, b: number, depth: number): number
if depth == 0 then
return (r * 19595 + g * 38470 + b * 7471) / 65536
end
return (r * 19595 + g * 38470 + b * 7471 + 32768) // 65536
end
function K.convert(src: buffer, sf: number, dst: buffer, df: number, n: number)
local sd, dd = DEPTH[sf] :: number, DEPTH[df] :: number
local smax = if sd == 8 then 255 elseif sd == 16 then 65535 else 1
local toGray = df == 5 or df == 6 or df == 7
for i = 0, n - 1 do
local r: number, g: number, b: number, a: number
if sf == 1 then
local o = i * 4
r, g, b, a = readu8(src, o), readu8(src, o + 1), readu8(src, o + 2), readu8(src, o + 3) :: number
elseif sf == 2 then
local o = i * 8
r, g, b, a = readu16(src, o), readu16(src, o + 2), readu16(src, o + 4), readu16(src, o + 6) :: number
elseif sf == 3 then
local o = i * 16
r, g, b, a = readf32(src, o), readf32(src, o + 4), readf32(src, o + 8), readf32(src, o + 12) :: number
elseif sf == 4 then
local o = i * 3
r, g, b, a = readu8(src, o), readu8(src, o + 1), readu8(src, o + 2), 255
elseif sf == 5 then
local v = readu8(src, i)
r, g, b, a = v, v, v, 255
elseif sf == 6 then
local v = readu16(src, i * 2)
r, g, b, a = v, v, v, 65535
elseif sf == 7 then
local v = readu8(src, i * 2)
r, g, b, a = v, v, v, readu8(src, i * 2 + 1) :: number
else
r, g, b, a = 255, 255, 255, readu8(src, i)
end
if toGray then
local isGray = sf == 5 or sf == 6 or sf == 7
local l = if isGray then r else luma(r, g, b, sd)
l = depthConv(l, sd, dd)
if df == 5 then
writeu8(dst, i, l)
elseif df == 6 then
writeu16(dst, i * 2, l)
else
writeu8(dst, i * 2, l)
writeu8(dst, i * 2 + 1, depthConv(a, sd, dd))
end
elseif df == 8 then
writeu8(dst, i, depthConv(a, sd, 8))
else
r, g, b = depthConv(r, sd, dd), depthConv(g, sd, dd), depthConv(b, sd, dd) :: number
if df == 1 then
local o = i * 4
writeu8(dst, o, r)
writeu8(dst, o + 1, g)
writeu8(dst, o + 2, b)
writeu8(dst, o + 3, depthConv(a, sd, dd))
elseif df == 2 then
local o = i * 8
writeu16(dst, o, r)
writeu16(dst, o + 2, g)
writeu16(dst, o + 4, b)
writeu16(dst, o + 6, depthConv(a, sd, dd))
elseif df == 3 then
local o = i * 16
writef32(dst, o, r)
writef32(dst, o + 4, g)
writef32(dst, o + 8, b)
writef32(dst, o + 12, if a == smax and sd ~= 0 then 1 else depthConv(a, sd, dd))
else -- rgb8
local o = i * 3
writeu8(dst, o, r)
writeu8(dst, o + 1, g)
writeu8(dst, o + 2, b)
end
end
end
end
-- JPEG-style BT.601 full range
function K.rgbaToYuv(src: buffer, w: number, h: number, dst: buffer)
local cw, ch = (w + 1) // 2, (h + 1) // 2
local uo, vo = w * h, w * h + cw * ch
for i = 0, w * h - 1 do
local o = i * 4
local r, g, b = readu8(src, o), readu8(src, o + 1), readu8(src, o + 2)
writeu8(dst, i, clamp(floor(0.299 * r + 0.587 * g + 0.114 * b + 0.5), 0, 255))
end
for cy = 0, ch - 1 do
for cx = 0, cw - 1 do
local sr, sg, sb, n = 0, 0, 0, 0
for dy = 0, 1 do
local y = cy * 2 + dy
if y < h then
for dx = 0, 1 do
local x = cx * 2 + dx
if x < w then
local o = (y * w + x) * 4
sr += readu8(src, o)
sg += readu8(src, o + 1)
sb += readu8(src, o + 2)
n += 1
end
end
end
end
sr, sg, sb = sr / n, sg / n, sb / n
local i = cy * cw + cx
writeu8(dst, uo + i, clamp(floor(128 - 0.168736 * sr - 0.331264 * sg + 0.5 * sb + 0.5), 0, 255))
writeu8(dst, vo + i, clamp(floor(128 + 0.5 * sr - 0.418688 * sg - 0.081312 * sb + 0.5), 0, 255))
end
end
end
function K.yuvToRgba(src: buffer, w: number, h: number, dst: buffer)
local cw, ch = (w + 1) // 2, (h + 1) // 2
local uo, vo = w * h, w * h + cw * ch
for y = 0, h - 1 do
local crow = (y // 2) * cw
for x = 0, w - 1 do
local yy = readu8(src, y * w + x)
local ci = crow + x // 2
local u, v = readu8(src, uo + ci) - 128, readu8(src, vo + ci) - 128
local o = (y * w + x) * 4
writeu8(dst, o, clamp(floor(yy + 1.402 * v + 0.5), 0, 255))
writeu8(dst, o + 1, clamp(floor(yy - 0.344136 * u - 0.714136 * v + 0.5), 0, 255))
writeu8(dst, o + 2, clamp(floor(yy + 1.772 * u + 0.5), 0, 255))
writeu8(dst, o + 3, 255)
end
end
local _ = ch
end
-- ------------------------------------------------------------------------------------------------ geometry
-- flip(src, dst, w, h, bpp, horizontal)
function K.flip(src: buffer, dst: buffer, w: number, h: number, bpp: number, horizontal: boolean)
local stride = w * bpp
if not horizontal then
for y = 0, h - 1 do
buffer.copy(dst, (h - 1 - y) * stride, src, y * stride, stride)
end
return
end
for y = 0, h - 1 do
local row = y * stride
for x = 0, w - 1 do
buffer.copy(dst, row + (w - 1 - x) * bpp, src, row + x * bpp, bpp)
end
end
end
-- rotate90(src, dst, w, h, bpp, turns): counter-clockwise quarter turns 1..3
function K.rotate90(src: buffer, dst: buffer, w: number, h: number, bpp: number, turns: number)
for y = 0, h - 1 do
for x = 0, w - 1 do
local ox, oy, ow
if turns == 1 then
ox, oy, ow = y, w - 1 - x, h
elseif turns == 2 then
ox, oy, ow = w - 1 - x, h - 1 - y, w
else
ox, oy, ow = h - 1 - y, x, h
end
local so, d = (y * w + x) * bpp, (oy * ow + ox) * bpp
if bpp == 4 then
buffer.writeu32(dst, d, buffer.readu32(src, so))
else
buffer.copy(dst, d, src, so, bpp)
end
end
end
end
-- ------------------------------------------------------------------------------------------------ resample
local function bilinearF(x: number): number
if x < 0 then
x = -x
end
if x < 1 then
return 1 - x
end
return 0
end
local function sinc(x: number): number
if x == 0 then
return 1
end
x *= math.pi
return math.sin(x) / x
end
local function lanczosF(x: number): number
if x > -3 and x < 3 then
return sinc(x) * sinc(x / 3)
end
return 0
end
local function trunc(x: number): number
return if x < 0 then math.ceil(x) else floor(x)
end
-- Pillow precompute_coeffs: per output index its first input index, tap count and weights (normalised)
export type Coeffs = { start: { number }, count: { number }, weights: { { number } } }
function K.coeffs(inSize: number, outSize: number, filter: string): Coeffs
local support0 = if filter == "lanczos" then 3 else 1
local f = if filter == "lanczos" then lanczosF else bilinearF
local scale = inSize / outSize
local filterscale = math.max(scale, 1)
local support = support0 * filterscale
local ss = 1 / filterscale
local start, count, weights = table.create(outSize, 0), table.create(outSize, 0), table.create(outSize) :: { { number } }
for xx = 0, outSize - 1 do
local center = (xx + 0.5) * scale
local xmin = math.max(0, trunc(center - support + 0.5))
local xmax = math.min(inSize, trunc(center + support + 0.5)) - xmin
local w = table.create(math.max(xmax, 0), 0)
local ww = 0
for x = 0, xmax - 1 do
local v = f((x + xmin - center + 0.5) * ss)
w[x + 1] = v
ww += v
end
if ww ~= 0 then
for x = 1, xmax do
w[x] /= ww
end
end
start[xx + 1], count[xx + 1], weights[xx + 1] = xmin, math.max(xmax, 0), w
end
return { start = start, count = count, weights = weights }
end
-- one separable pass. horizontal: src w x h -> dst outN x h; vertical: src w x h -> dst w x outN. ch channels of kind.
function K.resamplePass(src: buffer, w: number, h: number, ch: number, kind: number, dst: buffer, outN: number, c: Coeffs, horizontal: boolean)
local start, count, weights = c.start, c.count, c.weights
local size = kind
local fixed = kind == 1
-- 8-bit: Pillow's 22-bit fixed-point coefficients
local iw: { { number } } = weights
if fixed then
iw = table.create(outN) :: { { number } }
for i = 1, outN do
local src0 = weights[i]
local t = table.create(#src0, 0)
for k = 1, #src0 do
local v = src0[k] * 4194304
t[k] = if v < 0 then math.ceil(v - 0.5) else floor(v + 0.5)
end
iw[i] = t
end
end
if horizontal then
for y = 0, h - 1 do
local row = y * w
for xx = 0, outN - 1 do
local s0, n, wt = start[xx + 1], count[xx + 1], iw[xx + 1]
local o = ((y * outN) + xx) * ch * size
for cc = 0, ch - 1 do
if fixed then
local acc = 2097152
for k = 0, n - 1 do
acc += readu8(src, (row + s0 + k) * ch + cc) * wt[k + 1]
end
writeu8(dst, o + cc, clamp(acc // 4194304, 0, 255))
else
local acc = 0
for k = 0, n - 1 do
acc += rd(src, ((row + s0 + k) * ch + cc) * size, kind) * wt[k + 1]
end
wr(dst, o + cc * size, kind, acc)
end
end
end
end
else
for yy = 0, outN - 1 do
local s0, n, wt = start[yy + 1], count[yy + 1], iw[yy + 1]
for x = 0, w - 1 do
local o = (yy * w + x) * ch * size
for cc = 0, ch - 1 do
if fixed then
local acc = 2097152
for k = 0, n - 1 do
acc += readu8(src, ((s0 + k) * w + x) * ch + cc) * wt[k + 1]
end
writeu8(dst, o + cc, clamp(acc // 4194304, 0, 255))
else
local acc = 0
for k = 0, n - 1 do
acc += rd(src, (((s0 + k) * w + x) * ch + cc) * size, kind) * wt[k + 1]
end
wr(dst, o + cc * size, kind, acc)
end
end
end
end
end
local _ = h
end
-- nearest: dst pixel (x, y) = src pixel (floor((x + 0.5) * w / ow), ...)
function K.nearest(src: buffer, w: number, h: number, bpp: number, dst: buffer, ow: number, oh: number)
local sx, sy = w / ow, h / oh
for y = 0, oh - 1 do
local iy = math.min(floor((y + 0.5) * sy), h - 1)
for x = 0, ow - 1 do
local ix = math.min(floor((x + 0.5) * sx), w - 1)
buffer.copy(dst, (y * ow + x) * bpp, src, (iy * w + ix) * bpp, bpp)
end
end
end
-- affine(src, w, h, ch, kind, dst, ow, oh, m): dst pixel centres mapped through m = { a, b, c, d, e, f } to src
-- coordinates (xin = a x + b y + c), bilinear with clamped neighbours; centres outside the source -> zeros (Pillow)
function K.affine(src: buffer, w: number, h: number, ch: number, kind: number, dst: buffer, ow: number, oh: number, m: { number })
local a, b, c, d, e, f = m[1], m[2], m[3], m[4], m[5], m[6]
local size = kind
for y = 0, oh - 1 do
local py = y + 0.5
for x = 0, ow - 1 do
local px = x + 0.5
local xin = a * px + b * py + c - 0.5
local yin = d * px + e * py + f - 0.5
local o = (y * ow + x) * ch * size
local x0 = floor(xin)
local y0 = floor(yin)
if xin < -0.5 or xin >= w - 0.5 or yin < -0.5 or yin >= h - 0.5 then
continue -- dst starts zeroed
end
local dx, dy = xin - x0, yin - y0
local xa, xb = clamp(x0, 0, w - 1), clamp(x0 + 1, 0, w - 1)
local ya, yb = clamp(y0, 0, h - 1), clamp(y0 + 1, 0, h - 1)
for cc = 0, ch - 1 do
local p00 = rd(src, ((ya * w + xa) * ch + cc) * size, kind)
local p01 = rd(src, ((ya * w + xb) * ch + cc) * size, kind)
local p10 = rd(src, ((yb * w + xa) * ch + cc) * size, kind)
local p11 = rd(src, ((yb * w + xb) * ch + cc) * size, kind)
local top = p00 + (p01 - p00) * dx
local bot = p10 + (p11 - p10) * dx
local v = top + (bot - top) * dy
if kind == 1 then
writeu8(dst, o + cc, clamp(floor(v), 0, 255)) -- Pillow truncates
else
wr(dst, o + cc * size, kind, v)
end
end
end
end
end
-- ------------------------------------------------------------------------------------------------ compositing
-- overRgba8(dst, dw, src, sw, sx, sy, dx, dy, w, h, opacity255): Pillow's alpha_composite of src onto dst, in place
function K.overRgba8(dst: buffer, dw: number, src: buffer, sw: number, sx: number, sy: number, dx: number, dy: number, w: number, h: number, opacity: number)
for y = 0, h - 1 do
for x = 0, w - 1 do
local so = ((sy + y) * sw + sx + x) * 4
local doff = ((dy + y) * dw + dx + x) * 4
local sa = readu8(src, so + 3)
if opacity < 255 then
sa = (sa * opacity + 127) // 255
end
if sa == 0 then
continue
end
local da = readu8(dst, doff + 3)
local blend = da * (255 - sa)
local outa255 = sa * 255 + blend
local coef1 = (sa * 255 * 255 * 128) // outa255
local coef2 = 255 * 128 - coef1
for cc = 0, 2 do
local t = readu8(src, so + cc) * coef1 + readu8(dst, doff + cc) * coef2 + 0x80 * 128
t = ((t // 256) + t) // 256
writeu8(dst, doff + cc, t // 128)
end
local t = outa255 + 0x80
writeu8(dst, doff + 3, ((t // 256) + t) // 256)
end
end
end
-- overFloat: straight-alpha "over" for rgba16 (kind 2) / rgbaf32 (kind 4); max = 65535 or 1
function K.overFloat(dst: buffer, dw: number, src: buffer, sw: number, sx: number, sy: number, dx: number, dy: number, w: number, h: number, opacity: number, kind: number)
local max = if kind == 2 then 65535 else 1
local px = 4 * kind
for y = 0, h - 1 do
for x = 0, w - 1 do
local so = ((sy + y) * sw + sx + x) * px
local doff = ((dy + y) * dw + dx + x) * px
local sa = rd(src, so + 3 * kind, kind) / max * opacity
if sa <= 0 then
continue
end
local da = rd(dst, doff + 3 * kind, kind) / max
local oa = sa + da * (1 - sa)
for cc = 0, 2 do
local s = rd(src, so + cc * kind, kind)
local d = rd(dst, doff + cc * kind, kind)
wr(dst, doff + cc * kind, kind, if oa > 0 then (s * sa + d * da * (1 - sa)) / oa else 0)
end
wr(dst, doff + 3 * kind, kind, oa * max)
end
end
end
-- copyRect: rows of a rectangle (bpp bytes per pixel)
function K.copyRect(dst: buffer, dw: number, src: buffer, sw: number, sx: number, sy: number, dx: number, dy: number, w: number, h: number, bpp: number)
for y = 0, h - 1 do
buffer.copy(dst, ((dy + y) * dw + dx) * bpp, src, ((sy + y) * sw + sx) * bpp, w * bpp)
end
end
-- premultiply / unpremultiply in place: ch channels, alpha last, kind
function K.premultiply(b: buffer, n: number, ch: number, kind: number, un: boolean)
local max = if kind == 1 then 255 elseif kind == 2 then 65535 else 1
local px = ch * kind
for i = 0, n - 1 do
local o = i * px
local a = rd(b, o + (ch - 1) * kind, kind)
for cc = 0, ch - 2 do
local v = rd(b, o + cc * kind, kind)
local r
if un then
if a <= 0 then
r = 0
elseif kind == 4 then
r = v / a
elseif kind == 1 then
r = math.min(255, (v * 255) // a) -- Pillow's rgbA2rgba
else
r = math.min(max, (v * max + a // 2) // a)
end
else
if kind == 4 then
r = v * a
elseif kind == 1 then
local t = v * a + 128 -- Pillow's MULDIV255
r = ((t // 256) + t) // 256
else
r = (v * a + max // 2) // max
end
end
wr(b, o + cc * kind, kind, r)
end
end
end
return K