Files

94 lines
5.0 KiB
Python

# Netpbm fixtures (tools/fixtures.py): files written by ImageMagick (P1-P7, ASCII / binary, 8 / 16-bit, PAM tuple
# types, PFM both byte orders) and a few hand-written edge cases (comments, packed P1 digits, odd maxvals, PAM
# GRAYSCALE_ALPHA / BLACKANDWHITE); references = ImageMagick's RGBA decodes (8-bit, and 16-bit for 16-bit files).
# tests/gen/pnm.luau { { name, data, width, height, rgba8 (bytes), rgba16? (bytes, big-endian), tol, floats? } }
import os, random, shutil, struct, subprocess, tempfile
def generate(ctx):
magick = ctx.nix("imagemagick", ["sh", "-c", "command -v magick"]).decode().strip()
work = tempfile.mkdtemp(prefix="formats-pnm-")
try:
_generate(ctx, magick, work)
finally:
shutil.rmtree(work, ignore_errors=True)
def _generate(ctx, magick, work):
rng = random.Random(3)
W = lambda n: os.path.join(work, n)
def run(*args, inp=None):
r = subprocess.run([magick] + list(args), input=inp, capture_output=True)
if r.returncode != 0:
raise SystemExit("fixtures/pnm: magick %s failed: %s" % (" ".join(args), r.stderr.decode()))
return r.stdout
# a 16-bit RGBA source with gradients, noise and transparency, written as a 16-bit PAM for ImageMagick
w, h = 37, 23
src = bytearray()
for y in range(h):
for x in range(w):
r = (x * 65535) // (w - 1)
g = (y * 65535) // (h - 1)
b = rng.randrange(65536)
a = 65535 if (x + y) % 5 else (x * 3000) % 65536
src += struct.pack(">HHHH", r, g, b, a)
open(W("src.pam"), "wb").write(b"P7\nWIDTH %d\nHEIGHT %d\nDEPTH 4\nMAXVAL 65535\nTUPLTYPE RGB_ALPHA\nENDHDR\n" % (w, h) + bytes(src))
cases = []
def ref(path, wide, tol=0, floats=None, name=None):
data = open(path, "rb").read()
c = {"name": name or os.path.basename(path), "data": data, "tol": tol}
info = run(path, "-format", "%w %h", "info:").decode().split()
c["width"], c["height"] = int(info[0]), int(info[1])
c["rgba8"] = run(path, "-depth", "8", "rgba:-")
if wide:
c["rgba16"] = run(path, "-depth", "16", "-endian", "MSB", "rgba:-")
if floats is not None:
c["floats"] = floats
cases.append(c)
variants = [
("p1.pbm", ["-colorspace", "gray", "-threshold", "50%", "-compress", "none"], "pbm:", False),
("p4.pbm", ["-colorspace", "gray", "-threshold", "50%"], "pbm:", False),
("p2.pgm", ["-colorspace", "gray", "-depth", "8", "-compress", "none"], "pgm:", False),
("p5.pgm", ["-colorspace", "gray", "-depth", "8"], "pgm:", False),
("p5-16.pgm", ["-colorspace", "gray", "-depth", "16"], "pgm:", True),
("p2-16.pgm", ["-colorspace", "gray", "-depth", "16", "-compress", "none"], "pgm:", True),
("p3.ppm", ["-alpha", "off", "-depth", "8", "-compress", "none"], "ppm:", False),
("p6.ppm", ["-alpha", "off", "-depth", "8"], "ppm:", False),
("p6-16.ppm", ["-alpha", "off", "-depth", "16"], "ppm:", True),
("p3-16.ppm", ["-alpha", "off", "-depth", "16", "-compress", "none"], "ppm:", True),
("rgba.pam", ["-depth", "8"], "pam:", False),
("rgba16.pam", ["-depth", "16"], "pam:", True),
("rgb.pam", ["-alpha", "off", "-depth", "8"], "pam:", False),
("gray.pam", ["-alpha", "off", "-colorspace", "gray", "-depth", "8"], "pam:", False),
("graya.pam", ["-colorspace", "gray", "-depth", "8"], "pam:", False),
("graya16.pam", ["-colorspace", "gray", "-depth", "16"], "pam:", True),
("bw.pam", ["-alpha", "off", "-colorspace", "gray", "-threshold", "50%", "-type", "bilevel"], "pam:", False),
]
for name, args, prefix, wide in variants:
run(W("src.pam"), *args, prefix + W(name))
ref(W(name), wide)
for endian in ("LSB", "MSB"):
for col in ("rgb", "gray"):
name = "%s-%s.pfm" % (col, endian)
extra = ["-colorspace", "gray"] if col == "gray" else []
run(W("src.pam"), "-alpha", "off", *extra, "-endian", endian, "pfm:" + W(name))
fl = run(W(name), "-depth", "32", "-define", "quantum:format=floating-point", "-endian", "LSB", "rgba:-")
ref(W(name), False, tol=1, floats=fl)
# hand-written edge cases (references from ImageMagick too)
def hand(name, data, wide=False, tol=0):
open(W(name), "wb").write(data)
ref(W(name), wide, tol=tol)
hand("comments.ppm", b"P6\n# a comment\n3 # inline\n2\n#another\n255\n" + bytes(range(18)))
hand("packed.pbm", b"P1\n5 3\n10101\n01010\n1 1 1 0 0\n")
hand("maxval1000.pgm", b"P2\n4 2\n1000\n0 250 500 1000\n999 1 333 750\n", wide=True, tol=1)
hand("maxval7.ppm", b"P3 2 2 7 0 1 2 3 4 5 6 7 7 7 0 0\n", tol=1)
hand("p4-odd.pbm", b"P4\n11 3\n" + bytes([0b10101010, 0b11100000, 0b00001111, 0b00100000, 0b11111111, 0b11000000]))
hand("graya-hand.pam", b"P7\nWIDTH 2\nHEIGHT 2\nDEPTH 2\nMAXVAL 255\nTUPLTYPE GRAYSCALE_ALPHA\nENDHDR\n" + bytes([0, 255, 100, 128, 200, 0, 255, 255]))
ctx.emit("pnm", cases)