home *** CD-ROM | disk | FTP | other *** search
/ Frozen Fish 1: Amiga / FrozenFish-Apr94.iso / bbs / alib / d1xx / d165 / plotview.lha / PlotView / plot2am.c < prev    next >
C/C++ Source or Header  |  1988-11-22  |  9KB  |  413 lines

  1. /*
  2.      plot2am.c : Plot Unix plot files on an Amiga HIRES Screen.
  3.  
  4.      By Joel Swank April, 1988
  5.  
  6.      */
  7. #include <exec/types.h>
  8. #include <exec/memory.h>
  9. #include <exec/tasks.h>
  10. #include <intuition/intuition.h>
  11. #include <stdio.h>
  12. /*  The header files needed for gadget definitions  */ 
  13. #include <intuition/intuitionbase.h> 
  14. #include <libraries/dosextens.h> 
  15. #include <graphics/gfxbase.h> 
  16. #include <graphics/gfx.h> 
  17. #include <graphics/gfxmacros.h> 
  18. #include <graphics/display.h> 
  19.  
  20. #include <graphics/text.h> 
  21. #include <ctype.h> 
  22.  
  23. #include <functions.h> 
  24. #include "plotscreen.h" 
  25. #ifdef NULL
  26. #undef NULL
  27. #endif
  28. #define NULL ((void *)0)
  29.  
  30. #define INTUITION_REV 1L
  31.  
  32. static struct NewWindow    New_Window = {
  33.     0, 2,            /* Take all except the */
  34.     XSIZE, YSIZE,    /* top two lines       */
  35.     -1, -1,            /* Default pens */
  36.     CLOSEWINDOW,    /* Inputs acceppeted */
  37.     WINDOWCLOSE        /* Fairly standard window */
  38.     | WINDOWDEPTH | WINDOWDRAG | SMART_REFRESH | BORDERLESS ,
  39.     NULL,            /* no gadgets */
  40.     (struct Image *) NULL,
  41.     (UBYTE *)"Plot2Am",    /* title */
  42.     (struct Screen *) NULL,        /* filled at startup */
  43.     (struct BitMap *) NULL,
  44.     0, 0, 0, 0,        /* no change sizes, doesn't matter */
  45.     CUSTOMSCREEN
  46.     } ;
  47.  
  48.  
  49.  
  50. struct    TextAttr myattr = {
  51.     (STRPTR)("puny.font"),
  52.     7,
  53.     0,
  54.     FPB_DISKFONT};
  55. struct    TextFont *myfont = NULL;
  56.  
  57.  
  58. /*
  59.  * Some things that need to be shared with done.
  60.  */
  61. struct IntuitionBase    *IntuitionBase = NULL;
  62. static struct Window        *Wind = NULL ;
  63. static struct Screen        *MyScreen = NULL ;
  64. static struct MsgPort        *CreatePort() ;
  65. struct GfxBase *GfxBase = NULL;
  66. struct RastPort *rp;
  67. struct Library *DiskfontBase = NULL;
  68. FILE *input;
  69.  
  70. /* scaling factors */
  71.  
  72. long xscale, yscale;  /* width, height of output device */
  73. long xmult, ymult;  /* width, height on Amiga Screen */
  74. long xoff, yoff;      /* offset to lower left corner  */
  75.  
  76. /* linemode constants */
  77.  
  78. char *lmodestr[] = { "dotted",
  79.                      "solid",
  80.                      "longdashed",
  81.                      "shortdashed",
  82.                      "dotdashed" };
  83.  
  84. USHORT lmode[] = { 0xaaaa, 0xffff, 0xff00, 0xf0f0, 0xf2f2 };
  85.  
  86.  
  87. main(argc,argv)
  88. int argc;
  89. char *argv[];
  90. {
  91.  
  92. struct IntuiMessage    *msg;
  93. int cmd, i;
  94. long x,y, r;
  95. long r1, r2;
  96. long x1,y1,x2,y2;
  97. char textbuf[100], *p;
  98.  
  99. /*************************************************
  100.      Init defaults
  101. *************************************************/
  102.  
  103. xscale = 3120;
  104. yscale = 3120;
  105. xmult = 550;
  106. ymult = 400;
  107. xoff = 0;
  108. yoff = 0;
  109.  
  110.  
  111. /*************************************************
  112.      Interrogate command line 
  113. *************************************************/
  114.  
  115. if (argc == 0) exit(1); /* no WB for now */
  116.  
  117. if (argc == 1)
  118.     {
  119.     fprintf(stderr,"Usage:plot2am plotfile\n");
  120.     exit(2);
  121.     }
  122.  
  123. while (*argv[1] == '-') 
  124.     {
  125.     p = (char *) &*argv[1];
  126.     p++;        /* point to the option chars */
  127.     switch (*p)
  128.         {
  129.         case 'x':        /* x size value */
  130.             xmult = atol(++p);
  131.             break;
  132.  
  133.         case 'y':        /* y size value */
  134.             ymult = atol(++p);
  135.             break;
  136.  
  137.         default:
  138.             fprintf(stderr,"plot2am:Invalid option %s\n",argv[1]);
  139.             exit(27);
  140.         }
  141.     argc--;
  142.     argv++;
  143.     }
  144.  
  145. if (argc == 1 || argc >2)
  146.     {
  147.     fprintf(stderr,"plot2am:Exactly One filename required\n");
  148.     exit(2);
  149.     }
  150.  
  151.  
  152. if ((input = fopen(argv[1],"r")) == NULL)
  153.     {
  154.     fprintf(stderr,"plot2am: %s: open failed\n",argv[1]);
  155.     exit(3);
  156.     }
  157.  
  158. /*************************************************
  159.      OPEN everything
  160. *************************************************/
  161.  
  162.  
  163. if ((IntuitionBase = (struct IntuitionBase *)
  164.     OpenLibrary("intuition.library", INTUITION_REV)) == NULL)
  165.     done(21);
  166.  
  167. GfxBase = (struct GfxBase *)OpenLibrary("graphics.library", NULL);
  168. if (GfxBase == NULL) done(22);
  169.  
  170. DiskfontBase = (struct Library *)OpenLibrary("diskfont.library", NULL);
  171. if (DiskfontBase == NULL) {
  172.     fprintf(stderr,"plot2am:cannot open font library - using default font\n");
  173.     }
  174.  
  175. if ((MyScreen = (struct Screen *) OpenScreen(&NewScreenStructure)) == NULL)
  176.     done(23);
  177.  
  178. New_Window.Screen = MyScreen;
  179.  
  180. if ((Wind = (struct Window *) OpenWindow(&New_Window)) == NULL)
  181.     done(25);
  182. rp = Wind->RPort;
  183.  
  184. if (DiskfontBase != NULL) {
  185.     if ((myfont = (struct TextFont *)OpenDiskFont(&myattr)) == NULL) {
  186.         fprintf(stderr,"plot2am:cannot find puny font - using default\n");
  187.     } else  SetFont(rp,myfont);
  188. }
  189.  
  190.  
  191. LoadRGB4(&(MyScreen->ViewPort),&Palette,PaletteColorCount);
  192.  
  193.  
  194. /*************************************************
  195.      MAIN Drawing loop
  196. *************************************************/
  197.  
  198. while ((cmd = getc(input)) != EOF)
  199.     {
  200.     switch (cmd)
  201.         {
  202.         case 'm':    /* move x,y */
  203.             get_xy(&x,&y);
  204.             Move(rp,x,y);
  205.             break;
  206.         case 'n':    /* draw x,y */
  207.             get_xy(&x,&y);
  208.             Draw(rp,x,y);
  209.             break;
  210.         case 'p':    /* point x,y */
  211.             get_xy(&x,&y);
  212.             WritePixel(rp,x,y);
  213.             break;
  214.         case 'l':    /* line xs,ys, xe,ye */
  215.             get_xy(&x,&y);
  216.             Move(rp,x,y);
  217.             get_xy(&x,&y);
  218.             Draw(rp,x,y);
  219.             break;
  220.         case 'a':    /* arc xc,yc, xs,ys, xe,ye */
  221.             get_xy(&x,&y);    /* get center */
  222.             get_xy(&x2,&y2);  /* get end point */
  223.             get_xy(&x1,&y1);  /* get start point */
  224.             arc(x, y, x1, y1, x2, y2);  /* draw counterclockwise  */
  225.             Move(rp,x1,y1);
  226.             break;
  227.         case 't':    /* Text string\n   */
  228.             get_txt(textbuf);
  229.             if (rp->cp_y == 0) break;
  230.             Text(rp,textbuf, (long) strlen(textbuf));
  231.             break;
  232.         case 'c':    /* circle xc,yc, r */
  233.             get_xy(&x,&y);
  234.             get_int(&r);
  235.             r1 = r*xmult/xscale;
  236.             r2 = r*ymult/yscale;
  237.             DrawEllipse(rp,x,y,r1,r2);
  238.             break;
  239.         case 'f':    /* linemode string\n   */
  240.             get_txt(textbuf);
  241.             for (i=0; i<5; i++)
  242.                 {
  243.                 if (0 == strcmp(textbuf,lmodestr[i]))
  244.                     {
  245.                     SetDrPt(rp,lmode[i]);
  246.                     break;
  247.                     }
  248.                 }
  249.             break;
  250.         case 's':    /* space xlo,ylo, xhi,yhi */
  251.             get_int(&xoff);
  252.             get_int(&yoff);
  253.             get_int(&xscale);
  254.             get_int(&yscale);
  255.             xscale = xscale - xoff;
  256.             yscale = yscale - yoff;
  257.             break;
  258.         case 'e':    /* erase */
  259.             SetAPen(rp,0L);
  260.             RectFill(rp,0L,0L,(long) (XSIZE-1),(long) (YSIZE-1));
  261.             SetAPen(rp,1L);
  262.             break;
  263.         }
  264.     }
  265.  
  266.  
  267. /*************************************************
  268.      WAIT for Close Gadget
  269. *************************************************/
  270.  
  271. while (1)
  272.     {
  273.     Wait( 1L << Wind->UserPort->mp_SigBit);    /* wait on mesg */
  274.     while(msg = (struct IntuiMessage *) GetMsg(Wind->UserPort)) {
  275.         switch(msg->Class) {
  276.             case CLOSEWINDOW:
  277.                 ReplyMsg(msg);
  278.                 done(0);
  279.             }
  280.             ReplyMsg(msg);
  281.         }
  282.     }
  283. }
  284.  
  285. /*************************************************
  286.      Parameter input routines
  287. *************************************************/
  288.  
  289.  
  290. /*
  291.  * input a pair of 16 bit ints, scale
  292.  * and return them as longs
  293.  *
  294.  */
  295.  
  296. get_xy(x,y)
  297. register long *x, *y;
  298. {
  299.     get_int(x);
  300.     *x = (*x-xoff)*xmult/xscale;
  301.     get_int(y);
  302.     *y = (YSIZE-1)-(*y-yoff)*ymult/yscale;
  303. }
  304.  
  305. /*
  306.  * input a 16 bit int and return as a long
  307.  */
  308.  
  309. get_int(num)
  310. long *num;
  311. {
  312.     register long hi, lo;
  313.     lo = (long) getc(input);
  314.     hi = ( (long) getc(input)) << 8;
  315.     *num = lo + hi;
  316. }
  317.  
  318. /*
  319.  * input a text string delinited by newline,
  320.  * return to buffer delimited by a null.
  321.  */
  322.  
  323. get_txt(str)
  324. char *str;
  325. {
  326.     register int cmd;
  327.     while ((cmd = getc(input)) != '\n')
  328.         *str++ = cmd;
  329.     *str = '\0';
  330. }
  331.  
  332. /*
  333.  * done - just clean up that which is open, and then leave.
  334.  */
  335. done(how)
  336. int how;
  337.     {
  338.     if (Wind) CloseWindow(Wind) ;
  339.     if (MyScreen) CloseScreen(MyScreen) ;
  340.     if (IntuitionBase) CloseLibrary(IntuitionBase) ;
  341.     if (GfxBase) CloseLibrary(GfxBase) ;
  342.     if (DiskfontBase) CloseLibrary(DiskfontBase) ;
  343.     if (myfont   != NULL) CloseFont( myfont );
  344.  
  345.     OpenWorkBench() ;        /* As requested */
  346.     exit(how) ;
  347.     }
  348.  
  349. /*
  350.  * arc and integer sqrt routines.
  351.  * lifted from sunplot program by:
  352.  
  353. Sjoerd Mullender
  354. Dept. of Mathematics and Computer Science
  355. Free University
  356. Amsterdam
  357. Netherlands
  358.  
  359. Email: sjoerd@cs.vu.nl
  360. If this doesn't work, try ...!seismo!mcvax!cs.vu.nl!sjoerd or
  361. ...!seismo!mcvax!vu44!sjoerd or sjoerd%cs.vu.nl@seismo.css.gov.
  362.  
  363.  *
  364.  */
  365.  
  366. long
  367. isqrt(n)
  368. long n;
  369. {
  370.     long a, b, c;
  371.  
  372.     a = n;
  373.     b = n;
  374.     if (n > 1) {
  375.         while (a > 0) {
  376.             a = a >> 2;
  377.             b = b >> 1;
  378.         }
  379.         do {
  380.             a = b;
  381.             c = n / b;
  382.             b = (c + a) >> 1;
  383.         } while ((a - c) < -1 || (a - c) > 1);
  384.     }
  385.     return b;
  386. }
  387.  
  388.  
  389. #define setcir(x, y, a1, b1, c1, a2, b2, c2) \
  390.     {if (a1 * (y) - b1 * (x) >= c1 && a2 * (y) - b2 * (x) <= c2) \
  391.     WritePixel(rp,x, y);}
  392.  
  393. arc(x, y, x1, y1, x2, y2)
  394. long x, y, x1, y1, x2, y2;
  395. {
  396.     register long a1 = x1 - x, b1 = y1 - y, a2 = x2 - x, b2 = y2 - y;
  397.     register long c1 = a1 * y - b1 * x, c2 = a2 * y - b2 * x;
  398.     register long r2 = a1 * a1 + b1 * b1;
  399.     register long i, sqrt;
  400.  
  401.     for (i = isqrt(r2 >> 1); i >= 0; i -= 1) {
  402.         sqrt = isqrt(r2 - i * i);
  403.         setcir(x + i, y + sqrt, a1, b1, c1, a2, b2, c2);
  404.         setcir(x + i, y - sqrt, a1, b1, c1, a2, b2, c2);
  405.         setcir(x - i, y + sqrt, a1, b1, c1, a2, b2, c2);
  406.         setcir(x - i, y - sqrt, a1, b1, c1, a2, b2, c2);
  407.         setcir(x + sqrt, y + i, a1, b1, c1, a2, b2, c2);
  408.         setcir(x + sqrt, y - i, a1, b1, c1, a2, b2, c2);
  409.         setcir(x - sqrt, y + i, a1, b1, c1, a2, b2, c2);
  410.         setcir(x - sqrt, y - i, a1, b1, c1, a2, b2, c2);
  411.     }
  412. }
  413.