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

  1. #ifndef lint
  2. static char SccsId[] = "%W%  %G%";
  3. #endif
  4.  
  5. /* Module:    readreal.c (Read Real)
  6.  * Purpose:    Scale real image data to fit in a short (int*2) buffer
  7.  * Subroutine:    scale_data_r4()            returns: void
  8.  * Subroutine:    scale_data_r8()            returns: void
  9.  * Xlib calls:    none
  10.  * Copyright:    1988 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.  * Modified:    {0} Michael VanHilst    initial version         31 October 1988
  17.  *        {n} <who> -- <does what> -- <when>
  18.  */
  19.  
  20. #include "hfiles/image.h"    /* image struct */
  21. #include "hfiles/scale.h"    /* define SCALEWIDTH, etc. */
  22.  
  23. /*
  24.  * Subroutine:    scale_data_r4
  25.  * Purpose:    scale 32 bit real data into the (short) img array
  26.  */
  27. void scale_data_r4 ( image, imgbuf, databuf, vals, verbose )
  28.      struct imageRec *image;
  29.      short *imgbuf;
  30.      float *databuf;
  31.      int vals;
  32.      int verbose;        /* whether to print explanatory messages */
  33. {
  34.   register float *fbuf, *fbufend;
  35.   float fmin, fmax;
  36.  
  37.   /* set buf start and end pointers for a pass through fbuf */
  38.   fbuf = databuf;
  39.   fbufend = fbuf + vals;
  40.   /* find the min and the max */
  41.   fmin = fmax = *fbuf;
  42.   /* skip the first val since we just used it */
  43.   while( ++fbuf < fbufend ) {
  44.     if( *fbuf < fmin )
  45.       fmin = *fbuf;
  46.     else if( *fbuf > fmax )
  47.       fmax = *fbuf;
  48.   }
  49.   /* make announcement if requested */
  50.   if( verbose ) {
  51.     (void)printf("Data min and max as read: %g, %g\n", fmin, fmax);
  52.     if( image->fimin < image->fimax ) {
  53.       (void)printf("Using given limits: %g, %g\n", image->fimin, image->fimax);
  54.     }
  55.   }
  56.   /* apply preset limits if given */
  57.   if( image->fimin < image->fimax ) {
  58.     fmin = image->fimin;
  59.     fmax = image->fimax;
  60.   }  
  61.   {
  62.     float scale, bias;
  63.     float ftemp;
  64.     register short *sbuf;
  65.  
  66.     /* set bias to offset values to zero center the range */
  67.     bias = -((fmin + fmax) / (float)2.0);
  68.     /* get the scale factor */
  69.     if( (fmax - fmin) > 0.0 ) {
  70.       scale = (float)SCALEWIDTH / (fmax - fmin);
  71.     } else {
  72.       scale = 1.0;
  73.     }
  74.     /* reset buf for another pass through fbuf */
  75.     /* while loop changed to current form from:
  76.      * buf = databuf;
  77.      * while( buf < bufend )
  78.      *   *sbuf RND((*buf++ + bias) * scale;
  79.      * because Sun compiler was inc'ing buf by 8 bytes */
  80.     fbuf = databuf;
  81.     sbuf = imgbuf;
  82.     /* use min and max to mark out limits */
  83.     fmin = (float)SCALEMIN;
  84.     fmax = (float)SCALEMAX;
  85.     /* scale the picture */
  86.     do {
  87.       ftemp = (*fbuf + bias) * scale;
  88.       if( ftemp < 0.0 ) {
  89.     ftemp -= 0.5;
  90.     if( ftemp < fmin )
  91.       *sbuf++ = fmin;
  92.     else
  93.       *sbuf++ = (short)ftemp;
  94.       } else {
  95.     ftemp += 0.5;
  96.     if( ftemp > fmax )
  97.       *sbuf++ = fmax;
  98.     else
  99.       *sbuf++ = (short)ftemp;
  100.       }
  101.     } while( ++fbuf < fbufend );
  102.     image->fiscaled = 1;
  103.     image->fibias = (double)(-bias);
  104.     image->fiscale = 1.0 / (double)scale;
  105.   }
  106. }
  107.  
  108. /*
  109.  * Subroutine:    scale_data_r8
  110.  * Purpose:    Scale 64 bit real data into the (short) img array
  111.  */
  112. void scale_data_r8 ( image, imgbuf, databuf, vals, verbose )
  113.      struct imageRec *image;
  114.      short *imgbuf;
  115.      double *databuf;
  116.      int vals;
  117.      int verbose;        /* whether to print explanatory messages */
  118. {
  119.   register double *dbuf, *dbufend;
  120.   double dmin, dmax;
  121.  
  122.   /* set buf start and end pointers for a pass through fbuf */
  123.   dbuf = databuf;
  124.   dbufend = dbuf + vals;
  125.   /* find the min and the max */
  126.   dmin = dmax = *dbuf;
  127.   /* skip the first val since we just used it */
  128.   while( ++dbuf < dbufend ) {
  129.     if( *dbuf < dmin )
  130.       dmin = *dbuf;
  131.     else if( *dbuf > dmax )
  132.       dmax = *dbuf;
  133.   }
  134.   /* make announcement if requested */
  135.   if( verbose ) {
  136.     (void)printf("Data min and max as read: %g, %g\n", dmin, dmax);
  137.     if( image->fimin < image->fimax ) {
  138.       (void)printf("Using given limits: %g, %g\n", image->fimin, image->fimax);
  139.     }
  140.   }
  141.   /* apply preset limits if given */
  142.   if( image->fimin < image->fimax ) {
  143.     dmin = image->fimin;
  144.     dmax = image->fimax;
  145.   }  
  146.   {
  147.     double scale, bias;
  148.     double dtemp;
  149.     register short *sbuf;
  150.  
  151.     /* set bias to offset values to zero center the range */
  152.     bias = -((dmin + dmax) / (double)2.0);
  153.     /* get the scale factor */
  154.     if( (dmax - dmin) > 0.0 ) {
  155.       scale = (double)SCALEWIDTH / (dmax - dmin);
  156.     } else {
  157.       scale = 1.0;
  158.     }
  159.     /* reset buf for another pass through fbuf */
  160.     dbuf = databuf;
  161.     sbuf = imgbuf;
  162.     /* use min and max to mark out limits */
  163.     dmin = (double)SCALEMIN;
  164.     dmax = (double)SCALEMAX;
  165.     /* scale the picture */
  166.     do {
  167.       dtemp = (*dbuf + bias) * scale;
  168.       if( dtemp < 0.0 ) {
  169.     dtemp -= 0.5;
  170.     if( dtemp < dmin )
  171.       *sbuf++ = dmin;
  172.     else
  173.       *sbuf++ = (short)dtemp;
  174.       } else {
  175.     dtemp += 0.5;
  176.     if( dtemp > dmax )
  177.       *sbuf++ = dmax;
  178.     else
  179.       *sbuf++ = (short)dtemp;
  180.       }
  181.     } while( ++dbuf < dbufend );
  182.     image->fiscaled = 1;
  183.     image->fibias = -bias;
  184.     image->fiscale = 1.0 / scale;
  185.   }
  186. }
  187.