--!strict --!native --!optimize 2 -- image/hdr/Codec: Radiance RGBE scanlines (Graphics Gems II; rgbe.c): mantissa * 2^(e - 136), no +0.5, as stb_image. local readu8, writeu8, writef32, readf32 = buffer.readu8, buffer.writeu8, buffer.writef32, buffer.readf32 local frexp = math.frexp local Codec = {} -- old-style RLE: 1, 1, 1, n repeats the previous pixel (n << 8 for consecutive markers); new-style: 2, 2, width, planes function Codec.readLine(b: buffer, p: number, limit: number, w: number, row: buffer): (number?, string?) if w >= 8 and w <= 32767 and p + 4 <= limit and readu8(b, p) == 2 and readu8(b, p + 1) == 2 and readu8(b, p + 2) < 128 then if readu8(b, p + 2) * 256 + readu8(b, p + 3) ~= w then return nil, "scanline width mismatch" end p += 4 for c = 0, 3 do local x = 0 while x < w do if p >= limit then return nil, "truncated" end local n = readu8(b, p) p += 1 if n > 128 then n -= 128 if x + n > w or p >= limit then return nil, if p >= limit then "truncated" else "bad run" end local v = readu8(b, p) p += 1 for i = x, x + n - 1 do writeu8(row, i * 4 + c, v) end else if n == 0 or x + n > w then return nil, "bad count" end if p + n > limit then return nil, "truncated" end for i = x, x + n - 1 do writeu8(row, i * 4 + c, readu8(b, p)) p += 1 end end x += n end end return p, nil end -- flat pixels, with old-style runs local x = 0 local shift = 0 while x < w do if p + 4 > limit then return nil, "truncated" end local r, g, bl, e = readu8(b, p), readu8(b, p + 1), readu8(b, p + 2), readu8(b, p + 3) p += 4 if r == 1 and g == 1 and bl == 1 then if x == 0 then return nil, "run at the start of a scanline" end local n = e * 2 ^ shift if x + n > w then return nil, "bad run" end local q = (x - 1) * 4 local pr, pg, pb, pe = readu8(row, q), readu8(row, q + 1), readu8(row, q + 2), readu8(row, q + 3) for i = x, x + n - 1 do writeu8(row, i * 4, pr) writeu8(row, i * 4 + 1, pg) writeu8(row, i * 4 + 2, pb) writeu8(row, i * 4 + 3, pe) end x += n shift += 8 if shift > 24 then return nil, "bad run" end else writeu8(row, x * 4, r) writeu8(row, x * 4 + 1, g) writeu8(row, x * 4 + 2, bl) writeu8(row, x * 4 + 3, e) x += 1 shift = 0 end end return p, nil end function Codec.toFloat(row: buffer, n: number, out: buffer, base: number, step: number) for i = 0, n - 1 do local q = i * 4 local e = readu8(row, q + 3) local o = base + i * step if e == 0 then writef32(out, o, 0) writef32(out, o + 4, 0) writef32(out, o + 8, 0) else local f = 2 ^ (e - 136) writef32(out, o, readu8(row, q) * f) writef32(out, o + 4, readu8(row, q + 1) * f) writef32(out, o + 8, readu8(row, q + 2) * f) end writef32(out, o + 12, 1) end end function Codec.fromFloat(px: buffer, y: number, w: number, row: buffer) for x = 0, w - 1 do local q = (y * w + x) * 16 local r, g, b = readf32(px, q), readf32(px, q + 4), readf32(px, q + 8) if r ~= r or r < 0 then r = 0 end if g ~= g or g < 0 then g = 0 end if b ~= b or b < 0 then b = 0 end local v = math.max(r, g, b) local o = x * 4 if v < 1e-32 or v == math.huge then if v == math.huge then -- clamp infinities to the largest RGBE value writeu8(row, o, if r == math.huge then 255 else 0) writeu8(row, o + 1, if g == math.huge then 255 else 0) writeu8(row, o + 2, if b == math.huge then 255 else 0) writeu8(row, o + 3, 255) else writeu8(row, o, 0) writeu8(row, o + 1, 0) writeu8(row, o + 2, 0) writeu8(row, o + 3, 0) end else local m, e = frexp(v) if e > 127 then writeu8(row, o, 255) writeu8(row, o + 1, 255) writeu8(row, o + 2, 255) writeu8(row, o + 3, 255) else local s = m * 256 / v writeu8(row, o, math.min(255, math.floor(r * s))) writeu8(row, o + 1, math.min(255, math.floor(g * s))) writeu8(row, o + 2, math.min(255, math.floor(b * s))) writeu8(row, o + 3, e + 128) end end end end function Codec.writeLine(row: buffer, w: number, out: buffer, o: number): number writeu8(out, o, 2) writeu8(out, o + 1, 2) writeu8(out, o + 2, w // 256) writeu8(out, o + 3, w % 256) o += 4 for c = 0, 3 do local cur = 0 while cur < w do local begRun = cur local runCount = 0 local oldRun = 0 while runCount < 4 and begRun < w do begRun += runCount oldRun = runCount runCount = 1 if begRun < w then local v = readu8(row, begRun * 4 + c) while begRun + runCount < w and runCount < 127 and readu8(row, (begRun + runCount) * 4 + c) == v do runCount += 1 end end end if oldRun > 1 and oldRun == begRun - cur then writeu8(out, o, 128 + oldRun) writeu8(out, o + 1, readu8(row, cur * 4 + c)) o += 2 cur = begRun end while cur < begRun do local n = math.min(begRun - cur, 128) writeu8(out, o, n) o += 1 for i = cur, cur + n - 1 do writeu8(out, o, readu8(row, i * 4 + c)) o += 1 end cur += n end if runCount >= 4 then writeu8(out, o, 128 + runCount) writeu8(out, o + 1, readu8(row, begRun * 4 + c)) o += 2 cur += runCount end end end return o end return Codec