import cv2
import os
import numpy as np

def load_normal(path, binocular=False, left_only=False):
    if binocular and not left_only:
        normal_path_L = os.path.join(
            os.path.dirname(path), "normal", os.path.basename(path) + "_L.png"
        )
        normal_image_L = cv2.imread(normal_path_L, cv2.IMREAD_UNCHANGED)
        normal_path_R = os.path.join(
            os.path.dirname(path), "normal", os.path.basename(path) + "_R.png"
        )
        normal_image_R = cv2.imread(normal_path_R, cv2.IMREAD_UNCHANGED)
        normalized_normal_image_L = normal_image_L / 255.0 * 2.0 - 1.0
        normalized_normal_image_R = normal_image_R / 255.0 * 2.0 - 1.0
        return normalized_normal_image_L, normalized_normal_image_R
    else:
        if binocular and left_only:
            normal_path = os.path.join(
                os.path.dirname(path), "normal", os.path.basename(path) + "_L.png"
            )
        else:
            normal_path = os.path.join(
                os.path.dirname(path), "normal", os.path.basename(path) + ".png"
            )
        normal_image = cv2.imread(normal_path, cv2.IMREAD_UNCHANGED)
        normalized_normal_image = normal_image / 255.0 * 2.0 - 1.0
        return normalized_normal_image

def show_rgb(event, x, y, flags, param):
    if event == cv2.EVENT_MOUSEMOVE:
        pixel_value = param[y, x]
        print(f"RGB at ({x},{y}): {pixel_value}")

if __name__ == "__main__":
    path = "/Users/hofee/temp/1"
    normal_image = load_normal(path, binocular=True, left_only=True)
    display_image = ((normal_image + 1.0) / 2.0 * 255).astype(np.uint8)

    cv2.namedWindow("Normal Image")
    cv2.setMouseCallback("Normal Image", show_rgb, param=display_image)

    while True:
        cv2.imshow("Normal Image", display_image)
        if cv2.waitKey(1) & 0xFF == ord('q'):
            break

    cv2.destroyAllWindows()
