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:
1 parent
5977b60df6
commit
c56ca75ed2
3 files changed
+77
-24
No files matched your search
@@ -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
|
||||
Reference in new issue
Block a user