"""
*************************************************************************
// Example program using OpenCV library
//      python >3.7 - OpenCV 4.5
// @file	e3b.py
// @author Luis M. Jimenez
// @date 2022
//
// @brief Course: Computer Vision (1782)
// Dept. of Systems Engineering and Automation
// Automation, Robotics and Computer Vision Lab (ARVC)
// http://arvc.umh.es
// University Miguel Hernandez
//
// @note Description:
//	- This example captures images from a camera, shows them in a window and...
//  - Enables a handler for mouse events
//  - Enables a trackbar
//  - Shows RGB color value under cursor in overlay
//
*************************************************************************
"""

# Import libraries
import cv2 as cv
import numpy as np
import argparse

# -----------------------------------------
# Functions
# -----------------------------------------
"""
//----------------------------------------------------------------------
// Mouse events handler  for image window
//----------------------------------------------------------------------
// event:	event type sent to the handler ->  cv.EVENT_MOUSEMOVE,
//          cv.EVENT_LBUTTONDOWN, cv.EVENT_LBUTTONUP, cv.EVENT_LBUTTONDBLCLK,
//          cv.EVENT_RBUTTONDOWN, cv.EVENT_RBUTTONUP, cv.EVENT_RBUTTONDBLCLK,
//          cv.EVENT_MBUTTONDOWN, cv.EVENT_MBUTTONUP, cv.EVENT_MBUTTONDBLCLK,
//          cv.EVENT_MOUSEWHEEL, cv.EVENT_MOUSEHWHEEL
// x:	X-coordinate position of the mouse in window
// y:	Y-coordinate position of the mouse in window
// flags: aditional flags sent to the handler ->
//          cv.EVENT_FLAG_SHIFTKEY, cv.EVENT_FLAG_CTRLKEY, cv.EVENT_FLAG_ALTKEY,
//          cv.EVENT_FLAG_LBUTTON, cv.EVENT_FLAG_RBUTTON, cv.EVENT_FLAG_MBUTTON, 
// param: set in cv.SetMouseCallback
//----------------------------------------------------------------------
"""
def onMouse(event, x, y, flags, param):
    global capture, ID_FILE         # global variables used in the mouse handler
    global CURSOR_POS, EXIT, BUTTON_POS, BUTTON_SIZE

    # print(f"{event=}, {x=}, {y=}, {flags=}, {param=}")

    # on click left mouse button and SHIFT key, saves image
    if event==cv.EVENT_LBUTTONDOWN and (flags & cv.EVENT_FLAG_SHIFTKEY):
        filename = f"Image{ID_FILE}.jpg"
        print(f"Saving image window in file: {filename}")
        cv.imwrite(filename, capture)   # save window image
        ID_FILE += 1

    # on moving the cursor over the image
    if event == cv.EVENT_MOUSEMOVE:
        CURSOR_POS = (y, x)     # save new cursor position in global variable cursorPos (row,col)

    # on click left mouse button
    if event == cv.EVENT_LBUTTONDOWN:
        # checks if Exit button is clicked
        if (x > BUTTON_POS[0] and x < (BUTTON_POS[0] + BUTTON_SIZE[0]) and
            y > BUTTON_POS[1] and y < (BUTTON_POS[1] + BUTTON_SIZE[1]) ):
            EXIT = True


#----------------------------------------------------------------------
# Trackbar events handler
# x: trackbar position
#----------------------------------------------------------------------
def onTrackbar(x):
    global ALPHA
    ALPHA = x


"""
//----------------------------------------------------------------------
// make a copy of input image as background (converts it to color if necessary)
// Draws in overlay color information (under cursor) of input image 
// blends two images: disp image with overlay (24 bits BGR) and returns background image
// alpha -> 0-1  (0 means only background, 1: only overlay) [0.6 default]
// imageQuery: optional, peeks the color from this matrix instead of image
//----------------------------------------------------------------------
"""
def drawOverlay(image, alpha=0.6, imageQuery=None):
    global CURSOR_POS, BUTTON_POS, BUTTON_SIZE

    background = image.copy()  # creates a copy to preserve original image

    # Allocates memory for overlay image of the same size as input image
    overlay = np.zeros((image.shape[0], image.shape[1], 3), dtype=np.uint8)

    # if input image is not BGR, it is converted to BGR
    if image.ndim < 3:
        background = cv.cvtColor(background, cv.COLOR_GRAY2BGR)

    # Drawing the overlay image
    # draw button
    cv.rectangle(overlay, BUTTON_POS, BUTTON_POS + BUTTON_SIZE, (0, 120, 120), cv.FILLED)
    cv.rectangle(overlay, BUTTON_POS, BUTTON_POS + BUTTON_SIZE, (0, 255, 255), 1)

    # text with the pixel color under the cursor
    if imageQuery is None:
        color = image[CURSOR_POS]
    else:
        color = imageQuery[CURSOR_POS]

    if np.ndim(color) == 0:     # color is scalar
        cursorColor = f"Gray[{color}]"  # Gray scale level
    else:
        cursorColor = f"RGB{color[::-1]}"  # reverse BGR tuple

    # print(cursorColor)

    # offset to draw text centered in the rectangle
    textsize, baseline = cv.getTextSize(cursorColor, cv.FONT_HERSHEY_DUPLEX, 0.4, 1)
    offset = BUTTON_SIZE * 0.5 + np.array(textsize) * [-0.5, 0.5]

    cv.putText(overlay, cursorColor, BUTTON_POS + offset.astype(int),
               cv.FONT_HERSHEY_DUPLEX, 0.4, (255, 255, 255), 1, cv.LINE_AA)

    # blending both images
    mask = (overlay != 0).any(axis=2)  # logical mask with non black pixels (0,0,0) in overlay
    background[mask] = cv.addWeighted(background[mask], 1 - alpha, overlay[mask], alpha, 0)

    return background

# End drawOverlay function


# -----------------------------------------
# Global variables
# -----------------------------------------
WINDOW_CAMERA1 = '(W1) Camera 1'   # window id
CAMERA_ID = 0	                   # default camera
KEY_F5 = 7602176                   # F5 unicode key code
ID_FILE = 1                        # filename id

ALPHA = 60		        # % level of transparency
EXIT = False            # exit the program
CURSOR_POS = (0, 0)     # current position of the cursor over the window (row,col)
BUTTON_SIZE = np.array((160, 25))    # Overlay button Size (width,heigth)
BUTTON_POS = np.array((0, 0)) 	 # overlay button upper left corner position (x,y)


# check command line parameters (camera id)
parser = argparse.ArgumentParser(description='OpenCV example: captures images from a camera')
parser.add_argument('-c', dest='cameraID', type=int, default=CAMERA_ID, metavar='id', help='camera id')
CAMERA_ID = parser.parse_args().cameraID

# -----------------------------------------
# Put here the code to Initialize objets
# -----------------------------------------

# Open camera object
camera = cv.VideoCapture(CAMERA_ID)
if not camera.isOpened():
    print("you need to connect a camera, sorry.")
    exit()


# Getting camera resolution
cameraWidth = int(camera.get(cv.CAP_PROP_FRAME_WIDTH))
cameraHeight = int(camera.get(cv.CAP_PROP_FRAME_HEIGHT))

# Creating visualization windows
cv.namedWindow(WINDOW_CAMERA1, cv.WINDOW_AUTOSIZE)

# enable a trackbar associated to variable ALPHA
cv.createTrackbar("Transp.", WINDOW_CAMERA1,  ALPHA,  100, onTrackbar)

# Setting Mouse Handler
cv.setMouseCallback(WINDOW_CAMERA1, onMouse)

print(f"Capturing images from camera {CAMERA_ID} ({cameraWidth},{cameraHeight})")
print("...Hit F5 or Space bar to capture and save the image")
print("...Hit q/Q/Esc to exit.")

# -----------------------------------------
# Main Loop
# while there are images ...
# -----------------------------------------
while True:
    # Capture frame-by-frame
    ret, capture = camera.read()

    # if frame is read correctly ret is True
    if not ret:
        print("Can't receive frame (stream end?). Exiting ...")
        break
    # -----------------------------------------
    # Put your image processing code here
    # -----------------------------------------

    #capture = cv.cvtColor(capture, cv.COLOR_BGR2GRAY);

    # -----------------------------------------
    # Put your visualization code here
    # -----------------------------------------
    # draws overlay with pixel color under cursor on capture image
    disp = drawOverlay(capture, float(ALPHA)/100)

    cv.imshow(WINDOW_CAMERA1, disp)     # Display the resulting frame

    # check keystroke to exit (image window must be on focus)
    key = cv.pollKey()
    if key == ord('q') or key == ord('Q') or key == 27 or EXIT:
        break
    elif key == KEY_F5 or key == ord(' '):
        filename = f"Image{ID_FILE}.jpg"
        print(f"Saving image window in file: {filename}")
        cv.imwrite(filename, capture)   # save window image
        ID_FILE += 1

# End while (main loop)

# -----------------------------------------
# free windows and camera resources
# -----------------------------------------
cv.destroyAllWindows()
if camera.isOpened():  camera.release()
