290 lines
8.5 KiB
Luau
290 lines
8.5 KiB
Luau
--!strict
|
|
--!native
|
|
--!optimize 2
|
|
-- image/pnm/Codec: the Netpbm sample loops (native; never throws). Written from the Netpbm format pages (pbm, pgm,
|
|
-- ppm, pam, pfm). Reading: binary 8 / 16-bit samples, ASCII samples (P1 digits may run together), P4 bit rows, PFM
|
|
-- floats (either byte order, bottom-to-top rows); packing samples into a Types pixel format with maxval scaling.
|
|
-- Writing: samples of any pixel format extracted at 1..4 channels and 8 or 16 bits. Every read is bounds-checked.
|
|
local readu8, writeu8, readu16, writeu16 = buffer.readu8, buffer.writeu8, buffer.readu16, buffer.writeu16
|
|
local readf32, writef32, readu32, writeu32 = buffer.readf32, buffer.writef32, buffer.readu32, buffer.writeu32
|
|
local byteswap = bit32.byteswap
|
|
|
|
local Codec = {}
|
|
|
|
-- readSamples(b, p, limit, count, mode, out) -> true | false, message: count raw values into out (u16 each).
|
|
-- mode 1 = binary 8-bit, 2 = binary 16-bit big-endian, 3 = ASCII decimal, 4 = ASCII P1 (single digits)
|
|
function Codec.readSamples(b: buffer, p: number, limit: number, count: number, mode: number, out: buffer): (boolean, string?)
|
|
if mode == 1 then
|
|
if p + count > limit then
|
|
return false, "truncated"
|
|
end
|
|
for i = 0, count - 1 do
|
|
writeu16(out, i * 2, readu8(b, p + i))
|
|
end
|
|
return true, nil
|
|
elseif mode == 2 then
|
|
if p + count * 2 > limit then
|
|
return false, "truncated"
|
|
end
|
|
for i = 0, count - 1 do
|
|
writeu16(out, i * 2, readu8(b, p + i * 2) * 256 + readu8(b, p + i * 2 + 1))
|
|
end
|
|
return true, nil
|
|
end
|
|
local i = 0
|
|
while i < count do
|
|
-- skip whitespace and comments
|
|
while p < limit do
|
|
local c = readu8(b, p)
|
|
if c == 35 then -- '#'
|
|
while p < limit and readu8(b, p) ~= 10 and readu8(b, p) ~= 13 do
|
|
p += 1
|
|
end
|
|
elseif c == 32 or (c >= 9 and c <= 13) then
|
|
p += 1
|
|
else
|
|
break
|
|
end
|
|
end
|
|
if p >= limit then
|
|
return false, "truncated"
|
|
end
|
|
local c = readu8(b, p)
|
|
if c < 48 or c > 57 then
|
|
return false, "bad ASCII sample"
|
|
end
|
|
if mode == 4 then
|
|
writeu16(out, i * 2, c - 48)
|
|
p += 1
|
|
else
|
|
local v = 0
|
|
while p < limit do
|
|
local d = readu8(b, p)
|
|
if d < 48 or d > 57 then
|
|
break
|
|
end
|
|
v = math.min(v * 10 + d - 48, 65535)
|
|
p += 1
|
|
end
|
|
writeu16(out, i * 2, v)
|
|
end
|
|
i += 1
|
|
end
|
|
return true, nil
|
|
end
|
|
|
|
-- readBits(b, p, limit, w, h, out) -> ok, message: P4 rows (MSB first, rows padded to bytes) as u16 0 / 1 values
|
|
function Codec.readBits(b: buffer, p: number, limit: number, w: number, h: number, out: buffer): (boolean, string?)
|
|
local rowBytes = (w + 7) // 8
|
|
if p + rowBytes * h > limit then
|
|
return false, "truncated"
|
|
end
|
|
for y = 0, h - 1 do
|
|
local r = p + y * rowBytes
|
|
for x = 0, w - 1 do
|
|
local byte = readu8(b, r + x // 8)
|
|
writeu16(out, (y * w + x) * 2, (byte // 2 ^ (7 - x % 8)) % 2)
|
|
end
|
|
end
|
|
return true, nil
|
|
end
|
|
|
|
-- format codes for pack / extract
|
|
Codec.RGBA8, Codec.RGB8, Codec.GRAY8, Codec.GRAYA8, Codec.RGBA16, Codec.GRAY16, Codec.RGBAF32 = 0, 1, 2, 3, 4, 5, 6
|
|
|
|
-- pack(samples, n, ch, maxval, invert, fmt, dst): n pixels of ch raw samples (u16, 0..maxval; invert: 1 - v for
|
|
-- PBM bits) into dst in pixel format fmt, scaled to the format's range (8-bit: round(v * 255 / maxval))
|
|
function Codec.pack(s: buffer, n: number, ch: number, maxval: number, invert: boolean, fmt: number, dst: buffer)
|
|
local wide = fmt == 4 or fmt == 5
|
|
local top = if wide then 65535 else 255
|
|
local half = maxval // 2
|
|
local function get(i: number): number
|
|
local v = math.min(readu16(s, i * 2), maxval)
|
|
if invert then
|
|
v = maxval - v
|
|
end
|
|
if maxval == top then
|
|
return v
|
|
end
|
|
return (v * top + half) // maxval
|
|
end
|
|
for i = 0, n - 1 do
|
|
local q = i * ch
|
|
local r, g, b, a
|
|
if ch == 1 then
|
|
r = get(q)
|
|
g, b, a = r, r, top
|
|
elseif ch == 2 then
|
|
r = get(q)
|
|
g, b, a = r, r, get(q + 1)
|
|
elseif ch == 3 then
|
|
r, g, b, a = get(q), get(q + 1), get(q + 2), top
|
|
else
|
|
r, g, b, a = get(q), get(q + 1), get(q + 2), get(q + 3)
|
|
end
|
|
if fmt == 0 then
|
|
writeu32(dst, i * 4, r + g * 256 + b * 65536 + (a :: number) * 16777216)
|
|
elseif fmt == 1 then
|
|
writeu8(dst, i * 3, r)
|
|
writeu8(dst, i * 3 + 1, g)
|
|
writeu8(dst, i * 3 + 2, b)
|
|
elseif fmt == 2 then
|
|
writeu8(dst, i, r)
|
|
elseif fmt == 3 then
|
|
writeu8(dst, i * 2, r)
|
|
writeu8(dst, i * 2 + 1, a :: number)
|
|
elseif fmt == 4 then
|
|
writeu16(dst, i * 8, r)
|
|
writeu16(dst, i * 8 + 2, g)
|
|
writeu16(dst, i * 8 + 4, b)
|
|
writeu16(dst, i * 8 + 6, a :: number)
|
|
elseif fmt == 5 then
|
|
writeu16(dst, i * 2, r)
|
|
end
|
|
end
|
|
end
|
|
|
|
-- readFloats(b, p, limit, w, h, ch, le, dst) -> ok, message: PFM rows (bottom-to-top) into rgbaf32 dst
|
|
function Codec.readFloats(b: buffer, p: number, limit: number, w: number, h: number, ch: number, le: boolean, dst: buffer): (boolean, string?)
|
|
if p + w * h * ch * 4 > limit then
|
|
return false, "truncated"
|
|
end
|
|
local tmp = buffer.create(4)
|
|
for y = 0, h - 1 do
|
|
local row = p + (h - 1 - y) * w * ch * 4
|
|
for x = 0, w - 1 do
|
|
local o = (y * w + x) * 16
|
|
for c = 0, ch - 1 do
|
|
local v
|
|
if le then
|
|
v = readf32(b, row + (x * ch + c) * 4)
|
|
else
|
|
writeu32(tmp, 0, byteswap(readu32(b, row + (x * ch + c) * 4)))
|
|
v = readf32(tmp, 0)
|
|
end
|
|
writef32(dst, o + c * 4, v)
|
|
end
|
|
if ch == 1 then
|
|
local v = readf32(dst, o)
|
|
writef32(dst, o + 4, v)
|
|
writef32(dst, o + 8, v)
|
|
end
|
|
writef32(dst, o + 12, 1)
|
|
end
|
|
end
|
|
return true, nil
|
|
end
|
|
|
|
local function f(v: number): number
|
|
if v ~= v then
|
|
return 0
|
|
end
|
|
return math.floor(math.clamp(v, 0, 1) * 65535 + 0.5)
|
|
end
|
|
|
|
-- extract(px, fmt, n, ch, wide) -> u16 samples: n pixels of format fmt as ch channels (1 grey (Rec. 601 luma for
|
|
-- colour sources, Pillow's L = (299 R + 587 G + 114 B) / 1000), 2 grey + alpha, 3 RGB, 4 RGBA), 16-bit if wide
|
|
function Codec.extract(px: buffer, fmt: number, n: number, ch: number, wide: boolean): buffer
|
|
local out = buffer.create(n * ch * 2)
|
|
local srcWide = fmt == 4 or fmt == 5
|
|
local isGray = fmt == 2 or fmt == 3 or fmt == 5
|
|
for i = 0, n - 1 do
|
|
local r, g, b, a
|
|
if fmt == 0 then
|
|
r, g, b, a = readu8(px, i * 4), readu8(px, i * 4 + 1), readu8(px, i * 4 + 2), readu8(px, i * 4 + 3)
|
|
elseif fmt == 1 then
|
|
r, g, b, a = readu8(px, i * 3), readu8(px, i * 3 + 1), readu8(px, i * 3 + 2), 255
|
|
elseif fmt == 2 then
|
|
r = readu8(px, i)
|
|
g, b, a = r, r, 255
|
|
elseif fmt == 3 then
|
|
r, a = readu8(px, i * 2), readu8(px, i * 2 + 1)
|
|
g, b = r, r
|
|
elseif fmt == 4 then
|
|
r, g, b, a = readu16(px, i * 8), readu16(px, i * 8 + 2), readu16(px, i * 8 + 4), readu16(px, i * 8 + 6)
|
|
elseif fmt == 5 then
|
|
r = readu16(px, i * 2)
|
|
g, b, a = r, r, 65535
|
|
else
|
|
r, g, b, a = f(readf32(px, i * 16)), f(readf32(px, i * 16 + 4)), f(readf32(px, i * 16 + 8)), f(readf32(px, i * 16 + 12))
|
|
end
|
|
local w16 = srcWide or fmt == 6
|
|
local gray = r
|
|
if ch <= 2 and not isGray then
|
|
gray = (r * 299 + g * 587 + b * 114) // 1000
|
|
end
|
|
local vals = if ch == 1 then 1 elseif ch == 2 then 2 else ch
|
|
for c = 0, vals - 1 do
|
|
local v
|
|
if ch <= 2 then
|
|
v = if c == 0 then gray else a :: number
|
|
else
|
|
v = if c == 0 then r elseif c == 1 then g elseif c == 2 then b else a :: number
|
|
end
|
|
if wide and not w16 then
|
|
v *= 257
|
|
elseif w16 and not wide then
|
|
v = (v * 255 + 32767) // 65535
|
|
end
|
|
writeu16(out, (i * ch + c) * 2, v)
|
|
end
|
|
end
|
|
return out
|
|
end
|
|
|
|
-- writeSamples(s, count, wide, out, o) -> new offset: binary samples (8-bit or 16-bit big-endian)
|
|
function Codec.writeSamples(s: buffer, count: number, wide: boolean, out: buffer, o: number): number
|
|
if wide then
|
|
for i = 0, count - 1 do
|
|
local v = readu16(s, i * 2)
|
|
writeu8(out, o, v // 256)
|
|
writeu8(out, o + 1, v % 256)
|
|
o += 2
|
|
end
|
|
else
|
|
for i = 0, count - 1 do
|
|
writeu8(out, o, readu16(s, i * 2))
|
|
o += 1
|
|
end
|
|
end
|
|
return o
|
|
end
|
|
|
|
-- writeBits(s, w, h, threshold, out, o) -> new offset: P4 rows from grey samples (below threshold = black = 1)
|
|
function Codec.writeBits(s: buffer, w: number, h: number, threshold: number, out: buffer, o: number): number
|
|
for y = 0, h - 1 do
|
|
local acc, nb = 0, 0
|
|
for x = 0, w - 1 do
|
|
acc = acc * 2 + (if readu16(s, (y * w + x) * 2) < threshold then 1 else 0)
|
|
nb += 1
|
|
if nb == 8 then
|
|
writeu8(out, o, acc)
|
|
o += 1
|
|
acc, nb = 0, 0
|
|
end
|
|
end
|
|
if nb > 0 then
|
|
writeu8(out, o, acc * 2 ^ (8 - nb))
|
|
o += 1
|
|
end
|
|
end
|
|
return o
|
|
end
|
|
|
|
-- writeFloats(px, w, h, ch, out, o) -> new offset: rgbaf32 (ch 3) or its first channel (ch 1) as little-endian PFM
|
|
-- rows, bottom-to-top
|
|
function Codec.writeFloats(px: buffer, w: number, h: number, ch: number, out: buffer, o: number): number
|
|
for y = h - 1, 0, -1 do
|
|
for x = 0, w - 1 do
|
|
local q = (y * w + x) * 16
|
|
for c = 0, ch - 1 do
|
|
writef32(out, o, readf32(px, q + c * 4))
|
|
o += 4
|
|
end
|
|
end
|
|
end
|
|
return o
|
|
end
|
|
|
|
return Codec
|