import cv2 as cv
import numpy as np

def crop(image: cv.Mat, lower_rgb: np.ndarray, upper_rgb: np.ndarray) -> cv.Mat:	
	width, height = image.shape[:2]

	x1 = None
	for x in range(width):
		for y in range(height):
			b, g, r = image[x][y]
			if(not (r >= lower_rgb[0] and r <= upper_rgb[0] and
	  		   g >= lower_rgb[1] and g <= upper_rgb[1] and
			   b >= lower_rgb[2] and b <= upper_rgb[2])
			):
				x1 = x
				break
		if x1 is not None:
			break

	y1 = None
	for y in range(height):
		for x in range(width):
			b, g, r = image[x][y]
			if(not (r >= lower_rgb[0] and r <= upper_rgb[0] and
	  		   g >= lower_rgb[1] and g <= upper_rgb[1] and
			   b >= lower_rgb[2] and b <= upper_rgb[2])
			):
				y1 = y
				break
		if y1 is not None:
			break
	
	x2 = None
	for x in range(width-1, 0, -1):
		for y in range(height):
			b, g, r = image[x][y]
			if(not (r >= lower_rgb[0] and r <= upper_rgb[0] and
	  		   g >= lower_rgb[1] and g <= upper_rgb[1] and
			   b >= lower_rgb[2] and b <= upper_rgb[2])
			):
				x2 = x
				break
		if x2 is not None:
			break

	y2 = None
	for y in range(height-1, 0, -1):
		for x in range(width):
			b, g, r = image[x][y]
			if(not (r >= lower_rgb[0] and r >= upper_rgb[0] and
	  		   g >= lower_rgb[1] and g >= upper_rgb[1] and
			   b >= lower_rgb[2] and b >= upper_rgb[2])
			):
				y2 = y
				break
		if y2 is not None:
			break
	
	return image[x1-2:x2+2, y1-2:y2+2]


def remove_background(image: cv.Mat, lower_rgb: np.ndarray, upper_rgb: np.ndarray, new_background: np.ndarray) -> cv.Mat:
	width, height = image.shape[:2]

	# left to right
	for x in range(width):
		for y in range(height):
			b, g, r = image[x][y]
			if(r >= lower_rgb[0] and r <= upper_rgb[0] and
	  		   g >= lower_rgb[1] and g <= upper_rgb[1] and
			   b >= lower_rgb[2] and b <= upper_rgb[2]
			):
				image[x][y] = new_background
			else:
				break
	
	# # top to bottom
	# for y in range(height):
	# 	for x in range(width):
	# 		b, g, r = image[x][y]
	# 		if(r >= lower_rgb[0] and r <= upper_rgb[0] and
	#  		   g >= lower_rgb[1] and g <= upper_rgb[1] and
	#		   b >= lower_rgb[2] and b <= upper_rgb[2]
	#		):
	# 			image[x][y] = new_background
	# 		else:
	# 			break

	# right to left
	for x in range(width):
		for y in range(height-1, 0, -1):
			b, g, r = image[x][y]
			if(r >= lower_rgb[0] and r <= upper_rgb[0] and
	  		   g >= lower_rgb[1] and g <= upper_rgb[1] and
			   b >= lower_rgb[2] and b <= upper_rgb[2]
			):
				image[x][y] = new_background
			else:
				break
	
	# bottom to top
	# for y in range(height):
	# 	for x in range(width-1, 0, -1):
	# 		b, g, r = image[x][y]
	# 		if(r >= lower_rgb[0] and r <= upper_rgb[0] and
	#  		   g >= lower_rgb[1] and g <= upper_rgb[1] and
	#		   b >= lower_rgb[2] and b <= upper_rgb[2]
	#		):
	# 			image[x][y] = new_background
	# 		else:
	# 			break

	return image


def raster_image(image: cv.Mat, num_cols: int = 4, num_rows: int = 4) -> list[list[cv.Mat]]:
	width, height = image.shape[:2]
	rasters = [[0 for x in range(num_rows)] for y in range(num_cols)]
	for y in range(num_cols):
		for x in range(num_rows):
			x1 = int(x * (width/num_rows))
			y1 = int(y * (height/num_cols))
			x2 = int((x+1) * (width/num_rows))
			y2 = int((y+1) * (height/num_cols))

			rasters[x][y] = image[x1:x2, y1:y2]

	return rasters