ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/util/xglaresrc.c
Revision: 2.8
Committed: Tue Nov 11 16:24:06 2003 UTC (20 years, 4 months ago) by greg
Content type: text/plain
Branch: MAIN
Changes since 2.7: +2 -3 lines
Log Message:
Replaced all calls to vfork() with regular fork() calls

File Contents

# User Rev Content
1 greg 1.1 #ifndef lint
2 greg 2.8 static const char RCSid[] = "$Id: xglaresrc.c,v 2.7 2003/11/10 16:52:24 greg Exp $";
3 greg 1.1 #endif
4     /*
5     * Circle sources in a displayed image.
6     *
7     * 18 Mar 1991 Greg Ward
8     */
9    
10     #include "standard.h"
11     #include "view.h"
12 greg 1.7 #include <signal.h>
13 greg 1.1 #include <X11/Xlib.h>
14     #include <X11/Xutil.h>
15 greg 1.7
16     #define XIM "ximage"
17    
18 greg 1.1 #define NSEG 30 /* number of segments per circle */
19    
20 greg 1.9 #define FONTNAME "8x13" /* text font we'll use */
21    
22     float col[3] = {1.,0.,0.}; /* color */
23    
24 greg 1.1 VIEW ourview = STDVIEW; /* view for picture */
25 greg 1.10 RESOLU pres; /* picture resolution */
26 greg 1.1
27     char *progname; /* program name */
28    
29     Display *theDisplay = NULL; /* connection to server */
30    
31     #define rwind RootWindow(theDisplay,ourScreen)
32     #define ourScreen DefaultScreen(theDisplay)
33    
34 greg 1.9 GC vecGC, strGC;
35 greg 1.1 Window gwind;
36    
37    
38     main(argc, argv)
39     int argc;
40     char *argv[];
41     {
42 greg 1.9 char *windowname = NULL;
43 greg 1.1 FILE *fp;
44    
45 greg 1.9 progname = *argv++; argc--;
46     while (argc > 0 && argv[0][0] == '-') {
47     switch (argv[0][1]) {
48     case 'n':
49     windowname = *++argv;
50     argc--;
51     break;
52     case 'c':
53     col[0] = atof(*++argv);
54     col[1] = atof(*++argv);
55     col[2] = atof(*++argv);
56     argc -= 3;
57     break;
58     }
59     argv++; argc--;
60     }
61     if (argc < 1 || argc > 2) {
62 greg 1.7 fprintf(stderr,
63 greg 1.9 "Usage: %s [-n windowname][-c color] picture [glaresrc]\n",
64 greg 1.1 progname);
65     exit(1);
66     }
67 greg 1.9 init(argv[0], windowname);
68     if (argc < 2)
69 greg 1.1 fp = stdin;
70 greg 1.9 else if ((fp = fopen(argv[1], "r")) == NULL) {
71     fprintf(stderr, "%s: cannot open \"%s\"\n", progname, argv[1]);
72 greg 1.1 exit(1);
73     }
74     circle_sources(fp);
75     exit(0);
76     }
77    
78    
79 greg 1.7 init(pname, wname) /* get view and find window */
80     char *pname, *wname;
81 greg 1.1 {
82 greg 1.5 extern Window xfindwind();
83 greg 1.1 XWindowAttributes wa;
84     XColor xc;
85     XGCValues gcv;
86 greg 2.5 register int i;
87 greg 1.1 /* get the viewing parameters */
88 greg 1.10 if (viewfile(pname, &ourview, &pres) <= 0 ||
89 greg 1.1 setview(&ourview) != NULL) {
90     fprintf(stderr, "%s: cannot get view from \"%s\"\n",
91 greg 1.7 progname, pname);
92 greg 1.1 exit(1);
93     }
94     /* open the display */
95     if ((theDisplay = XOpenDisplay(NULL)) == NULL) {
96     fprintf(stderr,
97     "%s: cannot open display; DISPLAY variable set?\n",
98     progname);
99     exit(1);
100     }
101     /* find our window */
102 greg 2.5 if (wname == NULL) {
103     /* remove directory prefix from name */
104     for (i = strlen(pname); i-- > 0; )
105     if (pname[i] == '/')
106     break;
107     wname = pname+i+1;
108     i = 0;
109     } else
110     i = 1;
111 greg 2.4 gwind = xfindwind(theDisplay, rwind, wname, 4);
112 greg 1.5 if (gwind == None) {
113 greg 2.5 if (i) {
114 greg 1.7 fprintf(stderr, "%s: cannot find \"%s\" window\n",
115     progname, wname);
116     exit(2);
117     }
118     /* start ximage */
119 greg 2.8 if (fork() == 0) {
120 greg 2.4 execlp(XIM, XIM, "-c", "256", pname, 0);
121 greg 1.7 perror(XIM);
122     fprintf(stderr, "%s: cannot start %s\n",
123     progname, XIM);
124     kill(getppid(), SIGPIPE);
125     _exit(1);
126     }
127     do
128     sleep(8);
129 greg 2.5 while ((gwind=xfindwind(theDisplay,rwind,wname,4)) == None);
130 greg 1.8 } else
131 greg 1.7 XMapRaised(theDisplay, gwind);
132 greg 1.8 do {
133     XGetWindowAttributes(theDisplay, gwind, &wa);
134 greg 1.9 sleep(6);
135 greg 1.8 } while (wa.map_state != IsViewable);
136 greg 1.10 if (wa.width != scanlen(&pres) || wa.height != numscans(&pres)) {
137 greg 1.1 fprintf(stderr,
138     "%s: warning -- window seems to be the wrong size!\n",
139     progname);
140 greg 2.6 if (pres.rt & YMAJOR) {
141 greg 1.10 pres.xr = wa.width;
142     pres.yr = wa.height;
143     } else {
144     pres.xr = wa.height;
145     pres.yr = wa.width;
146     }
147 greg 1.1 }
148     /* set graphics context */
149 greg 1.9 gcv.font = XLoadFont(theDisplay, FONTNAME);
150     if (gcv.font == 0) {
151     fprintf(stderr, "%s: cannot load font \"%s\"\n",
152     progname, FONTNAME);
153     exit(1);
154     }
155     xc.red = col[0] >= 1.0 ? 65535 : (unsigned)(65536*col[0]);
156     xc.green = col[1] >= 1.0 ? 65535 : (unsigned)(65536*col[1]);
157     xc.blue = col[2] >= 1.0 ? 65535 : (unsigned)(65536*col[2]);
158 greg 1.1 xc.flags = DoRed|DoGreen|DoBlue;
159 greg 1.9 gcv.background = xc.green >= 32768 ?
160     BlackPixel(theDisplay,DefaultScreen(theDisplay)) :
161     WhitePixel(theDisplay,DefaultScreen(theDisplay)) ;
162 greg 1.1 if (XAllocColor(theDisplay, wa.colormap, &xc)) {
163     gcv.foreground = xc.pixel;
164 greg 1.9 vecGC = XCreateGC(theDisplay,gwind,
165     GCForeground|GCBackground|GCFont,&gcv);
166     strGC = vecGC;
167 greg 1.1 } else {
168     gcv.function = GXinvert;
169     vecGC = XCreateGC(theDisplay,gwind,GCFunction,&gcv);
170 greg 1.9 gcv.foreground = xc.green < 32768 ?
171     BlackPixel(theDisplay,DefaultScreen(theDisplay)) :
172     WhitePixel(theDisplay,DefaultScreen(theDisplay)) ;
173     strGC = XCreateGC(theDisplay,gwind,
174     GCForeground|GCBackground|GCFont,&gcv);
175 greg 1.1 }
176     }
177    
178    
179     circle_sources(fp) /* circle sources listed in fp */
180     FILE *fp;
181     {
182     char linbuf[256];
183     int reading = 0;
184     FVECT dir;
185 greg 1.9 double dom, lum;
186 greg 1.1
187     while (fgets(linbuf, sizeof(linbuf), fp) != NULL)
188     if (reading) {
189     if (!strncmp(linbuf, "END", 3)) {
190     XFlush(theDisplay);
191     return;
192     }
193 greg 1.9 if (sscanf(linbuf, "%lf %lf %lf %lf %lf",
194 greg 1.1 &dir[0], &dir[1], &dir[2],
195 greg 1.9 &dom, &lum) != 5)
196 greg 1.1 break;
197     circle(dir, dom);
198 greg 1.9 value(dir, lum);
199 greg 1.1 } else if (!strcmp(linbuf, "BEGIN glare source\n"))
200     reading++;
201    
202     fprintf(stderr, "%s: error reading glare sources\n", progname);
203     exit(1);
204     }
205    
206    
207     circle(dir, dom) /* indicate a solid angle on image */
208     FVECT dir;
209     double dom;
210     {
211     FVECT start, cur;
212     XPoint pt[NSEG+1];
213 greg 1.10 FVECT pp;
214     int ip[2];
215 greg 1.1 register int i;
216    
217     fcross(cur, dir, ourview.vup);
218     if (normalize(cur) == 0.0)
219     goto fail;
220     spinvector(start, dir, cur, acos(1.-dom/(2.*PI)));
221     for (i = 0; i <= NSEG; i++) {
222     spinvector(cur, start, dir, 2.*PI*i/NSEG);
223     cur[0] += ourview.vp[0];
224     cur[1] += ourview.vp[1];
225     cur[2] += ourview.vp[2];
226 greg 1.10 viewloc(pp, &ourview, cur);
227     if (pp[2] <= 0.0)
228 greg 1.1 goto fail;
229 greg 1.10 loc2pix(ip, &pres, pp[0], pp[1]);
230     pt[i].x = ip[0];
231     pt[i].y = ip[1];
232 greg 1.1 }
233     XDrawLines(theDisplay, gwind, vecGC, pt, NSEG+1, CoordModeOrigin);
234     return;
235     fail:
236     fprintf(stderr, "%s: cannot draw source at (%f,%f,%f)\n",
237     progname, dir[0], dir[1], dir[2]);
238 greg 1.9 }
239    
240    
241     value(dir, v) /* print value on image */
242     FVECT dir;
243     double v;
244     {
245     FVECT pos;
246 greg 1.10 FVECT pp;
247     int ip[2];
248 greg 1.9 char buf[32];
249    
250     pos[0] = ourview.vp[0] + dir[0];
251     pos[1] = ourview.vp[1] + dir[1];
252     pos[2] = ourview.vp[2] + dir[2];
253 greg 1.10 viewloc(pp, &ourview, pos);
254     if (pp[2] <= 0.0)
255 greg 1.9 return;
256 greg 1.10 loc2pix(ip, &pres, pp[0], pp[1]);
257 greg 1.9 sprintf(buf, "%.0f", v);
258     XDrawImageString(theDisplay, gwind, strGC,
259 greg 1.10 ip[0], ip[1], buf, strlen(buf));
260 greg 1.1 }