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).
132 lines
5.1 KiB
Luau
132 lines
5.1 KiB
Luau
--!strict
|
|
-- image/lmp: Quake .lmp lumps (wad.h / draw.c of the Quake source; no code taken), read and write: qpic pictures (u32
|
|
-- width, height, 8-bit pixels; index 255 transparent), palette.lmp (768 bytes: a 16 x 16 swatch) and colormap.lmp (64
|
|
-- light levels x 256: a 256 x 64 image). Pictures and colormaps need Quake's palette, which is never embedded (GPL):
|
|
-- opts.palette or gfx/palette.lmp through files / resolve; else grey (meta.source.palette = false).
|
|
|
|
-- ------------------------------------------------------------------------------------------------ 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 Pixels = require("@rosetta/codecs/image/Pixels")
|
|
local Miptex = require("@rosetta/codecs/image/Miptex")
|
|
|
|
-- ------------------------------------------------------------------------------------------------ contract
|
|
local Lmp = {
|
|
name = "lmp",
|
|
category = "image",
|
|
extensions = { "lmp" },
|
|
mime = { "image/x-quake-lmp" },
|
|
deps = { "core/Err", "core/Bytes", "core/Input", "Types", "codecs/image/Pixels", "codecs/image/Miptex" },
|
|
caps = { decode = true, encode = true, alpha = true, depths = { 8 }, variants = { "qpic", "palette", "colormap" } },
|
|
}
|
|
|
|
-- ------------------------------------------------------------------------------------------------ types
|
|
export type DecodeOptions = {
|
|
palette: (buffer | string)?, -- 768 bytes
|
|
kind: ("qpic" | "palette" | "colormap")?, -- default: from the size
|
|
keyed: boolean?, -- qpic index 255 transparent, default true
|
|
resolve: ((uri: string) -> (buffer | string)?)?,
|
|
files: { [string]: buffer | string }?,
|
|
maxPixels: number?, -- default 2^24
|
|
}
|
|
export type EncodeOptions = {
|
|
palette: (buffer | string)?, -- 768 bytes: required for qpics
|
|
kind: ("qpic" | "palette")?, -- default "qpic"; "palette" writes a 16 x 16 swatch as 768 bytes
|
|
resolve: ((uri: string) -> (buffer | string)?)?,
|
|
files: { [string]: buffer | string }?,
|
|
}
|
|
|
|
-- ------------------------------------------------------------------------------------------------ sniff
|
|
local function kindOf(r: Bytes.Reader): string?
|
|
local n = r:remaining()
|
|
if n == 768 then
|
|
return "palette"
|
|
elseif n == 16385 or n == 16384 then
|
|
return "colormap"
|
|
end
|
|
if n < 9 then
|
|
return nil
|
|
end
|
|
local w, h = buffer.readu32(r.buf, r.pos), buffer.readu32(r.buf, r.pos + 4)
|
|
if w > 0 and h > 0 and w <= 4096 and h <= 4096 and 8 + w * h == n then
|
|
return "qpic"
|
|
end
|
|
return nil
|
|
end
|
|
|
|
function Lmp.sniff(r: Bytes.Reader): number
|
|
local k = kindOf(r)
|
|
return if k == "qpic" then 0.3 elseif k then 0.05 else 0
|
|
end
|
|
|
|
-- ------------------------------------------------------------------------------------------------ decode
|
|
local function palette(opts: any?): buffer?
|
|
local raw = if opts and opts.palette then Bytes.toBuffer(opts.palette) else Input.sibling(opts, "gfx/palette.lmp")
|
|
if raw and buffer.len(raw) >= 768 then
|
|
return Pixels.palette(raw, 0, 256)
|
|
end
|
|
return nil
|
|
end
|
|
|
|
function Lmp.decode(r: Bytes.Reader, opts: DecodeOptions?): (Types.Image?, Err.Err?)
|
|
local kind = opts and opts.kind or kindOf(r)
|
|
local b, at, n = r.buf, r.base, r.limit - r.base
|
|
if kind == "palette" then
|
|
if n < 768 then
|
|
return nil, Err.new(Err.TRUNCATED, "palette", 0)
|
|
end
|
|
local img = Types.Image.new(16, 16)
|
|
buffer.copy(img.pixels, 0, Pixels.palette(b, at, 256), 0, 1024)
|
|
local list = Pixels.colorList(img.pixels)
|
|
img.meta.palette = list
|
|
img.meta.source = { format = "lmp", kind = "palette" }
|
|
return img, nil
|
|
end
|
|
local pal = palette(opts)
|
|
if kind == "colormap" then
|
|
local rows = math.min(64, n // 256)
|
|
if rows < 1 then
|
|
return nil, Err.new(Err.TRUNCATED, "colormap", 0)
|
|
end
|
|
local img = Types.Image.new(256, rows)
|
|
Pixels.indexed(b, at, 256, 8, img.pixels, 256, rows, true, pal or Pixels.grey())
|
|
img.meta.source = { format = "lmp", kind = "colormap", palette = pal ~= nil }
|
|
return img, nil
|
|
end
|
|
if kind ~= "qpic" and n >= 8 then
|
|
kind = "qpic"
|
|
end
|
|
local img, msg = Miptex.qpic(b, at, n, { palette = pal, keyed = opts and opts.keyed, maxPixels = opts and opts.maxPixels })
|
|
if not img then
|
|
return nil, Err.from(msg, 0)
|
|
end
|
|
img.meta.source = { format = "lmp", kind = "qpic", palette = pal ~= nil, keyed = (opts == nil or opts.keyed ~= false) or nil }
|
|
return img, nil
|
|
end
|
|
|
|
-- ------------------------------------------------------------------------------------------------ encode
|
|
function Lmp.encode(img: Types.Image, opts: EncodeOptions?): (buffer?, Err.Err?)
|
|
if type(img) ~= "table" or img.format ~= "rgba8" or type(img.pixels) ~= "buffer" then
|
|
return nil, Err.new(Err.ARG, "expected an rgba8 Image")
|
|
end
|
|
if opts and opts.kind == "palette" then
|
|
if img.width * img.height < 256 then
|
|
return nil, Err.new(Err.ARG, "a palette needs 256 pixels (16 x 16)")
|
|
end
|
|
return Pixels.paletteBytes(img.pixels, 256), nil
|
|
end
|
|
local pal = palette(opts)
|
|
if not pal then
|
|
return nil, Err.new(Err.ARG, "a qpic needs Quake's palette (opts.palette or gfx/palette.lmp; it is not embedded)")
|
|
end
|
|
local out, msg = Miptex.encodeQpic(img, pal, false, true)
|
|
if not out then
|
|
return nil, Err.new(Err.ARG, msg or "cannot encode")
|
|
end
|
|
return out, nil
|
|
end
|
|
|
|
return Lmp
|