/* Program to illustrate the creation and development of a Picture
   with MyroC.

   For this example, a Picture is developed, displayed, and saved with
   these properties:

   * the Picture will be 200 pixels high by 300 pixels wide
   * the outside border of the picture is black
   * the inside picture (a square of 150 pixels by 150 pixels)
        has diagonal rows of colored stripes
   
   Written by Henry Walker with modifications by Jerod Weinman
*/

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

/* Create and return an image that is all black */
Picture
create_black_image (int height, int width);

/* Add a diagonal rainbow of stripes to a Picture */
void
add_stripes (Picture * p_pic);

int
main (void)
{
  printf ("program to create, display, and save an image\n");

  printf ("creating and displayng a black image\n");
  Picture pic = create_black_image (200, 300);
  /* Display image for 5 seconds in window called "original pic" */
  rDisplayPicture (&pic, -5.0, "original pic");
  
  printf ("adding stripes to image and displaying the result\n");
  add_stripes (&pic);
  rDisplayPicture (&pic, 5.0, "stripped pic");

  printf ("saving picture to file called 'stripped-picture.jpg'\n");
  rSavePicture (&pic, "stripped-picture.jpg");
  
  return 0;
} // main

/* Create and return an image that is all black */
Picture
create_black_image (int height, int width)
{
  Picture newPic;
  const Pixel blackPixel = {0, 0, 0};

  /* set dimensions of new picture */
  newPic.width = width;
  newPic.height = height;

  /* iterate through all pixels in the picture, setting each to black */
  for (int row = 0; row < height; row++)
    for (int col = 0; col < width; col++)
      newPic.pix_array[row][col] = blackPixel;

  return newPic;
} // create_black_image

/* Add a diagonal rainbow of stripes to a Picture */
void
add_stripes (Picture * p_pic)
{
  const int TOP_BOT_BORDER = 25;
  const int LEFT_RIGHT_BORDER = 75;

  /* Calculate loop bounds (only once) */
  const int end_row = (*p_pic).height - TOP_BOT_BORDER;
  const int end_col = (*p_pic).width - LEFT_RIGHT_BORDER;
  
  /* Define an array of pixel colors */
  const Pixel colorPalette [6] = {   {255, 0, 0},      /* red */
                                     {0, 0, 255},      /* blue  */
                                     {255, 255, 0},    /* redGreen */
                                     {0, 255, 0},      /* green */
                                     {255, 0, 255},    /* redBlue*/
                                     {0, 255, 255}  }; /* blueGreen*/
                             
  /* in adding stripes to the image, leave a border unchanged at top and
     bottom, left and right */
  for (int row = TOP_BOT_BORDER ; row < end_row ; row++)
    for (int col = LEFT_RIGHT_BORDER ; col < end_col ; col++)
    {
      /* stripes will be 10 pixels wide,
         and will repeat every 6 colors */
      int colorIndex = ((row + col) / 10) % 6;
      
      (*p_pic).pix_array[row][col] = colorPalette [colorIndex];
    }
} // add_stripes


