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,131 @@
|
||||
--!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
|
||||
@@ -0,0 +1,311 @@
|
||||
--!strict
|
||||
-- image/spr: Quake (IDSP 1), Half-Life (IDSP 2, own palette, texture formats) and DarkPlaces SPR32 (IDSP 32, rgba)
|
||||
-- sprites, read and write (spritegn.h of the Quake source, the GoldSrc sprite layout; no code taken). Frames and frame
|
||||
-- groups become Image frames on one canvas around the sprite origin (meta.origin); group intervals give the delays.
|
||||
-- Quake sprites need Quake's palette (opts.palette or gfx/palette.lmp through files / resolve; never embedded).
|
||||
-- Not supported: Quake II sprites (IDS2: frames are separate .pcx files).
|
||||
|
||||
-- ------------------------------------------------------------------------------------------------ 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 Spr = {
|
||||
name = "spr",
|
||||
category = "image",
|
||||
extensions = { "spr" },
|
||||
mime = { "image/x-quake-sprite" },
|
||||
deps = { "core/Err", "core/Bytes", "core/Input", "Types", "codecs/image/Pixels", "codecs/image/Miptex" },
|
||||
caps = { decode = true, encode = true, alpha = true, animation = true, variants = { "quake", "halflife", "spr32" } },
|
||||
}
|
||||
|
||||
-- ------------------------------------------------------------------------------------------------ types
|
||||
export type DecodeOptions = {
|
||||
palette: (buffer | string)?, -- Quake: 768 bytes
|
||||
resolve: ((uri: string) -> (buffer | string)?)?,
|
||||
files: { [string]: buffer | string }?,
|
||||
maxPixels: number?, -- per frame, default 2^24
|
||||
maxFrames: number?, -- default 4096
|
||||
}
|
||||
export type EncodeOptions = {
|
||||
version: number?, -- 2 (Half-Life, default), 1 (Quake: needs opts.palette) or 32 (rgba)
|
||||
palette: (buffer | string)?,
|
||||
type: number?, -- orientation: 0 parallel upright, 1 facing upright, 2 parallel (default), 3 oriented, 4 parallel oriented
|
||||
texFormat: number?, -- Half-Life: 0 normal, 1 additive, 2 index alpha, 3 alpha test (default: 3 when any pixel is transparent)
|
||||
delay: number?, -- seconds per frame when the image has none, default 0.1
|
||||
}
|
||||
|
||||
-- ------------------------------------------------------------------------------------------------ constants
|
||||
local TEX_FORMATS = { [0] = "normal", "additive", "indexalpha", "alphatest" }
|
||||
|
||||
-- ------------------------------------------------------------------------------------------------ sniff
|
||||
function Spr.sniff(r: Bytes.Reader): number
|
||||
if r:remaining() < 36 or r:peek(4) ~= "IDSP" then
|
||||
return 0
|
||||
end
|
||||
local v = buffer.readi32(r.buf, r.pos + 4)
|
||||
return if v == 1 or v == 2 or v == 32 then 1 else 0.3
|
||||
end
|
||||
|
||||
-- ------------------------------------------------------------------------------------------------ decode
|
||||
type Raw = { x: number, y: number, w: number, h: number, at: number, delay: number }
|
||||
|
||||
function Spr.decode(r: Bytes.Reader, opts: DecodeOptions?): (Types.Image?, Err.Err?)
|
||||
if r:string(4) ~= "IDSP" then
|
||||
return nil, r.err or Err.new(Err.CORRUPT, "bad magic", 0)
|
||||
end
|
||||
local version = r:i32le()
|
||||
if version ~= 1 and version ~= 2 and version ~= 32 then
|
||||
return nil, Err.new(Err.UNSUPPORTED, "sprite version " .. tostring(version), 4)
|
||||
end
|
||||
local stype = r:i32le()
|
||||
local texFormat = if version == 2 then r:i32le() else 3
|
||||
local radius = r:f32le()
|
||||
local mw, mh, nframes = r:i32le(), r:i32le(), r:i32le()
|
||||
local beam, sync = r:f32le(), r:i32le()
|
||||
if r.err then
|
||||
return nil, r.err
|
||||
end
|
||||
local maxFrames = opts and opts.maxFrames or 4096
|
||||
local maxPixels = opts and opts.maxPixels or 2 ^ 24
|
||||
if nframes < 1 or nframes > maxFrames then
|
||||
return nil, Err.new(if nframes < 1 then Err.CORRUPT else Err.LIMIT, "frame count " .. nframes, 24)
|
||||
end
|
||||
local pal: buffer
|
||||
local own = false
|
||||
if version == 2 then
|
||||
local n = r:u16le()
|
||||
if r.err then
|
||||
return nil, r.err
|
||||
end
|
||||
if n < 1 or n > 256 or not r:need(n * 3) then
|
||||
return nil, r.err or Err.new(Err.CORRUPT, "palette size " .. n, r:tell() - 2)
|
||||
end
|
||||
pal = Pixels.palette(r.buf, r.pos, n)
|
||||
r:skip(n * 3)
|
||||
own = true
|
||||
else
|
||||
local raw = if opts and opts.palette then Bytes.toBuffer(opts.palette) else Input.sibling(opts, "gfx/palette.lmp")
|
||||
own = raw ~= nil and buffer.len(raw) >= 768
|
||||
pal = if raw and own then Pixels.palette(raw, 0, 256) else Pixels.grey()
|
||||
end
|
||||
local bpp = if version == 32 then 4 else 1
|
||||
local raws: { Raw } = {}
|
||||
local groups: { { type: string, count: number } } = {}
|
||||
local function single(delay: number): Err.Err?
|
||||
local x, y, w, h = r:i32le(), r:i32le(), r:i32le(), r:i32le()
|
||||
if r.err then
|
||||
return r.err
|
||||
end
|
||||
if w < 1 or h < 1 then
|
||||
return Err.new(Err.CORRUPT, "frame size " .. w .. " x " .. h, r:tell() - 8)
|
||||
end
|
||||
if w * h > maxPixels then
|
||||
return Err.new(Err.LIMIT, "frame " .. w .. " x " .. h)
|
||||
end
|
||||
if not r:need(w * h * bpp) then
|
||||
return r.err
|
||||
end
|
||||
table.insert(raws, { x = x, y = y, w = w, h = h, at = r.pos, delay = delay })
|
||||
r:skip(w * h * bpp)
|
||||
return nil
|
||||
end
|
||||
for _ = 1, nframes do
|
||||
local ft = r:i32le()
|
||||
if r.err then
|
||||
return nil, r.err
|
||||
end
|
||||
if ft == 0 then
|
||||
local e = single(0.1)
|
||||
if e then
|
||||
return nil, e
|
||||
end
|
||||
table.insert(groups, { type = "single", count = 1 })
|
||||
else
|
||||
local n = r:i32le()
|
||||
if r.err then
|
||||
return nil, r.err
|
||||
end
|
||||
if n < 1 or #raws + n > maxFrames then
|
||||
return nil, Err.new(if n < 1 then Err.CORRUPT else Err.LIMIT, "group of " .. n .. " frames", r:tell() - 4)
|
||||
end
|
||||
local ivs = {}
|
||||
for k = 1, n do
|
||||
ivs[k] = r:f32le()
|
||||
end
|
||||
if r.err then
|
||||
return nil, r.err
|
||||
end
|
||||
for k = 1, n do
|
||||
-- intervals are cumulative end times (R_GetSpriteFrame)
|
||||
local e = single(math.max(0, ivs[k] - (if k > 1 then ivs[k - 1] else 0)))
|
||||
if e then
|
||||
return nil, e
|
||||
end
|
||||
end
|
||||
table.insert(groups, { type = "group", count = n })
|
||||
end
|
||||
end
|
||||
local minX: number, maxX: number, minY: number, maxY: number = math.huge, -math.huge, math.huge, -math.huge
|
||||
for _, f in raws do
|
||||
minX = math.min(minX, f.x)
|
||||
maxX = math.max(maxX, f.x + f.w)
|
||||
maxY = math.max(maxY, f.y)
|
||||
minY = math.min(minY, f.y - f.h)
|
||||
end
|
||||
local cw, ch = maxX - minX, maxY - minY
|
||||
if cw * ch > maxPixels then
|
||||
return nil, Err.new(Err.LIMIT, "canvas " .. cw .. " x " .. ch)
|
||||
end
|
||||
local fpal = pal
|
||||
if version ~= 2 or texFormat == 3 then
|
||||
fpal = Pixels.keyed(pal, 255)
|
||||
end
|
||||
local b = r.buf
|
||||
local frames: { any } = {}
|
||||
for _, f in raws do
|
||||
local img = Types.Image.new(f.w, f.h)
|
||||
if version == 32 then
|
||||
buffer.copy(img.pixels, 0, b, f.at, f.w * f.h * 4)
|
||||
elseif version == 2 and texFormat == 2 then
|
||||
-- index alpha: the colour is the last palette entry, the index is the alpha
|
||||
local c = bit32.band(buffer.readu32(pal, 255 * 4), 0xFFFFFF)
|
||||
for k = 0, f.w * f.h - 1 do
|
||||
buffer.writeu32(img.pixels, k * 4, c + buffer.readu8(b, f.at + k) * 0x1000000)
|
||||
end
|
||||
else
|
||||
Pixels.indexed(b, f.at, f.w, 8, img.pixels, f.w, f.h, true, fpal)
|
||||
end
|
||||
table.insert(frames, { image = img, delay = f.delay, x = f.x - minX, y = maxY - f.y, dispose = "background", blend = "source" })
|
||||
end
|
||||
local out = Types.Image.new(cw, ch)
|
||||
local f1 = frames[1]
|
||||
for yy = 0, f1.image.height - 1 do
|
||||
buffer.copy(out.pixels, ((f1.y + yy) * cw + f1.x) * 4, f1.image.pixels, yy * f1.image.width * 4, f1.image.width * 4)
|
||||
end
|
||||
out.frames = if #frames > 1 then frames else nil
|
||||
out.loops = 0
|
||||
out.meta.origin = { -minX, maxY }
|
||||
out.meta.source = {
|
||||
format = "spr", version = version, type = stype, texFormat = if version == 2 then TEX_FORMATS[texFormat] or texFormat else nil,
|
||||
radius = radius, width = mw, height = mh, beamLength = beam, sync = sync, groups = groups,
|
||||
palette = own, origins = (function()
|
||||
local o = {}
|
||||
for i, f in raws do
|
||||
o[i] = { f.x, f.y }
|
||||
end
|
||||
return o
|
||||
end)(),
|
||||
}
|
||||
return out, nil
|
||||
end
|
||||
|
||||
-- ------------------------------------------------------------------------------------------------ encode
|
||||
function Spr.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
|
||||
local version = opts and opts.version or 2
|
||||
if version ~= 1 and version ~= 2 and version ~= 32 then
|
||||
return nil, Err.new(Err.ARG, "version must be 1, 2 or 32")
|
||||
end
|
||||
local list: { { image: Types.Image, x: number, y: number, delay: number } } = {}
|
||||
if img.frames and #img.frames > 0 then
|
||||
for _, f in img.frames do
|
||||
if f.image.format ~= "rgba8" then
|
||||
return nil, Err.new(Err.ARG, "frames must be rgba8")
|
||||
end
|
||||
table.insert(list, { image = f.image, x = f.x, y = f.y, delay = f.delay })
|
||||
end
|
||||
else
|
||||
table.insert(list, { image = img, x = 0, y = 0, delay = opts and opts.delay or 0.1 })
|
||||
end
|
||||
local origin = img.meta and img.meta.origin or { img.width // 2, img.height // 2 }
|
||||
local mw: number, mh: number, alpha = 0, 0, false
|
||||
for _, f in list do
|
||||
mw = math.max(mw, f.image.width)
|
||||
mh = math.max(mh, f.image.height)
|
||||
local px = f.image.pixels
|
||||
for k = 0, f.image.width * f.image.height - 1 do
|
||||
if buffer.readu8(px, k * 4 + 3) < 128 then
|
||||
alpha = true
|
||||
break
|
||||
end
|
||||
end
|
||||
end
|
||||
local pal: buffer? = nil
|
||||
if version == 1 then
|
||||
local raw = if opts and opts.palette then Bytes.toBuffer(opts.palette) else nil
|
||||
if not raw or buffer.len(raw) < 768 then
|
||||
return nil, Err.new(Err.ARG, "a Quake sprite needs Quake's palette (opts.palette; it is not embedded)")
|
||||
end
|
||||
pal = Pixels.palette(raw, 0, 256)
|
||||
elseif version == 2 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
|
||||
-- one palette for every frame: the frames side by side
|
||||
local tw = 0
|
||||
for _, f in list do
|
||||
tw += f.image.width
|
||||
end
|
||||
local strip = Types.Image.new(tw, mh)
|
||||
local x0 = 0
|
||||
for _, f in list do
|
||||
for yy = 0, f.image.height - 1 do
|
||||
buffer.copy(strip.pixels, (yy * tw + x0) * 4, f.image.pixels, yy * f.image.width * 4, f.image.width * 4)
|
||||
end
|
||||
x0 += f.image.width
|
||||
end
|
||||
local _, colors = Miptex.indices(strip, nil, alpha)
|
||||
local p = buffer.create(1024)
|
||||
for i, c in colors do
|
||||
buffer.writeu32(p, (i - 1) * 4, 0xFF000000 + c[3] * 65536 + c[2] * 256 + c[1])
|
||||
end
|
||||
if alpha then
|
||||
buffer.writeu32(p, 255 * 4, 0xFFFF0000) -- the alpha-test key: blue
|
||||
end
|
||||
pal = p
|
||||
end
|
||||
end
|
||||
local texFormat = opts and opts.texFormat or (if alpha then 3 else 0)
|
||||
local w = Bytes.writer()
|
||||
w:string("IDSP")
|
||||
w:u32le(version)
|
||||
w:u32le(opts and opts.type or 2)
|
||||
if version == 2 then
|
||||
w:u32le(texFormat)
|
||||
end
|
||||
w:f32le(math.sqrt(mw * mw + mh * mh) / 2)
|
||||
w:u32le(mw)
|
||||
w:u32le(mh)
|
||||
w:u32le(#list)
|
||||
w:f32le(0)
|
||||
w:u32le(0)
|
||||
if version == 2 then
|
||||
w:u16le(256)
|
||||
w:bytes(Pixels.paletteBytes(pal :: buffer, 256))
|
||||
end
|
||||
for _, f in list do
|
||||
local fw, fh = f.image.width, f.image.height
|
||||
w:u32le(0)
|
||||
w:u32le(bit32.band(f.x - origin[1], 0xFFFFFFFF))
|
||||
w:u32le(bit32.band(origin[2] - f.y, 0xFFFFFFFF))
|
||||
w:u32le(fw)
|
||||
w:u32le(fh)
|
||||
if version == 32 then
|
||||
w:bytes(f.image.pixels, 0, fw * fh * 4)
|
||||
else
|
||||
local idx = Miptex.indices(f.image, pal, alpha and texFormat == 3 or version == 1)
|
||||
w:bytes(idx, 0, fw * fh)
|
||||
end
|
||||
end
|
||||
return w:finish(), nil
|
||||
end
|
||||
|
||||
return Spr
|
||||
Reference in new issue
Block a user