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

+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,