merge ws/quake

This commit is contained in:
4DBug committed 2026-11-01 20:08:48 -06:00
commit 58df9e8a3a
39 files changed
+10044 -1413

No files matched your search

+99 -15
View File
@@ -113,30 +113,28 @@ function Miptex.encode(img: Types.Image, name: string, palette: buffer?, goldsrc
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
if not palette and not goldsrc then
return nil, "a Quake miptex needs a palette"
end
local keyed = string.sub(name, 1, 1) == "{"
local _, colors = Miptex.indices(img, palette, keyed)
if keyed and not palette then
colors[256] = { 0, 0, 255, 255 } -- GoldSrc '{' textures: index 255 is the (blue) key colour
end
local pal1024 = palette or buffer.create(1024)
if not palette then
for i, c in colors do
buffer.writeu32(pal1024, (i - 1) * 4, 0xFF000000 + c[3] * 65536 + c[2] * 256 + c[1])
end
end
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
local lvl = { kind = "image", width = lw, height = lh, format = "rgba8", pixels = px, loops = 0, meta = {} } :: any
levels[m + 1] = (Miptex.indices(lvl, pal1024, keyed))
end
local total = 40
for _, l in levels do
@@ -211,4 +209,90 @@ function Miptex.find(lumps: { Lump }, name: string): Lump?
return best
end
-- qpic (Quake .lmp / WAD2 type 0x42: u32 width, height, 8-bit pixels; GoldSrc adds u16 count + count * 3 palette bytes)
-- -> rgba8. keyed: index 255 transparent (Quake's Draw_TransPic), default true
function Miptex.qpic(b: buffer, at: number, size: number, opts: DecodeOptions?): (Types.Image?, string?)
if size < 8 or at < 0 or at + 8 > buffer.len(b) then
return nil, "truncated qpic header"
end
local w, h = readu32(b, at), readu32(b, at + 4)
if w == 0 or h == 0 then
return nil, "empty qpic"
end
if w * h > (opts and opts.maxPixels or 2 ^ 24) then
return nil, "limit: qpic " .. w .. " x " .. h
end
if 8 + w * h > size or at + 8 + w * h > buffer.len(b) then
return nil, "truncated qpic pixels"
end
local own: buffer? = nil
if opts and opts.goldsrc then
local p = 8 + w * h
if p + 2 <= size then
local n = buffer.readu16(b, at + p)
if n > 0 and n <= 256 and p + 2 + n * 3 <= size then
own = Pixels.palette(b, at + p + 2, n)
end
end
end
local pal = own or (opts and opts.palette) or Pixels.grey()
local keyed = if opts and opts.keyed ~= nil then opts.keyed else true
if keyed then
pal = Pixels.keyed(pal, 255)
end
local img = Types.Image.new(w, h)
Pixels.indexed(b, at + 8, w, 8, img.pixels, w, h, true, pal)
img.meta.source = { format = "qpic", palette = own ~= nil or (opts ~= nil and opts.palette ~= nil), keyed = keyed or nil }
return img, nil
end
-- palette indices of an rgba8 image (nearest entry; alpha < 128 -> 255 when keyed and the image has any) and the
-- palette used
function Miptex.indices(img: Types.Image, palette: buffer?, keyed: boolean): (buffer, { { number } })
if keyed then
local any = false
for k = 0, img.width * img.height - 1 do
if readu8(img.pixels, k * 4 + 3) < 128 then
any = true
break
end
end
keyed = any
end
local colors = if palette then Pixels.colorList(palette) else Quantize.palette(img.pixels, img.width, img.height, if keyed then 255 else 256)
local idx = Quantize.map(img.pixels, img.width, img.height, colors, { transparentIndex = if keyed and palette then 255 else nil })
if keyed then
for k = 0, img.width * img.height - 1 do
if readu8(img.pixels, k * 4 + 3) < 128 then
buffer.writeu8(idx, k, 255)
end
end
end
return idx, colors
end
-- an rgba8 image -> qpic bytes (goldsrc: with its palette, made by median cut when none is given)
function Miptex.encodeQpic(img: Types.Image, palette: buffer?, goldsrc: boolean?, keyed: boolean?): (buffer?, string?)
if not palette and not goldsrc then
return nil, "a Quake qpic needs a palette"
end
local w, h = img.width, img.height
local idx, colors = Miptex.indices(img, palette, keyed ~= false)
local out = buffer.create(8 + w * h + (if goldsrc then 2 + 768 else 0))
buffer.writeu32(out, 0, w)
buffer.writeu32(out, 4, h)
buffer.copy(out, 8, idx, 0, w * h)
if goldsrc then
local p = 8 + w * h
buffer.writeu16(out, p, 256)
for i = 0, 255 do
local c = colors[i + 1] or { 0, 0, 0 }
buffer.writeu8(out, p + 2 + i * 3, c[1])
buffer.writeu8(out, p + 3 + i * 3, c[2])
buffer.writeu8(out, p + 4 + i * 3, c[3])
end
end
return out, nil
end
return Miptex
+3 -3
View File
@@ -269,13 +269,13 @@ function Pixels.grey(): buffer
return pal
end
-- a copy of a 1024-byte palette with entry index fully transparent (Quake / GoldSrc: index 255 of sprites, qpics and
-- '{' textures)
-- a copy of a 1024-byte palette with entry index transparent (alpha 0, colour kept as Pillow does; 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)
writeu8(out, index * 4 + 3, 0)
end
return out
end