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

+91
View File
@@ -161,6 +161,97 @@ do
end, samples, { mutations = 3, garbage = 300 })
end
-- zlibStream: split input ranges (junk between them), a small output window, a sink that takes fixed-size records
local function stream(b: buffer, pieces: number, window: number?, record: number): (string?, any)
local n = buffer.len(b)
local big = buffer.create(n + pieces * 7)
buffer.fill(big, 0, 0xCD)
local ranges = {}
local at, w = 0, 0
for i = 1, pieces do
local len = if i == pieces then n - at else (n - at) // (pieces - i + 1)
buffer.copy(big, w, b, at, len)
table.insert(ranges, { w, len })
at += len
w += len + 7
end
local parts = {}
local size, err = Deflate.zlibStream(big, ranges, function(out: buffer, from: number, to: number): number
table.insert(parts, buffer.readstring(out, from, to - from))
return (to - from) % record
end, window)
if not size then
return nil, err
end
return table.concat(parts), size
end
do
local cases, other = 0, nil
for _, c in Fx.cases do
if c.kind == "zlib" then
local want = Fx.corpora[c.corpus]
local b = buffer.fromstring(c.data)
other = other or b
for _, v in { { 1, nil, 1 }, { 3, 1, 777 }, { 9, 5000, 4096 } } do
local got, size = stream(b, math.min(v[1], buffer.len(b)), v[2], v[3])
t:check(got == want and size == #want, string.format("%s: zlibStream %d ranges, window %s: %s", c.name, v[1], tostring(v[2]), tostring(size)))
end
cases += 1
end
end
t:note("zlibStream: %d zlib streams match, split and through small windows", cases)
-- a sink that yields while another inflate runs in between
local src = buffer.create(300000)
for i = 0, 299999 do
buffer.writeu8(src, i, (i * 13 + (i // 1000) * 7 + (i // 7) % 11) % 256)
end
local z = assert(Deflate.zlibCompress(src, { level = 6 }))
local got = {}
local co = coroutine.wrap(function(): any
return Deflate.zlibStream(z, { { 0, buffer.len(z) } }, function(out: buffer, from: number, to: number): number
table.insert(got, buffer.readstring(out, from, to - from))
coroutine.yield()
return 0
end, 4096)
end)
local res = co()
local yields = 0
while res == nil do
yields += 1
local mid = Deflate.zlib(z)
t:check(mid and buffer.len(mid) == 300000, "inflate while a stream is suspended")
res = co()
end
t:check(res == 300000 and table.concat(got) == buffer.tostring(src), "zlibStream across yields (" .. yields .. ")")
local bad = buffer.fromstring(buffer.tostring(z))
buffer.writeu8(bad, buffer.len(bad) - 1, 0)
local _, e = Deflate.zlibStream(bad, { { 0, buffer.len(bad) } }, function()
return 0
end)
t:check(e and e.code == Err.CHECKSUM, "zlibStream Adler-32: " .. tostring(e))
local _, e2 = Deflate.zlibStream(z, { { 0, buffer.len(z) } }, function()
return -1
end)
t:check(e2 and e2.code == Err.CORRUPT, "zlibStream sink abort: " .. tostring(e2))
local _, e3 = Deflate.zlibStream(z, { { 0, buffer.len(z) + 1 } }, function()
return 0
end)
t:check(e3 and e3.code == Err.ARG, "zlibStream range out of bounds")
local samples = {}
for i, c in Fx.cases do
if c.kind == "zlib" and i % 5 == 0 then
table.insert(samples, c.data)
end
end
t:fuzz("zlibStream", function(b: buffer)
local half = buffer.len(b) // 2
return Deflate.zlibStream(b, { { 0, half }, { half, buffer.len(b) - half } }, function(_: buffer, from: number, to: number): number
return (to - from) % 5
end, 100)
end, samples, { mutations = 3, garbage = 100 })
end
-- peek: the first bytes of a gzip / zlib / raw stream without inflating the rest
do
local src = buffer.create(100000)
+32
View File
@@ -69,4 +69,36 @@ function R.samples(cases: { any }, every: number?): { string }
return samples
end
-- the reference k x k box reduction of an rgba8 Image (the PNG decoder's maxSide / reduce): the alpha-weighted mean,
-- halves up, a fully transparent block is 0, 0, 0, 0
function R.box(img: Types.Image, k: number): Types.Image
local w, h, px = img.width, img.height, img.pixels
local ow, oh = (w + k - 1) // k, (h + k - 1) // k
local out = Types.Image.new(ow, oh)
for oy = 0, oh - 1 do
for ox = 0, ow - 1 do
local r, g, b, a, n = 0, 0, 0, 0, 0
for y = oy * k, math.min(oy * k + k, h) - 1 do
for x = ox * k, math.min(ox * k + k, w) - 1 do
local o = (y * w + x) * 4
local al = buffer.readu8(px, o + 3)
r += buffer.readu8(px, o) * al
g += buffer.readu8(px, o + 1) * al
b += buffer.readu8(px, o + 2) * al
a += al
n += 1
end
end
local o = (oy * ow + ox) * 4
if a > 0 then
buffer.writeu8(out.pixels, o, (r + a // 2) // a)
buffer.writeu8(out.pixels, o + 1, (g + a // 2) // a)
buffer.writeu8(out.pixels, o + 2, (b + a // 2) // a)
buffer.writeu8(out.pixels, o + 3, (a + n // 2) // n)
end
end
end
return out
end
return R
+18
View File
@@ -6,6 +6,7 @@ local Crc32 = require("@rosetta/core/Crc32")
local Err = require("@rosetta/core/Err")
local Types = require("@rosetta/Types")
local Cases = require("@tests/gen/apng_cases")
local R = require("@tests/image/_raster")
local t = T.suite("apng")
@@ -145,6 +146,23 @@ do
t:check(Png.composite({} :: any) == nil, "composite of a non-image")
end
do
-- reduce: every frame is box-reduced on its own; offsets are divided (rounded down)
local frames = 0
for _, c in Cases do
local full = decode(c.data)
local img, err = decode(c.data, { reduce = 2 })
t:check(img and #img.frames == #full.frames, c.name .. " reduce 2: " .. tostring(err))
for i, f in full.frames do
local g = img.frames[i]
t:check(g.x == f.x // 2 and g.y == f.y // 2 and g.delay == f.delay and g.dispose == f.dispose, c.name .. " reduce 2: frame " .. i .. " fields")
t:check(same(g.image, R.box(f.image, 2)), c.name .. " reduce 2: frame " .. i .. " pixels")
frames += 1
end
end
t:note("reduce 2: %d frames match the box reduction of the full frames", frames)
end
do
local samples = {}
for _, c in Cases do
+49
View File
@@ -272,6 +272,52 @@ do
t:note("%d truncated files, %d decoded partially", count, partial)
end
do
-- maxSide picks the smallest scale (up to 8) that fits: the same pixels as djpeg -scale 1/n
local checked, yielded = 0, 0
for _, c in Dec do
if c.refs then
local byScale = {}
for _, r in c.refs do
byScale[r.scale] = r
end
for _, r in c.refs do
local m = math.max(r.width, r.height)
local img, err = decode(c.data, { maxSide = m })
local s = img and img.meta.source.scale
local ref = s and byScale[s]
t:check(img and img.width <= m and img.height <= m and s <= r.scale, c.name .. " maxSide " .. m .. ": " .. tostring(err))
if ref then
t:check(Crc32.of(img.pixels) == ref.crc, c.name .. " maxSide " .. m .. ": pixels match djpeg 1/" .. s)
checked += 1
end
end
local r1 = byScale[1]
if r1 then
-- yield: called, may suspend the coroutine, and the pixels do not change
local calls = 0
local co = coroutine.wrap(function(): any
return decode(c.data, { yield = function()
calls += 1
coroutine.yield(false)
end })
end)
local img = co()
while img == false do
img = co()
end
t:check(img and Crc32.of(img.pixels) == r1.crc and (calls > 0 or r1.height <= 8), c.name .. ": yielding decode")
yielded += 1
end
end
end
t:note("maxSide: %d decodes bit-exact with djpeg -scale; yield: %d files", checked, yielded)
local _, e = decode(file("photo_q90_420"), { maxSide = 0 })
t:check(e and e.code == Err.ARG, "maxSide 0")
local big = decode(file("photo_q90_420"), { maxSide = 1 })
t:check(big and big.meta.source.scale == 8 and big.width == 20, "maxSide below 1/8 stops at 1/8")
end
do
local samples = {}
for i, c in Dec do
@@ -285,6 +331,9 @@ do
t:fuzz("decode 1/8", function(b: buffer)
return Jpeg.decode(Bytes.reader(b), { scale = 8 })
end, samples, { mutations = 2, garbage = 30 })
t:fuzz("decode maxSide + yield", function(b: buffer)
return Jpeg.decode(Bytes.reader(b), { maxSide = 30, yield = function() end })
end, samples, { mutations = 2, keepHeader = 2 })
end
t:done()
+90
View File
@@ -7,6 +7,8 @@ local Err = require("@rosetta/core/Err")
local Types = require("@rosetta/Types")
local Suite = require("@tests/gen/png_suite")
local Gen = require("@tests/gen/png_gen")
local Big = require("@tests/gen/png_big16")
local R = require("@tests/image/_raster")
local t = T.suite("png")
@@ -124,6 +126,91 @@ do
t:check(e2 and e2.code == Err.ARG, "unknown colorType returns Err.ARG")
end
do
-- maxSide / reduce: the box reduction of the full decode, for every colour type, depth and interlace
local n = 0
local function cases(list: { any })
for _, c in list do
if not c.corrupt then
local full = decode(c.data)
for _, k in { 2, 4, 8 } do
local img, err = decode(c.data, { reduce = k })
local want = R.box(full, k)
t:check(img and img.width == want.width and img.height == want.height, c.name .. " reduce " .. k .. ": " .. tostring(err))
t:check(img and buffer.tostring(img.pixels) == buffer.tostring(want.pixels), c.name .. " reduce " .. k .. ": pixels")
n += 1
end
local m = math.max(1, (math.max(c.width, c.height) + 2) // 3)
local img = decode(c.data, { maxSide = m })
t:check(img and img.width <= m and img.height <= m and img.meta.source.reduce >= 1, c.name .. " maxSide " .. m)
local k = img.meta.source.reduce
t:check(k == 1 or (c.width + k // 2 - 1) // (k // 2) > m or (c.height + k // 2 - 1) // (k // 2) > m, c.name .. " maxSide: the smallest factor")
t:check(buffer.tostring(img.pixels) == buffer.tostring((if k == 1 then full else R.box(full, k)).pixels), c.name .. " maxSide " .. m .. ": pixels")
end
end
end
cases(Suite)
cases(Gen)
t:note("reduce: %d decodes match the reference box reduction", n)
local _, e = decode(Gen[1].data, { reduce = 3 })
t:check(e and e.code == Err.ARG, "reduce 3: " .. tostring(e))
local _, e2 = decode(Gen[1].data, { maxSide = 0 })
t:check(e2 and e2.code == Err.ARG, "maxSide 0: " .. tostring(e2))
local _, e3 = decode(Gen[1].data, { yield = 1 })
t:check(e3 and e3.code == Err.ARG, "yield not a function: " .. tostring(e3))
end
do
-- 2048 x 2048 16-bit RGB in 8 KB IDATs: full, maxSide 512, and a yielding decode inside a coroutine
local b = buffer.fromstring(Big.data)
local full, err = decode(b)
t:check(full and full.width == 2048 and Crc32.of(full.pixels) == Big.crc, "16-bit 2048x2048: " .. tostring(err))
local small = decode(b, { maxSide = 512 })
t:check(small and small.width == 512 and small.height == 512 and small.meta.source.reduce == 4, "16-bit maxSide 512: size")
t:check(small and buffer.tostring(small.pixels) == buffer.tostring(R.box(full, 4).pixels), "16-bit maxSide 512: pixels")
local calls = 0
local co = coroutine.create(function(): any
return decode(b, { maxSide = 512, yieldRows = 64, yield = function()
calls += 1
coroutine.yield()
end })
end)
local ok, res = coroutine.resume(co)
local resumes = 0
while ok and coroutine.status(co) ~= "dead" do
resumes += 1
ok, res = coroutine.resume(co)
end
t:check(ok and res and buffer.tostring(res.pixels) == buffer.tostring(small.pixels), "yielding decode: same pixels")
t:check(calls == 2048 // 64 and resumes == calls, string.format("yield: %d calls every 64 rows, %d resumes", calls, resumes))
t:note("16-bit 2048x2048 in %d IDATs: full and maxSide 512 match, %d yields", #Big.data // 8204, calls)
end
do
-- yield on every PngSuite / generated image (plain and Adam7): called, and the pixels do not change
local bad, total = 0, 0
for _, list in { Suite, Gen } do
for _, c in list :: { any } do
if not c.corrupt then
local calls = 0
local img = decode(c.data, { yieldRows = 1, yield = function()
calls += 1
end })
local k = 2
local red = decode(c.data, { reduce = k, yieldRows = 1, yield = function()
calls += 1
end })
if not img or Crc32.of(img.pixels) ~= c.crc or calls < c.height or not red then
bad += 1
end
total += 1
end
end
end
t:eq(bad, 0, "yield: every image decodes the same with a yield hook")
t:note("yield: %d images decode the same with a hook on every row", total)
end
do
local samples = {}
for i, c in Suite do
@@ -142,6 +229,9 @@ do
t:fuzz("decode checkCrc", function(b: buffer)
return Png.decode(Bytes.reader(b), { checkCrc = true })
end, samples, { mutations = 2, garbage = 50 })
t:fuzz("decode maxSide + yield", function(b: buffer)
return Png.decode(Bytes.reader(b), { maxSide = 5, yieldRows = 1, yield = function() end })
end, samples, { mutations = 4, keepHeader = 8 })
end
t:done()