ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/px/pcomb.c
Revision: 2.31
Committed: Mon Mar 15 21:16:54 2004 UTC (20 years, 1 month ago) by greg
Content type: text/plain
Branch: MAIN
Changes since 2.30: +3 -2 lines
Log Message:
Made it so pcompos does not cut off input command streams abruptly

File Contents

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