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