183 lines
6.4 KiB
Luau
183 lines
6.4 KiB
Luau
-- Image throughput (ws-perf-image): decoders, encoders and ImageOps on the tools/bench.luau fixtures. MB/s of rgba8.
|
|
-- luau -O2 tools/bench_image.luau [-a filter]
|
|
local Bytes = require("@rosetta/core/Bytes")
|
|
local Crc32 = require("@rosetta/core/Crc32")
|
|
local Types = require("@rosetta/Types")
|
|
|
|
local runtime = if (_G :: any).jit ~= nil then "luajit" else "luau"
|
|
local filter = (...) :: string?
|
|
local REP = 5
|
|
|
|
local function want(...: string): boolean
|
|
if not filter then
|
|
return true
|
|
end
|
|
for _, k in { ... } do
|
|
if string.find(filter :: string, k, 1, true) or string.find(k, filter :: string, 1, true) then
|
|
return true
|
|
end
|
|
end
|
|
return false
|
|
end
|
|
|
|
local function bench(label: string, n: number, bytes: number, f: () -> ())
|
|
if filter and not string.find(label, filter, 1, true) then
|
|
return
|
|
end
|
|
f()
|
|
local dt = math.huge
|
|
for _ = 1, math.max(n, REP) do
|
|
local t0 = os.clock()
|
|
f()
|
|
dt = math.min(dt, os.clock() - t0)
|
|
end
|
|
print(string.format("%-8s %-36s %8.2f ms %7.1f MB/s", runtime, label, dt * 1000, bytes / dt / 1e6))
|
|
end
|
|
|
|
if want("jpeg") then
|
|
local Jpeg = require("@rosetta/image/jpeg")
|
|
local JB = require("@tests/gen/jpeg_bench")
|
|
for _, c in JB.files do
|
|
local b = buffer.fromstring(c.data)
|
|
local img = Jpeg.decode(Bytes.reader(b))
|
|
assert(img and Crc32.of(img.pixels) == c.crc, "jpeg bench: " .. c.name .. " does not match djpeg")
|
|
bench("jpeg decode " .. c.name, 3, c.width * c.height * 4, function()
|
|
Jpeg.decode(Bytes.reader(b))
|
|
end)
|
|
end
|
|
local b0 = buffer.fromstring(JB.files[1].data)
|
|
bench("jpeg decode 1/8 (DC only)", 3, JB.width * JB.height * 4, function()
|
|
Jpeg.decode(Bytes.reader(b0), { scale = 8 })
|
|
end)
|
|
local src = Types.Image.new(JB.width, JB.height, "rgb8")
|
|
buffer.writestring(src.pixels, 0, JB.rgb)
|
|
for _, o in { { label = "baseline 4:2:0 q85", quality = 85 }, { label = "progressive 4:2:0 q85", quality = 85, progressive = true }, { label = "baseline 4:4:4 q85 optimized", quality = 85, subsampling = "444", optimize = true } } do
|
|
bench("jpeg encode " .. o.label, 2, JB.width * JB.height * 3, function()
|
|
Jpeg.encode(src, o :: any)
|
|
end)
|
|
end
|
|
end
|
|
if want("png", "imageops") then
|
|
local Png = require("@rosetta/image/png")
|
|
local PB = require("@tests/gen/png_bench")
|
|
for _, c in PB do
|
|
local b = buffer.fromstring(c.data)
|
|
bench("png decode " .. c.name, 3, c.width * c.height * 4, function()
|
|
Png.decode(Bytes.reader(b))
|
|
end)
|
|
end
|
|
local img = assert(Png.decode(Bytes.reader(buffer.fromstring(PB[1].data))))
|
|
bench("png encode rgba8", 2, img.width * img.height * 4, function()
|
|
Png.encode(img)
|
|
end)
|
|
local Ops = require("@rosetta/ops/ImageOps")
|
|
local n = img.width * img.height * 4
|
|
bench("imageops resize 1/2 bilinear", 2, n, function()
|
|
Ops.resize(img, img.width // 2, img.height // 2, "bilinear")
|
|
end)
|
|
bench("imageops resize 1/2 lanczos", 2, n, function()
|
|
Ops.resize(img, img.width // 2, img.height // 2, "lanczos")
|
|
end)
|
|
bench("imageops resize 1/4 nearest", 2, n, function()
|
|
Ops.resize(img, img.width // 4, img.height // 4, "nearest")
|
|
end)
|
|
bench("imageops resize x1.5 bilinear", 1, n, function()
|
|
Ops.resize(img, img.width * 3 // 2, img.height * 3 // 2, "bilinear")
|
|
end)
|
|
bench("imageops rotate90", 3, n, function()
|
|
Ops.rotate90(img, 1)
|
|
end)
|
|
bench("imageops convert rgba8 -> gray8", 3, n, function()
|
|
Ops.convert(img, "gray8")
|
|
end)
|
|
bench("imageops convert rgba8 -> rgb8", 3, n, function()
|
|
Ops.convert(img, "rgb8")
|
|
end)
|
|
local other = assert(Ops.flip(img, "h"))
|
|
bench("imageops composite over", 3, n, function()
|
|
Ops.composite(img, other)
|
|
end)
|
|
end
|
|
if want("gif", "quantize") then
|
|
local Gif = require("@rosetta/image/gif")
|
|
local Quantize = require("@rosetta/codecs/image/Quantize")
|
|
local GB = require("@tests/gen/gif_bench")
|
|
local still = buffer.fromstring(GB.still)
|
|
local anim = buffer.fromstring(GB.anim)
|
|
local simg = assert(Gif.decode(Bytes.reader(still)))
|
|
local aimg = assert(Gif.decode(Bytes.reader(anim)))
|
|
local sw, sh = simg.width, simg.height
|
|
bench("gif decode 512x384", 5, sw * sh * 4, function()
|
|
Gif.decode(Bytes.reader(still))
|
|
end)
|
|
bench("gif decode 40 x 128x128 anim", 3, #(aimg.frames :: any) * 128 * 128 * 4, function()
|
|
Gif.decode(Bytes.reader(anim))
|
|
end)
|
|
bench("gif encode 512x384 (exact pal)", 3, sw * sh * 4, function()
|
|
Gif.encode(simg)
|
|
end)
|
|
local photo = Types.Image.new(sw, sh)
|
|
for i = 0, sw * sh - 1 do
|
|
local x, y = i % sw, i // sw
|
|
buffer.writeu32(photo.pixels, i * 4, (x % 256) + (y % 256) * 256 + ((x * y) % 256) * 65536 + 0xFF000000)
|
|
end
|
|
bench("quantize mediancut 256", 3, sw * sh * 4, function()
|
|
Quantize.palette(photo.pixels, sw, sh, 256)
|
|
end)
|
|
local pal = Quantize.palette(photo.pixels, sw, sh, 256)
|
|
bench("quantize map", 3, sw * sh * 4, function()
|
|
Quantize.map(photo.pixels, sw, sh, pal)
|
|
end)
|
|
bench("quantize map + dither", 3, sw * sh * 4, function()
|
|
Quantize.map(photo.pixels, sw, sh, pal, { dither = true })
|
|
end)
|
|
bench("gif encode 512x384 (quantize)", 2, sw * sh * 4, function()
|
|
Gif.encode(photo)
|
|
end)
|
|
end
|
|
if want("webp") then
|
|
local Webp = require("@rosetta/image/webp")
|
|
local WB = require("@tests/gen/webp_bench")
|
|
local n = WB.width * WB.height * 4
|
|
local lossy, lossless = buffer.fromstring(WB.lossy), buffer.fromstring(WB.lossless)
|
|
bench("webp lossy decode 512x512", 3, n, function()
|
|
Webp.decode(Bytes.reader(lossy))
|
|
end)
|
|
bench("webp lossless decode 512x512", 3, n, function()
|
|
Webp.decode(Bytes.reader(lossless))
|
|
end)
|
|
local img = assert(Webp.decode(Bytes.reader(lossless)))
|
|
bench("webp lossless encode (effort 1)", 1, n, function()
|
|
Webp.encode(img, { effort = 1 })
|
|
end)
|
|
end
|
|
if want("qoi", "tga", "bmp", "tiff") then
|
|
local Qoi = require("@rosetta/image/qoi")
|
|
local Tga = require("@rosetta/image/tga")
|
|
local Bmp = require("@rosetta/image/bmp")
|
|
local Tiff = require("@rosetta/image/tiff")
|
|
local QB = require("@tests/gen/qoi_bench")
|
|
local qd = buffer.fromstring(QB.data)
|
|
local n = QB.width * QB.height * 4
|
|
bench("qoi decode 1024x768", 5, n, function()
|
|
Qoi.decode(Bytes.reader(qd))
|
|
end)
|
|
local img = Types.Image.new(QB.width, QB.height)
|
|
img.pixels = buffer.fromstring(QB.pixels)
|
|
bench("qoi encode 1024x768", 5, n, function()
|
|
Qoi.encode(img)
|
|
end)
|
|
local tr = Tga.encode(img, { rle = true }) :: buffer
|
|
bench("tga decode rle 1024x768", 3, n, function()
|
|
Tga.decode(Bytes.reader(tr))
|
|
end)
|
|
local bm = Bmp.encode(img) :: buffer
|
|
bench("bmp decode 1024x768", 3, n, function()
|
|
Bmp.decode(Bytes.reader(bm))
|
|
end)
|
|
local tl = Tiff.encode(img, { compression = "lzw" }) :: buffer
|
|
bench("tiff decode lzw 1024x768", 3, n, function()
|
|
Tiff.decode(Bytes.reader(tl))
|
|
end)
|
|
end
|