// FILE: scilabWhiteBox0_1a.sce
// DATE: 03/01/20 11:49
// AUTH: P.Oh
// VERS: 0.1a: draw white box on gray-scaled video
// REFS: scilabHelloVision1_1b.sci

// (1) initialize the Scilab Computer Vision Module
scicv_Init(); 

// (2) Get ID of the webcam (assumes only 1 webcam connected)
videoCapture = new_VideoCapture(0); 

// (3) Set up a current graphic figure (window) - which will display our video

f = scf(0); 

// Grab one frame
[ret, frame] = VideoCapture_read(videoCapture); // grab and return a frame
subplot(1,3,1); // Set up 1 row and 2 columns of sub-plots.  Draw in Plot 1
matplot(frame);

frameRows = Mat_rows_get(frame);
frameCols = Mat_cols_get(frame);
disp("Number of rows:");
disp(frameRows);
disp("Number of cols:");
disp(frameCols);

boxColWidth = 75; 
boxRowHeight = 50; 
boxRowCenter = 128; 
boxColCenter = 128;

boxTopLeftRowCorner = boxRowCenter - ((boxRowHeight-1)/2); 
boxTopLeftColCorner = boxColCenter - ((boxColWidth-1)/2);
boxTopRightRowCorner = boxRowCenter - ((boxRowHeight-1)/2);
boxTopRightColCorner = boxColCenter + ((boxColWidth-1)/2);
boxBottomLeftRowCorner = boxRowCenter + ((boxRowHeight-1)/2);
boxBottomLeftColCorner = boxColCenter - ((boxColWidth-1)/2);
boxBottomRightRowCorner = boxRowCenter + ((boxRowHeight-1)/2);
boxBottomRightColCorner = boxColCenter + ((boxColWidth-1)/2);

// (4) Endless loop that grabs frame, displays it, and repeats
while is_handle_valid(f)

    [ret, frame] = VideoCapture_read(videoCapture); // grab and return a frame
  
    if is_handle_valid(f) then      
        // ret is TRUE, so display frame
        subplot(1,2,1); // Set up 1 row and 2 columns of sub-plots.  Draw in Plot 1
        matplot(frame); // Display original frame in this plot

        // Draw in Plot 2: Grey-scale version of RGB color frame
        // greyFrame = new_Mat(:, :);
        greyFrame = cvtColor(frame, CV_BGR2GRAY);
        // disp(greyFrame(320,240)
        S = imread(greyFrame, CV_LOAD_IMAGE_GRAYSCALE);
        matplot(S)
        for i=0:frameRows-1
            for j=0:frameCols-1
               val = greyFrame(i, j);
               if (i==boxTopLeftRowCorner || i==boxBottomLeftRowCorner) then
                   // OK, we're on box's top or bottom row
                   if (j>=boxTopLeftColCorner && j<=boxTopRightColCorner) || (j>=boxBottomLeftColCorner && j<=boxBottomRightColCorner) then
                       // Draw top OR bottom line
                       val = 255; // make row white between left and right side
                   end // otherwise just keep the original value of val
               end // end if that checks for box's top or bottom row
               if j==boxTopLeftColCorner || j==boxTopRightColCorner then 
                  // OK, we're on left or right side
                  if (i>=boxTopLeftRowCorner && i<=boxBottomLeftRowCorner) || (i>=boxTopRightRowCorner && i<=boxBottomRightRowCorner) then
                     // Draw left OR right line
                     val = 255; // make column white between top and bottom row
                  end;
               end // end if that checks for box's left or right side             
              greyFrame(i,j) = val;
            end
        end
        subplot(1,2,2);
        matplot(greyFrame);

    end // end if  
end // end while


delete("all"); // kill all frames
