ws-webp: merge ws/absorb (containers/Riff); image/webp reads and writes its chunks through containers/Riff (private chunk reader removed); bench lines merged; registry regenerated
This commit is contained in:
commit
d19c949e95
216 files changed
+24295
-165
No files matched your search
@@ -1,7 +1,7 @@
|
||||
--!strict
|
||||
-- image/webp: WebP (RFC 9649; Google's "WebP Container Specification"). New code: the bitstreams are ports of libwebp 1.6
|
||||
-- (codecs/Vp8, codecs/Vp8L; BSD-3-Clause, licenses/libwebp-LICENSE.txt), the container and the compositing follow
|
||||
-- libwebp's demux / WebPAnimDecoder.
|
||||
-- (codecs/Vp8, codecs/Vp8L; BSD-3-Clause, licenses/libwebp-LICENSE.txt), the chunks go through containers/Riff, the
|
||||
-- container rules and the compositing follow libwebp's demux / WebPAnimDecoder.
|
||||
-- Supports: decode of lossy (VP8 key frames: bit-exact with dwebp, fancy or point chroma upsampling, or the raw
|
||||
-- yuv420p planes with opts.native), lossless (VP8L, bit-exact), alpha (ALPH raw or lossless, every filter), the
|
||||
-- extended format (VP8X), animation (ANIM / ANMF: frames as their own rectangles plus the composited first frame, or
|
||||
@@ -20,7 +20,7 @@ local Bytes = require("@formats/core/Bytes")
|
||||
local Types = require("@formats/Types")
|
||||
local Vp8 = require("@formats/codecs/Vp8")
|
||||
local Vp8L = require("@formats/codecs/Vp8L")
|
||||
local Riff = require("@formats/image/webp/Riff")
|
||||
local Riff = require("@formats/containers/Riff")
|
||||
local Alpha = require("@formats/image/webp/Alpha")
|
||||
local Anim = require("@formats/image/webp/Anim")
|
||||
|
||||
@@ -30,7 +30,7 @@ local Webp = {
|
||||
category = "image",
|
||||
extensions = { "webp" },
|
||||
mime = { "image/webp" },
|
||||
deps = { "core/Err", "core/Bytes", "Types", "codecs/Vp8", "codecs/Vp8L" },
|
||||
deps = { "core/Err", "core/Bytes", "Types", "codecs/Vp8", "codecs/Vp8L", "containers/Riff" },
|
||||
caps = { decode = true, encode = true, animation = true, alpha = true, lossy = true, lossless = true, depths = { 8 } },
|
||||
}
|
||||
|
||||
@@ -90,11 +90,58 @@ function Webp.sniff(r: Bytes.Reader): number
|
||||
end
|
||||
|
||||
-- ------------------------------------------------------------------------------------------------ decode
|
||||
type Chunk = { id: string, off: number, size: number } -- off: absolute offset of the payload
|
||||
|
||||
local function convert(list: { Riff.Chunk }): ({ Chunk }?, Err.Err?)
|
||||
local out: { Chunk } = {}
|
||||
for _, c in list do
|
||||
if c.truncated then
|
||||
return nil, Err.new(Err.TRUNCATED, "chunk '" .. c.id .. "' runs past the end", c.offset)
|
||||
end
|
||||
table.insert(out, { id = c.id, off = c.offset, size = c.size })
|
||||
end
|
||||
return out, nil
|
||||
end
|
||||
|
||||
-- readChunks(r) -> the chunks of a RIFF "WEBP" file, the containers/Riff File | nil, Err. As libwebp: the RIFF size
|
||||
-- must fit the data (TRUNCATED) and bytes after it are ignored.
|
||||
local function readChunks(r: Bytes.Reader): ({ Chunk }?, any)
|
||||
local buf, base, limit = r.buf, r.base, r.limit
|
||||
if limit - base < 12 then
|
||||
return nil, Err.new(Err.TRUNCATED, "RIFF header", base)
|
||||
end
|
||||
if buffer.readstring(buf, base, 4) ~= "RIFF" or buffer.readstring(buf, base + 8, 4) ~= "WEBP" then
|
||||
return nil, Err.new(Err.CORRUPT, "not a RIFF WEBP file", base)
|
||||
end
|
||||
local size = buffer.readu32(buf, base + 4)
|
||||
if size < 12 then
|
||||
return nil, Err.new(Err.CORRUPT, "RIFF size too small", base + 4)
|
||||
end
|
||||
if 8 + size > limit - base then
|
||||
return nil, Err.new(Err.TRUNCATED, "the RIFF size runs past the end of the data", base + 4)
|
||||
end
|
||||
local f, err = Riff.read(Bytes.reader(buf, base, 8 + size))
|
||||
if not f then
|
||||
return nil, err
|
||||
end
|
||||
local list, cerr = convert(f.chunks)
|
||||
if not list then
|
||||
return nil, cerr
|
||||
end
|
||||
return list, f
|
||||
end
|
||||
|
||||
-- subChunks(f, c) -> the chunks inside an ANMF payload (after its 16-byte header)
|
||||
local function subChunks(f: Riff.File, c: Chunk): ({ Chunk }?, Err.Err?)
|
||||
local n = c.size - 12
|
||||
return convert(Riff.list(f, { id = c.id, offset = c.off + 12, size = n, declared = n, avail = n, truncated = false, streamed = false }))
|
||||
end
|
||||
|
||||
type Bitstream = { rgba: buffer?, yuv: Types.Image?, w: number, h: number, hasAlpha: boolean, source: { [string]: any } }
|
||||
|
||||
-- the image chunks of a still image or an ANMF frame: the first ALPH (if any) and the first VP8 / VP8L chunk
|
||||
local function findImage(chunks: { Riff.Chunk }, from: number): (Riff.Chunk?, Riff.Chunk?)
|
||||
local alph: Riff.Chunk? = nil
|
||||
local function findImage(chunks: { Chunk }, from: number): (Chunk?, Chunk?)
|
||||
local alph: Chunk? = nil
|
||||
for i = from, #chunks do
|
||||
local c = chunks[i]
|
||||
if c.id == "ALPH" and not alph then
|
||||
@@ -108,7 +155,7 @@ local function findImage(chunks: { Riff.Chunk }, from: number): (Riff.Chunk?, Ri
|
||||
return nil, nil
|
||||
end
|
||||
|
||||
local function decodeBitstream(buf: buffer, c: Riff.Chunk, alph: Riff.Chunk?, opts: DecodeOptions, allowNative: boolean): (Bitstream?, Err.Err?)
|
||||
local function decodeBitstream(buf: buffer, c: Chunk, alph: Chunk?, opts: DecodeOptions, allowNative: boolean): (Bitstream?, Err.Err?)
|
||||
local maxPixels = opts.maxPixels or DEFAULT_MAX_PIXELS
|
||||
if c.id == "VP8L" then
|
||||
local rgba, w, h, alphaHint = Vp8L.decode(buf, c.off, c.size, maxPixels)
|
||||
@@ -143,7 +190,7 @@ local function decodeBitstream(buf: buffer, c: Riff.Chunk, alph: Riff.Chunk?, op
|
||||
return { rgba = rgba, w = fw, h = fh, hasAlpha = alph ~= nil, source = source }, nil
|
||||
end
|
||||
|
||||
local function collectMeta(buf: buffer, chunks: { Riff.Chunk }, meta: Types.ImageMeta)
|
||||
local function collectMeta(buf: buffer, chunks: { Chunk }, meta: Types.ImageMeta)
|
||||
for _, c in chunks do
|
||||
local key = if c.id == "ICCP" then "icc" elseif c.id == "EXIF" then "exif" elseif c.id == "XMP " then "xmp" else nil
|
||||
if key and meta[key] == nil then
|
||||
@@ -158,9 +205,9 @@ function Webp.decode(r: Bytes.Reader, opts: DecodeOptions?): (Types.Image?, Err.
|
||||
local o: DecodeOptions = opts or {}
|
||||
local maxPixels = o.maxPixels or DEFAULT_MAX_PIXELS
|
||||
local buf = r.buf
|
||||
local chunks, cerr = Riff.chunks(buf, r.base, r.limit)
|
||||
local chunks, file = readChunks(r)
|
||||
if not chunks then
|
||||
return nil, fromMessage(cerr, r.base)
|
||||
return nil, file
|
||||
end
|
||||
local first = chunks[1]
|
||||
if not first then
|
||||
@@ -255,9 +302,9 @@ function Webp.decode(r: Bytes.Reader, opts: DecodeOptions?): (Types.Image?, Err.
|
||||
if fx + fw > cw or fy + fh > ch then
|
||||
return corrupt("frame " .. (#frames + 1) .. " lies outside the canvas", c.off)
|
||||
end
|
||||
local sub, serr = Riff.list(buf, c.off + 16, c.off + c.size)
|
||||
local sub, serr = subChunks(file, c)
|
||||
if not sub then
|
||||
return nil, fromMessage(serr, c.off + 16)
|
||||
return nil, serr
|
||||
end
|
||||
local img, alph = findImage(sub, 1)
|
||||
if not img then
|
||||
@@ -329,9 +376,9 @@ end
|
||||
|
||||
-- info(r) -> { width, height, animated, alpha, lossless, frames? } from the headers only | nil, Err
|
||||
function Webp.info(r: Bytes.Reader): (any, Err.Err?)
|
||||
local chunks, cerr = Riff.chunks(r.buf, r.base, r.limit)
|
||||
local chunks, cerr = readChunks(r)
|
||||
if not chunks then
|
||||
return nil, fromMessage(cerr, r.base)
|
||||
return nil, cerr
|
||||
end
|
||||
local first = chunks[1]
|
||||
if not first then
|
||||
@@ -383,22 +430,13 @@ local function validImage(img: any): boolean
|
||||
return type(img) == "table" and img.format == "rgba8" and type(img.pixels) == "buffer" and type(img.width) == "number" and type(img.height) == "number" and img.width >= 1 and img.height >= 1 and img.width <= 16384 and img.height <= 16384 and img.width % 1 == 0 and img.height % 1 == 0 and buffer.len(img.pixels) >= img.width * img.height * 4
|
||||
end
|
||||
|
||||
local function finishRiff(body: Bytes.Writer): buffer
|
||||
local out = Bytes.writer(body:tell() + 12)
|
||||
out:string("RIFF")
|
||||
out:u32le(body:tell() + 4)
|
||||
out:string("WEBP")
|
||||
out:bytes(body.buf, 0, body:tell())
|
||||
return out:finish()
|
||||
end
|
||||
|
||||
local function writeVp8x(body: Bytes.Writer, flags: number, w: number, h: number)
|
||||
body:string("VP8X")
|
||||
body:u32le(10)
|
||||
body:u8(flags)
|
||||
body:u24le(0)
|
||||
body:u24le(w - 1)
|
||||
body:u24le(h - 1)
|
||||
local function vp8x(flags: number, w: number, h: number): buffer
|
||||
local b = Bytes.writer(10)
|
||||
b:u8(flags)
|
||||
b:u24le(0)
|
||||
b:u24le(w - 1)
|
||||
b:u24le(h - 1)
|
||||
return b:finish()
|
||||
end
|
||||
|
||||
local function metaChunk(meta: any, key: string): buffer?
|
||||
@@ -427,7 +465,7 @@ function Webp.encode(img: Types.Image, opts: EncodeOptions?): (buffer?, Err.Err?
|
||||
local w, h = img.width, img.height
|
||||
local icc, exif, xmp = metaChunk(img.meta, "icc"), metaChunk(img.meta, "exif"), metaChunk(img.meta, "xmp")
|
||||
local frames = img.frames
|
||||
local body = Bytes.writer(w * h + 1024)
|
||||
local body = Riff.writer("WEBP", { rf64 = false })
|
||||
|
||||
if not frames or #frames == 0 then
|
||||
local bits, err = Vp8L.encode(img.pixels, w, h, vopts)
|
||||
@@ -439,21 +477,21 @@ function Webp.encode(img: Types.Image, opts: EncodeOptions?): (buffer?, Err.Err?
|
||||
if hasAlpha(img.pixels, w * h) then
|
||||
flags += FLAG_ALPHA
|
||||
end
|
||||
writeVp8x(body, flags, w, h)
|
||||
body:chunk("VP8X", vp8x(flags, w, h))
|
||||
if icc then
|
||||
Riff.write(body, "ICCP", icc)
|
||||
body:chunk("ICCP", icc)
|
||||
end
|
||||
Riff.write(body, "VP8L", bits)
|
||||
body:chunk("VP8L", bits)
|
||||
if exif then
|
||||
Riff.write(body, "EXIF", exif)
|
||||
body:chunk("EXIF", exif)
|
||||
end
|
||||
if xmp then
|
||||
Riff.write(body, "XMP ", xmp)
|
||||
body:chunk("XMP ", xmp)
|
||||
end
|
||||
else
|
||||
Riff.write(body, "VP8L", bits)
|
||||
body:chunk("VP8L", bits)
|
||||
end
|
||||
return finishRiff(body), nil
|
||||
return body:finish()
|
||||
end
|
||||
|
||||
-- animation: frames as they are when WebP can store them (even offsets, dispose none / background), else every
|
||||
@@ -502,9 +540,9 @@ function Webp.encode(img: Types.Image, opts: EncodeOptions?): (buffer?, Err.Err?
|
||||
end
|
||||
end
|
||||
local flags = FLAG_ANIM + (if anyAlpha then FLAG_ALPHA else 0) + (if icc then FLAG_ICC else 0) + (if exif then FLAG_EXIF else 0) + (if xmp then FLAG_XMP else 0)
|
||||
writeVp8x(body, flags, w, h)
|
||||
body:chunk("VP8X", vp8x(flags, w, h))
|
||||
if icc then
|
||||
Riff.write(body, "ICCP", icc)
|
||||
body:chunk("ICCP", icc)
|
||||
end
|
||||
local loops = o.loops or img.loops or 0
|
||||
if type(loops) ~= "number" or loops < 0 or loops > 65535 then
|
||||
@@ -527,7 +565,7 @@ function Webp.encode(img: Types.Image, opts: EncodeOptions?): (buffer?, Err.Err?
|
||||
buffer.writeu8(anim, 2, bgc[1] or 0)
|
||||
buffer.writeu8(anim, 3, bgc[4] or 0)
|
||||
buffer.writeu16(anim, 4, loops)
|
||||
Riff.write(body, "ANIM", anim)
|
||||
body:chunk("ANIM", anim)
|
||||
for i, f in list do
|
||||
local bits, err = Vp8L.encode(f.rgba, f.w, f.h, vopts)
|
||||
if not bits then
|
||||
@@ -542,16 +580,22 @@ function Webp.encode(img: Types.Image, opts: EncodeOptions?): (buffer?, Err.Err?
|
||||
hdr:u24le(f.h - 1)
|
||||
hdr:u24le(ms)
|
||||
hdr:u8((if f.blend then 0 else 2) + (if f.disposeBg then 1 else 0))
|
||||
Riff.write(hdr, "VP8L", bits)
|
||||
Riff.write(body, "ANMF", hdr.buf, 0, hdr:tell())
|
||||
local nb = buffer.len(bits)
|
||||
hdr:string("VP8L")
|
||||
hdr:u32le(nb)
|
||||
hdr:bytes(bits)
|
||||
if nb % 2 == 1 then
|
||||
hdr:u8(0)
|
||||
end
|
||||
body:chunk("ANMF", hdr.buf, 0, hdr:tell())
|
||||
end
|
||||
if exif then
|
||||
Riff.write(body, "EXIF", exif)
|
||||
body:chunk("EXIF", exif)
|
||||
end
|
||||
if xmp then
|
||||
Riff.write(body, "XMP ", xmp)
|
||||
body:chunk("XMP ", xmp)
|
||||
end
|
||||
return finishRiff(body), nil
|
||||
return body:finish()
|
||||
end
|
||||
|
||||
-- ------------------------------------------------------------------------------------------------ extras
|
||||
|
||||
Reference in new issue
Block a user