--!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