png: tests for maxSide / reduce (every PngSuite and generated image, APNG frames, a 2048x2048 16-bit fixture), yield; paeth for 16-bit RGB rows; jpeg: opts.maxSide (scale 1..8) and opts.yield reach the codec; Deflate.zlibStream tests

This commit is contained in:
4DBug committed 2026-11-02 23:01:59 -06:00
1 parent e3e6273799
commit 64a2553c0c
8 files changed
+321 -9

No files matched your search

+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
+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()