-- 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 Big = require("@tests/gen/png_big16") local R = require("@tests/image/_raster") 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 -- maxSide / reduce: the box reduction of the full decode, for every colour type, depth and interlace local n = 0 local function cases(list: { any }) for _, c in list do if not c.corrupt then local full = decode(c.data) for _, k in { 2, 4, 8 } do local img, err = decode(c.data, { reduce = k }) local want = R.box(full, k) t:check(img and img.width == want.width and img.height == want.height, c.name .. " reduce " .. k .. ": " .. tostring(err)) t:check(img and buffer.tostring(img.pixels) == buffer.tostring(want.pixels), c.name .. " reduce " .. k .. ": pixels") n += 1 end local m = math.max(1, (math.max(c.width, c.height) + 2) // 3) local img = decode(c.data, { maxSide = m }) t:check(img and img.width <= m and img.height <= m and img.meta.source.reduce >= 1, c.name .. " maxSide " .. m) local k = img.meta.source.reduce t:check(k == 1 or (c.width + k // 2 - 1) // (k // 2) > m or (c.height + k // 2 - 1) // (k // 2) > m, c.name .. " maxSide: the smallest factor") t:check(buffer.tostring(img.pixels) == buffer.tostring((if k == 1 then full else R.box(full, k)).pixels), c.name .. " maxSide " .. m .. ": pixels") end end end cases(Suite) cases(Gen) t:note("reduce: %d decodes match the reference box reduction", n) local _, e = decode(Gen[1].data, { reduce = 3 }) t:check(e and e.code == Err.ARG, "reduce 3: " .. tostring(e)) local _, e2 = decode(Gen[1].data, { maxSide = 0 }) t:check(e2 and e2.code == Err.ARG, "maxSide 0: " .. tostring(e2)) local _, e3 = decode(Gen[1].data, { yield = 1 }) t:check(e3 and e3.code == Err.ARG, "yield not a function: " .. tostring(e3)) end do -- 2048 x 2048 16-bit RGB in 8 KB IDATs: full, maxSide 512, and a yielding decode inside a coroutine local b = buffer.fromstring(Big.data) local full, err = decode(b) t:check(full and full.width == 2048 and Crc32.of(full.pixels) == Big.crc, "16-bit 2048x2048: " .. tostring(err)) local small = decode(b, { maxSide = 512 }) t:check(small and small.width == 512 and small.height == 512 and small.meta.source.reduce == 4, "16-bit maxSide 512: size") t:check(small and buffer.tostring(small.pixels) == buffer.tostring(R.box(full, 4).pixels), "16-bit maxSide 512: pixels") local calls = 0 local co = coroutine.create(function(): any return decode(b, { maxSide = 512, yieldRows = 64, yield = function() calls += 1 coroutine.yield() end }) end) local ok, res = coroutine.resume(co) local resumes = 0 while ok and coroutine.status(co) ~= "dead" do resumes += 1 ok, res = coroutine.resume(co) end t:check(ok and res and buffer.tostring(res.pixels) == buffer.tostring(small.pixels), "yielding decode: same pixels") t:check(calls == 2048 // 64 and resumes == calls, string.format("yield: %d calls every 64 rows, %d resumes", calls, resumes)) t:note("16-bit 2048x2048 in %d IDATs: full and maxSide 512 match, %d yields", #Big.data // 8204, calls) end do -- yield on every PngSuite / generated image (plain and Adam7): called, and the pixels do not change local bad, total = 0, 0 for _, list in { Suite, Gen } do for _, c in list :: { any } do if not c.corrupt then local calls = 0 local img = decode(c.data, { yieldRows = 1, yield = function() calls += 1 end }) local k = 2 local red = decode(c.data, { reduce = k, yieldRows = 1, yield = function() calls += 1 end }) if not img or Crc32.of(img.pixels) ~= c.crc or calls < c.height or not red then bad += 1 end total += 1 end end end t:eq(bad, 0, "yield: every image decodes the same with a yield hook") t:note("yield: %d images decode the same with a hook on every row", total) end do local samples = {} for i, c in Suite do 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 }) t:fuzz("decode maxSide + yield", function(b: buffer) return Png.decode(Bytes.reader(b), { maxSide = 5, yieldRows = 1, yield = function() end }) end, samples, { mutations = 4, keepHeader = 8 }) end t:done()