Back to index

radiance  4R0+20100331
xmeta.c
Go to the documentation of this file.
00001 #ifndef lint
00002 static const char    RCSid[] = "$Id: xmeta.c,v 1.5 2007/11/21 18:51:05 greg Exp $";
00003 #endif
00004 /*
00005  *  Program to output meta-files to X window system.
00006  *
00007  *  cc -o Xmeta Xmeta.c Xplot.o plot.o mfio.o syscalls.o misc.o
00008  *
00009  *     2/26/86
00010  */
00011 
00012 #include  "rtprocess.h"
00013 #include  "string.h"
00014 #include  "meta.h"
00015 #include  "plot.h"
00016 
00017 extern void   init(char *name, char *geom);
00018 
00019 #define  overlap(p,xmn,ymn,xmx,ymx)  (ov((p)->xy[XMN],(p)->xy[XMX],xmn,xmx) \
00020                                  &&ov((p)->xy[YMN],(p)->xy[YMX],ymn,ymx))
00021 
00022 #define  ov(mn1,mx1,mn2,mx2)  ((mn1)<(mx2)&&(mn2)<(mx1))
00023 
00024 #define  XCOM  "pexpand +OCIsv -P %s"
00025 
00026 
00027 char  *progname;
00028 
00029 static short  newpage = TRUE; 
00030 
00031 static PLIST  recording;
00032 
00033 int maxalloc = 0;           /* no limit */
00034 
00035 extern void   pXFlush(void);
00036 
00037 
00038 void
00039 save(p)                            /* record primitive */
00040 PRIMITIVE  *p;
00041 {
00042     register PRIMITIVE  *pnew;
00043 
00044     if ((pnew = palloc()) == NULL)
00045            error(SYSTEM, "out of memory in save");
00046     mcopy((char *)pnew, (char *)p, sizeof(PRIMITIVE));
00047     add(pnew, &recording);
00048 }
00049 
00050 
00051 void
00052 doglobal(g, sf)                    /* execute a global command */
00053 register PRIMITIVE  *g;
00054 int  sf;
00055 {
00056 
00057     switch (g->com) {
00058 
00059        case PEOF:
00060            return;
00061 
00062        case PDRAW:
00063            pXFlush();
00064            break;
00065 
00066        case PEOP:
00067            endpage();
00068            plfree(&recording);
00069            set(SALL, NULL);
00070            newpage = TRUE;
00071            return;
00072 
00073        case PSET:
00074            set(g->arg0, g->args);
00075            break;
00076 
00077        case PUNSET:
00078            unset(g->arg0);
00079            break;
00080 
00081        case PRESET:
00082            reset(g->arg0);
00083            break;
00084 
00085        default:
00086            sprintf(errmsg, "unknown command '%c' in doglobal", g->com);
00087            error(WARNING, errmsg);
00088            return;
00089        }
00090        if (sf)
00091            save(g);
00092 
00093 }
00094 
00095 
00096 void
00097 doprim(p, sf)        /* plot primitive */
00098 register PRIMITIVE  *p;
00099 int  sf;
00100 {
00101 
00102     switch (p->com) {
00103 
00104        case PMSTR:
00105            printstr(p);
00106            break;
00107 
00108        case PLSEG:
00109            plotlseg(p);
00110            break;
00111 
00112        case PRFILL:
00113            fillrect(p);
00114            break;
00115 
00116        case PTFILL:
00117            filltri(p);
00118            break;
00119 
00120        case PPFILL:
00121            fillpoly(p);
00122            break;
00123 
00124        default:
00125            sprintf(errmsg, "unknown command '%c' in doprim", p->com);
00126            error(WARNING, errmsg);
00127            return;
00128     }
00129     if (sf) {
00130        save(p);
00131        newpage = FALSE;
00132     }
00133 
00134 }
00135 
00136 
00137 void
00138 plot(infp)           /* plot meta-file */
00139 FILE  *infp;
00140 {
00141     PRIMITIVE  nextp;
00142 
00143     set(SALL, NULL);
00144     do {
00145        readp(&nextp, infp);
00146        while (isprim(nextp.com)) {
00147            doprim(&nextp, 1);
00148            readp(&nextp, infp);
00149        }
00150        doglobal(&nextp, 1);
00151     } while (nextp.com != PEOF);
00152 
00153 }
00154 
00155 
00156 void
00157 replay(xmin, ymin, xmax, ymax)            /* play back region */
00158 int  xmin, ymin, xmax, ymax;
00159 {
00160     register PRIMITIVE  *p;
00161 
00162     unset(SALL);
00163     set(SALL, NULL);
00164     for (p = recording.ptop; p != NULL; p = p->pnext)
00165        if (isprim(p->com)) {
00166            if (overlap(p, xmin, ymin, xmax, ymax))
00167               doprim(p, 0);
00168        } else
00169            doglobal(p, 0);
00170 
00171 }
00172 
00173 
00174 int
00175 main(argc, argv)
00176 int  argc;
00177 char  **argv;
00178 {
00179  FILE  *fp;
00180  char  *geometry = NULL;
00181  short  condonly, conditioned;
00182  char  comargs[500], command[600];
00183 
00184  progname = *argv++;
00185  argc--;
00186 
00187  condonly = FALSE;
00188  conditioned = FALSE;
00189 
00190  for ( ; argc; argv++, argc--)
00191     if (!strcmp(*argv, "-c"))
00192        condonly = TRUE;
00193     else if (!strcmp(*argv, "-r"))
00194        conditioned = TRUE;
00195     else if (**argv == '=')
00196        geometry = *argv;
00197     else
00198        break;
00199 
00200  if (conditioned) {
00201     init(progname, geometry);
00202     if (argc) 
00203        while (argc)  {
00204          fp = efopen(*argv, "r");
00205          plot(fp);
00206          fclose(fp);
00207          argv++;
00208          argc--;
00209          }
00210     else
00211        plot(stdin);
00212  } else  {
00213     comargs[0] = '\0';
00214     while (argc)  {
00215        strcat(comargs, " ");
00216        strcat(comargs, *argv);
00217        argv++;
00218        argc--;
00219        }
00220     sprintf(command, XCOM, comargs);
00221     if (condonly)
00222        return(system(command));
00223     else  {
00224        init(progname, geometry);
00225        if ((fp = popen(command, "r")) == NULL)
00226           error(SYSTEM, "cannot execute input filter");
00227        plot(fp);
00228        pclose(fp);
00229        }
00230     }
00231 
00232  if (!newpage)
00233      endpage();
00234 
00235  return(0);
00236  }