home *** CD-ROM | disk | FTP | other *** search
/ Usenet 1994 October / usenetsourcesnewsgroupsinfomagicoctober1994disk2.iso / misc / volume10 / tek / tekdecode.c < prev    next >
Encoding:
C/C++ Source or Header  |  1990-02-20  |  4.4 KB  |  260 lines

  1. /*
  2.  *    tekdecode.c - converts Tek4014 picture files 
  3.  *    copyright 1988 Ronald Florence 
  4.  *
  5.  *    decoding algorithm from J. Tenenbaum (2/24/87)
  6.  *    alpha mode (2/21/88)
  7.  *    linetypes (5/12/88)
  8.  *    increment mode (5/23/88)
  9.  *    corrected algorithm (6/30/88)
  10.  */
  11.  
  12. #include        <stdio.h>
  13. #include    "tek4.h"
  14.  
  15. #define US      0x1f
  16. #define RS    0x1e
  17. #define GS      0x1d
  18. #define FS      0x1c
  19. #define ESC    0x1b
  20. #define BS      0x8
  21. #define HT      0x9
  22. #define LF      0xa
  23. #define VT      0xb
  24.  
  25. #define NONE        0
  26. #define VECTOR      1
  27. #define POINT       2
  28. #define ALPHA       3
  29. #define INCREMENT    4
  30.  
  31. #define Low_Y        0x2
  32. #define CTRL        0
  33. #define HI        1
  34. #define LOX        2
  35. #define LOY        3
  36.  
  37. static    int    charht[4] = { 36, 40, 61, 66 },
  38.         charwd[4] = { 24, 26, 40, 44 },
  39. #ifdef WEATHER
  40.         weather,
  41. #endif
  42.         cellw,            /* current width of a character cell */
  43.         chsize = 3;         /* font */
  44.  
  45.  
  46. tekdecode (infile, opt) 
  47.      FILE  *infile;
  48.      int   opt;
  49. {
  50.   register    c, mode; 
  51.   int    pts[Maxpts * 2], pn, got = 0, escape = 0, hisave, 
  52.            tx, ty, lox, loy, hix, hiy, devx, devy;
  53.   char    gstring[256], *g;
  54.  
  55.   mode = NONE;
  56.   Align(Left, Base);
  57.   Set_charsize(chsize);
  58.   while ((c = getc(infile)) != EOF) 
  59.     {
  60.       c &= 0x7f;
  61.       if (escape) 
  62.     {
  63.       parse(c);
  64.       escape = 0;
  65.       continue;
  66.     }
  67.       if (c >> 5 == CTRL)
  68.     {
  69.       if (c == '\0')    /* they pad, we strip */
  70.         continue;
  71.       if (mode == ALPHA) 
  72.         {
  73.           *g = '\0';
  74.           if (g > gstring)
  75.         Wr_str(devx, devy, gstring);
  76.           devx += (g - gstring) * cellw;
  77.           mode = NONE;
  78.         }
  79.       if (mode == INCREMENT) 
  80.         {
  81.           tx *= xm;
  82.           ty *= ym;
  83.           Increment(tx, ty);
  84.         }
  85.       if (mode == VECTOR && pn)
  86.         Vector(pn, pts);
  87.       if (mode == POINT && pn) 
  88.         Marker(pn, pts);
  89.      
  90.       switch (c) 
  91.         {
  92.         case BS:        /* cursor motions */
  93.         case HT:        /* we'll do nothing */
  94.         case VT:
  95.           break;
  96.         case GS:
  97.           mode = VECTOR;
  98.           pn = 0;
  99.           break;
  100.         case FS:
  101.           mode = POINT;
  102.           pn = 0;
  103.           break;
  104.         case US:
  105.           g = gstring;
  106.           mode = ALPHA;
  107.           break;
  108.         case ESC :
  109.           ++escape;
  110.           break;
  111.         case RS :
  112.           mode = INCREMENT;
  113.           tx = 0;
  114.           ty = 0;
  115.           break;
  116.         case LF:        /* clear bypass */
  117.           mode = NONE;
  118.           break;
  119.         }
  120.       continue;
  121.     }
  122.                 /* else not a control char */
  123. #ifdef WEATHER
  124.       if (opt && weather)  
  125.     {
  126.       weather = 0;    
  127.       if (c == 'P')        /* bailout at "PRESS" */
  128.         return;
  129.       if (c == 'c' && chsize == 0) /* tiny "c" for (c) */
  130.         Switch_pen();
  131.     }
  132. #endif
  133.       switch (mode) 
  134.     {
  135.     case NONE:
  136.       continue;
  137.     case VECTOR:
  138.     case POINT :
  139.       switch (c >> 5)
  140.         {
  141.         case HI:
  142.           hisave = c & ~0x20;
  143.           got |= HI;
  144.           break;
  145.         case LOY:
  146.           loy = c & ~0x60;
  147.           got |= Low_Y;
  148.           if (got & HI) 
  149.         {
  150.           hiy = hisave;
  151.           got &= ~HI;
  152.         }
  153.           break;
  154.         case LOX:
  155.           lox = c & ~0x40;
  156.           if (got & HI)
  157.         {
  158.           if (got & Low_Y)
  159.             hix = hisave;
  160.           else
  161.             hiy = hisave;
  162.         }
  163.           got &= ~(HI | Low_Y);
  164.                       /* process the coordinates */
  165.           tx = ((hix << 5) + lox) << 2;
  166.           ty = ((hiy << 5) + loy) << 2;
  167.           devx = tx * xm + xscale;
  168.           devy = ty * ym + yscale;
  169.           pts[pn++] = devx;
  170.           pts[pn++] = devy;
  171.           if (pn > Maxpts * 2)
  172.         err("data");
  173.           break;
  174.         }
  175.       break;
  176.     case ALPHA:
  177.       *g++ = c;
  178.       break;
  179.     case INCREMENT:
  180.       switch (c) 
  181.         {
  182.         case ' ':
  183.           break;
  184.         case 'D':        /* north */
  185.           ++ty;
  186.           break;
  187.         case 'H':        /* south */
  188.           --ty;
  189.           break;
  190.         case 'A':        /* east */
  191.           ++tx;
  192.           break;
  193.         case 'B':        /* west */
  194.           --tx;
  195.           break;
  196.         case 'E':        /* northeast */
  197.           ++ty;
  198.           ++tx;
  199.           break;
  200.         case 'J':        /* southwest */
  201.           --ty;
  202.           --tx;
  203.           break;
  204.         }
  205.       break;
  206.     }
  207.     }
  208. }
  209.  
  210.  
  211. parse(inch)
  212.      int  inch;
  213. {
  214.   int    ltype = NONE;
  215.  
  216.   switch (inch) 
  217.     {
  218.     case '\f':
  219.       Clear_scr();
  220.       break;
  221.     case '`' : 
  222.     case 'e' :
  223.     case 'f' :
  224.     case 'g' :
  225.     case 'h' :
  226.     case 'm' :
  227.     case 'n' :
  228.     case 'o' :
  229.       ltype = Solid;
  230.       break;
  231.     case 'a' :
  232.     case 'i' :
  233.       ltype = Dotted;
  234.       break;
  235.     case 'b' :
  236.     case 'j' :
  237.       ltype = Dotdash;
  238.       break;
  239.     case 'c' :
  240.     case 'k' :
  241.       ltype = Shortdash;
  242.       break;
  243.     case 'd' :
  244.     case 'l' :
  245.       ltype = Longdash;
  246.       break;
  247.     case '8' :
  248.     case '9' :
  249.     case ':' :
  250.     case ';' :
  251.       Set_charsize (chsize = ';' - inch);
  252. #ifdef WEATHER
  253.       ++weather;
  254. #endif
  255.       break;
  256.     }
  257.   if (ltype > 0)
  258.     Set_line(ltype);
  259. }
  260.