codecs/model/Brush, codecs/model/Patch, codecs/image/Miptex; Pixels palette helpers
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.
This commit is contained in:
1 parent
01c044805f
commit
5ad934565d
19 files changed
+4520
-923
No files matched your search
@@ -0,0 +1,214 @@
|
||||
--!strict
|
||||
-- codecs/image/Miptex: Quake / GoldSrc mip textures and the WAD2 / WAD3 lump directory, read and write.
|
||||
-- miptex_t of bspfile.h: name[16], width, height, offsets[4], four 8-bit mip levels; GoldSrc adds u16 count + count * 3
|
||||
-- palette bytes after mip 3. wad.h: "WAD2" / "WAD3", numlumps, infotableofs; 32-byte lumpinfo_t. No code taken.
|
||||
local Types = require("@rosetta/Types")
|
||||
local Pixels = require("@rosetta/codecs/image/Pixels")
|
||||
local Quantize = require("@rosetta/codecs/image/Quantize")
|
||||
|
||||
local Miptex = {}
|
||||
|
||||
local readu8, readu32, readi32 = buffer.readu8, buffer.readu32, buffer.readi32
|
||||
|
||||
export type Header = { name: string, width: number, height: number, offsets: { number } }
|
||||
export type Lump = { name: string, offset: number, disksize: number, size: number, type: number, compression: number }
|
||||
export type DecodeOptions = {
|
||||
palette: buffer?, -- 1024-byte rgba8 palette (Pixels.palette) when the texture has none of its own
|
||||
goldsrc: boolean?, -- read the palette stored after mip 3
|
||||
mip: number?, -- 0..3, default 0
|
||||
keyed: boolean?, -- index 255 transparent; default: names starting with '{'
|
||||
maxPixels: number?, -- default 2^24
|
||||
}
|
||||
|
||||
-- lumpinfo_t types (wad.h TYP_*; WAD3: 0x43 is a miptex with its palette, 0x46 a font)
|
||||
Miptex.TYPES = table.freeze({ palette = 0x40, qtex = 0x41, qpic = 0x42, sound = 0x43, miptex = 0x44, font = 0x46 })
|
||||
|
||||
local function cstr(b: buffer, at: number, n: number): string
|
||||
return (string.match(buffer.readstring(b, at, n), "^[^%z]*")) :: string
|
||||
end
|
||||
|
||||
function Miptex.header(b: buffer, at: number, size: number): (Header?, string?)
|
||||
if at < 0 or size < 40 or at + 40 > buffer.len(b) then
|
||||
return nil, "truncated miptex header"
|
||||
end
|
||||
local w, h = readu32(b, at + 16), readu32(b, at + 20)
|
||||
local offs = { readu32(b, at + 24), readu32(b, at + 28), readu32(b, at + 32), readu32(b, at + 36) }
|
||||
return { name = cstr(b, at, 16), width = w, height = h, offsets = offs }, nil
|
||||
end
|
||||
|
||||
-- the GoldSrc palette after mip 3, as a 1024-byte rgba8 palette, or nil when it is missing / out of range
|
||||
function Miptex.ownPalette(b: buffer, at: number, size: number, hd: Header): buffer?
|
||||
local w, h = hd.width, hd.height
|
||||
local p = hd.offsets[4] + (w // 8) * (h // 8)
|
||||
if hd.offsets[4] == 0 or p + 2 > size then
|
||||
return nil
|
||||
end
|
||||
local n = buffer.readu16(b, at + p)
|
||||
if n == 0 or n > 256 or p + 2 + n * 3 > size then
|
||||
return nil
|
||||
end
|
||||
return Pixels.palette(b, at + p + 2, n)
|
||||
end
|
||||
|
||||
-- decodes one mip level to rgba8; without pixels (offsets 0: the texture lives in a WAD) returns nil, "external"
|
||||
function Miptex.decode(b: buffer, at: number, size: number, opts: DecodeOptions?): (Types.Image?, string?)
|
||||
local hd, err = Miptex.header(b, at, size)
|
||||
if not hd then
|
||||
return nil, err
|
||||
end
|
||||
local mip = math.clamp(math.floor(opts and opts.mip or 0), 0, 3)
|
||||
local w, h = hd.width // (2 ^ mip), hd.height // (2 ^ mip)
|
||||
if hd.width == 0 or hd.height == 0 or w == 0 or h == 0 then
|
||||
return nil, "empty miptex"
|
||||
end
|
||||
if hd.width * hd.height > (opts and opts.maxPixels or 2 ^ 24) then
|
||||
return nil, "limit: miptex " .. hd.width .. " x " .. hd.height
|
||||
end
|
||||
local off = hd.offsets[mip + 1]
|
||||
if off == 0 then
|
||||
return nil, "external"
|
||||
end
|
||||
if off + w * h > size or at + off + w * h > buffer.len(b) then
|
||||
return nil, "truncated miptex pixels"
|
||||
end
|
||||
local own = if opts and opts.goldsrc then Miptex.ownPalette(b, at, size, hd) else nil
|
||||
local pal = own or (opts and opts.palette) or Pixels.grey()
|
||||
local keyed = if opts and opts.keyed ~= nil then opts.keyed else string.sub(hd.name, 1, 1) == "{"
|
||||
if keyed then
|
||||
pal = Pixels.keyed(pal, 255)
|
||||
end
|
||||
local img = Types.Image.new(w, h)
|
||||
Pixels.indexed(b, at + off, w, 8, img.pixels, w, h, true, pal)
|
||||
img.meta.source = { format = "miptex", name = hd.name, mip = mip, palette = own ~= nil or (opts ~= nil and opts.palette ~= nil), keyed = keyed or nil }
|
||||
return img, nil
|
||||
end
|
||||
|
||||
local function halve(px: buffer, w: number, h: number): (buffer, number, number)
|
||||
local w2, h2 = math.max(1, w // 2), math.max(1, h // 2)
|
||||
local out = buffer.create(w2 * h2 * 4)
|
||||
for y = 0, h2 - 1 do
|
||||
for x = 0, w2 - 1 do
|
||||
for c = 0, 3 do
|
||||
local s = 0
|
||||
for dy = 0, 1 do
|
||||
for dx = 0, 1 do
|
||||
local sx, sy = math.min(x * 2 + dx, w - 1), math.min(y * 2 + dy, h - 1)
|
||||
s += readu8(px, (sy * w + sx) * 4 + c)
|
||||
end
|
||||
end
|
||||
buffer.writeu8(out, (y * w2 + x) * 4 + c, (s + 2) // 4)
|
||||
end
|
||||
end
|
||||
end
|
||||
return out, w2, h2
|
||||
end
|
||||
|
||||
-- an rgba8 image -> a miptex_t with four mip levels (box filtered, mapped to the nearest palette entry); goldsrc
|
||||
-- appends the palette. palette: 1024-byte rgba8; without it (GoldSrc only) a 256-colour median-cut palette is made.
|
||||
function Miptex.encode(img: Types.Image, name: string, palette: buffer?, goldsrc: boolean?): (buffer?, string?)
|
||||
local w, h = img.width, img.height
|
||||
if w <= 0 or h <= 0 or w % 16 ~= 0 or h % 16 ~= 0 then
|
||||
return nil, "miptex sizes must be multiples of 16"
|
||||
end
|
||||
if #name > 15 then
|
||||
return nil, "miptex name over 15 bytes"
|
||||
end
|
||||
local colors: { { number } }
|
||||
if palette then
|
||||
colors = Pixels.colorList(palette)
|
||||
elseif goldsrc then
|
||||
colors = Quantize.palette(img.pixels, w, h, 256)
|
||||
else
|
||||
return nil, "a Quake miptex needs a palette"
|
||||
end
|
||||
local keyed = string.sub(name, 1, 1) == "{"
|
||||
local levels: { buffer } = {}
|
||||
local px, lw, lh = img.pixels, w, h
|
||||
for m = 0, 3 do
|
||||
if m > 0 then
|
||||
px, lw, lh = halve(px, lw, lh)
|
||||
end
|
||||
local idx = Quantize.map(px, lw, lh, colors, { transparentIndex = if keyed then 255 else nil })
|
||||
if keyed then
|
||||
for k = 0, lw * lh - 1 do
|
||||
if readu8(px, k * 4 + 3) < 128 then
|
||||
buffer.writeu8(idx, k, 255)
|
||||
end
|
||||
end
|
||||
end
|
||||
levels[m + 1] = idx
|
||||
end
|
||||
local total = 40
|
||||
for _, l in levels do
|
||||
total += buffer.len(l)
|
||||
end
|
||||
local palAt = total
|
||||
if goldsrc then
|
||||
total += 2 + 768 + 2
|
||||
end
|
||||
local out = buffer.create(total)
|
||||
buffer.writestring(out, 0, name)
|
||||
buffer.writeu32(out, 16, w)
|
||||
buffer.writeu32(out, 20, h)
|
||||
local o = 40
|
||||
for m, l in levels do
|
||||
buffer.writeu32(out, 24 + (m - 1) * 4, o)
|
||||
buffer.copy(out, o, l)
|
||||
o += buffer.len(l)
|
||||
end
|
||||
if goldsrc then
|
||||
buffer.writeu16(out, palAt, 256)
|
||||
for i = 0, 255 do
|
||||
local c = colors[i + 1] or { 0, 0, 0 }
|
||||
buffer.writeu8(out, palAt + 2 + i * 3, c[1])
|
||||
buffer.writeu8(out, palAt + 3 + i * 3, c[2])
|
||||
buffer.writeu8(out, palAt + 4 + i * 3, c[3])
|
||||
end
|
||||
end
|
||||
return out, nil
|
||||
end
|
||||
|
||||
-- the lump directory of a WAD2 / WAD3 file (offsets relative to base), or nil, message
|
||||
function Miptex.wad(b: buffer, base: number, size: number, maxLumps: number?): ({ Lump }?, string?, string?)
|
||||
if size < 12 or base + 12 > buffer.len(b) then
|
||||
return nil, "truncated header", nil
|
||||
end
|
||||
local magic = buffer.readstring(b, base, 4)
|
||||
if magic ~= "WAD2" and magic ~= "WAD3" then
|
||||
return nil, "bad magic", nil
|
||||
end
|
||||
local n, dir = readi32(b, base + 4), readi32(b, base + 8)
|
||||
if n < 0 or dir < 0 then
|
||||
return nil, "negative directory field", nil
|
||||
end
|
||||
if n > (maxLumps or 1e6) then
|
||||
return nil, "limit: too many lumps: " .. n, nil
|
||||
end
|
||||
if dir + n * 32 > size then
|
||||
return nil, "truncated directory past the end", nil
|
||||
end
|
||||
local out: { Lump } = {}
|
||||
for i = 0, n - 1 do
|
||||
local o = base + dir + i * 32
|
||||
local at, disk, full = readi32(b, o), readi32(b, o + 4), readi32(b, o + 8)
|
||||
if at < 0 or disk < 0 or at + disk > size then
|
||||
return nil, "truncated lump past the end", nil
|
||||
end
|
||||
out[i + 1] = { name = cstr(b, o + 16, 16), offset = at, disksize = disk, size = full, type = readu8(b, o + 12), compression = readu8(b, o + 13) }
|
||||
end
|
||||
return out, nil, magic
|
||||
end
|
||||
|
||||
-- the lump named name (case-insensitive, the last one wins as in W_GetLumpinfo)
|
||||
function Miptex.find(lumps: { Lump }, name: string): Lump?
|
||||
local want = string.lower(name)
|
||||
local best: Lump? = nil
|
||||
for _, l in lumps do
|
||||
if string.lower(l.name) == want then
|
||||
best = l
|
||||
end
|
||||
end
|
||||
return best
|
||||
end
|
||||
|
||||
return Miptex
|
||||
@@ -1,8 +1,9 @@
|
||||
--!strict
|
||||
--!native
|
||||
--!optimize 2
|
||||
-- codecs/image/Pixels: row loops between packed pixel layouts and rgba8 for BMP / DIB.
|
||||
-- Indexed, BGR(A), 5-5-5, bit masks, RLE4 / RLE8. From bmp-luau b86aed6 (same author); the encoder loops are new.
|
||||
-- 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
|
||||
|
||||
@@ -259,4 +260,59 @@ function Pixels.colours(src: buffer, n: number, max: number): ({ number }, boole
|
||||
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
|
||||
Reference in new issue
Block a user