vec: bench lines (psd decode/encode, flatten, svg parse/rasterize); psd: fast 8-bit RGB/grey channel merge

This commit is contained in:
4DBug committed 2026-10-31 09:09:26 -05:00
1 parent 3408cb9b6b
commit 5155ca5ba3
2 files changed
+56

No files matched your search

+11
View File
@@ -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
+45
View File
@@ -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("<circle cx='%d' cy='%d' r='%d' fill='url(#g)' stroke='#222' stroke-width='2' opacity='0.8'/>", (k * 37) % 100, (k * 53) % 100, 5 + k % 12))
table.insert(shapes, string.format("<path d='M%d %d q 20 -30 40 0 t 40 0' fill='none' stroke='hsl(%d, 70%%, 50%%)' stroke-width='3' stroke-dasharray='4 2'/>", k % 50, k * 2, k * 9))
end
local text = "<svg xmlns='http://www.w3.org/2000/svg' width='100' height='100'><defs><radialGradient id='g' fx='0.3' fy='0.3'><stop offset='0' stop-color='#fff'/><stop offset='1' stop-color='#36c'/></radialGradient></defs>" .. table.concat(shapes) .. "</svg>"
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