-- 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()