Tested against libicns (png2icns / icns2png -x), Pillow's writer and a hand-written file.
74 lines
4.4 KiB
Python
74 lines
4.4 KiB
Python
# ICNS fixtures (tools/fixtures.py): a libicns png2icns file (is32 / il32 / ih32 RLE entries with their masks, ic07 /
|
|
# ic08 PNG entries), a JPEG 2000 entry that must be skipped (hand-written into hand.icns), a Pillow file (PNG entries), a hand-written one (ics# / ICN#
|
|
# 1-bit icons, an ic04 ARGB entry, an uncompressed is32); every entry's reference pixels from icns2png -x (libicns)
|
|
# or written here; our encoder's RLE file (tools/fixtures/_tex_encode.luau -a icns) read back by icns2png.
|
|
# tests/gen/icns.luau { cases = { { name, data, entries = { { type, w, h, rgba } }, skipped } }, ours = { ... } }
|
|
import importlib.util, os, re, 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, T = _load("_raster_ref"), _load("_simple"), _load("_tex_reference")
|
|
INPUTS = ["tools/fixtures/_tex_encode.luau", "src/Formats/image/icns"]
|
|
|
|
|
|
def extract(ctx, w, name):
|
|
"""icns2png -x name -> { 'WxH': rgba } (one per extracted entry)"""
|
|
log = w.sh("libicns", ["mkdir -p x_%s && cd x_%s && icns2png -x ../%s" % (name, name, name)]).decode()
|
|
files = sorted(f for f in os.listdir(w.path("x_" + name)) if f.endswith(".png"))
|
|
out = {}
|
|
for f, (mode, ww, hh, raw) in zip(files, T.pillow(ctx, [w.read(os.path.join("x_" + name, f)) for f in files])):
|
|
out["%dx%d" % (ww, hh)] = T.rgba_from(mode, raw)
|
|
return out
|
|
|
|
|
|
def generate(ctx):
|
|
w = R.Work(ctx)
|
|
try:
|
|
for s in (16, 32, 48, 128, 256):
|
|
w.png("i%d.png" % s, s, s, R.photo(s, s, s, True))
|
|
w.sh("libicns", ["png2icns libicns.icns i16.png i32.png i48.png i128.png i256.png"])
|
|
w.png("p64.png", 64, 64, R.photo(64, 64, 9, True))
|
|
ctx.python(["pillow"], "from PIL import Image\nimport os\nos.chdir(%r)\nImage.open('p64.png').save('pillow.icns')\n" % w.dir)
|
|
cases = []
|
|
lib = extract(ctx, w, "libicns.icns")
|
|
cases.append({"name": "libicns.icns", "data": w.read("libicns.icns"), "skipped": 0,
|
|
"entries": [{"type": t, "w": s, "h": s, "rgba": lib["%dx%d" % (s, s)]} for t, s in (("is32", 16), ("il32", 32), ("ih32", 48), ("ic07", 128), ("ic08", 256))]})
|
|
pil = extract(ctx, w, "pillow.icns")
|
|
cases.append({"name": "pillow.icns", "data": w.read("pillow.icns"), "skipped": 0,
|
|
"entries": [{"type": "", "w": int(k.split("x")[0]), "h": int(k.split("x")[1]), "rgba": v} for k, v in sorted(pil.items())]})
|
|
# hand-written: ics# (16x16 1-bit + mask), an uncompressed is32 + s8mk, an ic04 ARGB with RLE runs
|
|
bits = bytes((0xF0 if (i // 2) % 2 else 0x0F) for i in range(32))
|
|
mask = bytes((0xFF if i < 24 else 0x00) for i in range(32))
|
|
mono = []
|
|
for i in range(256):
|
|
b = (bits[i // 8] >> (7 - i % 8)) & 1
|
|
m = (mask[i // 8] >> (7 - i % 8)) & 1
|
|
mono += [0, 0, 0] if b else [255, 255, 255]
|
|
mono.append(255 if m else 0)
|
|
raw = bytes((i * 3 + k * 50) % 256 for k in range(3) for i in range(256))
|
|
alpha = bytes((i * 7) % 256 for i in range(256))
|
|
rgb = bytes(v for i in range(256) for v in (raw[i], raw[256 + i], raw[512 + i], alpha[i]))
|
|
def ch(t, d):
|
|
return t + struct.pack(">I", len(d) + 8) + d
|
|
# ARGB channels: A = 256 x 77 (two runs of 128), then R / G / B as runs
|
|
argb = b"ARGB" + bytes((253, 77, 253, 77)) + bytes((253, 10, 253, 10)) + bytes((253, 20, 253, 20)) + bytes((253, 30, 253, 30))
|
|
jp2 = b"\0\0\0\x0cjP \r\n\x87\n" + b"\0" * 20
|
|
body = ch(b"ics#", bits + mask) + ch(b"is32", raw) + ch(b"s8mk", alpha) + ch(b"ic04", argb) + ch(b"ic09", jp2)
|
|
hand = b"icns" + struct.pack(">I", len(body) + 8) + body
|
|
cases.append({"name": "hand.icns", "data": hand, "skipped": 1, "entries": [
|
|
{"type": "ics#", "w": 16, "h": 16, "rgba": bytes(mono)}, {"type": "is32", "w": 16, "h": 16, "rgba": rgb},
|
|
{"type": "ic04", "w": 16, "h": 16, "rgba": bytes((10, 20, 30, 77)) * 256}]})
|
|
mine = S.ours(ctx, "icns")
|
|
ours = []
|
|
for n, d in mine.items():
|
|
w.write(n, d)
|
|
ex = extract(ctx, w, n)
|
|
ours.append({"name": n, "data": d, "entries": [{"type": "", "w": int(k.split("x")[0]), "h": int(k.split("x")[1]), "rgba": v} for k, v in sorted(ex.items())]})
|
|
ctx.emit("icns", {"cases": cases, "ours": ours})
|
|
finally:
|
|
w.close()
|