ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/px/pcomb.c
(Generate patch)

Comparing ray/src/px/pcomb.c (file contents):
Revision 1.12 by greg, Tue Apr 23 15:49:53 1991 UTC vs.
Revision 2.47 by greg, Fri Jan 12 00:50:17 2018 UTC

# Line 1 | Line 1
1 /* Copyright (c) 1991 Regents of the University of California */
2
1   #ifndef lint
2 < static char SCCSid[] = "$SunId$ LBL";
2 > static const char       RCSid[] = "$Id$";
3   #endif
6
4   /*
5   *  Combine picture files according to calcomp functions.
6   *
7   *      1/4/89
8   */
9  
10 < #include <stdio.h>
11 <
12 < #include <errno.h>
16 <
10 > #include "platform.h"
11 > #include "standard.h"
12 > #include "paths.h"
13   #include "color.h"
18
14   #include "calcomp.h"
15 + #include "view.h"
16  
17 < #define MAXINP          32              /* maximum number of input files */
17 > #define MAXINP          1024            /* maximum number of input files */
18 > #define WINSIZ          127             /* scanline window size */
19 > #define MIDSCN          ((WINSIZ-1)/2+1)
20  
21   struct {
22 <        char    *name;          /* file name */
22 >        char    *name;          /* file or command name */
23          FILE    *fp;            /* stream pointer */
24 <        COLOR   *scan;          /* input scanline */
24 >        VIEW    vw;             /* view for picture */
25 >        RESOLU  rs;             /* image resolution and orientation */
26 >        float   pa;             /* pixel aspect ratio */
27 >        COLOR   *scan[WINSIZ];  /* input scanline window */
28          COLOR   coef;           /* coefficient */
29 +        COLOR   expos;          /* recorded exposure */
30   }       input[MAXINP];                  /* input pictures */
31  
32   int     nfiles;                         /* number of input files */
33  
34 < char    *vcolin[3] = {"ri", "gi", "bi"};
33 < char    *vcolout[3] = {"ro", "go", "bo"};
34 < #define vbrtin          "li"
35 < #define vbrtout         "lo"
34 > char    ourfmt[LPICFMT+1] = PICFMT;     /* input picture format */
35  
36 < #define vnfiles         "nfiles"
37 < #define vxres           "xres"
38 < #define vyres           "yres"
39 < #define vxpos           "x"
40 < #define vypos           "y"
36 > char    StandardInput[] = "<stdin>";
37 > char    Command[] = "<Command>";
38 > char    vcolin[3][4] = {"ri", "gi", "bi"};
39 > char    vcolout[3][4] = {"ro", "go", "bo"};
40 > char    vbrtin[] = "li";
41 > char    vbrtout[] = "lo";
42 > char    vcolexp[3][4] = {"re", "ge", "be"};
43 > char    vbrtexp[] = "le";
44 > char    vpixaspect[] = "pa";
45  
46 + char    vray[7][4] = {"Ox", "Oy", "Oz", "Dx", "Dy", "Dz", "T"};
47 +
48 + char    vpsize[] = "S";
49 +
50 + char    vnfiles[] = "nfiles";
51 + char    vwhteff[] = "WE";
52 + char    vxmax[] = "xmax";
53 + char    vymax[] = "ymax";
54 + char    vxres[] = "xres";
55 + char    vyres[] = "yres";
56 + char    vxpos[] = "x";
57 + char    vypos[] = "y";
58 +
59   int     nowarn = 0;                     /* no warning messages? */
60  
61 < int     original = 0;                   /* origninal values? */
61 > int     xmax = 0, ymax = 0;             /* input resolution */
62  
63 < int     xres=0, yres=0;                 /* picture resolution */
63 > int     xscan, yscan;                   /* input position */
64  
65 < int     xpos, ypos;                     /* picture position */
65 > int     xres, yres;                     /* output resolution */
66  
67 < int     wrongformat = 0;
67 > int     xpos, ypos;                     /* output position */
68  
69 + char    *progname;                      /* global argv[0] */
70  
71 < tputs(s)                        /* put out string preceded by a tab */
72 < char    *s;
73 < {
57 <        char    fmt[32];
58 <        double  d;
59 <        COLOR   ctmp;
71 > int     echoheader = 1;
72 > int     wrongformat = 0;
73 > int     gotview;
74  
61        if (isformat(s)) {                      /* check format */
62                formatval(fmt, s);
63                wrongformat = strcmp(fmt, COLRFMT);
64        } else if (original && isexpos(s)) {    /* exposure */
65                d = 1.0/exposval(s);
66                scalecolor(input[nfiles].coef, d);
67        } else if (original && iscolcor(s)) {   /* color correction */
68                colcorval(ctmp, s);
69                colval(input[nfiles].coef,RED) /= colval(ctmp,RED);
70                colval(input[nfiles].coef,GRN) /= colval(ctmp,GRN);
71                colval(input[nfiles].coef,BLU) /= colval(ctmp,BLU);
72        } else {                                /* echo unaffected line */
73                putchar('\t');
74                fputs(s, stdout);
75        }
75  
76 < }
76 > static gethfunc headline;
77 > static void checkfile(void);
78 > static double rgb_bright(COLOR  clr);
79 > static double xyz_bright(COLOR  clr);
80 > static void init(void);
81 > static void combine(void);
82 > static void advance(void);
83 > static double l_expos(char      *nam);
84 > static double l_pixaspect(char *nm);
85 > static double l_colin(char      *nam);
86 > static double l_ray(char        *nam);
87 > static double l_psize(char *nm);
88  
89  
90 < main(argc, argv)
91 < int     argc;
92 < char    *argv[];
90 > int
91 > main(
92 >        int     argc,
93 >        char    *argv[]
94 > )
95   {
96 <        extern double   l_redin(), l_grnin(), l_bluin(), l_brtin(), atof();
96 >        int     original;
97          double  f;
98          int     a;
99 <        
100 <        funset(vcolin[RED], 1, '=', l_redin);
101 <        funset(vcolin[GRN], 1, '=', l_grnin);
102 <        funset(vcolin[BLU], 1, '=', l_bluin);
103 <        funset(vbrtin, 1, '=', l_brtin);
104 <        
105 <        for (a = 1; a < argc; a++)
99 >
100 >        SET_DEFAULT_BINARY();
101 >        SET_FILE_BINARY(stdin);
102 >        SET_FILE_BINARY(stdout);
103 >        progname = argv[0];
104 >        esupport |= E_VARIABLE|E_FUNCTION|E_RCONST;
105 >        esupport &= ~(E_OUTCHAN|E_INCHAN);
106 >                                                /* scan options */
107 >        for (a = 1; a < argc; a++) {
108                  if (argv[a][0] == '-')
109                          switch (argv[a][1]) {
96                        case '\0':
97                        case 's':
98                        case 'c':
99                                goto getfiles;
110                          case 'x':
101                                xres = atoi(argv[++a]);
102                                break;
111                          case 'y':
112 <                                yres = atoi(argv[++a]);
113 <                                break;
112 >                                a++;
113 >                                continue;
114                          case 'w':
115                                  nowarn = !nowarn;
116 <                                break;
117 <                        case 'o':
118 <                                original = !original;
119 <                                break;
116 >                                continue;
117 >                        case 'h':
118 >                                echoheader = !echoheader;
119 >                                continue;
120                          case 'f':
113                                fcompile(argv[++a]);
114                                break;
121                          case 'e':
122 <                                scompile(argv[++a], NULL, 0);
123 <                                break;
118 <                        default:
119 <                                goto usage;
122 >                                a++;
123 >                                continue;
124                          }
125 <                else
126 <                        break;
127 < getfiles:
128 <        for (nfiles = 0; nfiles < MAXINP; nfiles++)
125 >                break;
126 >        }
127 >        newheader("RADIANCE", stdout);  /* start header */
128 >        fputnow(stdout);
129 >                                        /* process files */
130 >        for (nfiles = 0; nfiles < MAXINP; nfiles++) {
131                  setcolor(input[nfiles].coef, 1.0, 1.0, 1.0);
132 +                setcolor(input[nfiles].expos, 1.0, 1.0, 1.0);
133 +                input[nfiles].vw = stdview;
134 +                input[nfiles].pa = 1.0;
135 +        }
136          nfiles = 0;
137 +        original = 0;
138          for ( ; a < argc; a++) {
139                  if (nfiles >= MAXINP) {
140                          eputs(argv[0]);
# Line 133 | Line 144 | getfiles:
144                  if (argv[a][0] == '-')
145                          switch (argv[a][1]) {
146                          case '\0':
147 <                                input[nfiles].name = "<stdin>";
147 >                                input[nfiles].name = StandardInput;
148                                  input[nfiles].fp = stdin;
149                                  break;
150 +                        case 'o':
151 +                                original++;
152 +                                continue;
153                          case 's':
154                                  f = atof(argv[++a]);
155                                  scalecolor(input[nfiles].coef, f);
# Line 149 | Line 163 | getfiles:
163                                  goto usage;
164                          }
165                  else {
166 <                        input[nfiles].name = argv[a];
167 <                        input[nfiles].fp = fopen(argv[a], "r");
166 >                        if (argv[a][0] == '!') {
167 >                                input[nfiles].name = Command;
168 >                                input[nfiles].fp = popen(argv[a]+1, "r");
169 >                        } else {
170 >                                input[nfiles].name = argv[a];
171 >                                input[nfiles].fp = fopen(argv[a], "r");
172 >                        }
173                          if (input[nfiles].fp == NULL) {
174                                  perror(argv[a]);
175                                  quit(1);
176                          }
177                  }
178 <                fputs(input[nfiles].name, stdout);
179 <                fputs(":\n", stdout);
180 <                getheader(input[nfiles].fp, tputs, NULL);
181 <                if (wrongformat) {
182 <                        eputs(input[nfiles].name);
183 <                        eputs(": not in Radiance picture format\n");
184 <                        quit(1);
178 >                checkfile();
179 >                if (original) {
180 >                        colval(input[nfiles].coef,RED) /=
181 >                                        colval(input[nfiles].expos,RED);
182 >                        colval(input[nfiles].coef,GRN) /=
183 >                                        colval(input[nfiles].expos,GRN);
184 >                        colval(input[nfiles].coef,BLU) /=
185 >                                        colval(input[nfiles].expos,BLU);
186 >                        setcolor(input[nfiles].expos, 1.0, 1.0, 1.0);
187                  }
167                if (fgetresolu(&xpos, &ypos, input[nfiles].fp) !=
168                                (YMAJOR|YDECR)) {
169                        eputs(input[nfiles].name);
170                        eputs(": bad picture size\n");
171                        quit(1);
172                }
173                if (xres == 0 && yres == 0) {
174                        xres = xpos;
175                        yres = ypos;
176                } else if (xpos != xres || ypos != yres) {
177                        eputs(argv[0]);
178                        eputs(": resolution mismatch\n");
179                        quit(1);
180                }
181                input[nfiles].scan = (COLOR *)emalloc(xres*sizeof(COLOR));
188                  nfiles++;
189 +                original = 0;
190          }
191 +        init();                         /* set constants */
192 +                                        /* go back and get expressions */
193 +        for (a = 1; a < argc; a++) {
194 +                if (argv[a][0] == '-')
195 +                        switch (argv[a][1]) {
196 +                        case 'x':
197 +                                varset(vxres, ':', eval(argv[++a]));
198 +                                continue;
199 +                        case 'y':
200 +                                varset(vyres, ':', eval(argv[++a]));
201 +                                continue;
202 +                        case 'w':
203 +                                continue;
204 +                        case 'h':
205 +                                continue;
206 +                        case 'f':
207 +                                fcompile(argv[++a]);
208 +                                continue;
209 +                        case 'e':
210 +                                scompile(argv[++a], NULL, 0);
211 +                                continue;
212 +                        }
213 +                break;
214 +        }
215 +                                                /* set/get output resolution */
216 +        if (!vardefined(vxres))
217 +                varset(vxres, ':', (double)xmax);
218 +        if (!vardefined(vyres))
219 +                varset(vyres, ':', (double)ymax);
220 +        xres = varvalue(vxres) + .5;
221 +        yres = varvalue(vyres) + .5;
222 +        if (xres <= 0 || yres <= 0) {
223 +                eputs(argv[0]);
224 +                eputs(": illegal output resolution\n");
225 +                quit(1);
226 +        }
227 +                                                /* complete header */
228          printargs(argc, argv, stdout);
229 <        fputformat(COLRFMT, stdout);
229 >        if (strcmp(ourfmt, PICFMT))
230 >                fputformat(ourfmt, stdout);     /* print format if known */
231          putchar('\n');
232 <        fputresolu(YMAJOR|YDECR, xres, yres, stdout);
232 >        fprtresolu(xres, yres, stdout);
233 >                                                /* combine pictures */
234          combine();
235          quit(0);
236   usage:
237          eputs("Usage: ");
238          eputs(argv[0]);
239          eputs(
240 < " [-w][-h][-x xr][-y yr][-e expr][-f file] [ [-s f][-c r g b] pic ..]\n");
240 > " [-w][-h][-x xr][-y yr][-e expr][-f file] [ [-o][-s f][-c r g b] hdr ..]\n");
241          quit(1);
242 +        return 1; /* pro forma return */
243   }
244  
245  
246 < combine()                       /* combine pictures */
246 > static int
247 > headline(                       /* check header line & echo if requested */
248 >        char    *s,
249 >        void    *p
250 > )
251   {
252 +        char    fmt[32];
253 +        double  d;
254 +        COLOR   ctmp;
255 +
256 +        if (isheadid(s))                        /* header id */
257 +                return(0);      /* don't echo */
258 +        if (formatval(fmt, s)) {                /* check format */
259 +                if (globmatch(ourfmt, fmt)) {
260 +                        wrongformat = 0;
261 +                        strcpy(ourfmt, fmt);
262 +                } else
263 +                        wrongformat = globmatch(PICFMT, fmt) ? 1 : -1;
264 +                return(0);      /* don't echo */
265 +        }
266 +        if (isexpos(s)) {                       /* exposure */
267 +                d = exposval(s);
268 +                scalecolor(input[nfiles].expos, d);
269 +        } else if (iscolcor(s)) {               /* color correction */
270 +                colcorval(ctmp, s);
271 +                multcolor(input[nfiles].expos, ctmp);
272 +        } else if (isaspect(s))
273 +                input[nfiles].pa *= aspectval(s);
274 +        else if (isview(s) && sscanview(&input[nfiles].vw, s) > 0)
275 +                gotview++;
276 +
277 +        if (echoheader) {                       /* echo line */
278 +                putchar('\t');
279 +                return(fputs(s, stdout));
280 +        }
281 +        return(0);
282 + }
283 +
284 +
285 + static void
286 + checkfile(void)                 /* ready a file */
287 + {
288 +        int     i;
289 +                                        /* process header */
290 +        gotview = 0;
291 +        if (echoheader) {
292 +                fputs(input[nfiles].name, stdout);
293 +                fputs(":\n", stdout);
294 +        }
295 +        getheader(input[nfiles].fp, headline, NULL);
296 +        if (wrongformat < 0) {
297 +                eputs(input[nfiles].name);
298 +                eputs(": not a Radiance picture\n");
299 +                quit(1);
300 +        }
301 +        if (wrongformat > 0) {
302 +                wputs(input[nfiles].name);
303 +                wputs(": warning -- incompatible picture format\n");
304 +        }
305 +        if (!gotview || setview(&input[nfiles].vw) != NULL)
306 +                input[nfiles].vw.type = 0;
307 +        if (!fgetsresolu(&input[nfiles].rs, input[nfiles].fp)) {
308 +                eputs(input[nfiles].name);
309 +                eputs(": bad picture size\n");
310 +                quit(1);
311 +        }
312 +        if (xmax == 0 && ymax == 0) {
313 +                xmax = scanlen(&input[nfiles].rs);
314 +                ymax = numscans(&input[nfiles].rs);
315 +        } else if (scanlen(&input[nfiles].rs) != xmax ||
316 +                        numscans(&input[nfiles].rs) != ymax) {
317 +                eputs(input[nfiles].name);
318 +                eputs(": resolution mismatch\n");
319 +                quit(1);
320 +        }
321 +                                        /* allocate scanlines */
322 +        for (i = 0; i < WINSIZ; i++)
323 +                input[nfiles].scan[i] = (COLOR *)emalloc(xmax*sizeof(COLOR));
324 + }
325 +
326 +
327 + static double
328 + rgb_bright(
329 +        COLOR  clr
330 + )
331 + {
332 +        return(bright(clr));
333 + }
334 +
335 +
336 + static double
337 + xyz_bright(
338 +        COLOR  clr
339 + )
340 + {
341 +        return(clr[CIEY]);
342 + }
343 +
344 +
345 + double  (*ourbright)() = rgb_bright;
346 +
347 +
348 + static void
349 + init(void)                                      /* perform final setup */
350 + {
351 +        int     i;
352 +                                                /* define constants */
353 +        varset("PI", ':', PI);
354 +        varset(vnfiles, ':', (double)nfiles);
355 +        varset(vxmax, ':', (double)xmax);
356 +        varset(vymax, ':', (double)ymax);
357 +                                                /* set functions */
358 +        for (i = 0; i < 3; i++) {
359 +                funset(vcolexp[i], 1, ':', l_expos);
360 +                funset(vcolin[i], 1, '=', l_colin);
361 +        }
362 +        funset(vbrtexp, 1, ':', l_expos);
363 +        funset(vbrtin, 1, '=', l_colin);
364 +        funset(vpixaspect, 1, ':', l_pixaspect);
365 +        for (i = 0; i < 7; i++)
366 +                funset(vray[i], 1, '=', l_ray);
367 +        funset(vpsize, 1, '=', l_psize);
368 +                                                /* set brightness function */
369 +        if (!strcmp(ourfmt, CIEFMT)) {
370 +                varset(vwhteff, ':', 1.0);
371 +                ourbright = xyz_bright;
372 +        } else
373 +                varset(vwhteff, ':', WHTEFFICACY);
374 + }
375 +
376 +
377 + static void
378 + combine(void)                   /* combine pictures */
379 + {
380          EPNODE  *coldef[3], *brtdef;
381          COLOR   *scanout;
382          double  d;
383 <        register int    i, j;
383 >        int     i, j;
384                                                  /* check defined variables */
385          for (j = 0; j < 3; j++) {
386                  if (vardefined(vcolout[j]))
# Line 213 | Line 392 | combine()                      /* combine pictures */
392                  brtdef = eparse(vbrtout);
393          else
394                  brtdef = NULL;
216                                                /* predefine variables */
217        varset(vnfiles, '=', (double)nfiles);
218        varset(vxres, '=', (double)xres);
219        varset(vyres, '=', (double)yres);
395                                                  /* allocate scanline */
396          scanout = (COLOR *)emalloc(xres*sizeof(COLOR));
397 +                                                /* set input position */
398 +        yscan = ymax+MIDSCN;
399                                                  /* combine files */
400          for (ypos = yres-1; ypos >= 0; ypos--) {
401 <            for (i = 0; i < nfiles; i++)
225 <                    if (freadscan(input[i].scan, xres, input[i].fp) < 0) {
226 <                            eputs(input[i].name);
227 <                            eputs(": read error\n");
228 <                            quit(1);
229 <                    }
401 >            advance();
402              varset(vypos, '=', (double)ypos);
403              for (xpos = 0; xpos < xres; xpos++) {
404 <                for (i = 0; i < nfiles; i++)
233 <                        multcolor(input[i].scan[xpos],input[i].coef);
404 >                xscan = (xpos+.5)*xmax/xres;
405                  varset(vxpos, '=', (double)xpos);
406                  eclock++;
407                  if (brtdef != NULL) {
# Line 241 | Line 412 | combine()                      /* combine pictures */
412                  } else {
413                      for (j = 0; j < 3; j++) {
414                          if (coldef[j] != NULL) {
415 <                                colval(scanout[xpos],j) = evalue(coldef[j]);
415 >                                d = evalue(coldef[j]);
416                          } else {
417 <                            colval(scanout[xpos],j) = 0.0;
417 >                            d = 0.0;
418                              for (i = 0; i < nfiles; i++)
419 <                                colval(scanout[xpos],j) +=
249 <                                        colval(input[i].scan[xpos],j);
419 >                                d += colval(input[i].scan[MIDSCN][xscan],j);
420                          }
421 <                        if (colval(scanout[xpos],j) < 0.0)
422 <                            colval(scanout[xpos],j) = 0.0;
421 >                        if (d < 0.0)
422 >                            d = 0.0;
423 >                        colval(scanout[xpos],j) = d;
424                      }
425                  }
426              }
# Line 258 | Line 429 | combine()                      /* combine pictures */
429                      quit(1);
430              }
431          }
432 <        efree(scanout);
432 >        efree((char *)scanout);
433   }
434  
435  
436 < double
437 < colin(fn, ci)                   /* return color value for picture */
267 < register int    fn, ci;
436 > static void
437 > advance(void)                   /* read in data for next scanline */
438   {
439 <        if (fn == 0)
440 <                return((double)nfiles);
441 <        if (fn < 1 || fn > nfiles) {
439 >        int     ytarget;
440 >        COLOR   *st;
441 >        int     i, j;
442 >
443 >        for (ytarget = (ypos+.5)*ymax/yres; yscan > ytarget; yscan--)
444 >                for (i = 0; i < nfiles; i++) {
445 >                        st = input[i].scan[WINSIZ-1];
446 >                        for (j = WINSIZ-1; j > 0; j--)  /* rotate window */
447 >                                input[i].scan[j] = input[i].scan[j-1];
448 >                        input[i].scan[0] = st;
449 >                        if (yscan <= MIDSCN)            /* hit bottom? */
450 >                                continue;
451 >                        if (freadscan(st, xmax, input[i].fp) < 0) {  /* read */
452 >                                eputs(input[i].name);
453 >                                eputs(": read error\n");
454 >                                quit(1);
455 >                        }
456 >                        for (j = 0; j < xmax; j++)      /* adjust color */
457 >                                multcolor(st[j], input[i].coef);
458 >                }
459 > }
460 >
461 >
462 > static double
463 > l_expos(                        /* return picture exposure */
464 >        char    *nam
465 > )
466 > {
467 >        int     fn, n;
468 >        double  d;
469 >
470 >        d = argument(1);
471 >        if (d <= -0.5 || d >= nfiles+0.5) {
472                  errno = EDOM;
473                  return(0.0);
474          }
475 <        return(colval(input[fn-1].scan[xpos],ci));
475 >        if (d < 0.5)
476 >                return((double)nfiles);
477 >        fn = d - 0.5;
478 >        if (nam == vbrtexp)
479 >                return((*ourbright)(input[fn].expos));
480 >        n = 3;
481 >        while (n--)
482 >                if (nam == vcolexp[n])
483 >                        return(colval(input[fn].expos,n));
484 >        eputs("Bad call to l_expos()!\n");
485 >        quit(1);
486 >        return 1; /* pro forma return */
487   }
488  
489  
490 < double
491 < l_redin()                       /* get red color */
490 > static double
491 > l_pixaspect(char *nm)           /* return pixel aspect ratio */
492   {
493 <        return(colin((int)(argument(1)+.5), RED));
493 >        int     fn;
494 >        double  d;
495 >
496 >        d = argument(1);
497 >        if (d <= -0.5 || d >= nfiles+0.5) {
498 >                errno = EDOM;
499 >                return(0.0);
500 >        }
501 >        if (d < 0.5)
502 >                return((double)nfiles);
503 >        fn = d - 0.5;
504 >        return(input[fn].pa);
505   }
506  
507  
508 < double
509 < l_grnin()                       /* get green color */
508 > static double
509 > l_colin(                        /* return color value for picture */
510 >        char    *nam
511 > )
512   {
513 <        return(colin((int)(argument(1)+.5), GRN));
513 >        int     fn;
514 >        int     n, xoff, yoff;
515 >        double  d;
516 >
517 >        d = argument(1);
518 >        if (d <= -0.5 || d >= nfiles+0.5) {
519 >                errno = EDOM;
520 >                return(0.0);
521 >        }
522 >        if (d < 0.5)
523 >                return((double)nfiles);
524 >        fn = d - 0.5;
525 >        xoff = yoff = 0;
526 >        n = nargum();
527 >        if (n >= 2) {
528 >                d = argument(2);
529 >                if (d < 0.0) {
530 >                        xoff = d-.5;
531 >                        if (xscan+xoff < 0)
532 >                                xoff = -xscan;
533 >                } else {
534 >                        xoff = d+.5;
535 >                        if (xscan+xoff >= xmax)
536 >                                xoff = xmax-1-xscan;
537 >                }
538 >        }
539 >        if (n >= 3) {
540 >                d = argument(3);
541 >                if (d < 0.0) {
542 >                        yoff = d-.5;
543 >                        if (yoff+MIDSCN < 0)
544 >                                yoff = -MIDSCN;
545 >                        if (yscan+yoff < 0)
546 >                                yoff = -yscan;
547 >                } else {
548 >                        yoff = d+.5;
549 >                        if (yoff+MIDSCN >= WINSIZ)
550 >                                yoff = WINSIZ-1-MIDSCN;
551 >                        if (yscan+yoff >= ymax)
552 >                                yoff = ymax-1-yscan;
553 >                }
554 >        }
555 >        if (nam == vbrtin)
556 >                return((*ourbright)(input[fn].scan[MIDSCN+yoff][xscan+xoff]));
557 >        n = 3;
558 >        while (n--)
559 >            if (nam == vcolin[n])
560 >                return(colval(input[fn].scan[MIDSCN+yoff][xscan+xoff],n));
561 >        eputs("Bad call to l_colin()!\n");
562 >        quit(1);
563 >        return 1; /* pro forma return */
564   }
565  
566  
567 < double
568 < l_bluin()                       /* get blue color */
567 > static double
568 > l_ray(          /* return ray origin or direction */
569 >        char    *nam
570 > )
571   {
572 <        return(colin((int)(argument(1)+.5), BLU));
572 >        static unsigned long    ltick[MAXINP];
573 >        static FVECT    lorg[MAXINP], ldir[MAXINP];
574 >        static double   ldist[MAXINP];
575 >        RREAL   loc[2];
576 >        double  d;
577 >        int     fn;
578 >        int     i;
579 >
580 >        d = argument(1);
581 >        if (d <= -0.5 || d >= nfiles+0.5) {
582 >                errno = EDOM;
583 >                return(0.0);
584 >        }
585 >        if (d < 0.5)
586 >                return((double)nfiles);
587 >        fn = d - 0.5;
588 >        if (ltick[fn] != eclock) {              /* need to compute? */
589 >                lorg[fn][0] = lorg[fn][1] = lorg[fn][2] = 0.0;
590 >                ldir[fn][0] = ldir[fn][1] = ldir[fn][2] = 0.0;
591 >                ldist[fn] = -1.0;
592 >                if (input[fn].vw.type == 0)
593 >                        errno = EDOM;
594 >                else {
595 >                        pix2loc(loc, &input[fn].rs, xscan, ymax-1-yscan);
596 >                        ldist[fn] = viewray(lorg[fn], ldir[fn],
597 >                                        &input[fn].vw, loc[0], loc[1]);
598 >                }
599 >                ltick[fn] = eclock;
600 >        }
601 >        if (nam == vray[i=6])
602 >                return(ldist[fn]);
603 >        while (i--)
604 >                if (nam == vray[i])
605 >                        return(i < 3 ? lorg[fn][i] : ldir[fn][i-3]);
606 >        eputs("Bad call to l_ray()!\n");
607 >        quit(1);
608 >        return 1; /* pro forma return */
609   }
610  
611  
612 < double
613 < l_brtin()                       /* get brightness value */
612 > static double
613 > l_psize(char *nm)               /* compute pixel size in steradians */
614   {
615 <        register int    fn;
615 >        static unsigned long    ltick[MAXINP];
616 >        static double   psize[MAXINP];
617 >        FVECT   dir0, org, dirx, diry;
618 >        RREAL   locx[2], locy[2];
619 >        double  d;
620 >        int     fn;
621 >        int     i;
622  
623 <        fn = argument(1)+.5;
624 <        if (fn == 0)
307 <                return((double)nfiles);
308 <        if (fn < 1 || fn > nfiles) {
623 >        d = argument(1);
624 >        if (d <= -0.5 || d >= nfiles+0.5) {
625                  errno = EDOM;
626                  return(0.0);
627          }
628 <        return(bright(input[fn-1].scan[xpos]));
628 >        if (d < 0.5)
629 >                return((double)nfiles);
630 >        fn = d - 0.5;
631 >        if (ltick[fn] != eclock) {              /* need to compute? */
632 >                psize[fn] = 0.0;
633 >                if (input[fn].vw.type == 0)
634 >                        errno = EDOM;
635 >                else if (input[fn].vw.type != VT_PAR &&
636 >                                funvalue(vray[6], 1, &d) >= -FTINY) {
637 >                        for (i = 0; i < 3; i++)
638 >                                dir0[i] = funvalue(vray[3+i], 1, &d);
639 >                        pix2loc(locx, &input[fn].rs, xscan+1, ymax-1-yscan);
640 >                        pix2loc(locy, &input[fn].rs, xscan, ymax-yscan);
641 >                        if (viewray(org, dirx, &input[fn].vw,
642 >                                        locx[0], locx[1]) >= -FTINY &&
643 >                                        viewray(org, diry, &input[fn].vw,
644 >                                        locy[0], locy[1]) >= -FTINY) {
645 >                                                /* approximate solid angle */
646 >                                for (i = 0; i < 3; i++) {
647 >                                        dirx[i] -= dir0[i];
648 >                                        diry[i] -= dir0[i];
649 >                                }
650 >                                fcross(dir0, dirx, diry);
651 >                                psize[fn] = VLEN(dir0);
652 >                        }
653 >                }
654 >                ltick[fn] = eclock;
655 >        }
656 >        return(psize[fn]);
657   }
658  
659  
660 < wputs(msg)
661 < char    *msg;
660 > extern void
661 > wputs(char      *msg)
662   {
663          if (!nowarn)
664                  eputs(msg);
665   }
666  
667  
668 < eputs(msg)
669 < char    *msg;
668 > extern void
669 > eputs(char *msg)
670   {
671          fputs(msg, stderr);
672   }
673  
674  
675 < quit(code)
676 < int     code;
675 > extern void
676 > quit(int code)          /* exit gracefully */
677   {
678 +        int  i;
679 +                                /* close input files */
680 +        for (i = 0; i < nfiles; i++)
681 +                if (input[i].name == Command)
682 +                        pclose(input[i].fp);
683 +                else
684 +                        fclose(input[i].fp);
685          exit(code);
686   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines