#!/bin/bash

# *** WIP ***

# I'm using a regular V1.3 pi camera and a NOIR V1.3 pi camera.
# The O/S sees them both as identical.  
# It turns out that one of the two cameras has a marginally different focal length
# even though they are both supposed to be the same.
# The NOIR camera is a tiny bit more wide-angle than the vis camera -
# the image is maybe 3% wider?

# Also the lens/sensor unit on the NOIR camera is deeper than the one on the
# regular camera and it doesn't lock into place quite as well in the prism holder,
# which I'll need to re-engineer slightly by shaving some depth off the outside
# bump.  Note that the space used for that bump may be needed for adding an
# IR pass or UV pass filter to cut out the visible light.

# The right-hand IR image looks a little blurry, and I'm not sure if the blurriness
# is a focus issue or due to the glass in the prism.

# I do have one other spare NOIR camera (not counting the cameras attached to my
# previous camera builds - the IR one and the UV one) but it says "V2" rather than V1.3.
# which I believe is the 8Mp camera so using it would need some more coding work...
# I'll either cannibalize my original IR camera or hunt around for the other older
# NOIR units that I'm sure I have but which are not in my box of camera components.

#  NOTE: this pi 5 seems to lose wifi when the cameras are both running.  Makes development
#  very awkward as I only have a small HDMI display attached and an awkward Kano keyboard
#  not to mention the lack of decent cut & paste that I get when accessing the pi from a
#  Putty ssh client with my windows portable.
#  I don't know if this is a hardware problem with this unit, or with the version of
#  the OS I have installed on this unit.  Changing the OS will be easy, swapping the
#  unit out, not so much, since my other Pi 5 is being used for my file server.

# We run until ^C is pressed

terminate() {
  killall libcamera-hello
  exit 0
}

trap terminate SIGINT

# kill any previous runs
killall libcamera-hello > /dev/null 2> /dev/null

# current HDMI display is 1024 x 600 resolution, camera currently being used in 640 x 480 resolution:
DISPLAY_WIDTH=1024 ; DISPLAY_HEIGHT=600  ; HALF_DISPLAY_WIDTH=512 ; HALF_DISPLAY_HEIGHT=300 
#SENSOR_WIDTH=640 ; SENSOR_HEIGHT=480  # in current mode for testing
SENSOR_WIDTH=1296 ; SENSOR_HEIGHT=972  # in current mode for testing

# Width and Height in pixels of image extracted from camera (not the full area available):
# Need some slack to allow image to be slightly repositioned and rescaled a little, and also
# allows for small amount of cut-off from the 3d-printed frame (see README re angle of view)
IMAGE_WIDTH=$(echo "scale=0; ($SENSOR_WIDTH * 0.9)/1" | bc)
IMAGE_HEIGHT=$(echo "scale=0; (($IMAGE_WIDTH * $SENSOR_HEIGHT) / $SENSOR_WIDTH) /1" | bc)

# RIGHT HAND IMAGE:  higher X value moves pic to left, higher Y value move pic upward
# left hand image 'to do' - offsets may be different due to left/right reflection in axis

# sensor region we are using by default works out to 100,100 to 1196,872

# xl/yt positions:

# Left X positions in pixels - tweak these to align the two images perfectly:
# (ROI_XL_PIXEL+IMAGE_WIDTH must be <= SENSOR_WIDTH)
IR_ROI_XL_PIXEL=0  # 100  #73 ; # IR_ROI_XL_NORM=$(echo "scale=5; $IR_ROI_XL_PIXEL / $SENSOR_WIDTH" | bc)
VIS_ROI_XL_PIXEL=0 #25 ; # VIS_ROI_XL_NORM=$(echo "scale=5; $VIS_ROI_XL_PIXEL / $SENSOR_WIDTH" | bc)

# Top Y positions in pixels:
IR_ROI_YT_PIXEL=0 # 100  #28 ; # IR_ROI_YT_NORM=$(echo "scale=5; $IR_ROI_YT_PIXEL / $SENSOR_HEIGHT" | bc)
VIS_ROI_YT_PIXEL=0 #13 ; # VIS_ROI_YT_NORM=$(echo "scale=5; $VIS_ROI_YT_PIXEL / $SENSOR_HEIGHT" | bc)


# xr/yb positions are placeholders for now:

# Right X positions in pixels:
IR_ROI_XR_PIXEL=1296 # 1196 # $(echo "scale=0; ($SENSOR_WIDTH - 100)/1" | bc)
VIS_ROI_XR_PIXEL=1296 # $(echo "scale=0; ($SENSOR_WIDTH - 100)/1" | bc)

# Bottom Y positions in pixels:
IR_ROI_YB_PIXEL=972 # 872 # $(echo "scale=0; ($SENSOR_HEIGHT - 100)/1" | bc)
VIS_ROI_YB_PIXEL=972 # $(echo "scale=0; ($SENSOR_HEIGHT - 100)/1" | bc)


IR_IMAGE_WIDTH=$(echo "scale=5; $IR_ROI_XR_PIXEL - $IR_ROI_XL_PIXEL" | bc)
IR_IMAGE_HEIGHT=$(echo "scale=5; $IR_ROI_YB_PIXEL - $IR_ROI_YT_PIXEL" | bc)

IR_ROI_WIDTH_NORM=$(echo "scale=5; $IR_IMAGE_WIDTH / $SENSOR_WIDTH" | bc)
IR_ROI_HEIGHT_NORM=$(echo "scale=5; $IR_IMAGE_HEIGHT / $SENSOR_HEIGHT" | bc)

VIS_IMAGE_WIDTH=$(echo "scale=5; $VIS_ROI_XR_PIXEL - $VIS_ROI_XL_PIXEL" | bc)
VIS_IMAGE_HEIGHT=$(echo "scale=5; $VIS_ROI_YB_PIXEL - $VIS_ROI_YT_PIXEL" | bc)

VIS_ROI_WIDTH_NORM=$(echo "scale=5; $VIS_IMAGE_WIDTH / $SENSOR_WIDTH" | bc)
VIS_ROI_HEIGHT_NORM=$(echo "scale=5; $VIS_IMAGE_HEIGHT / $SENSOR_HEIGHT" | bc)

# currently the way the cameras are inserted, the visible-light camera has the straight-through path and the IR camera uses the mirror path

# --width and --height are the pixel dimensions of the output image which is scaled from the ROI.

# visible camera, left image, straight path:
echo VIS: Extracting region from ${VIS_ROI_XL_PIXEL},${VIS_ROI_YT_PIXEL} to ${VIS_ROI_XR_PIXEL},${VIS_ROI_YB_PIXEL} as a $VIS_IMAGE_WIDTH x $VIS_IMAGE_HEIGHT bitmap to be displayed in a $HALF_DISPLAY_WIDTH x $HALF_DISPLAY_HEIGHT area in the left half of the display.
DISPLAY=:0 libcamera-hello --tuning-file /usr/share/libcamera/ipa/rpi/pisp/ov5647.json      \
       --camera 0 -t 0 --width $VIS_IMAGE_WIDTH --height $VIS_IMAGE_HEIGHT --preview 0,0,$HALF_DISPLAY_WIDTH,$HALF_DISPLAY_HEIGHT \
       --vflip \
       --roi "${VIS_ROI_XL_NORM},${VIS_ROI_YT_NORM},${VIS_ROI_WIDTH_NORM},${VIS_ROI_HEIGHT_NORM}" \
       2> /dev/null &

# IR camera, right image, mirrored path:
echo IR: Extracting region from ${IR_ROI_XL_PIXEL},${IR_ROI_YT_PIXEL} to ${IR_ROI_XR_PIXEL},${IR_ROI_YB_PIXEL} as a $IR_IMAGE_WIDTH x $IR_IMAGE_HEIGHT bitmap to be displayed in a $HALF_DISPLAY_WIDTH x $HALF_DISPLAY_HEIGHT area in the right half of the display.
## testing with 2 identical vis cameras at the moment: DISPLAY=:0 libcamera-hello --tuning-file /usr/share/libcamera/ipa/rpi/pisp/ov5647_noir.json \
DISPLAY=:0 libcamera-hello --tuning-file /usr/share/libcamera/ipa/rpi/pisp/ov5647.json \
       --camera 1 -t 0 --width $IR_IMAGE_WIDTH --height $IR_IMAGE_HEIGHT --preview $HALF_DISPLAY_WIDTH,0,$HALF_DISPLAY_WIDTH,$HALF_DISPLAY_HEIGHT \
       --vflip --hflip \
       --roi "${IR_ROI_XL_NORM},${IR_ROI_YT_NORM},${IR_ROI_WIDTH_NORM},${IR_ROI_HEIGHT_NORM}" \
       2> /dev/null

exit 0
