ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/px/pcomb.c
Revision: 2.4
Committed: Thu Aug 13 10:49:08 1992 UTC (31 years, 8 months ago) by greg
Content type: text/plain
Branch: MAIN
Changes since 2.3: +30 -8 lines
Log Message:
improved reaping of child processes

File Contents

# User Rev Content
1 greg 1.8 /* Copyright (c) 1991 Regents of the University of California */
2 greg 1.1
3     #ifndef lint
4     static char SCCSid[] = "$SunId$ LBL";
5     #endif
6    
7     /*
8     * Combine picture files according to calcomp functions.
9     *
10     * 1/4/89
11     */
12    
13     #include <stdio.h>
14    
15     #include <errno.h>
16    
17     #include "color.h"
18    
19 greg 1.23 #include "resolu.h"
20    
21 greg 1.1 #include "calcomp.h"
22    
23 greg 1.6 #define MAXINP 32 /* maximum number of input files */
24 greg 1.14 #define WINSIZ 9 /* scanline window size */
25     #define MIDSCN 4 /* current scan position */
26 greg 1.1
27     struct {
28 greg 1.19 char *name; /* file or command name */
29 greg 1.1 FILE *fp; /* stream pointer */
30 greg 1.14 COLOR *scan[WINSIZ]; /* input scanline window */
31 greg 1.3 COLOR coef; /* coefficient */
32 greg 1.17 COLOR expos; /* recorded exposure */
33 greg 1.1 } input[MAXINP]; /* input pictures */
34    
35     int nfiles; /* number of input files */
36    
37 greg 1.21 char vcolin[3][4] = {"ri", "gi", "bi"};
38     char vcolout[3][4] = {"ro", "go", "bo"};
39 greg 1.15 char vbrtin[] = "li";
40     char vbrtout[] = "lo";
41 greg 1.21 char vcolexp[3][4] = {"re", "ge", "be"};
42 greg 1.17 char vbrtexp[] = "le";
43 greg 1.1
44 greg 1.18 char vnfiles[] = "nfiles";
45     char vxmax[] = "xmax";
46     char vymax[] = "ymax";
47     char vxres[] = "xres";
48     char vyres[] = "yres";
49     char vxpos[] = "x";
50     char vypos[] = "y";
51 greg 1.1
52     int nowarn = 0; /* no warning messages? */
53    
54 greg 1.18 int xmax = 0, ymax = 0; /* input resolution */
55 greg 1.9
56 greg 1.18 int xscan, yscan; /* input position */
57 greg 1.1
58 greg 1.18 int xres, yres; /* output resolution */
59 greg 1.1
60 greg 1.18 int xpos, ypos; /* output position */
61    
62 greg 1.10 int wrongformat = 0;
63 greg 1.1
64 greg 1.19 FILE *popen();
65 greg 1.10
66 greg 1.19
67 greg 1.1 main(argc, argv)
68     int argc;
69     char *argv[];
70     {
71 greg 1.18 int original;
72 greg 1.3 double f;
73 greg 1.14 int a, i;
74 greg 1.15 /* scan options */
75     for (a = 1; a < argc; a++) {
76 greg 1.1 if (argv[a][0] == '-')
77     switch (argv[a][1]) {
78     case 'x':
79     case 'y':
80 greg 1.18 a++;
81 greg 1.15 continue;
82 greg 1.1 case 'w':
83     nowarn = !nowarn;
84 greg 1.15 continue;
85 greg 1.1 case 'f':
86     case 'e':
87 greg 1.16 a++;
88 greg 1.15 continue;
89 greg 1.1 }
90 greg 1.15 break;
91     }
92     /* process files */
93 greg 1.17 for (nfiles = 0; nfiles < MAXINP; nfiles++) {
94 greg 1.3 setcolor(input[nfiles].coef, 1.0, 1.0, 1.0);
95 greg 1.17 setcolor(input[nfiles].expos, 1.0, 1.0, 1.0);
96     }
97 greg 1.1 nfiles = 0;
98 greg 1.18 original = 0;
99 greg 1.1 for ( ; a < argc; a++) {
100     if (nfiles >= MAXINP) {
101     eputs(argv[0]);
102     eputs(": too many picture files\n");
103     quit(1);
104     }
105 greg 1.3 if (argv[a][0] == '-')
106     switch (argv[a][1]) {
107     case '\0':
108     input[nfiles].name = "<stdin>";
109     input[nfiles].fp = stdin;
110     break;
111 greg 1.17 case 'o':
112     original++;
113 greg 1.22 continue;
114 greg 1.3 case 's':
115     f = atof(argv[++a]);
116     scalecolor(input[nfiles].coef, f);
117     continue;
118     case 'c':
119     colval(input[nfiles].coef,RED)*=atof(argv[++a]);
120     colval(input[nfiles].coef,GRN)*=atof(argv[++a]);
121     colval(input[nfiles].coef,BLU)*=atof(argv[++a]);
122     continue;
123     default:
124     goto usage;
125     }
126     else {
127 greg 2.2 if (argv[a][0] == '!') {
128     input[nfiles].name = "<Command>";
129     input[nfiles].fp = popen(argv[a]+1, "r");
130     } else {
131     input[nfiles].name = argv[a];
132     input[nfiles].fp = fopen(argv[a], "r");
133     }
134 greg 1.1 if (input[nfiles].fp == NULL) {
135 greg 1.6 perror(argv[a]);
136 greg 1.1 quit(1);
137     }
138     }
139 greg 1.16 checkfile();
140 greg 1.18 if (original) {
141     colval(input[nfiles].coef,RED) /=
142     colval(input[nfiles].expos,RED);
143     colval(input[nfiles].coef,GRN) /=
144     colval(input[nfiles].expos,GRN);
145     colval(input[nfiles].coef,BLU) /=
146     colval(input[nfiles].expos,BLU);
147     }
148     nfiles++;
149 greg 1.17 original = 0;
150 greg 1.15 }
151 greg 1.18 init(); /* set constants */
152 greg 1.15 /* go back and get expressions */
153     for (a = 1; a < argc; a++) {
154     if (argv[a][0] == '-')
155     switch (argv[a][1]) {
156     case 'x':
157 greg 1.21 varset(vxres, ':', eval(argv[++a]));
158 greg 1.18 continue;
159 greg 1.15 case 'y':
160 greg 1.21 varset(vyres, ':', eval(argv[++a]));
161 greg 1.16 continue;
162 greg 1.15 case 'w':
163     continue;
164     case 'f':
165     fcompile(argv[++a]);
166     continue;
167     case 'e':
168     scompile(argv[++a], NULL, 0);
169     continue;
170     }
171     break;
172     }
173 greg 1.18 /* set/get output resolution */
174     if (!vardefined(vxres))
175     varset(vxres, ':', (double)xmax);
176     if (!vardefined(vyres))
177     varset(vyres, ':', (double)ymax);
178     xres = varvalue(vxres) + .5;
179     yres = varvalue(vyres) + .5;
180     if (xres <= 0 || yres <= 0) {
181     eputs(argv[0]);
182     eputs(": illegal output resolution\n");
183     quit(1);
184     }
185 greg 1.15 /* complete header */
186     printargs(argc, argv, stdout);
187     fputformat(COLRFMT, stdout);
188     putchar('\n');
189 greg 1.23 fprtresolu(xres, yres, stdout);
190 greg 1.15 /* combine pictures */
191     combine();
192     quit(0);
193     usage:
194     eputs("Usage: ");
195     eputs(argv[0]);
196     eputs(
197 greg 1.20 " [-w][-x xr][-y yr][-e expr][-f file] [ [-s f][-c r g b] pic ..]\n");
198 greg 1.15 quit(1);
199     }
200    
201    
202     tputs(s) /* put out string preceded by a tab */
203     char *s;
204     {
205     char fmt[32];
206     double d;
207     COLOR ctmp;
208    
209     if (isformat(s)) { /* check format */
210     formatval(fmt, s);
211     wrongformat = strcmp(fmt, COLRFMT);
212 greg 1.17 return; /* don't echo */
213     }
214     if (isexpos(s)) { /* exposure */
215     d = exposval(s);
216     scalecolor(input[nfiles].expos, d);
217     } else if (iscolcor(s)) { /* color correction */
218 greg 1.15 colcorval(ctmp, s);
219 greg 1.17 multcolor(input[nfiles].expos, ctmp);
220 greg 1.15 }
221 greg 1.17 /* echo line */
222     putchar('\t');
223     fputs(s, stdout);
224 greg 1.16 }
225 greg 1.15
226 greg 1.16
227     checkfile() /* ready a file */
228     {
229 greg 1.18 int xinp, yinp;
230 greg 1.16 register int i;
231     /* process header */
232     fputs(input[nfiles].name, stdout);
233     fputs(":\n", stdout);
234     getheader(input[nfiles].fp, tputs, NULL);
235     if (wrongformat) {
236     eputs(input[nfiles].name);
237     eputs(": not in Radiance picture format\n");
238     quit(1);
239     }
240 greg 1.23 if (fgetresolu(&xinp, &yinp, input[nfiles].fp) < 0) {
241 greg 1.16 eputs(input[nfiles].name);
242     eputs(": bad picture size\n");
243     quit(1);
244     }
245 greg 1.18 if (xmax == 0 && ymax == 0) {
246     xmax = xinp;
247     ymax = yinp;
248     } else if (xinp != xmax || yinp != ymax) {
249 greg 1.16 eputs(input[nfiles].name);
250     eputs(": resolution mismatch\n");
251     quit(1);
252     }
253     /* allocate scanlines */
254     for (i = 0; i < WINSIZ; i++)
255 greg 1.18 input[nfiles].scan[i] = (COLOR *)emalloc(xmax*sizeof(COLOR));
256 greg 1.15 }
257    
258    
259 greg 1.18 init() /* perform final setup */
260 greg 1.15 {
261 greg 1.17 double l_colin(), l_expos();
262 greg 1.16 register int i;
263 greg 1.15 /* define constants */
264     varset(vnfiles, ':', (double)nfiles);
265 greg 1.18 varset(vxmax, ':', (double)xmax);
266     varset(vymax, ':', (double)ymax);
267 greg 1.15 /* set functions */
268     for (i = 0; i < 3; i++) {
269 greg 1.17 funset(vcolexp[i], 1, ':', l_expos);
270 greg 1.15 funset(vcolin[i], 1, '=', l_colin);
271     }
272 greg 1.17 funset(vbrtexp, 1, ':', l_expos);
273 greg 1.15 funset(vbrtin, 1, '=', l_colin);
274 greg 1.1 }
275    
276    
277     combine() /* combine pictures */
278     {
279 greg 1.8 EPNODE *coldef[3], *brtdef;
280 greg 1.1 COLOR *scanout;
281 greg 1.8 double d;
282 greg 1.1 register int i, j;
283     /* check defined variables */
284 greg 1.4 for (j = 0; j < 3; j++) {
285     if (vardefined(vcolout[j]))
286     coldef[j] = eparse(vcolout[j]);
287     else
288     coldef[j] = NULL;
289     }
290 greg 1.8 if (vardefined(vbrtout))
291     brtdef = eparse(vbrtout);
292     else
293     brtdef = NULL;
294 greg 1.1 /* allocate scanline */
295     scanout = (COLOR *)emalloc(xres*sizeof(COLOR));
296 greg 1.18 /* set input position */
297     yscan = ymax+MIDSCN;
298 greg 1.1 /* combine files */
299     for (ypos = yres-1; ypos >= 0; ypos--) {
300 greg 1.14 advance();
301 greg 1.11 varset(vypos, '=', (double)ypos);
302 greg 1.8 for (xpos = 0; xpos < xres; xpos++) {
303 greg 1.18 xscan = (long)xpos*xmax/xres;
304 greg 1.11 varset(vxpos, '=', (double)xpos);
305 greg 1.8 eclock++;
306     if (brtdef != NULL) {
307     d = evalue(brtdef);
308     if (d < 0.0)
309     d = 0.0;
310     setcolor(scanout[xpos], d, d, d);
311     } else {
312     for (j = 0; j < 3; j++) {
313     if (coldef[j] != NULL) {
314 greg 1.13 d = evalue(coldef[j]);
315 greg 1.8 } else {
316 greg 1.13 d = 0.0;
317 greg 1.8 for (i = 0; i < nfiles; i++)
318 greg 1.18 d += colval(input[i].scan[MIDSCN][xscan],j);
319 greg 1.1 }
320 greg 1.13 if (d < 0.0)
321     d = 0.0;
322     colval(scanout[xpos],j) = d;
323 greg 1.8 }
324 greg 1.1 }
325 greg 1.8 }
326     if (fwritescan(scanout, xres, stdout) < 0) {
327     perror("write error");
328     quit(1);
329     }
330 greg 1.1 }
331     efree(scanout);
332     }
333    
334    
335 greg 1.18 advance() /* read in data for next scanline */
336 greg 1.14 {
337 greg 2.4 extern double fabs();
338 greg 1.18 int ytarget;
339 greg 1.14 register COLOR *st;
340     register int i, j;
341    
342 greg 1.18 for (ytarget = (long)ypos*ymax/yres; yscan > ytarget; yscan--)
343     for (i = 0; i < nfiles; i++) {
344     st = input[i].scan[WINSIZ-1];
345     for (j = WINSIZ-1; j > 0; j--) /* rotate window */
346     input[i].scan[j] = input[i].scan[j-1];
347     input[i].scan[0] = st;
348     if (yscan <= MIDSCN) /* hit bottom? */
349     continue;
350 greg 2.4 if (freadscan(st, xmax, input[i].fp) < 0) { /* read */
351 greg 1.18 eputs(input[i].name);
352     eputs(": read error\n");
353     quit(1);
354     }
355 greg 2.4 if (fabs(colval(input[i].coef,RED)-1.0) > 1e-3 ||
356     fabs(colval(input[i].coef,GRN)-1.0) > 1e-3 ||
357     fabs(colval(input[i].coef,BLU)-1.0) > 1e-3)
358     for (j = 0; j < xmax; j++) /* adjust color */
359     multcolor(st[j], input[i].coef);
360 greg 1.14 }
361     }
362    
363    
364 greg 1.1 double
365 greg 1.17 l_expos(nam) /* return picture exposure */
366 greg 1.15 register char *nam;
367 greg 1.1 {
368 greg 1.15 register int fn, n;
369 greg 1.14
370 greg 1.18 fn = argument(1) - .5;
371     if (fn < 0 || fn >= nfiles)
372     return(1.0);
373 greg 1.17 if (nam == vbrtexp)
374     return(bright(input[fn].expos));
375 greg 1.15 n = 3;
376     while (n--)
377 greg 1.17 if (nam == vcolexp[n])
378     return(colval(input[fn].expos,n));
379     eputs("Bad call to l_expos()!\n");
380 greg 1.15 quit(1);
381     }
382    
383    
384     double
385     l_colin(nam) /* return color value for picture */
386     register char *nam;
387     {
388     int fn;
389     register int n, xoff, yoff;
390     double d;
391    
392 greg 1.16 d = argument(1);
393     if (d > -.5 && d < .5)
394     return((double)nfiles);
395     fn = d - .5;
396     if (fn < 0 || fn >= nfiles) {
397 greg 1.15 errno = EDOM;
398     return(0.0);
399     }
400 greg 1.14 xoff = yoff = 0;
401     n = nargum();
402     if (n >= 2) {
403     d = argument(2);
404     if (d < 0.0) {
405     xoff = d-.5;
406 greg 1.18 if (xscan+xoff < 0)
407     xoff = -xscan;
408 greg 1.14 } else {
409     xoff = d+.5;
410 greg 1.18 if (xscan+xoff >= xmax)
411     xoff = xmax-1-xscan;
412 greg 1.14 }
413     }
414     if (n >= 3) {
415     d = argument(3);
416     if (d < 0.0) {
417     yoff = d-.5;
418     if (yoff+MIDSCN < 0)
419     yoff = -MIDSCN;
420 greg 1.18 if (yscan+yoff < 0)
421     yoff = -yscan;
422 greg 1.14 } else {
423     yoff = d+.5;
424     if (yoff+MIDSCN >= WINSIZ)
425     yoff = WINSIZ-1-MIDSCN;
426 greg 1.18 if (yscan+yoff >= ymax)
427     yoff = ymax-1-yscan;
428 greg 1.14 }
429     }
430 greg 1.15 if (nam == vbrtin)
431 greg 1.18 return(bright(input[fn].scan[MIDSCN+yoff][xscan+xoff]));
432 greg 1.15 n = 3;
433     while (n--)
434     if (nam == vcolin[n])
435 greg 1.18 return(colval(input[fn].scan[MIDSCN+yoff][xscan+xoff],n));
436 greg 1.15 eputs("Bad call to l_colin()!\n");
437     quit(1);
438 greg 1.1 }
439    
440    
441     wputs(msg)
442     char *msg;
443     {
444     if (!nowarn)
445     eputs(msg);
446     }
447    
448    
449     eputs(msg)
450     char *msg;
451     {
452     fputs(msg, stderr);
453     }
454    
455    
456 greg 2.4 #ifdef NIX
457    
458 greg 1.1 quit(code)
459 greg 2.4 int code;
460 greg 1.1 {
461 greg 2.4 exit(code);
462     }
463    
464     #else
465    
466     #include <signal.h>
467    
468     quit(code) /* exit gracefully */
469     int code;
470     {
471 greg 2.3 int status;
472    
473 greg 2.4 if (code) { /* abnormal exit -- kill children */
474     signal(SIGPIPE, SIG_IGN);
475     kill(0, SIGPIPE);
476     }
477     /* reap any children */
478     while (wait(&status) != -1)
479     if (code == 0)
480     code = status>>8 & 0xff;
481 greg 1.1 exit(code);
482     }
483 greg 2.4
484     #endif