import numpy as np
from PIL import Image

def analyze():
    img = Image.open("output2g.png").convert('L')
    data = np.array(img)
    rows, cols = data.shape
    matrix = (data < 128).astype(np.int8)
    
    print(f"Matrix shape: {rows}x{cols}")
    
    # Check for cyclic shifts
    for b in range(1, 1024):
        if rows % b == 0 and cols % b == 0:
            # Check if matrix[i, j] == matrix[(i+1)%b + (i//b)*b, (j+1)%b + (j//b)*b]
            # Actually simpler: check block-wise
            is_qc = True
            # Test a few random blocks
            for block_r in range(min(5, rows // b)):
                for block_c in range(min(5, cols // b)):
                    r0, c0 = block_r * b, block_c * b
                    block = matrix[r0:r0+b, c0:c0+b]
                    shifted = np.roll(np.roll(block, 1, axis=0), 1, axis=1)
                    if not np.array_equal(block, shifted):
                        is_qc = False
                        break
                if not is_qc: break
            if is_qc:
                print(f"Found potential lifting factor B: {b}")
                return b
    print("No simple QC structure found.")
    return None

analyze()
