Brush: plane brushes -> polygons (qbsp-style clipping), Quake standard / Valve 220 / brush-primitive projection, optional coplanar merge and CSG hull; tested against ericw-tools qbsp -nofill (area per plane, texels per corner). Patch: Bezier control grids moved out of model/bsp/Quake3, per-axis subdivisions (patchDef2 / patchDef3), normals. Miptex: Quake / GoldSrc miptex decode / encode and the WAD2 / WAD3 directory (model/bsp uses it now). Pixels: grey, keyed, colorList, colormap, paletteBytes. STYLE: layout block and 2a table list the new modules.
319 lines
9.8 KiB
Luau
319 lines
9.8 KiB
Luau
--!strict
|
|
--!native
|
|
--!optimize 2
|
|
-- codecs/image/Pixels: row loops between packed pixel layouts and rgba8 for BMP / DIB, and palette helpers.
|
|
-- Indexed, BGR(A), 5-5-5, bit masks, RLE4 / RLE8; palettes: keyed index (Quake 255), grey ramp, Quake colormap light
|
|
-- levels. From bmp-luau b86aed6 (same author); the encoder loops and palette helpers are new.
|
|
local readu8, readu16, readu32, writeu32, writeu8 = buffer.readu8, buffer.readu16, buffer.readu32, buffer.writeu32, buffer.writeu8
|
|
local band, rshift, bor, lshift = bit32.band, bit32.rshift, bit32.bor, bit32.lshift
|
|
|
|
local Pixels = {}
|
|
|
|
function Pixels.palette(src: buffer, base: number, n: number, bgr: boolean?): buffer
|
|
local pal = buffer.create(1024)
|
|
local m = if base >= 0 then math.clamp(math.min(n, (buffer.len(src) - base) // 3), 0, 256) else 0
|
|
for i = 0, 255 do
|
|
local v = 0xFF000000
|
|
if i < m then
|
|
local o = base + i * 3
|
|
local r, g, b = readu8(src, o), readu8(src, o + 1), readu8(src, o + 2)
|
|
if bgr then
|
|
r, b = b, r
|
|
end
|
|
v = bor(0xFF000000, lshift(b, 16), lshift(g, 8), r)
|
|
end
|
|
writeu32(pal, i * 4, v)
|
|
end
|
|
return pal
|
|
end
|
|
|
|
function Pixels.indexed(src: buffer, base: number, stride: number, bpp: number, dst: buffer, w: number, h: number,
|
|
topDown: boolean, pal: buffer)
|
|
local mask = lshift(1, bpp) - 1
|
|
for r = 0, h - 1 do
|
|
local so = base + r * stride
|
|
local d = (if topDown then r else h - 1 - r) * w * 4
|
|
if bpp == 8 then
|
|
for x = 0, w - 1 do
|
|
writeu32(dst, d + x * 4, readu32(pal, readu8(src, so + x) * 4))
|
|
end
|
|
elseif bpp == 4 then
|
|
for x = 0, w - 1 do
|
|
local b = readu8(src, so + rshift(x, 1))
|
|
local i = if band(x, 1) == 0 then rshift(b, 4) else band(b, 15)
|
|
writeu32(dst, d + x * 4, readu32(pal, i * 4))
|
|
end
|
|
else
|
|
for x = 0, w - 1 do
|
|
local bit = x * bpp
|
|
local b = readu8(src, so + rshift(bit, 3))
|
|
local i = band(rshift(b, 8 - bpp - band(bit, 7)), mask)
|
|
writeu32(dst, d + x * 4, readu32(pal, i * 4))
|
|
end
|
|
end
|
|
end
|
|
end
|
|
|
|
-- 16 bpp X-R-G-B 1-5-5-5 (BI_RGB 16-bit), opaque; 5-bit channels scaled to 8 bits with rounding
|
|
function Pixels.rgb555(src: buffer, base: number, stride: number, dst: buffer, w: number, h: number, topDown: boolean)
|
|
for r = 0, h - 1 do
|
|
local so = base + r * stride
|
|
local d = (if topDown then r else h - 1 - r) * w * 4
|
|
for x = 0, w - 1 do
|
|
local s = readu16(src, so + x * 2)
|
|
local cr, cg, cb = band(rshift(s, 10), 31), band(rshift(s, 5), 31), band(s, 31)
|
|
writeu32(dst, d + x * 4, (cr * 255 + 15) // 31 + ((cg * 255 + 15) // 31) * 256 + ((cb * 255 + 15) // 31) * 65536 + 0xFF000000)
|
|
end
|
|
end
|
|
end
|
|
|
|
-- 24 bpp BI_RGB: B, G, R
|
|
function Pixels.bgr24(src: buffer, base: number, stride: number, dst: buffer, w: number, h: number, topDown: boolean)
|
|
for r = 0, h - 1 do
|
|
local so = base + r * stride
|
|
local d = (if topDown then r else h - 1 - r) * w * 4
|
|
for x = 0, w - 1 do
|
|
local s = so + x * 3
|
|
writeu32(dst, d + x * 4,
|
|
bor(readu8(src, s + 2), lshift(readu8(src, s + 1), 8), lshift(readu8(src, s), 16), 0xFF000000))
|
|
end
|
|
end
|
|
end
|
|
|
|
-- 32 bpp B, G, R, A (BI_RGB, or BITFIELDS with the standard masks)
|
|
function Pixels.bgra32(src: buffer, base: number, stride: number, dst: buffer, w: number, h: number,
|
|
topDown: boolean): number
|
|
local aor = 0
|
|
for r = 0, h - 1 do
|
|
local so = base + r * stride
|
|
local d = (if topDown then r else h - 1 - r) * w * 4
|
|
for x = 0, w - 1 do
|
|
local p = readu32(src, so + x * 4)
|
|
writeu32(dst, d + x * 4, bor(band(p, 0xFF00FF00), band(rshift(p, 16), 0xFF), lshift(band(p, 0xFF), 16)))
|
|
aor = bor(aor, p)
|
|
end
|
|
end
|
|
return rshift(aor, 24)
|
|
end
|
|
|
|
function Pixels.masked(src: buffer, base: number, stride: number, bpp: number, dst: buffer, w: number, h: number,
|
|
topDown: boolean, shr: number, mr: number, tr: buffer, shg: number, mg: number, tg: buffer, shb: number,
|
|
mb: number, tb: buffer, sha: number, ma: number, ta: buffer): number
|
|
local bytes = rshift(bpp, 3)
|
|
local aor = 0
|
|
for r = 0, h - 1 do
|
|
local so = base + r * stride
|
|
local d = (if topDown then r else h - 1 - r) * w * 4
|
|
for x = 0, w - 1 do
|
|
local s = so + x * bytes
|
|
local p
|
|
if bytes == 2 then
|
|
p = readu16(src, s)
|
|
elseif bytes == 4 then
|
|
p = readu32(src, s)
|
|
else
|
|
p = bor(readu16(src, s), lshift(readu8(src, s + 2), 16))
|
|
end
|
|
local a = band(rshift(p, sha), ma)
|
|
aor = bor(aor, a)
|
|
local o = d + x * 4
|
|
writeu8(dst, o, readu8(tr, band(rshift(p, shr), mr)))
|
|
writeu8(dst, o + 1, readu8(tg, band(rshift(p, shg), mg)))
|
|
writeu8(dst, o + 2, readu8(tb, band(rshift(p, shb), mb)))
|
|
writeu8(dst, o + 3, readu8(ta, a))
|
|
end
|
|
end
|
|
return aor
|
|
end
|
|
|
|
-- 32-bit images whose alpha channel is all zero are opaque
|
|
function Pixels.opaque(dst: buffer, n: number)
|
|
for i = 0, n - 1 do
|
|
writeu8(dst, i * 4 + 3, 255)
|
|
end
|
|
end
|
|
|
|
-- RLE8 / RLE4: pixels skipped by a delta or an early end of line / bitmap stay transparent
|
|
function Pixels.rle(src: buffer, pos: number, stop: number, rle4: boolean, dst: buffer, w: number, h: number,
|
|
topDown: boolean, pal: buffer): number
|
|
local x, y = 0, 0
|
|
local written = 0
|
|
local row = (if topDown then 0 else h - 1) * w * 4
|
|
local rowStep = if topDown then w * 4 else -w * 4
|
|
while y < h and pos + 1 < stop do
|
|
local n = readu8(src, pos)
|
|
local v = readu8(src, pos + 1)
|
|
pos += 2
|
|
if n > 0 then
|
|
-- encoded run: n pixels of index v (RLE4: alternating high / low nibble)
|
|
local e = math.min(x + n, w)
|
|
if rle4 then
|
|
local hi, lo = readu32(pal, rshift(v, 4) * 4), readu32(pal, band(v, 15) * 4)
|
|
for i = x, e - 1 do
|
|
writeu32(dst, row + i * 4, if band(i - x, 1) == 0 then hi else lo)
|
|
end
|
|
else
|
|
local c = readu32(pal, v * 4)
|
|
for i = x, e - 1 do
|
|
writeu32(dst, row + i * 4, c)
|
|
end
|
|
end
|
|
if e > x then
|
|
written += e - x
|
|
end
|
|
x += n
|
|
elseif v == 0 then
|
|
-- end of line
|
|
x = 0
|
|
y += 1
|
|
row += rowStep
|
|
elseif v == 1 then
|
|
break -- end of bitmap
|
|
elseif v == 2 then
|
|
-- delta: right dx, up (in file rows) dy
|
|
if pos + 1 >= stop then
|
|
break
|
|
end
|
|
local dx, dy = readu8(src, pos), readu8(src, pos + 1)
|
|
pos += 2
|
|
x += dx
|
|
y += dy
|
|
row += rowStep * dy
|
|
else
|
|
-- absolute run of v pixels, padded to a 16-bit boundary
|
|
local nb = if rle4 then rshift(v + 1, 1) else v
|
|
if pos + nb > stop then
|
|
break
|
|
end
|
|
local e = math.min(x + v, w)
|
|
for i = x, e - 1 do
|
|
local k = i - x
|
|
local idx
|
|
if rle4 then
|
|
local b = readu8(src, pos + rshift(k, 1))
|
|
idx = if band(k, 1) == 0 then rshift(b, 4) else band(b, 15)
|
|
else
|
|
idx = readu8(src, pos + k)
|
|
end
|
|
writeu32(dst, row + i * 4, readu32(pal, idx * 4))
|
|
end
|
|
if e > x then
|
|
written += e - x
|
|
end
|
|
x += v
|
|
pos += nb + band(nb, 1)
|
|
end
|
|
end
|
|
return written
|
|
end
|
|
|
|
function Pixels.encode24(src: buffer, w: number, h: number, dst: buffer, base: number, stride: number)
|
|
for y = 0, h - 1 do
|
|
local s = y * w * 4
|
|
local d = base + (h - 1 - y) * stride
|
|
for x = 0, w - 1 do
|
|
local v = readu32(src, s + x * 4)
|
|
writeu8(dst, d, band(rshift(v, 16), 255))
|
|
writeu8(dst, d + 1, band(rshift(v, 8), 255))
|
|
writeu8(dst, d + 2, band(v, 255))
|
|
d += 3
|
|
end
|
|
end
|
|
end
|
|
|
|
function Pixels.encode32(src: buffer, w: number, h: number, dst: buffer, base: number)
|
|
for y = 0, h - 1 do
|
|
local s = y * w * 4
|
|
local d = base + (h - 1 - y) * w * 4
|
|
for x = 0, w - 1 do
|
|
local v = readu32(src, s + x * 4)
|
|
writeu32(dst, d + x * 4, bor(band(v, 0xFF00FF00), band(rshift(v, 16), 255), lshift(band(v, 255), 16)))
|
|
end
|
|
end
|
|
end
|
|
|
|
function Pixels.encode8(src: buffer, w: number, h: number, dst: buffer, base: number, stride: number, idx: { [number]: number })
|
|
for y = 0, h - 1 do
|
|
local s = y * w * 4
|
|
local d = base + (h - 1 - y) * stride
|
|
for x = 0, w - 1 do
|
|
writeu8(dst, d + x, idx[band(readu32(src, s + x * 4), 0xFFFFFF)] or 0)
|
|
end
|
|
end
|
|
end
|
|
|
|
function Pixels.colours(src: buffer, n: number, max: number): ({ number }, boolean)
|
|
local seen: { [number]: boolean } = {}
|
|
local list: { number } = {}
|
|
local alpha = false
|
|
for i = 0, n - 1 do
|
|
local v = readu32(src, i * 4)
|
|
if v < 0xFF000000 then
|
|
alpha = true
|
|
end
|
|
local c = band(v, 0xFFFFFF)
|
|
if #list <= max and not seen[c] then
|
|
seen[c] = true
|
|
list[#list + 1] = c
|
|
end
|
|
end
|
|
return list, alpha
|
|
end
|
|
|
|
-- a 256-entry rgba8 palette (u32 LE per entry) with entry i = { i, i, i, 255 }
|
|
function Pixels.grey(): buffer
|
|
local pal = buffer.create(1024)
|
|
for i = 0, 255 do
|
|
writeu32(pal, i * 4, 0xFF000000 + i * 0x10101)
|
|
end
|
|
return pal
|
|
end
|
|
|
|
-- a copy of a 1024-byte palette with entry index fully transparent (Quake / GoldSrc: index 255 of sprites, qpics and
|
|
-- '{' textures)
|
|
function Pixels.keyed(pal: buffer, index: number): buffer
|
|
local out = buffer.create(1024)
|
|
buffer.copy(out, 0, pal, 0, math.min(buffer.len(pal), 1024))
|
|
if index >= 0 and index < 256 then
|
|
writeu32(out, index * 4, 0)
|
|
end
|
|
return out
|
|
end
|
|
|
|
-- palette (1024 bytes) -> { { r, g, b, a } } for codecs/image/Quantize
|
|
function Pixels.colorList(pal: buffer, n: number?): { { number } }
|
|
local out: { { number } } = {}
|
|
for i = 0, math.clamp(n or 256, 0, buffer.len(pal) // 4) - 1 do
|
|
out[i + 1] = { readu8(pal, i * 4), readu8(pal, i * 4 + 1), readu8(pal, i * 4 + 2), readu8(pal, i * 4 + 3) }
|
|
end
|
|
return out
|
|
end
|
|
|
|
-- Quake colormap.lmp: rows of 256 palette indices, row 0 brightest .. 63 darkest (32 = unlit); the palette for one
|
|
-- light level, or nil past the end of the map
|
|
function Pixels.colormap(pal: buffer, cmap: buffer, base: number, row: number): buffer?
|
|
local o = base + row * 256
|
|
if row < 0 or base < 0 or o + 256 > buffer.len(cmap) then
|
|
return nil
|
|
end
|
|
local out = buffer.create(1024)
|
|
for i = 0, 255 do
|
|
writeu32(out, i * 4, readu32(pal, readu8(cmap, o + i) * 4))
|
|
end
|
|
return out
|
|
end
|
|
|
|
-- rgba8 palette (1024 bytes) -> 3 * n bytes R, G, B (the Quake / GoldSrc palette layout)
|
|
function Pixels.paletteBytes(pal: buffer, n: number?): buffer
|
|
local m = math.clamp(n or 256, 0, 256)
|
|
local out = buffer.create(m * 3)
|
|
for i = 0, m - 1 do
|
|
writeu8(out, i * 3, readu8(pal, i * 4))
|
|
writeu8(out, i * 3 + 1, readu8(pal, i * 4 + 1))
|
|
writeu8(out, i * 3 + 2, readu8(pal, i * 4 + 2))
|
|
end
|
|
return out
|
|
end
|
|
|
|
return Pixels
|