ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/px/pcomb.c
Revision: 2.60
Committed: Thu Nov 7 20:07:08 2024 UTC (5 months, 3 weeks ago) by greg
Content type: text/plain
Branch: MAIN
CVS Tags: HEAD
Changes since 2.59: +4 -2 lines
Log Message:
fix(pcomb): Added default RGB format string if unknown

File Contents

# User Rev Content
1 greg 1.1 #ifndef lint
2 greg 2.60 static const char RCSid[] = "$Id: pcomb.c,v 2.59 2024/02/23 03:47:57 greg Exp $";
3 greg 1.1 #endif
4     /*
5     * Combine picture files according to calcomp functions.
6     *
7     * 1/4/89
8     */
9    
10 greg 2.28 #include "platform.h"
11 greg 2.33 #include "standard.h"
12 greg 2.45 #include "paths.h"
13 greg 1.1 #include "color.h"
14     #include "calcomp.h"
15 greg 2.10 #include "view.h"
16    
17 greg 2.44 #define MAXINP 1024 /* maximum number of input files */
18 greg 2.46 #define WINSIZ 127 /* scanline window size */
19 greg 2.13 #define MIDSCN ((WINSIZ-1)/2+1)
20 greg 1.1
21     struct {
22 greg 1.19 char *name; /* file or command name */
23 greg 1.1 FILE *fp; /* stream pointer */
24 greg 2.10 VIEW vw; /* view for picture */
25     RESOLU rs; /* image resolution and orientation */
26 greg 2.57 int infloat; /* input is floating point (#comp)? */
27 greg 2.17 float pa; /* pixel aspect ratio */
28 greg 1.14 COLOR *scan[WINSIZ]; /* input scanline window */
29 greg 1.3 COLOR coef; /* coefficient */
30 greg 1.17 COLOR expos; /* recorded exposure */
31 greg 1.1 } input[MAXINP]; /* input pictures */
32    
33     int nfiles; /* number of input files */
34    
35 greg 2.52 VIEW *commvp = NULL; /* common view parameters */
36 greg 2.49
37 greg 2.56 char ourfmt[MAXFMTLEN] = PICFMT; /* input picture format */
38 greg 2.57 int outfloat = 0; /* #component float output? */
39 greg 2.16
40 greg 2.31 char StandardInput[] = "<stdin>";
41 greg 2.6 char Command[] = "<Command>";
42 greg 1.21 char vcolin[3][4] = {"ri", "gi", "bi"};
43     char vcolout[3][4] = {"ro", "go", "bo"};
44 greg 1.15 char vbrtin[] = "li";
45     char vbrtout[] = "lo";
46 greg 1.21 char vcolexp[3][4] = {"re", "ge", "be"};
47 greg 1.17 char vbrtexp[] = "le";
48 greg 2.18 char vpixaspect[] = "pa";
49 greg 1.1
50 greg 2.18 char vray[7][4] = {"Ox", "Oy", "Oz", "Dx", "Dy", "Dz", "T"};
51 greg 2.10
52 greg 2.17 char vpsize[] = "S";
53    
54 greg 1.18 char vnfiles[] = "nfiles";
55 greg 2.18 char vwhteff[] = "WE";
56 greg 1.18 char vxmax[] = "xmax";
57     char vymax[] = "ymax";
58     char vxres[] = "xres";
59     char vyres[] = "yres";
60     char vxpos[] = "x";
61     char vypos[] = "y";
62 greg 1.1
63     int nowarn = 0; /* no warning messages? */
64    
65 greg 1.18 int xmax = 0, ymax = 0; /* input resolution */
66 greg 1.9
67 greg 1.18 int xscan, yscan; /* input position */
68 greg 1.1
69 greg 1.18 int xres, yres; /* output resolution */
70 greg 1.1
71 greg 1.18 int xpos, ypos; /* output position */
72    
73 greg 2.10 char *progname; /* global argv[0] */
74    
75 greg 2.39 int echoheader = 1;
76 greg 1.10 int wrongformat = 0;
77 greg 2.10 int gotview;
78 greg 1.1
79 greg 1.10
80 greg 2.39 static gethfunc headline;
81 greg 2.53 static void checkfile(int orig);
82 schorsch 2.32 static double rgb_bright(COLOR clr);
83     static double xyz_bright(COLOR clr);
84     static void init(void);
85     static void combine(void);
86     static void advance(void);
87     static double l_expos(char *nam);
88     static double l_pixaspect(char *nm);
89     static double l_colin(char *nam);
90     static double l_ray(char *nam);
91     static double l_psize(char *nm);
92    
93    
94     int
95     main(
96     int argc,
97     char *argv[]
98     )
99 greg 1.1 {
100 greg 1.18 int original;
101 greg 1.3 double f;
102 schorsch 2.32 int a;
103 greg 2.47
104 schorsch 2.23 SET_DEFAULT_BINARY();
105     SET_FILE_BINARY(stdin);
106     SET_FILE_BINARY(stdout);
107 greg 2.10 progname = argv[0];
108 greg 2.47 esupport |= E_VARIABLE|E_FUNCTION|E_RCONST;
109     esupport &= ~(E_OUTCHAN|E_INCHAN);
110 greg 1.15 /* scan options */
111     for (a = 1; a < argc; a++) {
112 greg 1.1 if (argv[a][0] == '-')
113     switch (argv[a][1]) {
114     case 'x':
115     case 'y':
116 greg 1.18 a++;
117 greg 1.15 continue;
118 greg 1.1 case 'w':
119     nowarn = !nowarn;
120 greg 1.15 continue;
121 greg 2.39 case 'h':
122     echoheader = !echoheader;
123     continue;
124 greg 1.1 case 'f':
125 greg 2.57 if (argv[a][2]) {
126     outfloat = (argv[a][2] == 'f');
127     continue;
128     }
129     /* fall through */
130 greg 1.1 case 'e':
131 greg 1.16 a++;
132 greg 1.15 continue;
133 greg 1.1 }
134 greg 1.15 break;
135     }
136 greg 2.14 newheader("RADIANCE", stdout); /* start header */
137 greg 2.22 fputnow(stdout);
138 greg 1.15 /* process files */
139 greg 1.17 for (nfiles = 0; nfiles < MAXINP; nfiles++) {
140 greg 1.3 setcolor(input[nfiles].coef, 1.0, 1.0, 1.0);
141 greg 1.17 setcolor(input[nfiles].expos, 1.0, 1.0, 1.0);
142 schorsch 2.26 input[nfiles].vw = stdview;
143 greg 2.17 input[nfiles].pa = 1.0;
144 greg 1.17 }
145 greg 1.1 nfiles = 0;
146 greg 1.18 original = 0;
147 greg 1.1 for ( ; a < argc; a++) {
148     if (nfiles >= MAXINP) {
149     eputs(argv[0]);
150     eputs(": too many picture files\n");
151     quit(1);
152     }
153 greg 1.3 if (argv[a][0] == '-')
154     switch (argv[a][1]) {
155     case '\0':
156 greg 2.31 input[nfiles].name = StandardInput;
157 greg 1.3 input[nfiles].fp = stdin;
158     break;
159 greg 1.17 case 'o':
160     original++;
161 greg 1.22 continue;
162 greg 1.3 case 's':
163     f = atof(argv[++a]);
164     scalecolor(input[nfiles].coef, f);
165     continue;
166     case 'c':
167     colval(input[nfiles].coef,RED)*=atof(argv[++a]);
168     colval(input[nfiles].coef,GRN)*=atof(argv[++a]);
169     colval(input[nfiles].coef,BLU)*=atof(argv[++a]);
170     continue;
171     default:
172     goto usage;
173     }
174     else {
175 greg 2.2 if (argv[a][0] == '!') {
176 greg 2.6 input[nfiles].name = Command;
177 greg 2.2 input[nfiles].fp = popen(argv[a]+1, "r");
178     } else {
179     input[nfiles].name = argv[a];
180     input[nfiles].fp = fopen(argv[a], "r");
181     }
182 greg 1.1 if (input[nfiles].fp == NULL) {
183 greg 1.6 perror(argv[a]);
184 greg 1.1 quit(1);
185     }
186     }
187 greg 2.53 checkfile(original);
188 greg 1.18 nfiles++;
189 greg 1.17 original = 0;
190 greg 1.15 }
191 greg 1.18 init(); /* set constants */
192 greg 1.15 /* go back and get expressions */
193     for (a = 1; a < argc; a++) {
194 greg 2.50 char *fpath;
195 greg 1.15 if (argv[a][0] == '-')
196     switch (argv[a][1]) {
197     case 'x':
198 greg 1.21 varset(vxres, ':', eval(argv[++a]));
199 greg 1.18 continue;
200 greg 1.15 case 'y':
201 greg 1.21 varset(vyres, ':', eval(argv[++a]));
202 greg 1.16 continue;
203 greg 1.15 case 'w':
204     continue;
205 greg 2.40 case 'h':
206     continue;
207 greg 1.15 case 'f':
208 greg 2.57 if (argv[a][2])
209     continue;
210 greg 2.50 fpath = getpath(argv[++a], getrlibpath(), 0);
211     if (fpath == NULL) {
212     eputs(argv[0]);
213     eputs(": cannot find file '");
214     eputs(argv[a]);
215     eputs("'\n");
216     quit(1);
217     }
218     fcompile(fpath);
219 greg 1.15 continue;
220     case 'e':
221     scompile(argv[++a], NULL, 0);
222     continue;
223     }
224     break;
225     }
226 greg 2.48 /* get output resolution */
227 greg 1.18 xres = varvalue(vxres) + .5;
228     yres = varvalue(vyres) + .5;
229     if (xres <= 0 || yres <= 0) {
230     eputs(argv[0]);
231     eputs(": illegal output resolution\n");
232     quit(1);
233     }
234 greg 2.57 if (!vardefined(vbrtout)) /* single or 3-channel? */
235     outfloat *= 3;
236    
237     printargs(argc, argv, stdout); /* complete header */
238 greg 2.52 if (commvp != NULL) {
239 greg 2.49 fputs(VIEWSTR, stdout);
240 greg 2.52 fprintview(commvp, stdout);
241 greg 2.49 fputc('\n', stdout);
242     }
243 greg 2.60 if (outfloat) { /* print output format */
244 greg 2.57 printf("NROWS=%d\nNCOLS=%d\n", yres, xres);
245     fputncomp(outfloat, stdout);
246     fputendian(stdout);
247     fputformat("float", stdout);
248     } else if (strcmp(ourfmt, PICFMT))
249     fputformat(ourfmt, stdout);
250 greg 2.60 else
251     fputformat(COLRFMT, stdout);
252 greg 2.57 fputc('\n', stdout); /* end header */
253     if (!outfloat)
254     fprtresolu(xres, yres, stdout);
255    
256 greg 2.59 doptimize(1); /* optimize definitions */
257 greg 2.57 combine(); /* combine pictures */
258 greg 1.15 quit(0);
259     usage:
260     eputs("Usage: ");
261     eputs(argv[0]);
262     eputs(
263 greg 2.57 " [-w][-h][-ff][-x xr][-y yr][-e expr][-f file] [ [-o][-s f][-c r g b] hdr ..]\n");
264 greg 1.15 quit(1);
265 schorsch 2.32 return 1; /* pro forma return */
266 greg 1.15 }
267    
268    
269 schorsch 2.30 static int
270 greg 2.39 headline( /* check header line & echo if requested */
271 schorsch 2.30 char *s,
272     void *p
273     )
274 greg 1.15 {
275 greg 2.53 int orig = *(int *)p;
276 greg 2.51 char fmt[MAXFMTLEN];
277 greg 1.15 double d;
278 greg 2.57 int bigend;
279 greg 1.15 COLOR ctmp;
280    
281 greg 2.14 if (isheadid(s)) /* header id */
282 gwlarson 2.21 return(0); /* don't echo */
283 greg 2.14 if (formatval(fmt, s)) { /* check format */
284 greg 2.16 if (globmatch(ourfmt, fmt)) {
285     wrongformat = 0;
286     strcpy(ourfmt, fmt);
287 greg 2.57 } else if (!strcmp("float", fmt))
288     input[nfiles].infloat *= -1;
289     else
290 greg 2.22 wrongformat = globmatch(PICFMT, fmt) ? 1 : -1;
291 gwlarson 2.21 return(0); /* don't echo */
292 greg 1.17 }
293 greg 2.57 if ((bigend = isbigendian(s)) >= 0) {
294     if (bigend != nativebigendian()) {
295     eputs(input[nfiles].name);
296     eputs(": unsupported input byte ordering\n");
297     quit(1);
298     }
299     return(0); /* don't echo */
300     }
301     if (!strncmp("NROWS=", s, 6)) { /* X-resolution */
302     input[nfiles].rs.yr = atoi(s+6);
303     return(0); /* don't echo */
304     }
305     if (!strncmp("NCOLS=", s, 6)) { /* Y-resolution */
306     input[nfiles].rs.xr = atoi(s+6);
307     return(0); /* don't echo */
308     }
309     if (isncomp(s)) /* # components */
310     input[nfiles].infloat *= ncompval(s);
311    
312 greg 2.53 if (orig) { /* undo exposure? */
313     if (isexpos(s)) {
314     d = 1./exposval(s);
315     scalecolor(input[nfiles].coef, d);
316     return(0); /* don't echo */
317     }
318     if (iscolcor(s)) {
319     int i;
320     colcorval(ctmp, s);
321     for (i = 3; i--; )
322     colval(input[nfiles].coef,i) /= colval(ctmp,i);
323     return(0); /* don't echo */
324     }
325     } else if (isexpos(s)) { /* record exposure? */
326 greg 1.17 d = exposval(s);
327     scalecolor(input[nfiles].expos, d);
328 greg 2.53 } else if (iscolcor(s)) {
329 greg 1.15 colcorval(ctmp, s);
330 greg 1.17 multcolor(input[nfiles].expos, ctmp);
331 greg 2.54 }
332     if (isaspect(s))
333 greg 2.17 input[nfiles].pa *= aspectval(s);
334 greg 2.18 else if (isview(s) && sscanview(&input[nfiles].vw, s) > 0)
335 greg 2.10 gotview++;
336 greg 2.39
337     if (echoheader) { /* echo line */
338     putchar('\t');
339     return(fputs(s, stdout));
340     }
341     return(0);
342 greg 1.16 }
343 greg 1.15
344 greg 1.16
345 schorsch 2.32 static void
346 greg 2.53 checkfile(int orig) /* ready a file */
347 greg 1.16 {
348 greg 2.47 int i;
349 greg 1.16 /* process header */
350 greg 2.10 gotview = 0;
351 greg 2.57 input[nfiles].infloat = -1;
352     input[nfiles].rs.rt = PIXSTANDARD;
353     input[nfiles].rs.xr = input[nfiles].rs.yr = 0;
354 greg 2.39 if (echoheader) {
355     fputs(input[nfiles].name, stdout);
356     fputs(":\n", stdout);
357     }
358 greg 2.53 getheader(input[nfiles].fp, headline, &orig);
359 greg 2.57
360     if (input[nfiles].infloat <= 0)
361     input[nfiles].infloat = 0;
362     else if ((input[nfiles].infloat != 3) &
363     (input[nfiles].infloat != 1)) {
364     eputs(input[nfiles].name);
365     eputs(": unsupported number of components\n");
366     quit(1);
367     }
368 greg 2.22 if (wrongformat < 0) {
369 greg 1.16 eputs(input[nfiles].name);
370 greg 2.57 eputs(": not a Radiance picture or float matrix\n");
371 greg 1.16 quit(1);
372     }
373 greg 2.22 if (wrongformat > 0) {
374     wputs(input[nfiles].name);
375     wputs(": warning -- incompatible picture format\n");
376     }
377 greg 2.10 if (!gotview || setview(&input[nfiles].vw) != NULL)
378     input[nfiles].vw.type = 0;
379 greg 2.52 else if (commvp == NULL)
380     commvp = &input[nfiles].vw;
381 greg 2.57 if ((input[nfiles].rs.xr <= 0) | (input[nfiles].rs.yr <= 0) &&
382     !fgetsresolu(&input[nfiles].rs, input[nfiles].fp)) {
383 greg 1.16 eputs(input[nfiles].name);
384     eputs(": bad picture size\n");
385     quit(1);
386     }
387 greg 1.18 if (xmax == 0 && ymax == 0) {
388 greg 2.10 xmax = scanlen(&input[nfiles].rs);
389     ymax = numscans(&input[nfiles].rs);
390     } else if (scanlen(&input[nfiles].rs) != xmax ||
391     numscans(&input[nfiles].rs) != ymax) {
392 greg 1.16 eputs(input[nfiles].name);
393     eputs(": resolution mismatch\n");
394     quit(1);
395     }
396     /* allocate scanlines */
397     for (i = 0; i < WINSIZ; i++)
398 greg 1.18 input[nfiles].scan[i] = (COLOR *)emalloc(xmax*sizeof(COLOR));
399 greg 1.15 }
400    
401    
402 schorsch 2.32 static double
403     rgb_bright(
404     COLOR clr
405     )
406 greg 2.16 {
407     return(bright(clr));
408     }
409    
410    
411 schorsch 2.32 static double
412     xyz_bright(
413     COLOR clr
414     )
415 greg 2.16 {
416     return(clr[CIEY]);
417     }
418    
419    
420     double (*ourbright)() = rgb_bright;
421    
422    
423 schorsch 2.32 static void
424     init(void) /* perform final setup */
425 greg 1.15 {
426 greg 2.47 int i;
427 greg 1.15 /* define constants */
428 greg 2.19 varset("PI", ':', PI);
429 greg 1.15 varset(vnfiles, ':', (double)nfiles);
430 greg 1.18 varset(vxmax, ':', (double)xmax);
431     varset(vymax, ':', (double)ymax);
432 greg 1.15 /* set functions */
433     for (i = 0; i < 3; i++) {
434 greg 1.17 funset(vcolexp[i], 1, ':', l_expos);
435 greg 1.15 funset(vcolin[i], 1, '=', l_colin);
436     }
437 greg 1.17 funset(vbrtexp, 1, ':', l_expos);
438 greg 1.15 funset(vbrtin, 1, '=', l_colin);
439 greg 2.18 funset(vpixaspect, 1, ':', l_pixaspect);
440     for (i = 0; i < 7; i++)
441 greg 2.10 funset(vray[i], 1, '=', l_ray);
442 greg 2.17 funset(vpsize, 1, '=', l_psize);
443 greg 2.16 /* set brightness function */
444 greg 2.18 if (!strcmp(ourfmt, CIEFMT)) {
445     varset(vwhteff, ':', 1.0);
446 greg 2.16 ourbright = xyz_bright;
447 greg 2.18 } else
448     varset(vwhteff, ':', WHTEFFICACY);
449 greg 2.48 /* these may be overridden */
450     varset(vxres, ':', (double)xmax);
451     varset(vyres, ':', (double)ymax);
452 greg 1.1 }
453    
454    
455 schorsch 2.32 static void
456     combine(void) /* combine pictures */
457 greg 1.1 {
458 greg 1.8 EPNODE *coldef[3], *brtdef;
459 greg 2.58 int set_x, set_y;
460 greg 1.1 COLOR *scanout;
461 greg 1.8 double d;
462 greg 2.47 int i, j;
463 greg 1.1 /* check defined variables */
464 greg 1.4 for (j = 0; j < 3; j++) {
465     if (vardefined(vcolout[j]))
466     coldef[j] = eparse(vcolout[j]);
467     else
468     coldef[j] = NULL;
469     }
470 greg 1.8 if (vardefined(vbrtout))
471     brtdef = eparse(vbrtout);
472     else
473     brtdef = NULL;
474 greg 2.58 /* what to set */
475     set_x = varlookup(vxpos) != NULL && !vardefined(vxpos);
476     set_y = varlookup(vypos) != NULL && !vardefined(vypos);
477 greg 1.1 /* allocate scanline */
478     scanout = (COLOR *)emalloc(xres*sizeof(COLOR));
479 greg 1.18 /* set input position */
480     yscan = ymax+MIDSCN;
481 greg 1.1 /* combine files */
482     for (ypos = yres-1; ypos >= 0; ypos--) {
483 greg 1.14 advance();
484 greg 2.58 if (set_y) varset(vypos, '=', (double)ypos);
485 greg 1.8 for (xpos = 0; xpos < xres; xpos++) {
486 greg 2.42 xscan = (xpos+.5)*xmax/xres;
487 greg 2.58 if (set_x) varset(vxpos, '=', (double)xpos);
488 greg 1.8 eclock++;
489     if (brtdef != NULL) {
490     d = evalue(brtdef);
491 greg 2.57 d *= (outfloat > 0) | (d >= 0);
492 greg 1.8 setcolor(scanout[xpos], d, d, d);
493     } else {
494     for (j = 0; j < 3; j++) {
495     if (coldef[j] != NULL) {
496 greg 1.13 d = evalue(coldef[j]);
497 greg 1.8 } else {
498 greg 1.13 d = 0.0;
499 greg 1.8 for (i = 0; i < nfiles; i++)
500 greg 1.18 d += colval(input[i].scan[MIDSCN][xscan],j);
501 greg 1.1 }
502 greg 2.57 d *= (outfloat > 0) | (d >= 0);
503 greg 1.13 colval(scanout[xpos],j) = d;
504 greg 1.8 }
505 greg 1.1 }
506 greg 1.8 }
507 greg 2.57 switch (outfloat) {
508     case 3: /* writing out float triplets */
509     if (fwrite(scanout, sizeof(float)*3, xres, stdout) != xres)
510     goto writerr;
511     break;
512     case 1: /* writing out gray float values */
513     for (xpos = 0; xpos < xres; xpos++)
514     if (putbinary(&scanout[xpos][CIEY],
515     sizeof(float), 1, stdout) != 1)
516     goto writerr;
517     break;
518     default: /* writing out Radiance picture */
519     if (fwritescan(scanout, xres, stdout) < 0)
520     goto writerr;
521     break;
522 greg 1.8 }
523 greg 1.1 }
524 greg 2.22 efree((char *)scanout);
525 greg 2.57 return;
526     writerr:
527     perror("write error");
528     quit(1);
529 greg 1.1 }
530    
531    
532 schorsch 2.32 static void
533     advance(void) /* read in data for next scanline */
534 greg 1.14 {
535 greg 1.18 int ytarget;
536 greg 2.47 COLOR *st;
537     int i, j;
538 greg 1.14
539 greg 2.42 for (ytarget = (ypos+.5)*ymax/yres; yscan > ytarget; yscan--)
540 greg 1.18 for (i = 0; i < nfiles; i++) {
541     st = input[i].scan[WINSIZ-1];
542     for (j = WINSIZ-1; j > 0; j--) /* rotate window */
543     input[i].scan[j] = input[i].scan[j-1];
544     input[i].scan[0] = st;
545     if (yscan <= MIDSCN) /* hit bottom? */
546     continue;
547 greg 2.57 /* read scanline */
548     if (input[i].infloat) {
549     if (fread(st, sizeof(float)*input[i].infloat,
550     xmax, input[i].fp) != xmax)
551     goto readerr;
552     if (input[i].infloat == 1) {
553     const COLORV *f = st[0] + xmax;
554     int x = xmax;
555     while (x-- > 0)
556     st[x][BLU] = st[x][GRN] =
557     st[x][RED] = *--f;
558     }
559     } else if (freadscan(st, xmax, input[i].fp) < 0)
560     goto readerr;
561 greg 2.37 for (j = 0; j < xmax; j++) /* adjust color */
562     multcolor(st[j], input[i].coef);
563 greg 1.14 }
564 greg 2.57 return;
565     readerr:
566     eputs(input[i].name);
567     eputs(": read error\n");
568     quit(1);
569 greg 1.14 }
570    
571    
572 schorsch 2.32 static double
573     l_expos( /* return picture exposure */
574 greg 2.42 char *nam
575 schorsch 2.32 )
576 greg 1.1 {
577 greg 2.42 int fn, n;
578     double d;
579 greg 1.14
580 greg 2.42 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 greg 1.17 if (nam == vbrtexp)
589 greg 2.16 return((*ourbright)(input[fn].expos));
590 greg 1.15 n = 3;
591     while (n--)
592 greg 1.17 if (nam == vcolexp[n])
593     return(colval(input[fn].expos,n));
594     eputs("Bad call to l_expos()!\n");
595 greg 1.15 quit(1);
596 schorsch 2.32 return 1; /* pro forma return */
597 greg 1.15 }
598    
599    
600 schorsch 2.32 static double
601 greg 2.27 l_pixaspect(char *nm) /* return pixel aspect ratio */
602 greg 2.18 {
603 greg 2.42 int fn;
604     double d;
605 greg 2.18
606 greg 2.42 d = argument(1);
607     if (d <= -0.5 || d >= nfiles+0.5) {
608     errno = EDOM;
609     return(0.0);
610     }
611     if (d < 0.5)
612     return((double)nfiles);
613     fn = d - 0.5;
614 greg 2.18 return(input[fn].pa);
615     }
616    
617    
618 schorsch 2.32 static double
619     l_colin( /* return color value for picture */
620 greg 2.47 char *nam
621 schorsch 2.32 )
622 greg 1.15 {
623     int fn;
624 greg 2.47 int n, xoff, yoff;
625 greg 1.15 double d;
626    
627 greg 2.42 d = argument(1);
628     if (d <= -0.5 || d >= nfiles+0.5) {
629 greg 1.15 errno = EDOM;
630     return(0.0);
631     }
632 greg 2.42 if (d < 0.5)
633     return((double)nfiles);
634     fn = d - 0.5;
635 greg 1.14 xoff = yoff = 0;
636     n = nargum();
637     if (n >= 2) {
638     d = argument(2);
639     if (d < 0.0) {
640     xoff = d-.5;
641 greg 1.18 if (xscan+xoff < 0)
642     xoff = -xscan;
643 greg 1.14 } else {
644     xoff = d+.5;
645 greg 1.18 if (xscan+xoff >= xmax)
646     xoff = xmax-1-xscan;
647 greg 1.14 }
648     }
649     if (n >= 3) {
650     d = argument(3);
651     if (d < 0.0) {
652     yoff = d-.5;
653     if (yoff+MIDSCN < 0)
654     yoff = -MIDSCN;
655 greg 1.18 if (yscan+yoff < 0)
656     yoff = -yscan;
657 greg 1.14 } else {
658     yoff = d+.5;
659     if (yoff+MIDSCN >= WINSIZ)
660     yoff = WINSIZ-1-MIDSCN;
661 greg 1.18 if (yscan+yoff >= ymax)
662     yoff = ymax-1-yscan;
663 greg 1.14 }
664     }
665 greg 1.15 if (nam == vbrtin)
666 greg 2.16 return((*ourbright)(input[fn].scan[MIDSCN+yoff][xscan+xoff]));
667 greg 1.15 n = 3;
668     while (n--)
669     if (nam == vcolin[n])
670 greg 1.18 return(colval(input[fn].scan[MIDSCN+yoff][xscan+xoff],n));
671 greg 1.15 eputs("Bad call to l_colin()!\n");
672 greg 2.10 quit(1);
673 schorsch 2.32 return 1; /* pro forma return */
674 greg 2.10 }
675    
676    
677 schorsch 2.32 static double
678     l_ray( /* return ray origin or direction */
679 greg 2.47 char *nam
680 schorsch 2.32 )
681 greg 2.10 {
682 greg 2.15 static unsigned long ltick[MAXINP];
683 greg 2.10 static FVECT lorg[MAXINP], ldir[MAXINP];
684 greg 2.18 static double ldist[MAXINP];
685 schorsch 2.25 RREAL loc[2];
686 greg 2.42 double d;
687 greg 2.10 int fn;
688 greg 2.47 int i;
689 greg 2.10
690 greg 2.42 d = argument(1);
691     if (d <= -0.5 || d >= nfiles+0.5) {
692 greg 2.10 errno = EDOM;
693     return(0.0);
694     }
695 greg 2.42 if (d < 0.5)
696     return((double)nfiles);
697     fn = d - 0.5;
698 greg 2.22 if (ltick[fn] != eclock) { /* need to compute? */
699 greg 2.57 if (input[fn].vw.type == 0) {
700     lorg[fn][0] = lorg[fn][1] = lorg[fn][2] = 0.0;
701     ldir[fn][0] = ldir[fn][1] = ldir[fn][2] = 0.0;
702     ldist[fn] = -1.0;
703 greg 2.12 errno = EDOM;
704 greg 2.57 } else {
705 greg 2.11 pix2loc(loc, &input[fn].rs, xscan, ymax-1-yscan);
706 greg 2.18 ldist[fn] = viewray(lorg[fn], ldir[fn],
707     &input[fn].vw, loc[0], loc[1]);
708 greg 2.57 if (ldist[fn] < -FTINY)
709     errno = EDOM;
710 greg 2.10 }
711 greg 2.18 ltick[fn] = eclock;
712 greg 2.10 }
713 greg 2.18 if (nam == vray[i=6])
714     return(ldist[fn]);
715 greg 2.10 while (i--)
716     if (nam == vray[i])
717     return(i < 3 ? lorg[fn][i] : ldir[fn][i-3]);
718     eputs("Bad call to l_ray()!\n");
719 greg 1.15 quit(1);
720 schorsch 2.32 return 1; /* pro forma return */
721 greg 2.17 }
722    
723    
724 schorsch 2.32 static double
725 greg 2.27 l_psize(char *nm) /* compute pixel size in steradians */
726 greg 2.17 {
727     static unsigned long ltick[MAXINP];
728     static double psize[MAXINP];
729 greg 2.18 FVECT dir0, org, dirx, diry;
730 schorsch 2.25 RREAL locx[2], locy[2];
731 greg 2.17 double d;
732     int fn;
733 greg 2.47 int i;
734 greg 2.17
735     d = argument(1);
736 greg 2.42 if (d <= -0.5 || d >= nfiles+0.5) {
737 greg 2.17 errno = EDOM;
738     return(0.0);
739     }
740 greg 2.42 if (d < 0.5)
741     return((double)nfiles);
742     fn = d - 0.5;
743 greg 2.22 if (ltick[fn] != eclock) { /* need to compute? */
744 greg 2.18 psize[fn] = 0.0;
745     if (input[fn].vw.type == 0)
746 greg 2.17 errno = EDOM;
747 greg 2.18 else if (input[fn].vw.type != VT_PAR &&
748 greg 2.36 funvalue(vray[6], 1, &d) >= -FTINY) {
749 greg 2.18 for (i = 0; i < 3; i++)
750     dir0[i] = funvalue(vray[3+i], 1, &d);
751     pix2loc(locx, &input[fn].rs, xscan+1, ymax-1-yscan);
752     pix2loc(locy, &input[fn].rs, xscan, ymax-yscan);
753     if (viewray(org, dirx, &input[fn].vw,
754 greg 2.35 locx[0], locx[1]) >= -FTINY &&
755 greg 2.18 viewray(org, diry, &input[fn].vw,
756 greg 2.35 locy[0], locy[1]) >= -FTINY) {
757 greg 2.17 /* approximate solid angle */
758 greg 2.18 for (i = 0; i < 3; i++) {
759     dirx[i] -= dir0[i];
760     diry[i] -= dir0[i];
761     }
762     fcross(dir0, dirx, diry);
763 greg 2.35 psize[fn] = VLEN(dir0);
764 greg 2.17 }
765     }
766 greg 2.18 ltick[fn] = eclock;
767     }
768 greg 2.17 return(psize[fn]);
769 greg 1.1 }
770    
771    
772 schorsch 2.29 extern void
773 greg 2.55 wputs(const char *msg)
774 greg 1.1 {
775     if (!nowarn)
776     eputs(msg);
777     }
778    
779    
780 schorsch 2.29 extern void
781 greg 2.55 eputs(const char *msg)
782 greg 1.1 {
783     fputs(msg, stderr);
784     }
785    
786    
787 schorsch 2.29 extern void
788     quit(int code) /* exit gracefully */
789 greg 2.4 {
790 greg 2.47 int i;
791 greg 2.5 /* close input files */
792     for (i = 0; i < nfiles; i++)
793 greg 2.6 if (input[i].name == Command)
794     pclose(input[i].fp);
795     else
796     fclose(input[i].fp);
797 greg 1.1 exit(code);
798     }