94 lines
4.3 KiB
Python
94 lines
4.3 KiB
Python
# ICO / CUR fixtures (tools/fixtures.py): icons written by ImageMagick (auto-resize sets with a 256 PNG entry) and
|
|
# Pillow (PNG and BMP entries), and hand-built ones from Pillow's BMP writer (1 / 4 / 8 / 24 / 32-bit DIBs with AND
|
|
# masks, a cursor with hotspots); references = Pillow's RGBA decode of every entry, in file order.
|
|
# tests/gen/ico.luau { { name, data, entries = { { width, height, rgba (bytes) } } } }
|
|
import struct
|
|
|
|
CODE = r'''
|
|
import io, struct, subprocess, sys
|
|
from PIL import Image, IcoImagePlugin
|
|
magick = sys.argv[1]
|
|
out = []
|
|
def refs(data):
|
|
if data[2] == 2: # IcoFile reads icons only: decode a cursor's entries as an icon's
|
|
data = data[:2] + b"\x01" + data[3:]
|
|
f = IcoImagePlugin.IcoFile(io.BytesIO(data))
|
|
order = sorted(range(len(f.entry)), key=lambda i: f.entry[i].offset)
|
|
res = []
|
|
for i in order:
|
|
im = f.frame(i).convert("RGBA")
|
|
res.append((im.size[0], im.size[1], im.tobytes()))
|
|
return res
|
|
def emit(name, data):
|
|
out.append((name, data, refs(data)))
|
|
src = Image.new("RGBA", (64, 64))
|
|
px = src.load()
|
|
for y in range(64):
|
|
for x in range(64):
|
|
a = 255 if (x - 32) ** 2 + (y - 32) ** 2 < 900 else (0 if x < 8 else 128)
|
|
px[x, y] = ((x * 4) % 256, (y * 4) % 256, ((x ^ y) * 8) % 256, a)
|
|
png = io.BytesIO(); src.save(png, "PNG")
|
|
r = subprocess.run([magick, "png:-", "-define", "icon:auto-resize=256,48,32,16", "ico:-"], input=png.getvalue(), capture_output=True, check=True)
|
|
emit("im-autoresize", r.stdout)
|
|
for fmt in ("png", "bmp"):
|
|
b = io.BytesIO(); src.save(b, "ICO", sizes=[(16, 16), (32, 32), (64, 64)], bitmap_format=fmt); emit("pil-" + fmt, b.getvalue())
|
|
# hand-built DIB entries from Pillow BMPs
|
|
def dib(im, mask_fn):
|
|
b = io.BytesIO(); im.save(b, "BMP"); d = b.getvalue()[14:]
|
|
w, h = im.size
|
|
hs = struct.unpack("<I", d[:4])[0]
|
|
d = d[:8] + struct.pack("<i", h * 2) + d[12:]
|
|
stride = ((w + 31) // 32) * 4
|
|
mask = bytearray()
|
|
for y in range(h - 1, -1, -1):
|
|
row = bytearray(stride)
|
|
for x in range(w):
|
|
if mask_fn(x, y):
|
|
row[x // 8] |= 0x80 >> (x % 8)
|
|
mask += row
|
|
return d + bytes(mask)
|
|
def icon(entries, typ=1, hotspots=None):
|
|
hdr = struct.pack("<HHH", 0, typ, len(entries))
|
|
off = 6 + 16 * len(entries)
|
|
dirs, blobs = b"", b""
|
|
for i, (im, blob, bpp) in enumerate(entries):
|
|
w, h = im.size
|
|
f1, f2 = (1, bpp) if typ == 1 else hotspots[i]
|
|
dirs += struct.pack("<BBBBHHII", w % 256, h % 256, 0, 0, f1, f2, len(blob), off)
|
|
off += len(blob); blobs += blob
|
|
return hdr + dirs + blobs
|
|
base = src.resize((24, 20))
|
|
m = lambda x, y: (x + y) % 7 == 0
|
|
e1 = base.convert("1"); e4 = base.convert("RGB").quantize(16); e8 = base.convert("RGB").quantize(200); e24 = base.convert("RGB")
|
|
emit("hand-dibs", icon([(e1, dib(e1, m), 1), (e4, dib(e4, m), 4), (e8, dib(e8, m), 8), (e24, dib(e24, m), 24)]))
|
|
e32 = src.resize((30, 17))
|
|
b = io.BytesIO(); e32.save(b, "BMP"); d32 = b.getvalue()[14:]
|
|
# Pillow writes 32-bit BMPs as BGRA? if not, build one by hand
|
|
w, h = e32.size
|
|
bgra = b"".join(bytes((p[2], p[1], p[0], p[3])) for y in range(h - 1, -1, -1) for p in (e32.getpixel((x, y)) for x in range(w)))
|
|
stride = ((w + 31) // 32) * 4
|
|
d32 = struct.pack("<IiiHHIIiiII", 40, w, h * 2, 1, 32, 0, len(bgra), 0, 0, 0, 0) + bgra + bytes(stride * h)
|
|
emit("hand-32", icon([(e32, d32, 32)]))
|
|
emit("hand-cur", icon([(e24, dib(e24, m), 24), (e8, dib(e8, m), 8)], typ=2, hotspots=[(3, 4), (5, 6)]))
|
|
for name, data, rs in out:
|
|
sys.stdout.buffer.write(struct.pack("<HIH", len(name), len(data), len(rs)) + name.encode() + data)
|
|
for w, h, px in rs:
|
|
sys.stdout.buffer.write(struct.pack("<III", w, h, len(px)) + px)
|
|
'''
|
|
|
|
|
|
def generate(ctx):
|
|
magick = ctx.nix("imagemagick", ["sh", "-c", "command -v magick"]).decode().strip()
|
|
raw = ctx.python(["pillow"], CODE.replace("sys.argv[1]", repr(magick)))
|
|
cases, p = [], 0
|
|
while p < len(raw):
|
|
nl, dl, ne = struct.unpack("<HIH", raw[p:p + 8]); p += 8
|
|
name = raw[p:p + nl].decode(); p += nl
|
|
data = raw[p:p + dl]; p += dl
|
|
ents = []
|
|
for _ in range(ne):
|
|
w, h, pl = struct.unpack("<III", raw[p:p + 12]); p += 12
|
|
ents.append({"width": w, "height": h, "rgba": raw[p:p + pl]}); p += pl
|
|
cases.append({"name": name, "data": data, "entries": ents})
|
|
ctx.emit("ico", cases)
|