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

  1. #ifndef lint
  2. static char SccsId[] = "%W%  %G%";
  3. #endif
  4.  
  5. /* Module:    imgparam.c (Image Parameters)
  6.  * Purpose:    Resolve missing image and display parameters
  7.  * Subroutine:    init_img()            returns: void
  8.  * Subroutine:    init_dispcen()            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 August 1988
  17.  *              {1} MVH BSDonly strings.h compatability           19 Feb 1990
  18.  *        {n} <who> -- <does what> -- <when>
  19.  */
  20.  
  21. #include <stdio.h>            /* stderr, NULL, etc. */
  22.  
  23. #ifndef VMS
  24. #ifdef SYSV
  25. #include <string.h>        /* strlen, strcat, strcpy, strrchr */
  26. #else
  27. #include <strings.h>        /* strlen, etc. for unenlightened BSD's */
  28. #endif
  29. #else
  30. #include <string.h>        /* strlen, strcat, strcpy, strrchr */
  31. #endif
  32.  
  33. #include "hfiles/coord.h"        /* coord structs */
  34. #include "hfiles/define.h"        /* MIN, MAX, and more */
  35. #include "hfiles/image.h"        /* image struct */
  36.  
  37. /*
  38.  * Subroutine:    init_img
  39.  * Purpose:    Resolve readable subsection selection and fill in
  40.  *        missing params
  41.  * Called by:    init_image in ImageRead.c
  42.  * Called by:    imtool_reinit in RemoteImtool.c
  43.  */
  44. void init_img ( image, coord )
  45.      struct imageRec *image;
  46.      struct coordRec *coord;
  47. {
  48.   double ioff;
  49.   int width, height;
  50.   int low_edge, wide_edge, high_edge;
  51.   void set_coordsys(), set_imgtran();
  52.  
  53.   /* set img parameters */
  54.   /* file to buffer must never use replication */
  55.   if( image->fiblock < 1 ) image->fiblock = 1;
  56.   /* file coordinates must work for either 0 based or 1 based indexing */
  57.   /* offset of 0.5 is used in 0 based indexing, 0.0 is used in 1 based */
  58.   low_edge = image->index_base;
  59.   if( low_edge == 0 ) {
  60.     ioff = 0.5;
  61.     wide_edge = image->filecols - 1;
  62.     high_edge = image->filerows - 1;
  63.   } else {
  64.     ioff = 0.0;
  65.     wide_edge = image->filecols;
  66.     high_edge = image->filerows;
  67.   }
  68.   /* clip inputs to file coordinate limits */
  69.   if( image->fiX1 < low_edge ) image->fiX1 = low_edge;
  70.   if( image->fiY1 < low_edge ) image->fiY1 = low_edge;
  71.   if( image->fiX2 > wide_edge ) image->fiX2 = wide_edge;
  72.   if( image->fiY2 > high_edge ) image->fiY2 = high_edge;
  73.   width = image->fiX2 - image->fiX1 + 1;
  74.   height = image->fiY2 - image->fiY1 + 1;
  75.   /* if image section not set or incorrectly set, default to full file */
  76.   if( (width <= 1) || (height <= 1) ) {
  77.     width = image->filecols;
  78.     height = image->filerows;
  79.     image->fiX1 = low_edge;
  80.     image->fiY1 = low_edge;
  81.     image->fiX2 = wide_edge;
  82.     image->fiY2 = high_edge;
  83.   }
  84.   /* set up the description of the file coordinate system */
  85.   set_coordsys(&coord->file, ioff, image->filecols, image->filerows,
  86.            image->fiX1, image->fiY1, image->fiX2, image->fiY2);
  87.   image->ficenX = coord->file.cenX;
  88.   image->ficenY = coord->file.cenY;
  89.   /* calculate size of img (check for 1 since it is the most common case) */
  90.   if( image->fiblock != 1 ) {
  91.     width /= image->fiblock;
  92.     height /= image->fiblock;
  93.   }
  94.   /* account for rotation of the axes in going from file to display */
  95.   /* set up the description of the img coordinate system */
  96.   if( image->rotate_code & 1 ) {
  97.     set_coordsys(&coord->img, 0.5, height, width,
  98.          0, 0, height - 1, width - 1);
  99.   } else {
  100.     set_coordsys(&coord->img, 0.5, width, height,
  101.          0, 0, width - 1, height - 1);
  102.   }
  103.   /* create the transformation matrices between img and file */
  104.   set_imgtran(image, &coord->img, &coord->file,
  105.           &coord->imgtofile, &coord->filetoimg);
  106. }
  107.  
  108. /*
  109.  * Subroutine:    init_dispcen
  110.  * Purpose:    Adjust chosen or default display parameters
  111.  * Called by:    init_image in ImageRead.c
  112.  * Called by:    imtool_reinit in RemoteImtool.c
  113.  */
  114. void init_dispcen ( image, coord )
  115.      struct imageRec *image;
  116.      struct coordRec *coord;
  117. {
  118.   int spec;
  119.   void d_transform();
  120.  
  121.   /* if disp size is not given choose one within limits */
  122.   if( image->dispcols <= 0 )
  123.     image->dispcols = MAX( MIN( 512, coord->img.width ), 200 );
  124.   if( image->disprows <= 0 )
  125.     image->disprows = MAX( MIN( 512, coord->img.height ), 200 );
  126.   /* set center of display, or use that which was given */
  127.   spec = 0;
  128.   if( image->fdcenX > 0 ) {
  129.     if( image->fdcenX < image->fiX1 ) image->fdcenX = image->fiX1;
  130.     if( image->fdcenX > image->fiX2 ) image->fdcenX = image->fiX2;
  131.     spec = 1;
  132.   } else
  133.     image->fdcenX = image->ficenX;
  134.   if( image->fdcenY > 0 ) {
  135.     if( image->fdcenY < image->fiY1 ) image->fdcenY = image->fiY1;
  136.     if( image->fdcenY > image->fiY2 ) image->fdcenY = image->fiY2;
  137.     spec = 1;
  138.   } else
  139.     image->fdcenY = image->ficenY;
  140.   /* get display center in img coords */
  141.   if( spec ) {
  142.     /* translate from file to img coords */
  143.     d_transform(&coord->filetoimg, image->fdcenX, image->fdcenY,
  144.         &(coord->tid.cenX), &(coord->tid.cenY));
  145.   } else {
  146.     /* use default (center of img) */
  147.     coord->tid.cenX = coord->img.cenX;
  148.     coord->tid.cenY = coord->img.cenY;
  149.   }
  150.   /* calculate disptoimg zoom factor */
  151.   /* allow only positive blocking when reading file */
  152.   if( image->fiblock < 0 )
  153.     image->fiblock = -image->fiblock;
  154.   /* if display blocking not set, make it that of img */
  155.   if( image->fdblock == 0 ) {
  156.     image->fdblock = image->fiblock;
  157.     coord->tid.zoom = 1.0;
  158.     coord->tid.block = 1;
  159.   } else {
  160.     double dzoom;
  161.     if( image->fdblock > 0 )
  162.       dzoom = 1.0 / (double)image->fdblock;
  163.     else
  164.       dzoom = (double)-(image->fdblock);
  165.     if( image->fiblock != 1 )
  166.       dzoom *= image->fiblock;
  167.     /* set id block and zoom (make sure zoom is integerized) */
  168.     if( dzoom > 0.75 ) {
  169.       coord->tid.block = -(int)(dzoom + 0.5);
  170.       coord->tid.zoom = (double)(-coord->tid.block);
  171.     } else {
  172.       coord->tid.block = (int)((1.0 / dzoom) + 0.5);
  173.       coord->tid.zoom = 1.0 / (double)coord->tid.block;
  174.     }
  175.   }
  176. }
  177.