image/art: Build engine ART tiles (frames per tile, picanm, PALETTE.DAT / LOOKUP.DAT through opts or resolve, shade / palookup remaps), read + write; tested against arttool from the eduke32 source

This commit is contained in:
4DBug committed 2026-11-01 18:33:29 -06:00
1 parent 01c044805f
commit 75984be212
7 files changed
+857 -1

No files matched your search

+12
View File
@@ -26,6 +26,18 @@ local list: { Entry } = {
return require("@rosetta/image/ani")
end,
},
{
name = "art",
category = "image",
path = "image/art",
extensions = { "art" },
mime = { "image/x-build-art" },
deps = { "core/Err", "core/Bytes", "Types", "codecs/image/Pixels", "codecs/image/Quantize" },
caps = { decode = true, encode = true, alpha = true, palette = true, frames = true, depths = { 8 } },
load = function()
return require("@rosetta/image/art")
end,
},
{
name = "blp",
category = "image",
+486
View File
@@ -0,0 +1,486 @@
--!strict
-- image/art: Build engine ART tile files (TILES000.ART ...), from Ken Silverman's BUILDINF.TXT and the eduke32 wiki
-- ("ART format"); no code taken. Supports: version 1 with the optional "BUILDART" prefix, every tile as a frame (tile
-- order, empty tiles 0 x 0), picanm animation / centre fields, PALETTE.DAT (6-bit VGA, shade tables, translucency
-- table) and LOOKUP.DAT palookups through opts / resolve; writes ART from gray8 indices or rgba8 mapped to the palette.
-- ------------------------------------------------------------------------------------------------ requires
local Err = require("@rosetta/core/Err")
local Bytes = require("@rosetta/core/Bytes")
local Types = require("@rosetta/Types")
local Pixels = require("@rosetta/codecs/image/Pixels")
local Quantize = require("@rosetta/codecs/image/Quantize")
-- ------------------------------------------------------------------------------------------------ contract
local Art = {
name = "art",
category = "image",
extensions = { "art" },
mime = { "image/x-build-art" },
deps = { "core/Err", "core/Bytes", "Types", "codecs/image/Pixels", "codecs/image/Quantize" },
caps = { decode = true, encode = true, alpha = true, palette = true, frames = true, depths = { 8 } },
}
-- ------------------------------------------------------------------------------------------------ types
export type PaletteSource = buffer | string | { { number } } -- PALETTE.DAT bytes, or 8-bit { r, g, b[, a] } entries
export type DecodeOptions = {
palette: PaletteSource?, -- default opts.resolve("PALETTE.DAT"); without one, a grey ramp (meta.source.palette = false)
lookup: (buffer | string)?, -- LOOKUP.DAT bytes; default opts.resolve("LOOKUP.DAT")
shade: number?, -- shade table of PALETTE.DAT applied to the indices (0 = none, default)
pal: number?, -- LOOKUP.DAT palookup applied before the shade (0 = none, default)
native: boolean?, -- gray8 palette indices instead of rgba8
resolve: ((uri: string) -> buffer?)?,
maxPixels: number?, -- all tiles together, default 2^26
}
export type Picanm = {
frames: number, -- bits 0-5
type: "none" | "oscillate" | "forward" | "backward", -- bits 6-7
xoffset: number, -- bits 8-15, signed
yoffset: number, -- bits 16-23, signed
speed: number, -- bits 24-27: a frame lasts 2^speed ticks of 120 Hz
extra: number, -- bits 28-31
}
export type EncodeOptions = {
palette: PaletteSource?, -- for rgba8 frames; default img.meta.palette
tileStart: number?, -- default img.meta.source.tileStart, else the first frame's meta.tile, else 0
numtiles: number?, -- the legacy header field; default img.meta.source.numtiles, else 0
buildart: boolean?, -- write the "BUILDART" prefix (eduke32); default img.meta.source.buildart
}
-- ------------------------------------------------------------------------------------------------ constants
local TYPES = { "none", "oscillate", "forward", "backward" }
local TYPE_INDEX = { none = 0, oscillate = 1, forward = 2, backward = 3 }
local TRANSPARENT = 255
local readu8, readu16, readu32, readi16 = buffer.readu8, buffer.readu16, buffer.readu32, buffer.readi16
-- ------------------------------------------------------------------------------------------------ sniff
local function header(b: buffer, o: number, limit: number): (number?, number?, number?, number?, boolean?)
local p = o
local prefixed = false
if limit - p >= 8 and buffer.readstring(b, p, 8) == "BUILDART" then
p += 8
prefixed = true
end
if limit - p < 16 or readu32(b, p) ~= 1 then
return nil
end
local first, last = readu32(b, p + 8), readu32(b, p + 12)
if last < first or last - first >= 65536 then
return nil
end
return p + 16, first, last, readu32(b, p + 4), prefixed
end
function Art.sniff(r: Bytes.Reader): number
local at, first, last = header(r.buf, r.base, r.limit)
if not at or not first or not last then
return 0
end
local n = last - first + 1
local p = at :: number
if r.limit - p < n * 8 then
return 0
end
local total = 0
for i = 0, n - 1 do
local w, h = readi16(r.buf, p + i * 2), readi16(r.buf, p + n * 2 + i * 2)
if w < 0 or h < 0 then
return 0
end
total += w * h
end
local size = p + n * 8 + total - r.base
local have = r.limit - r.base
return if size == have then 0.9 elseif size < have and have - size < 4096 then 0.5 else 0
end
-- ------------------------------------------------------------------------------------------------ decode
local function bytesOf(v: any): buffer?
if type(v) == "buffer" then
return v
elseif type(v) == "string" then
return buffer.fromstring(v)
end
return nil
end
local function fetch(opts: DecodeOptions, key: string, file: string): buffer?
local v = bytesOf((opts :: any)[key])
if v then
return v
end
local resolve = opts.resolve
if type(resolve) == "function" then
for _, name in { file, string.lower(file) } do
local ok, res = pcall(resolve, name)
local b = if ok then bytesOf(res) else nil
if b then
return b
end
end
end
return nil
end
-- palette as 256 u32 rgba words; PALETTE.DAT stores 6-bit components, scaled << 2 as the engine and arttool do
local function makePalette(src: any, dat: buffer?): buffer
if type(src) == "table" then
local tri = buffer.create(768)
for i = 0, 255 do
local c = src[i + 1]
if type(c) == "table" then
for k = 1, 3 do
buffer.writeu8(tri, i * 3 + k - 1, math.clamp(math.floor(tonumber(c[k]) or 0), 0, 255))
end
end
end
return Pixels.palette(tri, 0, 256)
end
if dat and buffer.len(dat) >= 768 then
local tri = buffer.create(768)
for i = 0, 767 do
buffer.writeu8(tri, i, bit32.band(readu8(dat, i), 63) * 4)
end
return Pixels.palette(tri, 0, 256)
end
local tri = buffer.create(768)
for i = 0, 767 do
buffer.writeu8(tri, i, i // 3)
end
return Pixels.palette(tri, 0, 256)
end
local function paletteList(pal: buffer): { { number } }
local list: { { number } } = table.create(256)
for i = 0, 255 do
local v = readu32(pal, i * 4)
list[i + 1] = { bit32.band(v, 255), bit32.band(bit32.rshift(v, 8), 255), bit32.band(bit32.rshift(v, 16), 255), 255 }
end
return list
end
-- PALETTE.DAT: 768 palette bytes, u16 numshades, numshades x 256 shade table, 65536-byte translucency table
local function shadeTables(dat: buffer): (number, buffer?, buffer?)
local n = buffer.len(dat)
if n < 770 then
return 0, nil, nil
end
local count = readu16(dat, 768)
if count == 0 or 770 + count * 256 > n then
return 0, nil, nil
end
local tables = buffer.create(count * 256)
buffer.copy(tables, 0, dat, 770, count * 256)
local tr: buffer? = nil
local at = 770 + count * 256
if at + 65536 <= n then
local t = buffer.create(65536)
buffer.copy(t, 0, dat, at, 65536)
tr = t
end
return count, tables, tr
end
-- LOOKUP.DAT: u8 count, count x (u8 palnum, 256-byte remap), then up to 5 alternate 768-byte palettes (Duke 3D)
local function lookups(dat: buffer): ({ [number]: buffer }, { buffer })
local n = buffer.len(dat)
local out: { [number]: buffer } = {}
local alts: { buffer } = {}
if n < 1 then
return out, alts
end
local count = readu8(dat, 0)
local p = 1
for _ = 1, count do
if p + 257 > n then
return out, alts
end
local t = buffer.create(256)
buffer.copy(t, 0, dat, p + 1, 256)
out[readu8(dat, p)] = t
p += 257
end
while p + 768 <= n and #alts < 5 do
local a = buffer.create(768)
buffer.copy(a, 0, dat, p, 768)
table.insert(alts, a)
p += 768
end
return out, alts
end
local function picanm(v: number): Picanm
local function s8(x: number): number
return if x >= 128 then x - 256 else x
end
return {
frames = bit32.band(v, 63),
type = TYPES[bit32.band(bit32.rshift(v, 6), 3) + 1] :: any,
xoffset = s8(bit32.band(bit32.rshift(v, 8), 255)),
yoffset = s8(bit32.band(bit32.rshift(v, 16), 255)),
speed = bit32.band(bit32.rshift(v, 24), 15),
extra = bit32.rshift(v, 28),
}
end
function Art.decode(r: Bytes.Reader, opts: DecodeOptions?): (Types.Image?, Err.Err?)
local o: DecodeOptions = opts or {}
local b, base, limit = r.buf, r.base, r.limit
local at, first, last, numtiles, prefixed = header(b, base, limit)
if not at or not first or not last then
if limit - base < 16 then
return nil, Err.new(Err.TRUNCATED, "short header", base)
end
return nil, Err.new(Err.CORRUPT, "not an ART file (version 1, first <= last tile)", base)
end
local n = last - first + 1
local p = at :: number
if limit - p < n * 8 then
return nil, Err.new(Err.TRUNCATED, "tile tables", p)
end
local maxPixels = o.maxPixels or 2 ^ 26
local sizes: { number } = table.create(n * 2)
local total = 0
for i = 0, n - 1 do
local w, h = readi16(b, p + i * 2), readi16(b, p + n * 2 + i * 2)
if w < 0 or h < 0 then
return nil, Err.new(Err.CORRUPT, string.format("tile %d has a negative size", first + i), p + i * 2)
end
sizes[i * 2 + 1], sizes[i * 2 + 2] = w, h
total += w * h
end
if total > maxPixels then
return nil, Err.new(Err.LIMIT, string.format("%d pixels is over the %d pixel limit", total, maxPixels))
end
local datStart = p + n * 8
local palDat = fetch(o, "palette", "PALETTE.DAT")
local pal = makePalette(if type(o.palette) == "table" then o.palette else nil, palDat)
local hasPalette = type(o.palette) == "table" or (palDat ~= nil and buffer.len(palDat) >= 768)
local remap: buffer? = nil
local numShades, shades, translucency = 0, nil :: buffer?, nil :: buffer?
if palDat then
numShades, shades, translucency = shadeTables(palDat)
end
local lookDat = fetch(o, "lookup", "LOOKUP.DAT")
local looks, alts = {} :: { [number]: buffer }, {} :: { buffer }
if lookDat then
looks, alts = lookups(lookDat)
end
local palNum = math.floor(tonumber(o.pal) or 0)
if palNum ~= 0 then
local t = looks[palNum]
if not t then
return nil, Err.new(Err.ARG, string.format("palookup %d is not in LOOKUP.DAT", palNum))
end
remap = t
end
local shade = math.floor(tonumber(o.shade) or 0)
local shadeAt = -1
if shade ~= 0 then
if not shades or shade < 0 or shade >= numShades then
return nil, Err.new(Err.ARG, string.format("shade %d is not in PALETTE.DAT", shade))
end
shadeAt = shade * 256
end
local native = o.native == true
local frames: { Types.Frame } = table.create(n)
local truncated = false
local q = datStart
local main: Types.Image? = nil
for i = 0, n - 1 do
local w, h = sizes[i * 2 + 1], sizes[i * 2 + 2]
local img = Types.Image.new(w, h, if native then "gray8" else "rgba8")
local px = img.pixels
local avail = math.clamp(limit - q, 0, w * h)
if avail < w * h then
truncated = true
end
-- pixels are stored column by column
for x = 0, w - 1 do
local col = q + x * h
for y = 0, h - 1 do
local k = x * h + y
local idx = if k < avail then readu8(b, col + y) else TRANSPARENT
local out = idx
if idx ~= TRANSPARENT then
if remap then
out = readu8(remap, out)
end
if shades and shadeAt >= 0 then
out = readu8(shades, shadeAt + out)
end
end
if native then
buffer.writeu8(px, y * w + x, out)
elseif idx == TRANSPARENT then
buffer.writeu32(px, (y * w + x) * 4, 0)
else
buffer.writeu32(px, (y * w + x) * 4, readu32(pal, out * 4))
end
end
end
q += w * h
local anm = readu32(b, p + n * 4 + i * 4)
img.meta.tile = first + i
img.meta.picanm = picanm(anm)
img.meta.source = { format = "art", picanm = anm }
table.insert(frames, { image = img, delay = 0, x = 0, y = 0, dispose = "none", blend = "source" } :: Types.Frame)
if not main and w > 0 then
main = img
end
end
local top = main or Types.Image.new(0, 0, if native then "gray8" else "rgba8")
local img = Types.Image.new(top.width, top.height, top.format)
buffer.copy(img.pixels, 0, top.pixels)
img.frames = frames
img.meta.palette = paletteList(pal)
img.meta.tile = top.meta.tile
img.meta.picanm = top.meta.picanm
if shades then
img.meta.shades = { count = numShades, data = shades }
end
if translucency then
img.meta.translucency = translucency
end
if next(looks) then
img.meta.lookups = looks
end
if #alts > 0 then
img.meta.altPalettes = alts
end
if truncated then
img.meta.truncated = true
end
img.meta.source = {
format = "art",
bitDepth = 8,
colorType = "palette",
version = 1,
numtiles = numtiles,
tileStart = first,
tileEnd = last,
buildart = prefixed,
palette = hasPalette,
trailing = math.max(0, limit - q),
}
return img, nil
end
-- ------------------------------------------------------------------------------------------------ encode
local function picanmWord(m: any, fallback: number?): number
if type(m) ~= "table" then
return fallback or 0
end
local function u8(v: any): number
return math.floor(tonumber(v) or 0) % 256
end
local t = TYPE_INDEX[m.type] or 0
return bit32.bor(
bit32.band(math.floor(tonumber(m.frames) or 0), 63),
bit32.lshift(t, 6),
bit32.lshift(u8(m.xoffset), 8),
bit32.lshift(u8(m.yoffset), 16),
bit32.lshift(bit32.band(math.floor(tonumber(m.speed) or 0), 15), 24),
bit32.lshift(bit32.band(math.floor(tonumber(m.extra) or 0), 15), 28)
)
end
function Art.encode(img: Types.Image, opts: EncodeOptions?): (buffer?, Err.Err?)
if type(img) ~= "table" or type(img.pixels) ~= "buffer" then
return nil, Err.new(Err.ARG, "needs an Image")
end
local o: EncodeOptions = opts or {}
local list: { Types.Image } = {}
if type(img.frames) == "table" and #img.frames > 0 then
for _, f in img.frames do
if type(f) ~= "table" or type(f.image) ~= "table" then
return nil, Err.new(Err.ARG, "bad frame")
end
table.insert(list, f.image)
end
else
list[1] = img
end
local meta = img.meta or {}
local src = meta.source or {}
local palSrc: any = o.palette or meta.palette
local palList: { { number } }? = nil
if palSrc ~= nil then
if type(palSrc) == "table" then
palList = palSrc
else
local dat = bytesOf(palSrc)
if not dat or buffer.len(dat) < 768 then
return nil, Err.new(Err.ARG, "palette must be PALETTE.DAT bytes or a list of colours")
end
palList = paletteList(makePalette(nil, dat))
end
end
local first = math.floor(tonumber(o.tileStart) or tonumber(src.tileStart) or tonumber(list[1].meta and list[1].meta.tile) or 0)
if first < 0 or first + #list - 1 > 0xFFFFFFFF then
return nil, Err.new(Err.ARG, "bad tileStart")
end
local n = #list
local total = 0
for i, t in list do
local w, h = t.width, t.height
if type(w) ~= "number" or type(h) ~= "number" or w < 0 or h < 0 or w > 32767 or h > 32767 or type(t.pixels) ~= "buffer" then
return nil, Err.new(Err.ARG, string.format("frame %d: size must be 0..32767", i))
end
if w * h > 0 then
if t.format == "rgba8" then
if not palList then
return nil, Err.new(Err.ARG, "rgba8 tiles need a palette (opts.palette or meta.palette)")
end
elseif t.format ~= "gray8" then
return nil, Err.new(Err.ARG, string.format("frame %d: needs rgba8 or gray8 (indices)", i))
end
if buffer.len(t.pixels) < w * h * (if t.format == "rgba8" then 4 else 1) then
return nil, Err.new(Err.ARG, string.format("frame %d: short pixel buffer", i))
end
end
total += w * h
end
local prefixed = if o.buildart ~= nil then o.buildart == true else src.buildart == true
local out = Bytes.writer((if prefixed then 8 else 0) + 16 + n * 8 + total)
if prefixed then
out:string("BUILDART")
end
out:u32le(1)
out:u32le(math.floor(tonumber(o.numtiles) or tonumber(src.numtiles) or 0) % 2 ^ 32)
out:u32le(first)
out:u32le(first + n - 1)
for _, t in list do
out:u16le(t.width)
end
for _, t in list do
out:u16le(t.height)
end
for _, t in list do
local m = t.meta or {}
out:u32le(picanmWord(m.picanm, m.source and tonumber(m.source.picanm)))
end
for _, t in list do
local w, h = t.width, t.height
if w * h > 0 then
local idx: buffer
if t.format == "rgba8" then
idx = Quantize.map(t.pixels, w, h, palList :: { { number } }, { transparentIndex = TRANSPARENT })
else
idx = t.pixels
end
for x = 0, w - 1 do
for y = 0, h - 1 do
out:u8(readu8(idx, y * w + x))
end
end
end
end
return out:finish(), nil
end
-- ------------------------------------------------------------------------------------------------ extras
Art.picanm = picanm
Art.picanmWord = picanmWord
return Art