/*****************************************************
*   file c:\cips\display.c
*
*   Purpose:  These functions display images on
*   a the monitor.
*
*       External Calls:
*          cips.c - clear_text_screen
*
*       Modifications:
*          17 June 1987 - created
*          August 1990 - extension modifications for use
*              in the C Image Processing System.
********************************************************/

#include "d:\cips\cips.h"

/***************************
 *   display_image(...
 ****************************/

display_image(file_name, image, il, ie, ll, le,
              image_header, monitor_type,
              color_transform, invert,
              image_colors, display_colors)
   char    color_transform[],
           file_name[],
           monitor_type[];
   int     display_colors,
           image_colors,
           invert,
           il,
           ie,
           ll,
           le;
   short   image[ROWS][COLS];
   struct  tiff_header_struct *image_header;
{
   char  channels[80],
         response[80];

   int   a,
         b,
         c,
         channel,
         display_mode,
         key,
         horizontal,
         max_horizontal,
         max_vertical,
         not_finished,
         r,
         vertical;

   unsigned int block,
                color,
                i,
                j,
                x,
                y;


   not_finished = 1;
   while(not_finished){


      if(display_colors == 16){
         if(monitor_type[0] == 'V'){
            horizontal   = 4;
            vertical     = 6;
            display_mode = _VRES16COLOR; /* MSC 6.0 */
         }  /* ends if V */
         if(monitor_type[0] == 'E'){
            horizontal   = 3;
            vertical     = 6;
            display_mode = _ERESCOLOR; /* MSC 6.0 */
         }  /* ends if E */

      }  /* ends if colors == 16 */

      else{
         horizontal   = 2;
         vertical     = 2;
         display_mode = _MAXCOLORMODE; /* MSC 6.0 */
      }

      max_horizontal = (image_header->image_length+50)/100;
      max_vertical   = (image_header->image_width+50)/100;

      if(horizontal > max_horizontal) horizontal = max_horizontal;
      if(vertical > max_vertical) vertical = max_vertical;


        /* set graphics mode */

   _setvideomode(display_mode); /* MSC 6.0 */
   if(display_colors == 16) map_16_shades_of_gray(display_mode);

   /****************************************
   *   Loop over this size and
   *   read and display ROWSxCOLS arrays.
   *****************************************/

      for(a=0; a<vertical; a++){
         for(b=0; b<horizontal; b++){
            x = a*100;
            y = b*100;
            read_tiff_image(file_name, image, il+y, 
                            ie+x, ll+y, le+x);
            display_image_portion(image, x, y, display_colors, 
                                  image_colors, invert);
         }        /* ends loop over b */
      }        /* ends loop over a */

      read_string(response);
      printf("\nEnter 0 to quit 1 to do again");
      get_integer(&not_finished);

          /* set display back to text mode */
      clear_text_screen();

   }  /* ends while not_finished  */
}  /* ends main  */


 /**********************************
 *   display_menu_for_display_image(
 ************************************/

display_menu_for_display_image(image_colors,
             display_colors, invert,
             color_transform, monitor_type)
   char color_transform[], monitor_type[];
   int  *invert, *image_colors, *display_colors;
{
   char response[80];
   int  int_response, not_finished, r;

   not_finished = 1;
   while(not_finished){
      printf("\n\nDISPLAY> Enter choice (0 for no change) ");
      printf("\nDISPLAY> 1. Invert is %d (1=on 0=off)", *invert);
      printf("\nDISPLAY> 2. Color Transform-- %s", 
             color_transform);
      printf("\nDISPLAY> 3. Input image has %d colors", 
             *image_colors);
      printf("\nDISPLAY> 4. Display will show %d colors", 
             *display_colors);
      printf("\nDISPLAY> 5. Monitor type is %s",
             monitor_type);
      printf("\nDISPLAY> _\b");
      get_integer(&r);

      if(r == 0){
         not_finished = 0;
      }

      if(r == 1){
         printf(
            "\nDISPLAY> Enter 1 for invert on 0 for invert off");
         printf("\nDISPLAY> ___");
         get_integer(&int_response);
         *invert = int_response;
      }  /* ends if r == 1 */

      if(r == 2){
         printf("\nDISPLAY> Enter the new color transform mode ");
         printf(
             "\nDISPLAY> (S) Straight mode   (M) Modified mode");
         printf("\nDISPLAY> _\b");
         read_string(response);
         if((response[0] == 'S') ||
            (response[0] == 's'))
               strcpy(color_transform, "Straight mode");
         else
               strcpy(color_transform, "Modified mode");
      }  /* ends if r == 2  */

      if(r == 3){
         printf(
         "\nDISPLAY> Enter the number of colors in the input image"
          );
         printf("\nDISPLAY> ___");
         get_integer(&int_response);
         *image_colors = int_response;
      }  /* ends if r == 3 */

      if(r == 4){
         printf( 
          "\nDISPLAY> Enter the number of colors for the display");
         printf("\nDISPLAY> ___");
         get_integer(&int_response);
         *display_colors = int_response;
      }  /* ends if r == 4 */

      if(r == 5){
         printf("\nDISPLAY> Enter the new monitor type");
         printf(
             "\nDISPLAY> (E) EGA     (V) VGA");
         printf("\nDISPLAY> _\b");
         read_string(response);
         if((response[0] == 'E') ||
            (response[0] == 'e'))
               strcpy(monitor_type, "EGA");
         else
               strcpy(monitor_type, "VGA");
      }  /* ends if r == 5  */


   }  /* ends while not_finished  */
}  /* ends display_menu  */


 /********************************
 *   display_image_portion(...
 *********************************/

display_image_portion(image, x, y, display_colors, image_colors,
                      invert)
   int      invert, display_colors, image_colors;
   short    image[ROWS][COLS];
   unsigned int x, y;
{
   unsigned int color, i, j;

      if(invert == 1){
         if(image_colors == 256){
            for(i=0; i<ROWS; i++)
               for(j=0; j<COLS; j++)
                  image[i][j] = 255 - image[i][j];
         }  /* ends if image_colors = 256 */

         if(image_colors == 16){
            for(i=0; i<ROWS; i++)
               for(j=0; j<COLS; j++)
                  image[i][j] = 15 - image[i][j];
         }  /* ends if image_colors = 16 */

      }  /* ends if invert == 1 */


      for(i=0; i<ROWS; i++){
         for(j=0; j<COLS; j++){

            if( (display_colors == 16) &&
                (image_colors  == 256))
               color = image[i][j]/16;
            if( (display_colors == 16) &&
                (image_colors  == 16))
               color = image[i][j];
            if( (display_colors == 256) &&
                (image_colors  == 256))
               color = image[i][j];
            if( (display_colors == 256) &&
                (image_colors  == 16))
               color = image[i][j]*16;

            _setcolor(color);    /* MSC 6.0 statements */
            _setpixel(j+x, i+y);

            /*my_set_pixel(j+x, i+y, color);*/
         }  /* ends loop over j  */
      }     /* ends loop over i  */

}  /* ends display_image_portion  */


/**********************************************
*   map_16_shades_of_gray(...
*
*   This function maps 16 shades of gray into
*   the first 16 color indices.  This allows
*   you to display a true "black and white"
*   image on a color monitor.
*********************************************/

map_16_shades_of_gray(display_mode)
   int display_mode;
{
   /* all MSC 6.0 statements */
_setvideomode(display_mode);
_remappalette(0,  0x000000L);
_remappalette(1,  0x040404L);
_remappalette(2,  0x080808L);
_remappalette(3,  0x0c0c0cL);
_remappalette(4,  0x101010L);
_remappalette(5,  0x141414L);
_remappalette(6,  0x181818L);
_remappalette(7,  0x1c1c1cL);
_remappalette(8,  0x202020L);
_remappalette(9,  0x242424L);
_remappalette(10, 0x282828L);
_remappalette(11, 0x2c2c2cL);
_remappalette(12, 0x303030L);
_remappalette(13, 0x343434L);
_remappalette(14, 0x383838L);
_remappalette(15, 0x3f3f3fL);
}

