# Shared by the simple raster fixture generators (pcx, sgi, sunras, xbm, xpm, ilbm, icns, ani): source images, a PNG # writer, netpbm / ImageMagick runs in one temp dir, and a PNM / PAM reader for their output. # work = Work(ctx) a temp dir; work.png(name, w, h, rgba); work.sh("netpbm", ["cmd", ...]); work.read(n) # pnm_rgba(data) -> (w, h, rgba) for P1..P7 (PBM 1 = black; PAM GRAYSCALE[_ALPHA] / RGB[_ALPHA]) import os, random, shutil, struct, subprocess, tempfile, zlib def png(w, h, rgba): raw = b"".join(b"\0" + rgba[y * w * 4:(y + 1) * w * 4] for y in range(h)) def chunk(t, d): return struct.pack(">I", len(d)) + t + d + struct.pack(">I", zlib.crc32(t + d)) return (b"\x89PNG\r\n\x1a\n" + chunk(b"IHDR", struct.pack(">IIBBBBB", w, h, 8, 6, 0, 0, 0)) + chunk(b"IDAT", zlib.compress(raw)) + chunk(b"IEND", b"")) def photo(w, h, seed=1, alpha=False): rng = random.Random(seed) out = bytearray() for y in range(h): for x in range(w): r = (x * 255) // max(1, w - 1) g = (y * 255) // max(1, h - 1) b = ((x * 7 + y * 3) * 4 + rng.randrange(8)) % 256 a = 255 if not alpha else ((x * 37 + y * 11) % 256) out += bytes((r, g, b, a)) return bytes(out) def palette_image(w, h, ncolours, seed=2): rng = random.Random(seed) cols = [(rng.randrange(256), rng.randrange(256), rng.randrange(256)) for _ in range(ncolours)] out = bytearray() for y in range(h): for x in range(w): out += bytes(cols[(x // 3 + y // 2 + (x * y) % 3) % ncolours]) + b"\xff" return bytes(out) class Work: def __init__(self, ctx): self.ctx = ctx self.dir = tempfile.mkdtemp(prefix="formats-raster-") def close(self): shutil.rmtree(self.dir, ignore_errors=True) def path(self, n): return os.path.join(self.dir, n) def write(self, n, data): with open(self.path(n), "wb") as f: f.write(data) def read(self, n): with open(self.path(n), "rb") as f: return f.read() def png(self, n, w, h, rgba): self.write(n, png(w, h, rgba)) def sh(self, pkg, cmds): script = " && ".join(cmds) res = subprocess.run(["nix", "shell", "nixpkgs#" + pkg, "-c", "sh", "-c", "cd %s && %s" % (self.dir, script)], capture_output=True) if res.returncode: raise RuntimeError("%s failed: %s" % (pkg, res.stderr.decode()[-1500:])) return res.stdout def _tokens(data, n, pos): out = [] while len(out) < n: while data[pos:pos + 1].isspace(): pos += 1 if data[pos:pos + 1] == b"#": while data[pos:pos + 1] not in (b"\n", b""): pos += 1 continue s = pos while not data[pos:pos + 1].isspace(): pos += 1 out.append(int(data[s:pos])) return out, pos + 1 def pnm_rgba(data): kind = data[:2] if kind == b"P7": end = data.index(b"ENDHDR\n") + 7 hdr = dict(l.split(None, 1) for l in data[3:end - 7].decode().strip().split("\n") if l and not l.startswith("#")) w, h, d, mx = int(hdr["WIDTH"]), int(hdr["HEIGHT"]), int(hdr["DEPTH"]), int(hdr["MAXVAL"]) raw, pos = data[end:], 0 elif kind in (b"P4",): (w, h), pos = _tokens(data, 2, 2) bits = data[pos:] stride = (w + 7) // 8 out = bytearray() for y in range(h): for x in range(w): v = 0 if (bits[y * stride + x // 8] >> (7 - x % 8)) & 1 else 255 out += bytes((v, v, v, 255)) return w, h, bytes(out) else: (w, h, mx), pos = _tokens(data, 3, 2) d = 1 if kind == b"P5" else 3 raw = data[pos:] bps = 2 if mx > 255 else 1 out = bytearray() for i in range(w * h): vals = [] for k in range(d): o = (i * d + k) * bps v = raw[o] if bps == 1 else (raw[o] << 8 | raw[o + 1]) vals.append((v * 255 + mx // 2) // mx) if d == 1: out += bytes((vals[0],) * 3 + (255,)) elif d == 2: out += bytes((vals[0],) * 3 + (vals[1],)) elif d == 3: out += bytes(vals) + b"\xff" else: out += bytes(vals) return w, h, bytes(out)