ws-webp: encoder tries several transform configurations at effort 6+ (palette vs predictor, tile sizes); argument validation in encode / coalesce; NOTICE row for the libwebp ports

This commit is contained in:
4DBug committed 2026-10-31 09:00:59 -05:00
1 parent 5977b60df6
commit c56ca75ed2
3 files changed
+77 -24

No files matched your search

+59 -19
View File
@@ -853,23 +853,14 @@ local function palette(px: buffer, n: number): { number }?
return list
end
-- encode(rgba, w, h, effort, exact, alphaHint?, headerless) -> the VP8L bitstream (headerless: the image stream only,
-- as an ALPH chunk stores it)
function Encode.encode(rgba: buffer, w: number, h: number, effort: number, exact: boolean, alphaHint: boolean?, headerless: boolean): buffer
type Config = { palette: { number }?, pbits: number, cc: boolean, ccbits: number }
-- stream(px, w, h, effort, cfg, bw): the transforms and the main image of one configuration (px is consumed)
local function stream(px: buffer, w: number, h: number, effort: number, cfg: Config, bw: W)
local n = w * h
local px, usesAlpha = toArgb(rgba, n, exact)
local bw = newWriter(n + 256)
if not headerless then
put(bw, 0x2f, 8)
put(bw, w - 1, 14)
put(bw, h - 1, 14)
local hint = if alphaHint == nil then usesAlpha else alphaHint
put(bw, if hint then 1 else 0, 1)
put(bw, 0, 3)
end
local xs = w
local sub = math.min(effort, 3)
local pal = if effort > 0 then palette(px, n) else nil
local pal = cfg.palette
if pal then
local num = #pal
local bits = if num > 16 then 0 elseif num > 4 then 1 elseif num > 2 then 2 else 3
@@ -902,7 +893,7 @@ function Encode.encode(rgba: buffer, w: number, h: number, effort: number, exact
end
end
px = packed
elseif effort > 0 then
elseif cfg.pbits > 0 then
-- subtract green
put(bw, 1, 1)
put(bw, 2, 2)
@@ -912,15 +903,15 @@ function Encode.encode(rgba: buffer, w: number, h: number, effort: number, exact
writeu32(px, i * 4, band(v, 0xff00ff00) + band(band(v, 0x00ff00ff) + 0x01000100 - g * 65537, 0x00ff00ff))
end
-- predictor
local pbits = if effort >= 5 then 4 else 5
local pbits = cfg.pbits
local modes = predictorTransform(px, w, h, pbits, effort)
put(bw, 1, 1)
put(bw, 0, 2)
put(bw, pbits - 2, 3)
writeImage(bw, modes, subSample(w, pbits), subSample(w, pbits) * subSample(h, pbits), sub, false)
-- cross colour
if effort >= 3 then
local cbits = if effort >= 6 then 4 else 5
if cfg.cc then
local cbits = cfg.ccbits
local cimg = crossColorTransform(px, w, h, cbits)
put(bw, 1, 1)
put(bw, 1, 2)
@@ -930,7 +921,56 @@ function Encode.encode(rgba: buffer, w: number, h: number, effort: number, exact
end
put(bw, 0, 1) -- no more transforms
writeImage(bw, px, xs, xs * h, effort, true)
return finish(bw)
end
-- encode(rgba, w, h, effort, exact, alphaHint?, headerless) -> the VP8L bitstream (headerless: the image stream only,
-- as an ALPH chunk stores it). Efforts 6+ try several transform configurations and keep the smallest.
function Encode.encode(rgba: buffer, w: number, h: number, effort: number, exact: boolean, alphaHint: boolean?, headerless: boolean): buffer
local n = w * h
local px, usesAlpha = toArgb(rgba, n, exact)
local pal = if effort > 0 then palette(px, n) else nil
local configs: { Config } = {}
if effort == 0 then
table.insert(configs, { palette = nil, pbits = 0, cc = false, ccbits = 0 })
else
local pbits = if effort >= 5 then 4 else 5
local plain = { palette = nil, pbits = pbits, cc = effort >= 3, ccbits = if effort >= 6 then 4 else 5 }
if pal then
table.insert(configs, { palette = pal, pbits = 0, cc = false, ccbits = 0 })
if effort >= 6 then
table.insert(configs, plain)
end
else
table.insert(configs, plain)
end
if effort >= 9 then
table.insert(configs, { palette = nil, pbits = 3, cc = true, ccbits = 3 })
table.insert(configs, { palette = nil, pbits = 5, cc = false, ccbits = 0 })
end
end
local best: buffer? = nil
for k, cfg in configs do
local bw = newWriter(n + 256)
if not headerless then
put(bw, 0x2f, 8)
put(bw, w - 1, 14)
put(bw, h - 1, 14)
local hint = if alphaHint == nil then usesAlpha else alphaHint
put(bw, if hint then 1 else 0, 1)
put(bw, 0, 3)
end
local work = px
if k < #configs then
work = bcreate(n * 4)
bcopy(work, 0, px, 0, n * 4)
end
stream(work, w, h, effort, cfg, bw)
local out = finish(bw)
if not best or blen(out) < blen(best) then
best = out
end
end
return best :: buffer
end
return Encode
+17 -5
View File
@@ -508,6 +508,15 @@ function Webp.encode(img: Types.Image, opts: EncodeOptions?): (buffer?, Err.Err?
return nil, Err.new(Err.ARG, "loops must be 0..65535")
end
local bgc = o.background or (img.meta and img.meta.background) or { 0, 0, 0, 0 }
if type(bgc) ~= "table" then
return nil, Err.new(Err.ARG, "background must be { r, g, b, a }")
end
for k = 1, 4 do
local v = bgc[k]
if v ~= nil and (type(v) ~= "number" or v < 0 or v > 255 or v % 1 ~= 0) then
return nil, Err.new(Err.ARG, "background must be { r, g, b, a } with 0..255 integers")
end
end
local anim = buffer.create(6)
-- the background colour is stored as B, G, R, A
buffer.writeu8(anim, 0, bgc[3] or 0)
@@ -521,8 +530,8 @@ function Webp.encode(img: Types.Image, opts: EncodeOptions?): (buffer?, Err.Err?
if not bits then
return nil, err
end
local delay = frames[i].delay or 0.1
local ms = math.clamp(math.floor(delay * 1000 + 0.5), 0, 0xffffff)
local delay = frames[i].delay
local ms = if type(delay) == "number" and delay == delay then math.clamp(math.floor(delay * 1000 + 0.5), 0, 0xffffff) else 100
local hdr = Bytes.writer(16 + buffer.len(bits) + 9)
hdr:u24le(f.x // 2)
hdr:u24le(f.y // 2)
@@ -544,13 +553,16 @@ end
-- ------------------------------------------------------------------------------------------------ extras
-- coalesce(img) -> the full composited canvases of a decoded animation's frames ({ buffer }, rgba8), the way
-- WebPAnimDecoder composites them (nil for a still image)
-- WebPAnimDecoder composites them (nil for a still image or frames that do not fit the canvas)
function Webp.coalesce(img: Types.Image): { buffer }?
if not img.frames then
if type(img) ~= "table" or type(img.frames) ~= "table" or type(img.width) ~= "number" or type(img.height) ~= "number" then
return nil
end
local list: { Anim.Frame } = {}
for _, f in img.frames do
for _, f in img.frames :: { Types.Frame } do
if type(f) ~= "table" or not validImage(f.image) or type(f.x) ~= "number" or type(f.y) ~= "number" or f.x < 0 or f.y < 0 or f.x % 1 ~= 0 or f.y % 1 ~= 0 or f.x + f.image.width > img.width or f.y + f.image.height > img.height then
return nil
end
table.insert(list, {
rgba = f.image.pixels,
x = f.x,