101 lines
2.8 KiB
Luau
101 lines
2.8 KiB
Luau
--!strict
|
|
--!native
|
|
-- archive/wad/Picture: Doom picture lumps (column posts: topdelta / length / pad / pixels / pad; DeePsea tall patches:
|
|
-- a topdelta not above the previous is relative) and raw flats to rgba8, from the Unofficial Doom Specs and r_data.c;
|
|
-- no code taken. Transparent pixels are 0.
|
|
local readu8, readu16, readi16, readu32, writeu32 = buffer.readu8, buffer.readu16, buffer.readi16, buffer.readu32, buffer.writeu32
|
|
|
|
local Picture = {}
|
|
|
|
function Picture.header(b: buffer, at: number, n: number): (number?, number, number, number)
|
|
if n < 8 then
|
|
return nil, 0, 0, 0
|
|
end
|
|
local w, h = readu16(b, at), readu16(b, at + 2)
|
|
if w == 0 or h == 0 or w > 4096 or h > 4096 or 8 + w * 4 > n then
|
|
return nil, 0, 0, 0
|
|
end
|
|
for x = 0, w - 1 do
|
|
local o = readu32(b, at + 8 + x * 4)
|
|
if o < 8 + w * 4 or o >= n then
|
|
return nil, 0, 0, 0
|
|
end
|
|
end
|
|
return w, h, readi16(b, at + 4), readi16(b, at + 6)
|
|
end
|
|
|
|
function Picture.decode(b: buffer, at: number, n: number, pal: buffer, out: buffer, w: number, h: number): (boolean, string?)
|
|
local stop = at + n
|
|
for x = 0, w - 1 do
|
|
local p = at + readu32(b, at + 8 + x * 4)
|
|
local top = -1
|
|
while true do
|
|
if p >= stop then
|
|
return false, "column " .. x .. " runs past the end"
|
|
end
|
|
local delta = readu8(b, p)
|
|
if delta == 255 then
|
|
break
|
|
end
|
|
if p + 3 > stop then
|
|
return false, "post header past the end"
|
|
end
|
|
local len = readu8(b, p + 1)
|
|
top = if delta <= top then top + delta else delta
|
|
if p + 4 + len > stop then
|
|
return false, "post past the end"
|
|
end
|
|
local src = p + 3
|
|
for k = 0, len - 1 do
|
|
local y = top + k
|
|
if y < h then
|
|
writeu32(out, (y * w + x) * 4, readu32(pal, readu8(b, src + k) * 4))
|
|
end
|
|
end
|
|
p += 4 + len
|
|
end
|
|
end
|
|
return true, nil
|
|
end
|
|
|
|
function Picture.flatSize(n: number): (number?, number)
|
|
if n == 4096 then
|
|
return 64, 64
|
|
elseif n == 4160 then
|
|
return 64, 64 -- Heretic / Hexen: 64 x 65, the last row is padding
|
|
elseif n == 8192 then
|
|
return 64, 128
|
|
elseif n == 12800 then
|
|
return 64, 200 -- Heretic / Hexen scrolling flats
|
|
end
|
|
local s = math.floor(math.sqrt(n) + 0.5)
|
|
if s * s == n and s > 0 then
|
|
return s, s
|
|
end
|
|
if n > 0 and n % 64 == 0 then
|
|
return 64, n // 64
|
|
end
|
|
return nil, 0
|
|
end
|
|
|
|
function Picture.flat(b: buffer, at: number, w: number, h: number, pal: buffer, out: buffer)
|
|
for i = 0, w * h - 1 do
|
|
writeu32(out, i * 4, readu32(pal, readu8(b, at + i) * 4))
|
|
end
|
|
end
|
|
|
|
function Picture.blit(src: buffer, sw: number, sh: number, dst: buffer, dw: number, dh: number, ox: number, oy: number)
|
|
local x0, x1 = math.max(0, -ox), math.min(sw, dw - ox) - 1
|
|
for y = math.max(0, -oy), math.min(sh, dh - oy) - 1 do
|
|
local so, d = y * sw, (y + oy) * dw + ox
|
|
for x = x0, x1 do
|
|
local v = readu32(src, (so + x) * 4)
|
|
if v ~= 0 then
|
|
writeu32(dst, (d + x) * 4, v)
|
|
end
|
|
end
|
|
end
|
|
end
|
|
|
|
return Picture
|