import numpy as np
from PIL import Image

def main():
    img = Image.open("output2g.png").convert("L")
    P = (np.array(img) < 128).astype(np.uint8) # 16384 x 2048
    
    # Blocks 64x8, each 256x256
    block_h, block_w = 256, 256
    num_by = 16384 // block_h
    num_bx = 2048 // block_w
    
    # Store first rows of each block
    first_rows = []
    for by in range(num_by):
        row_polys = []
        for bx in range(num_bx):
            block = P[by*block_h : (by+1)*block_h, bx*block_w : (bx+1)*block_w]
            poly = block[0, :]
            poly_bits = "".join(map(str, poly))
            row_polys.append(poly_bits)
        first_rows.append(row_polys)
    
    # Count unique polynomials
    all_polys = [p for sub in first_rows for p in sub]
    unique_polys = set(all_polys)
    print(f"Total blocks: {len(all_polys)}")
    print(f"Unique polynomials: {len(unique_polys)}")
    
    # Check if any blocks are all zeros or ones
    zero_poly = "0" * 256
    if zero_poly in unique_polys:
        print("Found zero blocks!")
        
    # Check if any row of blocks is a repeat
    row_strings = ["".join(row) for row in first_rows]
    unique_rows = set(row_strings)
    print(f"Unique block-rows: {len(unique_rows)} out of 64")

if __name__ == "__main__":
    main()
