okami.mom/strata
git clone https://git.nkp.pet/okami.mom/strata
e4309aaf3
main
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 5python -I -B png_pixels.py <file.png> <raw> [<raw> ...] prints, per raw file, identical or the count of 6differing 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 ): 16assert data [:8 ]== b"\x89PNG\r\n\x1a\n" ,"not a PNG" 17at ,idat ,ihdr ,text = 8 , [],None , {} 18while at < len (data ): 19n ,= struct .unpack (">I" ,data [at :at + 4 ]) 20typ ,body = data [at + 4 :at + 8 ],data [at + 8 :at + 8 + n ] 21if typ == b"IHDR" : 22ihdr = struct .unpack (">IIBBBBB" ,body ) 23elif typ == b"IDAT" : 24idat .append (body ) 25elif typ == b"tEXt" : 26k ,_ ,v = body .partition (b"\x00" ) 27text [k .decode ("latin-1" )]= v .decode ("latin-1" ) 28at += 12 + n 29w ,h ,depth ,ctype ,_ ,_ ,interlace = ihdr 30assert depth == 8 and interlace == 0 31bpp = {0 :1 ,4 :2 ,2 :3 ,6 :4 }[ctype ] 32raw = zlib .decompress (b"" .join (idat )) 33stride = w * bpp 34out = bytearray (h * stride ) 35prev = bytearray (stride ) 36for y in range (h ): 37f = raw [y * (stride + 1 )] 38line = bytearray (raw [y * (stride + 1 )+ 1 :(y + 1 )* (stride + 1 )]) 39for x in range (stride ): 40a = line [x - bpp ]if x >= bpp else 0 41b = prev [x ] 42c = prev [x - bpp ]if x >= bpp else 0 43if f == 1 : 44line [x ]= (line [x ]+ a )& 255 45elif f == 2 : 46line [x ]= (line [x ]+ b )& 255 47elif f == 3 : 48line [x ]= (line [x ]+ ((a + b )>> 1 ))& 255 49elif f == 4 : 50p = a + b - c 51pa ,pb ,pc = abs (p - a ),abs (p - b ),abs (p - c ) 52line [x ]= (line [x ]+ (a if pa <= pb and pa <= pc else b if pb <= pc else c ))& 255 53else : 54assert f == 0 ,f 55out [y * stride :(y + 1 )* stride ]= line 56prev = line 57return (w ,h ,bpp ),bytes (out ),text 58 59 60def main (argv = None )-> int : 61a = sys .argv [1 :]if argv is None else argv 62 (w ,h ,c ),px ,_ = png_pixels (Path (a [0 ]).read_bytes ()) 63first_bad = False 64for i ,p in enumerate (a [1 :]): 65raw = Path (p ).read_bytes () 66n = sum (1 for x ,y in zip (raw ,px )if x != y )if len (raw )== len (px )else - 1 67f" { 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" )) 69if i == 0 : 70first_bad = n != 0 71return 1 if first_bad else 0 72 73 74if __name__ == "__main__" : 75sys .exit (main ())