/* program to display a sequence of images
 *    each image has an outer blue border, a middle green region, 
 *       and an inner red section
 *    successive images the blue and green regions get 
 *       successively bigger, smaller, bigger, etc.
 *
 * This program and all MyroC software is licensed under the Creative Commons
 * Attribution-Noncommercial-Share Alike 3.0 United States License.
 * Details may be found at http://creativecommons.org/licenses/by-nc-sa/3.0/us/
 */

/* thus program only displays one image window, entitled "image window"
 */

/* utilize a MyroC-type package for rDisplayPicture
 */
#include "MyroC.h"
#include <stdio.h>
#include <unistd.h>   // needed for sleep function

/* approach outline based on two threads:
 * this [user] program runs in one thread
 *    as user thread, this program provides overall control of the robot, images, etc.
 * a second thread provides OpenGL control for the display of images
 *
 * overall algorithm
 *   utilize shared Picture struct variable 
 *   first thread 
 *      provides general user interface
 *      call to rDisplayPicture
 *         set shared Picture struct variable to desired Picture iamge
 *         if second thread not running, 
 *             create second thread
 *             initialize graphics environment
 *             establish shared Picture image as texture for image
 *             establish new_image procedure as glutIdleFunc
 *         if second thread already running
 *             first thread sends signal to activate processing in new_image procedure
 *             new_image uses glTexImage2D to establish shared Picture image
 *             use glutPostRedisplay to force display of new image
 */

/* create picture:
 *    blue outer border
 *    green middle section
 *    red inner section
 * borders get successively larger for indent 0, 1, 2, 3, 4
 *             successively smaller for indent 5, 6, 7, 8, 9
 */
void init_pic (Picture * pic, int indent)
{
  int row, col;
  for (row = 0; row < pic->height; row++)
    for (col = 0; col < pic->width; col++)
      { 
	indent = indent % 10;
	int border;
	if (indent < 5)
	  border = (indent + 1) * 5;
	else
	  border = (10 - indent) * 5;

	pic->pix_array[row][col].R = 0;
	pic->pix_array[row][col].G = 0;
	pic->pix_array[row][col].B = 0;
	if ((row < border) || (row >= pic->height-2*border) 
	    || (col < 2*border) || ( col >= pic->width-2*border))
	  {
	    pic->pix_array[row][col].B = 255;
	    // printf ("blue row: %d, col: %d\n", row, col);
	  }
	else if ((row < 2*border) || (row >= pic->height-4*border) 
		 || (col < 4*border) || ( col >= pic->width-4*border))
	  {
	    pic->pix_array[row][col].G = 255;
 	    //printf ("green  row: %d, col: %d\n", row, col);
	  }
	else
	  {
	    pic->pix_array[row][col].R = 255;
 	    //printf ("red row: %d, col: %d\n", row, col);
	  }
      }
}

/* command-line parameters might include -display, -geometry for X */
int main (int argc, char* argv [])
{
  Picture pic;

  /* printf statements added to example to report processing steps */
  printf ("example program to display a sequence of rectanges\n");
  printf ("   in which borders get bigger, smaller, bigger, etc.\n");

  int i;

  char * files[13] = {"one.jpg", "two.jpg", "three.jpg",
                       "four.jpg", "five.jpg", "six.jpg",
                       "seven.jpg", "eight.jpg", "nine.jpg",
                       "ten.jpg", "eleven.jpg", "twelve.jpg",
                       "thirteen.jpg"};

  for (i = 0; i < 5; i++)
    {
      printf ("initializing image %d\n", i);
      
      //pic.height = 35*(i+1);
      //pic.width  = 50*(i+1);

      pic.height = 192;
      pic.width  = 256;

      init_pic (&pic, i);
      printf ("saving image\n");
      rSavePicture (&pic, files[i]);
     }

  printf ("program completed\n");
  return 0;
}
