ws-tex: image/ilbm (IFF ILBM / PBM read: planar, EHB, HAM6/8, deep 24/32, masks; write palette / 24 / 32 planes)

Chunks through containers/Chunks, ByteRun1 through codecs/compress/PackBits. Tested against ppmtoilbm files and
hand-written EHB / PBM / mask / transparent-colour files decoded by ilbmtoppm; our files read by ilbmtoppm.
This commit is contained in:
4DBug committed 2026-10-31 15:03:24 -05:00
1 parent ef460955d4
commit ce4845ae13
6 files changed
+532

No files matched your search

+12
View File
@@ -86,6 +86,18 @@ local list: { Entry } = {
return require("@formats/image/ico")
end,
},
{
name = "ilbm",
category = "image",
path = "image/ilbm",
extensions = { "iff", "ilbm", "lbm", "pbm" },
mime = { "image/x-ilbm", "image/x-iff" },
deps = { "core/Err", "core/Bytes", "Types", "containers/Chunks", "codecs/compress/PackBits", "codecs/image/Pixels" },
caps = { decode = true, encode = true, alpha = true, palette = true, depths = { 1, 2, 3, 4, 5, 6, 7, 8, 24, 32 } },
load = function()
return require("@formats/image/ilbm")
end,
},
{
name = "jpeg",
category = "image",
+324
View File
@@ -0,0 +1,324 @@
--!strict
-- image/ilbm: IFF ILBM and PBM pictures (EA IFF 85 "ILBM" standard, the Amiga ROM Kernel Manual; new code; chunks
-- through containers/Chunks, ByteRun1 through codecs/compress/PackBits). Decode: planar ILBM with 1..8 planes and a
-- CMAP (grey without one), EHB (extra half-brite, CAMG 0x80), HAM6 / HAM8 (CAMG 0x800), deep 24 / 32-plane RGB(A),
-- chunky PBM (8-bit, Deluxe Paint), masking 1 (a mask plane) and 2 (a transparent colour index), uncompressed or
-- ByteRun1 bodies; opts.adjustColors scales 4-bit OCS palettes (all low nibbles zero) to 8 bits; NAME / ANNO / AUTH
-- text (meta.text). HAM channel values replicate their bits (4-bit v -> v * 17; ilbmtoppm shifts: v * 16). Encode: rgba8 as a planar ILBM: up to 256 opaque colours with a CMAP and the fewest planes, else
-- 24 planes (32 with alpha), ByteRun1 (default) or uncompressed. Not supported: animations (ANIM), the rarer
-- encodings (RGBN / RGB8 / ACBM / DEEP chunks: UNSUPPORTED), SHAM / PCHG palette changes (ignored).
-- ------------------------------------------------------------------------------------------------ requires
local Err = require("@formats/core/Err")
local Bytes = require("@formats/core/Bytes")
local Types = require("@formats/Types")
local Chunks = require("@formats/containers/Chunks")
local PackBits = require("@formats/codecs/compress/PackBits")
local Pixels = require("@formats/codecs/image/Pixels")
-- ------------------------------------------------------------------------------------------------ contract
local Ilbm = {
name = "ilbm",
category = "image",
extensions = { "iff", "ilbm", "lbm", "pbm" },
mime = { "image/x-ilbm", "image/x-iff" },
deps = { "core/Err", "core/Bytes", "Types", "containers/Chunks", "codecs/compress/PackBits", "codecs/image/Pixels" },
caps = { decode = true, encode = true, alpha = true, palette = true, depths = { 1, 2, 3, 4, 5, 6, 7, 8, 24, 32 } },
}
-- ------------------------------------------------------------------------------------------------ types
export type DecodeOptions = {
adjustColors: boolean?, -- scale 4-bit OCS palettes (every low nibble 0) to 8 bits (default false)
maxPixels: number?, -- refuse larger images (default 2^26)
}
export type EncodeOptions = {
compress: boolean?, -- ByteRun1 (default true)
}
local readu8, readu16 = buffer.readu8, buffer.readu16
local function be16(b: buffer, o: number): number
return bit32.rshift(bit32.byteswap(readu16(b, o)), 16)
end
-- ------------------------------------------------------------------------------------------------ sniff
function Ilbm.sniff(r: Bytes.Reader): number
if r:peek(4) ~= "FORM" then
return 0
end
local t = r:peek(4, 8)
return if t == "ILBM" or t == "PBM " then 1 else 0
end
-- ------------------------------------------------------------------------------------------------ decode
function Ilbm.decode(r: Bytes.Reader, opts: DecodeOptions?): (Types.Image?, Err.Err?)
local b, o, limit = r.buf, r.base, r.limit
if limit - o < 12 or buffer.readstring(b, o, 4) ~= "FORM" then
return nil, Err.new(Err.CORRUPT, "not an IFF FORM", o)
end
local form = buffer.readstring(b, o + 8, 4)
if form ~= "ILBM" and form ~= "PBM " then
return nil, Err.new(Err.UNSUPPORTED, "FORM " .. form, o + 8)
end
local chunky = form == "PBM "
local list = Chunks.walk(b, o + 12, math.min(limit, o + 8 + bit32.byteswap(buffer.readu32(b, o + 4))), Chunks.IFF)
local bmhd, cmap, body = Chunks.find(list, "BMHD"), Chunks.find(list, "CMAP"), Chunks.find(list, "BODY")
if not bmhd or bmhd.avail < 20 then
return nil, Err.new(Err.CORRUPT, "missing BMHD", o + 12)
end
if not body then
return nil, Err.new(Err.TRUNCATED, "missing BODY", o + 12)
end
local q = bmhd.offset
local w, h = be16(b, q), be16(b, q + 2)
local planes, masking, comp = readu8(b, q + 8), readu8(b, q + 9), readu8(b, q + 10)
local trans = be16(b, q + 12)
if w == 0 or h == 0 then
return nil, Err.new(Err.CORRUPT, "bad size", q)
end
if comp > 1 then
return nil, Err.new(Err.UNSUPPORTED, "compression " .. comp, q + 10)
end
if not (planes >= 1 and planes <= 8 or planes == 24 or planes == 32) or (chunky and planes ~= 8) then
return nil, Err.new(Err.UNSUPPORTED, planes .. " planes", q + 8)
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 camg = 0
local c = Chunks.find(list, "CAMG")
if c and c.avail >= 4 then
camg = bit32.byteswap(buffer.readu32(b, c.offset))
end
local ham = bit32.band(camg, 0x800) ~= 0 and (planes == 6 or planes == 8)
local ehb = bit32.band(camg, 0x80) ~= 0 and planes == 6 and not ham
-- palette
local pal = buffer.create(1024)
local ncol = 0
if cmap then
ncol = math.min(256, cmap.avail // 3)
local low = true
for i = 0, ncol * 3 - 1 do
low = low and bit32.band(readu8(b, cmap.offset + i), 15) == 0
end
local adjust = low and opts and opts.adjustColors
for i = 0, ncol - 1 do
local function ch(k: number): number
local v = readu8(b, cmap.offset + i * 3 + k)
return if adjust then v + bit32.rshift(v, 4) else v
end
buffer.writeu32(pal, i * 4, ch(0) + ch(1) * 256 + ch(2) * 65536 + 0xFF000000)
end
end
if ncol == 0 and planes <= 8 then
local n = bit32.lshift(1, planes)
for i = 0, n - 1 do
local g = (i * 255) // math.max(1, n - 1)
buffer.writeu32(pal, i * 4, g * 0x10101 + 0xFF000000)
end
ncol = n
end
if ehb then
for i = 0, 31 do
local v = buffer.readu32(pal, i * 4)
local rr, gg, bb = bit32.band(v, 255), bit32.band(bit32.rshift(v, 8), 255), bit32.band(bit32.rshift(v, 16), 255)
buffer.writeu32(pal, (32 + i) * 4, bit32.rshift(rr, 1) + bit32.rshift(gg, 1) * 256 + bit32.rshift(bb, 1) * 65536 + 0xFF000000)
end
end
-- the body, decompressed
local rowBytes = if chunky then w + w % 2 else ((w + 15) // 16) * 2
local total = if chunky then rowBytes * h else rowBytes * (planes + (if masking == 1 then 1 else 0)) * h
local raw = buffer.create(total)
local truncated
if comp == 1 then
local written = PackBits.decode(b, body.offset, body.offset + body.avail, raw, 0, total)
truncated = written < total
else
local n = math.min(total, body.avail)
buffer.copy(raw, 0, b, body.offset, n)
truncated = n < total
end
local img = Types.Image.new(w, h, "rgba8")
local px = img.pixels
if chunky then
Pixels.indexed(raw, 0, rowBytes, 8, px, w, h, true, pal)
if masking == 2 then
for i = 0, w * h - 1 do
local y, x = i // w, i % w
if readu8(raw, y * rowBytes + x) == trans then
buffer.writeu32(px, i * 4, 0)
end
end
end
else
local stride = rowBytes * (planes + (if masking == 1 then 1 else 0))
local deep = planes >= 24
local bits = planes - 2
for y = 0, h - 1 do
local row = y * stride
local pr: number, pg: number, pb: number = 0, 0, 0
if ham then
local v = buffer.readu32(pal, 0)
pr = bit32.band(v, 255)
pg = bit32.band(bit32.rshift(v, 8), 255)
pb = bit32.band(bit32.rshift(v, 16), 255)
end
for x = 0, w - 1 do
local byteAt, shift = bit32.rshift(x, 3), 7 - bit32.band(x, 7)
local idx = 0
for k = 0, planes - 1 do
idx += bit32.band(bit32.rshift(readu8(raw, row + k * rowBytes + byteAt), shift), 1) * 2 ^ k
end
local out: number
if deep then
local a = if planes == 32 then bit32.rshift(idx, 24) % 256 else 255
out = bit32.band(idx, 0xFFFFFF) + a * 16777216
elseif ham then
local ctl = bit32.rshift(idx, bits)
local val = bit32.band(idx, bit32.lshift(1, bits) - 1)
local v8 = if bits == 4 then val * 17 else val * 4 + bit32.rshift(val, 4)
if ctl == 0 then
local v = buffer.readu32(pal, val * 4)
pr = bit32.band(v, 255)
pg = bit32.band(bit32.rshift(v, 8), 255)
pb = bit32.band(bit32.rshift(v, 16), 255)
elseif ctl == 1 then
pb = v8
elseif ctl == 2 then
pr = v8
else
pg = v8
end
out = pr + pg * 256 + pb * 65536 + 0xFF000000
else
out = buffer.readu32(pal, bit32.band(idx, 255) * 4)
if masking == 2 and idx == trans then
out = 0
end
end
if masking == 1 then
local m = bit32.band(bit32.rshift(readu8(raw, row + planes * rowBytes + byteAt), shift), 1)
if m == 0 then
out = 0
end
end
buffer.writeu32(px, (y * w + x) * 4, out)
end
end
end
if ncol > 0 and planes <= 8 and not ham then
local plist: { { number } } = {}
for i = 0, (if ehb then 64 else ncol) - 1 do
local v = buffer.readu32(pal, i * 4)
plist[i + 1] = { bit32.band(v, 255), bit32.band(bit32.rshift(v, 8), 255), bit32.band(bit32.rshift(v, 16), 255), 255 }
end
img.meta.palette = plist
end
local text: { [string]: string } = {}
for _, ch in list do
if ch.id == "NAME" or ch.id == "ANNO" or ch.id == "AUTH" or ch.id == "(c) " then
local s = buffer.readstring(b, ch.offset, ch.avail)
local key = string.gsub(ch.id, "[^%w]", "")
text[string.lower(key)] = (string.gsub(s, "%z+$", ""))
end
end
if next(text) then
img.meta.text = text
end
if truncated then
img.meta.truncated = true
end
img.meta.source = { format = "ilbm", form = form, planes = planes, bitDepth = planes, ham = ham, ehb = ehb, masking = masking, compression = comp, camg = camg }
return img, nil
end
-- ------------------------------------------------------------------------------------------------ encode
function Ilbm.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 or w > 65535 or h > 65535 then
return nil, Err.new(Err.ARG, "size must be 1..65535")
end
local px = img.pixels
local cols, alpha = Pixels.colours(px, w * h, 256)
local usePal = not alpha and #cols <= 256
local planes = if usePal then 1 elseif alpha then 32 else 24
if usePal then
while bit32.lshift(1, planes) < #cols do
planes += 1
end
end
local compress = not (opts and opts.compress == false)
local index: { [number]: number } = {}
for i, cv in cols do
index[cv] = i - 1
end
local rowBytes = ((w + 15) // 16) * 2
local body = Bytes.writer(rowBytes * planes * h)
local row = buffer.create(rowBytes)
for y = 0, h - 1 do
for k = 0, planes - 1 do
buffer.fill(row, 0, 0, rowBytes)
for x = 0, w - 1 do
local v = buffer.readu32(px, (y * w + x) * 4)
local idx = if usePal then (index[bit32.band(v, 0xFFFFFF)] or 0) else v
if bit32.band(bit32.rshift(idx, k), 1) == 1 then
local at = bit32.rshift(x, 3)
buffer.writeu8(row, at, bit32.bor(readu8(row, at), bit32.rshift(0x80, bit32.band(x, 7))))
end
end
if compress then
body:bytes(PackBits.encode(row, 0, rowBytes))
else
body:bytes(row)
end
end
end
local bodyData = body:finish()
local out = Bytes.writer(buffer.len(bodyData) + 900)
out:string("FORM")
out:u32be(0)
out:string("ILBM")
out:string("BMHD")
out:u32be(20)
out:u16be(w)
out:u16be(h)
out:u16be(0)
out:u16be(0)
out:u8(planes)
out:u8(0)
out:u8(if compress then 1 else 0)
out:u8(0)
out:u16be(0)
out:u8(1)
out:u8(1)
out:u16be(w)
out:u16be(h)
if usePal then
local n = bit32.lshift(1, planes)
out:string("CMAP")
out:u32be(n * 3)
for i = 1, n do
local cv = cols[i] or 0
out:u8(bit32.band(cv, 255))
out:u8(bit32.band(bit32.rshift(cv, 8), 255))
out:u8(bit32.rshift(cv, 16))
end
if (n * 3) % 2 == 1 then
out:u8(0)
end
end
out:string("BODY")
out:u32be(buffer.len(bodyData))
out:bytes(bodyData)
if buffer.len(bodyData) % 2 == 1 then
out:u8(0)
end
local res = out:finish()
buffer.writeu32(res, 4, bit32.byteswap(buffer.len(res) - 8))
return res, nil
end
return Ilbm