merge ws/downscale

This commit is contained in:
4DBug committed 2026-11-02 23:41:47 -06:00
commit f1d98ec904
24 files changed
+3432 -1340

No files matched your search

+59
View File
@@ -46,6 +46,9 @@ if want("jpeg") then
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)
@@ -70,6 +73,62 @@ if want("png", "imageops") then
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()
+30
View File
@@ -4,6 +4,8 @@
# tests/gen/png_gen.luau images written by a small PNG writer here: every colour type and bit depth, plain and
# Adam7, odd sizes, every filter, split IDATs, tRNS keys; with the CRC-32 of the RGBA8 pixels
# tests/gen/png_bench.luau 1024 x 768 RGBA / RGB / Adam7 / palette images for tools/bench.luau
# tests/gen/png_big16.luau a 2048 x 2048 16-bit RGB image in 8 KB IDATs (rows alternate between two patterns, every
# 97th row is noise under a random filter), with the CRC-32 of its RGBA8 pixels
import os, random, struct, zlib
INPUTS = ["tests/fixtures/pngsuite", "tests/fixtures/pngsuite_ref.txt"]
@@ -163,3 +165,31 @@ def generate(ctx):
bench.append((label, data, zlib.crc32(exp)))
ctx.emit("png_bench", [{"name": l, "width": 1024, "height": 768, "crc": c, "data": d} for l, d, c in bench])
# --- a large 16-bit image that stays small compressed (rows repeat at a distance of two rows: 24578 bytes)
w = h = 2048
pat = []
for k in range(2):
pat.append([((x * 97 + k * 4000) & 0xFFFF, (x * x * 13 + k * 777) & 0xFFFF, (x * 7919 + k * 31337) & 0xFFFF) for x in range(w)])
def row16(px):
return b"".join(struct.pack(">HHH", *p) for p in px)
def row8(px):
return b"".join(bytes((p[0] >> 8, p[1] >> 8, p[2] >> 8, 255)) for p in px)
raws = [row16(p) for p in pat]
rgba = [row8(p) for p in pat]
out, exp, prev = bytearray(), [], None
for y in range(h):
if y % 97 == 50:
px = [(rng.randrange(65536), rng.randrange(65536), rng.randrange(65536)) for _ in range(w)]
raw, r8, ft = row16(px), row8(px), rng.randrange(1, 5)
else:
raw, r8, ft = raws[y % 2], rgba[y % 2], 0
out += filt(raw, prev, 6, ft) if ft else b"\0" + raw
exp.append(r8)
prev = raw
z = zlib.compress(bytes(out), 9)
data = b"\x89PNG\r\n\x1a\n" + chunk(b"IHDR", struct.pack(">IIBBBBB", w, h, 16, 2, 0, 0, 0))
for i in range(0, len(z), 8192):
data += chunk(b"IDAT", z[i:i + 8192])
data += chunk(b"IEND", b"")
ctx.emit("png_big16", {"width": w, "height": h, "crc": zlib.crc32(b"".join(exp)), "data": data})