ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/px/pcomb.c
Revision: 2.6
Committed: Tue Sep 8 10:35:05 1992 UTC (31 years, 7 months ago) by greg
Content type: text/plain
Branch: MAIN
Changes since 2.5: +7 -20 lines
Log Message:
portability fixes

File Contents

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