ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/util/xglaresrc.c
Revision: 2.5
Committed: Thu Apr 29 12:58:20 1993 UTC (30 years, 11 months ago) by greg
Content type: text/plain
Branch: MAIN
Changes since 2.4: +12 -4 lines
Log Message:
changed window name in ximage, so had to change it here, too

File Contents

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