imgA: merge ws/imgA-simple (tga, qoi, pnm, ico, hdr)
This commit is contained in:
commit
f0f1eb6c47
23 files changed
+3293
No files matched your search
@@ -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,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()
|
||||
Reference in new issue
Block a user