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

  1. #ifndef lint
  2. static char SccsId[] = "%W%  %G%";
  3. #endif
  4.  
  5. /* Module:    mgfyval.c (Magnify Value)
  6.  * Purpose:    Get file pixel value and make a formatted string to show it
  7.  * Subroutine:    get_pixel_val()        returns: int
  8.  * Subroutine:    integer_string()    returns: void
  9.  * Subroutine:    real_string()        returns: void
  10.  * Copyright:    1990 Smithsonian Astrophysical Observatory
  11.  *        You may do anything you like with this file except remove
  12.  *        this copyright.  The Smithsonian Astrophysical Observatory
  13.  *        makes no representations about the suitability of this
  14.  *        software for any purpose.  It is provided "as is" without
  15.  *        express or implied warranty.
  16.  * Note:    Table based on code from Bill Wyatt's showimg
  17.  * Modified:    {0} Michael VanHilst    initial version         21 February 1990
  18.  *        {1} MVH improved G formatter (c)MVH           1 Jan 1991
  19.  *        {n} <who> -- <does what> -- <when>
  20.  */
  21.  
  22. #include <stdio.h>        /*  Get stderr  */
  23.  
  24. #ifndef VMS
  25. #ifdef SYSV
  26. #include <string.h>        /*  strlen, strcat, strcpy, strrchr  */
  27. #else
  28. #include <strings.h>        /*  strlen, strcat, strcpy, rindex  */
  29. #define strchr index        /*  Needed for a few unenlightened BSD's  */
  30. #define strrchr rindex
  31. #endif
  32. #else
  33. #include <string.h>        /*  strlen, strcat, strcpy, strrchr  */
  34. #endif
  35.  
  36. #include <X11/Xlib.h>        /*  X window stuff  */
  37. #include <X11/Xutil.h>        /*  X window manager stuff  */
  38. #include "hfiles/struct.h"    /*  Declare structure types  */
  39. #include "hfiles/constant.h"    /*  Define ARR_ codes  */
  40. #include "hfiles/extern.h"    /*  Extern main parameter structures  */
  41.  
  42. #ifdef ANSIC
  43. /*  Exported declarations must be centralized before ANSI C can be used  */
  44.  
  45. int        get_pixel_val(    int bufx, int bufy,
  46.                 int* ival, double* dval, int* clip);
  47. void        integer_string(    int ival, int clip, char *string, int width);
  48. void        real_string(    double val, char *string, int width);
  49. static void    mark_overflow(    char *string, int string_width,
  50.                 char* label, int label_width);
  51.  
  52. #else
  53.  
  54.   void i_transform();
  55.   int get_pixel_val();
  56.   void integer_string(), real_string();
  57.   static void mark_overflow();
  58.  
  59. #endif
  60.  
  61.  
  62. /*  Subroutine:    get_pixel_val
  63.  *  Purpose:    Return pixel value and code representing best format
  64.  *  Returns:    1 if value is an integer, else 0
  65.  *  Note:    Puts integer val in ival
  66.  *  Note:    If float, double or scaled, puts float val in dval
  67.  *  Note:    Sets clip if buffer value is at clipped value edge (IRAF)
  68.  */
  69. #ifdef ANSIC
  70. int get_pixel_val( int bufx, int bufy, int* ival, double* dval, int* clip )
  71. #else
  72. int get_pixel_val ( bufx, bufy, ival, dval, clip )
  73.      int bufx, bufy;    /* shortbuf ("buf") coordinates */
  74.      int *ival;        /* gets integer value */
  75.      double *dval;    /* gets floating point value (unless just integer) */
  76.      int *clip;        /* -1 if at clipping min, 1 if at clipping max */
  77. #endif
  78. {
  79.   int val;
  80.  
  81.   *clip = 0;
  82.   if( img.fiscaled == 0 ) {
  83.     *ival = buffer.shortbuf[(bufy * coord.buf.width) + bufx];
  84.     return( 1 );
  85.   } else {
  86.     if( (buffer.filebuf == NULL) ||
  87.         (buffer.filebuf == (char *)buffer.shortbuf) ) {
  88.       /* values scaled, originals not available */
  89.       val = buffer.shortbuf[bufx + (bufy * coord.buf.width)];
  90.       *dval = ((double)val * img.fiscale) + img.fibias;
  91.       /*  Print strings with spaces padding out the end  */
  92.       if( val <= buffer.clipmin )
  93.     *clip = -1;
  94.       else if( val >= buffer.clipmax )
  95.     *clip = 1;
  96.     } else {
  97.       /*  Values scaled, originals in filebuf  */
  98.       float fbX, fbY;
  99.       i_transform(&coord.buftofbuf, bufx, bufy, &fbX, &fbY);
  100.       if( img.storage_type == ARR_I4 ) {
  101.     *dval = (double)
  102.       *((int *)(buffer.filebuf +
  103.             (((int)fbX + ((int)fbY * coord.fbuf.width)) *
  104.              sizeof(int))));
  105.       } else if( img.storage_type == ARR_R4 ) {
  106.     *dval = (double)
  107.       *((float *)(buffer.filebuf +
  108.               (((int)fbX + ((int)fbY * coord.fbuf.width)) *
  109.                sizeof(float))));
  110.       } else if( img.storage_type == ARR_R8 ) {
  111.     *dval = *((double *)(buffer.filebuf +
  112.                 (((int)fbX + ((int)fbY * coord.fbuf.width)) *
  113.                  sizeof(double))));
  114.       } else
  115.     *dval = 0.0;
  116.       if( img.fscaled )
  117.     *dval = img.fbias + (*dval * img.fscale);
  118.     }
  119.     *ival = (int)(*dval);
  120.     if( ((double)(*ival)) == *dval )
  121.       return( 1 );
  122.     else
  123.       return( 0 );
  124.   }
  125. }
  126.  
  127.  
  128. static char *iform[16] = { "x", "%d", "%2d", "%3d", "%4d", "%5d", "%6d",
  129.                  "%7d", "%8d", "%9d", "%10d", "%11d", "%12d",
  130.                  "%13d", "%14d", "%15d" };
  131. /*  Subroutine:    integer_string
  132.  *  Purpose:    Create an integer string, include clipping mark if appropriate
  133.  *  Note:    to be called with full column width
  134.  *  Note:    2<=width<=15
  135.  */
  136. #ifdef ANSIC
  137. void integer_string( int ival, int clip, char *string, int width )
  138. #else
  139. void integer_string ( ival, clip, string, width )
  140.      int ival;
  141.      int clip;
  142.      char *string;
  143.      int width;
  144. #endif
  145. {
  146.   char *edge;
  147.  
  148.   (void)sprintf(string, iform[width], ival);
  149.   if( clip ) {
  150.     if( string[0] != ' ' ) {
  151.       if( width > 8 )
  152.     real_string((double)ival, &string[1], width-1);
  153.       else
  154.     mark_overflow(string, width, "CLIP", 4);
  155.       edge = string;
  156.     } else
  157.       edge = strrchr(string, ' ');
  158.     /*  If value known to be clipped, indicate true value is beyond this  */
  159.     if( clip > 0 )
  160.       *edge = '>';
  161.     else
  162.       *edge = '<';
  163.   } else {
  164.     if( string[width] != '\0' ) {
  165.       if( width > 8 ) {
  166.     real_string((double)ival, &string[1], width-1);
  167.     *string = ' ';
  168.       } else
  169.     mark_overflow(string, width, "ovfl", 4);
  170.     }
  171.   }
  172. }
  173.  
  174.  
  175. /*  Subroutine:    mark_overflow
  176.  *  Purpose:    Print overflow message in place of value
  177.  */
  178. #ifdef ANSIC
  179. static void mark_overflow( char *string, int string_width,
  180.                char* label, int label_width )
  181. #else
  182. static void mark_overflow ( string, string_width, label, label_width )
  183.      char *string;
  184.      int string_width;
  185.      char *label;
  186.      int label_width;
  187. #endif
  188. {
  189.   int i;
  190.  
  191.   *string = ' ';
  192.   for( i=0; i<label_width; i++ )
  193.     string[i+1] = label[i];
  194.   for( i=label_width+1; i<string_width; i++ )
  195.     string[i] = ' ';
  196.   string[string_width] = '\0';
  197. }
  198.  
  199.  
  200. static double emax[17] = { 1.0, 10.0, 1e2, 1e3, 1e4, 1e5, 1e6, 1e7, 1e8,
  201.                1e9, 1e10, 1e11, 1e12, 1e13, 1e14, 1e15, 1e17 };
  202. static double emin[18] = {  0.0,  -1.0, -10.0,  -1e2,  -1e3,  -1e4,
  203.                -1e5,  -1e6,  -1e7,  -1e8,  -1e9, -1e10,
  204.               -1e11, -1e12, -1e13, -1e14, -1e15, -1e17 };
  205. static char *fform[16] = { "%.0f", "%.1f", "%.2f", "%.3f", "%.4f", "%.5f",
  206.                  "%.6f", "%.7f", "%.8f", "%.9f", "%.10f",
  207.                  "%.11f", "%.12f", "%.13f", "%.14f", "%.15f" };
  208. static char *eform[16] = { "%.0e", "%.1e", "%.2e", "%.3e", "%.4e", "%.5e",
  209.                  "%.6e", "%.7e", "%.8e", "%.9e", "%.10e",
  210.                  "%.11e", "%.12e", "%.13e", "%.14e", "%.15e" };
  211. /*  Subroutine:    real_string
  212.  *  Purpose:    Print the real value in best format for character space given
  213.  *  Note:    8<=width<=15 (7 makes -1e-10\0, 16 goes beyond lookup arrays)
  214.  *  Note:    width characters beyond string[width] my get messed up
  215.  *  Method:    For speed, compares and formats get values through lookups
  216.  */
  217. #ifdef ANSIC
  218. void real_string( double val, char *string, int width )
  219. #else
  220. void real_string ( val, string, width )
  221.      double val;    /* value to print */
  222.      char *string;    /* string to recieve number (see note 2 above) */
  223.      int width;        /* i: number of characters to use */
  224. #endif
  225. {
  226.   if( val >= 0.0 ) {
  227.     if( (val < 0.001) || (val >= emax[width-1]) ) {
  228.       /*  Outside range for simple %f format  */
  229.       if( (val >= 1e-9) && (val < 1e10) ) {
  230.     /*  Inside range for 1 digit exponent (move digit 2 over)  */
  231.     if( val >= 1.0 ) {
  232.       /*  Positive number with 1 digit positive exponent  */
  233.       (void)sprintf(string, eform[width-4], val);
  234.       string[width-1] = string[width+1];
  235.     } else {
  236.       /*  Positive number with 1 digit negative exponent  */
  237.       (void)sprintf(string, eform[width-5], val);
  238.       string[width-1] = string[width];
  239.     }
  240.       } else if( val >= 1.0 ) {
  241.     /*  Positive number with 2 digit positive exponent  */
  242.     (void)sprintf(string, eform[width-5], val);
  243.     string[width-2] = string[width-1];
  244.     string[width-1] = string[width];
  245.       } else
  246.     /*  Positive number with 2 digit negative exponent  */
  247.     (void)sprintf(string, eform[width-6], val);
  248.     } else
  249.       /*  Positive number in fixed point notation  */
  250.       (void)sprintf(string, fform[width], val);
  251.   } else {
  252.     if( (val > -0.001) || (val <= emin[width]) ) {
  253.       if( (val <= -1e-9) && (val > -1e10) ) {
  254.     /*  Inside range for 1 digit exponent (move digit 2 over)  */
  255.     if( val > -1.0 ) {
  256.       /*  Negative number with 1 digit negative exponent  */
  257.       (void)sprintf(string, eform[width-6], val);
  258.       string[width-1] = string[width];
  259.     } else {
  260.       /*  Negative number with 1 digit positive exponent  */
  261.       (void)sprintf(string, eform[width-5], val);
  262.       string[width-1] = string[width+1];
  263.     }
  264.       } else if( val <= -1.0 ) {
  265.     /*  Negative number with 2 digit positive exponent  */
  266.     (void)sprintf(string, eform[width-6], val);
  267.     string[width-2] = string[width-1];
  268.     string[width-1] = string[width];
  269.       } else
  270.     /*  Negative number with 2 digit negative exponent  */
  271.     (void)sprintf(string, eform[width-7], val);
  272.     } else
  273.       /*  Negative number in fixed point notation  */
  274.       (void)sprintf(string, fform[width], val);
  275.   }
  276.   string[width] = '\0';
  277. }
  278.