merge ws/imgA: jpeg gif tiff tga qoi pnm ico hdr, codecs Dct/Lzw/Quantize, ops/ImageOps, api image hooks
This commit is contained in:
commit
2db77a7db8
55 files changed
+13401
-2
No files matched your search
@@ -0,0 +1,158 @@
|
||||
-- codecs/Dct tests: the islow inverse DCT against a double-precision reference (within 1 level per sample, as T.81
|
||||
-- annex A accuracy needs), forward DCT + quantiser against the reference transform (round to nearest of v / (8 q)),
|
||||
-- fdct -> idct round trips at q = 1 within 1 level, edge replication of fdctQuantize, and garbage coefficients never
|
||||
-- throw (outputs stay clamped). The bit-exact djpeg / cjpeg checks are in tests/image/jpeg.luau.
|
||||
local T = require("@tests/T")
|
||||
local Dct = require("@formats/codecs/Dct")
|
||||
|
||||
local t = T.suite("dct")
|
||||
local rand = T.rng(11)
|
||||
|
||||
local C = {}
|
||||
for u = 0, 7 do
|
||||
C[u] = if u == 0 then math.sqrt(0.5) else 1
|
||||
end
|
||||
local cosT = {}
|
||||
for x = 0, 7 do
|
||||
for u = 0, 7 do
|
||||
cosT[x * 8 + u] = math.cos((2 * x + 1) * u * math.pi / 16)
|
||||
end
|
||||
end
|
||||
|
||||
local ones = buffer.create(128)
|
||||
for i = 0, 63 do
|
||||
buffer.writeu16(ones, i * 2, 1)
|
||||
end
|
||||
|
||||
-- inverse: random sparse coefficients, compare with the float IDCT
|
||||
do
|
||||
local worst = 0
|
||||
for trial = 1, 300 do
|
||||
local coef = buffer.create(128)
|
||||
local F = {}
|
||||
for i = 0, 63 do
|
||||
local v = 0
|
||||
if rand(4) == 0 or i == 0 then
|
||||
v = rand(512) - 256
|
||||
if i == 0 then
|
||||
v = rand(2048) - 1024
|
||||
end
|
||||
end
|
||||
F[i] = v
|
||||
buffer.writei16(coef, i * 2, v)
|
||||
end
|
||||
local plane = buffer.create(128)
|
||||
Dct.idctBlock(coef, 0, ones, plane, 0, 8, 2, 128, 255)
|
||||
for y = 0, 7 do
|
||||
for x = 0, 7 do
|
||||
local s = 0
|
||||
for v = 0, 7 do
|
||||
for u = 0, 7 do
|
||||
s += C[u] * C[v] * F[v * 8 + u] * cosT[x * 8 + u] * cosT[y * 8 + v]
|
||||
end
|
||||
end
|
||||
local want = math.clamp(math.floor(s / 4 + 128 + 0.5), 0, 255)
|
||||
local got = buffer.readu16(plane, (y * 8 + x) * 2)
|
||||
worst = math.max(worst, math.abs(got - want))
|
||||
end
|
||||
end
|
||||
local _ = trial
|
||||
end
|
||||
t:check(worst <= 1, "idct within 1 of the float reference: " .. worst)
|
||||
t:note("idct: worst error %d over 300 random blocks", worst)
|
||||
end
|
||||
|
||||
-- forward + quantiser
|
||||
do
|
||||
local worst = 0
|
||||
for _ = 1, 200 do
|
||||
local src = buffer.create(64)
|
||||
for i = 0, 63 do
|
||||
buffer.writeu8(src, i, rand(256))
|
||||
end
|
||||
local q = buffer.create(128)
|
||||
for i = 0, 63 do
|
||||
buffer.writeu16(q, i * 2, 1 + rand(40))
|
||||
end
|
||||
local coef = buffer.create(128)
|
||||
Dct.fdctQuantize(src, 8, 0, 0, 8, 8, q, coef, 0)
|
||||
for v = 0, 7 do
|
||||
for u = 0, 7 do
|
||||
local s = 0
|
||||
for y = 0, 7 do
|
||||
for x = 0, 7 do
|
||||
s += (buffer.readu8(src, y * 8 + x) - 128) * cosT[x * 8 + u] * cosT[y * 8 + v]
|
||||
end
|
||||
end
|
||||
s *= C[u] * C[v] / 4
|
||||
local want = s / buffer.readu16(q, (v * 8 + u) * 2)
|
||||
worst = math.max(worst, math.abs(buffer.readi16(coef, (v * 8 + u) * 2) - want))
|
||||
end
|
||||
end
|
||||
end
|
||||
t:check(worst <= 0.75, "fdct + quantise within rounding of the float reference: " .. worst)
|
||||
-- round trip at q = 1
|
||||
local rt = 0
|
||||
for _ = 1, 100 do
|
||||
local src = buffer.create(64)
|
||||
for i = 0, 63 do
|
||||
buffer.writeu8(src, i, rand(256))
|
||||
end
|
||||
local coef = buffer.create(128)
|
||||
Dct.fdctQuantize(src, 8, 0, 0, 8, 8, ones, coef, 0)
|
||||
local plane = buffer.create(128)
|
||||
Dct.idctBlock(coef, 0, ones, plane, 0, 8, 2, 128, 255)
|
||||
for i = 0, 63 do
|
||||
rt = math.max(rt, math.abs(buffer.readu16(plane, i * 2) - buffer.readu8(src, i)))
|
||||
end
|
||||
end
|
||||
t:check(rt <= 1, "fdct -> idct round trip at q = 1: " .. rt)
|
||||
-- edge replication: a 3x2 image is the same as its 8x8 edge-extended version
|
||||
local small = T.bytes({ 10, 200, 30, 40, 50, 250 })
|
||||
local ext = buffer.create(64)
|
||||
for y = 0, 7 do
|
||||
for x = 0, 7 do
|
||||
buffer.writeu8(ext, y * 8 + x, buffer.readu8(small, math.min(y, 1) * 3 + math.min(x, 2)))
|
||||
end
|
||||
end
|
||||
local a, b = buffer.create(128), buffer.create(128)
|
||||
Dct.fdctQuantize(small, 3, 0, 0, 3, 2, ones, a, 0)
|
||||
Dct.fdctQuantize(ext, 8, 0, 0, 8, 8, ones, b, 0)
|
||||
t:check(buffer.tostring(a) == buffer.tostring(b), "edge replication")
|
||||
end
|
||||
|
||||
-- components and garbage: clamped output, no throw
|
||||
do
|
||||
local coef = buffer.create(4 * 3 * 128)
|
||||
for i = 0, buffer.len(coef) // 2 - 1 do
|
||||
buffer.writei16(coef, i * 2, rand(65536) - 32768)
|
||||
end
|
||||
local q = buffer.create(128)
|
||||
for i = 0, 63 do
|
||||
buffer.writeu16(q, i * 2, rand(65536))
|
||||
end
|
||||
for _, size in { 8, 4, 2, 1 } do
|
||||
for _, prec in { { 2, 128, 255 }, { 1, 2048, 4095 } } do
|
||||
local plane = buffer.create(4 * size * 3 * size * 2)
|
||||
local ok = pcall(Dct.idctComponent, coef, q, 4, 3, size, plane, prec[1], prec[2], prec[3])
|
||||
t:check(ok, "idctComponent size " .. size .. " on garbage")
|
||||
local inRange = true
|
||||
for i = 0, buffer.len(plane) // 2 - 1 do
|
||||
if buffer.readu16(plane, i * 2) > prec[3] then
|
||||
inRange = false
|
||||
end
|
||||
end
|
||||
t:check(inRange, "clamped output, size " .. size)
|
||||
end
|
||||
end
|
||||
local samples = { string.rep("\0", 128) }
|
||||
t:fuzz("idctBlock", function(b: buffer)
|
||||
local cb = buffer.create(128)
|
||||
buffer.copy(cb, 0, b, 0, math.min(128, buffer.len(b)))
|
||||
local plane = buffer.create(128)
|
||||
Dct.idctBlock(cb, 0, q, plane, 0, 8, 2, 128, 255)
|
||||
return plane
|
||||
end, samples, { garbage = 50 })
|
||||
end
|
||||
|
||||
t:done()
|
||||
@@ -0,0 +1,119 @@
|
||||
-- codecs/Lzw tests: GIF (LSB) and TIFF (MSB, early change) code streams from Pillow / libtiff decode to their data;
|
||||
-- our encoder round-trips random and structured data at every GIF minimum code size and through table resets; the
|
||||
-- output limit, truncated and corrupt streams; the fuzz no-throw pass. Fixtures: tools/fixtures/lzw.py.
|
||||
local T = require("@tests/T")
|
||||
local Lzw = require("@formats/codecs/Lzw")
|
||||
local Fx = require("@tests/gen/lzw")
|
||||
|
||||
local t = T.suite("lzw")
|
||||
|
||||
local function dec(codes: buffer, n: number, variant: string, min: number?): (string, number, string?)
|
||||
local out = buffer.create(n)
|
||||
local got, err = Lzw.decode(codes, 0, buffer.len(codes), out, 0, n, variant, min)
|
||||
return buffer.tostring(out), got, err
|
||||
end
|
||||
|
||||
-- reference streams
|
||||
for _, c in Fx.gif do
|
||||
local s, n, err = dec(buffer.fromstring(c.codes), #c.data, "gif", c.min)
|
||||
t:check(err == nil, "gif " .. c.name .. ": " .. tostring(err))
|
||||
t:eq(n, #c.data, "gif " .. c.name .. ": length")
|
||||
t:check(s == c.data, "gif " .. c.name .. ": data")
|
||||
end
|
||||
for _, c in Fx.tiff do
|
||||
local s, n, err = dec(buffer.fromstring(c.codes), #c.data, "tiff")
|
||||
t:check(err == nil, "tiff " .. c.name .. ": " .. tostring(err))
|
||||
t:eq(n, #c.data, "tiff " .. c.name .. ": length")
|
||||
t:check(s == c.data, "tiff " .. c.name .. ": data")
|
||||
end
|
||||
t:note("%d GIF and %d TIFF reference streams decode", #Fx.gif, #Fx.tiff)
|
||||
|
||||
-- round trips
|
||||
local rand = T.rng(3)
|
||||
local function data(n: number, kind: number, max: number): buffer
|
||||
local b = buffer.create(n)
|
||||
for i = 0, n - 1 do
|
||||
local v
|
||||
if kind == 0 then
|
||||
v = rand(max)
|
||||
elseif kind == 1 then
|
||||
v = (i // 7) % max
|
||||
else
|
||||
v = if rand(10) == 0 then rand(max) else (i % 13) % max
|
||||
end
|
||||
buffer.writeu8(b, i, v)
|
||||
end
|
||||
return b
|
||||
end
|
||||
local trips = 0
|
||||
for _, n in { 0, 1, 2, 3, 100, 5000, 70000 } do
|
||||
for kind = 0, 2 do
|
||||
for min = 2, 8 do
|
||||
local d = data(n, kind, 2 ^ min)
|
||||
local codes = Lzw.encode(d, 0, n, "gif", min)
|
||||
local s, got, err = dec(codes, n, "gif", min)
|
||||
t:check(err == nil and got == n and s == buffer.tostring(d), string.format("gif round trip n=%d kind=%d min=%d: %s", n, kind, min, tostring(err)))
|
||||
trips += 1
|
||||
end
|
||||
local d = data(n, kind, 256)
|
||||
local codes = Lzw.encode(d, 0, n, "tiff")
|
||||
local s, got, err = dec(codes, n, "tiff")
|
||||
t:check(err == nil and got == n and s == buffer.tostring(d), string.format("tiff round trip n=%d kind=%d: %s", n, kind, tostring(err)))
|
||||
trips += 1
|
||||
end
|
||||
end
|
||||
t:note("%d round trips", trips)
|
||||
|
||||
-- compression actually happens; a range inside a bigger buffer
|
||||
do
|
||||
local d = buffer.create(10000)
|
||||
local codes = Lzw.encode(d, 0, 10000, "tiff")
|
||||
t:check(buffer.len(codes) < 400, "zeros compress: " .. buffer.len(codes))
|
||||
local big = data(300, 1, 256)
|
||||
local codes2 = Lzw.encode(big, 100, 100, "gif", 8)
|
||||
local out = buffer.create(120)
|
||||
local n = Lzw.decode(codes2, 0, buffer.len(codes2), out, 10, 100, "gif", 8)
|
||||
t:eq(n, 100, "sub-range length")
|
||||
t:check(buffer.readstring(out, 10, 100) == buffer.readstring(big, 100, 100), "sub-range data")
|
||||
end
|
||||
|
||||
-- limits, truncation, corruption
|
||||
do
|
||||
local d = data(1000, 2, 16)
|
||||
local codes = Lzw.encode(d, 0, 1000, "gif", 4)
|
||||
local s, n, err = dec(codes, 500, "gif", 4)
|
||||
t:check(n == 500 and err == nil and s == buffer.readstring(d, 0, 500), "output limit")
|
||||
local cut = buffer.create(buffer.len(codes) // 2)
|
||||
buffer.copy(cut, 0, codes, 0, buffer.len(cut))
|
||||
local _, n2, err2 = dec(cut, 1000, "gif", 4)
|
||||
t:check(err2 == "truncated" and n2 > 0 and n2 < 1000, "truncated: " .. tostring(err2))
|
||||
local bad = buffer.fromstring("\255\255\255\255")
|
||||
local _, _, err3 = dec(bad, 100, "gif", 2)
|
||||
t:check(err3 == "corrupt", "corrupt: " .. tostring(err3))
|
||||
local _, _, err4 = dec(codes, 100, "gif", 0)
|
||||
t:check(err4 == "corrupt", "bad min code size")
|
||||
end
|
||||
|
||||
-- fuzz
|
||||
do
|
||||
local samples = {}
|
||||
for i, c in Fx.gif do
|
||||
if i % 2 == 1 then
|
||||
table.insert(samples, c.codes)
|
||||
end
|
||||
end
|
||||
for i, c in Fx.tiff do
|
||||
if i % 2 == 0 then
|
||||
table.insert(samples, c.codes)
|
||||
end
|
||||
end
|
||||
local out = buffer.create(100000)
|
||||
t:fuzz("decode gif", function(b: buffer)
|
||||
return Lzw.decode(b, 0, buffer.len(b), out, 0, 100000, "gif", 8)
|
||||
end, samples)
|
||||
t:fuzz("decode tiff", function(b: buffer)
|
||||
return Lzw.decode(b, 0, buffer.len(b), out, 0, 100000, "tiff")
|
||||
end, samples)
|
||||
end
|
||||
|
||||
t:done()
|
||||
@@ -0,0 +1,135 @@
|
||||
-- codecs/Quantize tests: palette size bounds for every method and colour budget, the exact (lossless) palette at
|
||||
-- <= maxColors colours, transparency handling, PSNR of quantized images above thresholds, determinism of both
|
||||
-- methods and of Floyd-Steinberg dithering (the same CRC on both runtimes), bad arguments, and a no-throw pass.
|
||||
local T = require("@tests/T")
|
||||
local Quantize = require("@formats/codecs/Quantize")
|
||||
local Crc32 = require("@formats/core/Crc32")
|
||||
|
||||
local t = T.suite("quantize")
|
||||
local rand = T.rng(5)
|
||||
|
||||
local function photo(w: number, h: number): buffer
|
||||
local px = buffer.create(w * h * 4)
|
||||
for i = 0, w * h - 1 do
|
||||
local x, y = i % w, i // w
|
||||
local r = (x * 255) // (w - 1)
|
||||
local g = (y * 255) // (h - 1)
|
||||
local b = math.floor(128 + 100 * math.sin(x * 0.11) * math.cos(y * 0.07)) + rand(9)
|
||||
buffer.writeu32(px, i * 4, r + g * 256 + math.clamp(b, 0, 255) * 65536 + 0xFF000000)
|
||||
end
|
||||
return px
|
||||
end
|
||||
|
||||
local function render(idx: buffer, pal: { { number } }, n: number): buffer
|
||||
local out = buffer.create(n * 4)
|
||||
for i = 0, n - 1 do
|
||||
local c = pal[buffer.readu8(idx, i) + 1]
|
||||
buffer.writeu32(out, i * 4, c[1] + c[2] * 256 + c[3] * 65536 + 0xFF000000)
|
||||
end
|
||||
return out
|
||||
end
|
||||
|
||||
local function psnr(a: buffer, b: buffer): number
|
||||
local se, n = 0, 0
|
||||
for o = 0, buffer.len(a) - 4, 4 do
|
||||
for k = 0, 2 do
|
||||
local d = buffer.readu8(a, o + k) - buffer.readu8(b, o + k)
|
||||
se += d * d
|
||||
n += 1
|
||||
end
|
||||
end
|
||||
return if se == 0 then 99 else 10 * math.log10(255 * 255 * n / se)
|
||||
end
|
||||
|
||||
local W, H = 128, 96
|
||||
local px = photo(W, H)
|
||||
-- palette bounds and quality
|
||||
local MIN_PSNR = { [256] = 30, [64] = 24, [16] = 17, [2] = 10 }
|
||||
for _, method in { "mediancut", "octree" } do
|
||||
for _, k in { 2, 16, 64, 256 } do
|
||||
local pal, exact = Quantize.palette(px, W, H, k, { method = method })
|
||||
t:check(#pal >= 1 and #pal <= k, string.format("%s %d: palette size %d", method, k, #pal))
|
||||
t:check(not exact, method .. ": not exact")
|
||||
for _, c in pal do
|
||||
t:check(#c == 4 and c[4] == 255 and c[1] % 1 == 0 and c[1] >= 0 and c[1] <= 255, "palette entries")
|
||||
end
|
||||
for _, dither in { false, true } do
|
||||
local idx = Quantize.map(px, W, H, pal, { dither = dither })
|
||||
t:eq(buffer.len(idx), W * H, "index buffer size")
|
||||
local p = psnr(render(idx, pal, W * H), px)
|
||||
local want = MIN_PSNR[k] - (if dither then 4 else 0)
|
||||
t:check(p > want, string.format("%s %d dither=%s: PSNR %.1f > %d", method, k, tostring(dither), p, want))
|
||||
if k == 256 or k == 16 then
|
||||
t:note("%-9s %3d colours dither=%-5s PSNR %.1f dB, crc %08x", method, k, tostring(dither), p, Crc32.of(idx))
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
-- exact palettes: lossless
|
||||
do
|
||||
local n = 50 * 40
|
||||
local b = buffer.create(n * 4)
|
||||
for i = 0, n - 1 do
|
||||
local k = (i * 7 + i // 50) % 200
|
||||
buffer.writeu32(b, i * 4, (k * 3) % 256 + ((k * 11) % 256) * 256 + k * 65536 + 0xFF000000)
|
||||
end
|
||||
local pal, exact = Quantize.palette(b, 50, 40, 256)
|
||||
t:check(exact and #pal == 200, "exact palette of 200 colours: " .. #pal)
|
||||
local idx = Quantize.map(b, 50, 40, pal, { dither = true })
|
||||
t:check(buffer.tostring(render(idx, pal, n)) == buffer.tostring(b), "exact palette maps losslessly (dither too)")
|
||||
local pal2, exact2 = Quantize.palette(b, 50, 40, 199)
|
||||
t:check(not exact2 and #pal2 <= 199, "199 colours is not exact")
|
||||
local i3, p3, e3 = Quantize.quantize(b, 50, 40, 256, { dither = true })
|
||||
t:check(e3 and #p3 == 200 and buffer.tostring(render(i3, p3, n)) == buffer.tostring(b), "quantize() exact")
|
||||
end
|
||||
|
||||
-- transparency
|
||||
do
|
||||
local b = buffer.create(16 * 4)
|
||||
for i = 0, 15 do
|
||||
buffer.writeu32(b, i * 4, if i % 3 == 0 then 0x10FFFFFF else 0xFF0000FF + i * 256)
|
||||
end
|
||||
local pal, exact = Quantize.palette(b, 4, 4, 255)
|
||||
t:check(exact and #pal == 10, "transparent pixels are not in the palette")
|
||||
table.insert(pal, { 0, 0, 0, 0 })
|
||||
local idx = Quantize.map(b, 4, 4, pal, { transparentIndex = #pal - 1, dither = true })
|
||||
for i = 0, 15 do
|
||||
local v = buffer.readu8(idx, i)
|
||||
t:check((i % 3 == 0) == (v == #pal - 1), "transparent index " .. i)
|
||||
end
|
||||
end
|
||||
|
||||
-- determinism (also across runtimes: these CRCs are pinned)
|
||||
do
|
||||
local pal = Quantize.palette(px, W, H, 64, { method = "mediancut" })
|
||||
local a = Crc32.of(Quantize.map(px, W, H, pal, { dither = true }))
|
||||
local pal2 = Quantize.palette(px, W, H, 64, { method = "mediancut" })
|
||||
t:eq(Crc32.of(Quantize.map(px, W, H, pal2, { dither = true })), a, "mediancut + dither deterministic")
|
||||
t:eq(a, 0x50c4e466, "mediancut 64 + dither: the pinned CRC (the same on both runtimes)")
|
||||
local o1 = Quantize.palette(px, W, H, 37, { method = "octree" })
|
||||
local o2 = Quantize.palette(px, W, H, 37, { method = "octree" })
|
||||
t:check(#o1 == #o2 and o1[#o1][1] == o2[#o2][1], "octree deterministic")
|
||||
end
|
||||
|
||||
-- bad arguments never throw
|
||||
do
|
||||
t:eq(#Quantize.palette(nil :: any, 1, 1, 4), 0, "nil pixels")
|
||||
t:eq(#Quantize.palette(buffer.create(3), 1, 1, 4), 0, "short buffer")
|
||||
t:eq(buffer.len(Quantize.map(px, W, H, {})), W * H, "empty palette")
|
||||
t:eq(buffer.len(Quantize.map(buffer.create(1), 2, 2, { { 1, 2, 3, 255 } })), 0, "short buffer map")
|
||||
local ok = pcall(function()
|
||||
for i = 1, 20 do
|
||||
local w, h = 1 + rand(20), 1 + rand(20)
|
||||
local b = buffer.create(w * h * 4)
|
||||
for k = 0, w * h * 4 - 1 do
|
||||
buffer.writeu8(b, k, rand(256))
|
||||
end
|
||||
local pal = Quantize.palette(b, w, h, 1 + rand(256), { method = if i % 2 == 0 then "octree" else "mediancut" })
|
||||
Quantize.map(b, w, h, pal, { dither = i % 3 == 0, transparentIndex = if i % 4 == 0 then 0 else nil })
|
||||
end
|
||||
end)
|
||||
t:check(ok, "random images never throw")
|
||||
end
|
||||
|
||||
t:done()
|
||||
Reference in new issue
Block a user