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

+1
View File
@@ -11,6 +11,7 @@ is tested under both runtimes (luau `--codegen -O2` and LuaJIT through the build
| image | gif | .gif | yes | yes | animation: true, alpha: true, palette: true, depths: 1, 2, 4, 8 | `tests/image/gif.luau` |
| image | hdr | .hdr .pic .rgbe | yes | yes | alpha: false, float: true, depths: 32 | `tests/image/hdr.luau` |
| image | ico | .ico .cur | yes | yes | alpha: true, frames: true, depths: 1, 4, 8, 16, 24, 32 | `tests/image/ico.luau` |
| image | ilbm | .iff .ilbm .lbm .pbm | yes | yes | alpha: true, palette: true, depths: 1, 2, 3, 4, 5, 6, 7, 8, 24, 32 | `tests/image/ilbm.luau` |
| image | jpeg | .jpg .jpeg .jpe .jfif | yes | yes | lossy: true, alpha: false, depths: 8, 12, progressive: true, arithmetic: true | `tests/image/jpeg.luau` |
| image | ktx | .ktx2 .ktx | yes | yes | alpha: true, float: true, mipmaps: true, cubemap: true, compressed: "bc1", "bc2", "bc3", "bc4", "bc5", "bc6h", "bc7", "etc1", "etc2", "eac" | `tests/image/ktx.luau` |
| image | pcx | .pcx .pcc .dcx | yes | yes | alpha: true, palette: true, depths: 1, 2, 4, 8, 24, 32 | `tests/image/pcx.luau` |
+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
+72
View File
@@ -0,0 +1,72 @@
-- image/ilbm tests: files from netpbm's ppmtoilbm (palette, uncompressed, HAM6, HAM8, 24-bit) and hand-written ones
-- (EHB, chunky PBM, a mask plane, a transparent colour) against ilbmtoppm (colours) and the written masks (alpha); our
-- encoder's files read by ilbmtoppm; round trips (palette, 24 and 32 planes, uncompressed); errors; the fuzz no-throw
-- pass. Fixtures: tools/fixtures/ilbm.py.
local T = require("@tests/T")
local Ilbm = require("@formats/image/ilbm")
local Bytes = require("@formats/core/Bytes")
local Err = require("@formats/core/Err")
local R = require("@tests/image/_raster")
local Fx = require("@tests/gen/ilbm")
local t = T.suite("ilbm")
t:eq(Ilbm.name, "ilbm", "name")
t:check(Ilbm.caps.decode and Ilbm.caps.encode, "caps")
local function check(c: any)
local img, err = Ilbm.decode(Bytes.reader(buffer.fromstring(c.data)))
t:check(img, c.name .. ": " .. tostring(err))
t:check(img.width == c.w and img.height == c.h, c.name .. ": size")
local ref = buffer.fromstring(c.rgba)
local bad, badA = 0, 0
for i = 0, c.w * c.h - 1 do
local transparent = c.transparent and c.transparent[i + 1]
local v = buffer.readu32(img.pixels, i * 4)
if transparent then
badA += if v == 0 then 0 else 1
else
for k = 0, 3 do
if math.abs(buffer.readu8(img.pixels, i * 4 + k) - buffer.readu8(ref, i * 4 + k)) > (c.tol or 0) then
bad += 1
break
end
end
end
end
t:eq(bad, 0, c.name .. ": colours against ilbmtoppm")
t:eq(badA, 0, c.name .. ": transparent pixels")
return img
end
for _, c in Fx.cases do
check(c)
if not c.transparent and not c.tol then
R.roundTrip(t, Ilbm, R.image(c.w, c.h, c.rgba), nil, c.name .. " re-encoded")
R.roundTrip(t, Ilbm, R.image(c.w, c.h, c.rgba), { compress = false }, c.name .. " re-encoded uncompressed")
end
end
for _, o in Fx.ours do
check(o)
end
t:note("%d files and %d of ours match ilbmtoppm", #Fx.cases, #Fx.ours)
do
local img = R.image(3, 2, string.rep("\1\2\3\128", 6))
local enc = R.roundTrip(t, Ilbm, img, nil, "alpha: 32 planes") :: buffer
t:eq(Ilbm.sniff(Bytes.reader(enc)), 1, "sniff")
t:eq(Ilbm.sniff(Bytes.reader(buffer.fromstring("FORM\0\0\0\4AIFF"))), 0, "sniff AIFF")
local back = Ilbm.decode(Bytes.reader(enc)) :: any
t:eq(back.meta.source.planes, 32, "32 planes")
local _, e = Ilbm.decode(Bytes.reader(buffer.fromstring("FORM\0\0\0\4ILBM")))
t:check(e and e.code == Err.CORRUPT, "no BMHD")
local _, e2 = Ilbm.decode(Bytes.reader(enc), { maxPixels = 2 })
t:check(e2 and e2.code == Err.LIMIT, "limit")
for _, c in Fx.cases do
if c.name == "hand_transparent.iff" then
local im = Ilbm.decode(Bytes.reader(buffer.fromstring(c.data))) :: any
t:eq(im.meta.text and im.meta.text.name, "tiny", "NAME chunk")
end
end
end
t:fuzz("decode", function(b: buffer)
return Ilbm.decode(Bytes.reader(b))
end, R.samples(Fx.cases), { mutations = 6 })
t:done()
+2
View File
@@ -140,6 +140,8 @@ local function simple(): any
return require("@formats/image/xbm")
elseif which == "xpm" then
return require("@formats/image/xpm")
elseif which == "ilbm" then
return require("@formats/image/ilbm")
end
return nil
end
+121
View File
@@ -0,0 +1,121 @@
# IFF ILBM fixtures (tools/fixtures.py): files from netpbm's ppmtoilbm (palette ByteRun1 / uncompressed, few planes,
# HAM6, HAM8, 24-bit deep) and hand-written ones (EHB, chunky PBM, a mask plane, a transparent colour index, an odd
# CMAP), decoded by ilbmtoppm as the reference (alpha separately: ilbmtoppm writes PPM; HAM values within the scaling
# difference: ilbmtoppm shifts, we replicate bits); our encoder's files
# (tools/fixtures/_tex_encode.luau -a ilbm) read by ilbmtoppm.
# tests/gen/ilbm.luau { cases = { { name, data, w, h, rgba, transparent? } }, ours = { ... } }
import importlib.util, os, random, struct
def _load(n):
spec = importlib.util.spec_from_file_location(n, os.path.join(os.path.dirname(os.path.abspath(__file__)), n + ".py"))
m = importlib.util.module_from_spec(spec)
spec.loader.exec_module(m)
return m
R, S = _load("_raster_ref"), _load("_simple")
INPUTS = ["tools/fixtures/_tex_encode.luau", "src/Formats/image/ilbm"]
def chunk(cid, data):
return cid + struct.pack(">I", len(data)) + data + (b"\0" if len(data) % 2 else b"")
def byterun1(row):
out = bytearray()
i = 0
while i < len(row):
run = 1
while i + run < len(row) and run < 128 and row[i + run] == row[i]:
run += 1
if run >= 2:
out += bytes((257 - run, row[i]))
i += run
else:
j = i
while j < len(row) and j - i < 128 and not (j + 1 < len(row) and row[j + 1] == row[j]):
j += 1
j = max(j, i + 1)
out += bytes((j - i - 1,)) + row[i:j]
i = j
return bytes(out)
def planar(w, h, idx, planes, mask=None, comp=True):
rb = ((w + 15) // 16) * 2
body = bytearray()
for y in range(h):
rows = []
for k in range(planes):
row = bytearray(rb)
for x in range(w):
if (idx[y * w + x] >> k) & 1:
row[x // 8] |= 0x80 >> (x % 8)
rows.append(bytes(row))
if mask is not None:
row = bytearray(rb)
for x in range(w):
if mask[y * w + x]:
row[x // 8] |= 0x80 >> (x % 8)
rows.append(bytes(row))
for row in rows:
body += byterun1(row) if comp else row
return bytes(body)
def ilbm(w, h, planes, body, cmap=b"", camg=None, masking=0, trans=0, comp=1, form=b"ILBM", extra=b""):
bmhd = struct.pack(">HHhhBBBxHBBhh", w, h, 0, 0, planes, masking, comp, trans, 10, 11, w, h)
data = form + chunk(b"BMHD", bmhd) + (chunk(b"CMAP", cmap) if cmap else b"") + (chunk(b"CAMG", struct.pack(">I", camg)) if camg is not None else b"") + extra + chunk(b"BODY", body)
return b"FORM" + struct.pack(">I", len(data)) + data
def generate(ctx):
rng = random.Random(8)
w = R.Work(ctx)
try:
w.png("photo.png", 37, 23, R.photo(37, 23))
w.png("pal.png", 31, 19, R.palette_image(31, 19, 60))
w.png("pal8.png", 26, 11, R.palette_image(26, 11, 7))
w.sh("netpbm", ["pngtopam photo.png | pamtopnm > photo.ppm", "pngtopam pal.png | pamtopnm > pal.ppm", "pngtopam pal8.png | pamtopnm > pal8.ppm",
"ppmtoilbm pal.ppm > nb_pal.iff", "ppmtoilbm -nocompress pal.ppm > nb_pal_raw.iff", "ppmtoilbm pal8.ppm > nb_3planes.iff",
"ppmtoilbm -ham6 photo.ppm > nb_ham6.iff", "ppmtoilbm -ham8 photo.ppm > nb_ham8.iff", "ppmtoilbm -24force photo.ppm > nb_24.iff"])
hand = []
# EHB: 6 planes, 32 colours
W, H = 20, 7
cm = bytes(rng.randrange(256) for _ in range(32 * 3))
idx = [rng.randrange(64) for _ in range(W * H)]
hand.append(("hand_ehb.iff", ilbm(W, H, 6, planar(W, H, idx, 6), cm, 0x80), None))
# chunky PBM (rows padded to even), 256 colours
cm2 = bytes(rng.randrange(256) for _ in range(256 * 3))
W2 = 14
idx2 = [rng.randrange(256) for _ in range(W2 * H)]
hand.append(("hand_pbm.lbm", ilbm(W2, H, 8, b"".join(byterun1(bytes(idx2[y * W2:(y + 1) * W2])) for y in range(H)), cm2, form=b"PBM "), None))
# a mask plane and a transparent colour index
cm3 = bytes(rng.randrange(256) for _ in range(16 * 3))
idx3 = [rng.randrange(16) for _ in range(W * H)]
mask = [rng.random() < 0.7 for _ in range(W * H)]
hand.append(("hand_mask.iff", ilbm(W, H, 4, planar(W, H, idx3, 4, mask), cm3, masking=1), [not m for m in mask]))
hand.append(("hand_transparent.iff", ilbm(W, H, 4, planar(W, H, idx3, 4, comp=False), cm3, masking=2, trans=5, comp=0,
extra=chunk(b"NAME", b"tiny\0")), [i == 5 for i in idx3]))
for n, data, _ in hand:
w.write(n, data)
names = sorted(f for f in os.listdir(w.dir) if f.endswith(".iff") or f.endswith(".lbm"))
mine = S.ours(ctx, "ilbm")
for n, d in mine.items():
w.write(n, d)
allnames = names + sorted(mine)
allnames = [n for n in allnames if n != "ours_alpha.ilbm"] # ilbmtoppm reads no 32-plane files
w.sh("netpbm", ["ilbmtoppm %s > %s.ppm" % (n, n) for n in allnames])
tmap = {n: t for n, _, t in hand}
out = {"cases": [], "ours": []}
for n in allnames:
ww, hh, rgba = R.pnm_rgba(w.read(n + ".ppm"))
c = {"name": n, "data": w.read(n), "w": ww, "h": hh, "rgba": rgba}
if "ham" in n:
# ilbmtoppm scales HAM values by shifting (4-bit v -> v << 4); we replicate the bits (v * 17)
c["tol"] = 15 if "ham6" in n else 3
if tmap.get(n):
c["transparent"] = [bool(v) for v in tmap[n]]
out["ours" if n.startswith("ours_") else "cases"].append(c)
ctx.emit("ilbm", out)
finally:
w.close()