/* Example program taking two pictures using the Scribbler 2 and shows a
 *  picture composed of pieces of the two pictures */

#include <stdio.h>
#include <MyroC.h>

int
main (void)
{
  Picture  pic1,  pic2,  spliced;
  int width, height, midcol, midrow;
  
  rConnect ("/dev/rfcomm0");

  /* Take a picture from current position, turn slightly, and take another */
  pic1 = rTakePicture();
  rTurnLeft (1, 1);
  pic2 = rTakePicture();

  /* Display both pictures */
  rDisplayPicture (&pic1, 5, "Picture 1");
  rDisplayPicture (&pic2, 5, "Picture 2");

  /* Picture size depends on the camera -- extract and calculate the middle */
  height = pic1.height;
  width = pic1.width;
  midrow = height / 2;
  midcol = width / 2;

  spliced.height = height;
  spliced.width = width;

  /* Iterate over the pixel locations, taking the top-left and bottom-right
     quadrant's pixels from pic1, and the others from pic2 */
  for (int row = 0; row < height; row++)
  {
    for (int col = 0; col < width ; col++)
    {
      if ( ((col < midcol) && (row < midrow)) || /* top-left quadrant */
           ((col > midcol) && (row > midrow)) )  /* bottom-right quadrant */
        spliced.pix_array[row][col] = pic1.pix_array[row][col];           
      else
        spliced.pix_array[row][col] = pic2.pix_array[row][col]; 
    } // col
  } // row

  /* Display the result as a non-blocking command before exiting */
  rDisplayPicture (&spliced, -3, "Spliced Picture");
  rDisconnect();
  return 0;
} // main
