image formats: comments are specs only

This commit is contained in:
4DBug committed 2026-11-01 01:54:53 -05:00
1 parent 0bd7b87d2b
commit b5546e208e
40 files changed
+246 -626

No files matched your search

+2 -12
View File
@@ -1,17 +1,13 @@
--!strict
--!native
--!optimize 2
-- image/hdr/Codec: Radiance RGBE scanlines (native; never throws). Written from the Radiance file format notes
-- (Greg Ward, "Real Pixels", Graphics Gems II) and Bruce Walter's rgbe.c conventions: flat pixels, old-style RLE
-- (1, 1, 1, n repeats the previous pixel, shifted by 8 for consecutive markers) and new-style RLE (2, 2, width, then
-- four run-length coded channel planes). RGBE -> float is mantissa * 2^(e - 136), as rgbe.c, stb_image and
-- ImageMagick (no +0.5 offset); float -> RGBE as rgbe.c float2rgbe (frexp). Every read is bounds-checked.
-- 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 = {}
-- readLine(b, p, limit, w, row) -> new position | nil, message: one scanline into row (w * 4 RGBE bytes)
-- 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
@@ -95,7 +91,6 @@ function Codec.readLine(b: buffer, p: number, limit: number, w: number, row: buf
return p, nil
end
-- toFloat(row, n, out, offsets): n RGBE pixels -> rgbaf32 at out byte offsets dest(i) = base + i * step
function Codec.toFloat(row: buffer, n: number, out: buffer, base: number, step: number)
for i = 0, n - 1 do
local q = i * 4
@@ -115,7 +110,6 @@ function Codec.toFloat(row: buffer, n: number, out: buffer, base: number, step:
end
end
-- fromFloat(px, y, w, row): row y of an rgbaf32 image -> w RGBE pixels (rgbe.c float2rgbe)
function Codec.fromFloat(px: buffer, y: number, w: number, row: buffer)
for x = 0, w - 1 do
local q = (y * w + x) * 16
@@ -162,7 +156,6 @@ function Codec.fromFloat(px: buffer, y: number, w: number, row: buffer)
end
end
-- writeLine(row, w, out, o) -> new offset: new-style RLE (rgbe.c RGBE_WriteBytes_RLE); out needs w * 4 + w + 8
function Codec.writeLine(row: buffer, w: number, out: buffer, o: number): number
writeu8(out, o, 2)
writeu8(out, o + 1, 2)
@@ -172,7 +165,6 @@ function Codec.writeLine(row: buffer, w: number, out: buffer, o: number): number
for c = 0, 3 do
local cur = 0
while cur < w do
-- find the next run of at least 4
local begRun = cur
local runCount = 0
local oldRun = 0
@@ -187,14 +179,12 @@ function Codec.writeLine(row: buffer, w: number, out: buffer, o: number): number
end
end
end
-- a short run just before the long one
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
-- raw bytes up to the run
while cur < begRun do
local n = math.min(begRun - cur, 128)
writeu8(out, o, n)