merge ws/tex

This commit is contained in:
4DBug committed 2026-10-31 15:44:25 -05:00
commit 93a5afbe65
63 files changed
+11009 -3

No files matched your search

+241
View File
@@ -0,0 +1,241 @@
-- codecs/image/Bcn tests: random BC1..BC7 blocks (every BC7 and BC6H mode, the BC1 3-colour mode, odd sizes) against
-- Pillow's and Godot's (bcdec) decodes; BC6H floats against Godot; the encoders (BC1 / BC3 / BC4 / BC5) within reach
-- of Pillow's encoder PSNR, exact on flat blocks; argument errors; the fuzz no-throw pass. Fixtures: tools/fixtures/bcn.py.
local T = require("@tests/T")
local Bcn = require("@formats/codecs/image/Bcn")
local Half = require("@formats/codecs/image/Half")
local Fx = require("@tests/gen/bcn")
local t = T.suite("bcn")
-- the largest per-byte difference between two rgba8 buffers (channels: which of r, g, b, a count)
local function maxDiff(a: buffer, b: buffer, n: number, channels: { number }): (number, number)
local worst, at = 0, -1
for i = 0, n - 1 do
for _, c in channels do
local d = math.abs(buffer.readu8(a, i * 4 + c) - buffer.readu8(b, i * 4 + c))
if d > worst then
worst, at = d, i
end
end
end
return worst, at
end
local RGBA, RGB, RG, R = { 0, 1, 2, 3 }, { 0, 1, 2 }, { 0, 1 }, { 0 }
-- which channels each reference defines, and the allowed difference to it
local PILLOW: { [string]: { any } } = {
bc1 = { RGBA, 0 },
bc2 = { RGBA, 0 },
bc3 = { RGBA, 0 },
bc4 = { RGB, 0 },
bc5 = { RG, 0 },
bc5s = { RG, 1 }, -- Pillow interpolates the biased unsigned values with truncation
bc7 = { RGBA, 0 },
}
local stats = { pillow = 0, godot = 0 }
for _, c in Fx.cases do
local src = buffer.fromstring(c.data)
local px, err = Bcn.decode(c.kind, src, 0, c.w, c.h)
t:check(px, c.name .. ": " .. tostring(err))
local n = c.w * c.h
if Bcn.FLOAT[c.kind] then
t:eq(buffer.len(px), n * 16, c.name .. ": rgbaf32 size")
-- Godot (bcdec) floats: exact half values
local g = buffer.fromstring(c.godot)
local worst = 0
for i = 0, n - 1 do
for ch = 0, 2 do
local a, b = buffer.readf32(px, i * 16 + ch * 4), buffer.readf32(g, i * 16 + ch * 4)
local d = math.abs(a - b) / math.max(math.abs(b), 1e-3)
worst = math.max(worst, d)
end
end
t:check(worst < 1e-3, string.format("%s: relative error %g against Godot", c.name, worst))
stats.godot += 1
-- Pillow: 8-bit truncation of the clamped float (and no rounding term in its interpolation): within 1. Its
-- signed BC6H differs from bcdec and the spec (negative endpoints), so only BC6H UF16 is compared.
local p = buffer.fromstring(c.pillow)
local w2 = 0
for i = 0, n - 1 do
for ch = 0, 2 do
local v = math.clamp(buffer.readf32(px, i * 16 + ch * 4), 0, 1)
w2 = math.max(w2, math.abs(math.floor(v * 255) - buffer.readu8(p, i * 4 + ch)))
end
end
if c.kind == "bc6h" then
t:check(w2 <= 1, string.format("%s: %d off Pillow", c.name, w2))
stats.pillow += 1
end
else
t:eq(buffer.len(px), n * 4, c.name .. ": rgba8 size")
local ref = PILLOW[c.kind]
local d, at = maxDiff(px, buffer.fromstring(c.pillow), n, ref[1])
t:check(d <= ref[2], string.format("%s: %d off Pillow at pixel %d", c.name, d, at))
stats.pillow += 1
if c.godot then
local chans = if c.kind == "bc4" then R elseif c.kind == "bc5" then RG else RGBA
local dg, atg = maxDiff(px, buffer.fromstring(c.godot), n, chans)
-- bcdec rounds some interpolated values where Pillow truncates: within 1
t:check(dg <= 1, string.format("%s: %d off Godot at pixel %d", c.name, dg, atg))
stats.godot += 1
end
end
-- into a bigger buffer at an offset, from an offset
local big = buffer.create(buffer.len(src) + 5)
buffer.copy(big, 5, src)
local bpp = if Bcn.FLOAT[c.kind] then 16 else 4
local out = buffer.create(n * bpp + 8)
local px2 = Bcn.decode(c.kind, big, 5, c.w, c.h, out, 8)
t:check(px2 == out and buffer.readstring(out, 8, n * bpp) == buffer.readstring(px, 0, n * bpp), c.name .. ": offsets")
end
t:note("%d block images decoded: %d Pillow, %d Godot comparisons", #Fx.cases, stats.pillow, stats.godot)
-- BC4 SNORM = the red channel of BC5 SNORM
do
for _, c in Fx.cases do
if c.kind == "bc5s" then
local src = buffer.fromstring(c.data)
local nb = buffer.len(src) // 16
local b4 = buffer.create(nb * 8)
for i = 0, nb - 1 do
buffer.copy(b4, i * 8, src, i * 16, 8)
end
local p4 = Bcn.decode("bc4s", b4, 0, c.w, c.h) :: buffer
local p5 = Bcn.decode("bc5s", src, 0, c.w, c.h) :: buffer
local ok = true
for i = 0, c.w * c.h - 1 do
ok = ok and buffer.readu8(p4, i * 4) == buffer.readu8(p5, i * 4) and buffer.readu8(p4, i * 4 + 1) == buffer.readu8(p5, i * 4)
end
t:check(ok, c.name .. ": bc4s = bc5s red")
end
end
end
-- encoders
local function psnr(a: buffer, b: buffer, n: number, channels: { number }): number
local se = 0
for i = 0, n - 1 do
for _, c in channels do
local d = buffer.readu8(a, i * 4 + c) - buffer.readu8(b, i * 4 + c)
se += d * d
end
end
return if se == 0 then 99 else 10 * math.log10(255 * 255 * n * #channels / se)
end
for _, e in Fx.enc do
local px = buffer.fromstring(e.rgba)
local n = e.w * e.h
for _, k in { { "bc1", RGB }, { "bc3", RGBA }, { "bc5", RG }, { "bc4", R } } do
local kind, ch = k[1], k[2]
local blocks, err = Bcn.encode(kind, px, e.w, e.h)
t:check(blocks, kind .. " encode: " .. tostring(err))
t:eq(buffer.len(blocks), Bcn.size(kind, e.w, e.h), e.name .. " " .. kind .. ": size")
local back = Bcn.decode(kind, blocks, 0, e.w, e.h) :: buffer
local p = psnr(px, back, n, ch)
local base = e.psnr[kind]
if base then
t:check(p >= base - 0.5, string.format("%s %s: PSNR %.2f dB, Pillow %.2f dB", e.name, kind, p, base))
t:note("%s %s: %.2f dB (Pillow %.2f dB)", e.name, kind, p, base)
else
t:check(p >= 35, string.format("%s %s: PSNR %.2f dB", e.name, kind, p))
end
if kind == "bc1" then
-- the transparent pixels survive (3-colour mode)
local bad = 0
for i = 0, n - 1 do
local a = buffer.readu8(px, i * 4 + 3)
if (a >= 128) ~= (buffer.readu8(back, i * 4 + 3) == 255) then
bad += 1
end
end
t:eq(bad, 0, e.name .. " bc1: alpha mask")
end
end
end
-- flat colours are exact through the single-colour tables (BC1 4-colour and 3-colour, BC4)
do
local rand = T.rng(3)
local worst = 0
for _ = 1, 200 do
local r, g, b = rand(256), rand(256), rand(256)
local px = buffer.create(64)
for i = 0, 15 do
buffer.writeu32(px, i * 4, r + g * 256 + b * 65536 + (if i == 5 then 0 else 255) * 16777216)
end
for _, opaque in { true, false } do
if opaque then
buffer.writeu8(px, 23, 255)
else
buffer.writeu8(px, 23, 0)
end
local back = Bcn.decode("bc1", Bcn.encode("bc1", px, 4, 4) :: buffer, 0, 4, 4) :: buffer
for i = 0, 15 do
if opaque or i ~= 5 then
for c = 0, 2 do
worst = math.max(worst, math.abs(buffer.readu8(back, i * 4 + c) - buffer.readu8(px, i * 4 + c)))
end
end
end
end
end
-- 5 / 6-bit endpoints reach every 8-bit value within 1 (4-colour) / 2 (3-colour midpoints)
t:check(worst <= 2, "flat bc1 colours within " .. worst)
local px = buffer.create(64)
for v = 0, 255, 5 do
for i = 0, 15 do
buffer.writeu32(px, i * 4, v + 0xFF000000)
end
local back = Bcn.decode("bc4", Bcn.encode("bc4", px, 4, 4) :: buffer, 0, 4, 4) :: buffer
t:eq(buffer.readu8(back, 0), v, "flat bc4 " .. v)
end
end
-- Half
do
t:eq(Half.fromFloat(1), 0x3C00, "half 1")
t:eq(Half.fromFloat(-2), 0xC000, "half -2")
t:eq(Half.fromFloat(65504), 0x7BFF, "half max")
t:eq(Half.fromFloat(1e6), 0x7C00, "half overflow")
t:eq(Half.fromFloat(2 ^ -24), 1, "half smallest subnormal")
t:eq(Half.fromFloat(0 / 0), 0x7E00, "half NaN")
local ok = true
for h = 0, 65535 do
local f = Half.TABLE[h]
if f == f and Half.fromFloat(f) ~= h and not (h == 0x8000) then
ok = false
end
end
t:check(ok, "half round trip of every pattern")
end
-- errors
do
local _, e1 = Bcn.decode("bc9", buffer.create(8), 0, 4, 4)
t:check(e1 and string.find(e1, "unsupported") ~= nil, "unknown kind")
local _, e2 = Bcn.decode("bc1", buffer.create(7), 0, 4, 4)
t:check(e2 and string.find(e2, "truncated") ~= nil, "short data")
local _, e3 = Bcn.encode("bc7", buffer.create(64), 4, 4)
t:check(e3 ~= nil, "no bc7 encoder")
local _, e4 = Bcn.encode("bc1", buffer.create(10), 4, 4)
t:check(e4 ~= nil, "short pixels")
end
-- fuzz: random block data of every kind and size
local samples = {}
for _, c in Fx.cases do
table.insert(samples, buffer.fromstring(c.data))
end
for _, kind in { "bc1", "bc2", "bc3", "bc4", "bc4s", "bc5", "bc5s", "bc6h", "bc6hs", "bc7" } do
t:fuzz("decode " .. kind, function(b: buffer)
local n = buffer.len(b) // Bcn.BLOCK[kind]
if n < 1 then
return Bcn.decode(kind, b, 0, 4, 4)
end
return Bcn.decode(kind, b, 0, 4 * n - 1, 3)
end, samples, { mutations = 2 })
end
t:done()
+129
View File
@@ -0,0 +1,129 @@
-- codecs/image/Etc tests: random ETC1 / ETC2 (every mode: individual, differential, T, H, planar; RGB A1 opaque and
-- transparent; RGBA with EAC alpha) and EAC R11 / RG11 (unsigned, signed, multiplier 0) blocks against Godot 4's
-- decode (etcpak); odd sizes; the 16-bit EAC output; errors; the fuzz no-throw pass. Fixtures: tools/fixtures/etc.py.
local T = require("@tests/T")
local Etc = require("@formats/codecs/image/Etc")
local Fx = require("@tests/gen/etc")
local t = T.suite("etc")
local function maxDiff(a: buffer, b: buffer, n: number, channels: { number }): (number, number)
local worst, at = 0, -1
for i = 0, n - 1 do
for _, c in channels do
local d = math.abs(buffer.readu8(a, i * 4 + c) - buffer.readu8(b, i * 4 + c))
if d > worst then
worst, at = d, i
end
end
end
return worst, at
end
local worstEac = 0
for _, c in Fx do
local src = buffer.fromstring(c.data)
local px, err = Etc.decode(c.kind, src, 0, c.w, c.h)
t:check(px, c.name .. ": " .. tostring(err))
local n = c.w * c.h
t:eq(buffer.len(px), n * 4, c.name .. ": size")
local eacKind = string.sub(c.kind, 1, 3) == "eac"
if c.spec then
-- signed EAC against the spec rule (values -1023..1023), biased into 8 bits
local two = c.kind == "eac_rg11s"
local bad = 0
for i = 0, n - 1 do
for ch = 0, if two then 1 else 0 do
local want = math.floor(c.spec[i * 2 + ch + 1] * 127 / 1023 + 0.5) + 128
if buffer.readu8(px, i * 4 + ch) ~= want then
bad += 1
end
end
end
t:eq(bad, 0, c.name .. ": against the spec values")
elseif c.kind == "etc2_rgba1" then
-- Godot decoded the blocks as ETC2 RGB: opaque blocks match it; transparent ones have index 2 transparent
-- black and indexes 1 / 3 unchanged (index 0 drops its modifier in the differential mode); planar is opaque
local g = buffer.fromstring(c.godot)
local bw = (c.w + 3) // 4
local bad = 0
for y = 0, c.h - 1 do
for x = 0, c.w - 1 do
local bo = ((y // 4) * bw + x // 4) * 8
local hi = bit32.byteswap(buffer.readu32(src, bo))
local lo = bit32.byteswap(buffer.readu32(src, bo + 4))
local i = (x % 4) * 4 + y % 4
local idx = bit32.band(bit32.rshift(lo, i + 15), 2) + bit32.band(bit32.rshift(lo, i), 1)
local opaque = bit32.band(hi, 2) ~= 0
local function ch(v: number, at: number): number
v = bit32.band(bit32.rshift(hi, at), 31) + (function(d: number): number
return if d >= 4 then d - 8 else d
end)(bit32.band(bit32.rshift(hi, at - 3), 7))
return v
end
local r2, g2, b2 = ch(0, 27), ch(0, 19), ch(0, 11)
local planar = r2 >= 0 and r2 <= 31 and g2 >= 0 and g2 <= 31 and (b2 < 0 or b2 > 31)
local diff = r2 >= 0 and r2 <= 31 and g2 >= 0 and g2 <= 31 and b2 >= 0 and b2 <= 31
local o = (y * c.w + x) * 4
local mine, ref = buffer.readu32(px, o), buffer.readu32(g, o)
if opaque or planar then
bad += if mine == ref then 0 else 1
elseif idx == 2 then
bad += if mine == 0 then 0 else 1
elseif idx ~= 0 or not diff then
bad += if mine == ref then 0 else 1
end
end
end
t:eq(bad, 0, c.name .. ": punch-through rules over Godot's ETC2 RGB decode")
else
local g = buffer.fromstring(c.godot)
local chans = if c.kind == "eac_r11" then { 0 } elseif eacKind then { 0, 1 } else { 0, 1, 2, 3 }
local d, at = maxDiff(px, g, n, chans)
if eacKind then
-- Godot keeps EAC as 16-bit / half floats and converts to 8 bits with its own rounding
worstEac = math.max(worstEac, d)
t:check(d <= 1, string.format("%s: %d off Godot at pixel %d", c.name, d, at))
else
t:check(d == 0, string.format("%s: %d off Godot at pixel %d", c.name, d, at))
end
end
-- 16-bit EAC agrees with the 8-bit output
if eacKind then
local p16 = Etc.decode(c.kind, src, 0, c.w, c.h, nil, 0, true) :: buffer
t:eq(buffer.len(p16), n * 8, c.name .. ": rgba16 size")
local ok = true
for i = 0, n - 1 do
local v16 = buffer.readu16(p16, i * 8)
ok = ok and math.abs(math.floor(v16 / 257 + 0.5) - buffer.readu8(px, i * 4)) <= 1
end
t:check(ok, c.name .. ": rgba16 ~ rgba8")
end
-- from / into offsets
local big = buffer.create(buffer.len(src) + 3)
buffer.copy(big, 3, src)
local out = buffer.create(n * 4 + 4)
Etc.decode(c.kind, big, 3, c.w, c.h, out, 4)
t:check(buffer.readstring(out, 4, n * 4) == buffer.tostring(px), c.name .. ": offsets")
end
t:note("%d block images match Godot (EAC within %d)", #Fx, worstEac)
do
local _, e1 = Etc.decode("etc3", buffer.create(8), 0, 4, 4)
t:check(e1 and string.find(e1, "unsupported") ~= nil, "unknown kind")
local _, e2 = Etc.decode("etc2_rgba", buffer.create(8), 0, 4, 4)
t:check(e2 and string.find(e2, "truncated") ~= nil, "short data")
t:eq(Etc.size("etc2_rgba", 5, 5), 64, "size")
end
local samples = {}
for _, c in Fx do
table.insert(samples, buffer.fromstring(c.data))
end
for kind in Etc.BLOCK do
t:fuzz("decode " .. kind, function(b: buffer)
local n = buffer.len(b) // Etc.BLOCK[kind]
return Etc.decode(kind, b, 0, math.max(1, 4 * n - 2), 4)
end, samples, { mutations = 2 })
end
t:done()
+78
View File
@@ -0,0 +1,78 @@
-- tests/image/_raster: the shared checks of the simple raster formats' tests (pcx, sgi, sunras, xbm, xpm, ilbm, icns,
-- ani): decode every case against its reference rgba8 pixels, round-trip images through the encoder, fuzz samples.
local Bytes = require("@formats/core/Bytes")
local Types = require("@formats/Types")
local R = {}
local function maxDiff(a: buffer, b: buffer, n: number): (number, number)
local worst, at = 0, -1
for i = 0, n * 4 - 1 do
local d = math.abs(buffer.readu8(a, i) - buffer.readu8(b, i))
if d > worst then
worst, at = d, i // 4
end
end
return worst, at
end
R.maxDiff = maxDiff
-- cases: { { name, data, w, h, rgba, tol? } } (rgba: the reference decode)
function R.decodes(t: any, Fmt: any, cases: { any }, opts: any?): number
local n = 0
for _, c in cases do
local img, err = Fmt.decode(Bytes.reader(buffer.fromstring(c.data)), opts)
t:check(img, c.name .. ": " .. tostring(err))
if img then
t:check(img.width == c.w and img.height == c.h and img.format == "rgba8", string.format("%s: %d x %d %s", c.name, img.width, img.height, img.format))
if img.width == c.w and img.height == c.h then
local d, at = maxDiff(img.pixels, buffer.fromstring(c.rgba), c.w * c.h)
t:check(d <= (c.tol or 0), string.format("%s: %d off the reference at pixel %d", c.name, d, at))
end
n += 1
end
-- from inside a bigger buffer
local big = buffer.create(#c.data + 9)
buffer.writestring(big, 5, c.data)
local img2 = Fmt.decode(Bytes.reader(big, 5, #c.data), opts)
t:check(img2 and img and buffer.tostring(img2.pixels) == buffer.tostring(img.pixels), c.name .. ": from a sub-range")
end
return n
end
-- image(w, h, rgba string) -> Types.Image
function R.image(w: number, h: number, rgba: string): Types.Image
local img = Types.Image.new(w, h)
img.pixels = buffer.fromstring(rgba)
return img
end
-- roundTrip(t, Fmt, img, encodeOpts?, label, tol?) -> the encoded buffer
function R.roundTrip(t: any, Fmt: any, img: Types.Image, eopts: any?, label: string, tol: number?): buffer?
local enc, err = Fmt.encode(img, eopts)
t:check(enc, label .. ": encode " .. tostring(err))
if not enc then
return nil
end
local back, derr = Fmt.decode(Bytes.reader(enc))
t:check(back, label .. ": decode " .. tostring(derr))
if back then
t:check(back.width == img.width and back.height == img.height, label .. ": size")
local d = maxDiff(back.pixels, img.pixels, img.width * img.height)
t:check(d <= (tol or 0), string.format("%s: round trip %d off", label, d))
end
return enc
end
-- samples(cases, every?) -> the fuzz samples (every n-th case's data)
function R.samples(cases: { any }, every: number?): { string }
local samples = {}
for i, c in cases do
if i % (every or 1) == 0 then
table.insert(samples, c.data)
end
end
return samples
end
return R
+72
View File
@@ -0,0 +1,72 @@
-- image/ani tests: animated cursors around Pillow-written ICO / CUR frames (seq + rate, plain, cursor hotspots, INFO
-- title) against Pillow's frame decodes and the written steps / delays; AF_ICON clear and a missing hook are
-- UNSUPPORTED; the encoder through the ICO hook round-trips; the fuzz no-throw pass. Fixtures: tools/fixtures/ani.py.
local T = require("@tests/T")
local Ani = require("@formats/image/ani")
local Ico = require("@formats/image/ico")
local Png = require("@formats/image/png")
local Bytes = require("@formats/core/Bytes")
local Err = require("@formats/core/Err")
local R = require("@tests/image/_raster")
local Fx = require("@tests/gen/ani")
local t = T.suite("ani")
t:eq(Ani.name, "ani", "name")
t:check(Ani.caps.decode and Ani.caps.encode, "caps")
local hooks = {
decodeImage = function(b: buffer): (any, any)
-- Pillow writes the ICO entries as PNG: the ICO decoder gets its own hook
return Ico.decode(Bytes.reader(b), { decodeImage = function(p: buffer): (any, any)
return Png.decode(Bytes.reader(p))
end })
end,
encodeImage = function(img: any, _fmt: string): (any, any)
return Ico.encode(img)
end,
}
for _, c in Fx.cases do
local img, err = Ani.decode(Bytes.reader(buffer.fromstring(c.data)), hooks)
if c.unsupported then
t:check(img == nil and err and err.code == Err.UNSUPPORTED, c.name .. ": UNSUPPORTED")
continue
end
t:check(img, c.name .. ": " .. tostring(err))
t:check(img.width == c.w and img.height == c.h, c.name .. ": size")
t:eq(#img.frames, #c.steps, c.name .. ": steps")
for i, s in c.steps do
local f = img.frames[i]
t:check(math.abs(f.delay - s.delay) < 1e-9, c.name .. ": step " .. i .. " delay")
t:eq(R.maxDiff(f.image.pixels, buffer.fromstring(c.frames[s.frame]), c.w * c.h), 0, c.name .. ": step " .. i .. " pixels")
end
t:eq(img.loops, 0, c.name .. ": loops forever")
if c.title then
t:eq(img.meta.text.title, c.title, c.name .. ": title")
end
if c.hotspot then
t:check(img.meta.hotspot and img.meta.hotspot.x == c.hotspot[1] and img.meta.hotspot.y == c.hotspot[2], c.name .. ": hotspot")
end
-- re-encode through the ICO hook
local enc, eerr = Ani.encode(img, hooks)
t:check(enc, c.name .. ": encode " .. tostring(eerr))
local back = Ani.decode(Bytes.reader(enc :: buffer), hooks) :: any
t:eq(#back.frames, #img.frames, c.name .. ": re-encoded steps")
for i, f in back.frames do
t:eq(buffer.tostring(f.image.pixels), buffer.tostring(img.frames[i].image.pixels), c.name .. ": re-encoded frame " .. i)
t:check(math.abs(f.delay - img.frames[i].delay) < 1 / 120, c.name .. ": re-encoded delay " .. i)
end
end
do
local c = Fx.cases[1]
t:eq(Ani.sniff(Bytes.reader(buffer.fromstring(c.data))), 1, "sniff")
t:eq(Ani.sniff(Bytes.reader(buffer.fromstring("RIFF\0\0\0\0WAVE"))), 0, "sniff WAVE")
local _, e = Ani.decode(Bytes.reader(buffer.fromstring(c.data)))
t:check(e and e.code == Err.UNSUPPORTED, "no hook")
local _, e2 = Ani.encode(Ico.decode(Bytes.reader(buffer.fromstring(c.data))) or ({ pixels = buffer.create(4) } :: any))
t:check(e2 ~= nil, "encode without the hook")
end
t:fuzz("decode", function(b: buffer)
return Ani.decode(Bytes.reader(b), hooks)
end, R.samples(Fx.cases), { mutations = 10 })
t:done()
+212
View File
@@ -0,0 +1,212 @@
-- image/dds tests: files from NVIDIA Texture Tools (BGRA, BC1..BC7 incl. BC6H, mips, DX10, cube / array / volume)
-- and ImageMagick against every surface's reference decode (Pillow per surface, Godot floats for BC6H); hand-written
-- uncompressed DXGI / legacy / FourCC formats against their values (rgba8 and native floats); our encoder's files read
-- by Pillow and nvddsinfo, and round trips; errors; the fuzz no-throw pass. Fixtures: tools/fixtures/dds.py.
local T = require("@tests/T")
local Dds = require("@formats/image/dds")
local Bytes = require("@formats/core/Bytes")
local Err = require("@formats/core/Err")
local Types = require("@formats/Types")
local Fx = require("@tests/gen/dds")
local t = T.suite("dds")
t:eq(Dds.name, "dds", "name")
t:check(Dds.caps.decode and Dds.caps.encode, "caps")
local function dec(data: any, opts: any?): (any, any)
return Dds.decode(Bytes.reader(if type(data) == "string" then buffer.fromstring(data) else data), opts)
end
local function maxDiff(a: buffer, b: buffer, n: number): number
local worst = 0
for i = 0, n * 4 - 1 do
worst = math.max(worst, math.abs(buffer.readu8(a, i) - buffer.readu8(b, i)))
end
return worst
end
-- tool-written files
local surfaces = 0
for _, c in Fx.cases do
local img, err = dec(c.data, { mips = true })
t:check(img, c.name .. ": " .. tostring(err))
t:eq(img.meta.layout, c.layout, c.name .. ": layout")
t:eq(img.meta.source.mips, c.mips, c.name .. ": mips")
local frames = img.frames or { { image = img } }
local native = nil
for _, s in c.surfaces do
local im
if s.level == 0 then
local idx = s.layer * (if c.layout == "volume" then c.depth else 1) + s.slice + 1
im = frames[idx] and frames[idx].image
else
im = img.meta.mipmaps and img.meta.mipmaps[s.level]
end
local label = string.format("%s: level %d layer %d slice %d", c.name, s.level, s.layer, s.slice)
t:check(im and im.width == s.w and im.height == s.h, label .. ": surface size")
if s.floats then
native = native or dec(c.data, { native = true, mips = true })
local nim = if s.level == 0 then native else native.meta.mipmaps[s.level]
t:eq(nim.format, "rgbaf32", label .. ": native rgbaf32")
local g = buffer.fromstring(s.floats)
local worst = 0
for i = 0, s.w * s.h * 4 - 1 do
if i % 4 ~= 3 then
local a, b = buffer.readf32(nim.pixels, i * 4), buffer.readf32(g, i * 4)
worst = math.max(worst, math.abs(a - b) / math.max(math.abs(b), 1e-3))
end
end
t:check(worst < 1e-3, label .. ": floats against Godot " .. worst)
else
local d = maxDiff(im.pixels, buffer.fromstring(s.rgba), s.w * s.h)
t:check(d <= s.tol, string.format("%s: %d off the reference", label, d))
end
surfaces += 1
end
-- surfaces = false keeps only the first
local one = dec(c.data, { surfaces = false })
t:check(one and one.frames == nil, c.name .. ": surfaces = false")
end
t:note("%d tool-written files, %d surfaces match the references", #Fx.cases, surfaces)
-- hand-written formats
for _, c in Fx.hand do
local img, err = dec(c.data)
t:check(img, c.name .. ": " .. tostring(err))
local n = img.width * img.height
local d = maxDiff(img.pixels, buffer.fromstring(c.rgba8), n)
t:check(d <= 1, string.format("%s: rgba8 %d off", c.name, d))
if c.floats then
local nimg = dec(c.data, { native = true })
local f = buffer.fromstring(c.floats)
local worst = 0
local bpp = Types.BPP[nimg.format]
for i = 0, n * 4 - 1 do
local want = buffer.readf32(f, i * 4)
local got
if nimg.format == "rgbaf32" then
got = buffer.readf32(nimg.pixels, i * 4)
elseif nimg.format == "rgba16" then
got = buffer.readu16(nimg.pixels, i * 2) / 65535
else
got = buffer.readu8(nimg.pixels, i) / 255
end
worst = math.max(worst, math.abs(got - want) / math.max(1, math.abs(want)))
end
local tol = if nimg.format == "rgba8" then 1 / 255 elseif nimg.format == "rgba16" then 1 / 65535 else 1e-6
t:check(worst <= tol + 1e-9, string.format("%s: native %s off by %g", c.name, nimg.format, worst))
t:check(bpp ~= nil, c.name .. ": a standard format")
end
end
t:note("%d hand-written formats match", #Fx.hand)
-- our encoder: Pillow / nvddsinfo read the files, and they round-trip
local function source(w: number, h: number): buffer
local px = buffer.create(w * h * 4)
for y = 0, h - 1 do
for x = 0, w - 1 do
local o = (y * w + x) * 4
buffer.writeu8(px, o, math.floor(x / (w - 1) * 255 + 0.5))
buffer.writeu8(px, o + 1, math.floor(y / (h - 1) * 255 + 0.5))
buffer.writeu8(px, o + 2, math.floor(((x * 7 + y * 13) % 64) / 63 * 255 + 0.5))
buffer.writeu8(px, o + 3, math.floor(((x + y) % 5) / 4 * 255 + 0.5))
end
end
return px
end
local src = source(37, 23)
for _, o in Fx.ours do
local img, err = dec(o.data, { mips = true, native = true })
t:check(img, o.name .. ": " .. tostring(err))
t:check(o.width == img.width and o.height == img.height, o.name .. ": nvddsinfo size")
local mips = img.meta.source.mips
t:eq(math.max(1, o.mipcount), mips, o.name .. ": nvddsinfo mip count")
if string.find(o.name, "mips") or string.find(o.name, "rgba16") or string.find(o.name, "rgba32f") or string.find(o.name, "cube") or string.find(o.name, "dx10") then
if not string.find(o.name, "rgba16f") and not string.find(o.name, "array") then
t:check(mips > 1 and #img.meta.mipmaps == mips - 1, o.name .. ": a mip chain")
end
end
if o.pillow then
local p = buffer.fromstring(o.pillow)
local exact = string.find(o.name, "rgba8") or string.find(o.name, "bgra8")
local d = maxDiff(img.pixels, p, img.width * img.height)
t:check(d == 0, string.format("%s: Pillow reads %d off our decode", o.name, d))
if exact then
t:eq(buffer.tostring(p), buffer.tostring(src), o.name .. ": Pillow reads the source exactly")
end
end
if string.find(o.name, "cube") then
t:eq(img.meta.layout, "cube", o.name .. ": cube")
t:eq(#img.frames, 6, o.name .. ": 6 faces")
t:check(string.find(o.caps2, "FE00", 1, true) ~= nil, o.name .. ": nvddsinfo sees all faces")
elseif string.find(o.name, "array") then
t:eq(#img.frames, 6, o.name .. ": 6 layers")
t:eq(o.array, "6", o.name .. ": nvddsinfo array size")
end
if string.find(o.name, "rgba32f") then
t:check(string.find(o.dxgi, "R32G32B32A32_FLOAT", 1, true) ~= nil, o.name .. ": DXGI format " .. o.dxgi)
t:eq(img.format, "rgbaf32", o.name .. ": rgbaf32")
t:check(math.abs(buffer.readf32(img.pixels, 36 * 16) - 4) < 1e-6, o.name .. ": float values kept")
elseif string.find(o.name, "rgba16f") then
t:check(string.find(o.dxgi, "R16G16B16A16_FLOAT", 1, true) ~= nil, o.name .. ": DXGI format " .. o.dxgi)
t:check(math.abs(buffer.readf32(img.pixels, 36 * 16) - 4) < 1e-3, o.name .. ": half values")
elseif string.find(o.name, "rgba16") then
t:check(string.find(o.dxgi, "R16G16B16A16_UNORM", 1, true) ~= nil, o.name .. ": DXGI format " .. o.dxgi)
t:eq(img.format, "rgba16", o.name .. ": rgba16")
end
end
t:note("%d of our files read by Pillow / nvddsinfo", #Fx.ours)
-- round trips
do
local img = Types.Image.new(37, 23)
img.pixels = src
for _, f in { "rgba8", "bgra8" } do
local back = dec(Dds.encode(img, { pixelFormat = f :: any }) :: buffer)
t:eq(buffer.tostring(back.pixels), buffer.tostring(src), f .. " round trip")
end
local f = Types.Image.new(3, 2, "rgbaf32")
for i = 0, 23 do
buffer.writef32(f.pixels, i * 4, i * 0.37 - 2)
end
local back = dec(Dds.encode(f) :: buffer, { native = true })
t:eq(buffer.tostring(back.pixels), buffer.tostring(f.pixels), "rgba32f round trip")
local _, e1 = Dds.encode(f, { pixelFormat = "bc1" })
t:check(e1 and e1.code == Err.ARG, "bc1 needs rgba8")
local _, e2 = Dds.encode(img, { pixelFormat = "bc7" :: any })
t:check(e2 and e2.code == Err.ARG, "no bc7 encoder")
end
-- sniff and errors
do
local c = Fx.cases[1]
t:eq(Dds.sniff(Bytes.reader(buffer.fromstring(c.data))), 1, "sniff")
t:eq(Dds.sniff(Bytes.reader(buffer.fromstring("DDX \124\0\0\0"))), 0, "sniff other")
local _, e = dec(string.sub(c.data, 1, 100))
t:check(e and e.code == Err.TRUNCATED, "short header")
local _, e2 = dec(string.sub(c.data, 1, 140))
t:check(e2 and e2.code == Err.TRUNCATED, "no pixels: " .. tostring(e2))
local bad = buffer.fromstring(c.data)
buffer.writeu32(bad, 4, 100)
local _, e3 = dec(bad)
t:check(e3 and e3.code == Err.CORRUPT, "bad header size")
local _, e4 = dec(c.data, { maxPixels = 10 })
t:check(e4 and e4.code == Err.LIMIT, "limit")
local info = Dds.info(Bytes.reader(buffer.fromstring(c.data)))
t:check(info and info.width > 0, "info")
end
local samples = {}
for i, c in Fx.cases do
if i % 3 == 0 then
table.insert(samples, c.data)
end
end
for _, c in Fx.hand do
table.insert(samples, c.data)
end
t:fuzz("decode", function(b: buffer)
return dec(b, { mips = true })
end, samples, { mutations = 6 })
t:done()
+143
View File
@@ -0,0 +1,143 @@
-- image/exr tests: files from the OpenEXR 3.5 bindings (NONE / RLE / ZIPS / ZIP / PIZ; HALF / FLOAT / UINT; offset
-- data window; decreasing lines; Y-only; a layer), a hand-written subsampled file, exrmaketiled tiles (one level,
-- mip-map, rip-map, round up) and exrmultipart parts against the bindings' reads; PXR24 / B44 / DWAA UNSUPPORTED; our
-- files read back by the bindings and exrheader; round trips; errors; the fuzz no-throw pass. Fixtures:
-- tools/fixtures/exr.py.
local T = require("@tests/T")
local Exr = require("@formats/image/exr")
local Bytes = require("@formats/core/Bytes")
local Err = require("@formats/core/Err")
local Types = require("@formats/Types")
local Fx = require("@tests/gen/exr")
local t = T.suite("exr")
t:eq(Exr.name, "exr", "name")
t:check(Exr.caps.decode and Exr.caps.encode, "caps")
local function dec(data: any, opts: any?): (any, any)
return Exr.decode(Bytes.reader(if type(data) == "string" then buffer.fromstring(data) else data), opts)
end
-- the largest difference between two rgbaf32 buffers (exact for HALF / FLOAT, UINT as their values)
local function worst(a: buffer, b: buffer, n: number): number
local m = 0
for i = 0, n * 4 - 1 do
local x, y = buffer.readf32(a, i * 4), buffer.readf32(b, i * 4)
if x ~= y then
m = math.max(m, math.abs(x - y) / math.max(1, math.abs(y)))
end
end
return m
end
for _, c in Fx.cases do
if c.unsupported then
local img, e = dec(c.data)
t:check(img == nil and e and e.code == Err.UNSUPPORTED, c.name .. ": UNSUPPORTED (" .. tostring(e) .. ")")
continue
end
local opts = { native = true, layer = c.layer, part = c.part, mips = c.mips }
local img, err = dec(c.data, opts)
t:check(img, c.name .. ": " .. tostring(err))
t:check(img.width == c.w and img.height == c.h and img.format == "rgbaf32", c.name .. ": size")
local d = worst(img.pixels, buffer.fromstring(c.floats), c.w * c.h)
t:check(d < 1e-6, string.format("%s: off by %g", c.name, d))
if c.partName then
local byName = dec(c.data, { native = true, part = c.partName })
t:check(byName and buffer.tostring(byName.pixels) == buffer.tostring(img.pixels), c.name .. ": part by name")
t:eq(img.meta.source.parts[c.part].name, c.partName, c.name .. ": part names")
end
if c.mips and string.find(c.name, "mip") then
local mm = img.meta.mipmaps
t:check(mm and #mm >= 5, c.name .. ": mip levels")
local up = string.find(c.name, "up") ~= nil
t:eq(mm[1].width, if up then math.ceil(c.w / 2) else c.w // 2, c.name .. ": level 1 width")
end
-- the rgba8 display conversion
local d8 = dec(c.data, { layer = c.layer, part = c.part })
t:eq(d8.format, "rgba8", c.name .. ": rgba8 by default")
end
t:note("%d files match the OpenEXR bindings", #Fx.cases)
-- the window, the attributes
do
for _, c in Fx.cases do
if c.name == "window_zip.exr" then
local img = dec(c.data)
t:check(img.meta.dataWindow.x == 5 and img.meta.dataWindow.y == -3, "data window origin")
t:check(img.meta.displayWindow.width == 40 and img.meta.displayWindow.height == 20, "display window")
t:eq(img.meta.source.compression, "zip", "compression name")
end
end
end
-- our files: the bindings read our values back, exrheader accepts them
for _, o in Fx.ours do
t:check(o.headerOk, o.name .. ": exrheader")
local img = dec(o.data, { native = true })
local n = img.width * img.height
local d = worst(img.pixels, buffer.fromstring(o.floats), n)
t:check(d == 0, string.format("%s: the bindings read %g off our decode", o.name, d))
end
t:note("%d of our files read back by the OpenEXR bindings", #Fx.ours)
-- round trips: FLOAT exactly, HALF to half precision
do
local f = Types.Image.new(13, 9, "rgbaf32")
local rand = T.rng(5)
for i = 0, 13 * 9 * 4 - 1 do
buffer.writef32(f.pixels, i * 4, (rand(20000) - 5000) / 997)
end
for _, comp in { "none", "zips", "zip" } do
local back = dec(Exr.encode(f, { compression = comp :: any, pixelType = "float" }) :: buffer, { native = true })
t:eq(buffer.tostring(back.pixels), buffer.tostring(f.pixels), comp .. ": float round trip")
local hb = dec(Exr.encode(f, { compression = comp :: any }) :: buffer, { native = true })
t:check(worst(hb.pixels, f.pixels, 13 * 9) < 1e-3, comp .. ": half round trip")
end
local img8 = Types.Image.new(4, 4)
for i = 0, 63 do
buffer.writeu8(img8.pixels, i, (i * 37) % 256)
end
local back8 = dec(Exr.encode(img8, { pixelType = "float" }) :: buffer)
local ok = true
for i = 0, 63 do
ok = ok and math.abs(buffer.readu8(back8.pixels, i) - buffer.readu8(img8.pixels, i)) <= 1
end
t:check(ok, "rgba8 through the display curve and back")
local _, e = Exr.encode(f, { compression = "piz" :: any })
t:check(e and e.code == Err.ARG, "no PIZ encoder")
end
-- sniff, info, errors
do
local c = Fx.cases[1]
t:eq(Exr.sniff(Bytes.reader(buffer.fromstring(c.data))), 1, "sniff")
t:eq(Exr.sniff(Bytes.reader(buffer.fromstring("v/1\0"))), 0, "sniff other")
local info = Exr.info(Bytes.reader(buffer.fromstring(c.data)))
t:check(info and #info.parts == 1 and #info.parts[1].channels == 4, "info")
local _, e = dec(string.sub(c.data, 1, 40))
t:check(e and e.code == Err.TRUNCATED, "short header: " .. tostring(e))
local _, e2 = dec(c.data, { maxPixels = 10 })
t:check(e2 and e2.code == Err.LIMIT, "limit")
local _, e3 = dec(c.data, { part = 3 })
t:check(e3 and e3.code == Err.ARG, "no such part")
-- a zeroed offset table is rebuilt
local b = buffer.fromstring(c.data)
local info2 = Exr.info(Bytes.reader(b)) :: any
local tab = info2.dataStart - 8 * info2.parts[1].chunks
buffer.fill(b, tab, 0, 8 * info2.parts[1].chunks)
local img = dec(b, { native = true })
t:check(img and buffer.tostring(img.pixels) == buffer.tostring(dec(c.data, { native = true }).pixels), "rebuilt offset table")
end
local samples = {}
for i, c in Fx.cases do
if i % 3 == 0 or c.unsupported then
table.insert(samples, c.data)
end
end
t:fuzz("decode", function(b: buffer)
return dec(b, { mips = true })
end, samples, { mutations = 6 })
t:done()
+84
View File
@@ -0,0 +1,84 @@
-- image/icns tests: a libicns file (RLE entries + masks, JPEG 2000 entries skipped), a Pillow file (PNG entries through
-- the decodeImage hook) and a hand-written one (ics#, raw is32, ic04 ARGB) against icns2png's / the written pixels;
-- our RLE file read back by icns2png; PNG entries through encodeImage; errors; the fuzz no-throw pass. Fixtures:
-- tools/fixtures/icns.py.
local T = require("@tests/T")
local Icns = require("@formats/image/icns")
local Png = require("@formats/image/png")
local Bytes = require("@formats/core/Bytes")
local Err = require("@formats/core/Err")
local Types = require("@formats/Types")
local R = require("@tests/image/_raster")
local Fx = require("@tests/gen/icns")
local t = T.suite("icns")
t:eq(Icns.name, "icns", "name")
t:check(Icns.caps.decode and Icns.caps.encode, "caps")
local hooks = {
decodeImage = function(b: buffer): (any, any)
return Png.decode(Bytes.reader(b))
end,
encodeImage = function(img: any, _fmt: string): (any, any)
return Png.encode(img)
end,
}
local function check(c: any)
local img, err = Icns.decode(Bytes.reader(buffer.fromstring(c.data)), hooks)
t:check(img, c.name .. ": " .. tostring(err))
local frames = img.frames or { { image = img } }
t:check(#frames >= #c.entries, c.name .. ": entries") -- icns2png writes one file per size (@2x entries collide)
for _, e in c.entries do
local found = nil
for _, f in frames do
local im = f.image
if im.width == e.w and im.height == e.h and (e.type == "" or im.meta.source.type == e.type) then
found = im
end
end
t:check(found, c.name .. ": entry " .. e.type .. " " .. e.w)
if found then
local d = R.maxDiff(found.pixels, buffer.fromstring(e.rgba), e.w * e.h)
t:eq(d, 0, string.format("%s: %s %dx%d pixels", c.name, e.type, e.w, e.h))
end
end
t:eq(if img.meta.skipped then #img.meta.skipped else 0, c.skipped or 0, c.name .. ": skipped entries")
return img
end
for _, c in Fx.cases do
check(c)
end
for _, o in Fx.ours do
check(o)
end
t:note("%d files and our encoder's file match", #Fx.cases)
do
-- PNG entries through the hooks, legacy entries without them
local img = Types.Image.new(64, 64)
for i = 0, 64 * 64 - 1 do
buffer.writeu32(img.pixels, i * 4, i * 2654435761 % 4294967296)
end
local small = Types.Image.new(16, 16)
buffer.fill(small.pixels, 0, 200)
img.frames = { { image = img, delay = 0, x = 0, y = 0, dispose = "none", blend = "source" }, { image = small, delay = 0, x = 0, y = 0, dispose = "none", blend = "source" } }
local enc = Icns.encode(img, hooks) :: buffer
local back = Icns.decode(Bytes.reader(enc), hooks) :: any
t:check(back and back.width == 64 and buffer.tostring(back.pixels) == buffer.tostring(img.pixels), "PNG entries round trip")
t:eq(#back.frames, 2, "two sizes")
local _, e = Icns.encode(img)
t:check(e and e.code == Err.ARG, "64 x 64 needs the PNG hook")
local leg = Icns.encode(small) :: buffer
local lb = Icns.decode(Bytes.reader(leg)) :: any
t:eq(buffer.tostring(lb.pixels), buffer.tostring(small.pixels), "legacy round trip")
local _, e2 = Icns.decode(Bytes.reader(enc))
t:check(e2 and e2.code == Err.UNSUPPORTED, "PNG entries without the hook: UNSUPPORTED")
t:eq(Icns.sniff(Bytes.reader(enc)), 1, "sniff")
local _, e3 = Icns.encode(Types.Image.new(10, 12))
t:check(e3 and e3.code == Err.ARG, "not square")
end
t:fuzz("decode", function(b: buffer)
return Icns.decode(Bytes.reader(b), hooks)
end, R.samples(Fx.cases), { mutations = 6 })
t:done()
+72
View File
@@ -0,0 +1,72 @@
-- image/ilbm tests: files from netpbm's ppmtoilbm (palette, uncompressed, HAM6, HAM8, 24-bit) and hand-written ones
-- (EHB, chunky PBM, a mask plane, a transparent colour) against ilbmtoppm (colours) and the written masks (alpha); our
-- encoder's files read by ilbmtoppm; round trips (palette, 24 and 32 planes, uncompressed); errors; the fuzz no-throw
-- pass. Fixtures: tools/fixtures/ilbm.py.
local T = require("@tests/T")
local Ilbm = require("@formats/image/ilbm")
local Bytes = require("@formats/core/Bytes")
local Err = require("@formats/core/Err")
local R = require("@tests/image/_raster")
local Fx = require("@tests/gen/ilbm")
local t = T.suite("ilbm")
t:eq(Ilbm.name, "ilbm", "name")
t:check(Ilbm.caps.decode and Ilbm.caps.encode, "caps")
local function check(c: any)
local img, err = Ilbm.decode(Bytes.reader(buffer.fromstring(c.data)))
t:check(img, c.name .. ": " .. tostring(err))
t:check(img.width == c.w and img.height == c.h, c.name .. ": size")
local ref = buffer.fromstring(c.rgba)
local bad, badA = 0, 0
for i = 0, c.w * c.h - 1 do
local transparent = c.transparent and c.transparent[i + 1]
local v = buffer.readu32(img.pixels, i * 4)
if transparent then
badA += if v == 0 then 0 else 1
else
for k = 0, 3 do
if math.abs(buffer.readu8(img.pixels, i * 4 + k) - buffer.readu8(ref, i * 4 + k)) > (c.tol or 0) then
bad += 1
break
end
end
end
end
t:eq(bad, 0, c.name .. ": colours against ilbmtoppm")
t:eq(badA, 0, c.name .. ": transparent pixels")
return img
end
for _, c in Fx.cases do
check(c)
if not c.transparent and not c.tol then
R.roundTrip(t, Ilbm, R.image(c.w, c.h, c.rgba), nil, c.name .. " re-encoded")
R.roundTrip(t, Ilbm, R.image(c.w, c.h, c.rgba), { compress = false }, c.name .. " re-encoded uncompressed")
end
end
for _, o in Fx.ours do
check(o)
end
t:note("%d files and %d of ours match ilbmtoppm", #Fx.cases, #Fx.ours)
do
local img = R.image(3, 2, string.rep("\1\2\3\128", 6))
local enc = R.roundTrip(t, Ilbm, img, nil, "alpha: 32 planes") :: buffer
t:eq(Ilbm.sniff(Bytes.reader(enc)), 1, "sniff")
t:eq(Ilbm.sniff(Bytes.reader(buffer.fromstring("FORM\0\0\0\4AIFF"))), 0, "sniff AIFF")
local back = Ilbm.decode(Bytes.reader(enc)) :: any
t:eq(back.meta.source.planes, 32, "32 planes")
local _, e = Ilbm.decode(Bytes.reader(buffer.fromstring("FORM\0\0\0\4ILBM")))
t:check(e and e.code == Err.CORRUPT, "no BMHD")
local _, e2 = Ilbm.decode(Bytes.reader(enc), { maxPixels = 2 })
t:check(e2 and e2.code == Err.LIMIT, "limit")
for _, c in Fx.cases do
if c.name == "hand_transparent.iff" then
local im = Ilbm.decode(Bytes.reader(buffer.fromstring(c.data))) :: any
t:eq(im.meta.text and im.meta.text.name, "tiny", "NAME chunk")
end
end
end
t:fuzz("decode", function(b: buffer)
return Ilbm.decode(Bytes.reader(b))
end, R.samples(Fx.cases), { mutations = 6 })
t:done()
+167
View File
@@ -0,0 +1,167 @@
-- image/ktx tests: KTX2 files from KTX-Software (`ktx create` from PNGs and raw data in uncompressed / packed / float /
-- BCn / ETC2 / EAC formats, mips, cube maps, arrays, 3D, Zstandard and ZLIB supercompression; Basis Universal is
-- UNSUPPORTED) and KTX1 files from toktx / nvcompress / hand-written ones (big-endian, packed types, row padding,
-- KTXorientation) against their sources or reference decodes; our KTX2 files through `ktx validate` / `ktx extract`;
-- round trips; errors; the fuzz no-throw pass. Fixtures: tools/fixtures/ktx.py.
local T = require("@tests/T")
local Ktx = require("@formats/image/ktx")
local Bytes = require("@formats/core/Bytes")
local Err = require("@formats/core/Err")
local Types = require("@formats/Types")
local Fx = require("@tests/gen/ktx")
local t = T.suite("ktx")
t:eq(Ktx.name, "ktx", "name")
t:check(Ktx.caps.decode and Ktx.caps.encode, "caps")
local function dec(data: any, opts: any?): (any, any)
return Ktx.decode(Bytes.reader(if type(data) == "string" then buffer.fromstring(data) else data), opts)
end
local function maxDiff(a: buffer, b: buffer, n: number): number
local worst = 0
for i = 0, n * 4 - 1 do
worst = math.max(worst, math.abs(buffer.readu8(a, i) - buffer.readu8(b, i)))
end
return worst
end
local nsurf = 0
for _, c in Fx.cases do
if c.unsupported then
local img, e = dec(c.data)
t:check(img == nil and e and e.code == Err.UNSUPPORTED, c.name .. ": UNSUPPORTED (" .. tostring(e) .. ")")
continue
end
local img, err = dec(c.data, { mips = true })
t:check(img, c.name .. ": " .. tostring(err))
if c.layout then
t:eq(img.meta.layout, c.layout, c.name .. ": layout")
end
if c.mips then
t:eq(img.meta.source.mips, c.mips, c.name .. ": mip count")
end
if c.srgb then
t:eq(img.meta.colorSpace, "srgb", c.name .. ": sRGB")
end
local native = nil
local frames = img.frames or { { image = img } }
for _, s in c.surfaces do
local label = string.format("%s: level %d layer %d slice %d", c.name, s.level, s.layer, s.slice)
local im
if s.level == 0 then
local f = frames[s.layer + s.slice + 1]
im = f and f.image
else
im = img.meta.mipmaps and img.meta.mipmaps[s.level]
end
t:check(im and im.width == s.w and im.height == s.h, label .. ": surface size")
if s.rgba then
local d = maxDiff(im.pixels, buffer.fromstring(s.rgba), s.w * s.h)
t:check(d <= s.tol, string.format("%s: %d off the reference", label, d))
end
if s.floats and s.level == 0 and s.layer == 0 then
native = native or dec(c.data, { native = true })
local f = buffer.fromstring(s.floats)
local worst = 0
for i = 0, s.w * s.h * 4 - 1 do
local want = buffer.readf32(f, i * 4)
local got
if native.format == "rgbaf32" then
got = buffer.readf32(native.pixels, i * 4)
elseif native.format == "rgba16" then
got = buffer.readu16(native.pixels, i * 2) / 65535
else
got = buffer.readu8(native.pixels, i) / 255
end
if i % 4 ~= 3 or not string.find(c.name, "bc6h") then
worst = math.max(worst, math.abs(got - want) / math.max(1, math.abs(want)))
end
end
local tol = if native.format == "rgba8" then 1 / 255 elseif native.format == "rgba16" then 1 / 65535 else 1e-3
t:check(worst <= tol + 1e-9, string.format("%s: native %s off by %g", label, native.format, worst))
end
nsurf += 1
end
end
t:note("%d files, %d surfaces match", #Fx.cases, nsurf)
-- our files: ktx validate accepts them, ktx extract reads the first image
local function source(w: number, h: number): buffer
local px = buffer.create(w * h * 4)
for y = 0, h - 1 do
for x = 0, w - 1 do
local o = (y * w + x) * 4
buffer.writeu8(px, o, math.floor(x / (w - 1) * 255 + 0.5))
buffer.writeu8(px, o + 1, math.floor(y / (h - 1) * 255 + 0.5))
buffer.writeu8(px, o + 2, math.floor(((x * 7 + y * 13) % 64) / 63 * 255 + 0.5))
buffer.writeu8(px, o + 3, math.floor(((x + y) % 5) / 4 * 255 + 0.5))
end
end
return px
end
local src = source(37, 23)
for _, o in Fx.ours do
t:check(o.valid, o.name .. ": ktx validate: " .. tostring(o.report))
local img, err = dec(o.data, { mips = true, native = true })
t:check(img, o.name .. ": " .. tostring(err))
if o.png and (string.find(o.name, "rgba8") or string.find(o.name, "srgb")) then
t:eq(o.png, buffer.tostring(src), o.name .. ": ktx extract gives the source")
end
if string.find(o.name, "mips") or string.find(o.name, "zlib") then
t:eq(#img.meta.mipmaps, 5, o.name .. ": mip chain")
end
if string.find(o.name, "cube") then
t:eq(img.meta.layout, "cube", o.name .. ": cube")
t:eq(#img.frames, 6, o.name .. ": faces")
end
end
t:note("%d of our files pass ktx validate", #Fx.ours)
-- round trips
do
local img = Types.Image.new(37, 23)
img.pixels = src
for _, o in { {}, { mips = true }, { zlib = 9 }, { srgb = true } } do
local back = dec(Ktx.encode(img, o :: any) :: buffer)
t:eq(buffer.tostring(back.pixels), buffer.tostring(src), "rgba8 round trip")
end
local f = Types.Image.new(3, 2, "rgbaf32")
for i = 0, 23 do
buffer.writef32(f.pixels, i * 4, i * 0.37 - 2)
end
local back = dec(Ktx.encode(f) :: buffer, { native = true })
t:eq(buffer.tostring(back.pixels), buffer.tostring(f.pixels), "rgba32f round trip")
local _, e = Ktx.encode(f, { pixelFormat = "rgba8" })
t:check(e and e.code == Err.ARG, "format mismatch")
local _, e2 = Ktx.encode(img, { zlib = 12 })
t:check(e2 and e2.code == Err.ARG, "zlib level")
end
-- sniff, info, errors
do
local c = Fx.cases[1]
t:eq(Ktx.sniff(Bytes.reader(buffer.fromstring(c.data))), 1, "sniff KTX2")
t:eq(Ktx.sniff(Bytes.reader(buffer.fromstring("\171KTX 11\187\r\n\26\n"))), 1, "sniff KTX1")
t:eq(Ktx.sniff(Bytes.reader(buffer.fromstring("PNG"))), 0, "sniff other")
local info = Ktx.info(Bytes.reader(buffer.fromstring(c.data)))
t:check(info and info.version == 2 and info.vkFormat == 37, "info")
local _, e = dec(string.sub(c.data, 1, 60))
t:check(e and e.code == Err.TRUNCATED, "short header")
local _, e2 = dec(string.sub(c.data, 1, #c.data - 100))
t:check(e2 and e2.code == Err.TRUNCATED, "cut data: " .. tostring(e2))
local _, e3 = dec(c.data, { maxPixels = 10 })
t:check(e3 and e3.code == Err.LIMIT, "limit")
end
local samples = {}
for i, c in Fx.cases do
if i % 2 == 0 or c.unsupported then
table.insert(samples, c.data)
end
end
t:fuzz("decode", function(b: buffer)
return dec(b, { mips = true })
end, samples, { mutations = 6 })
t:done()
+39
View File
@@ -0,0 +1,39 @@
-- image/pcx tests: files from netpbm (24-bit, 8-bit, packed 2 / 4-bit, 3 / 4 planes, mono), ImageMagick and Pillow
-- against pcxtoppm's decode; our files read by ImageMagick; round trips (palette, 24-bit, 32-bit alpha); errors; the
-- fuzz no-throw pass. Fixtures: tools/fixtures/pcx.py.
local T = require("@tests/T")
local Pcx = require("@formats/image/pcx")
local Bytes = require("@formats/core/Bytes")
local Err = require("@formats/core/Err")
local R = require("@tests/image/_raster")
local Fx = require("@tests/gen/pcx")
local t = T.suite("pcx")
t:eq(Pcx.name, "pcx", "name")
t:check(Pcx.caps.decode and Pcx.caps.encode, "caps")
t:note("%d files match pcxtoppm", R.decodes(t, Pcx, Fx.cases))
-- our files: ImageMagick reads what we decode
for _, o in Fx.ours do
local img = Pcx.decode(Bytes.reader(buffer.fromstring(o.data))) :: any
t:eq(R.maxDiff(img.pixels, buffer.fromstring(o.rgba), o.w * o.h), 0, o.name .. ": ImageMagick reads our file")
end
for _, c in Fx.cases do
R.roundTrip(t, Pcx, R.image(c.w, c.h, c.rgba), nil, c.name .. " re-encoded")
R.roundTrip(t, Pcx, R.image(c.w, c.h, c.rgba), { palette = false }, c.name .. " re-encoded 24-bit")
end
do
local img = R.image(3, 2, string.rep("\1\2\3\128", 6))
local enc = R.roundTrip(t, Pcx, img, nil, "alpha: 4 planes") :: buffer
t:eq(buffer.readu8(enc, 65), 4, "alpha -> 4 planes")
t:eq(Pcx.sniff(Bytes.reader(enc)), 0.8, "sniff")
t:eq(Pcx.sniff(Bytes.reader(buffer.fromstring("GIF89a"))), 0, "sniff other")
local _, e = Pcx.decode(Bytes.reader(buffer.fromstring(string.sub(buffer.tostring(enc), 1, 60))))
t:check(e and e.code == Err.TRUNCATED, "short header")
local _, e2 = Pcx.decode(Bytes.reader(enc), { maxPixels = 2 })
t:check(e2 and e2.code == Err.LIMIT, "limit")
end
t:fuzz("decode", function(b: buffer)
return Pcx.decode(Bytes.reader(b))
end, R.samples(Fx.cases), { mutations = 6 })
t:done()
+34
View File
@@ -0,0 +1,34 @@
-- image/sgi tests: files from netpbm / ImageMagick / Pillow against ImageMagick's decode, our encoder's files read
-- by ImageMagick, round trips through every option, errors, the fuzz no-throw pass. Fixtures: tools/fixtures/sgi.py.
local T = require("@tests/T")
local Sgi = require("@formats/image/sgi")
local Bytes = require("@formats/core/Bytes")
local Err = require("@formats/core/Err")
local R = require("@tests/image/_raster")
local Fx = require("@tests/gen/sgi")
local t = T.suite("sgi")
t:eq(Sgi.name, "sgi", "name")
t:check(Sgi.caps.decode and Sgi.caps.encode, "caps")
t:note("%d files match ImageMagick", R.decodes(t, Sgi, Fx.cases))
t:note("%d of our files read by ImageMagick", R.decodes(t, Sgi, Fx.ours))
for _, c in Fx.cases do
R.roundTrip(t, Sgi, R.image(c.w, c.h, c.rgba), nil, c.name .. " re-encoded")
R.roundTrip(t, Sgi, R.image(c.w, c.h, c.rgba), { rle = false }, c.name .. " re-encoded without RLE")
end
do
local enc = Sgi.encode(R.image(2, 2, string.rep("\1\2\3\4", 4))) :: buffer
t:eq(Sgi.sniff(Bytes.reader(enc)), 1, "sniff")
t:eq(Sgi.sniff(Bytes.reader(buffer.fromstring("GIF89a"))), 0, "sniff other")
local _, e = Sgi.decode(Bytes.reader(buffer.fromstring(string.sub(buffer.tostring(enc), 1, 20))))
t:check(e and e.code == Err.TRUNCATED, "short header")
local _, e2 = Sgi.decode(Bytes.reader(enc), { maxPixels = 2 })
t:check(e2 and e2.code == Err.LIMIT, "limit")
local _, e3 = Sgi.encode({} :: any)
t:check(e3 and e3.code == Err.ARG, "bad image")
end
t:fuzz("decode", function(b: buffer)
return Sgi.decode(Bytes.reader(b))
end, R.samples(Fx.cases), { mutations = 6 })
t:done()
+42
View File
@@ -0,0 +1,42 @@
-- image/sunras tests: files from netpbm / ImageMagick against ImageMagick's decode, our encoder's files read
-- by rasttopnm, round trips through every option, errors, the fuzz no-throw pass. Fixtures: tools/fixtures/sunras.py.
local T = require("@tests/T")
local Sunras = require("@formats/image/sunras")
local Bytes = require("@formats/core/Bytes")
local Err = require("@formats/core/Err")
local R = require("@tests/image/_raster")
local Fx = require("@tests/gen/sunras")
local t = T.suite("sunras")
t:eq(Sunras.name, "sunras", "name")
t:check(Sunras.caps.decode and Sunras.caps.encode, "caps")
t:note("%d files match rasttopnm / ImageMagick", R.decodes(t, Sunras, Fx.cases))
-- our files: rasttopnm reads the colours (it drops alpha)
for _, o in Fx.ours do
local img = Sunras.decode(Bytes.reader(buffer.fromstring(o.data))) :: any
local px = buffer.fromstring(buffer.tostring(img.pixels))
for i = 0, o.w * o.h - 1 do
buffer.writeu8(px, i * 4 + 3, 255)
end
t:eq(R.maxDiff(px, buffer.fromstring(o.rgba), o.w * o.h), 0, o.name .. ": rasttopnm reads our file")
end
for _, c in Fx.cases do
R.roundTrip(t, Sunras, R.image(c.w, c.h, c.rgba), nil, c.name .. " re-encoded")
R.roundTrip(t, Sunras, R.image(c.w, c.h, c.rgba), { rle = false }, c.name .. " re-encoded without RLE")
end
do
local enc = Sunras.encode(R.image(2, 2, string.rep("\1\2\3\4", 4))) :: buffer
t:eq(Sunras.sniff(Bytes.reader(enc)), 1, "sniff")
t:eq(Sunras.sniff(Bytes.reader(buffer.fromstring("GIF89a"))), 0, "sniff other")
local _, e = Sunras.decode(Bytes.reader(buffer.fromstring(string.sub(buffer.tostring(enc), 1, 20))))
t:check(e and e.code == Err.TRUNCATED, "short header")
local _, e2 = Sunras.decode(Bytes.reader(enc), { maxPixels = 2 })
t:check(e2 and e2.code == Err.LIMIT, "limit")
local _, e3 = Sunras.encode({} :: any)
t:check(e3 and e3.code == Err.ARG, "bad image")
end
t:fuzz("decode", function(b: buffer)
return Sunras.decode(Bytes.reader(b))
end, R.samples(Fx.cases), { mutations = 6 })
t:done()
+43
View File
@@ -0,0 +1,43 @@
-- image/xbm tests: files from netpbm / ImageMagick / Pillow and hand-written ones against the reference decode
-- (xbmtopbm), our encoder's files read back by it, round trips, errors, the fuzz no-throw pass.
-- Fixtures: tools/fixtures/xbm.py.
local T = require("@tests/T")
local Xbm = require("@formats/image/xbm")
local Bytes = require("@formats/core/Bytes")
local Err = require("@formats/core/Err")
local R = require("@tests/image/_raster")
local Fx = require("@tests/gen/xbm")
local t = T.suite("xbm")
t:eq(Xbm.name, "xbm", "name")
t:check(Xbm.caps.decode and Xbm.caps.encode, "caps")
t:note("%d files match the reference", R.decodes(t, Xbm, Fx.cases))
t:note("%d of our files read back by it", R.decodes(t, Xbm, Fx.ours))
for _, c in Fx.cases do
R.roundTrip(t, Xbm, R.image(c.w, c.h, c.rgba), nil, c.name .. " re-encoded")
if c.hotspot then
local img = Xbm.decode(Bytes.reader(buffer.fromstring(c.data))) :: any
t:check(img.meta.hotspot and img.meta.hotspot.x == c.hotspot[1] and img.meta.hotspot.y == c.hotspot[2], c.name .. ": hotspot")
end
end
do
-- decimal and octal values, the X10 short arrays
local img = Xbm.decode(Bytes.reader(buffer.fromstring("#define a_width 9\n#define a_height 1\nstatic char a_bits[] = { 1, 0400 };"))) :: any
t:eq(buffer.readu32(img.pixels, 0), 0xFF000000, "decimal: pixel 0 black")
t:eq(buffer.readu32(img.pixels, 4), 0xFFFFFFFF, "decimal: pixel 1 white")
local x10 = Xbm.decode(Bytes.reader(buffer.fromstring("#define b_width 17\n#define b_height 1\nstatic short b_bits[] = { 0x8001, 0x0001 };"))) :: any
t:eq(buffer.readu32(x10.pixels, 15 * 4), 0xFF000000, "x10: bit 15")
t:eq(buffer.readu32(x10.pixels, 16 * 4), 0xFF000000, "x10: second short")
local tr = Xbm.decode(Bytes.reader(buffer.fromstring("#define c_width 2\n#define c_height 1\nstatic char c_bits[] = { 0x01 };")), { transparent = true }) :: any
t:eq(buffer.readu32(tr.pixels, 4), 0x00FFFFFF, "transparent clear bits")
t:eq(Xbm.sniff(Bytes.reader(buffer.fromstring("#define c_width 2\n"))), 0.9, "sniff")
local _, e = Xbm.decode(Bytes.reader(buffer.fromstring("static char x[] = {};")))
t:check(e and e.code == Err.CORRUPT, "no defines")
local named = Xbm.encode(R.image(1, 1, "\0\0\0\255"), { name = "1 bad.xbm" }) :: buffer
t:check(string.find(buffer.tostring(named), "#define _1_bad_width 1", 1, true) ~= nil, "name made an identifier")
end
t:fuzz("decode", function(b: buffer)
return Xbm.decode(Bytes.reader(b))
end, R.samples(Fx.cases), { mutations = 6 })
t:done()
+44
View File
@@ -0,0 +1,44 @@
-- image/xpm tests: files from netpbm / ImageMagick and hand-written ones against the reference decode
-- (ImageMagick), our encoder's files read back by it, round trips, errors, the fuzz no-throw pass.
-- Fixtures: tools/fixtures/xpm.py.
local T = require("@tests/T")
local Xpm = require("@formats/image/xpm")
local Bytes = require("@formats/core/Bytes")
local Err = require("@formats/core/Err")
local R = require("@tests/image/_raster")
local Fx = require("@tests/gen/xpm")
local t = T.suite("xpm")
t:eq(Xpm.name, "xpm", "name")
t:check(Xpm.caps.decode and Xpm.caps.encode, "caps")
t:note("%d files match the reference", R.decodes(t, Xpm, Fx.cases))
t:note("%d of our files read back by it", R.decodes(t, Xpm, Fx.ours))
for _, c in Fx.cases do
R.roundTrip(t, Xpm, R.image(c.w, c.h, c.rgba), nil, c.name .. " re-encoded")
if c.hotspot then
local img = Xpm.decode(Bytes.reader(buffer.fromstring(c.data))) :: any
t:check(img.meta.hotspot and img.meta.hotspot.x == c.hotspot[1] and img.meta.hotspot.y == c.hotspot[2], c.name .. ": hotspot")
end
end
do
local img = Xpm.decode(Bytes.reader(buffer.fromstring('/* XPM */\nstatic char *x[] = {"2 1 2 1 0 0", "a c gray50", "b c Unknownish", "ab"};'))) :: any
t:eq(buffer.readu32(img.pixels, 0), 0xFF7F7F7F, "gray50")
t:check(img.meta.unknownColours and img.meta.unknownColours[1] == "Unknownish", "unknown colour listed")
t:check(img.meta.hotspot and img.meta.hotspot.x == 0, "hotspot")
t:eq(Xpm.sniff(Bytes.reader(buffer.fromstring("/* XPM */\n"))), 1, "sniff")
local _, e = Xpm.decode(Bytes.reader(buffer.fromstring('/* XPM */ static char *x[] = {"2 1 5 1", "a c red"};')))
t:check(e and e.code == Err.TRUNCATED, "short colour table")
-- many colours: two chars per pixel
local big = R.image(300, 1, "")
big.pixels = buffer.create(1200)
for i = 0, 299 do
buffer.writeu32(big.pixels, i * 4, i * 997 + 0xFF000000)
end
local enc = R.roundTrip(t, Xpm, big, nil, "300 colours") :: buffer
t:check(string.find(buffer.tostring(enc), '"300 1 300 2"', 1, true) ~= nil, "two chars per pixel")
end
t:fuzz("decode", function(b: buffer)
return Xpm.decode(Bytes.reader(b))
end, R.samples(Fx.cases), { mutations = 6 })
t:done()
+124
View File
@@ -0,0 +1,124 @@
-- layered/ora tests: OpenRaster files assembled by the spec (offsets, opacity, hidden, composite-ops, nested isolate /
-- auto stacks, no merged image) against the attributes and pixels written there; our encoder's file taken apart by
-- python (stored mimetype first, stack.xml, Pillow's read of every PNG); round trips; hooks; errors; the fuzz no-throw
-- pass. Fixtures: tools/fixtures/ora.py.
local T = require("@tests/T")
local Ora = require("@formats/layered/ora")
local Png = require("@formats/image/png")
local Bytes = require("@formats/core/Bytes")
local Err = require("@formats/core/Err")
local Fx = require("@tests/gen/ora")
local t = T.suite("ora")
t:eq(Ora.name, "ora", "name")
t:eq(Ora.category, "layered", "category")
t:check(Ora.caps.decode and Ora.caps.encode, "caps")
local hooks = {
decodeImage = function(b: buffer): (any, any)
return Png.decode(Bytes.reader(b))
end,
encodeImage = function(img: any, _f: string): (any, any)
return Png.encode(img)
end,
}
-- the tree depth-first, bottom to top, groups before their children
local function walk(layers: { any }, depth: number, out: { any })
for _, l in layers do
table.insert(out, { layer = l, depth = depth })
if l.children then
walk(l.children, depth + 1, out)
end
end
return out
end
local function compare(label: string, doc: any, want: { any })
local got = walk(doc.layers, 0, {})
t:eq(#got, #want, label .. ": layer count")
for i, w in want do
local g = got[i]
if not g then
break
end
local l = g.layer
local ll = label .. ": " .. w.name
t:eq(l.name, w.name, ll .. " name")
t:eq(g.depth, w.depth, ll .. " depth")
t:check(math.abs(l.opacity - w.opacity) < 1e-6, ll .. " opacity")
t:eq(l.visible, w.visible, ll .. " visibility")
t:eq(l.blend, w.blend, ll .. " blend")
if not w.group then
t:check(l.x == w.x and l.y == w.y, ll .. " position")
t:check(l.image.width == w.w and l.image.height == w.h and buffer.tostring(l.image.pixels) == w.rgba, ll .. " pixels")
else
t:check(l.children ~= nil, ll .. " is a group")
end
end
end
for _, c in Fx.cases do
local doc, err = Ora.decode(Bytes.reader(buffer.fromstring(c.data)), hooks)
t:check(doc, c.name .. ": " .. tostring(err))
t:check(doc.kind == "layered" and doc.width == c.w and doc.height == c.h, c.name .. ": size")
compare(c.name, doc, c.layers)
t:eq(doc.meta.composite ~= nil, c.merged, c.name .. ": merged image")
if c.dpi then
t:eq(doc.meta.dpi.x, c.dpi, c.name .. ": dpi")
end
-- round trip
local enc, eerr = Ora.encode(doc, hooks)
t:check(enc, c.name .. ": encode " .. tostring(eerr))
local back = Ora.decode(Bytes.reader(enc :: buffer), hooks)
compare(c.name .. " round trip", back, c.layers)
t:eq(Ora.sniff(Bytes.reader(enc :: buffer)), 1, c.name .. ": sniff our file")
end
-- our encoder's file, as python and Pillow read it
do
local o = Fx.ours
t:check(o.ok, "ours: mimetype first, stored")
t:eq(o.entries[1], "mimetype", "ours: first entry")
t:check(table.find(o.entries, "mergedimage.png") ~= nil and table.find(o.entries, "Thumbnails/thumbnail.png") ~= nil, "ours: merged image and thumbnail")
t:check(o.thumb[1] == 40 and o.thumb[2] == 30, "ours: thumbnail size")
local doc = Ora.decode(Bytes.reader(buffer.fromstring(o.data)), hooks) :: any
t:check(doc and doc.width == o.w and doc.height == o.h, "ours: decodes")
-- python's walk is top first (xml order): compare by name
local byName = {}
for _, g in walk(doc.layers, 0, {}) do
byName[g.layer.name] = g
end
for _, l in o.layers do
local g = byName[l.name]
t:check(g, "ours: layer " .. l.name)
if g then
t:check(math.abs(g.layer.opacity - l.opacity) < 1e-6 and g.layer.visible == l.visible and g.depth == l.depth, "ours: " .. l.name .. " attributes")
if l.tag == "layer" then
t:check(g.layer.x == l.x and g.layer.y == l.y, "ours: " .. l.name .. " position")
t:eq(buffer.tostring(g.layer.image.pixels), l.rgba, "ours: " .. l.name .. " pixels through Pillow")
end
end
end
t:eq(buffer.tostring(doc.meta.composite.pixels), o.merged.rgba, "ours: merged image through Pillow")
end
do
local c = Fx.cases[1]
local _, e = Ora.decode(Bytes.reader(buffer.fromstring(c.data)))
t:check(e and e.code == Err.UNSUPPORTED, "no decodeImage hook")
local doc = Ora.decode(Bytes.reader(buffer.fromstring(c.data)), hooks) :: any
local _, e2 = Ora.encode(doc)
t:check(e2 and e2.code == Err.UNSUPPORTED, "no encodeImage hook")
local _, e3 = Ora.encode({} :: any, hooks)
t:check(e3 and e3.code == Err.ARG, "not a document")
t:eq(Ora.sniff(Bytes.reader(buffer.fromstring("PK\3\4"))), 0, "sniff a plain zip")
end
local samples = {}
for _, c in Fx.cases do
table.insert(samples, c.data)
end
t:fuzz("decode", function(b: buffer)
return Ora.decode(Bytes.reader(b), hooks)
end, samples, { mutations = 12 })
t:done()