-- image/png tests: the PngSuite against pypng (tools/fixtures/_png_reference.py). Fixtures: tools/fixtures/png.py. local T = require("@tests/T") local Png = require("@rosetta/image/png") local Bytes = require("@rosetta/core/Bytes") local Crc32 = require("@rosetta/core/Crc32") local Err = require("@rosetta/core/Err") local Types = require("@rosetta/Types") local Suite = require("@tests/gen/png_suite") local Gen = require("@tests/gen/png_gen") local t = T.suite("png") local function decode(data: any, opts: any?): (any, any) local b = if type(data) == "string" then buffer.fromstring(data) else data return Png.decode(Bytes.reader(b), opts) end t:eq(Png.name, "png", "name") t:eq(Png.category, "image", "category") t:check(table.find(Png.extensions, "png") ~= nil, "extensions") t:check(Png.caps.decode and Png.caps.encode, "caps") local function verify(c: any, label: string) local img, err = decode(c.data, { checkCrc = true }) t:check(img, label .. ": " .. tostring(err)) t:check(img.kind == "image" and img.format == "rgba8", label .. ": a standard rgba8 Image") t:check(img.width == c.width and img.height == c.height, label .. ": size") t:eq(buffer.len(img.pixels), c.width * c.height * 4, label .. ": pixel buffer size") local crc = Crc32.of(img.pixels) t:check(crc == c.crc, string.format("%s: pixels crc %08x, expected %08x", label, crc, c.crc)) local img2 = decode(c.data) t:check(img2 and Crc32.of(img2.pixels) == c.crc, label .. ": no-CRC decode") local big = buffer.create(#c.data + 10) buffer.writestring(big, 7, c.data) local img3 = Png.decode(Bytes.reader(big, 7, #c.data)) t:check(img3 and Crc32.of(img3.pixels) == c.crc, label .. ": decode from a sub-range") end local good, corrupt = 0, 0 for _, c in Suite do if c.corrupt then local ok, img, err = pcall(decode, c.data, { checkCrc = true }) t:check(ok, c.name .. ": threw " .. tostring(img)) t:check(img == nil and Err.is(err), c.name .. ": corrupt file decoded") corrupt += 1 else verify(c, c.name) good += 1 end end t:note("PngSuite: %d images match the reference, %d corrupt files rejected", good, corrupt) for _, c in Gen do verify(c, c.name) end t:note("%d generated images match", #Gen) do local c for _, s in Suite do if s.name == "basi3p04.png" then c = s end end t:eq(Png.sniff(Bytes.reader(buffer.fromstring(c.data))), 1, "sniff a PNG") t:eq(Png.sniff(Bytes.reader(buffer.fromstring("GIF89a......"))), 0, "sniff a GIF") t:eq(Png.sniff(Bytes.reader(buffer.create(0))), 0, "sniff nothing") local r = Bytes.reader(buffer.fromstring(c.data)) local info = Png.info(r) t:check(info and info.width == 32 and info.height == 32 and info.bitDepth == 4 and info.colorType == 3 and info.interlace == 1, "info") t:eq(r:tell(), 0, "info does not move the reader") local img = decode(c.data) t:check(img.meta.palette and #img.meta.palette > 0 and #img.meta.palette[1] == 4, "palette table") t:check(img.meta.source.format == "png" and img.meta.source.bitDepth == 4 and img.meta.source.colorType == "palette", "meta.source") local _, e = decode(c.data, { maxPixels = 100 }) t:check(e and e.code == Err.LIMIT, "maxPixels: " .. tostring(e)) local _, e2 = decode("nope") t:check(e2 and e2.code == Err.CORRUPT, "junk: " .. tostring(e2)) end do local rand = T.rng(99) local function image(w: number, h: number, kind: string): Types.Image local img = Types.Image.new(w, h) for i = 0, w * h - 1 do local o = i * 4 local v = (i * 7 + (i // w) * 3) % 256 local r, g, b, a = v, (v * 3) % 256, rand(256), rand(256) if kind == "gray" or kind == "graya" then g, b = r, r end if kind == "gray" or kind == "rgb" then a = 255 end buffer.writeu8(img.pixels, o, r) buffer.writeu8(img.pixels, o + 1, g) buffer.writeu8(img.pixels, o + 2, b) buffer.writeu8(img.pixels, o + 3, a) end return img end local CT = { rgba = 6, rgb = 2, gray = 0, graya = 4 } for _, kind in { "rgba", "rgb", "gray", "graya" } do for _, size in { { 1, 1 }, { 3, 2 }, { 37, 29 }, { 200, 3 } } do local img = image(size[1], size[2], kind) for _, level in { 0, 1, 6, 9 } do local png, err = Png.encode(img, { level = level }) t:check(png, kind .. ": encode: " .. tostring(err)) local back, berr = Png.decode(Bytes.reader(png :: buffer), { checkCrc = true }) t:check(back, kind .. ": decode our PNG: " .. tostring(berr)) t:check(buffer.tostring(back.pixels) == buffer.tostring(img.pixels), string.format("%s %dx%d level %d: round trip", kind, size[1], size[2], level)) t:eq(back.meta.source.colorType, kind, kind .. ": auto colour type") t:eq(Png.info(Bytes.reader(png :: buffer)).colorType, CT[kind], kind .. ": IHDR colour type") end end end local img = image(16, 16, "rgba") img.meta.text = { Comment = "hello", Software = "rosetta" } local png = Png.encode(img, { colorType = "rgba" }) :: buffer local back = Png.decode(Bytes.reader(png)) t:check(back.meta.text and back.meta.text.Comment == "hello" and back.meta.text.Software == "rosetta", "tEXt round trip") local _, e = Png.encode({} :: any) t:check(e and e.code == Err.ARG, "encode of a non-image returns Err.ARG") local _, e2 = Png.encode(img, { colorType = "cmyk" } :: any) t:check(e2 and e2.code == Err.ARG, "unknown colorType returns Err.ARG") end do local samples = {} for i, c in Suite do if i % 4 == 0 then table.insert(samples, c.data) end end for i, c in Gen do if i % 6 == 0 then table.insert(samples, c.data) end end t:fuzz("decode", function(b: buffer) return Png.decode(Bytes.reader(b)) end, samples, { mutations = 10, keepHeader = 8 }) t:fuzz("decode checkCrc", function(b: buffer) return Png.decode(Bytes.reader(b), { checkCrc = true }) end, samples, { mutations = 2, garbage = 50 }) end t:done()