import numpy as np
import matplotlib.pyplot as plt
import imageio.v3 as iio

def norm_minmax(img, C, m):
    img = (img - img.min())/(img.max() - img.min())
    img *= C
    img -= m
    return img

def conv(i, j, img, kernel):

    h, w = kernel.shape
    a = (h-1) // 2
    b = (w-1) // 2
    neighbourhood = img[i-a:i+a+1, j-b:j+b+1]

    c_mul = kernel*neighbourhood
    return c_mul.sum()

def convolve(img, kernel):

    new_img = np.zeros_like(img)
    h, w = img.shape

    a = kernel.shape[0] // 2
    b = kernel.shape[1] // 2

    for i in range(a, h-a):
        for j in range(b, w-b):
            new_img[i, j] = conv(i, j, img, kernel)

    return new_img


def main():
    
    dummy = np.random.randint(0, 8, (10, 10))
    kernel = np.ones((3, 3))/9

    # breakpoint()


    img = iio.imread('notre_dame_small.jpeg')
    img = img[..., 0]*0.25 + img[..., 1]*0.5 + img[..., 2]*0.25
    img = img.astype(np.uint8)

    new_img = convolve(img, kernel)

    plt.figure(figsize=(12, 12))
    plt.imshow(img, cmap='gray')
    plt.show()

    plt.figure(figsize=(12, 12))
    plt.imshow(new_img, cmap='gray')
    plt.show()

    kernel = np.ones((7, 7))/49
    new_img = convolve(img, kernel)

    plt.figure(figsize=(12, 12))
    plt.imshow(new_img, cmap='gray')
    plt.show()

    kernel = np.array([
        [-1, 0, 1],
        [-2, 0, 2],
        [-1, 0, 1]])
    new_img = convolve(img, kernel)

    plt.figure(figsize=(12, 12))
    plt.imshow(new_img, cmap='gray')
    plt.show()

    kernel = np.array([
        [-1, -2, -1],
        [0, 0, 0],
        [1, 2, 1]])
    new_img = convolve(img, kernel)

    plt.figure(figsize=(12, 12))
    plt.imshow(new_img, cmap='gray')
    plt.show()

if __name__ == "__main__":
    main()
