okami.mom/strata

git clone https://git.nkp.pet/okami.mom/strata

Niko1221Merge f-own-5b adfcf9028 into f-own-int (round 16: the strata-image test program and its parts, DiT runner, sampler, noise, PNG writer; hooked into no build)e4309aaf3

main
2.9 KiB75 linesraw
1"""tools/image/gate/png_pixels.py - TEST-ONLY: the 8-bit pixels of a PNG (8-bit grey / RGB / RGBA, not interlaced),
2decoded with Python's zlib and the five PNG row filters (stb_image_write, which sd-cli uses, picks a filter per row);
3and the comparison with a raw interleaved file.
4
5    python -I -B png_pixels.py <file.png> <raw> [<raw> ...]     prints, per raw file, identical or the count of
6                                                                differing bytes; exit 1 if the first raw differs"""
7from __future__ import annotations
8
9import struct
10import sys
11import zlib
12from pathlib import Path
13
14
15def png_pixels(data: bytes):
16    assert data[:8] == b"\x89PNG\r\n\x1a\n", "not a PNG"
17    at, idat, ihdr, text = 8, [], None, {}
18    while at < len(data):
19        n, = struct.unpack(">I", data[at:at + 4])
20        typ, body = data[at + 4:at + 8], data[at + 8:at + 8 + n]
21        if typ == b"IHDR":
22            ihdr = struct.unpack(">IIBBBBB", body)
23        elif typ == b"IDAT":
24            idat.append(body)
25        elif typ == b"tEXt":
26            k, _, v = body.partition(b"\x00")
27            text[k.decode("latin-1")] = v.decode("latin-1")
28        at += 12 + n
29    w, h, depth, ctype, _, _, interlace = ihdr
30    assert depth == 8 and interlace == 0
31    bpp = {0: 1, 4: 2, 2: 3, 6: 4}[ctype]
32    raw = zlib.decompress(b"".join(idat))
33    stride = w * bpp
34    out = bytearray(h * stride)
35    prev = bytearray(stride)
36    for y in range(h):
37        f = raw[y * (stride + 1)]
38        line = bytearray(raw[y * (stride + 1) + 1:(y + 1) * (stride + 1)])
39        for x in range(stride):
40            a = line[x - bpp] if x >= bpp else 0
41            b = prev[x]
42            c = prev[x - bpp] if x >= bpp else 0
43            if f == 1:
44                line[x] = (line[x] + a) & 255
45            elif f == 2:
46                line[x] = (line[x] + b) & 255
47            elif f == 3:
48                line[x] = (line[x] + ((a + b) >> 1)) & 255
49            elif f == 4:
50                p = a + b - c
51                pa, pb, pc = abs(p - a), abs(p - b), abs(p - c)
52                line[x] = (line[x] + (a if pa <= pb and pa <= pc else b if pb <= pc else c)) & 255
53            else:
54                assert f == 0, f
55        out[y * stride:(y + 1) * stride] = line
56        prev = line
57    return (w, h, bpp), bytes(out), text
58
59
60def main(argv=None) -> int:
61    a = sys.argv[1:] if argv is None else argv
62    (w, h, c), px, _ = png_pixels(Path(a[0]).read_bytes())
63    first_bad = False
64    for i, p in enumerate(a[1:]):
65        raw = Path(p).read_bytes()
66        n = sum(1 for x, y in zip(raw, px) if x != y) if len(raw) == len(px) else -1
67        print(f"{Path(p).name} vs {Path(a[0]).name} ({w} x {h} x {c}): " +
68              ("identical pixels" if n == 0 else f"{n} bytes differ" if n > 0 else "different size"))
69        if i == 0:
70            first_bad = n != 0
71    return 1 if first_bad else 0
72
73
74if __name__ == "__main__":
75    sys.exit(main())