Files

68 lines
2.5 KiB
Luau

-- codecs/image/Yuv tests: scale (bilinear yuv420p), imageScale, and a toRgba round trip.
local T = require("@tests/T")
local Yuv = require("@rosetta/codecs/image/Yuv")
local Types = require("@rosetta/Types")
local t = T.suite("yuv")
local function planes(w: number, h: number): (number, number)
local cw, ch = (w + 1) // 2, (h + 1) // 2
return w * h, cw * ch
end
-- a w x h yuv420p picture: Y = horizontal ramp, U = 90, V = 200
local function picture(w: number, h: number): buffer
local ny, nc = planes(w, h)
local b = buffer.create(ny + 2 * nc)
for y = 0, h - 1 do
for x = 0, w - 1 do
buffer.writeu8(b, y * w + x, math.floor(x * 255 / math.max(w - 1, 1)))
end
end
buffer.fill(b, ny, 90, nc)
buffer.fill(b, ny + nc, 200, nc)
return b
end
for _, c in { { 64, 48, 32, 24 }, { 800, 600, 480, 360 }, { 33, 17, 13, 9 }, { 20, 20, 20, 20 } } do
local w, h, dw, dh = c[1], c[2], c[3], c[4]
local out = Yuv.scale(picture(w, h), w, h, dw, dh)
local ny, nc = planes(dw, dh)
local label = ("%dx%d -> %dx%d"):format(w, h, dw, dh)
t:eq(buffer.len(out), ny + 2 * nc, label .. ": size")
local mono, flat = true, true
for y = 0, dh - 1 do
local prev = -1
for x = 0, dw - 1 do
local v = buffer.readu8(out, y * dw + x)
mono = mono and v >= prev
prev = v
end
end
for i = 0, nc - 1 do
flat = flat and buffer.readu8(out, ny + i) == 90 and buffer.readu8(out, ny + nc + i) == 200
end
t:check(mono, label .. ": the ramp stays monotonic")
t:check(flat, label .. ": flat chroma stays flat")
t:check(buffer.readu8(out, 0) <= 16 and buffer.readu8(out, dw - 1) >= 239, label .. ": ramp ends")
end
local src = picture(20, 20)
t:eq(buffer.tostring(Yuv.scale(src, 20, 20, 20, 20)), buffer.tostring(src), "identity")
-- a short source gives a zero picture of the right size, no throw
local short = Yuv.scale(buffer.create(10), 64, 48, 32, 24)
t:eq(buffer.len(short), 32 * 24 + 2 * 16 * 12, "short source: size")
-- out reused when big enough
local reuse = buffer.create(32 * 24 + 2 * 16 * 12)
t:check(Yuv.scale(picture(64, 48), 64, 48, 32, 24, reuse) == reuse, "out reused")
local img = Types.Image.new(64, 48, "yuv420p")
img.pixels = picture(64, 48)
img.meta.yuv = { range = "limited", matrix = "bt601" }
local small = Yuv.imageScale(img, 32, 24)
t:check(small.format == "yuv420p" and small.width == 32 and small.height == 24, "imageScale")
t:check(small.meta.yuv and small.meta.yuv.range == "limited", "imageScale keeps meta")
local rgba = Yuv.imageToRgba(small)
t:check(rgba and rgba.width == 32 and rgba.format == "rgba8", "imageScale -> rgba")
t:done()