Files
rosetta/tests/image/tiff.luau
T

186 lines
7.9 KiB
Luau

-- 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("@rosetta/image/tiff")
local Bytes = require("@rosetta/core/Bytes")
local Crc32 = require("@rosetta/core/Crc32")
local Err = require("@rosetta/core/Err")
local Types = require("@rosetta/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("@rosetta")
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()