merge master (ws/tex, ws/3d3, ws/video2, ...)
This commit is contained in:
commit
43df95f6e0
200 files changed
+45540
-1559
No files matched your search
@@ -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
|
||||
@@ -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()
|
||||
@@ -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()
|
||||
@@ -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()
|
||||
@@ -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()
|
||||
@@ -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()
|
||||
@@ -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()
|
||||
@@ -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()
|
||||
@@ -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()
|
||||
@@ -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()
|
||||
@@ -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()
|
||||
@@ -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()
|
||||
Reference in new issue
Block a user