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:
1 parent
e3e6273799
commit
64a2553c0c
8 files changed
+321
-9
No files matched your search
@@ -22,9 +22,11 @@ local Jpeg = {
|
||||
-- ------------------------------------------------------------------------------------------------ types
|
||||
export type DecodeOptions = {
|
||||
scale: number?, -- 1 (default), 2, 4 or 8
|
||||
maxSide: number?, -- the smallest scale (up to 8) whose output sides are at most this
|
||||
orient: boolean?, -- default false
|
||||
native: boolean?, -- default false
|
||||
maxPixels: number?, -- default 2^26
|
||||
yield: (() -> ())?, -- pcall'ed every MCU row of each Huffman scan, every block row of the IDCT, every 8 output rows
|
||||
}
|
||||
export type EncodeOptions = {
|
||||
quality: number?, -- 1..100, default 90 (libjpeg scaling of the T.81 annex K tables)
|
||||
@@ -55,12 +57,33 @@ function Jpeg.decode(r: Bytes.Reader, opts: DecodeOptions?): (Types.Image?, Err.
|
||||
return nil, Err.new(Err.ARG, "options must be a table")
|
||||
end
|
||||
local o = opts or {} :: DecodeOptions
|
||||
local scale = o.scale
|
||||
if o.maxSide ~= nil then
|
||||
local m = o.maxSide
|
||||
if type(m) ~= "number" or not (m >= 1) then
|
||||
return nil, Err.new(Err.ARG, "maxSide must be at least 1")
|
||||
end
|
||||
local info, ierr = Codec.info(r.buf, r.base, r.limit - r.base)
|
||||
if not info then
|
||||
return nil, ierr
|
||||
end
|
||||
-- the output side at scale s is ceil(side / s) (libjpeg-turbo jdmaster.c)
|
||||
local s = if type(scale) == "number" then scale else 1
|
||||
while s < 8 and ((info.width + s - 1) // s > m or (info.height + s - 1) // s > m) do
|
||||
s *= 2
|
||||
end
|
||||
scale = s
|
||||
end
|
||||
if o.yield ~= nil and type(o.yield) ~= "function" then
|
||||
return nil, Err.new(Err.ARG, "yield must be a function")
|
||||
end
|
||||
-- two JPEGs back to back are an image and a preview, as in MPF
|
||||
return Codec.decode(r.buf, r.base, r.limit - r.base, {
|
||||
scale = o.scale,
|
||||
scale = scale,
|
||||
orient = o.orient,
|
||||
native = o.native,
|
||||
maxPixels = o.maxPixels,
|
||||
yield = o.yield,
|
||||
fields = false,
|
||||
})
|
||||
end
|
||||
|
||||
@@ -102,6 +102,30 @@ local function paeth3(raw: buffer, cur: number, e: number, stride: number, pt: b
|
||||
end
|
||||
end
|
||||
|
||||
-- 16-bit RGB: two three-byte lanes carried as in paeth3
|
||||
local function paeth6(raw: buffer, cur: number, e: number, stride: number, pt: buffer)
|
||||
local a0, a1, a2, a3, a4, a5 = readu8(raw, cur), readu8(raw, cur + 1), readu8(raw, cur + 2), readu8(raw, cur + 3), readu8(raw, cur + 4), readu8(raw, cur + 5)
|
||||
local u = cur - stride
|
||||
local c0, c1, c2, c3, c4, c5 = readu8(raw, u), readu8(raw, u + 1), readu8(raw, u + 2), readu8(raw, u + 3), readu8(raw, u + 4), readu8(raw, u + 5)
|
||||
for i = cur + 6, e - 6, 6 do
|
||||
local j = i - stride
|
||||
local b0, b1, b2, b3, b4, b5 = readu8(raw, j), readu8(raw, j + 1), readu8(raw, j + 2), readu8(raw, j + 3), readu8(raw, j + 4), readu8(raw, j + 5)
|
||||
a0 = (readu8(raw, i) + c0 + readi16(pt, (a0 * 511 + b0 - c0 * 512) * 2 + 261120)) % 256
|
||||
a1 = (readu8(raw, i + 1) + c1 + readi16(pt, (a1 * 511 + b1 - c1 * 512) * 2 + 261120)) % 256
|
||||
a2 = (readu8(raw, i + 2) + c2 + readi16(pt, (a2 * 511 + b2 - c2 * 512) * 2 + 261120)) % 256
|
||||
a3 = (readu8(raw, i + 3) + c3 + readi16(pt, (a3 * 511 + b3 - c3 * 512) * 2 + 261120)) % 256
|
||||
a4 = (readu8(raw, i + 4) + c4 + readi16(pt, (a4 * 511 + b4 - c4 * 512) * 2 + 261120)) % 256
|
||||
a5 = (readu8(raw, i + 5) + c5 + readi16(pt, (a5 * 511 + b5 - c5 * 512) * 2 + 261120)) % 256
|
||||
writeu8(raw, i, a0)
|
||||
writeu8(raw, i + 1, a1)
|
||||
writeu8(raw, i + 2, a2)
|
||||
writeu8(raw, i + 3, a3)
|
||||
writeu8(raw, i + 4, a4)
|
||||
writeu8(raw, i + 5, a5)
|
||||
c0, c1, c2, c3, c4, c5 = b0, b1, b2, b3, b4, b5
|
||||
end
|
||||
end
|
||||
|
||||
-- any bpp: the left byte is the one written bpp bytes earlier
|
||||
local function paethN(raw: buffer, cur: number, e: number, stride: number, bpp: number, pt: buffer)
|
||||
for i = cur + bpp, e - 1 do
|
||||
@@ -213,6 +237,8 @@ local function unfilter(raw: buffer, p: number, ph: number, rb: number, bpp: num
|
||||
paeth4(raw, cur, e, stride, pt)
|
||||
elseif bpp == 3 then
|
||||
paeth3(raw, cur, e, stride, pt)
|
||||
elseif bpp == 6 then
|
||||
paeth6(raw, cur, e, stride, pt)
|
||||
elseif bpp == 1 then
|
||||
paeth1(raw, cur, e, stride, pt)
|
||||
else
|
||||
|
||||
@@ -29,8 +29,8 @@ export type DecodeOptions = {
|
||||
maxPixels: number?, -- default 2^26, of the source image
|
||||
maxSide: number?, -- output sides at most this: the smallest power-of-two box reduction that fits
|
||||
reduce: number?, -- box reduction factor, a power of two (default 1)
|
||||
yield: (() -> ())?, -- pcall'ed every yieldRows rows (default 16) and per inflate window when interlaced
|
||||
yieldRows: number?,
|
||||
yield: (() -> ())?, -- pcall'ed every yieldRows rows, and per 64 KB inflated when interlaced
|
||||
yieldRows: number?, -- default: about 64 KB of row data
|
||||
}
|
||||
export type EncodeOptions = {
|
||||
level: number?, -- 0..9, default 6
|
||||
@@ -117,8 +117,10 @@ local function pixels(b: buffer, chunks: { { number } }, w: number, ht: number,
|
||||
local k = rd.k
|
||||
local channels = Decode.CHANNELS[ct]
|
||||
if interlace == 0 then
|
||||
local window = math.min(262144, ht * (Decode.rowBytes(w, channels, depth) + 1))
|
||||
local sink, status = Decode.stream(w, ht, depth, ct, pal, tr, k, out, rd.hook, rd.every)
|
||||
local stride = Decode.rowBytes(w, channels, depth) + 1
|
||||
local window = math.min(if rd.hook then 65536 else 262144, ht * stride)
|
||||
local every = if rd.every > 0 then rd.every else math.max(1, 65536 // stride)
|
||||
local sink, status = Decode.stream(w, ht, depth, ct, pal, tr, k, out, rd.hook, every)
|
||||
local _, zerr = Deflate.zlibStream(b, chunks, sink, window)
|
||||
local y, derr = status()
|
||||
if derr then
|
||||
@@ -153,14 +155,14 @@ local function pixels(b: buffer, chunks: { { number } }, w: number, ht: number,
|
||||
pcall(hook)
|
||||
end
|
||||
return 0
|
||||
end, math.min(262144, expected))
|
||||
end, math.min(if hook then 65536 else 262144, expected))
|
||||
if zerr then
|
||||
return zerr
|
||||
elseif got < expected then
|
||||
return Err.new(Err.CORRUPT, string.format("inflate: output is %d bytes, expected %d", got, expected))
|
||||
end
|
||||
local full = if k > 1 then buffer.create(w * ht * 4) else out
|
||||
local ok, derr = Decode.run(raw, w, ht, depth, ct, pal, tr, full, hook, rd.every)
|
||||
local ok, derr = Decode.run(raw, w, ht, depth, ct, pal, tr, full, hook, if rd.every > 0 then rd.every else math.max(1, 65536 // (w * 4)))
|
||||
if not ok then
|
||||
return Err.new(Err.CORRUPT, tostring(derr))
|
||||
end
|
||||
@@ -193,8 +195,8 @@ local function reduction(w: number, ht: number, opts: DecodeOptions?): (Reduce?,
|
||||
if hook ~= nil and type(hook) ~= "function" then
|
||||
return nil, Err.new(Err.ARG, "yield must be a function")
|
||||
end
|
||||
local every = o.yieldRows or 16
|
||||
if type(every) ~= "number" or not (every >= 1) or every % 1 ~= 0 then
|
||||
local every = o.yieldRows or 0
|
||||
if type(every) ~= "number" or not (every >= 0) or every % 1 ~= 0 then
|
||||
return nil, Err.new(Err.ARG, "yieldRows must be a positive integer")
|
||||
end
|
||||
return { k = k, hook = hook, every = every }, nil
|
||||
|
||||
@@ -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)
|
||||
|
||||
@@ -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
|
||||
@@ -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
|
||||
|
||||
@@ -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()
|
||||
@@ -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})
|
||||
Reference in new issue
Block a user