home *** CD-ROM | disk | FTP | other *** search
/ Encyclopedia of Graphics File Formats Companion / GFF_CD.ISO / software / unix / saoimage / sao1_07.tar / mgfytabl.c < prev    next >
C/C++ Source or Header  |  1990-04-20  |  9KB  |  288 lines

  1. #ifndef lint
  2. static char SccsId[] = "%W%  %G%";
  3. #endif
  4.  
  5. /* Module:    mgfytabl.c (Magnify Table)
  6.  * Purpose:    print a table of image values on the screen
  7.  * Subroutine:    print_table()        returns: void
  8.  * Copyright:    1989, 1990 Smithsonian Astrophysical Observatory
  9.  *        You may do anything you like with this file except remove
  10.  *        this copyright.  The Smithsonian Astrophysical Observatory
  11.  *        makes no representations about the suitability of this
  12.  *        software for any purpose.  It is provided "as is" without
  13.  *        express or implied warranty.
  14.  * Note:    Based on code by Bill Wyatt's showimg
  15.  * Modified:    {0} Michael VanHilst    initial version        29 September 1988
  16.  *        {1} MVH redone for modularity, was ImageTable.c      21 Feb 1990
  17.  *        {n} <who> -- <does what> -- <when>
  18.  */
  19.  
  20. #include <stdio.h>        /* get stderr */
  21. #include <X11/Xlib.h>        /* X window stuff */
  22. #include <X11/Xutil.h>        /* X window manager stuff */
  23. #include "hfiles/struct.h"    /* declare structure types */
  24. #include "hfiles/extern.h"    /* extern main parameter structures */
  25.  
  26. /*
  27.  * Subroutine:    print_table
  28.  * Purpose:    Print out the values of memory image given the event
  29.  *        coordinates
  30.  * Note:    Uses event coords in control struct (control.event.xkey)
  31.  */
  32. void print_table ()
  33. {
  34.   int bufx, bufy;
  35.   int filex, filey;
  36.   int xarray[16];
  37.   int yarray[16];
  38.   int table_size;
  39.   int col_width;
  40.   int rot;
  41.   static int get_key_buf_coords(), set_table_params();
  42.   static void output_column_labels(), output_pixval_table();
  43.  
  44.   /* determine the buffer coordinates of the event */
  45.   if( get_key_buf_coords(&control.event.xkey, &bufx, &bufy) == 0 ) {
  46.     (void)printf("SORRY: portion of image not in buffer.\n");
  47.     return;
  48.   }
  49.   /* note if there is something special about the values */
  50.   if( buffer.shortbuf_summing > 1 ) {
  51.     (void)printf("Image buffer has summed values:\n");
  52.     (void)printf("Each pixel summed from %d by %d square of pixels in file\n",
  53.         coord.fb.block, coord.fb.block);
  54.     (void)printf("Square starts at file coordinates shown in table\n");
  55.   }
  56.   /* set a table size that fits with the word size needed */
  57.   if( img.fiscaled ) {
  58.     table_size = 7;
  59.     col_width = 10;
  60.   } else {
  61.     table_size = 11;
  62.     col_width = 6;
  63.   }
  64.   /* decide which pixels and get the relevant file coordinates */
  65.   rot = set_table_params(bufx, bufy, table_size, table_size, xarray, yarray);
  66.   filex = xarray[table_size];
  67.   filey = yarray[table_size];
  68.   /* identify the pixel and note any coordinate oddities */
  69.   (void)printf("\n\nFile Column: %d, Row: %d\n", filex, filey);
  70.   if( rot > 1 ) {
  71.     (void)printf("WARNING: image coordinates rotated from file\n");
  72.     (void)printf(" Table labels give image buffer offsets\n");
  73.     filex = 0;
  74.     filey = 0;
  75.   } else if( rot > 0 ) {
  76.     (void)printf("Image is orthogonally rotated from file\n");
  77.     (void)printf(" File rows read across, file columns read up and down\n");
  78.   }
  79.   /* make the table header line */
  80.   output_column_labels(xarray, table_size, col_width, filex);
  81.   /* print a table of values in a table_size x table_size picture region */
  82.   output_pixval_table(xarray, yarray, table_size, table_size, col_width,
  83.               xarray[table_size + 1], yarray[table_size + 1],
  84.               filex, filey);
  85. }
  86.  
  87. /*
  88.  * Subroutine:    get_key_buf_coords
  89.  * Purpose:    Determine the buffer coordinates of the pixel identified
  90.  *        with a key event.
  91.  * Returns:    1 if the pixel is in the image data buffer, else 0.
  92.  */
  93. static int get_key_buf_coords ( xkey, bufX, bufY )
  94.      XKeyEvent *xkey;
  95.      int *bufX, *bufY;
  96. {
  97.   float x, y;
  98.   void i_transform(), d_transform();
  99.  
  100.   /* translate event to buffer coordinates */
  101.   if( xkey->window == dispbox.ID ) {
  102.     i_transform(&coord.disptobuf, xkey->x, xkey->y, &x, &y);
  103.   } else if( xkey->window == panbox.ID ) {
  104.     i_transform(&coord.pantoimg, xkey->x, xkey->y, &x, &y);
  105.     d_transform(&coord.imgtobuf, (double)x, (double)y, &x, &y);
  106.   } else
  107.     return;
  108.   *bufX = (int)x;
  109.   *bufY = (int)y;
  110.   /* check if within buffer */
  111.   if ((*bufX < 0) || (*bufX >= coord.buf.width) ||
  112.       (*bufY < 0) || (*bufY >= coord.buf.height) )
  113.     return( 0 );
  114.   return( 1 );
  115. }
  116.  
  117. /*
  118.  * Subroutine:    output_column_labels
  119.  * Purpose:    Print a line of column indexes and underline it.
  120.  */
  121. static void output_column_labels ( xarray, cols, col_width, filex )
  122.      int xarray[];    /* array of column labels */
  123.      int cols;        /* number of cols */
  124.      int col_width;    /* width of each column */
  125.      int filex;        /* given coord to be highlighted */
  126. {
  127.   int i;
  128.   char format[32];
  129.  
  130.   /* space down and start next line over from row labels */
  131.   (void)printf("\n       ");
  132.   /* print the column numbers */
  133.   for( i=0; i<cols; i++ ) {
  134.     /* special treatment (reverse video) for the chosen pixel column */
  135.     if( xarray[i] == filex ) {
  136.       sprintf(format, "  %%c[7m%%%dd%%c[0m", col_width - 2);
  137.       (void)printf(format, 27, xarray[i], 27);
  138.     } else {
  139.       sprintf(format, "  %%%dd", col_width - 2);
  140.       (void)printf(format, xarray[i]);
  141.     }
  142.   }
  143.   /* carriage return and underline the column labels */
  144.   (void)printf("\n       ");
  145.   for( i=2; i<col_width; i++ )
  146.     format[i] = '-';
  147.   format[col_width] = '\0';
  148.   for( i=0; i<cols; i++)
  149.     (void)printf(format);
  150. }
  151.  
  152. /*
  153.  * Subroutine:    output_pixval_table
  154.  * Purpose:    Print a table of pixel values
  155.  * Note:    Uses VT100 escape sequence for reverse video (seems to work)
  156.  * Note:    Uses global image and buffer structures
  157.  */
  158. static void output_pixval_table ( xarray, yarray, cols, rows, col_width,
  159.                   bufx, bufy, filex, filey )
  160.      int xarray[], yarray[];    /* array of labels */
  161.      int cols, rows;        /* dimensions of table */
  162.      int col_width;        /* width of each column in output line */
  163.      int bufx, bufy;        /* buffer coord of upper left in table */
  164.      int filex, filey;        /* label vals of highlighted pixel */
  165. {
  166.   double dval;
  167.   int ival;
  168.   int clip;
  169.   int i, j;
  170.   char string[16];
  171.   int get_pixel_val();
  172.   void real_string(), integer_string();
  173.  
  174.   for( j=0; j<rows; j++ ) {
  175.     /* print <nl> and the row label (reverse video if chosen row) */
  176.     if( yarray[j] == filey )
  177.       (void)printf("\n %c[7m%4d%c[0m |", 27, yarray[j], 27);
  178.     else
  179.       (void)printf("\n%5d |", yarray[j]);
  180.     /* print the data */
  181.     for( i=0; i<cols; i++ ) {
  182.       if( get_pixel_val((bufx + i), (bufy + j), &ival, &dval, &clip) ) {
  183.     /* value is an integer */
  184.     integer_string(ival, clip, string, col_width);
  185.       } else {
  186.     real_string(dval, &string[1], col_width - 1);
  187.     if( clip ) {
  188.       if( clip > 0 )
  189.         *string = '>';
  190.       else
  191.         *string = '<';
  192.     } else
  193.       *string = ' ';
  194.       }
  195.       if( (xarray[i] == filex) && (yarray[j] == filey) )
  196.     (void)printf("%c[7m%s%c[0m", 27, string, 27);
  197.       else
  198.     (void)printf("%s", string);
  199.     }
  200.   }
  201.   (void)printf("\n");
  202. }
  203.  
  204. /*
  205.  * Subroutine:    set_table_params
  206.  * Purpose:    Put pixval table parameters in the x and y arrays
  207.  *        0 to (dim-1) has file coords,
  208.  *        _array[dim]: main file coord, _array[dim+1]: starting buf coord
  209.  */
  210. static int set_table_params ( bufx, bufy, xdim, ydim, xarray, yarray )
  211.      int bufx, bufy;
  212.      int xdim, ydim;
  213.      int *xarray, *yarray;
  214. {
  215.   float x0, x1, x2, y0, y1, y2;
  216.   int xinc, yinc;
  217.   int fx0, fx1, fx2, fy0, fy1, fy2;
  218.   int bufx0, bufy0, bufoff;
  219.   int i, rot;
  220.   void i_transform();
  221.   
  222.   /* determine starting buffer position */
  223.   bufoff = (xdim - 1) / 2;
  224.   bufx0 = bufx - bufoff;
  225.   if( bufx0 < 0 )
  226.     bufx0 = 0;
  227.   else if( (bufoff = bufx0 + xdim - coord.buf.width) > 0 )
  228.     bufx0 -= bufoff;
  229.   bufoff = (ydim - 1) / 2;
  230.   bufy0 = bufy - bufoff;
  231.   if( bufy0 < 0 )
  232.     bufy0 = 0;
  233.   else if( (bufoff = bufy0 + ydim - coord.buf.height) > 0 )
  234.     bufy0 -= bufoff;
  235.   /* store the coordinates of the first buffer element */
  236.   xarray[xdim+1] = bufx0;
  237.   yarray[ydim+1] = bufy0;
  238.   /* set buf0 to offset from focus pixel */
  239.   bufx0 = bufx0 - bufx; 
  240.   bufy0 = bufy0 - bufy; 
  241.   /* determine file coordinates of focus and two other pixels */
  242.   i_transform (&coord.buftofile, bufx, bufy, &x0, &y0);
  243.   i_transform (&coord.buftofile, bufx+1, bufy, &x1, &y1);
  244.   i_transform (&coord.buftofile, bufx, bufy+1, &x2, &y2);
  245.   if( coord.file.ioff > 0.1 ) {
  246.     fx0 = (int)x0;
  247.     fy0 = (int)y0;
  248.     fx1 = (int)x1;
  249.     fy1 = (int)y1;
  250.     fx2 = (int)x2;
  251.     fy2 = (int)y2;
  252.   } else {
  253.     fx0 = (int)(x0 + 0.5);
  254.     fy0 = (int)(y0 + 0.5);
  255.     fx1 = (int)(x1 + 0.5);
  256.     fy1 = (int)(y1 + 0.5);
  257.     fx2 = (int)(x2 + 0.5);
  258.     fy2 = (int)(y2 + 0.5);
  259.   }
  260.   /* store the focus coordinates */
  261.   xarray[xdim] = fx0;
  262.   yarray[ydim] = fy0;
  263.   /* determine the file increments along both axes and rotation */
  264.   if( (fx1 != fx0) && (fy1 == fy0) ) {
  265.     xinc = fx1 - fx0;
  266.     yinc = fy2 - fy0;
  267.     rot = 0;
  268.   } else if( (fx2 != fx0) && (fy2 = fy0) ) {
  269.     xinc = fx2 - fx0;
  270.     yinc = fy1 - fy0;
  271.     rot = 1;
  272.   } else {
  273.     for( i=0; i<xdim; i++ )
  274.       xarray[i] = bufx0++;
  275.     for( i=0; i<ydim; i++ )
  276.       yarray[i] = bufy0++;
  277.     return( 2 );
  278.   }
  279.   /* install the file coordinates */
  280.   xarray[0] = fx0 + (bufx0 * xinc);
  281.   for( i=1; i<xdim; i++ )
  282.     xarray[i] = xarray[i-1] + xinc;
  283.   yarray[0] = fy0 + (bufy0 * yinc);
  284.   for( i=1; i<ydim; i++ )
  285.     yarray[i] = yarray[i-1] + yinc;
  286.   return( rot );
  287. }
  288.