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,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()
|
||||
Reference in new issue
Block a user