home *** CD-ROM | disk | FTP | other *** search
/ GRIPS 2: Government Rast…rocessing Software & Data / GRIPS_2.cdr / dos / imdisp / source / display.c < prev    next >
C/C++ Source or Header  |  1990-11-20  |  8KB  |  260 lines

  1. /*************************************************************/
  2. /*  Copyright (C) 1989, California Institute of Technology   */
  3. /*  U. S. Government Sponsorship under NASA Contract         */
  4. /*  NAS7-918 is acknowledged.                                */
  5. /*************************************************************/
  6.  
  7. /***  IMDISP module DISPLAY.C
  8.  
  9.         DISPLAY contains the routines to handle the actual display
  10.     screen.
  11.  
  12. ***/
  13.  
  14. /* * * * INCLUDE files * * * */
  15.  
  16. #include <conio.h>
  17. #include <malloc.h>
  18. #include <math.h>
  19. #include <stdio.h>
  20. #include <stdlib.h>
  21. #include "imdef.h"
  22. #include "imdisp.h"
  23. #include "dispio.h"
  24. #include "disputil.h"
  25. #include "imageio.h"
  26. #include "labutil.h"
  27. #include "refresh.h"
  28.  
  29. /* * * * External functions * * * */
  30.  
  31. /* * * * Function declarations * * * */
  32.  
  33. int DisplayImage (void);
  34.  
  35. /* * * * Global Variables * * * */
  36.  
  37.  
  38. int DisplayImage(void)
  39.  
  40. /* DisplayImage performs the display command.
  41.     First it gets the values (or defaults) of all of the possible
  42.     parameters.  Then it checks them for validity and processes
  43.     the parameters into useful form.  It then loops through the
  44.     image, reading a line, formatting it, displaying it, and stuffing
  45.     it into the refresh buffer.
  46.  
  47. */
  48. {
  49.     int     line, samp, i, j, k;
  50.     int     bitshift, NoScale, flag;
  51.     int     centsub, centerflag, flipflag;
  52.     int     up, down, left, right, upflag, downflag, leftflag, rightflag;
  53.     unsigned int   bytesneeded;
  54.     char    status[128];
  55.     unsigned char *buffer,ch;
  56.  
  57.     if (OpenFileFlag == 0) 
  58.     {
  59.        StatusLine(0,"Use the \'FILE fname\' command to select image");
  60.        return;
  61.     }
  62.  
  63.     if (IMCB[0].nl == 0) 
  64.     {
  65.        StatusLine(0,"No lines in image");
  66.        return;
  67.     }
  68.       
  69.     GetKeywordInteger (CommandString, "SL", 1, &sl, &flag);
  70.     GetKeywordInteger (CommandString, "SS", 1, &ss, &flag);
  71.     if (ss > ns)
  72.        ss = 1;
  73.  
  74.     GetKeywordInteger (CommandString, "NL", nl-sl+1, &nld, &flag);
  75.     GetKeywordInteger (CommandString, "NS", ns-ss+1, &nsd, &flag);
  76.     GetKeywordInteger (CommandString, "DSL", 1, &sldd, &flag);
  77.     GetKeywordInteger (CommandString, "DSS", 1, &ssdd, &flag);
  78.     GetKeywordInteger (CommandString, "SUB", 1, &subsample, &flag);
  79.     GetKeywordInteger (CommandString, "ZOO", 1, &zoom, &flag);
  80.     GetKeywordSubcommand (CommandString, "CEN", ¢erflag);
  81.     GetKeywordInteger (CommandString, "LEF", dispns, &left, &leftflag);
  82.     GetKeywordInteger (CommandString, "RIG", dispns, &right, &rightflag);
  83.     GetKeywordInteger (CommandString, "UP", dispnl, &up, &upflag);
  84.     GetKeywordInteger (CommandString, "DOW", dispnl, &down, &downflag);
  85.     GetKeywordSubcommand (CommandString, "FLI", &flipflag);
  86.  
  87.     if (subsample < 1)
  88.        subsample = 1;
  89.     if (zoom < 1)
  90.        zoom = 1;
  91.     if (subsample > 1)
  92.        zoom = 1;
  93.  
  94.     if ( (leftflag > -1) || (rightflag > -1) ||
  95.          (upflag > -1)   || (downflag > -1) )
  96.     {
  97.        sl = lastsl;
  98.        ss = lastss;
  99.        if (leftflag > -1)
  100.            ss -= left;
  101.        if (rightflag > -1)
  102.            ss += right;
  103.        if (upflag > -1)
  104.            sl -= up;
  105.        if (downflag > -1)
  106.            sl += down;
  107.     }
  108.  
  109.     if (sl < 1)
  110.        sl = 1;
  111.     else if (sl > nl)
  112.        sl = nl;
  113.     if (ss < 1)
  114.        ss = 1;
  115.     else if (ss > ns)
  116.        ss = ns;
  117.  
  118. /* CENTer command stuff */
  119.     if ( (centerflag >= 0) && (centerline > 0) )
  120.     {
  121.        nldd = zoom*nld / subsample;
  122.        nsdd = zoom*nsd / subsample;
  123.        if (nldd+sldd-1 > dispnl)
  124.            nldd = dispnl-sldd+1;
  125.        if (nsdd+ssdd-1 > dispns)
  126.            nsdd = dispns-ssdd+1;
  127.        nld = subsample*nldd / zoom;
  128.        nsd = subsample*nsdd / zoom;
  129.        sl = centerline - (nld / 2);
  130.        if (sl < 1)  sl = 1;
  131.        ss = centersamp - (nsd / 2);
  132.        if (ss < 1)  ss = 1;
  133.        CursorLine = zoom*(centerline - sl) / subsample + sldd;
  134.        CursorSample = zoom*(centersamp - ss) / subsample + ssdd;
  135.     }
  136.  
  137.     lastsl = sl;
  138.     lastss = ss;
  139.  
  140.     if (nld+sl-1 > nl)
  141.        nld = nl-sl+1;
  142.     if (nsd+ss-1 > ns)
  143.        nsd = ns-ss+1;
  144.     nldd = zoom*nld / subsample;
  145.     nsdd = zoom*nsd / subsample;
  146.     if (nldd+sldd-1 > dispnl)
  147.        nldd = dispnl-sldd+1;
  148.     if (nsdd+ssdd-1 > dispns)
  149.        nsdd = dispns-ssdd+1;
  150.     nld = subsample*nldd / zoom;
  151.     nsd = subsample*nsdd / zoom;
  152.     nldd = zoom*nld / subsample;
  153.     nsdd = zoom*nsd / subsample;
  154.  
  155.     if (bitsperpix <= 8)
  156.        bytesneeded = zoom*nsd;
  157.     else if (bitsperpix == 32)
  158.        bytesneeded = 4*zoom*nsd;
  159.     else
  160.        bytesneeded = 2*zoom*nsd;
  161.     while ((buffer = malloc(bytesneeded)) == NULL)
  162.        FreeRefresh("pixel buffer");
  163.  
  164.     if (bitsperpix >= 8)
  165.     {
  166.        bitshift = Round(log(((double)DNhigh-DNlow+1)/numDN) /0.693147);
  167.        NoScale = (DNlow==0) && (DNhigh==(1 << bitsperpix)-1);
  168.     }
  169.     else
  170.        bitshift =  bitsperpix - Round(log((double)numDN)/0.693147);
  171.  
  172. /* Loop through image line by line */
  173.  
  174.     if (zoom == 1)
  175.     {
  176.        line = sl;
  177.        for (i = sldd;  i < nldd+sldd;  i++)
  178.        {
  179.            if (flipflag == 1)
  180.               j = nldd+sldd - i;
  181.            else
  182.               j = i;
  183.            ReadLine (0, buffer, line, ss, nsd, status);
  184.            if (BadStatus(status))
  185.            {
  186.                free (buffer);
  187.                break;
  188.            }
  189.  
  190.            FormatLine (buffer, nsdd, nsd, bitshift, NoScale, status);
  191.            if (BadStatus(status))
  192.            {
  193.                free (buffer);
  194.                break;
  195.            }
  196.  
  197.            DisplayLine (buffer, j, ssdd, nsdd);
  198.            PutRefresh (buffer, j, ssdd, nsdd);
  199.            line += subsample;
  200.            if (kbhit())  /* abort disp if keypressed mdm 2/19/88*/
  201.            {
  202.                if ((ch = getch()) == 0) ch = 0x80 | getch();
  203.                if (ch != 17) /* don't abort if cntl q */
  204.                {
  205.                    i=nldd+sldd;
  206.                    abort_disp = 1;
  207.                }
  208.            }
  209.        }
  210.     }
  211.     else
  212.     {
  213.        i = sldd;
  214.        for (line = sl;  line < nld+sl;  line++)
  215.        {
  216.            if (flipflag == 1)
  217.               j = nld*zoom - i;
  218.            else
  219.               j = i;
  220.            ReadLine (0, buffer, line, ss, nsd, status);
  221.            if (BadStatus(status))
  222.                free (buffer);
  223.  
  224.            FormatLine (buffer, nsdd, nsd, bitshift, NoScale, status);
  225.            if (BadStatus(status))
  226.                free (buffer);
  227.  
  228.            for (k = 1;  k <= zoom;  k++)
  229.            {
  230.                DisplayLine (buffer, j, ssdd, nsdd);
  231.                PutRefresh (buffer, j, ssdd, nsdd);
  232.                i++; j++;
  233.                if (kbhit()) /* abort disp if keypressed mdm 2/19/88*/
  234.                {
  235.                    if ((ch = getch()) == 0) ch = 0x80 | getch();
  236.                    if (ch != 17) /* don't abort if cntl q */
  237.                    {
  238.                        line = nld+sl;
  239.                        k=zoom;
  240.                        abort_disp = 1;
  241.                    }
  242.                }
  243.            }
  244.        }
  245.     }
  246.     /* If SelectFlag is raised indicating the SELECT option has been
  247.        selected in the BROWSE command, then prompt user
  248.        Ron Baalke  11/04/90                             */
  249.  
  250.     if (SelectFlag)
  251.     {
  252.         StatusLine(1,"Select this image (Y=Yes N=No (default) Q=Quit) ?");
  253.         i = getch();
  254.         if ((i == 89) || (i == 121))                  /* 'Y' or 'y' */
  255.            fprintf(SelectFile,"%s\n",ImageFileName);
  256.         else if ((i == 81) || (i == 113) || (i==27))  /* 'Q', 'q' or ESC */
  257.            abort_disp = 1;
  258.     }
  259. }
  260.