archive/wad2, image/lmp, image/spr, data/q3shader, data/q3skin
WAD2 / WAD3 with miptex / qpic / conchars / font / palette images and addImage; Quake .lmp pictures, palette and colormap (palette never embedded); Quake / Half-Life / SPR32 sprites with frame groups; Quake III shader scripts and .skin files. Miptex gains qpic decode / encode and palette mapping; Pixels.keyed keeps the key colour (alpha 0).
This commit is contained in:
1 parent
67e116a6c6
commit
175ca9e823
23 files changed
+5380
-1143
No files matched your search
@@ -0,0 +1,280 @@
|
||||
--!strict
|
||||
-- archive/wad2: Quake WAD2 and Half-Life WAD3 texture / graphics WADs (wad.h of the Quake source, the GoldSrc WAD3
|
||||
-- layout; no code taken), read and write. Lumps by name (repeats "<name>~<n>"); Wad2.image decodes miptex, qpic,
|
||||
-- conchars, palette and GoldSrc font lumps through codecs/image/Miptex (Quake 1 needs a palette: the WAD's own
|
||||
-- "palette" lump, opts.palette or gfx/palette.lmp through files / resolve; else grey). Not supported: compressed lumps.
|
||||
|
||||
-- ------------------------------------------------------------------------------------------------ requires
|
||||
local Err = require("@rosetta/core/Err")
|
||||
local Bytes = require("@rosetta/core/Bytes")
|
||||
local Input = require("@rosetta/core/Input")
|
||||
local Types = require("@rosetta/Types")
|
||||
local Archive = require("@rosetta/containers/Archive")
|
||||
local Pixels = require("@rosetta/codecs/image/Pixels")
|
||||
local Miptex = require("@rosetta/codecs/image/Miptex")
|
||||
|
||||
-- ------------------------------------------------------------------------------------------------ contract
|
||||
local Wad2 = {
|
||||
name = "wad2",
|
||||
category = "archive",
|
||||
extensions = { "wad" },
|
||||
mime = { "application/x-quake-wad" },
|
||||
deps = { "core/Err", "core/Bytes", "core/Input", "Types", "containers/Archive", "codecs/image/Pixels", "codecs/image/Miptex" },
|
||||
caps = { decode = true, encode = true, images = true, variants = { "wad2", "wad3" } },
|
||||
}
|
||||
|
||||
-- ------------------------------------------------------------------------------------------------ types
|
||||
export type DecodeOptions = { maxEntries: number? } -- default 1e6
|
||||
export type EncodeOptions = { variant: ("wad2" | "wad3")? } -- default: the source's, else wad3 when a lump is type 0x43
|
||||
export type ImageOptions = {
|
||||
palette: (buffer | string)?, -- 768 bytes (Quake 1)
|
||||
mip: number?, -- miptex level 0..3, default 0
|
||||
keyed: boolean?, -- index 255 transparent; default: qpics, conchars and '{' textures
|
||||
resolve: ((uri: string) -> (buffer | string)?)?,
|
||||
files: { [string]: buffer | string }?,
|
||||
}
|
||||
export type AddOptions = {
|
||||
kind: ("miptex" | "qpic")?, -- default "miptex"
|
||||
palette: (buffer | string)?, -- 768 bytes; required for WAD2 lumps, made by median cut for WAD3 without it
|
||||
}
|
||||
|
||||
-- ------------------------------------------------------------------------------------------------ constants
|
||||
local T = Miptex.TYPES
|
||||
|
||||
-- ------------------------------------------------------------------------------------------------ sniff
|
||||
function Wad2.sniff(r: Bytes.Reader): number
|
||||
if r:remaining() < 12 then
|
||||
return 0
|
||||
end
|
||||
local m = r:peek(4)
|
||||
return if m == "WAD2" or m == "WAD3" then 0.9 else 0
|
||||
end
|
||||
|
||||
-- ------------------------------------------------------------------------------------------------ decode
|
||||
local function encode(archive: any, opts: any?): (buffer?, Err.Err?)
|
||||
return Wad2.encode(archive, opts)
|
||||
end
|
||||
|
||||
function Wad2.decode(r: Bytes.Reader, opts: DecodeOptions?): (any, Err.Err?)
|
||||
local size = r.limit - r.base
|
||||
local lumps, msg, magic = Miptex.wad(r.buf, r.base, size, opts and opts.maxEntries)
|
||||
if not lumps then
|
||||
return nil, Err.from(msg, 0)
|
||||
end
|
||||
local a = Archive.new("wad2", encode)
|
||||
local seen: { [string]: number } = {}
|
||||
for _, l in lumps do
|
||||
if l.compression ~= 0 then
|
||||
return nil, Err.new(Err.UNSUPPORTED, "compressed lump " .. l.name)
|
||||
end
|
||||
local entry = l.name
|
||||
local k = seen[string.lower(entry)]
|
||||
if k then
|
||||
seen[string.lower(entry)] = k + 1
|
||||
entry = entry .. "~" .. (k + 1)
|
||||
else
|
||||
seen[string.lower(entry)] = 1
|
||||
end
|
||||
Archive.put(a, { name = entry, size = l.disksize, dir = false, method = "store", type = l.type, lump = l.name, load = Archive.slice(r.buf, r.base + l.offset, l.disksize) })
|
||||
end
|
||||
a.meta.source = { format = "wad2", variant = string.lower(magic :: string), lumps = #lumps }
|
||||
a.meta.wad2 = { buf = r.buf, base = r.base, lumps = lumps, goldsrc = magic == "WAD3" }
|
||||
return a, nil
|
||||
end
|
||||
|
||||
-- ------------------------------------------------------------------------------------------------ encode
|
||||
local function guessType(name: string, data: buffer, goldsrc: boolean): number
|
||||
local n = buffer.len(data)
|
||||
if string.lower(name) == "palette" and n == 768 then
|
||||
return T.palette
|
||||
end
|
||||
if n >= 40 then
|
||||
local w, h = buffer.readu32(data, 16), buffer.readu32(data, 20)
|
||||
if w > 0 and h > 0 and w < 65536 and h < 65536 and buffer.readu32(data, 24) == 40 then
|
||||
return if goldsrc then 0x43 else T.miptex
|
||||
end
|
||||
end
|
||||
if n >= 8 then
|
||||
local w, h = buffer.readu32(data, 0), buffer.readu32(data, 4)
|
||||
if w > 0 and h > 0 and 8 + w * h <= n then
|
||||
return T.qpic
|
||||
end
|
||||
end
|
||||
return T.miptex
|
||||
end
|
||||
|
||||
function Wad2.encode(archive: any, opts: EncodeOptions?): (buffer?, Err.Err?)
|
||||
local items, err = Archive.collect(archive)
|
||||
if not items then
|
||||
return nil, err
|
||||
end
|
||||
local src = type(archive) == "table" and type(archive.meta) == "table" and archive.meta.source
|
||||
local variant = opts and opts.variant or (if type(src) == "table" and src.format == "wad2" then src.variant else nil)
|
||||
if not variant then
|
||||
variant = "wad2"
|
||||
for _, it in items do
|
||||
if (it.entry :: any).type == 0x43 then
|
||||
variant = "wad3"
|
||||
end
|
||||
end
|
||||
end
|
||||
if variant ~= "wad2" and variant ~= "wad3" then
|
||||
return nil, Err.new(Err.ARG, "variant must be wad2 or wad3")
|
||||
end
|
||||
local list: { { name: string, data: buffer, type: number } } = {}
|
||||
for _, it in items do
|
||||
if not it.entry.dir then
|
||||
local e: any = it.entry
|
||||
local name = e.lump or string.gsub(string.match(e.name, "([^/\\]*)$") or e.name, "~%d+$", "")
|
||||
if #name == 0 or #name > 15 then
|
||||
return nil, Err.new(Err.ARG, "lump name must be 1..15 bytes: " .. e.name)
|
||||
end
|
||||
local typ = if type(e.type) == "number" then e.type else guessType(name, it.data, variant == "wad3")
|
||||
table.insert(list, { name = name, data = it.data, type = typ })
|
||||
end
|
||||
end
|
||||
local w = Bytes.writer()
|
||||
w:string(if variant == "wad3" then "WAD3" else "WAD2")
|
||||
w:u32le(#list)
|
||||
w:u32le(0)
|
||||
local offs: { number } = {}
|
||||
for i, l in list do
|
||||
offs[i] = w:tell()
|
||||
w:bytes(l.data)
|
||||
w:fill(0, (4 - buffer.len(l.data) % 4) % 4)
|
||||
end
|
||||
local dir = w:tell()
|
||||
for i, l in list do
|
||||
w:u32le(offs[i])
|
||||
w:u32le(buffer.len(l.data))
|
||||
w:u32le(buffer.len(l.data))
|
||||
w:u8(l.type)
|
||||
w:u8(0)
|
||||
w:u16le(0)
|
||||
w:string(l.name)
|
||||
w:fill(0, 16 - #l.name)
|
||||
end
|
||||
w:setU32le(8, dir)
|
||||
return w:finish(), nil
|
||||
end
|
||||
|
||||
-- ------------------------------------------------------------------------------------------------ extras
|
||||
local function info(a: any): any
|
||||
return type(a) == "table" and type(a.meta) == "table" and a.meta.wad2 or nil
|
||||
end
|
||||
|
||||
function Wad2.new(variant: ("wad2" | "wad3")?): any
|
||||
local a = Archive.new("wad2", encode)
|
||||
a.meta.source = { format = "wad2", variant = variant or "wad2" }
|
||||
return a
|
||||
end
|
||||
|
||||
function Wad2.lumps(a: any): { Miptex.Lump }?
|
||||
local w = info(a)
|
||||
return w and w.lumps
|
||||
end
|
||||
|
||||
-- the WAD's own "palette" lump (gfx.wad) as a 1024-byte rgba8 palette
|
||||
function Wad2.palette(a: any): buffer?
|
||||
local w = info(a)
|
||||
local l = w and Miptex.find(w.lumps, "palette")
|
||||
if l and l.disksize >= 768 then
|
||||
return Pixels.palette(w.buf, w.base + l.offset, 256)
|
||||
end
|
||||
return nil
|
||||
end
|
||||
|
||||
function Wad2.image(a: any, name: string, opts: ImageOptions?): (Types.Image?, Err.Err?)
|
||||
local w = info(a)
|
||||
if not w then
|
||||
return nil, Err.new(Err.ARG, "expected a decoded WAD2 / WAD3 archive")
|
||||
end
|
||||
local l = Miptex.find(w.lumps, tostring(name))
|
||||
if not l then
|
||||
return nil, Err.new(Err.ARG, "no lump named " .. tostring(name))
|
||||
end
|
||||
local b, at, size = w.buf, w.base + l.offset, l.disksize
|
||||
local pal: buffer? = nil
|
||||
if not w.goldsrc then
|
||||
local raw = if opts and opts.palette then Bytes.toBuffer(opts.palette) else nil
|
||||
if raw and buffer.len(raw) >= 768 then
|
||||
pal = Pixels.palette(raw, 0, 256)
|
||||
else
|
||||
pal = Wad2.palette(a)
|
||||
if not pal then
|
||||
local lmp = Input.sibling(opts, "gfx/palette.lmp")
|
||||
pal = if lmp and buffer.len(lmp) >= 768 then Pixels.palette(lmp, 0, 256) else nil
|
||||
end
|
||||
end
|
||||
end
|
||||
local keyed: boolean? = if opts then opts.keyed else nil
|
||||
local img: Types.Image?, msg: string?
|
||||
local kind: string
|
||||
if l.type == T.palette and size >= 768 then
|
||||
kind = "palette"
|
||||
local out = Types.Image.new(16, 16)
|
||||
buffer.copy(out.pixels, 0, Pixels.palette(b, at, 256), 0, 1024)
|
||||
img = out
|
||||
elseif string.lower(l.name) == "conchars" and size == 128 * 128 then
|
||||
-- gfx.wad's console font: raw 128 x 128 pixels, no header, index 0 transparent
|
||||
kind = "conchars"
|
||||
local out = Types.Image.new(128, 128)
|
||||
Pixels.indexed(b, at, 128, 8, out.pixels, 128, 128, true, Pixels.keyed(pal or Pixels.grey(), if keyed == false then -1 else 0))
|
||||
img = out
|
||||
elseif l.type == T.qpic then
|
||||
kind = "qpic"
|
||||
img, msg = Miptex.qpic(b, at, size, { palette = pal, goldsrc = w.goldsrc, keyed = keyed } :: any)
|
||||
elseif l.type == T.font and w.goldsrc and size >= 16 + 1024 then
|
||||
-- GoldSrc qfont_t: width (256), height, rowcount, rowheight, charinfo[256] (u16 offset, u16 width), pixels, palette
|
||||
kind = "font"
|
||||
local fw, fh = buffer.readu32(b, at), buffer.readu32(b, at + 4)
|
||||
local p = 16 + 1024 + fw * fh
|
||||
if fw == 0 or fh == 0 or fw * fh > 2 ^ 24 or p + 2 > size then
|
||||
return nil, Err.new(Err.CORRUPT, "bad font lump " .. l.name, l.offset)
|
||||
end
|
||||
local n = buffer.readu16(b, at + p)
|
||||
local fpal = if n > 0 and n <= 256 and p + 2 + n * 3 <= size then Pixels.palette(b, at + p + 2, n) else Pixels.grey()
|
||||
local out = Types.Image.new(fw, fh)
|
||||
Pixels.indexed(b, at + 16 + 1024, fw, 8, out.pixels, fw, fh, true, if keyed == false then fpal else Pixels.keyed(fpal, 255))
|
||||
local glyphs = {}
|
||||
for c = 0, 255 do
|
||||
glyphs[c + 1] = { offset = buffer.readu16(b, at + 16 + c * 4), width = buffer.readu16(b, at + 18 + c * 4) }
|
||||
end
|
||||
out.meta.font = { rows = buffer.readu32(b, at + 8), rowHeight = buffer.readu32(b, at + 12), glyphs = glyphs }
|
||||
img = out
|
||||
else
|
||||
kind = "miptex"
|
||||
img, msg = Miptex.decode(b, at, size, { palette = pal, goldsrc = w.goldsrc, mip = if opts then opts.mip else nil, keyed = keyed } :: any)
|
||||
end
|
||||
if not img then
|
||||
return nil, Err.from(msg, l.offset)
|
||||
end
|
||||
img.meta.source = { format = "wad2", kind = kind, lump = l.name, type = l.type, palette = w.goldsrc or pal ~= nil }
|
||||
return img, nil
|
||||
end
|
||||
|
||||
-- adds an rgba8 image as a miptex (sizes multiples of 16) or qpic lump; WAD3 lumps carry their own palette
|
||||
function Wad2.addImage(a: any, name: string, img: Types.Image, opts: AddOptions?): (boolean, Err.Err?)
|
||||
if type(img) ~= "table" or img.format ~= "rgba8" then
|
||||
return false, Err.new(Err.ARG, "expected an rgba8 Image")
|
||||
end
|
||||
local src = type(a) == "table" and type(a.meta) == "table" and a.meta.source
|
||||
local goldsrc = type(src) == "table" and src.variant == "wad3"
|
||||
local raw = opts and opts.palette and Bytes.toBuffer(opts.palette)
|
||||
local pal = if raw and buffer.len(raw) >= 768 then Pixels.palette(raw, 0, 256) else nil
|
||||
local kind = opts and opts.kind or "miptex"
|
||||
local data, msg
|
||||
if kind == "qpic" then
|
||||
data, msg = Miptex.encodeQpic(img, pal, goldsrc)
|
||||
else
|
||||
data, msg = Miptex.encode(img, name, pal, goldsrc)
|
||||
end
|
||||
if not data then
|
||||
return false, Err.new(Err.ARG, msg or "cannot encode")
|
||||
end
|
||||
Archive.put(a, { name = name, size = buffer.len(data), dir = false, method = "store", lump = name, data = data,
|
||||
type = if kind == "qpic" then T.qpic elseif goldsrc then 0x43 else T.miptex })
|
||||
return true, nil
|
||||
end
|
||||
|
||||
return Wad2
|
||||
Reference in new issue
Block a user