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()
|
||||
@@ -0,0 +1,221 @@
|
||||
-- image/gif tests: GIFs from ImageMagick and Pillow (static, interlaced, 2..256 colours, grey, transparency,
|
||||
-- animations with every disposal, sub-rectangles, local colour tables, loop counts, comments) against ImageMagick's
|
||||
-- decode of every frame; the encoder (lossless round trips at <= 256 colours, quantized PSNR above, animations with
|
||||
-- frames / delays / disposal / transparency / loops, options); error codes; the fuzz no-throw pass.
|
||||
-- Fixtures: tools/fixtures/gif.py.
|
||||
local T = require("@tests/T")
|
||||
local Gif = require("@formats/image/gif")
|
||||
local Bytes = require("@formats/core/Bytes")
|
||||
local Crc32 = require("@formats/core/Crc32")
|
||||
local Err = require("@formats/core/Err")
|
||||
local Types = require("@formats/Types")
|
||||
local Fx = require("@tests/gen/gif")
|
||||
|
||||
local t = T.suite("gif")
|
||||
|
||||
local function decode(data: any, opts: any?): (any, any)
|
||||
local b = if type(data) == "string" then buffer.fromstring(data) else data
|
||||
return Gif.decode(Bytes.reader(b), opts)
|
||||
end
|
||||
|
||||
-- rgba8 with alpha-0 pixels zeroed (the fixtures' convention)
|
||||
local function norm(px: buffer): buffer
|
||||
local n = buffer.len(px)
|
||||
local c = buffer.create(n)
|
||||
buffer.copy(c, 0, px)
|
||||
for o = 0, n - 4, 4 do
|
||||
if buffer.readu8(c, o + 3) == 0 then
|
||||
buffer.writeu32(c, o, 0)
|
||||
end
|
||||
end
|
||||
return c
|
||||
end
|
||||
|
||||
-- the contract
|
||||
t:eq(Gif.name, "gif", "name")
|
||||
t:eq(Gif.category, "image", "category")
|
||||
t:check(Gif.caps.decode and Gif.caps.encode and Gif.caps.animation, "caps")
|
||||
|
||||
-- every fixture against ImageMagick
|
||||
local nframes = 0
|
||||
for _, c in Fx do
|
||||
local img, err = decode(c.data)
|
||||
t:check(img, c.name .. ": " .. tostring(err))
|
||||
t:check(img.width == c.width and img.height == c.height and img.format == "rgba8", c.name .. ": canvas")
|
||||
t:check(not img.meta.truncated, c.name .. ": flagged truncated")
|
||||
t:eq(Crc32.of(norm(img.pixels)), c.first, c.name .. ": first frame composited")
|
||||
local frames = img.frames or { { image = img, x = 0, y = 0, delay = 0, dispose = "none" } }
|
||||
t:eq(#frames, #c.frames, c.name .. ": frame count")
|
||||
for i, rf in c.frames do
|
||||
local f = frames[i]
|
||||
local label = c.name .. " frame " .. i
|
||||
if img.frames then
|
||||
t:check(f.image.width == rf.w and f.image.height == rf.h and f.x == rf.x and f.y == rf.y, label .. ": rectangle")
|
||||
t:eq(math.floor(f.delay * 100 + 0.5), rf.delay, label .. ": delay")
|
||||
local want = if rf.dispose == "undefined" then "none" else rf.dispose
|
||||
t:eq(f.dispose, want, label .. ": dispose")
|
||||
t:eq(Crc32.of(norm(f.image.pixels)), rf.crc, label .. ": pixels")
|
||||
else
|
||||
t:eq(Crc32.of(norm(img.pixels)), rf.crc, label .. ": pixels")
|
||||
end
|
||||
nframes += 1
|
||||
end
|
||||
if c.loops then
|
||||
t:eq(img.loops, if c.loops == 0 then 0 else c.loops + 1, c.name .. ": loops (plays)")
|
||||
end
|
||||
if c.comment then
|
||||
t:eq(img.meta.text and img.meta.text.comment, c.comment, c.name .. ": comment")
|
||||
end
|
||||
end
|
||||
t:note("%d files, %d frames match ImageMagick", #Fx, nframes)
|
||||
|
||||
-- sniff, meta, limits, errors
|
||||
do
|
||||
local c = Fx[1]
|
||||
t:eq(Gif.sniff(Bytes.reader(buffer.fromstring(c.data))), 1, "sniff a GIF")
|
||||
t:eq(Gif.sniff(Bytes.reader(buffer.fromstring("\137PNG\r\n\26\n"))), 0, "sniff a PNG")
|
||||
t:eq(Gif.sniff(Bytes.reader(buffer.create(0))), 0, "sniff nothing")
|
||||
local img = decode(c.data)
|
||||
t:check(img.meta.source.format == "gif" and img.meta.source.version == "89a", "meta.source")
|
||||
t:check(img.meta.palette and #img.meta.palette >= 2 and #img.meta.palette[1] == 4, "meta.palette")
|
||||
local _, e = decode(c.data, { maxPixels = 100 })
|
||||
t:check(e and e.code == Err.LIMIT, "maxPixels: " .. tostring(e))
|
||||
local _, e2 = decode("GIF89a")
|
||||
t:check(e2 and e2.code == Err.TRUNCATED, "header only: " .. tostring(e2))
|
||||
local _, e3 = decode("NOTGIF1234567")
|
||||
t:check(e3 and e3.code == Err.CORRUPT, "junk: " .. tostring(e3))
|
||||
-- a truncated file keeps its frames
|
||||
for _, f in Fx do
|
||||
if f.name == "anim_none" then
|
||||
local cut = string.sub(f.data, 1, #f.data - 20)
|
||||
local part = decode(cut)
|
||||
t:check(part and part.meta.truncated, "truncated animation decodes partially")
|
||||
local one = decode(f.data, { maxFrames = 1 })
|
||||
t:check(one and one.frames == nil, "maxFrames = 1")
|
||||
local big = buffer.create(#f.data + 9)
|
||||
buffer.writestring(big, 5, f.data)
|
||||
local sub = Gif.decode(Bytes.reader(big, 5, #f.data))
|
||||
t:check(sub and sub.frames and #sub.frames == 4, "decode from a sub-range")
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
-- the encoder
|
||||
local rand = T.rng(17)
|
||||
local function image(w: number, h: number, colors: number?, alpha: boolean?): Types.Image
|
||||
local img = Types.Image.new(w, h)
|
||||
for i = 0, w * h - 1 do
|
||||
local x, y = i % w, i // w
|
||||
local r, g, b
|
||||
if colors then
|
||||
local k = (x // 3 + y // 2 * 7 + rand(2)) % colors
|
||||
r, g, b = (k * 53) % 256, (k * 97) % 256, (k * 31) % 256
|
||||
else
|
||||
r, g, b = (x * 255) // math.max(1, w - 1), (y * 255) // math.max(1, h - 1), ((x + y) * 3 + rand(16)) % 256
|
||||
end
|
||||
local a = if alpha and (x * x + y * y) % 11 == 0 then 0 else 255
|
||||
buffer.writeu32(img.pixels, i * 4, r + g * 256 + b * 65536 + a * 16777216)
|
||||
end
|
||||
return img
|
||||
end
|
||||
local function same(a: buffer, b: buffer): boolean
|
||||
return buffer.tostring(norm(a)) == buffer.tostring(norm(b))
|
||||
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
|
||||
if se == 0 then
|
||||
return 99
|
||||
end
|
||||
return 10 * math.log10(255 * 255 * n / se)
|
||||
end
|
||||
do
|
||||
for _, colors in { 2, 3, 16, 200, 256 } do
|
||||
for _, alpha in { false, true } do
|
||||
local ncol = if alpha and colors == 256 then 255 else colors
|
||||
local img = image(37, 23, ncol, alpha)
|
||||
local gif, err = Gif.encode(img)
|
||||
t:check(gif, "encode: " .. tostring(err))
|
||||
local back = decode(gif)
|
||||
t:check(back and same(back.pixels, img.pixels), string.format("%d colours alpha=%s: lossless round trip", ncol, tostring(alpha)))
|
||||
t:check(back.frames == nil, "single image")
|
||||
end
|
||||
end
|
||||
-- more than 256 colours: quantized, every method, with and without dithering
|
||||
local photo = image(96, 64)
|
||||
for _, method in { "mediancut", "octree" } do
|
||||
for _, dither in { false, true } do
|
||||
local gif = Gif.encode(photo, { method = method, dither = dither }) :: buffer
|
||||
local back = decode(gif)
|
||||
local p = psnr(back.pixels, photo.pixels)
|
||||
t:check(p > (if dither then 28 else 30), string.format("%s dither=%s: PSNR %.1f", method, tostring(dither), p))
|
||||
t:note("encode %s dither=%s: %d bytes, PSNR %.1f dB", method, tostring(dither), buffer.len(gif), p)
|
||||
end
|
||||
end
|
||||
local small = Gif.encode(photo, { maxColors = 4 }) :: buffer
|
||||
local sb = decode(small)
|
||||
t:check(sb.meta.palette and #sb.meta.palette <= 4, "maxColors = 4")
|
||||
-- animation: frames, delays, disposal, transparency, loops, comment, local / global palettes
|
||||
local anim = image(40, 30, 50)
|
||||
anim.frames = {
|
||||
{ image = image(40, 30, 50), x = 0, y = 0, delay = 0.1, dispose = "none", blend = "source" },
|
||||
{ image = image(10, 12, 7, true), x = 5, y = 6, delay = 0.25, dispose = "background", blend = "over" },
|
||||
{ image = image(20, 5), x = 20, y = 25, delay = 1.5, dispose = "previous", blend = "source" },
|
||||
}
|
||||
anim.loops = 4
|
||||
anim.meta.text = { comment = "hello gif" }
|
||||
for _, global in { false, true } do
|
||||
local gif, err = Gif.encode(anim, { globalPalette = global })
|
||||
t:check(gif, "animation encode: " .. tostring(err))
|
||||
local back = decode(gif)
|
||||
t:check(back.frames and #back.frames == 3, "animation frames")
|
||||
t:eq(back.loops, 4, "loops")
|
||||
t:eq(back.meta.text.comment, "hello gif", "comment")
|
||||
for i, f in anim.frames :: { Types.Frame } do
|
||||
local g = back.frames[i]
|
||||
t:check(g.x == f.x and g.y == f.y and g.image.width == f.image.width and g.image.height == f.image.height, "frame rectangle " .. i)
|
||||
t:eq(g.delay, f.delay, "frame delay " .. i)
|
||||
t:eq(g.dispose, f.dispose, "frame dispose " .. i)
|
||||
end
|
||||
t:check(same(back.frames[1].image.pixels, anim.frames[1].image.pixels), "frame 1 lossless")
|
||||
t:check(same(back.frames[2].image.pixels, anim.frames[2].image.pixels), "frame 2 lossless (transparency)")
|
||||
t:eq(back.frames[2].blend, "over", "frame 2 blend")
|
||||
t:check(psnr(back.frames[3].image.pixels, anim.frames[3].image.pixels) > 25, "frame 3 quantized")
|
||||
t:eq(back.meta.source.localPalettes, if global then 0 else 3, "palettes")
|
||||
end
|
||||
-- deterministic output
|
||||
t:eq(Crc32.of(Gif.encode(photo, { dither = true }) :: buffer), Crc32.of(Gif.encode(photo, { dither = true }) :: buffer), "deterministic")
|
||||
-- bad arguments
|
||||
local _, e = Gif.encode({} :: any)
|
||||
t:check(e and e.code == Err.ARG, "encode of a non-image")
|
||||
local _, e2 = Gif.encode(photo, { maxColors = 1 })
|
||||
t:check(e2 and e2.code == Err.ARG, "maxColors = 1")
|
||||
local _, e3 = Gif.encode(photo, { method = "kmeans" } :: any)
|
||||
t:check(e3 and e3.code == Err.ARG, "unknown method")
|
||||
local _, e4 = Gif.encode(Types.Image.new(70000, 1))
|
||||
t:check(e4 and e4.code == Err.ARG, "too wide")
|
||||
end
|
||||
|
||||
-- fuzz
|
||||
do
|
||||
local samples = {}
|
||||
for _, c in Fx do
|
||||
if #c.data < 40000 then
|
||||
table.insert(samples, c.data)
|
||||
end
|
||||
end
|
||||
t:fuzz("decode", function(b: buffer)
|
||||
return Gif.decode(Bytes.reader(b))
|
||||
end, samples, { mutations = 12, keepHeader = 6 })
|
||||
t:fuzz("decode (header too)", function(b: buffer)
|
||||
return Gif.decode(Bytes.reader(b), { maxPixels = 1e6 })
|
||||
end, samples, { mutations = 4, garbage = 50 })
|
||||
end
|
||||
|
||||
t:done()
|
||||
@@ -0,0 +1,117 @@
|
||||
-- image/hdr tests: ImageMagick-written Radiance files (new-style RLE) against its float decodes, hand-written ones
|
||||
-- (flat and old-style RLE scanlines, "#?RGBE", EXPOSURE products, all orientations) against the rgbe.c rule; the
|
||||
-- display conversion to rgba8; our encoder round-trips (RLE and flat, rgbaf32 and rgba8) within RGBE precision;
|
||||
-- errors; the fuzz no-throw pass. Fixtures: tools/fixtures/hdr.py.
|
||||
local T = require("@tests/T")
|
||||
local Hdr = require("@formats/image/hdr")
|
||||
local Bytes = require("@formats/core/Bytes")
|
||||
local Err = require("@formats/core/Err")
|
||||
local Types = require("@formats/Types")
|
||||
local Fx = require("@tests/gen/hdr")
|
||||
|
||||
local t = T.suite("hdr")
|
||||
|
||||
t:eq(Hdr.name, "hdr", "name")
|
||||
t:check(Hdr.caps.decode and Hdr.caps.encode, "caps")
|
||||
|
||||
local function dec(data: any, opts: any?): (any, any)
|
||||
return Hdr.decode(Bytes.reader(if type(data) == "string" then buffer.fromstring(data) else data), opts)
|
||||
end
|
||||
|
||||
-- worst relative error between two rgbaf32 buffers
|
||||
local function relErr(a: buffer, b: buffer, n: number): number
|
||||
local worst = 0
|
||||
for i = 0, n * 4 - 1 do
|
||||
local x, y = buffer.readf32(a, i * 4), buffer.readf32(b, i * 4)
|
||||
local d = math.abs(x - y) / math.max(math.abs(y), 1e-30)
|
||||
if math.abs(x - y) > 1e-30 then
|
||||
worst = math.max(worst, d)
|
||||
end
|
||||
end
|
||||
return worst
|
||||
end
|
||||
|
||||
for _, c in Fx do
|
||||
local img, err = dec(c.data, { native = true })
|
||||
t:check(img, c.name .. ": " .. tostring(err))
|
||||
t:check(img.width == c.width and img.height == c.height and img.format == "rgbaf32", c.name .. ": size")
|
||||
local e = relErr(img.pixels, buffer.fromstring(c.floats), c.width * c.height)
|
||||
t:check(e < 1e-6, string.format("%s: relative error %g against the reference", c.name, e))
|
||||
if c.exposure then
|
||||
t:check(math.abs(img.meta.exposure - c.exposure) < 1e-9, c.name .. ": exposure")
|
||||
end
|
||||
-- the default rgba8 display conversion
|
||||
local d8 = dec(c.data)
|
||||
t:eq(d8.format, "rgba8", c.name .. ": rgba8 by default")
|
||||
local v = buffer.readf32(img.pixels, 0)
|
||||
t:eq(buffer.readu8(d8.pixels, 0), math.floor(math.clamp(v, 0, 1) ^ (1 / 2.2) * 255 + 0.5), c.name .. ": display curve")
|
||||
-- round trips: RGBE is lossless for values already on its grid
|
||||
for _, rle in { true, false } do
|
||||
local enc = Hdr.encode(img, { rle = rle }) :: buffer
|
||||
local back = dec(enc, { native = true })
|
||||
local re = relErr(back.pixels, img.pixels, c.width * c.height)
|
||||
t:check(re < 1e-6, string.format("%s: round trip rle=%s relative error %g", c.name, tostring(rle), re))
|
||||
end
|
||||
end
|
||||
t:note("%d files match their references and round-trip", #Fx)
|
||||
|
||||
do
|
||||
-- arbitrary floats: RGBE keeps about 1% relative precision on the largest channel
|
||||
local rand = T.rng(8)
|
||||
local img = Types.Image.new(40, 10, "rgbaf32")
|
||||
for i = 0, 399 do
|
||||
for c = 0, 2 do
|
||||
buffer.writef32(img.pixels, i * 16 + c * 4, rand(100000) / 1000)
|
||||
end
|
||||
buffer.writef32(img.pixels, i * 16 + 12, 1)
|
||||
end
|
||||
local back = dec(Hdr.encode(img) :: buffer, { native = true })
|
||||
local worst = 0
|
||||
for i = 0, 399 do
|
||||
local m = 0
|
||||
for c = 0, 2 do
|
||||
m = math.max(m, buffer.readf32(img.pixels, i * 16 + c * 4))
|
||||
end
|
||||
for c = 0, 2 do
|
||||
worst = math.max(worst, math.abs(buffer.readf32(back.pixels, i * 16 + c * 4) - buffer.readf32(img.pixels, i * 16 + c * 4)) / math.max(m, 1e-9))
|
||||
end
|
||||
end
|
||||
t:check(worst < 1 / 128, "float precision " .. worst)
|
||||
-- rgba8 in, rgba8 out
|
||||
local i8 = Types.Image.new(9, 2)
|
||||
for i = 0, 17 do
|
||||
buffer.writeu32(i8.pixels, i * 4, 0xFF000000 + (i * 14) * 0x10101)
|
||||
end
|
||||
local b8 = dec(Hdr.encode(i8) :: buffer)
|
||||
local w8 = 0
|
||||
for i = 0, 71 do
|
||||
w8 = math.max(w8, math.abs(buffer.readu8(b8.pixels, i) - buffer.readu8(i8.pixels, i)))
|
||||
end
|
||||
t:check(w8 <= 2, "rgba8 round trip within 2: " .. w8)
|
||||
local ex = dec(Hdr.encode(img, { exposure = 0.5 }) :: buffer)
|
||||
t:eq(ex.meta.exposure, 0.5, "exposure written")
|
||||
t:eq(Hdr.sniff(Bytes.reader(buffer.fromstring("#?RADIANCE\n"))), 1, "sniff")
|
||||
t:eq(Hdr.sniff(Bytes.reader(buffer.fromstring("P6\n"))), 0, "sniff other")
|
||||
local _, e1 = dec("#?RADIANCE\nFORMAT=32-bit_rle_xyze\n\n-Y 1 +X 1\n\0\0\0\0")
|
||||
t:check(e1 and e1.code == Err.UNSUPPORTED, "xyze unsupported")
|
||||
local _, e2 = dec("#?RADIANCE\n\n-Y 10 +X 10\n", { maxPixels = 50 })
|
||||
t:check(e2 and e2.code == Err.LIMIT, "maxPixels")
|
||||
local _, e3 = dec("#?RADIANCE\n\nnonsense\n")
|
||||
t:check(e3 and e3.code == Err.CORRUPT, "bad resolution")
|
||||
local tr = dec(string.sub(Fx[1].data, 1, #Fx[1].data // 2))
|
||||
t:check(tr and tr.meta.truncated, "truncated data flagged")
|
||||
local _, e4 = Hdr.encode(Types.Image.new(1, 1, "gray8"))
|
||||
t:check(e4 and e4.code == Err.ARG, "gray8 refused")
|
||||
end
|
||||
|
||||
do
|
||||
local samples = {}
|
||||
for _, c in Fx do
|
||||
table.insert(samples, c.data)
|
||||
end
|
||||
t:fuzz("decode", function(b: buffer)
|
||||
return Hdr.decode(Bytes.reader(b))
|
||||
end, samples)
|
||||
end
|
||||
|
||||
t:done()
|
||||
@@ -0,0 +1,123 @@
|
||||
-- image/ico tests: ImageMagick- and Pillow-written icons (PNG and BMP entries, a 256 PNG entry) and hand-built ones
|
||||
-- (1 / 4 / 8 / 24 / 32-bit DIBs with AND masks, a cursor with hotspots) against Pillow's decode of every entry; PNG
|
||||
-- entries through opts.decodeImage (and UNSUPPORTED without it); the main image is the largest entry; our encoder
|
||||
-- (DIB, PNG through opts.encodeImage, cursors) round-trips; errors; the fuzz no-throw pass. Fixtures: tools/fixtures/ico.py.
|
||||
local T = require("@tests/T")
|
||||
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 Types = require("@formats/Types")
|
||||
local Fx = require("@tests/gen/ico")
|
||||
|
||||
local t = T.suite("ico")
|
||||
|
||||
t:eq(Ico.name, "ico", "name")
|
||||
t:check(Ico.caps.decode and Ico.caps.encode, "caps")
|
||||
|
||||
local hooks = {
|
||||
decodeImage = function(b: buffer)
|
||||
return Png.decode(Bytes.reader(b))
|
||||
end,
|
||||
encodeImage = function(img: any, _fmt: string)
|
||||
return Png.encode(img)
|
||||
end,
|
||||
}
|
||||
|
||||
local function dec(data: any, opts: any?): (any, any)
|
||||
return Ico.decode(Bytes.reader(if type(data) == "string" then buffer.fromstring(data) else data), opts)
|
||||
end
|
||||
|
||||
local entries = 0
|
||||
for _, c in Fx do
|
||||
local img, err = dec(c.data, hooks)
|
||||
t:check(img, c.name .. ": " .. tostring(err))
|
||||
local frames = img.frames or { { image = img } }
|
||||
t:eq(#frames, #c.entries, c.name .. ": entry count")
|
||||
local biggest = 0
|
||||
for i, e in c.entries do
|
||||
local f = frames[i].image
|
||||
t:check(f.width == e.width and f.height == e.height, string.format("%s entry %d: size %dx%d, want %dx%d", c.name, i, f.width, f.height, e.width, e.height))
|
||||
local worst = 0
|
||||
for k = 0, #e.rgba - 1 do
|
||||
worst = math.max(worst, math.abs(buffer.readu8(f.pixels, k) - string.byte(e.rgba, k + 1)))
|
||||
end
|
||||
t:check(worst == 0, string.format("%s entry %d: differs from Pillow by %d", c.name, i, worst))
|
||||
biggest = math.max(biggest, e.width * e.height)
|
||||
entries += 1
|
||||
end
|
||||
t:eq(img.width * img.height, biggest, c.name .. ": the main image is the largest entry")
|
||||
-- round trip through our encoder (DIB entries, and PNG for 256 through the hook)
|
||||
local enc, eerr = Ico.encode(img, hooks)
|
||||
t:check(enc, c.name .. ": encode: " .. tostring(eerr))
|
||||
local back = dec(enc, hooks)
|
||||
local bf = back.frames or { { image = back } }
|
||||
t:eq(#bf, #frames, c.name .. ": round trip entries")
|
||||
for i, f in frames do
|
||||
local a, b = f.image, bf[i].image
|
||||
local same = a.width == b.width and a.height == b.height
|
||||
if same then
|
||||
-- alpha-0 pixels keep their colour in 32-bit DIBs too
|
||||
same = buffer.tostring(a.pixels) == buffer.tostring(b.pixels)
|
||||
end
|
||||
t:check(same, string.format("%s: round trip entry %d", c.name, i))
|
||||
end
|
||||
end
|
||||
t:note("%d icons, %d entries match Pillow and round-trip", #Fx, entries)
|
||||
|
||||
do
|
||||
local byName = {}
|
||||
for _, c in Fx do
|
||||
byName[c.name] = c
|
||||
end
|
||||
-- without the hook: PNG entries are skipped, BMP entries still decode
|
||||
local big, sm = Types.Image.new(256, 256), Types.Image.new(16, 16)
|
||||
buffer.fill(big.pixels, 0, 77, 256 * 256 * 4)
|
||||
buffer.fill(sm.pixels, 0, 99, 16 * 16 * 4)
|
||||
local two = Types.Image.new(256, 256)
|
||||
two.frames = { { image = big, delay = 0, x = 0, y = 0, dispose = "none", blend = "source" }, { image = sm, delay = 0, x = 0, y = 0, dispose = "none", blend = "source" } }
|
||||
local mixed = Ico.encode(two, hooks) :: buffer
|
||||
t:eq(buffer.readstring(mixed, 6 + 32, 4), "\137PNG", "the 256 entry is written as PNG through the hook")
|
||||
local im = dec(mixed)
|
||||
t:check(im and im.width == 16 and im.meta.source.skipped == 1, "no hook: the PNG entry is skipped")
|
||||
t:eq(dec(mixed, hooks).width, 256, "with the hook: the PNG entry is the main image")
|
||||
local _, e = dec(byName["pil-png"].data)
|
||||
t:check(e and e.code == Err.UNSUPPORTED, "PNG-only icon without the hook: " .. tostring(e))
|
||||
local cur = dec(byName["hand-cur"].data)
|
||||
t:check(cur.meta.source.type == "cur" and cur.meta.hotspot and cur.meta.hotspot.x == 3 and cur.meta.hotspot.y == 4, "cursor hotspot")
|
||||
t:eq(cur.frames[2].image.meta.hotspot.x, 5, "second hotspot")
|
||||
local cenc = Ico.encode(cur) :: buffer
|
||||
t:eq(buffer.readu16(cenc, 2), 2, "cursor written as CUR")
|
||||
local cb = dec(cenc)
|
||||
t:check(cb.meta.hotspot.x == 3 and cb.frames[2].image.meta.hotspot.y == 6, "hotspots round-trip")
|
||||
-- PNG entries on request
|
||||
local small = Types.Image.new(16, 16)
|
||||
buffer.fill(small.pixels, 0, 200, 16 * 16 * 4)
|
||||
local pe = Ico.encode(small, { png = true, encodeImage = hooks.encodeImage }) :: buffer
|
||||
t:eq(buffer.readstring(pe, 22, 4), "\137PNG", "opts.png writes a PNG entry")
|
||||
local _, e2 = Ico.encode(small, { png = true })
|
||||
t:check(e2 and e2.code == Err.ARG, "opts.png without a hook")
|
||||
local _, e3 = Ico.encode(Types.Image.new(300, 10))
|
||||
t:check(e3 and e3.code == Err.ARG, "over 256 pixels")
|
||||
t:eq(Ico.sniff(Bytes.reader(buffer.fromstring(byName["pil-bmp"].data))), 0.8, "sniff")
|
||||
t:eq(Ico.sniff(Bytes.reader(buffer.fromstring(string.rep("\0", 30)))), 0, "sniff zeros")
|
||||
local _, e4 = dec(string.sub(byName["pil-bmp"].data, 1, 20))
|
||||
t:check(e4 and e4.code == Err.TRUNCATED, "truncated directory")
|
||||
end
|
||||
|
||||
do
|
||||
local samples = {}
|
||||
for _, c in Fx do
|
||||
if #c.data < 40000 then
|
||||
table.insert(samples, c.data)
|
||||
end
|
||||
end
|
||||
t:fuzz("decode", function(b: buffer)
|
||||
return Ico.decode(Bytes.reader(b), hooks)
|
||||
end, samples)
|
||||
t:fuzz("decode without hooks", function(b: buffer)
|
||||
return Ico.decode(Bytes.reader(b))
|
||||
end, samples, { mutations = 3, garbage = 50 })
|
||||
end
|
||||
|
||||
t:done()
|
||||
@@ -0,0 +1,300 @@
|
||||
-- image/jpeg tests: every cjpeg / jpegtran fixture (baseline, progressive, arithmetic, every sampling, restarts, grey,
|
||||
-- RGB, 12-bit, odd sizes, stripped DHT) against djpeg's decodes at scales 1, 1/2, 1/4, 1/8 (bit-exact, CRC-32);
|
||||
-- CMYK / YCCK against Pillow; refused files; info / meta (EXIF orientation applied, ICC, comment, density); the
|
||||
-- encoder byte-identical to cjpeg for the same settings, its PSNR, round trips through our decoder; truncated files;
|
||||
-- the fuzz no-throw pass. Ported from jpeg-luau tests/jpeg.luau. Fixtures: tools/fixtures/jpeg.py.
|
||||
local T = require("@tests/T")
|
||||
local Jpeg = require("@formats/image/jpeg")
|
||||
local Bytes = require("@formats/core/Bytes")
|
||||
local Crc32 = require("@formats/core/Crc32")
|
||||
local Err = require("@formats/core/Err")
|
||||
local Types = require("@formats/Types")
|
||||
local Dec = require("@tests/gen/jpeg_dec")
|
||||
local Enc = require("@tests/gen/jpeg_enc")
|
||||
|
||||
local t = T.suite("jpeg")
|
||||
|
||||
local function decode(data: any, opts: any?): (any, any)
|
||||
local b = if type(data) == "string" then buffer.fromstring(data) else data
|
||||
return Jpeg.decode(Bytes.reader(b), opts)
|
||||
end
|
||||
|
||||
-- the contract
|
||||
t:eq(Jpeg.name, "jpeg", "name")
|
||||
t:eq(Jpeg.category, "image", "category")
|
||||
t:check(Jpeg.caps.decode and Jpeg.caps.encode, "caps")
|
||||
|
||||
-- decodes against djpeg / Pillow
|
||||
local files: { [string]: string } = {}
|
||||
do
|
||||
local refs, arith = 0, 0
|
||||
for _, c in Dec do
|
||||
files[c.name] = c.data
|
||||
if c.err then
|
||||
local img, err = decode(c.data)
|
||||
t:check(img == nil and err and err.code == Err.UNSUPPORTED and string.find(err.message, c.err, 1, true), c.name .. ": expected UNSUPPORTED " .. c.err .. ", got " .. tostring(err))
|
||||
elseif c.cmyk then
|
||||
local img, err = decode(c.data)
|
||||
t:check(img, c.name .. ": " .. tostring(err))
|
||||
local n = #c.cmyk // 3
|
||||
t:eq(img.width * img.height, n, c.name .. ": size")
|
||||
local worst = 0
|
||||
for i = 0, n - 1 do
|
||||
for k = 0, 2 do
|
||||
worst = math.max(worst, math.abs(buffer.readu8(img.pixels, i * 4 + k) - string.byte(c.cmyk, i * 3 + k + 1)))
|
||||
end
|
||||
end
|
||||
t:check(worst <= 1, c.name .. ": max error " .. worst .. " against Pillow")
|
||||
refs += 1
|
||||
else
|
||||
for _, r in c.refs do
|
||||
local name = c.name .. " 1/" .. r.scale
|
||||
local img, err = decode(c.data, { scale = r.scale })
|
||||
t:check(img, name .. ": " .. tostring(err))
|
||||
t:check(img.width == r.width and img.height == r.height, string.format("%s: size %dx%d, djpeg %dx%d", name, img.width, img.height, r.width, r.height))
|
||||
t:check(not img.meta.truncated, name .. ": flagged truncated")
|
||||
local crc = Crc32.of(img.pixels)
|
||||
t:check(crc == r.crc, string.format("%s: pixels crc %08x, djpeg %08x", name, crc, r.crc))
|
||||
refs += 1
|
||||
if img.meta.source.arithmetic then
|
||||
arith += 1
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
t:note("%d files, %d decodes bit-exact with djpeg / Pillow (%d arithmetic-coded)", #Dec, refs, arith)
|
||||
end
|
||||
|
||||
local function file(name: string): buffer
|
||||
return buffer.fromstring(files[name])
|
||||
end
|
||||
|
||||
-- sniff, info, meta, options
|
||||
do
|
||||
t:eq(Jpeg.sniff(Bytes.reader(file("photo_q90_420"))), 1, "sniff a JPEG")
|
||||
t:eq(Jpeg.sniff(Bytes.reader(buffer.fromstring("GIF89a"))), 0, "sniff a GIF")
|
||||
t:eq(Jpeg.sniff(Bytes.reader(buffer.create(0))), 0, "sniff nothing")
|
||||
local i = Jpeg.info(Bytes.reader(file("photo_q90_420_prog")))
|
||||
t:check(i and i.width == 160 and i.height == 120 and i.progressive and i.components == 3, "info: progressive 4:2:0")
|
||||
t:check(i.sampling[1][1] == 2 and i.sampling[1][2] == 2 and i.sampling[2][1] == 1, "info: sampling")
|
||||
t:check(i.colorSpace == "ycbcr" and i.precision == 8 and i.jfif and not i.arithmetic, "info: colour space")
|
||||
t:check(Jpeg.info(Bytes.reader(file("photo_arith_420_prog"))).arithmetic, "info: arithmetic")
|
||||
t:eq(Jpeg.info(Bytes.reader(file("photo_gray"))).colorSpace, "gray", "info: grey")
|
||||
do
|
||||
local g = decode(file("photo_gray"), { native = true })
|
||||
local rgba = decode(file("photo_gray"))
|
||||
t:check(g.format == "gray8" and buffer.len(g.pixels) == g.width * g.height, "native grey decodes to gray8")
|
||||
t:eq(buffer.readu8(g.pixels, 77), buffer.readu8(rgba.pixels, 77 * 4), "gray8 samples")
|
||||
t:eq(decode(file("photo_q90_420"), { native = true }).format, "rgba8", "native colour stays rgba8")
|
||||
end
|
||||
t:eq(Jpeg.info(Bytes.reader(file("photo_rgb"))).colorSpace, "rgb", "info: RGB")
|
||||
t:eq(Jpeg.info(Bytes.reader(file("photo_cmyk"))).colorSpace, "cmyk", "info: CMYK")
|
||||
t:eq(Jpeg.info(Bytes.reader(file("photo_ycck"))).colorSpace, "ycck", "info: YCCK")
|
||||
t:eq(Jpeg.info(Bytes.reader(file("photo_12bit_420"))).precision, 12, "info: 12-bit")
|
||||
local img = decode(file("photo_q90_420_prog"))
|
||||
local s = img.meta.source
|
||||
t:check(img.kind == "image" and img.format == "rgba8", "a standard rgba8 Image")
|
||||
t:check(s.format == "jpeg" and s.progressive and s.components == 3 and s.colorType == "ycbcr" and img.meta.orientation == 1, "meta.source")
|
||||
local _, e = decode(file("photo_q90_420"), { maxPixels = 1000 })
|
||||
t:check(e and e.code == Err.LIMIT, "maxPixels: " .. tostring(e))
|
||||
t:check(decode(file("photo_q90_420"), { maxPixels = 160 * 120 }) ~= nil, "maxPixels at the limit")
|
||||
local _, e2 = decode(file("photo_q90_420"), { scale = 3 })
|
||||
t:check(e2 and e2.code == Err.ARG, "bad scale")
|
||||
local _, e3 = decode("nope")
|
||||
t:check(e3 and e3.code == Err.CORRUPT, "junk: " .. tostring(e3))
|
||||
-- a range inside a bigger buffer
|
||||
local data = files["photo_q90_420"]
|
||||
local big = buffer.create(#data + 20)
|
||||
buffer.writestring(big, 11, data)
|
||||
local a = Jpeg.decode(Bytes.reader(big, 11, #data))
|
||||
t:check(a and Crc32.of(a.pixels) == Crc32.of(decode(data).pixels), "decode from a sub-range")
|
||||
end
|
||||
|
||||
-- EXIF orientation (both byte orders), applied with opts.orient; ICC + comment + density segments
|
||||
local function withSegments(data: string, segs: { string }): string
|
||||
return string.sub(data, 1, 2) .. table.concat(segs) .. string.sub(data, 3)
|
||||
end
|
||||
local function seg(marker: number, body: string): string
|
||||
local len = #body + 2
|
||||
return "\255" .. string.char(marker, len // 256, len % 256) .. body
|
||||
end
|
||||
local function exifSeg(orient: number, le: boolean): string
|
||||
local tiff
|
||||
if le then
|
||||
tiff = "II\42\0\8\0\0\0" .. "\1\0" .. "\18\1\3\0\1\0\0\0" .. string.char(orient, 0, 0, 0) .. "\0\0\0\0"
|
||||
else
|
||||
tiff = "MM\0\42\0\0\0\8" .. "\0\1" .. "\1\18\0\3\0\0\0\1" .. string.char(0, orient, 0, 0) .. "\0\0\0\0"
|
||||
end
|
||||
return seg(0xE1, "Exif\0\0" .. tiff)
|
||||
end
|
||||
do
|
||||
local base = files["photo_q90_420"]
|
||||
local plain = decode(base)
|
||||
local w, h = plain.width, plain.height
|
||||
local function px(img: any, x: number, y: number): number
|
||||
return buffer.readu32(img.pixels, (y * img.width + x) * 4)
|
||||
end
|
||||
for _, le in { true, false } do
|
||||
for o = 1, 8 do
|
||||
local d = withSegments(base, { exifSeg(o, le) })
|
||||
local img = decode(d)
|
||||
t:check(img and img.meta.orientation == o, "EXIF orientation " .. o)
|
||||
t:check(img.meta.exif and buffer.readstring(img.meta.exif, 0, 2) == (if le then "II" else "MM"), "meta.exif")
|
||||
t:eq(Jpeg.info(Bytes.reader(buffer.fromstring(d))).orientation, o, "info orientation " .. o)
|
||||
local r = decode(d, { orient = true })
|
||||
local swap = o >= 5
|
||||
t:check(r.width == (if swap then h else w) and r.height == (if swap then w else h), "oriented size " .. o)
|
||||
-- where the source's top-left pixel lands
|
||||
local tl = px(plain, 0, 0)
|
||||
local want = ({
|
||||
{ 0, 0 },
|
||||
{ r.width - 1, 0 },
|
||||
{ r.width - 1, r.height - 1 },
|
||||
{ 0, r.height - 1 },
|
||||
{ 0, 0 },
|
||||
{ r.width - 1, 0 },
|
||||
{ r.width - 1, r.height - 1 },
|
||||
{ 0, r.height - 1 },
|
||||
})[o]
|
||||
t:eq(px(r, want[1], want[2]), tl, "orientation " .. o .. ": top-left pixel")
|
||||
end
|
||||
end
|
||||
local icc = string.rep("icc!", 50)
|
||||
local d = withSegments(base, {
|
||||
seg(0xE2, "ICC_PROFILE\0\1\2" .. string.sub(icc, 1, 120)),
|
||||
seg(0xE2, "ICC_PROFILE\0\2\2" .. string.sub(icc, 121)),
|
||||
seg(0xFE, "hello jpeg"),
|
||||
})
|
||||
local img = decode(d)
|
||||
t:check(img.meta.icc and buffer.tostring(img.meta.icc) == icc, "ICC chunks joined")
|
||||
t:check(img.meta.text and img.meta.text.comment == "hello jpeg", "COM")
|
||||
t:check(img.meta.dpi == nil, "no density in cjpeg's JFIF (aspect only)")
|
||||
end
|
||||
|
||||
-- the encoder: byte-identical to cjpeg
|
||||
local function source(c: any): any
|
||||
local img = Types.Image.new(c.width, c.height, c.format)
|
||||
if c.format == "rgb8" then
|
||||
buffer.writestring(img.pixels, 0, c.pixels)
|
||||
else
|
||||
buffer.writestring(img.pixels, 0, c.pixels)
|
||||
end
|
||||
return img
|
||||
end
|
||||
local function psnr(a: buffer, b: buffer, n: number, bpp: number): number
|
||||
local se = 0
|
||||
for i = 0, n - 1 do
|
||||
for k = 0, 2 do
|
||||
local d = buffer.readu8(a, i * 4 + k) - buffer.readu8(b, i * bpp + (if bpp == 1 then 0 else k))
|
||||
se += d * d
|
||||
end
|
||||
end
|
||||
if se == 0 then
|
||||
return 99
|
||||
end
|
||||
return 10 * math.log10(255 * 255 * n * 3 / se)
|
||||
end
|
||||
do
|
||||
local exact = 0
|
||||
for _, c in Enc do
|
||||
local img = source(c)
|
||||
local out, err = Jpeg.encode(img, c.opts)
|
||||
t:check(out, c.name .. ": encode: " .. tostring(err))
|
||||
local same = buffer.tostring(out :: buffer) == c.want
|
||||
if not same then
|
||||
local s = buffer.tostring(out :: buffer)
|
||||
local at = 0
|
||||
for i = 1, math.min(#s, #c.want) do
|
||||
if string.byte(s, i) ~= string.byte(c.want, i) then
|
||||
at = i
|
||||
break
|
||||
end
|
||||
end
|
||||
t:check(false, string.format("%s: differs from cjpeg at byte %d (ours %d bytes, cjpeg %d)", c.name, at, #s, #c.want))
|
||||
end
|
||||
exact += 1
|
||||
local back = decode(out)
|
||||
t:check(back and back.width == c.width and back.height == c.height, c.name .. ": decodes")
|
||||
local bpp = if c.format == "gray8" then 1 else 3
|
||||
local q = c.opts.quality
|
||||
local p = psnr(back.pixels, img.pixels, c.width * c.height, bpp)
|
||||
if c.width * c.height >= 1000 and not c.opts.gray then
|
||||
-- the synthetic source has salt-and-pepper noise: lower thresholds
|
||||
local floor = (if q >= 75 then 28 elseif q >= 50 then 25 else 20) - (if string.find(c.name, "synth") then 10 else 0)
|
||||
t:check(p > floor, string.format("%s: PSNR %.1f dB", c.name, p))
|
||||
end
|
||||
end
|
||||
t:note("%d encodes byte-identical to cjpeg", exact)
|
||||
end
|
||||
|
||||
-- encoder: meta segments round trip, rgba8 input, options, errors
|
||||
do
|
||||
local c = Enc[1]
|
||||
local rgb = source(c)
|
||||
local img = Types.Image.new(c.width, c.height)
|
||||
for i = 0, c.width * c.height - 1 do
|
||||
buffer.writeu32(img.pixels, i * 4, buffer.readu16(rgb.pixels, i * 3) + buffer.readu8(rgb.pixels, i * 3 + 2) * 65536 + 0x80000000)
|
||||
end
|
||||
local a = Jpeg.encode(rgb, { quality = 80 }) :: buffer
|
||||
local b = Jpeg.encode(img, { quality = 80 }) :: buffer
|
||||
t:check(buffer.tostring(a) == buffer.tostring(b), "rgba8 input encodes as rgb8 (alpha ignored)")
|
||||
img.meta.icc = buffer.fromstring(string.rep("P", 70000))
|
||||
img.meta.exif = buffer.fromstring("MM\0\42\0\0\0\8\0\1\1\18\0\3\0\0\0\1\0\6\0\0\0\0\0\0")
|
||||
img.meta.text = { comment = "made by formats-luau" }
|
||||
img.meta.dpi = { x = 300, y = 300 }
|
||||
local m = Jpeg.encode(img, { quality = 80 }) :: buffer
|
||||
local back = decode(m)
|
||||
t:check(back.meta.icc and buffer.len(back.meta.icc) == 70000, "ICC round trip (two APP2 chunks)")
|
||||
t:eq(back.meta.orientation, 6, "EXIF round trip")
|
||||
t:check(back.meta.text and back.meta.text.comment == "made by formats-luau", "COM round trip")
|
||||
t:check(back.meta.dpi and back.meta.dpi.x == 300, "density round trip")
|
||||
local nm = Jpeg.encode(img, { quality = 80, meta = false, jfif = false }) :: buffer
|
||||
t:check(buffer.len(nm) < buffer.len(a), "meta = false, jfif = false")
|
||||
local _, e1 = Jpeg.encode({} :: any)
|
||||
t:check(e1 and e1.code == Err.ARG, "encode of a non-image")
|
||||
local _, e2 = Jpeg.encode(Types.Image.new(4, 4, "rgba16"))
|
||||
t:check(e2 and e2.code == Err.ARG, "rgba16 is refused")
|
||||
local _, e3 = Jpeg.encode(img, { quality = 0 })
|
||||
t:check(e3 and e3.code == Err.ARG, "quality 0")
|
||||
local _, e4 = Jpeg.encode(img, { subsampling = "411" } :: any)
|
||||
t:check(e4 and e4.code == Err.ARG, "subsampling 411")
|
||||
local one = Jpeg.encode(Types.Image.new(1, 1), { progressive = true, subsampling = "444" })
|
||||
t:check(one and decode(one).width == 1, "1x1 progressive")
|
||||
end
|
||||
|
||||
-- truncated files: decode what is there (flagged) or fail with an Err; never throw
|
||||
do
|
||||
local count, partial = 0, 0
|
||||
for _, name in { "photo_q90_420", "photo_q90_420_prog", "photo_rst1", "photo_rst1b_prog", "photo_arith_420", "photo_arith_rst_prog" } do
|
||||
local s = files[name]
|
||||
for cut = 0, #s - 1, math.max(1, #s // 61) do
|
||||
local ok, img, err = pcall(decode, string.sub(s, 1, cut))
|
||||
t:check(ok, name .. " cut at " .. cut .. ": threw " .. tostring(img))
|
||||
t:check(img or Err.is(err), name .. " cut at " .. cut .. ": no image and no Err")
|
||||
if img then
|
||||
partial += 1
|
||||
t:check(img.meta.truncated or cut >= #s - 2, name .. " cut at " .. cut .. ": not flagged truncated")
|
||||
end
|
||||
count += 1
|
||||
end
|
||||
end
|
||||
t:note("%d truncated files, %d decoded partially", count, partial)
|
||||
end
|
||||
|
||||
-- fuzz
|
||||
do
|
||||
local samples = {}
|
||||
for i, c in Dec do
|
||||
if i % 9 == 0 and #c.data < 40000 then
|
||||
table.insert(samples, c.data)
|
||||
end
|
||||
end
|
||||
t:fuzz("decode", function(b: buffer)
|
||||
return Jpeg.decode(Bytes.reader(b))
|
||||
end, samples, { mutations = 6, keepHeader = 2 })
|
||||
t:fuzz("decode 1/8", function(b: buffer)
|
||||
return Jpeg.decode(Bytes.reader(b), { scale = 8 })
|
||||
end, samples, { mutations = 2, garbage = 30 })
|
||||
end
|
||||
|
||||
t:done()
|
||||
@@ -0,0 +1,125 @@
|
||||
-- image/pnm tests: ImageMagick-written P1-P7 / PFM files (ASCII and binary, 8 / 16-bit, PAM tuple types, both PFM
|
||||
-- byte orders) and hand-written edge cases (comments, packed P1 digits, odd maxvals) against ImageMagick's RGBA
|
||||
-- decodes (8-bit; 16-bit and float natively); round trips of every subtype through our encoder, binary and ASCII;
|
||||
-- errors; the fuzz no-throw pass. Fixtures: tools/fixtures/pnm.py.
|
||||
local T = require("@tests/T")
|
||||
local Pnm = require("@formats/image/pnm")
|
||||
local Bytes = require("@formats/core/Bytes")
|
||||
local Err = require("@formats/core/Err")
|
||||
local Types = require("@formats/Types")
|
||||
local Fx = require("@tests/gen/pnm")
|
||||
|
||||
local t = T.suite("pnm")
|
||||
|
||||
t:eq(Pnm.name, "pnm", "name")
|
||||
t:check(Pnm.caps.decode and Pnm.caps.encode, "caps")
|
||||
|
||||
local function dec(data: any, opts: any?): (any, any)
|
||||
return Pnm.decode(Bytes.reader(if type(data) == "string" then buffer.fromstring(data) else data), opts)
|
||||
end
|
||||
|
||||
-- channel c (0..3) of pixel i of a native image, in 16-bit scale
|
||||
local function chan16(img: any, i: number, c: number): number
|
||||
local px, f = img.pixels, img.format
|
||||
if f == "gray16" then
|
||||
return if c == 3 then 65535 else buffer.readu16(px, i * 2)
|
||||
elseif f == "rgba16" then
|
||||
return buffer.readu16(px, i * 8 + c * 2)
|
||||
elseif f == "gray8" then
|
||||
return if c == 3 then 65535 else buffer.readu8(px, i) * 257
|
||||
elseif f == "graya8" then
|
||||
return buffer.readu8(px, i * 2 + (if c == 3 then 1 else 0)) * 257
|
||||
elseif f == "rgb8" then
|
||||
return if c == 3 then 65535 else buffer.readu8(px, i * 3 + c) * 257
|
||||
end
|
||||
return buffer.readu8(px, i * 4 + c) * 257
|
||||
end
|
||||
|
||||
for _, c in Fx do
|
||||
local img, err = dec(c.data)
|
||||
t:check(img, c.name .. ": " .. tostring(err))
|
||||
t:check(img.width == c.width and img.height == c.height and img.format == "rgba8", c.name .. ": size")
|
||||
local worst = 0
|
||||
for i = 0, c.width * c.height * 4 - 1 do
|
||||
worst = math.max(worst, math.abs(buffer.readu8(img.pixels, i) - string.byte(c.rgba8, i + 1)))
|
||||
end
|
||||
t:check(worst <= c.tol, string.format("%s: max difference %d against ImageMagick (allowed %d)", c.name, worst, c.tol))
|
||||
local nat = dec(c.data, { native = true })
|
||||
if c.rgba16 then
|
||||
local w16 = 0
|
||||
for i = 0, c.width * c.height - 1 do
|
||||
for k = 0, 3 do
|
||||
local o = (i * 4 + k) * 2 + 1
|
||||
local want = string.byte(c.rgba16, o) * 256 + string.byte(c.rgba16, o + 1)
|
||||
w16 = math.max(w16, math.abs(chan16(nat, i, k) - want))
|
||||
end
|
||||
end
|
||||
t:check(w16 <= c.tol * 66, string.format("%s: 16-bit max difference %d", c.name, w16))
|
||||
end
|
||||
if c.floats then
|
||||
t:eq(nat.format, "rgbaf32", c.name .. ": native float")
|
||||
local fb = buffer.fromstring(c.floats)
|
||||
local fw = 0
|
||||
for i = 0, c.width * c.height * 4 - 1 do
|
||||
fw = math.max(fw, math.abs(buffer.readf32(nat.pixels, i * 4) - buffer.readf32(fb, i * 4)))
|
||||
end
|
||||
t:check(fw < 1e-6, c.name .. ": floats")
|
||||
end
|
||||
-- round trips through our encoder: the same subtype, binary and ASCII
|
||||
local ext = string.match(c.name, "%.(%a+)$")
|
||||
for _, ascii in { false, true } do
|
||||
local enc, eerr = Pnm.encode(nat, { format = ext, ascii = ascii })
|
||||
t:check(enc, c.name .. ": encode: " .. tostring(eerr))
|
||||
local back = dec(enc, { native = true })
|
||||
t:check(back and back.format == nat.format and buffer.tostring(back.pixels) == buffer.tostring(nat.pixels), string.format("%s: round trip (ascii %s)", c.name, tostring(ascii)))
|
||||
end
|
||||
end
|
||||
t:note("%d files match ImageMagick and round-trip", #Fx)
|
||||
|
||||
-- encoder choices, conversions, options
|
||||
do
|
||||
local img = Types.Image.new(3, 2)
|
||||
for i = 0, 23 do
|
||||
buffer.writeu8(img.pixels, i, (i * 40) % 256)
|
||||
end
|
||||
for i = 0, 5 do
|
||||
buffer.writeu8(img.pixels, i * 4 + 3, 255)
|
||||
end
|
||||
local s = buffer.tostring(Pnm.encode(img) :: buffer)
|
||||
t:eq(string.sub(s, 1, 2), "P6", "opaque rgba8 -> P6")
|
||||
buffer.writeu8(img.pixels, 3, 7)
|
||||
t:eq(string.sub(buffer.tostring(Pnm.encode(img) :: buffer), 1, 2), "P7", "alpha -> PAM")
|
||||
t:eq(string.sub(buffer.tostring(Pnm.encode(img, { format = "pgm" }) :: buffer), 1, 2), "P5", "pgm by name")
|
||||
t:eq(string.sub(buffer.tostring(Pnm.encode(img, { format = "pbm", ascii = true }) :: buffer), 1, 2), "P1", "ascii pbm")
|
||||
t:eq(string.sub(buffer.tostring(Pnm.encode(img, { format = "pfm" }) :: buffer), 1, 2), "PF", "pfm by name")
|
||||
local g = Types.Image.new(2, 1, "gray8")
|
||||
t:eq(string.sub(buffer.tostring(Pnm.encode(g) :: buffer), 1, 2), "P5", "gray8 -> P5")
|
||||
local m = Pnm.encode(g, { maxval = 15, ascii = true }) :: buffer
|
||||
t:check(string.find(buffer.tostring(m), "\n15\n", 1, true) ~= nil, "maxval option")
|
||||
local _, e = Pnm.encode(img, { maxval = 0 })
|
||||
t:check(e and e.code == Err.ARG, "bad maxval")
|
||||
local _, e2 = Pnm.encode({} :: any)
|
||||
t:check(e2 and e2.code == Err.ARG, "not an image")
|
||||
t:eq(Pnm.sniff(Bytes.reader(buffer.fromstring("P6\n1 1\n255\n..."))), 0.9, "sniff")
|
||||
t:eq(Pnm.sniff(Bytes.reader(buffer.fromstring("PNG"))), 0, "sniff other")
|
||||
local _, e3 = dec("P6\n100 100\n255\n" .. string.rep("x", 50))
|
||||
t:check(e3 and e3.code == Err.TRUNCATED, "truncated")
|
||||
local _, e4 = dec("P6\n10 10\n255\n", { maxPixels = 50 })
|
||||
t:check(e4 and e4.code == Err.LIMIT, "maxPixels")
|
||||
local _, e5 = dec("P3\n1 1\n255\n1 x 3\n")
|
||||
t:check(e5 and e5.code == Err.CORRUPT, "bad ascii")
|
||||
end
|
||||
|
||||
do
|
||||
local samples = {}
|
||||
for i, c in Fx do
|
||||
if i % 3 == 0 then
|
||||
table.insert(samples, c.data)
|
||||
end
|
||||
end
|
||||
t:fuzz("decode", function(b: buffer)
|
||||
return Pnm.decode(Bytes.reader(b))
|
||||
end, samples)
|
||||
end
|
||||
|
||||
t:done()
|
||||
@@ -0,0 +1,85 @@
|
||||
-- image/qoi tests: Pillow-written QOI files (RGB / RGBA, noise / gradients / runs / small deltas, 1x1 to 500x1)
|
||||
-- decode to Pillow's pixels; our encoder writes the same bytes as Pillow for the same pixels and round-trips exactly;
|
||||
-- native rgb8 output, the colour space tag, errors, and the fuzz no-throw pass. Fixtures: tools/fixtures/qoi.py.
|
||||
local T = require("@tests/T")
|
||||
local Qoi = require("@formats/image/qoi")
|
||||
local Bytes = require("@formats/core/Bytes")
|
||||
local Crc32 = require("@formats/core/Crc32")
|
||||
local Err = require("@formats/core/Err")
|
||||
local Types = require("@formats/Types")
|
||||
local Fx = require("@tests/gen/qoi")
|
||||
|
||||
local t = T.suite("qoi")
|
||||
|
||||
t:eq(Qoi.name, "qoi", "name")
|
||||
t:check(Qoi.caps.decode and Qoi.caps.encode, "caps")
|
||||
|
||||
local same = 0
|
||||
for _, c in Fx do
|
||||
local img, err = Qoi.decode(Bytes.reader(buffer.fromstring(c.data)))
|
||||
t:check(img, c.name .. ": " .. tostring(err))
|
||||
t:check(img.width == c.width and img.height == c.height and img.format == "rgba8", c.name .. ": size")
|
||||
t:eq(Crc32.of(img.pixels), c.crc, c.name .. ": pixels against Pillow")
|
||||
-- encode the source pixels: the same bytes as Pillow's writer
|
||||
local src = Types.Image.new(c.width, c.height, if c.channels == 4 then "rgba8" else "rgb8")
|
||||
src.pixels = buffer.fromstring(c.pixels)
|
||||
local enc = Qoi.encode(src, { channels = c.channels, linear = string.byte(c.data, 14) == 1 }) :: buffer -- Pillow tags its files linear
|
||||
if buffer.tostring(enc) == c.data then
|
||||
same += 1
|
||||
end
|
||||
local back = Qoi.decode(Bytes.reader(enc))
|
||||
t:eq(Crc32.of(back.pixels), c.crc, c.name .. ": our encoding round-trips")
|
||||
-- native rgb8
|
||||
if c.channels == 3 then
|
||||
local n = Qoi.decode(Bytes.reader(buffer.fromstring(c.data)), { native = true })
|
||||
t:check(n.format == "rgb8" and buffer.tostring(n.pixels) == c.pixels, c.name .. ": native rgb8")
|
||||
end
|
||||
end
|
||||
t:eq(same, #Fx, "byte-identical to Pillow's encoder")
|
||||
t:note("%d Pillow files decode; %d of our encodings are byte-identical to Pillow's", #Fx, same)
|
||||
|
||||
-- round trips of random images, a sub-range reader, colour space, errors
|
||||
do
|
||||
local rand = T.rng(5)
|
||||
for _, s in { { 1, 1 }, { 70, 3 }, { 33, 33 } } do
|
||||
local img = Types.Image.new(s[1], s[2])
|
||||
for i = 0, s[1] * s[2] * 4 - 1 do
|
||||
buffer.writeu8(img.pixels, i, if rand(3) == 0 then rand(256) else (i // 4) % 256)
|
||||
end
|
||||
local back = Qoi.decode(Bytes.reader(Qoi.encode(img) :: buffer))
|
||||
t:check(buffer.tostring(back.pixels) == buffer.tostring(img.pixels), "random round trip")
|
||||
end
|
||||
local c = Fx[4]
|
||||
local big = buffer.create(#c.data + 9)
|
||||
buffer.writestring(big, 5, c.data)
|
||||
local img = Qoi.decode(Bytes.reader(big, 5, #c.data))
|
||||
t:check(img and Crc32.of(img.pixels) == c.crc, "decode from a sub-range")
|
||||
local lin = Types.Image.new(2, 2)
|
||||
local d = Qoi.encode(lin, { linear = true }) :: buffer
|
||||
t:eq(buffer.readu8(d, 13), 1, "linear tag")
|
||||
t:eq(Qoi.decode(Bytes.reader(d)).meta.source.colorSpace, "linear", "linear read back")
|
||||
t:eq(Qoi.sniff(Bytes.reader(buffer.fromstring(c.data))), 1, "sniff")
|
||||
t:eq(Qoi.sniff(Bytes.reader(buffer.fromstring("GIF8"))), 0, "sniff other")
|
||||
local _, e = Qoi.decode(Bytes.reader(buffer.fromstring(c.data)), { maxPixels = 10 })
|
||||
t:check(e and e.code == Err.LIMIT, "maxPixels")
|
||||
local _, e2 = Qoi.decode(Bytes.reader(buffer.fromstring(string.sub(c.data, 1, 40))))
|
||||
t:check(e2 and e2.code == Err.TRUNCATED, "truncated: " .. tostring(e2))
|
||||
local _, e3 = Qoi.decode(Bytes.reader(buffer.fromstring("qoix" .. string.rep("\0", 20))))
|
||||
t:check(e3 and e3.code == Err.CORRUPT, "bad magic")
|
||||
local _, e4 = Qoi.encode(Types.Image.new(2, 2, "gray8"))
|
||||
t:check(e4 and e4.code == Err.ARG, "encode gray8 -> ARG")
|
||||
end
|
||||
|
||||
do
|
||||
local samples = {}
|
||||
for i, c in Fx do
|
||||
if i % 2 == 0 then
|
||||
table.insert(samples, c.data)
|
||||
end
|
||||
end
|
||||
t:fuzz("decode", function(b: buffer)
|
||||
return Qoi.decode(Bytes.reader(b))
|
||||
end, samples, { keepHeader = 4 })
|
||||
end
|
||||
|
||||
t:done()
|
||||
@@ -0,0 +1,96 @@
|
||||
-- image/tga tests: Pillow- and ImageMagick-written TGAs (RGB / RGBA / grey / grey + alpha / palette, raw and RLE, both
|
||||
-- origins) and hand-written ones (15 / 16-bit pixels and colour maps, first index, right-to-left, RLE across rows, an
|
||||
-- image ID) against the reference RGBA decodes; our encoder (raw / RLE, origins, 24 / 32-bit / grey) round-trips;
|
||||
-- sniffing stays low without a footer; errors; the fuzz no-throw pass. Fixtures: tools/fixtures/tga.py.
|
||||
local T = require("@tests/T")
|
||||
local Tga = require("@formats/image/tga")
|
||||
local Bytes = require("@formats/core/Bytes")
|
||||
local Err = require("@formats/core/Err")
|
||||
local Types = require("@formats/Types")
|
||||
local Fx = require("@tests/gen/tga")
|
||||
|
||||
local t = T.suite("tga")
|
||||
|
||||
t:eq(Tga.name, "tga", "name")
|
||||
t:check(Tga.caps.decode and Tga.caps.encode, "caps")
|
||||
|
||||
local function dec(data: any, opts: any?): (any, any)
|
||||
return Tga.decode(Bytes.reader(if type(data) == "string" then buffer.fromstring(data) else data), opts)
|
||||
end
|
||||
|
||||
for _, c in Fx do
|
||||
local img, err = dec(c.data)
|
||||
t:check(img, c.name .. ": " .. tostring(err))
|
||||
t:check(img.width == c.width and img.height == c.height, c.name .. ": size")
|
||||
local worst, at = 0, -1
|
||||
for i = 0, #c.rgba - 1 do
|
||||
local d = math.abs(buffer.readu8(img.pixels, i) - string.byte(c.rgba, i + 1))
|
||||
if d > worst then
|
||||
worst, at = d, i
|
||||
end
|
||||
end
|
||||
t:check(worst == 0, string.format("%s: differs from the reference by %d at byte %d", c.name, worst, at))
|
||||
t:check(not img.meta.truncated, c.name .. ": not truncated")
|
||||
-- round trips through our encoder
|
||||
for _, rle in { false, true } do
|
||||
for _, origin in { "bottom-left", "top-left" } do
|
||||
local enc = Tga.encode(img, { rle = rle, origin = origin }) :: buffer
|
||||
local back = dec(enc)
|
||||
t:check(back and buffer.tostring(back.pixels) == buffer.tostring(img.pixels), string.format("%s: round trip rle=%s %s", c.name, tostring(rle), origin))
|
||||
end
|
||||
end
|
||||
end
|
||||
t:note("%d files match the reference decodes and round-trip", #Fx)
|
||||
|
||||
do
|
||||
local byName = {}
|
||||
for _, c in Fx do
|
||||
byName[c.name] = c
|
||||
end
|
||||
local id = dec(byName["hand-id-top"].data)
|
||||
t:check(id.meta.text and id.meta.text.id == "hello", "image ID")
|
||||
local g = dec(byName["pil-L-raw-top"].data, { native = true })
|
||||
t:eq(g.format, "gray8", "native grey")
|
||||
t:eq(Tga.encode(g) and buffer.readu8(Tga.encode(g) :: buffer, 2), 3, "gray8 encodes as type 3")
|
||||
local rgb = dec(byName["pil-RGB-raw-top"].data, { native = true })
|
||||
t:eq(rgb.format, "rgb8", "native rgb8")
|
||||
local p = dec(byName["pil-P-raw-top"].data)
|
||||
t:check(p.meta.palette and #p.meta.palette > 0, "palette in meta")
|
||||
-- sniff: low without a footer, higher with ours, never for other formats
|
||||
t:eq(Tga.sniff(Bytes.reader(buffer.fromstring(byName["hand-16bit"].data))), 0.2, "sniff without a footer")
|
||||
t:eq(Tga.sniff(Bytes.reader(Tga.encode(p) :: buffer)), 0.9, "sniff with the footer")
|
||||
t:eq(Tga.sniff(Bytes.reader(buffer.fromstring("\0\0\1\0\1\0\16\16\0\0\1\0\32\0\0\0\0\0\0\0\0\0"))), 0, "an ICO header is not a TGA")
|
||||
t:eq(Tga.sniff(Bytes.reader(buffer.fromstring("\137PNG\r\n\26\n\0\0\0\13IHDR\0\0\0\0\0\0"))), 0, "a PNG is not a TGA")
|
||||
-- encoder choices
|
||||
local img = Types.Image.new(3, 2)
|
||||
buffer.fill(img.pixels, 0, 255, 24)
|
||||
t:eq(buffer.readu8(Tga.encode(img) :: buffer, 16), 24, "opaque rgba8 -> 24-bit")
|
||||
buffer.writeu8(img.pixels, 3, 0)
|
||||
t:eq(buffer.readu8(Tga.encode(img) :: buffer, 16), 32, "alpha -> 32-bit")
|
||||
local _, e = Tga.encode(img, { origin = "middle" } :: any)
|
||||
t:check(e and e.code == Err.ARG, "bad origin")
|
||||
local _, e2 = Tga.encode(Types.Image.new(2, 2, "gray16"))
|
||||
t:check(e2 and e2.code == Err.ARG, "gray16 refused")
|
||||
-- truncation keeps what is there
|
||||
local s = byName["pil-RGBA-raw-top"].data
|
||||
local tr = dec(string.sub(s, 1, #s // 2))
|
||||
t:check(tr and tr.meta.truncated, "truncated pixels flagged")
|
||||
local _, e3 = dec(string.sub(s, 1, 10))
|
||||
t:check(e3 and e3.code == Err.TRUNCATED, "short header")
|
||||
local _, e4 = dec(s, { maxPixels = 10 })
|
||||
t:check(e4 and e4.code == Err.LIMIT, "maxPixels")
|
||||
end
|
||||
|
||||
do
|
||||
local samples = {}
|
||||
for i, c in Fx do
|
||||
if i % 3 == 0 then
|
||||
table.insert(samples, c.data)
|
||||
end
|
||||
end
|
||||
t:fuzz("decode", function(b: buffer)
|
||||
return Tga.decode(Bytes.reader(b))
|
||||
end, samples)
|
||||
end
|
||||
|
||||
t:done()
|
||||
@@ -0,0 +1,185 @@
|
||||
-- image/tiff tests: files written by tifffile / imagecodecs and ImageMagick (every compression, predictors, strips and
|
||||
-- tiles, planar, both byte orders, BigTIFF, 1..32-bit integer and 16 / 32 / 64-bit float samples, palette, CMYK,
|
||||
-- alpha, multi-page) against tifffile's decode, in rgba8 and native layouts; the JPEG hook; encoder round trips for
|
||||
-- every layout and compression; errors; the fuzz pass. Fixtures: tools/fixtures/tiff.py.
|
||||
local T = require("@tests/T")
|
||||
local Tiff = require("@formats/image/tiff")
|
||||
local Bytes = require("@formats/core/Bytes")
|
||||
local Crc32 = require("@formats/core/Crc32")
|
||||
local Err = require("@formats/core/Err")
|
||||
local Types = require("@formats/Types")
|
||||
local Fx = require("@tests/gen/tiff")
|
||||
|
||||
local t = T.suite("tiff")
|
||||
|
||||
local function decode(data: any, opts: any?): (any, any)
|
||||
local b = if type(data) == "string" then buffer.fromstring(data) else data
|
||||
return Tiff.decode(Bytes.reader(b), opts)
|
||||
end
|
||||
|
||||
-- contract
|
||||
t:eq(Tiff.name, "tiff", "name")
|
||||
t:check(Tiff.caps.decode and Tiff.caps.encode, "caps")
|
||||
t:eq(Tiff.sniff(Bytes.reader(buffer.fromstring("II*\0\8\0\0\0"))), 1, "sniff II")
|
||||
t:eq(Tiff.sniff(Bytes.reader(buffer.fromstring("MM\0*"))), 1, "sniff MM")
|
||||
t:eq(Tiff.sniff(Bytes.reader(buffer.fromstring("\137PNG"))), 0, "sniff png")
|
||||
|
||||
-- reference files
|
||||
local pagesChecked = 0
|
||||
for _, c in Fx.cases do
|
||||
local img, err = decode(c.data)
|
||||
t:check(img, c.name .. ": " .. tostring(err))
|
||||
t:check(img.width == c.width and img.height == c.height and img.format == "rgba8", c.name .. ": size / format")
|
||||
local frames = img.frames or { { image = img } }
|
||||
t:eq(#frames, #c.pages, c.name .. ": pages")
|
||||
local nat = decode(c.data, { native = true })
|
||||
local nframes = nat.frames or { { image = nat } }
|
||||
for i, p in c.pages do
|
||||
local crc = Crc32.of(frames[i].image.pixels)
|
||||
t:check(crc == p.crc, string.format("%s page %d: rgba8 crc %08x, want %08x", c.name, i, crc, p.crc))
|
||||
local ni = nframes[i].image
|
||||
t:eq(ni.format, p.native, c.name .. " page " .. i .. ": native format")
|
||||
local ncrc = Crc32.of(ni.pixels)
|
||||
t:check(ncrc == p.ncrc, string.format("%s page %d: native crc %08x, want %08x", c.name, i, ncrc, p.ncrc))
|
||||
pagesChecked += 1
|
||||
end
|
||||
-- inside a bigger buffer
|
||||
local big = buffer.create(#c.data + 9)
|
||||
buffer.writestring(big, 5, c.data)
|
||||
local img3 = Tiff.decode(Bytes.reader(big, 5, #c.data))
|
||||
t:check(img3 and Crc32.of(img3.pixels) == c.pages[1].crc, c.name .. ": sub-range decode")
|
||||
end
|
||||
t:note("%d files, %d pages match tifffile", #Fx.cases, pagesChecked)
|
||||
|
||||
-- info, single page, meta
|
||||
do
|
||||
local multi
|
||||
for _, c in Fx.cases do
|
||||
if c.name == "pages3" then
|
||||
multi = c
|
||||
end
|
||||
end
|
||||
local info = Tiff.info(Bytes.reader(buffer.fromstring(multi.data)))
|
||||
t:check(info and info.pages == 3 and info.width == multi.width, "info")
|
||||
local p2 = decode(multi.data, { page = 2, native = true })
|
||||
t:check(p2 and p2.format == "gray16" and Crc32.of(p2.pixels) == multi.pages[2].ncrc, "opts.page")
|
||||
local _, e = decode(multi.data, { page = 9 })
|
||||
t:check(e and e.code == Err.ARG, "missing page")
|
||||
local _, e2 = decode(multi.data, { maxPixels = 10 })
|
||||
t:check(e2 and e2.code == Err.LIMIT, "maxPixels")
|
||||
local img = decode(multi.data)
|
||||
t:check(img.meta.source.format == "tiff" and img.meta.source.compression == "deflate", "meta.source")
|
||||
end
|
||||
|
||||
-- JPEG-compressed: needs the hook; the hook gets one stitched JPEG stream per strip
|
||||
do
|
||||
local _, e = decode(Fx.jpeg)
|
||||
t:check(e and e.code == Err.UNSUPPORTED, "jpeg without hook: " .. tostring(e))
|
||||
local calls = 0
|
||||
local img = decode(Fx.jpeg, {
|
||||
decodeImage = function(b: buffer)
|
||||
calls += 1
|
||||
t:check(buffer.readu8(b, 0) == 0xFF and buffer.readu8(b, 1) == 0xD8, "stitched stream starts with SOI")
|
||||
local s = buffer.tostring(b)
|
||||
t:check(string.find(s, "\255\218", 1, true) ~= nil, "stitched stream has SOS")
|
||||
t:check(string.find(s, "\255\196", 1, true) ~= nil or string.find(s, "\255\219", 1, true) ~= nil, "stitched stream has tables")
|
||||
local im = Types.Image.new(64, 64)
|
||||
buffer.fill(im.pixels, 0, 200)
|
||||
return im
|
||||
end,
|
||||
})
|
||||
t:check(img and calls > 0, "jpeg through the hook")
|
||||
-- the real thing through the api (image/jpeg decodes the stitched streams): within 2 levels of libjpeg-turbo
|
||||
local F = require("@formats")
|
||||
local ref = Fx.jpegRef
|
||||
local full, ferr = F.load(Fx.jpeg, { format = "tiff" })
|
||||
t:check(full and full.width == ref.width and full.height == ref.height, "jpeg-in-tiff through F.load: " .. tostring(ferr))
|
||||
local worst = 0
|
||||
for i = 0, ref.width * ref.height - 1 do
|
||||
for k = 0, 2 do
|
||||
worst = math.max(worst, math.abs(buffer.readu8(full.pixels, i * 4 + k) - string.byte(ref.rgb, i * 3 + k + 1)))
|
||||
end
|
||||
end
|
||||
t:check(worst <= 2, "jpeg-in-tiff pixels: max error " .. worst .. " against tifffile")
|
||||
t:note("JPEG-in-TIFF through F.load: max error %d against tifffile / libjpeg-turbo", worst)
|
||||
end
|
||||
|
||||
-- encoder round trips
|
||||
do
|
||||
local rand = T.rng(11)
|
||||
local function image(fmt: string, w: number, h: number): Types.Image
|
||||
local img = Types.Image.new(w, h, fmt :: any)
|
||||
local n = buffer.len(img.pixels)
|
||||
if fmt == "rgbaf32" then
|
||||
for i = 0, n // 4 - 1 do
|
||||
buffer.writef32(img.pixels, i * 4, (rand(2000) - 300) / 1000)
|
||||
end
|
||||
else
|
||||
for i = 0, n - 1 do
|
||||
buffer.writeu8(img.pixels, i, if rand(3) == 0 then rand(256) else (i * 7) % 256)
|
||||
end
|
||||
end
|
||||
return img
|
||||
end
|
||||
local trips = 0
|
||||
for _, fmt in { "rgba8", "rgb8", "gray8", "graya8", "gray16", "rgba16", "rgbaf32" } do
|
||||
for _, comp in { "none", "deflate", "lzw", "packbits" } do
|
||||
for _, pred in { true, false } do
|
||||
for _, size in { { 1, 1 }, { 13, 7 }, { 200, 90 } } do
|
||||
local img = image(fmt, size[1], size[2])
|
||||
img.meta.dpi = { x = 300, y = 150 }
|
||||
local out, err = Tiff.encode(img, { compression = comp :: any, predictor = pred, rowsPerStrip = if size[1] == 200 then 16 else nil })
|
||||
t:check(out, fmt .. " " .. comp .. ": " .. tostring(err))
|
||||
local back, berr = Tiff.decode(Bytes.reader(out :: buffer), { native = true })
|
||||
t:check(back, fmt .. " " .. comp .. ": decode: " .. tostring(berr))
|
||||
t:eq(back.format, fmt, fmt .. " " .. comp .. ": format")
|
||||
t:check(buffer.tostring(back.pixels) == buffer.tostring(img.pixels), string.format("%s %s pred=%s %dx%d: round trip", fmt, comp, tostring(pred), size[1], size[2]))
|
||||
t:check(back.meta.dpi and math.abs(back.meta.dpi.x - 300) < 0.01 and math.abs(back.meta.dpi.y - 150) < 0.01, "dpi")
|
||||
trips += 1
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
t:note("%d encoder round trips", trips)
|
||||
-- multi-page
|
||||
local a, b = image("rgb8", 20, 10), image("gray16", 7, 9)
|
||||
local anim = image("rgb8", 20, 10)
|
||||
anim.frames = { { image = a, delay = 0, x = 0, y = 0, dispose = "none", blend = "source" }, { image = b, delay = 0, x = 0, y = 0, dispose = "none", blend = "source" } } :: any
|
||||
local out = Tiff.encode(anim, { compression = "lzw" }) :: buffer
|
||||
local back = Tiff.decode(Bytes.reader(out), { native = true })
|
||||
t:check(back.frames and #back.frames == 2, "multi-page count")
|
||||
t:check(buffer.tostring(back.frames[1].image.pixels) == buffer.tostring(a.pixels), "page 1")
|
||||
t:check(buffer.tostring(back.frames[2].image.pixels) == buffer.tostring(b.pixels) and back.frames[2].image.format == "gray16", "page 2")
|
||||
local _, e = Tiff.encode({} :: any)
|
||||
t:check(e and e.code == Err.ARG, "encode non-image")
|
||||
local _, e2 = Tiff.encode(image("rgb8", 2, 2), { compression = "jpeg" } :: any)
|
||||
t:check(e2 and e2.code == Err.ARG, "bad compression")
|
||||
local _, e3 = Tiff.encode(Types.Image.new(2, 2, "yuv420p"))
|
||||
t:check(e3 and e3.code == Err.ARG, "unsupported layout")
|
||||
end
|
||||
|
||||
-- errors
|
||||
do
|
||||
local _, e = decode("II*\0\255\255\0\0")
|
||||
t:check(e and (e.code == Err.TRUNCATED or e.code == Err.CORRUPT), "bad IFD offset")
|
||||
local _, e2 = decode("XX*\0")
|
||||
t:check(e2 and e2.code == Err.TRUNCATED or e2.code == Err.CORRUPT, "junk")
|
||||
end
|
||||
|
||||
-- fuzz
|
||||
do
|
||||
local samples = {}
|
||||
for i, c in Fx.cases do
|
||||
if i % 3 == 0 and #c.data < 40000 then
|
||||
table.insert(samples, c.data)
|
||||
end
|
||||
end
|
||||
t:fuzz("decode", function(b: buffer)
|
||||
return Tiff.decode(Bytes.reader(b))
|
||||
end, samples, { mutations = 12 })
|
||||
t:fuzz("decode native", function(b: buffer)
|
||||
return Tiff.decode(Bytes.reader(b), { native = true })
|
||||
end, samples, { mutations = 4, garbage = 50 })
|
||||
end
|
||||
|
||||
t:done()
|
||||
@@ -0,0 +1,321 @@
|
||||
-- ops/ImageOps tests: against Pillow (tools/fixtures/imageops.py): grey conversion (exact), resize nearest / bilinear /
|
||||
-- Lanczos up and down (max error), rotate with bilinear + expand (PSNR), alpha_composite (exact); property tests for
|
||||
-- every pixel format (conversion round trips, flips, quarter turns, crop, premultiply, pixel access, fill), blit
|
||||
-- clipping and blend modes, animation helpers (coalesce with each disposal, fromFrames, durations), and a pass of bad
|
||||
-- arguments that must return nil, Err without throwing.
|
||||
local T = require("@tests/T")
|
||||
local Ops = require("@formats/ops/ImageOps")
|
||||
local Types = require("@formats/Types")
|
||||
local Err = require("@formats/core/Err")
|
||||
local Fx = require("@tests/gen/imageops")
|
||||
|
||||
local t = T.suite("imageops")
|
||||
|
||||
local function image(w: number, h: number, fmt: string, px: string): any
|
||||
local img = Types.Image.new(w, h, fmt :: any)
|
||||
buffer.writestring(img.pixels, 0, px)
|
||||
return img
|
||||
end
|
||||
|
||||
-- max abs difference and PSNR of two byte strings / buffers
|
||||
local function diff(a: buffer, b: string): (number, number)
|
||||
local n = #b
|
||||
local mx, se = 0, 0
|
||||
for i = 0, n - 1 do
|
||||
local d = math.abs(buffer.readu8(a, i) - string.byte(b, i + 1))
|
||||
if d > mx then
|
||||
mx = d
|
||||
end
|
||||
se += d * d
|
||||
end
|
||||
local mse = se / n
|
||||
return mx, if mse == 0 then 99 else 10 * math.log10(255 * 255 / mse)
|
||||
end
|
||||
|
||||
local rgb = image(Fx.rgb.w, Fx.rgb.h, "rgb8", Fx.rgb.px)
|
||||
local rgba = image(Fx.rgba.w, Fx.rgba.h, "rgba8", Fx.rgba.px)
|
||||
local rgba2 = image(Fx.rgba2.w, Fx.rgba2.h, "rgba8", Fx.rgba2.px)
|
||||
|
||||
-- Pillow references
|
||||
do
|
||||
local g = Ops.convert(rgb, "gray8") :: any
|
||||
t:check(buffer.tostring(g.pixels) == Fx.gray, "rgb8 -> gray8 matches Pillow convert('L')")
|
||||
local ga = Ops.convert(rgba, "gray8") :: any
|
||||
t:check(buffer.tostring(ga.pixels) == Fx.graya, "rgba8 -> gray8 matches Pillow (alpha ignored)")
|
||||
local gray = Ops.convert(rgb, "gray8") :: any
|
||||
local worst = { nearest = 0, bilinear = 0, lanczos = 0 }
|
||||
for _, c in Fx.resize do
|
||||
local src = if c.mode == "RGB" then rgb else gray
|
||||
local r = Ops.resize(src, c.w, c.h, c.filter) :: any
|
||||
t:check(r and r.width == c.w and r.height == c.h, "resize size")
|
||||
local mx = diff(r.pixels, c.px)
|
||||
worst[c.filter] = math.max(worst[c.filter], mx)
|
||||
t:check(mx <= (if c.filter == "nearest" then 0 else 1), string.format("resize %s %s %dx%d: max error %d", c.filter, c.mode, c.w, c.h, mx))
|
||||
end
|
||||
t:note("resize vs Pillow (%d cases): max error nearest %d, bilinear %d, lanczos %d", #Fx.resize, worst.nearest, worst.bilinear, worst.lanczos)
|
||||
local minPsnr = 99
|
||||
for _, c in Fx.rotate do
|
||||
local r = Ops.rotate(rgba, c.angle, { expand = c.expand }) :: any
|
||||
t:check(r and r.width == c.w and r.height == c.h, string.format("rotate %d: size %dx%d, Pillow %dx%d", c.angle, r.width, r.height, c.w, c.h))
|
||||
local _, psnr = diff(r.pixels, c.px)
|
||||
minPsnr = math.min(minPsnr, psnr)
|
||||
t:check(psnr > 40, string.format("rotate %d: PSNR %.1f", c.angle, psnr))
|
||||
end
|
||||
t:note("rotate vs Pillow (%d cases): min PSNR %.1f dB", #Fx.rotate, minPsnr)
|
||||
local c = Ops.composite(rgba, rgba2) :: any
|
||||
local mx = diff(c.pixels, Fx.composite)
|
||||
t:check(mx == 0, "alpha_composite matches Pillow exactly (max error " .. mx .. ")")
|
||||
end
|
||||
|
||||
-- conversions between every format
|
||||
local FORMATS = { "rgba8", "rgba16", "rgbaf32", "rgb8", "gray8", "gray16", "graya8", "alpha8", "yuv420p" }
|
||||
do
|
||||
for _, f in FORMATS do
|
||||
local c = Ops.convert(rgba, f) :: any
|
||||
t:check(c and c.format == f and c.width == rgba.width, "convert to " .. f)
|
||||
local back = Ops.convert(c, "rgba8") :: any
|
||||
t:check(back and back.format == "rgba8", f .. " back to rgba8")
|
||||
if f == "rgba16" or f == "rgbaf32" then
|
||||
t:check(buffer.tostring(back.pixels) == buffer.tostring(rgba.pixels), f .. ": lossless round trip")
|
||||
end
|
||||
if f == "yuv420p" then
|
||||
-- a smooth image (4:2:0 drops chroma detail)
|
||||
local sm = Types.Image.new(40, 30)
|
||||
for i = 0, 40 * 30 - 1 do
|
||||
local x, y = i % 40, i // 40
|
||||
buffer.writeu32(sm.pixels, i * 4, 0xFF000000 + (x * 6) + (y * 8) * 256 + ((x + y) * 3) * 65536)
|
||||
end
|
||||
local yb = Ops.convert(Ops.convert(sm, "yuv420p") :: any, "rgba8") :: any
|
||||
local _, psnr = diff(yb.pixels, buffer.tostring(sm.pixels))
|
||||
t:check(psnr > 38, string.format("yuv420p round trip PSNR %.1f", psnr))
|
||||
end
|
||||
for _, g in FORMATS do
|
||||
t:check(Ops.convert(c, g) ~= nil, f .. " -> " .. g)
|
||||
end
|
||||
end
|
||||
local g16 = Ops.convert(Ops.convert(rgb, "gray8") :: any, "gray16") :: any
|
||||
t:eq(buffer.readu16(g16.pixels, 0), buffer.readu8((Ops.convert(rgb, "gray8") :: any).pixels, 0) * 257, "8 -> 16 bit is * 257")
|
||||
local f = Types.Image.new(1, 1, "rgbaf32")
|
||||
buffer.writef32(f.pixels, 0, 1.5)
|
||||
buffer.writef32(f.pixels, 4, -0.2)
|
||||
buffer.writef32(f.pixels, 8, 0.5)
|
||||
buffer.writef32(f.pixels, 12, 1)
|
||||
local b = Ops.convert(f, "rgba8") :: any
|
||||
t:check(buffer.readu8(b.pixels, 0) == 255 and buffer.readu8(b.pixels, 1) == 0 and buffer.readu8(b.pixels, 2) == 128, "float -> 8 bit clamps and rounds")
|
||||
end
|
||||
|
||||
-- geometry properties on every format
|
||||
do
|
||||
for _, f in FORMATS do
|
||||
local img = Ops.convert(rgba, f) :: any
|
||||
local s = buffer.tostring(img.pixels)
|
||||
local r = img
|
||||
for _ = 1, 4 do
|
||||
r = Ops.rotate90(r, 1)
|
||||
end
|
||||
if f ~= "yuv420p" then
|
||||
t:check(buffer.tostring(r.pixels) == s, f .. ": four quarter turns are the identity")
|
||||
t:check(buffer.tostring((Ops.flip(Ops.flip(img, "h") :: any, "h") :: any).pixels) == s, f .. ": flip h twice")
|
||||
t:check(buffer.tostring((Ops.flip(Ops.flip(img, "v") :: any, "v") :: any).pixels) == s, f .. ": flip v twice")
|
||||
local q = Ops.rotate90(img, 1) :: any
|
||||
t:check(q.width == img.height and q.height == img.width, f .. ": a quarter turn swaps the size")
|
||||
t:check(buffer.tostring((Ops.rotate90(img, 2) :: any).pixels) == buffer.tostring((Ops.flip(Ops.flip(img, "h") :: any, "v") :: any).pixels), f .. ": half turn = both flips")
|
||||
t:check(buffer.tostring((Ops.rotate(img, 90) :: any).pixels) == buffer.tostring(q.pixels), f .. ": rotate 90 = one quarter turn")
|
||||
end
|
||||
local c = Ops.crop(img, -5, 10, 20, 100) :: any
|
||||
t:check(c and c.width == 15 and c.height == img.height - 10, f .. ": crop clipped")
|
||||
for _, filter in { "nearest", "bilinear", "lanczos" } do
|
||||
local z = Ops.resize(img, 23, 31, filter) :: any
|
||||
t:check(z and z.width == 23 and z.height == 31 and z.format == f, f .. ": resize " .. filter)
|
||||
end
|
||||
local rr = Ops.rotate(img, 33) :: any
|
||||
t:check(rr and rr.format == f, f .. ": rotate 33")
|
||||
end
|
||||
-- crop content, nearest identity, ccw convention
|
||||
local c = Ops.crop(rgba, 3, 4, 5, 6) :: any
|
||||
t:check(buffer.readu32(c.pixels, 0) == buffer.readu32(rgba.pixels, (4 * rgba.width + 3) * 4), "crop origin")
|
||||
t:check(buffer.tostring((Ops.resize(rgba, rgba.width, rgba.height, "lanczos") :: any).pixels) == buffer.tostring(rgba.pixels), "same-size resize is a copy")
|
||||
local q = Ops.rotate90(rgba, 1) :: any -- ccw: the top-right pixel goes to the top-left
|
||||
t:eq(buffer.readu32(q.pixels, 0), buffer.readu32(rgba.pixels, (rgba.width - 1) * 4), "rotate90 is counter-clockwise")
|
||||
end
|
||||
|
||||
-- pixels, fill, premultiply
|
||||
do
|
||||
for _, f in FORMATS do
|
||||
local img = Ops.new(3, 2, f) :: any
|
||||
t:check(img and img.format == f, "new " .. f)
|
||||
if f ~= "yuv420p" then
|
||||
local max = if f == "rgba16" or f == "gray16" then 65535 elseif f == "rgbaf32" then 1 else 255
|
||||
t:check(Ops.setPixel(img, 2, 1, max, max, max, max), f .. ": setPixel")
|
||||
local r, g, b, a = Ops.getPixel(img, 2, 1)
|
||||
t:check(r == max and g == max and b == max and a == max, f .. ": getPixel after setPixel")
|
||||
Ops.fill(img, 0, 0, 0, 0)
|
||||
local r2 = Ops.getPixel(img, 2, 1)
|
||||
t:check(r2 == (if f == "alpha8" then 255 else 0), f .. ": fill")
|
||||
else
|
||||
t:check(Ops.setPixel(img, 0, 0, 1, 2, 3) == nil, "setPixel refuses yuv420p")
|
||||
t:check(Ops.getPixel(img, 2, 1) ~= nil, "getPixel on yuv420p")
|
||||
end
|
||||
end
|
||||
local p = Ops.premultiply(rgba) :: any
|
||||
local u = Ops.unpremultiply(p) :: any
|
||||
local worst = 0
|
||||
for i = 0, rgba.width * rgba.height - 1 do
|
||||
local a = buffer.readu8(rgba.pixels, i * 4 + 3)
|
||||
t:check(buffer.readu8(p.pixels, i * 4) <= a, "premultiplied <= alpha")
|
||||
if a >= 32 then
|
||||
for c = 0, 2 do
|
||||
worst = math.max(worst, math.abs(buffer.readu8(u.pixels, i * 4 + c) - buffer.readu8(rgba.pixels, i * 4 + c)))
|
||||
end
|
||||
end
|
||||
end
|
||||
t:check(worst <= 4, "premultiply round trip error " .. worst .. " (alpha >= 32)")
|
||||
t:note("premultiply round trip: max error %d for alpha >= 32", worst)
|
||||
end
|
||||
|
||||
-- blit
|
||||
do
|
||||
local dst = Ops.new(10, 10, "rgba8") :: any
|
||||
Ops.fill(dst, 10, 20, 30, 255)
|
||||
local src = Ops.new(4, 4, "rgba8") :: any
|
||||
Ops.fill(src, 200, 100, 50, 255)
|
||||
Ops.blit(dst, src, -2, 8)
|
||||
t:check(select(1, Ops.getPixel(dst, 0, 8)) == 200 and select(1, Ops.getPixel(dst, 1, 9)) == 200, "blit clipped at the left / bottom")
|
||||
t:check(select(1, Ops.getPixel(dst, 2, 8)) == 10 and select(1, Ops.getPixel(dst, 0, 7)) == 10, "blit stays inside its rectangle")
|
||||
Ops.fill(src, 0, 0, 0, 0)
|
||||
Ops.blit(dst, src, 0, 0)
|
||||
t:check(select(1, Ops.getPixel(dst, 0, 0)) == 10, "over with a transparent source keeps dst")
|
||||
Ops.blit(dst, src, 0, 0, { blend = "source" })
|
||||
t:check(select(4, Ops.getPixel(dst, 0, 0)) == 0, "source blend copies alpha")
|
||||
Ops.fill(src, 255, 255, 255, 255)
|
||||
Ops.blit(dst, src, 5, 5, { opacity = 0.5 })
|
||||
local r = Ops.getPixel(dst, 5, 5)
|
||||
t:check(math.abs(r - 133) <= 1, "opacity 0.5 over (10 -> " .. r .. ")")
|
||||
-- rgb8 destination, gray source, float destination
|
||||
local d3 = Ops.convert(dst, "rgb8") :: any
|
||||
t:check(Ops.blit(d3, Ops.convert(src, "graya8") :: any, 1, 1) == d3, "blit onto rgb8")
|
||||
local df = Ops.convert(dst, "rgbaf32") :: any
|
||||
t:check(Ops.blit(df, src, 0, 0, { opacity = 0.25 }) == df, "blit onto rgbaf32")
|
||||
local dy = Ops.convert(dst, "yuv420p") :: any
|
||||
t:check(Ops.blit(dy, src, 3, 3) == dy, "blit onto yuv420p")
|
||||
end
|
||||
|
||||
-- animation helpers
|
||||
do
|
||||
local W, H = 6, 4
|
||||
local function solid(w: number, h: number, r: number, a: number): any
|
||||
local im = Ops.new(w, h, "rgba8") :: any
|
||||
Ops.fill(im, r, 0, 0, a)
|
||||
return im
|
||||
end
|
||||
local anim = Ops.new(W, H, "rgba8") :: any
|
||||
anim.frames = {
|
||||
{ image = solid(W, H, 10, 255), delay = 0.1, x = 0, y = 0, dispose = "none", blend = "source" },
|
||||
{ image = solid(2, 2, 20, 255), delay = 0.2, x = 1, y = 1, dispose = "background", blend = "over" },
|
||||
{ image = solid(2, 2, 30, 255), delay = 0.3, x = 3, y = 1, dispose = "previous", blend = "over" },
|
||||
{ image = solid(1, 1, 40, 128), delay = 0.4, x = 5, y = 3, dispose = "none", blend = "over" },
|
||||
}
|
||||
local fr = Ops.coalesce(anim) :: any
|
||||
t:eq(#fr, 4, "coalesce frame count")
|
||||
local function at(i: number, x: number, y: number): (number, number)
|
||||
local r, _, _, a = Ops.getPixel(fr[i], x, y)
|
||||
return r, a
|
||||
end
|
||||
t:check(at(1, 0, 0) == 10 and at(1, 1, 1) == 10, "frame 1")
|
||||
t:check(at(2, 1, 1) == 20 and at(2, 2, 2) == 20 and at(2, 0, 0) == 10, "frame 2 drawn at 1, 1")
|
||||
local r3, a3 = at(3, 1, 1)
|
||||
t:check(r3 == 0 and a3 == 0, "frame 2 disposed to background before frame 3")
|
||||
t:check(at(3, 3, 1) == 30, "frame 3 drawn")
|
||||
t:check(at(4, 3, 1) == 10, "frame 3 disposed to previous before frame 4")
|
||||
local r4 = at(4, 5, 3)
|
||||
t:check(r4 > 10 and r4 < 40, "frame 4 blends over")
|
||||
t:eq(Ops.frameCount(anim), 4, "frameCount")
|
||||
t:eq(Ops.durations(anim)[3], 0.3, "durations")
|
||||
t:eq(Ops.frameCount(rgba), 1, "frameCount of a still")
|
||||
t:check((Ops.coalesce(rgba) :: any)[1] == rgba, "coalesce of a still")
|
||||
local made = Ops.fromFrames({ fr[1], fr[2], fr[3] }, { 0.5, 0.25 }) :: any
|
||||
t:check(made and #made.frames == 3 and made.frames[2].delay == 0.25 and made.frames[3].delay == 0.1, "fromFrames delays")
|
||||
t:check(buffer.tostring(made.pixels) == buffer.tostring(fr[1].pixels), "fromFrames pixels = frame 1")
|
||||
local again = Ops.coalesce(made) :: any
|
||||
t:check(buffer.tostring(again[3].pixels) == buffer.tostring(fr[3].pixels), "coalesce(fromFrames) round trip")
|
||||
t:check(Ops.fromFrames({ fr[1], rgba }) == nil, "fromFrames refuses mixed sizes")
|
||||
end
|
||||
|
||||
-- bad arguments: nil, Err, never a throw
|
||||
do
|
||||
local rand = T.rng(5)
|
||||
local junk: { any } = { nil, 0, -1, 1.5, 2 ^ 40, "x", {}, true, 0 / 0, math.huge, -7 }
|
||||
local imgs: { any } = { rgba, rgb, {}, { pixels = buffer.create(1), width = 5, height = 5, format = "rgba8" }, Types.Image.new(1, 1, "alpha8"), Ops.convert(rgba, "yuv420p") }
|
||||
local calls = {
|
||||
function(a: any, b: any, c: any, d: any)
|
||||
return Ops.convert(a, b)
|
||||
end,
|
||||
function(a: any, b: any, c: any, d: any)
|
||||
return Ops.crop(a, b, c, d, b)
|
||||
end,
|
||||
function(a: any, b: any, c: any, _d: any)
|
||||
return Ops.resize(a, b, c, "lanczos")
|
||||
end,
|
||||
function(a: any, b: any, c: any, _d: any)
|
||||
return Ops.resize(a, b, c, b)
|
||||
end,
|
||||
function(a: any, b: any, _c: any, _d: any)
|
||||
return Ops.rotate(a, b)
|
||||
end,
|
||||
function(a: any, b: any, _c: any, _d: any)
|
||||
return Ops.rotate90(a, b)
|
||||
end,
|
||||
function(a: any, b: any, _c: any, _d: any)
|
||||
return Ops.flip(a, b)
|
||||
end,
|
||||
function(a: any, b: any, c: any, d: any)
|
||||
return Ops.blit(a, d, b, c)
|
||||
end,
|
||||
function(a: any, b: any, c: any, d: any)
|
||||
return Ops.blit(a, a, b, c, { opacity = d })
|
||||
end,
|
||||
function(a: any, b: any, c: any, _d: any)
|
||||
return Ops.getPixel(a, b, c)
|
||||
end,
|
||||
function(a: any, b: any, c: any, d: any)
|
||||
return Ops.setPixel(a, b, c, d, d, d)
|
||||
end,
|
||||
function(a: any, b: any, _c: any, _d: any)
|
||||
return Ops.new(a, b, "rgba8")
|
||||
end,
|
||||
function(a: any, _b: any, _c: any, _d: any)
|
||||
return Ops.coalesce(a)
|
||||
end,
|
||||
function(a: any, b: any, _c: any, _d: any)
|
||||
return Ops.fromFrames({ a, b } :: any)
|
||||
end,
|
||||
function(a: any, b: any, c: any, d: any)
|
||||
return Ops.fill(a, b, c, d)
|
||||
end,
|
||||
}
|
||||
local tries, threw = 0, 0
|
||||
for _ = 1, 600 do
|
||||
local f = calls[1 + rand(#calls)]
|
||||
local a = imgs[1 + rand(#imgs)]
|
||||
local args = { junk[1 + rand(#junk)], junk[1 + rand(#junk)], junk[1 + rand(#junk)] }
|
||||
if rand(3) == 0 then
|
||||
args[3] = imgs[1 + rand(#imgs)]
|
||||
end
|
||||
tries += 1
|
||||
local ok, err = pcall(f, a, args[1], args[2], args[3])
|
||||
if not ok then
|
||||
threw += 1
|
||||
print(" THREW: " .. tostring(err))
|
||||
end
|
||||
end
|
||||
t:check(threw == 0, "bad arguments never throw (" .. threw .. " of " .. tries .. ")")
|
||||
t:note("bad arguments: %d calls, none threw", tries)
|
||||
local _, e = Ops.resize(rgba, 100000, 100000)
|
||||
t:check(e and e.code == Err.LIMIT, "resize over the pixel limit")
|
||||
local _, e2 = Ops.crop(rgba, 100, 100, 5, 5)
|
||||
t:check(e2 and e2.code == Err.ARG, "empty crop")
|
||||
local _, e3 = Ops.convert(rgba, "cmyk")
|
||||
t:check(e3 and e3.code == Err.ARG, "unknown format")
|
||||
end
|
||||
|
||||
t:done()
|
||||
Reference in new issue
Block a user