dymokomi/luce-png
Source of release 0.10.1
0.10.1 / tests / drivers / gate.py
#!/usr/bin/env python3
"""luce-png's drivers, run by tests/drivers/main.luc (`luc test`, which runs the module's test
blocks itself): in native and C modes:
- every fixture decodes through the Raster API to exactly the samples of the 0.1
decoder built the same way (tests/fixtures/golden.txt);
- decode_rgba16, decode_rgba8, decode_rows and decode_rows_at (the file read in
pieces) agree with those samples, on one thread and on several, and (with
Pillow) with libpng for 8-bit files;
- the encoder is lossless in every format, gives the same bytes on one thread, on
all and streamed, at every level and filter, in one band or many; its files
decode the same here in parallel and sequentially and (with Pillow) in libpng;
- truncated and corrupted files fail cleanly, never crash or hang;
- the color chunks of tests/fixtures/color (cICP, iCCP and its profile, sRGB, gAMA, cHRM)
read as libpng 1.6.50 reads them (expected.txt, from the libpng oracle);
- the animated PNGs of tests/fixtures/apng (Ladybird's and gen_apng.py's, damaged ones
among them) give the frames, timing and failures libpng 1.6.50 with the APNG patch
gives when driven as Ladybird's PNG loader drives it, every frame composited as its
Painter (Skia) composites it, the hashes the same (expected.txt, from
luce-browser-tools/oracles/luce-png/apng), in native, C and diagnostic builds.
"""
import hashlib, os, random, struct, subprocess, tempfile
from pathlib import Path
ROOT = Path(__file__).resolve().parents[2]
BASE = os.environ.get("LUCE_BASE", "luce-base")
MODES = [["--native"], ["--backend=c"]]
env = dict(os.environ, LUCE_BASE=str(BASE))
FIXTURES = sorted((ROOT / "tests/fixtures").glob("*.png"))
GOLDEN = {}
for line in (ROOT / "tests/fixtures/golden.txt").read_text().splitlines():
if line and not line.startswith("#"):
native, c, name = line.split(" ")
GOLDEN[name] = {"--native": native, "--backend=c": c}
try:
from PIL import Image
except ImportError:
Image = None
def run(command, **options):
return subprocess.run([str(x) for x in command], env=env, cwd=ROOT, timeout=300, **options)
def fail(message):
raise SystemExit(f"FAIL {message}")
def header(path):
data = Path(path).read_bytes()
width, height, bits, color = struct.unpack(">IIBB", data[16:26])
return width, height, bits, color
def read_raw(path):
data = Path(path).read_bytes()
width, height, size = struct.unpack_from("<III", data)
return width, height, size, data[12:]
def samples16(data):
return list(struct.unpack(f"<{len(data) // 2}H", data))
def expected_rgba16(dump, bits):
"""RGBA16 from the Raster samples: 8-bit and low depths scale by 257."""
width, height, channels, data = read_raw(dump)
values = samples16(data)
scale = 1 if bits == 16 else 257
out = []
for p in range(width * height):
v = [x * scale for x in values[p * channels:(p + 1) * channels]]
if channels == 1:
out += [v[0], v[0], v[0], 65535]
elif channels == 2:
out += [v[0], v[0], v[0], v[1]]
elif channels == 3:
out += v + [65535]
else:
out += v
return out
def check_decoding(tmp, flags, dump, tool):
raw, out = tmp / "raw", tmp / "out"
compared = 0
for fixture in FIXTURES:
name = fixture.name
if run([dump, fixture, raw]).returncode != 0:
fail(f"{name}: the Raster decode failed")
if hashlib.sha256(raw.read_bytes()).hexdigest() != GOLDEN[name][flags[0]]:
fail(f"{name}: Raster samples differ from the 0.1 decoder")
width, height, bits, color = header(fixture)
deep = expected_rgba16(raw, bits)
shallow = bytes((v * 255 + 32767) // 65535 for v in deep)
for mode, threads in [("rgba16", 1), ("rgba16", 0), ("rows16", 0), ("rgba8", 1), ("rgba8", 0), ("rows8", 3), ("at8", 0), ("at16", 2)]:
if run([tool, "decode", fixture, out, mode, threads]).returncode != 0:
fail(f"{name}: {mode} decode on {threads} threads failed")
pixels = read_raw(out)[3]
if (samples16(pixels) if "16" in mode else pixels) != (deep if "16" in mode else shallow):
fail(f"{name}: {mode} on {threads} threads differs from the Raster samples")
if Image and bits == 8:
with Image.open(fixture) as image:
reference = image.convert("RGBA").tobytes()
if reference != shallow:
fail(f"{name}: RGBA8 differs from libpng")
compared += 1
# An interlaced picture's first sight is Adam7's first pass: every eighth
# pixel each way of the picture as it decodes whole.
early = 0
for fixture in FIXTURES:
width, height, bits, color = header(fixture)
coarse = tmp / "coarse"
coarse.unlink(missing_ok=True)
if run([tool, "decode", fixture, coarse, "coarse", 0]).returncode != 0:
fail(f"{fixture.name}: the coarse decode failed")
if coarse.stat().st_size == 0:
continue
run([tool, "decode", fixture, out, "rgba8", 0], check=True)
full = read_raw(out)[3]
w, h, _, pixels = read_raw(coarse)
expected = b"".join(full[(y * 8 * width + x * 8) * 4:(y * 8 * width + x * 8) * 4 + 4] for y in range(h) for x in range(w))
if (w, h) != ((width + 7) // 8, (height + 7) // 8) or pixels != expected:
fail(f"{fixture.name}: the first pass is not every eighth pixel")
early += 1
if early < 5:
fail(f"only {early} interlaced fixtures gave a first sight")
print(f"ok {len(FIXTURES)} fixtures identical to the 0.1 decoder through every API"
+ (f"; {compared} identical to libpng" if Image else "; Pillow absent, libpng comparisons skipped"))
def check_encoding(tmp, flags, tool):
out = tmp / "out"
sources = [ROOT / "tests/fixtures" / name for name in ["pil_rgba_400x300.png", "pil_gray_400x300.png", "c6_b16_n_33x17.png", "c4_b8_i_33x17.png", "c0_b1_n_1x1.png"]]
formats = ["gray8", "ga8", "rgb8", "rgba8", "gray16", "ga16", "rgb16", "rgba16"]
checked = 0
for source in sources:
if run([tool, "decode", source, out, "rgba16", 1]).returncode != 0:
fail(f"{source.name}: does not decode")
original = samples16(read_raw(out)[3])
for index, kind in enumerate(formats):
channels = index % 4 + 1
deep = index >= 4
# What the format keeps of the source.
expected = []
for p in range(len(original) // 4):
r, g, b, a = original[p * 4:p * 4 + 4]
if not deep:
r, g, b, a = [(v >> 8) * 257 for v in (r, g, b, a)]
if channels <= 2:
g = b = r
if channels in (1, 3):
a = 65535
expected += [r, g, b, a]
for level, filter_kind, band_rows in [(6, -1, 0), (1, 4, 37), (0, 0, 5), (9, 2, 64)]:
outputs = []
for how, threads in [("whole", 0), ("whole", 1), ("stream", 0)]:
encoded = tmp / f"{how}{threads}.png"
if run([tool, "encode", source, encoded, level, filter_kind, kind, how, threads, band_rows], capture_output=True).returncode != 0:
fail(f"{source.name}: {kind} level {level} encode failed")
outputs.append(encoded.read_bytes())
if outputs[1] != outputs[0] or outputs[2] != outputs[0]:
fail(f"{source.name}: {kind} level {level} differs between threads or streaming")
encoded = tmp / "whole0.png"
for threads in [0, 1]:
if run([tool, "decode", encoded, out, "rgba16", threads]).returncode != 0:
fail(f"{source.name}: {kind} level {level} output does not decode")
if samples16(read_raw(out)[3]) != expected:
fail(f"{source.name}: {kind} level {level} is not lossless (threads {threads})")
if Image and not deep:
run([tool, "decode", encoded, out, "rgba8", 0], check=True)
with Image.open(encoded) as image:
if image.convert("RGBA").tobytes() != read_raw(out)[3]:
fail(f"{source.name}: {kind} level {level} decodes differently in libpng")
checked += 1
print(f"ok {checked} encodings lossless and identical across threads, streaming and decoders")
def check_damage(tmp, dump, tool):
rng = random.Random(7)
bad = tmp / "bad.png"
encoded = tmp / "banded.png"
run([tool, "encode", ROOT / "tests/fixtures/pil_rgba_400x300.png", encoded, 6, -1, "rgba8", "whole", 0, 29], check=True)
damaged = 0
for path in [ROOT / "tests/fixtures/c6_b8_i_33x17.png", ROOT / "tests/fixtures/c3_b4_n_33x17.png", ROOT / "tests/fixtures/pil_rgba_400x300.png", encoded]:
data = path.read_bytes()
cases = [data[:n] for n in range(8, len(data), max(1, len(data) // 50))]
for _ in range(40):
changed = bytearray(data)
for _ in range(rng.randrange(1, 4)):
changed[rng.randrange(8, len(changed))] = rng.randrange(256)
cases.append(bytes(changed))
for case in cases:
bad.write_bytes(case)
for command in [[dump, bad, tmp / "raw"], [tool, "decode", bad, tmp / "out", "rgba8", 0], [tool, "decode", bad, tmp / "out", "rows8", 0], [tool, "decode", bad, tmp / "out", "at8", 0]]:
result = run(command, capture_output=True)
if result.returncode not in (0, 1):
fail(f"{path.name}: a damaged file ended the process with {result.returncode}")
damaged += 1
print(f"ok {damaged} decodes of damaged files ended cleanly")
def check_color(tmp, flags):
tool = tmp / "color_tool"
run([BASE, "build", ROOT / "tests/color_tool.lucb", *flags, "-o", tool], check=True)
folder = ROOT / "tests/fixtures/color"
names = sorted(p.name for p in folder.glob("*.png"))
found = subprocess.run([str(tool), *names], cwd=folder, env=env, capture_output=True, timeout=300, check=True).stdout.decode()
if found != (folder / "expected.txt").read_text():
fail("the color chunks differ from libpng's (tests/fixtures/color/expected.txt)")
print(f"ok {len(names)} PNGs' color chunks as libpng reads them")
def check_apng(tmp, flags):
tool = tmp / "apng_check"
run([BASE, "build", ROOT / "tests/apng_check.lucb", *flags, "-o", tool], check=True)
folder = ROOT / "tests/fixtures/apng"
names = sorted(p.name for p in folder.glob("*.png"))
found = subprocess.run([str(tool), *names], cwd=folder, env=env, capture_output=True, timeout=300, check=True).stdout.decode()
expected = "".join(line + "\n" for line in (folder / "expected.txt").read_text().splitlines() if not line.startswith("#"))
if found != expected:
fail(f"the animated PNGs differ from libpng's (tests/fixtures/apng/expected.txt), {' '.join(flags)}")
frames = sum(1 for line in found.splitlines() if line.startswith("frame"))
print(f"ok {len(names)} PNGs' {frames} frames as libpng with the APNG patch reads them ({' '.join(flags)})")
for flags in MODES + [["--profile", "diagnostic"]]:
if flags[0] == "--profile":
with tempfile.TemporaryDirectory(prefix="luce-png-") as name:
check_apng(Path(name), flags)
continue
with tempfile.TemporaryDirectory(prefix="luce-png-") as name:
tmp = Path(name)
dump, tool = tmp / "dump", tmp / "tool"
run([BASE, "build", ROOT / "tests/dump.lucb", *flags, "-o", dump], check=True)
run([BASE, "build", ROOT / "tests/tool.lucb", *flags, "-o", tool], check=True)
check_decoding(tmp, flags, dump, tool)
check_encoding(tmp, flags, tool)
check_damage(tmp, dump, tool)
check_color(tmp, flags)
check_apng(tmp, flags)
print("PASS luce-png")