Files
rosetta/tools/fixtures/_webp_encode.luau
T

96 lines
3.6 KiB
Luau

-- tools/fixtures/_webp_encode: run by tools/fixtures/webp.py (luau CLI, repo root). Encodes the WebP fixture sources
-- (tests/gen/webp_src) with image/webp at several efforts and two animations, and prints one line per file:
-- "<name> <hex>" (stills: <source>_e<effort>_<exact|loose>) or "anim<k>:<crc>,<crc>,... <hex>" with the CRC-32 of each
-- canvas the input frames composite to (image/webp/Anim.composite), for dwebp / Pillow to check.
local Webp = require("@formats/image/webp")
local Anim = require("@formats/image/webp/Anim")
local Types = require("@formats/Types")
local Crc32 = require("@formats/core/Crc32")
local Src = require("@tests/gen/webp_src")
local HEX = {}
for i = 0, 255 do
HEX[i] = string.format("%02x", i)
end
local function hex(b: buffer): string
local t = table.create(buffer.len(b))
for i = 0, buffer.len(b) - 1 do
t[i + 1] = HEX[buffer.readu8(b, i)]
end
return table.concat(t)
end
local function image(s: any): Types.Image
local img = Types.Image.new(s.width, s.height)
img.pixels = buffer.fromstring(s.rgba)
return img
end
local bySrc = {}
for _, s in Src do
bySrc[s.name] = s
for _, effort in { 0, 3, 5, 9 } do
local out = assert(Webp.encode(image(s), { effort = effort, exact = true }))
print(string.format("%s_e%d_exact %s", s.name, effort, hex(out)))
end
local out = assert(Webp.encode(image(s), { effort = 5 }))
print(string.format("%s_e5_loose %s", s.name, hex(out)))
end
-- crop(src, x, y, w, h) -> an rgba8 Image
local function crop(s: any, x: number, y: number, w: number, h: number): Types.Image
local img = Types.Image.new(w, h)
local px = buffer.fromstring(s.rgba)
for j = 0, h - 1 do
buffer.copy(img.pixels, j * w * 4, px, ((y + j) * s.width + x) * 4, w * 4)
end
return img
end
local function animation(cw: number, ch: number, frames: { Types.Frame }): Types.Image
local img = Types.Image.new(cw, ch)
img.frames = frames
img.loops = 2
return img
end
local rose, rosea, grad = bySrc.rose, bySrc.rose_a, bySrc.grad
local anims = {
-- stored as they are: even offsets, blending, dispose to background
animation(70, 46, {
{ image = crop(rose, 0, 0, 70, 46), delay = 0.1, x = 0, y = 0, dispose = "none", blend = "source" },
{ image = crop(rosea, 10, 4, 30, 20), delay = 0.05, x = 10, y = 4, dispose = "background", blend = "over" },
{ image = crop(rosea, 20, 10, 40, 30), delay = 0.2, x = 20, y = 10, dispose = "none", blend = "over" },
{ image = crop(grad, 0, 0, 24, 16), delay = 0.033, x = 46, y = 30, dispose = "none", blend = "source" },
}),
-- flattened: odd offsets and dispose "previous"
animation(70, 46, {
{ image = crop(rosea, 0, 0, 70, 46), delay = 0.1, x = 0, y = 0, dispose = "none", blend = "source" },
{ image = crop(grad, 3, 3, 21, 13), delay = 0.1, x = 5, y = 7, dispose = "previous", blend = "over" },
{ image = crop(rose, 1, 1, 9, 9), delay = 0.1, x = 33, y = 21, dispose = "background", blend = "over" },
{ image = crop(grad, 0, 0, 10, 10), delay = 0.1, x = 60, y = 36, dispose = "none", blend = "over" },
}),
}
for k, img in anims do
local list = {}
for _, f in img.frames :: { Types.Frame } do
table.insert(list, {
rgba = f.image.pixels,
x = f.x,
y = f.y,
w = f.image.width,
h = f.image.height,
blend = f.blend == "over",
disposeBg = f.dispose == "background",
disposePrev = f.dispose == "previous",
hasAlpha = true,
})
end
local crcs = {}
for _, c in Anim.composite(img.width, img.height, list) do
table.insert(crcs, tostring(Crc32.of(c)))
end
local out = assert(Webp.encode(img, { effort = 5, exact = true }))
print(string.format("anim%d:%s %s", k, table.concat(crcs, ","), hex(out)))
end