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