From 5155ca5ba35115a3c0c25b918c3abd052747ffaf Mon Sep 17 00:00:00 2001 From: 4DBug Date: Sat, 31 Oct 2026 09:09:26 -0500 Subject: [PATCH] vec: bench lines (psd decode/encode, flatten, svg parse/rasterize); psd: fast 8-bit RGB/grey channel merge --- src/Formats/layered/psd/Channels.luau | 11 +++++++ tools/bench.luau | 45 +++++++++++++++++++++++++++ 2 files changed, 56 insertions(+) diff --git a/src/Formats/layered/psd/Channels.luau b/src/Formats/layered/psd/Channels.luau index 4b68fdd..51ce6de 100644 --- a/src/Formats/layered/psd/Channels.luau +++ b/src/Formats/layered/psd/Channels.luau @@ -225,6 +225,17 @@ function Channels.toRGBA(planes: { [any]: buffer }, w: number, h: number, depth: local c1, c2, c3, c4 = planes[1], planes[2], planes[3], planes[4] local alpha = planes.alpha local bitmapW = (w + 7) // 8 + if depth == 8 and (mode == 3 or mode == 1) and c1 and blen(c1) >= n then + -- the common case, without a call per sample + local g8 = if mode == 3 and c2 and blen(c2) >= n then c2 else c1 + local b8 = if mode == 3 and c3 and blen(c3) >= n then c3 else c1 + local a8 = if alpha and blen(alpha) >= n then alpha else nil + for i = 0, n - 1 do + local a = if a8 then readu8(a8, i) else 255 + writeu32(out, i * 4, readu8(c1, i) + readu8(g8, i) * 256 + readu8(b8, i) * 65536 + a * 16777216) + end + return out + end for i = 0, n - 1 do local r, g, b = 0, 0, 0 if mode == 3 then diff --git a/tools/bench.luau b/tools/bench.luau index eb68b0a..f3024b6 100644 --- a/tools/bench.luau +++ b/tools/bench.luau @@ -86,3 +86,48 @@ do Xml.encode(doc) end) end +do + -- layered + vector (ws-vec): PSD decode / encode of 8 RGBA layers 1024 x 768, flatten (codecs/Blend), SVG + -- rasterize at 512 x 512 (codecs/Raster). No fixtures: the inputs are built here. + local Types = require("@formats/Types") + local Psd = require("@formats/layered/psd") + local LayerOps = require("@formats/ops/LayerOps") + local Svg = require("@formats/vector/svg") + local W, H = 1024, 768 + local doc = Types.Layered.new(W, H) + local modes = { "normal", "multiply", "screen", "overlay", "normal", "softLight", "difference", "normal" } + for k = 1, 8 do + local img = Types.Image.new(W, H) + local px = img.pixels + for i = 0, W * H - 1 do + local x, y = i % W, i // W + buffer.writeu32(px, i * 4, ((x * k) % 256) + ((y * 3) % 256) * 256 + (((x + y) // 4) % 256) * 65536 + (if (x // 64 + y // 64 + k) % 3 == 0 then 0 else 200) * 16777216) + end + local l = Types.Layered.layer("L" .. k, img) + l.blend = modes[k] + doc.layers[k] = l + end + local psd = assert(Psd.encode(doc)) + bench("psd decode 8 layers 1024x768", 2, W * H * 4 * 9, function() + Psd.decode(Bytes.reader(psd)) + end) + bench("psd encode 8 layers (RLE)", 1, W * H * 4 * 9, function() + Psd.encode(doc) + end) + bench("flatten 8 layers (blend modes)", 2, W * H * 4 * 8, function() + LayerOps.flatten(doc) + end) + local shapes = {} + for k = 0, 39 do + table.insert(shapes, string.format("", (k * 37) % 100, (k * 53) % 100, 5 + k % 12)) + table.insert(shapes, string.format("", k % 50, k * 2, k * 9)) + end + local text = "" .. table.concat(shapes) .. "" + local v = assert(Svg.decode(Bytes.reader(buffer.fromstring(text)))) + bench("svg parse (80 elements)", 5, #text, function() + Svg.decode(Bytes.reader(buffer.fromstring(text))) + end) + bench("svg rasterize 512x512 (80 shapes)", 2, 512 * 512 * 4, function() + Svg.rasterize(v, 512, 512) + end) +end