home *** CD-ROM | disk | FTP | other *** search
/ GRIPS 2: Government Rast…rocessing Software & Data / GRIPS_2.cdr / dos / imdisp / source / disputil.c < prev    next >
Encoding:
C/C++ Source or Header  |  1991-01-04  |  34.6 KB  |  983 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. /***  Module: DISPUTIL
  8.       Device Independent General Purpose Graphics Routines
  9.  
  10.     Includes line drawing, text display, and cursor routines.
  11. ***/
  12.  
  13. /* 10/6 Changed cursor routines for cursor control without numlock - mdm*/
  14.  
  15. /* * * * INCLUDE files * * * */
  16.  
  17. #include  <conio.h>
  18. #include  <math.h>
  19. #include  <malloc.h>
  20. #include  <stdio.h>
  21. #include  <string.h>
  22. #include  <stdlib.h>
  23. #include  "imdef.h"
  24. #include  "imdisp.h"
  25. #include  "dispio.h"
  26. #include  "refresh.h"
  27.  
  28. /* * * * External functions * * * */
  29.  
  30. extern int putmem(int, int, int);
  31.  
  32. /* * * * Function declarations * * * */
  33.  
  34. int  DrawLine ( int, int, int, int, int);
  35. int  Font (int);
  36. int  DrawText ( char *, int, int, int, int, int);
  37. int  EraseText (int, int, int, int, int);
  38. int  LengthText (char *, int, int *);
  39. int  DrawBox (int, int, int, int, int );
  40. int  TypeText (char *);
  41. int  AcceptText (char *);
  42. int  WriteText (char *);
  43. int  StatusLine (int, char *);
  44. int  RemoveCursor (void);
  45. int  PlaceCursor (int, int, int);
  46. int  MoveCursorInput (int *, int *, unsigned char *);
  47. int  MoveCursor (int *, int *);
  48. int  InitDisplay (void);
  49. void FrameBox (int, int, int, int, int, int);
  50.  
  51. /* * * * Global Variables * * * */
  52.  
  53. unsigned char  CursorShape[CURSORSIZE][CURSORSIZE]
  54.                             = { {0,0,0,0,1,0,0,0,0},
  55.                                 {0,0,0,0,1,0,0,0,0},
  56.                                 {0,0,0,0,1,0,0,0,0},
  57.                                 {0,0,0,0,1,0,0,0,0},
  58.                                 {1,1,1,1,1,1,1,1,1},
  59.                                 {0,0,0,0,1,0,0,0,0},
  60.                                 {0,0,0,0,1,0,0,0,0},
  61.                                 {0,0,0,0,1,0,0,0,0},
  62.                                 {0,0,0,0,1,0,0,0,0} };
  63.  
  64. int            CursorOn;                   /* cursor flag */
  65. int            CursorLine, CursorSample;   /* cursor location */
  66. int            CursorInc;                  /* size of cursor steps */
  67. unsigned char  CursorPatch[CURSORSIZE][CURSORSIZE];
  68.                  /* contains display pixels displaced by cursor */
  69.  
  70.  
  71. int    TextLine, TextSample;  /* line and sample where next text will go */
  72. int    TextHeight;            /* Default character size */
  73. int    BigChars, SmallChars;  /* multiples of character size */
  74. int    thefont;               /* The number of the font */
  75.  
  76.  
  77. struct FontDataType
  78.     {
  79.        unsigned char   x, y, pen;
  80.     }  FontData[MAXFONTSTROKES];
  81.      /* Contains the line strokes that define the characters for the
  82.         current font.  Stored in byte format.  The x and y coordinates
  83.         convert back to character coordinates with xc = (x-50)/100.
  84.         pen=2 is pen down, pen=3 is pen up. */
  85.  
  86. struct FontPtrType
  87.     {
  88.        int            index;
  89.        unsigned char  strokes, width;
  90.     }  FontPointer[128];
  91.      /* Contains the info on each ascii character in the current font.
  92.         The index points to where the character starts in the FontData
  93.         array.  The strokes is the number of line strokes for this
  94.         character.  The width is the width of the character; stored
  95.         the same as the x and y above.  */
  96.  
  97.  
  98.  
  99.  
  100. int DrawLine (int x1, int y1, int x2, int y2, int color)
  101.  
  102. /***  DrawLine draws a line of pixels from the first point to the
  103.     second point with the desired DN value.  Clipping is not performed.
  104.  
  105.        Parameter  Type   Description
  106.          x1       int    the line coordinate of the first point
  107.          y1       int    the sample coordinate of the first point
  108.          x2       int    the line coordinate of the second point
  109.          y2       int    the sample coordinate of the second point
  110.          color    int    the DN value of the drawn pixels
  111. ***/
  112.  
  113. {
  114.     int  x, y, delx, dely, absdelx, absdely, rem, address;
  115.     unsigned char buffer[1];
  116.     buffer[0]= color;
  117.     delx = x2 - x1;
  118.     dely = y2 - y1;
  119.     absdelx = abs(delx);
  120.     absdely = abs(dely);
  121.     rem = 0;
  122.  
  123.     if (absdely < absdelx)
  124.     {
  125.        y = y1;
  126.        if (delx > 0)
  127.        {
  128.            if (dely > 0)
  129.            {
  130.                for (x = x1; x <= x2; x++)
  131.                {
  132.                    rem += absdely;
  133.                    if (rem > absdelx)
  134.                    {
  135.                        rem -= absdelx;
  136.                        y++;
  137.                    }
  138.                    if (IntoMem)
  139.                        PutRefresh(buffer,x,y,1);
  140.                    else
  141.                        WritePixel (x,y, color);
  142.                }
  143.            }
  144.            else
  145.            {
  146.                for (x = x1;  x <= x2;  x++)
  147.                {
  148.                    rem += absdely;
  149.                    if (rem > absdelx)
  150.                    {
  151.                        rem -= absdelx;
  152.                        y--;
  153.                    }
  154.                    if (IntoMem)
  155.                        PutRefresh(buffer,x,y,1);
  156.                    else
  157.                        WritePixel (x,y, color);
  158.                }
  159.            }
  160.        }
  161.  
  162.        else
  163.        {
  164.            if (dely > 0)
  165.            {
  166.                for (x = x1;  x >= x2;  x--)
  167.                {
  168.                    rem += absdely;
  169.                    if (rem > absdelx)
  170.                    {
  171.                        rem -= absdelx;
  172.                        y++;
  173.                    }
  174.                    if (IntoMem)
  175.                        PutRefresh(buffer,x,y,1);
  176.                    else
  177.                        WritePixel (x,y, color);
  178.                }
  179.            }
  180.            else
  181.            {
  182.                for (x = x1;  x >= x2;  x--)
  183.                {
  184.                    rem += absdely;
  185.                    if (rem > absdelx)
  186.                    {
  187.                        rem -= absdelx;
  188.                        y--;
  189.                    }
  190.                    if (IntoMem)
  191.                        PutRefresh(buffer,x,y,1);
  192.                    else
  193.                        WritePixel (x,y, color);
  194.                }
  195.            }
  196.        }
  197.     }
  198.  
  199.     else
  200.     {
  201.        x = x1;
  202.        if (dely > 0)
  203.        {
  204.            if (delx > 0)
  205.            {
  206.                for (y = y1;  y <= y2;  y++)
  207.                {
  208.                    rem += absdelx;
  209.                    if (rem > absdely)
  210.                    {
  211.                        rem -= absdely;
  212.                        x++;
  213.                    }
  214.                    if (IntoMem)
  215.                        PutRefresh(buffer,x,y,1);
  216.                    else
  217.                        WritePixel (x,y, color);
  218.                }
  219.            }
  220.            else
  221.            {
  222.                for (y = y1;  y <= y2;  y++)
  223.                {
  224.                    rem += absdelx;
  225.                    if (rem > absdely)
  226.                    {
  227.                        rem -= absdely;
  228.                        x--;
  229.                    }
  230.                    if (IntoMem)
  231.                        PutRefresh(buffer,x,y,1);
  232.                    else
  233.                        WritePixel (x,y, color);
  234.                }
  235.            }
  236.        }
  237.  
  238.        else
  239.        {
  240.            if (delx > 0)
  241.            {
  242.                for (y = y1;  y >= y2;  y--)
  243.                {
  244.                    rem += absdelx;
  245.                    if (rem > absdely)
  246.                    {
  247.                        rem -= absdely;
  248.                        x++;
  249.                    }
  250.                    if (IntoMem)
  251.                        PutRefresh(buffer,x,y,1);
  252.                    else
  253.                        WritePixel (x,y, color);
  254.                }
  255.            }
  256.            else
  257.            {
  258.                for (y = y1;  y >= y2; y--)
  259.                {
  260.                    rem += absdelx;
  261.                    if (rem > absdely)
  262.                    {
  263.                        rem -= absdely;
  264.                        x--;
  265.                    }
  266.                    if (IntoMem)
  267.                        PutRefresh(buffer,x,y,1);
  268.                    else
  269.                        WritePixel (x,y, color);
  270.                }
  271.            }
  272.        }
  273.     }
  274. }
  275.  
  276. /*  The following is the internal default font.
  277.     It is in the exact same format as the font files.  */
  278.  
  279. unsigned char  DefaultFont[2560] = {
  280.  
  281. 114,0,7,150,3,100,100,2,100,133,2,67,133,2,67,67,2,133,67,2,133,133,2,100,133,
  282. 1,11,150,3,100,100,2,100,133,2,83,133,2,67,117,2,67,83,2,83,67,2,117,67,2,133,
  283. 83,2,133,117,2,117,133,2,100,133,2,5,150,3,100,100,2,100,133,2,67,67,2,133,67,
  284. 2,100,133,3,4,150,3,100,133,2,100,67,3,67,100,2,133,100,4,4,150,3,67,67,2,133,
  285. 133,3,67,133,2,133,67,5,6,150,3,100,100,2,100,133,2,67,100,2,100,67,2,133,100,
  286. 2,100,133,6,6,150,3,100,100,2,67,100,2,100,133,2,133,100,2,100,100,2,100,67,7,
  287. 4,150,3,67,67,2,133,133,2,67,133,2,133,67,8,6,150,3,67,133,2,133,133,2,67,67,2,
  288. 133,67,3,83,100,2,117,100,9,5,150,3,100,100,2,67,133,3,133,133,2,100,100,2,100,
  289. 67,10,13,150,3,100,100,2,133,133,3,117,117,2,83,117,2,67,133,3,83,117,2,83,83,
  290. 2,67,67,3,83,83,2,117,83,2,133,67,3,117,83,2,117,117,11,8,150,3,67,67,2,133,
  291. 133,3,67,133,2,133,67,3,100,67,2,100,133,3,67,100,2,133,100,12,6,150,3,100,100,
  292. 2,67,133,2,133,133,2,67,67,2,133,67,2,100,100,13,2,150,3,100,67,2,100,133,14,2,
  293. 150,3,67,100,2,133,100,15,6,150,3,67,100,2,133,100,3,67,133,2,133,133,3,67,67,
  294. 2,133,67,16,6,150,3,67,117,2,133,117,3,67,83,2,133,83,3,83,67,2,117,133,17,6,
  295. 150,3,100,100,2,100,133,3,83,117,2,117,117,3,83,83,2,117,83,32,0,150,33,9,150,
  296. 3,83,50,2,100,50,2,100,67,2,83,67,2,83,50,3,100,83,2,83,150,2,100,150,2,100,83,
  297. 34,4,150,3,83,117,2,83,150,3,117,150,2,117,117,35,8,150,3,83,50,2,83,150,3,117,
  298. 150,2,117,50,3,67,117,2,133,117,3,133,83,2,67,83,36,10,150,3,67,67,2,117,67,2,
  299. 133,83,2,117,100,2,83,100,2,67,117,2,83,133,2,133,133,3,100,150,2,100,50,37,12,
  300. 150,3,67,67,2,133,133,3,83,133,2,67,133,2,67,150,2,83,150,2,83,133,3,133,50,2,
  301. 117,50,2,117,67,2,133,67,2,133,50,38,11,150,3,133,83,2,100,50,2,83,50,2,67,67,
  302. 2,67,83,2,117,117,2,117,133,2,100,150,2,83,133,2,83,100,2,133,50,39,3,150,3,83,
  303. 117,2,100,133,2,100,150,40,4,150,3,117,150,2,83,117,2,83,83,2,117,50,41,4,150,
  304. 3,83,150,2,117,117,2,117,83,2,83,50,42,6,150,3,100,67,2,100,133,3,133,117,2,
  305. 67,83,3,67,117,2,133,83,43,4,150,3,100,67,2,100,133,3,67,100,2,133,100,44,3,
  306. 150,3,67,50,2,83,67,2,83,83,45,2,150,3,67,100,2,133,100,46,2,150,3,83,50,2,83,
  307. 50,47,2,150,3,67,50,2,133,150,48,10,150,3,67,67,2,67,133,2,83,150,2,117,150,2,
  308. 133,133,2,133,67,2,117,50,2,83,50,2,67,67,2,133,133,49,5,150,3,83,50,2,117,50,
  309. 3,100,50,2,100,150,2,83,133,50,8,150,3,67,133,2,83,150,2,117,150,2,133,133,2,
  310. 133,117,2,67,67,2,67,50,2,133,50,51,10,150,3,67,150,2,133,150,2,133,133,2,100,
  311. 100,2,117,100,2,133,83,2,133,67,2,117,50,2,83,50,2,67,67,52,5,150,3,117,50,2,
  312. 117,150,2,67,100,2,67,83,2,133,83,53,9,150,3,67,67,2,83,50,2,117,50,2,133,67,2,
  313. 133,100,2,117,117,2,67,117,2,67,150,2,133,150,54,10,150,3,67,100,2,117,100,2,
  314. 133,83,2,133,67,2,117,50,2,83,50,2,67,67,2,67,117,2,100,150,2,133,150,55,5,150,
  315. 3,67,150,2,133,150,2,133,133,2,83,83,2,83,50,56,17,150,3,83,50,2,117,50,2,133,
  316. 67,2,133,83,2,117,100,2,83,100,2,67,117,2,67,133,2,83,150,2,117,150,2,133,133,
  317. 2,133,117,2,117,100,3,83,100,2,67,83,2,67,67,2,83,50,57,10,150,3,67,50,2,100,
  318. 50,2,133,83,2,133,133,2,117,150,2,83,150,2,67,133,2,67,117,2,83,100,2,133,100,
  319. 58,4,150,3,83,50,2,83,50,3,83,83,2,83,83,59,5,150,3,83,50,2,100,67,2,100,83,3,
  320. 100,117,2,100,117,60,3,150,3,117,50,2,67,100,2,117,150,61,4,150,3,67,83,2,133,
  321. 83,3,133,117,2,67,117,62,3,150,3,83,50,2,133,100,2,83,150,63,8,150,3,100,50,2,
  322. 100,50,3,100,83,2,133,117,2,133,133,2,117,150,2,83,150,2,67,133,64,11,150,3,
  323. 117,50,2,83,50,2,67,67,2,67,133,2,83,150,2,117,150,2,133,133,2,133,83,2,100,83,
  324. 2,100,100,2,133,133,65,7,150,3,67,50,2,67,117,2,100,150,2,133,117,2,133,50,3,
  325. 67,83,2,133,83,66,12,150,3,67,50,2,67,150,2,117,150,2,133,133,2,133,117,2,117,
  326. 100,2,67,100,3,117,100,2,133,83,2,133,67,2,117,50,2,67,50,67,8,150,3,133,133,2,
  327. 117,150,2,83,150,2,67,133,2,67,67,2,83,50,2,117,50,2,133,67,68,8,150,3,67,50,2,
  328. 117,50,2,133,67,2,133,133,2,117,150,2,67,150,3,83,150,2,83,50,69,7,150,3,67,50,
  329. 2,67,150,2,133,150,3,117,100,2,67,100,3,67,50,2,133,50,70,5,150,3,67,50,2,67,
  330. 150,2,133,150,3,100,100,2,67,100,71,9,150,3,133,133,2,117,150,2,83,150,2,67,
  331. 133,2,67,67,2,83,50,2,133,50,2,133,83,2,117,83,72,6,150,3,67,50,2,67,150,3,67,
  332. 100,2,133,100,3,133,150,2,133,50,73,6,150,3,83,50,2,117,50,3,100,50,2,100,150,
  333. 3,83,150,2,117,150,74,5,150,3,67,67,2,83,50,2,117,50,2,133,67,2,133,150,75,5,
  334. 150,3,67,50,2,67,150,3,133,150,2,67,100,2,133,50,76,4,150,3,67,50,2,67,150,3,
  335. 67,50,2,133,50,77,5,150,3,67,50,2,67,150,2,100,100,2,133,150,2,133,50,78,6,150,
  336. 3,67,50,2,67,150,3,67,133,2,133,67,3,133,150,2,133,50,79,9,150,3,67,67,2,83,50,
  337. 2,117,50,2,133,67,2,133,133,2,117,150,2,83,150,2,67,133,2,67,67,80,7,150,3,67,
  338. 50,2,67,150,2,117,150,2,133,133,2,133,117,2,117,100,2,67,100,81,11,150,3,67,67,
  339. 2,83,50,2,117,50,2,133,67,2,133,133,2,117,150,2,83,150,2,67,133,2,67,67,3,100,
  340. 83,2,133,50,82,9,150,3,67,50,2,67,150,2,117,150,2,133,133,2,133,117,2,117,100,
  341. 2,67,100,3,100,100,2,133,50,83,12,150,3,67,67,2,83,50,2,117,50,2,133,67,2,133,
  342. 83,2,117,100,2,83,100,2,67,117,2,67,133,2,83,150,2,117,150,2,133,133,84,4,150,
  343. 3,67,150,2,133,150,3,100,150,2,100,50,85,6,150,3,67,150,2,67,67,2,83,50,2,117,
  344. 50,2,133,67,2,133,150,86,3,150,3,67,150,2,100,50,2,133,150,87,5,150,3,67,150,2,
  345. 83,50,2,100,100,2,117,50,2,133,150,88,8,150,3,67,50,2,67,67,2,133,133,2,133,
  346. 150,3,67,150,2,67,133,2,133,67,2,133,50,89,7,150,3,67,150,2,67,133,2,100,100,2,
  347. 100,50,3,133,150,2,133,133,2,100,100,90,6,150,3,67,150,2,133,150,2,133,133,2,
  348. 67,67,2,67,50,2,133,50,91,4,150,3,100,150,2,67,150,2,67,50,2,100,50,92,2,150,3,
  349. 133,50,2,67,150,93,4,150,3,67,50,2,100,50,2,100,150,2,67,150,94,5,150,3,67,100,
  350. 2,100,133,2,133,100,3,100,133,2,100,67,95,5,150,3,100,67,2,67,100,2,100,133,3,
  351. 67,100,2,133,100,96,2,150,3,67,150,2,117,100,97,10,150,3,67,67,2,67,100,2,83,
  352. 117,2,100,117,2,133,83,2,100,50,2,83,50,2,67,67,3,133,117,2,133,50,98,10,150,3,
  353. 67,50,2,67,150,3,67,83,2,100,117,2,117,117,2,133,100,2,133,67,2,117,50,2,100,
  354. 50,2,67,83,99,6,150,3,133,117,2,83,117,2,67,100,2,67,67,2,83,50,2,133,50,100,
  355. 10,150,3,133,150,2,133,50,3,133,83,2,100,117,2,83,117,2,67,100,2,67,67,2,83,50,
  356. 2,100,50,2,133,83,101,9,150,3,67,83,2,133,83,2,133,100,2,117,117,2,83,117,2,
  357. 67,100,2,67,67,2,83,50,2,117,50,102,5,150,3,100,50,2,100,133,2,117,150,3,83,
  358. 100,2,117,100,103,12,150,3,83,17,2,117,17,2,133,33,2,133,117,3,133,83,2,100,
  359. 117,2,83,117,2,67,100,2,67,67,2,83,50,2,100,50,2,133,83,104,6,150,3,67,50,2,67,
  360. 150,3,67,117,2,117,117,2,133,100,2,133,50,105,7,150,3,83,50,2,117,50,3,100,50,
  361. 2,100,100,2,83,100,3,100,133,2,100,133,106,6,150,3,83,50,2,100,50,2,117,67,2,
  362. 117,117,3,117,150,2,117,150,107,6,150,3,67,50,2,67,150,3,133,133,2,67,67,3,100,
  363. 100,2,133,50,108,5,150,3,83,50,2,117,50,3,100,50,2,100,150,2,83,150,109,10,150,
  364. 3,67,50,2,67,117,3,67,100,2,83,117,2,100,100,2,100,50,3,100,100,2,117,117,2,
  365. 133,100,2,133,50,110,7,150,3,67,50,2,67,117,3,67,100,2,83,117,2,117,117,2,133,
  366. 100,2,133,50,111,9,150,3,67,67,2,67,100,2,83,117,2,117,117,2,133,100,2,133,67,
  367. 2,117,50,2,83,50,2,67,67,112,10,150,3,67,17,2,67,117,3,67,83,2,100,117,2,117,
  368. 117,2,133,100,2,133,67,2,117,50,2,100,50,2,67,83,113,10,150,3,133,17,2,133,117,
  369. 3,133,83,2,100,117,2,83,117,2,67,100,2,67,67,2,83,50,2,100,50,2,133,83,114,5,
  370. 150,3,67,50,2,67,117,3,67,83,2,100,117,2,133,117,115,8,150,3,67,50,2,117,50,2,
  371. 133,67,2,117,83,2,83,83,2,67,100,2,83,117,2,133,117,116,5,150,3,83,117,2,117,
  372. 117,3,100,150,2,100,67,2,117,50,117,7,150,3,67,117,2,67,67,2,83,50,2,117,50,2,
  373. 133,67,3,133,117,2,133,50,118,3,150,3,67,117,2,100,50,2,133,117,119,5,150,3,67,
  374. 117,2,83,50,2,100,83,2,117,50,2,133,117,120,4,150,3,67,50,2,133,117,3,67,117,2,
  375. 133,50,121,8,150,3,83,17,2,117,17,2,133,33,2,133,117,3,67,117,2,67,67,2,83,50,
  376. 2,133,50,122,4,150,3,67,117,2,133,117,2,67,50,2,133,50,123,7,150,3,100,50,2,83,
  377. 67,2,83,83,2,67,100,2,83,117,2,83,133,2,100,150,124,4,150,3,100,50,2,100,83,3,
  378. 100,117,2,100,150,125,7,150,3,83,50,2,100,67,2,100,83,2,117,100,2,100,117,2,
  379. 100,133,2,83,150,126,4,150,3,67,83,2,83,100,2,117,100,2,133,117,127,8,150,3,67,
  380. 50,2,67,150,2,133,150,2,133,50,2,67,50,2,133,150,3,67,150,2,133,50,0,0,0,0,0,
  381. 0  };
  382.  
  383. int Font (int FontType)
  384.  
  385. /***  Font reads in the font file for type FontType and fills the
  386.     font arrays appropriately.  If FontType = 0 the internal font file
  387.     is used instead.  FontType = 5 reads in font file 005.FNT, etc.
  388.     The global variable thefont is set to the font number.
  389.  
  390.     The format of a font file is a sequence of bytes.  The first byte
  391.     is the number of characters defined in the font.  For each
  392.     character there is the following:  the ascii code, the number of
  393.     line strokes, the character width, and then the list of line strokes.
  394.     Each line stroke consists of three bytes: the pen motion, and the
  395.     x and y coordinate.  The width, x, and y values are converted
  396.     back into character coordinates by subtracting 50 and dividing
  397.     by 100.  The pen is 2 for pen down, and 3 for pen up.
  398.  
  399. ***/
  400.  
  401. {
  402.     char   FontName[16];
  403.     FILE   *FontFile;
  404.     int    i, j, ptr, bufptr, NumChar, ch;
  405.     unsigned char  NumStrokes, Ascii;
  406.     unsigned char  *TempBuf;
  407.  
  408.     if (thefont == FontType)
  409.        return;
  410.     if (FontType == 0)
  411.        TempBuf = DefaultFont;
  412.     else
  413.     {
  414.        sprintf (FontName, "%d", 1000+FontType);
  415.        strcat (FontName, ".FNT");
  416.        if ((FontFile = fopen (&FontName[1], "rb")) == NULL)
  417.            return;
  418.        while ((TempBuf = malloc(9000)) == NULL)
  419.            FreeRefresh("font");
  420.        i = 0;
  421.        while ( ((ch = getc(FontFile)) != EOF) && (i < 9000) )
  422.            TempBuf[i++] = ch;
  423.        fclose (FontFile);
  424.     }
  425.  
  426.     thefont = FontType;
  427.  
  428.     for (Ascii = 0;  Ascii <= 127;  Ascii++)
  429.     {
  430.         FontPointer[Ascii].width = 50;
  431.         FontPointer[Ascii].strokes = 0;
  432.     }
  433.  
  434.     bufptr = 0;
  435.     ptr = 1;
  436.     NumChar = TempBuf[bufptr++];
  437.     for (i = 1; i <= NumChar;  i++)
  438.     {
  439.         Ascii = TempBuf[bufptr++];
  440.         NumStrokes = TempBuf[bufptr++];
  441.         FontPointer[Ascii].index = ptr;
  442.         FontPointer[Ascii].strokes = NumStrokes;
  443.         FontPointer[Ascii].width = TempBuf[bufptr++];
  444.         for (j = 1;  j <= NumStrokes;  j++)
  445.         {
  446.             FontData[ptr].pen = TempBuf[bufptr++];
  447.             FontData[ptr].x = TempBuf[bufptr++];
  448.             FontData[ptr].y = TempBuf[bufptr++];
  449.             ptr++;
  450.         }
  451.     }
  452.     if (FontType != 0)
  453.        free (TempBuf);
  454. }
  455.  
  456.  
  457.  
  458. int DrawText (char * text, int line, int sample, int height, int angle, int DNvalue)
  459.  
  460. /***  DrawText draws a text string on the display using the
  461.     current font.
  462.  
  463.     Parameter  Type     Description
  464.      text     char ptr  text string to display
  465.      line     int       line coordinate of lower left of first char
  466.      sample   int       sample coordinate of lower left of first char
  467.      height   int       height of characters in pixels
  468.      angle    int       angle in degrees ccw from positive sample direction
  469.      DNvalue  int       the DN value to draw the pixels in with
  470. ***/
  471.  
  472. {
  473.     int    i, j, k, ascii, numstrokes;
  474.     int    linep, sampp, secth, cscth, dist;
  475.     int    chl, chs, pixl, pixs, oldl, olds;
  476.  
  477.  
  478.     linep = line;   sampp = sample;
  479.     secth = 100.0/(cos(0.0174533*angle)+0.01) + 0.5;
  480.     cscth = 100.0/(sin(0.0174533*angle)+0.01) + 0.5;
  481.  
  482.     for (i = 0;  i < strlen(text);  i++)
  483.     {
  484.         ascii = text[i];
  485.         j = FontPointer[ascii].index;
  486.         numstrokes = FontPointer[ascii].strokes;
  487.         for (k = 1;  k <= numstrokes;  k++)
  488.         {
  489.            chs  =  height*(FontData[j].x-50);
  490.            chl  = -height*(FontData[j].y-50);
  491.            pixs =  (chs / secth) + (chl / cscth) + sampp;
  492.            pixl = -(chs / cscth) + (chl / secth) + linep;
  493.            if (FontData[j].pen == 2)
  494.            {
  495.                if (ascii==95)  /* patch to fix underscore font char */
  496.                {
  497.                    if (k==2)
  498.                        DrawLine (oldl, olds-5, oldl, olds+5, DNvalue);
  499.                }
  500.                else
  501.                     DrawLine (oldl, olds, pixl, pixs, DNvalue);
  502.                     oldl = pixl;    olds = pixs;
  503.             }
  504.             else
  505.             {
  506.                 oldl = pixl;    olds = pixs;
  507.             }
  508.             j++;
  509.         }
  510.         dist = height*(FontPointer[ascii].width-50);
  511.         linep -= dist/cscth;
  512.         sampp += dist/secth;
  513.         if (sampp >= dispns) break;
  514.     }
  515. }
  516.  
  517. int EraseText (int line, int samp, int height, int len, int color)
  518.  
  519. /*** EraseText erases a string of text from the screen by writing over
  520.      it using blocks of the background color.  Calls DrawBox to do the
  521.      actual erasing.
  522.  
  523.     Parameter   Type     Description
  524.      line       int      starting line of text string
  525.      samp       int      starting sample of text string
  526.     height      int      size of text characters
  527.       len       int      length of text string
  528.      color      int      background color to use
  529. ***/
  530.  
  531. {
  532.     int    e_line, e_samp, e_height, e_len;
  533.  
  534.     e_line   = line - height - 1;
  535.     e_samp   = samp;
  536.     e_height = height + 7;
  537.     e_len    = len;
  538.     if (dispns < e_len+e_samp)
  539.        e_len = dispns-e_samp-1;
  540.     DrawBox( e_line, e_samp, e_height, e_len, color);
  541. }
  542.  
  543. int LengthText (char * text, int height, int * p_TextLength)
  544.  
  545. /*** LengthText finds the length in pixels of a given text string.
  546.  
  547.     Parameter       Type     Description
  548.      text          char ptr   text string to display
  549.      height         int       height of characters in pixels
  550.      p_TextLength  int ptr    where to return length of string
  551.  
  552. ***/
  553.  
  554. {
  555.     int   i, ascii;
  556.  
  557.     if (thefont == -1)
  558.         Font (0);
  559.  
  560.     *p_TextLength = 0;
  561.     for (i = 0;  i < strlen(text);  i++)
  562.     {
  563.         ascii = text[i];
  564.         *p_TextLength += (height*(FontPointer[ascii].width-50)) / 100;
  565.     }
  566. }
  567.  
  568.  
  569.  
  570. int DrawBox (int line, int samp, int lsize, int ssize, int color)
  571.  
  572. /*** DrawBox draws a solid rectangle on the screen.
  573.    Used for erasing parts of the screen.
  574.  
  575.     Parameter   Type     Description
  576.      line       int      starting line coordinate of rectangle
  577.      samp       int      starting sample coordinate of rectangle
  578.      lsize      int      number of lines in rectangle
  579.      ssize      int      number of samples in rectangle
  580.      color      int      the DN value to draw the box
  581. ***/
  582.  
  583. {
  584.     int    l, s;
  585.     unsigned char  linebuf[MAXDISPNS];
  586.  
  587.     for (s = 0;  s < ssize;  s++)
  588.         linebuf[s] = color;
  589.     for (l = line;  l < line+lsize;  l++)
  590.     {
  591.        if (IntoMem)
  592.            PutRefresh(linebuf,l,samp,ssize);
  593.        else
  594.            DisplayLine (linebuf, l, samp, ssize);
  595.      }
  596. }
  597.  
  598.  
  599.  
  600. int TypeText (char * TypeString)
  601.  
  602. /***  TypeText displays a line of text on the screen at the current
  603.     text location with a height of 15 pixels.  The area is first erased.
  604.     The text position is advanced to the next position after the last
  605.     character.
  606.  
  607.      Parameter       Type     Description
  608.      TypeString    char ptr   text string to display
  609. ***/
  610.  
  611. {
  612.     int   TextLength, n_chars;
  613.  
  614.     LengthText (TypeString, TextHeight, &TextLength);
  615.     EraseText (TextLine, TextSample, TextHeight, TextLength, 0);
  616.  
  617.     DrawText (TypeString, TextLine, TextSample, TextHeight, 0, numDN-1);
  618.     TextSample += TextLength;
  619. }
  620.  
  621. int WriteText (char * text)
  622.  
  623. /***  WriteText writes out a line of text to the user.  If there
  624.     is only one screen then the text is written on the image display.
  625.     Otherwise it is written to the terminal.
  626.      Parameter     Type     Description
  627.      text        char ptr   text string to display
  628. ***/
  629.  
  630. {
  631.     if (OneScreen)
  632.     {
  633.         if (TextLine >= dispnl)
  634.         {
  635.             ClearDisplay (0);
  636.             TextLine = TextHeight+2; TextSample = 1;
  637.         }
  638.         TypeText (text);
  639.         TextLine += TextHeight+5;
  640.         TextSample = 1;
  641.     }
  642.     else
  643.         printf ("%s", text);
  644. }
  645.  
  646.  
  647. int AcceptText (char * AcceptString)
  648.  
  649. /***  AcceptString gets a string from the keyboard.  The characters
  650.     are echoed on the display screen using DrawText.  The string is
  651.     terminated with a return (ascii 13).  Backspace deleting is supported.
  652.     The text position is left at the beginning of the next line.
  653.  
  654.      Parameter       Type      Description
  655.     AcceptString    char ptr   returned text string from user
  656. ***/
  657.  
  658. {
  659.     char   ch[2];
  660.     int    len, last, n_chars;
  661.  
  662.     strcpy (AcceptString, "");
  663.     ch[0] = 0;  ch[1] = 0;
  664.     while (ch[0] != 13)
  665.     {
  666.         EraseText (TextLine, TextSample, TextHeight, 15, 0);
  667.         ch[0] = getch();
  668.         if ( (ch[0] == 8) && (strlen(AcceptString) > 0) )
  669.         {
  670.             last = strlen(AcceptString)-1;
  671.             LengthText (&AcceptString[last], TextHeight, &len);
  672.             TextSample -= len;
  673.             AcceptString[last] = 0;
  674.         }
  675.         else if (ch[0] >= ' ')
  676.         {
  677.             DrawText (ch, TextLine, TextSample, TextHeight, 0, numDN-1);
  678.             LengthText (ch, TextHeight, &len);
  679.             TextSample += len;
  680.             strcat (AcceptString, ch);
  681.         }
  682.     }
  683.     TextSample = 1;
  684. }
  685.  
  686. int StatusLine (int WhichLine, char * TextString)
  687.  
  688. /***  StatusText puts a line of text on the screen at the top, (error
  689.     reporting), or one of two lines on the bottom of the screen (file
  690.     name and command line).   It erases the previous line before writing
  691.     the new one.
  692.  
  693.      Parameter      Type       Description
  694.     WhichLine       int        which line to erase and display
  695.     TextString    char ptr     returned text string from user
  696. ***/
  697.  
  698. {
  699.     static int  Last0=0, Last1=0, Last2=0;
  700.     int    TextLength, n_chars;
  701.  
  702.     {
  703.        if (WhichLine == 0)
  704.        {
  705.            TextLine = TextHeight+5; TextSample = 1;
  706.            TextLength = Last0;
  707.        }
  708.        if (WhichLine == 1)
  709.        {
  710.            TextLine = dispnl-TextHeight-12; TextSample = 1;
  711.            TextLength = Last1;
  712.        }
  713.        if (WhichLine == 2)
  714.        {
  715.            TextLine = dispnl-5; TextSample = 1;
  716.            TextLength = Last2;
  717.        }
  718.        if (TextLength > 0)
  719.        {
  720.            EraseText (TextLine, TextSample, TextHeight, TextLength, 0);
  721.        }
  722.  
  723.        LengthText (TextString, TextHeight, &TextLength);
  724.        if (WhichLine == 0) Last0 = TextLength;
  725.        if (WhichLine == 1) Last1 = TextLength;
  726.        if (WhichLine == 2) Last2 = TextLength;
  727.        TypeText (TextString);
  728.     }
  729. }
  730.  
  731. int RemoveCursor(void)
  732.  
  733. /***  RemoveCursor removes the cursor, if it is on, from the display
  734.     screen, replacing the original pixel values.
  735. ***/
  736. {
  737.     int    lcorn, scorn, l, s, DN;
  738.     int    ls, le, ss, se;
  739.  
  740.     if (CursorOn)
  741.     {
  742.         lcorn = CursorLine - (CURSORSIZE / 2) - 1;
  743.         scorn = CursorSample - (CURSORSIZE / 2) - 1;
  744.         ls = (lcorn < 0) ?  1 - lcorn : 1;
  745.         le = (lcorn > dispnl-CURSORSIZE) ?  dispnl-lcorn : CURSORSIZE;
  746.         ss = (scorn < 0) ?  1 - scorn : 1;
  747.         se = (scorn > dispns-CURSORSIZE) ?  dispns-scorn : CURSORSIZE;
  748.  
  749.         for (l = ls;  l <= le;  l++)
  750.             for (s = ss;  s <= se;  s++)
  751.                 if (CursorShape[l-1][s-1] > 0)
  752.                 {
  753.                     DN = CursorPatch[l-1][s-1];
  754.                     WritePixel (lcorn+l, scorn+s, DN);
  755.                 }
  756.         CursorOn = 0;
  757.     }
  758. }
  759.  
  760.  
  761. int PlaceCursor (int line, int sample, int cursDN)
  762.  
  763. /***  PlaceCursor puts the cursor, if it is not already on, on the
  764.     screen, while storing the original pixel values.
  765.      Parameter     Type     Description
  766.       line         int      the line coordinate to put the cursor at
  767.       sample       int      the sample coordinate to put the cursor at
  768.       cursDN       int      the DN value of the cursor
  769.  
  770. ***/
  771.  
  772. {
  773.     int   lcorn, scorn, l, s, DN;
  774.     int   ls, le, ss, se, ld, sd;
  775.  
  776.     if (!CursorOn)
  777.     {
  778.         lcorn = line - (CURSORSIZE / 2) - 1;
  779.         scorn = sample - (CURSORSIZE / 2) - 1;
  780.         ls = (lcorn < 0) ?  1 - lcorn : 1;
  781.         le = (lcorn > dispnl-CURSORSIZE) ?  dispnl-lcorn : CURSORSIZE;
  782.         ss = (scorn < 0) ?  1 - scorn : 1;
  783.         se = (scorn > dispns-CURSORSIZE) ?  dispns-scorn : CURSORSIZE;
  784.  
  785.         for (l = ls;  l <= le;  l++)
  786.             for (s = ss;  s <= se;  s++)
  787.                 if (CursorShape[l-1][s-1] > 0)
  788.                 {
  789.                     ld = lcorn + l;
  790.                     sd = scorn + s;
  791.                     ReadPixel (ld, sd, &DN);
  792.                     CursorPatch[l-1][s-1] = DN;
  793.                     WritePixel (ld, sd, cursDN );
  794.                 }
  795.         CursorLine = line;
  796.         CursorSample = sample;
  797.         CursorOn = 1;
  798.     }
  799. }
  800.  
  801. int MoveCursorInput (int *p_line, int *p_sample, unsigned char *ch)
  802.  
  803. /***  MoveCursorInput puts the cursor at the given place, waits
  804.     for a character from the keyboard, performs the cursor command
  805.     for the input character, and removes the cursor.
  806.      Parameter     Type     Description
  807.       p_line     int ptr     the cursor line coordinate
  808.       p_sample   int ptr     the cursor sample coordinate
  809.       ch         char ptr    the character input
  810. ***/
  811.  
  812. {
  813.     int    DN, center, len;
  814.     char   dispstr[32];
  815.  
  816.     PlaceCursor (*p_line, *p_sample, numDN-1);
  817.  
  818.     /* moved display of curs here - otherwise 1 behind mdm 3/88 */
  819.     center = CURSORSIZE/2;
  820.     DN     = CursorPatch[center][center];
  821.  
  822.     sprintf (dispstr, "%4d %4d %3d", *p_line, *p_sample, DN);
  823.     StatusLine(2,dispstr);
  824.  
  825.   if ((*ch = getch()) == 0)  /* mdm 10/6 */
  826.       *ch = 0x80 | getch();
  827.  
  828.     switch (*ch)
  829.     {
  830.     case UP_ARROW:
  831.     case '8' :
  832.              *p_line = max(*p_line-CursorInc, 1);
  833.              break;
  834.     case DOWN_ARROW:
  835.     case '2' :
  836.              *p_line = min(*p_line+CursorInc, dispnl);
  837.              break;
  838.     case LEFT_ARROW:
  839.     case '4' :
  840.              *p_sample = max(*p_sample-CursorInc, 1);
  841.              break;
  842.     case RIGHT_ARROW:
  843.     case '6' :
  844.              *p_sample = min(*p_sample+CursorInc, dispns);
  845.              break;
  846.     case UP_LEFT_ARROW:
  847.     case '7' :
  848.              *p_line = max(*p_line-CursorInc, 1);
  849.              *p_sample = max(*p_sample-CursorInc, 1);
  850.              break;
  851.     case UP_RIGHT_ARROW:
  852.     case '9' :
  853.              *p_line = max(*p_line-CursorInc, 1);
  854.              *p_sample = min(*p_sample+CursorInc, dispns);
  855.              break;
  856.     case DOWN_RIGHT_ARROW:
  857.     case '3' :
  858.              *p_line = min(*p_line+CursorInc, dispnl);
  859.              *p_sample = min(*p_sample+CursorInc, dispns);
  860.              break;
  861.     case DOWN_LEFT_ARROW:
  862.     case '1' :
  863.              *p_line = min(*p_line+CursorInc, dispnl);
  864.              *p_sample = max(*p_sample-CursorInc, 1);
  865.              break;
  866.     case '-' :
  867.     case '_' : /* accept as unshifted minus */
  868.              CursorInc = max(CursorInc / 2, 1);
  869.              break;
  870.     case '=' : /* accept as unshifted plus */ 
  871.     case '+' :
  872.              CursorInc = min(2*CursorInc, dispnl / 4);
  873.              break;
  874.     }
  875.  
  876.     RemoveCursor();
  877. }
  878.  
  879. int MoveCursor (int * p_line, int * p_sample)
  880.  
  881. /***  MoveCursor performs the cursor mode operation.
  882.     The user moves the cursor around using the numeric keypad.
  883.     The cursor moves in discrete steps whose size can be changed.
  884.     The final cursor position is returned.
  885.  
  886.     The following cursor command are performed:
  887.  
  888.            Character      Action
  889.               8           cursor up one step
  890.               9           cursor up and right one step
  891.               6           cursor right one step
  892.               3           cursor down and right one step
  893.               2           cursor down one step
  894.               1           cursor down and left one step
  895.               4           cursor left one step
  896.               7           cursor up and left one step
  897.               5           cursor position and correspoding pixel value
  898.                               display at upper right of screen
  899.               -           cursor step size decreased by factor of 2
  900.               +           cursor step size increased by factor of 2
  901.               .           exits cursor mode
  902.  
  903.      Parameter     Type      Description
  904.       p_line     int ptr     the cursor line coordinate
  905.       p_sample   int ptr     the cursor sample coordinate
  906. ***/
  907.  
  908. {
  909.     unsigned  char   ch;  /* mdm 10/6 */
  910.  
  911.     *p_line = CursorLine;
  912.     *p_sample = CursorSample;
  913.     do
  914.     {
  915.        MoveCursorInput (p_line, p_sample, &ch);
  916.        if (ch == RETURN || ch == ESCAPE)
  917.            ch = '.';
  918.     }
  919.     while (ch != '.');
  920.  
  921. }
  922.  
  923. int InitDisplay(void)
  924.  
  925. /*** InitDisplay turns the display device on, loads the default palette,
  926.     initializes the global variables, and loads the font arrays.
  927. ***/
  928.  
  929. {
  930.     int    i;
  931.     char   *devname;
  932.  
  933.     thefont = -1;
  934.     Font (0);
  935.     DisplayDevice = 0;
  936.  
  937.     if ((devname = getenv("IMDISP")) != NULL)
  938.       {
  939.        if (stricmp(devname, "cga"       ) == 0) DisplayDevice = CGA;
  940.        if (stricmp(devname, "ega480"    ) == 0) DisplayDevice = EGA480;
  941.        if (stricmp(devname, "ega"       ) == 0) DisplayDevice = EGA350;
  942.        if (stricmp(devname, "pga"       ) == 0) DisplayDevice = PGA;
  943.        if (stricmp(devname, "vga320"    ) == 0) DisplayDevice = VGA200;
  944.        if (stricmp(devname, "vga"       ) == 0) DisplayDevice = VGA480;
  945.        if (stricmp(devname, "orchid1024") == 0) DisplayDevice = ORCHID768;
  946.        if (stricmp(devname, "orchid800" ) == 0) DisplayDevice = ORCHID600;
  947.        if (stricmp(devname, "orchid"    ) == 0) DisplayDevice = ORCHID480;
  948.        if (stricmp(devname, "evga640"   ) == 0) DisplayDevice = EVGA640;
  949.        if (stricmp(devname, "evga512"   ) == 0) DisplayDevice = EVGA512;
  950.        if (stricmp(devname, "evga800"   ) == 0) DisplayDevice = EVGA800;
  951.        if (stricmp(devname, "bios"      ) == 0) DisplayDevice = BIOS;
  952.        if (stricmp(devname, "ati640"    ) == 0) DisplayDevice = ATI640;
  953.        if (stricmp(devname, "ati800"    ) == 0) DisplayDevice = ATI800;
  954.        if (stricmp(devname, "ati1024"   ) == 0) DisplayDevice = ATI1024;
  955.        if (stricmp(devname, "paradise"  ) == 0) DisplayDevice = PARADISE;
  956.       }
  957.     else if (getenv("orchid") != NULL)         DisplayDevice = ORCHID480;
  958.     else if (getenv("ega480") != NULL)         DisplayDevice = EGA480;
  959.     else if (getenv("vga")    != NULL)         DisplayDevice = VGA480;
  960.  
  961.  
  962.     DisplayOn();
  963.     WritePalette (DefaultPalette);
  964.  
  965.     CursorOn   = 0;
  966.     CursorLine = dispnl / 2;    CursorSample = dispns / 2;
  967.     CursorInc  = 16;
  968.  
  969.     TextLine = TextHeight+5;     TextSample = 1;
  970. }
  971.  
  972. void   FrameBox( int top, int left, int bottom, int right, int color, int flag)
  973. {
  974.        DrawLine( bottom, right, bottom, left, color);
  975.        DrawLine( bottom, left, top, left, color);
  976.        if (flag == TRUE)
  977.        {
  978.            DrawLine( top, left, top, right, color);
  979.            DrawLine( top, right, bottom, right, color);
  980.        }
  981. }
  982. 
  983.