init.luau (small, backward compatible): loadTextures forwards those four options to F.load.
273 lines
9.5 KiB
Luau
273 lines
9.5 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/4", 3, JB.width * JB.height * 4, function()
|
|
Jpeg.decode(Bytes.reader(b0), { scale = 4 })
|
|
end)
|
|
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)
|
|
-- 4096 x 4096 16-bit RGB (a normal-map export): sub / up / Paeth rows in stored deflate blocks, 8 KB IDATs
|
|
local Adler32 = require("@rosetta/core/Adler32")
|
|
local W = 4096
|
|
local stride = W * 6 + 1
|
|
local raw = buffer.create(stride * W)
|
|
local seed = 12345
|
|
for i = 0, stride * 7 - 1 do
|
|
seed = (seed * 1103515245 + 12345) % 2147483648
|
|
buffer.writeu8(raw, i, seed // 65536 % 256)
|
|
end
|
|
for y = 0, W - 1 do
|
|
if y >= 7 then
|
|
buffer.copy(raw, y * stride, raw, (y % 7) * stride, stride)
|
|
end
|
|
buffer.writeu8(raw, y * stride, ({ 1, 2, 4, 4 })[y % 4 + 1])
|
|
end
|
|
local nraw = buffer.len(raw)
|
|
local nblocks = (nraw + 65534) // 65535
|
|
local z = buffer.create(2 + nraw + nblocks * 5 + 4)
|
|
buffer.writeu8(z, 0, 0x78)
|
|
buffer.writeu8(z, 1, 0x01)
|
|
local zo = 2
|
|
for i = 0, nblocks - 1 do
|
|
local len = math.min(65535, nraw - i * 65535)
|
|
buffer.writeu8(z, zo, if i == nblocks - 1 then 1 else 0)
|
|
buffer.writeu16(z, zo + 1, len)
|
|
buffer.writeu16(z, zo + 3, 65535 - len)
|
|
buffer.copy(z, zo + 5, raw, i * 65535, len)
|
|
zo += 5 + len
|
|
end
|
|
buffer.writeu32(z, zo, bit32.byteswap(Adler32.raw(raw, 0, nraw)))
|
|
raw = nil :: any
|
|
local nz = buffer.len(z)
|
|
local nidat = (nz + 8191) // 8192
|
|
local p16 = buffer.create(33 + nz + nidat * 12 + 12)
|
|
buffer.writestring(p16, 0, "\137PNG\r\n\26\n\0\0\0\13IHDR")
|
|
buffer.writeu32(p16, 16, bit32.byteswap(W))
|
|
buffer.writeu32(p16, 20, bit32.byteswap(W))
|
|
buffer.writestring(p16, 24, "\16\2\0\0\0")
|
|
local po = 33
|
|
for i = 0, nidat - 1 do
|
|
local len = math.min(8192, nz - i * 8192)
|
|
buffer.writeu32(p16, po, bit32.byteswap(len))
|
|
buffer.writestring(p16, po + 4, "IDAT")
|
|
buffer.copy(p16, po + 8, z, i * 8192, len)
|
|
po += 12 + len
|
|
end
|
|
buffer.writestring(p16, po, "\0\0\0\0IEND")
|
|
z = nil :: any
|
|
bench("png decode 4096^2 rgb16", 1, W * W * 4, function()
|
|
assert(Png.decode(Bytes.reader(p16)))
|
|
end)
|
|
bench("png decode 4096^2 rgb16 maxSide 1024", 1, W * W * 4, function()
|
|
assert(Png.decode(Bytes.reader(p16), { maxSide = 1024 }))
|
|
end)
|
|
p16 = nil :: any
|
|
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
|
|
if want("bcn", "exr") then
|
|
local Bcn = require("@rosetta/codecs/image/Bcn")
|
|
local BB = require("@tests/gen/bcn_bench")
|
|
local w, h = BB.w, BB.h
|
|
local px = buffer.fromstring(BB.rgba)
|
|
for _, kind in { "bc1", "bc3", "bc5" } do
|
|
local blocks = Bcn.encode(kind, px, w, h) :: buffer
|
|
bench("bcn decode " .. kind .. " 256x256", 5, w * h * 4, function()
|
|
Bcn.decode(kind, blocks, 0, w, h)
|
|
end)
|
|
bench("bcn encode " .. kind .. " 256x256", 2, w * h * 4, function()
|
|
Bcn.encode(kind, px, w, h)
|
|
end)
|
|
end
|
|
local b7, b6 = buffer.fromstring(BB.bc7), buffer.fromstring(BB.bc6h)
|
|
bench("bcn decode bc7 256x256", 5, w * h * 4, function()
|
|
Bcn.decode("bc7", b7, 0, w, h)
|
|
end)
|
|
bench("bcn decode bc6h 256x256", 5, w * h * 16, function()
|
|
Bcn.decode("bc6h", b6, 0, w, h)
|
|
end)
|
|
local Exr = require("@rosetta/image/exr")
|
|
local EB = require("@tests/gen/exr_bench")
|
|
local n = 512 * 256 * 16
|
|
for _, k in { "zip", "piz" } do
|
|
local data = buffer.fromstring(EB[k])
|
|
bench("exr decode " .. k .. " 512x256 half", 3, n, function()
|
|
Exr.decode(Bytes.reader(data), { native = true })
|
|
end)
|
|
end
|
|
end
|