70 lines
3.6 KiB
Python
70 lines
3.6 KiB
Python
# BMP fixtures (tools/fixtures.py): image/bmp. Ported from bmp-luau tools/fixtures.sh + mkfixtures.py.
|
|
# tests/gen/bmp_suite.luau the BMP Suite 2.8 (tests/fixtures/bmpsuite: g/ must match, q/ and b/ must not throw;
|
|
# public domain, see licenses/) with Pillow's reference RGBA (tools/fixtures/_bmp_reference.py)
|
|
# tests/gen/bmp_enc.luau encoder references: images saved by Pillow as 24 bpp RGB, 8 bpp P (palette in first-appearance
|
|
# order) and 32 bpp RGBA (BI_RGB); our encoder must write the same bytes
|
|
# (bmp-luau also tested the Winamp skins of the Webamp demo; those are not redistributable and are not used here.)
|
|
import json, os, random
|
|
|
|
INPUTS = ["tests/fixtures/bmpsuite", "tools/fixtures/_bmp_reference.py"]
|
|
|
|
|
|
def image(rng, w, h, ncolours, alpha):
|
|
pal = [(rng.randrange(256), rng.randrange(256), rng.randrange(256)) for _ in range(ncolours)]
|
|
px = bytearray()
|
|
for y in range(h):
|
|
for x in range(w):
|
|
c = pal[(x * 7 + y * 13 + rng.randrange(3)) % ncolours] if ncolours else (rng.randrange(256), rng.randrange(256), rng.randrange(256))
|
|
px += bytes(c) + bytes([rng.randrange(256) if alpha else 255])
|
|
return bytes(px)
|
|
|
|
|
|
def first_appearance(rgba):
|
|
seen, pal = set(), []
|
|
for i in range(0, len(rgba), 4):
|
|
c = tuple(rgba[i:i + 3])
|
|
if c not in seen:
|
|
seen.add(c)
|
|
pal.append(list(c))
|
|
return pal
|
|
|
|
|
|
def generate(ctx):
|
|
root = os.path.join(ctx.root, "tests", "fixtures", "bmpsuite")
|
|
refs = {}
|
|
for line in open(os.path.join(root, "ref.txt")).read().split("\n"):
|
|
if line:
|
|
a, b = line.split()
|
|
refs[a] = b
|
|
files = [d + "/" + f for d in ("g", "q", "b") for f in sorted(os.listdir(os.path.join(root, d)))]
|
|
rng = random.Random(11)
|
|
enc = []
|
|
for name, w, h, ncol, alpha, mode in (("rgb-37x23", 37, 23, 0, False, "RGB"), ("rgb-1x1", 1, 1, 0, False, "RGB"),
|
|
("rgb-640x3", 640, 3, 0, False, "RGB"), ("pal-5-13x9", 13, 9, 5, False, "P"),
|
|
("pal-256-64x64", 64, 64, 256, False, "P"), ("pal-1-3x3", 3, 3, 1, False, "P"),
|
|
("pal-17-50x7", 50, 7, 17, False, "P"), ("rgba-21x11", 21, 11, 0, True, "RGBA"),
|
|
("rgba-pal-9x9", 9, 9, 4, True, "RGBA")):
|
|
rgba = image(rng, w, h, ncol, alpha)
|
|
if mode == "P":
|
|
pal = first_appearance(rgba)
|
|
assert len(pal) <= 256
|
|
enc.append({"name": name, "mode": mode, "w": w, "h": h, "rgba": rgba.hex(), **({"palette": pal} if mode == "P" else {})})
|
|
req = {"root": root, "files": files, "refs": refs, "encode": enc}
|
|
code = open(os.path.join(ctx.root, "tools", "fixtures", "_bmp_reference.py")).read()
|
|
res = json.loads(ctx.python(["pillow"], code, input=json.dumps(req).encode()))
|
|
suite = []
|
|
for f in res["files"]:
|
|
mode = "match" if f["name"].startswith("g/") else "nocrash"
|
|
if mode == "match" and "error" in f:
|
|
raise RuntimeError("pillow cannot read %s: %s" % (f["name"], f["error"]))
|
|
e = {"name": f["name"], "mode": mode, "data": ctx.read(os.path.join("tests", "fixtures", "bmpsuite", f["name"]))}
|
|
if "w" in f:
|
|
e.update(w=f["w"], h=f["h"], crc=f["crc"])
|
|
suite.append(e)
|
|
ctx.emit("bmp_suite", suite)
|
|
out = []
|
|
for e, r in zip(enc, res["encode"]):
|
|
out.append({"name": e["name"], "mode": e["mode"], "w": e["w"], "h": e["h"], "rgba": bytes.fromhex(e["rgba"]),
|
|
"bmp": bytes.fromhex(r["bmp"])})
|
|
ctx.emit("bmp_enc", out)
|