imgA-simple: image/qoi (full spec read/write; byte-identical to Pillow's encoder) with Pillow fixtures

This commit is contained in:
4DBug committed 2026-10-31 08:42:06 -05:00
1 parent 2eee0bc1a6
commit 985bb5364d
6 files changed
+428

No files matched your search

+64
View File
@@ -0,0 +1,64 @@
# QOI fixtures (tools/fixtures.py): Pillow's QOI writer and reader.
# tests/gen/qoi.luau { { name, width, height, mode, pixels (source bytes, RGB or RGBA), data (Pillow's .qoi), crc
# (CRC-32 of Pillow's RGBA decode) } }
# tests/gen/qoi_bench.luau { width, height, pixels (RGBA), data }
import struct
CODE = r'''
import io, random, struct, sys, zlib
from PIL import Image
rng = random.Random(11)
def make(w, h, mode, kind):
n = len(mode)
px = bytearray()
for y in range(h):
for x in range(w):
if kind == "noise":
v = [rng.randrange(256) for _ in range(n)]
elif kind == "grad":
v = [(x * 3 + y) % 256, (y * 5) % 256, (x + y * 2) % 256, 255 - (x % 7) * 30][:n]
elif kind == "flat":
v = [10, 200, 30, 255][:n] if (x // 9 + y // 7) % 2 else [10, 200, 31, 128][:n]
else: # small deltas around a walk, with an occasional jump
base = (x * 2 + y) % 256
v = [(base + rng.randrange(-2, 2)) % 256, (base + rng.randrange(-30, 30)) % 256, (base + rng.randrange(-9, 9)) % 256, 255 if rng.random() < 0.9 else rng.randrange(256)][:n]
px += bytes(v)
return bytes(px)
out = []
for (w, h, mode, kind) in ((1, 1, "RGB", "noise"), (1, 1, "RGBA", "noise"), (3, 2, "RGBA", "grad"), (64, 48, "RGB", "grad"),
(64, 48, "RGBA", "grad"), (100, 37, "RGB", "noise"), (100, 37, "RGBA", "noise"),
(200, 150, "RGBA", "flat"), (200, 150, "RGB", "flat"), (128, 99, "RGBA", "walk"), (128, 99, "RGB", "walk"),
(500, 1, "RGBA", "flat"), (1, 300, "RGB", "walk")):
px = make(w, h, mode, kind)
im = Image.frombytes(mode, (w, h), px)
b = io.BytesIO(); im.save(b, "QOI"); data = b.getvalue()
back = Image.open(io.BytesIO(data)).convert("RGBA").tobytes()
name = "%dx%d-%s-%s" % (w, h, mode, kind)
sys.stdout.buffer.write(struct.pack("<HIIB", len(name), w, h, len(mode)) + name.encode() + struct.pack("<III", len(px), len(data), zlib.crc32(back)) + px + data)
# bench: 1024 x 768 RGBA walk image
w, h = 1024, 768
px = bytearray()
for y in range(h):
for x in range(w):
base = (x // 4 + y // 3) % 256
px += bytes(((base + (x * y) % 5) % 256, (base * 2) % 256, (255 - base + x % 3) % 256, 255))
b = io.BytesIO(); Image.frombytes("RGBA", (w, h), bytes(px)).save(b, "QOI")
sys.stdout.buffer.write(struct.pack("<HIIB", 0, w, h, 4) + struct.pack("<III", len(px), len(b.getvalue()), 0) + bytes(px) + b.getvalue())
'''
def generate(ctx):
raw = ctx.python(["pillow"], CODE)
cases, p, bench = [], 0, None
while p < len(raw):
nl, w, h, ch = struct.unpack("<HIIB", raw[p:p + 11]); p += 11
name = raw[p:p + nl].decode(); p += nl
pl, dl, crc = struct.unpack("<III", raw[p:p + 12]); p += 12
px, data = raw[p:p + pl], raw[p + pl:p + pl + dl]; p += pl + dl
e = {"name": name, "width": w, "height": h, "channels": ch, "pixels": px, "data": data, "crc": crc}
if nl == 0:
bench = e
else:
cases.append(e)
ctx.emit("qoi", cases)
ctx.emit("qoi_bench", bench)