XBM: X11 / X10 arrays, hex / decimal / octal, hotspot; xbmtopbm reference (netpbm, ImageMagick, Pillow files). XPM: XPM 3 + XPM 2, c / g / g4 / m keys, hex depths, None, CSS / X11 names + grey0..100 (image/xpm/Names); ImageMagick reference.
162 lines
6.0 KiB
Luau
162 lines
6.0 KiB
Luau
--!strict
|
|
-- image/xbm: X BitMap (X11 bitmap(1) C source; X10 16-bit variant; new code). Decode: the #define _width / _height
|
|
-- (and _x_hot / _y_hot: meta.hotspot), the char / short bits array in hex, decimal or octal, LSB first, set bits black,
|
|
-- clear bits white (opts.transparent: clear bits transparent instead). Encode: rgba8 thresholded (luminance below
|
|
-- opts.threshold, default 128, and alpha >= 128 is black), X11 format, opts.name (default "image").
|
|
|
|
-- ------------------------------------------------------------------------------------------------ requires
|
|
local Err = require("@formats/core/Err")
|
|
local Bytes = require("@formats/core/Bytes")
|
|
local Sniff = require("@formats/core/Sniff")
|
|
local Types = require("@formats/Types")
|
|
|
|
-- ------------------------------------------------------------------------------------------------ contract
|
|
local Xbm = {
|
|
name = "xbm",
|
|
category = "image",
|
|
extensions = { "xbm", "bm" },
|
|
mime = { "image/x-xbitmap", "image/x-xbm" },
|
|
deps = { "core/Err", "core/Bytes", "core/Sniff", "Types" },
|
|
caps = { decode = true, encode = true, depths = { 1 } },
|
|
}
|
|
|
|
-- ------------------------------------------------------------------------------------------------ types
|
|
export type DecodeOptions = {
|
|
transparent: boolean?, -- clear bits transparent (default false: white)
|
|
maxPixels: number?, -- refuse larger images (default 2^26)
|
|
}
|
|
export type EncodeOptions = {
|
|
name: string?, -- the C identifier prefix (default "image")
|
|
threshold: number?, -- luminance below this is black (default 128)
|
|
}
|
|
|
|
-- ------------------------------------------------------------------------------------------------ sniff
|
|
function Xbm.sniff(r: Bytes.Reader): number
|
|
local s = Sniff.text(r, 256, true)
|
|
if string.match(s, "^#define%s+[%w_]*_?width%s+%d+") or string.match(s, "^/%*.-%*/%s*#define%s+[%w_]*width%s+%d+") then
|
|
return 0.9
|
|
end
|
|
return 0
|
|
end
|
|
|
|
-- ------------------------------------------------------------------------------------------------ decode
|
|
function Xbm.decode(r: Bytes.Reader, opts: DecodeOptions?): (Types.Image?, Err.Err?)
|
|
local n = r.limit - r.base
|
|
if n > 2 ^ 26 then
|
|
return nil, Err.new(Err.LIMIT, "file too large")
|
|
end
|
|
local s = buffer.readstring(r.buf, r.base, n)
|
|
local w = tonumber(string.match(s, "#define%s+[%w_]-_?width%s+(%d+)"))
|
|
local h = tonumber(string.match(s, "#define%s+[%w_]-_?height%s+(%d+)"))
|
|
if not w or not h or w < 1 or h < 1 then
|
|
return nil, Err.new(Err.CORRUPT, "missing width / height defines")
|
|
end
|
|
local maxPixels = opts and opts.maxPixels or 2 ^ 26
|
|
if w * h > maxPixels then
|
|
return nil, Err.new(Err.LIMIT, string.format("%d x %d is over the %d pixel limit", w, h, maxPixels))
|
|
end
|
|
local brace = string.find(s, "{", 1, true)
|
|
if not brace then
|
|
return nil, Err.new(Err.CORRUPT, "missing bits array")
|
|
end
|
|
local decl = string.sub(s, 1, brace)
|
|
local short = string.find(decl, "short", 1, true) ~= nil
|
|
local unit = if short then 16 else 8
|
|
local per = (w + unit - 1) // unit
|
|
local want = per * h
|
|
local img = Types.Image.new(w, h, "rgba8")
|
|
local px = img.pixels
|
|
local clear = if opts and opts.transparent then 0x00FFFFFF else 0xFFFFFFFF
|
|
local got = 0
|
|
local body = string.gsub(string.sub(s, brace + 1), "/%*.-%*/", " ")
|
|
for tok in string.gmatch(body, "[%w]+") do
|
|
if got >= want then
|
|
break
|
|
end
|
|
local v
|
|
if string.match(tok, "^0[xX]%x+$") then
|
|
v = tonumber(string.sub(tok, 3), 16)
|
|
elseif string.match(tok, "^0%d+$") then
|
|
v = tonumber(tok, 8)
|
|
elseif string.match(tok, "^%d+$") then
|
|
v = tonumber(tok)
|
|
end
|
|
if v then
|
|
local y, cx = got // per, (got % per) * unit
|
|
for k = 0, unit - 1 do
|
|
local x = cx + k
|
|
if x < w then
|
|
buffer.writeu32(px, (y * w + x) * 4, if bit32.band(bit32.rshift(v, k), 1) == 1 then 0xFF000000 else clear)
|
|
end
|
|
end
|
|
got += 1
|
|
end
|
|
end
|
|
if got < want then
|
|
for i = got * unit, want * unit - 1 do
|
|
local y, x = i // (per * unit), i % (per * unit)
|
|
if x < w then
|
|
buffer.writeu32(px, (y * w + x) * 4, clear)
|
|
end
|
|
end
|
|
img.meta.truncated = true
|
|
end
|
|
local hx = tonumber(string.match(s, "#define%s+[%w_]-_?x_hot%s+(%d+)"))
|
|
local hy = tonumber(string.match(s, "#define%s+[%w_]-_?y_hot%s+(%d+)"))
|
|
if hx and hy then
|
|
img.meta.hotspot = { x = hx, y = hy }
|
|
end
|
|
local name = string.match(s, "#define%s+([%w_]-)_?width%s")
|
|
img.meta.source = { format = "xbm", bitDepth = 1, name = name, x10 = short }
|
|
return img, nil
|
|
end
|
|
|
|
-- ------------------------------------------------------------------------------------------------ encode
|
|
function Xbm.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, "needs an rgba8 Image")
|
|
end
|
|
local w, h = img.width, img.height
|
|
if w < 1 or h < 1 then
|
|
return nil, Err.new(Err.ARG, "bad size")
|
|
end
|
|
local name = opts and opts.name or "image"
|
|
if not string.match(name, "^[%a_][%w_]*$") then
|
|
return nil, Err.new(Err.ARG, "name must be a C identifier")
|
|
end
|
|
local th = opts and opts.threshold or 128
|
|
local px = img.pixels
|
|
local parts = {
|
|
string.format("#define %s_width %d\n#define %s_height %d\n", name, w, name, h),
|
|
}
|
|
local hs: { x: number, y: number }? = img.meta and img.meta.hotspot
|
|
if hs then
|
|
table.insert(parts, string.format("#define %s_x_hot %d\n#define %s_y_hot %d\n", name, hs.x, name, hs.y))
|
|
end
|
|
table.insert(parts, string.format("static unsigned char %s_bits[] = {\n", name))
|
|
local per = (w + 7) // 8
|
|
local bytes = {}
|
|
for y = 0, h - 1 do
|
|
for bx = 0, per - 1 do
|
|
local v = 0
|
|
for k = 0, 7 do
|
|
local x = bx * 8 + k
|
|
if x < w then
|
|
local o = (y * w + x) * 4
|
|
local lum = 0.299 * buffer.readu8(px, o) + 0.587 * buffer.readu8(px, o + 1) + 0.114 * buffer.readu8(px, o + 2)
|
|
if lum < th and buffer.readu8(px, o + 3) >= 128 then
|
|
v += bit32.lshift(1, k)
|
|
end
|
|
end
|
|
end
|
|
table.insert(bytes, string.format("0x%02x", v))
|
|
end
|
|
end
|
|
for i = 1, #bytes, 12 do
|
|
table.insert(parts, " " .. table.concat(bytes, ", ", i, math.min(i + 11, #bytes)) .. (if i + 11 < #bytes then ",\n" else " };\n"))
|
|
end
|
|
return buffer.fromstring(table.concat(parts)), nil
|
|
end
|
|
|
|
return Xbm
|