-- shared checks of the simple raster formats' tests: decode against reference rgba8, round trips, fuzz samples. local Bytes = require("@rosetta/core/Bytes") local Types = require("@rosetta/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 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 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 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 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 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 -- the reference k x k box reduction of an rgba8 Image (the PNG decoder's maxSide / reduce): the alpha-weighted mean, -- halves up, a fully transparent block is 0, 0, 0, 0 function R.box(img: Types.Image, k: number): Types.Image local w, h, px = img.width, img.height, img.pixels local ow, oh = (w + k - 1) // k, (h + k - 1) // k local out = Types.Image.new(ow, oh) for oy = 0, oh - 1 do for ox = 0, ow - 1 do local r, g, b, a, n = 0, 0, 0, 0, 0 for y = oy * k, math.min(oy * k + k, h) - 1 do for x = ox * k, math.min(ox * k + k, w) - 1 do local o = (y * w + x) * 4 local al = buffer.readu8(px, o + 3) r += buffer.readu8(px, o) * al g += buffer.readu8(px, o + 1) * al b += buffer.readu8(px, o + 2) * al a += al n += 1 end end local o = (oy * ow + ox) * 4 if a > 0 then buffer.writeu8(out.pixels, o, (r + a // 2) // a) buffer.writeu8(out.pixels, o + 1, (g + a // 2) // a) buffer.writeu8(out.pixels, o + 2, (b + a // 2) // a) buffer.writeu8(out.pixels, o + 3, (a + n // 2) // n) end end end return out end return R