ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/px/pcomb.c
Revision: 1.23
Committed: Mon Nov 11 14:01:24 1991 UTC (32 years, 5 months ago) by greg
Content type: text/plain
Branch: MAIN
Changes since 1.22: +4 -2 lines
Log Message:
Improved handling of scanline ordering

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 1.1 input[nfiles].name = argv[a];
128 greg 1.19 input[nfiles].fp = argv[a][0]=='!' ?
129     popen(argv[a]+1, "r") :
130     fopen(argv[a], "r");
131 greg 1.1 if (input[nfiles].fp == NULL) {
132 greg 1.6 perror(argv[a]);
133 greg 1.1 quit(1);
134     }
135     }
136 greg 1.16 checkfile();
137 greg 1.18 if (original) {
138     colval(input[nfiles].coef,RED) /=
139     colval(input[nfiles].expos,RED);
140     colval(input[nfiles].coef,GRN) /=
141     colval(input[nfiles].expos,GRN);
142     colval(input[nfiles].coef,BLU) /=
143     colval(input[nfiles].expos,BLU);
144     }
145     nfiles++;
146 greg 1.17 original = 0;
147 greg 1.15 }
148 greg 1.18 init(); /* set constants */
149 greg 1.15 /* go back and get expressions */
150     for (a = 1; a < argc; a++) {
151     if (argv[a][0] == '-')
152     switch (argv[a][1]) {
153     case 'x':
154 greg 1.21 varset(vxres, ':', eval(argv[++a]));
155 greg 1.18 continue;
156 greg 1.15 case 'y':
157 greg 1.21 varset(vyres, ':', eval(argv[++a]));
158 greg 1.16 continue;
159 greg 1.15 case 'w':
160     continue;
161     case 'f':
162     fcompile(argv[++a]);
163     continue;
164     case 'e':
165     scompile(argv[++a], NULL, 0);
166     continue;
167     }
168     break;
169     }
170 greg 1.18 /* set/get output resolution */
171     if (!vardefined(vxres))
172     varset(vxres, ':', (double)xmax);
173     if (!vardefined(vyres))
174     varset(vyres, ':', (double)ymax);
175     xres = varvalue(vxres) + .5;
176     yres = varvalue(vyres) + .5;
177     if (xres <= 0 || yres <= 0) {
178     eputs(argv[0]);
179     eputs(": illegal output resolution\n");
180     quit(1);
181     }
182 greg 1.15 /* complete header */
183     printargs(argc, argv, stdout);
184     fputformat(COLRFMT, stdout);
185     putchar('\n');
186 greg 1.23 fprtresolu(xres, yres, stdout);
187 greg 1.15 /* combine pictures */
188     combine();
189     quit(0);
190     usage:
191     eputs("Usage: ");
192     eputs(argv[0]);
193     eputs(
194 greg 1.20 " [-w][-x xr][-y yr][-e expr][-f file] [ [-s f][-c r g b] pic ..]\n");
195 greg 1.15 quit(1);
196     }
197    
198    
199     tputs(s) /* put out string preceded by a tab */
200     char *s;
201     {
202     char fmt[32];
203     double d;
204     COLOR ctmp;
205    
206     if (isformat(s)) { /* check format */
207     formatval(fmt, s);
208     wrongformat = strcmp(fmt, COLRFMT);
209 greg 1.17 return; /* don't echo */
210     }
211     if (isexpos(s)) { /* exposure */
212     d = exposval(s);
213     scalecolor(input[nfiles].expos, d);
214     } else if (iscolcor(s)) { /* color correction */
215 greg 1.15 colcorval(ctmp, s);
216 greg 1.17 multcolor(input[nfiles].expos, ctmp);
217 greg 1.15 }
218 greg 1.17 /* echo line */
219     putchar('\t');
220     fputs(s, stdout);
221 greg 1.16 }
222 greg 1.15
223 greg 1.16
224     checkfile() /* ready a file */
225     {
226 greg 1.18 int xinp, yinp;
227 greg 1.16 register int i;
228     /* process header */
229     fputs(input[nfiles].name, stdout);
230     fputs(":\n", stdout);
231     getheader(input[nfiles].fp, tputs, NULL);
232     if (wrongformat) {
233     eputs(input[nfiles].name);
234     eputs(": not in Radiance picture format\n");
235     quit(1);
236     }
237 greg 1.23 if (fgetresolu(&xinp, &yinp, input[nfiles].fp) < 0) {
238 greg 1.16 eputs(input[nfiles].name);
239     eputs(": bad picture size\n");
240     quit(1);
241     }
242 greg 1.18 if (xmax == 0 && ymax == 0) {
243     xmax = xinp;
244     ymax = yinp;
245     } else if (xinp != xmax || yinp != ymax) {
246 greg 1.16 eputs(input[nfiles].name);
247     eputs(": resolution mismatch\n");
248     quit(1);
249     }
250     /* allocate scanlines */
251     for (i = 0; i < WINSIZ; i++)
252 greg 1.18 input[nfiles].scan[i] = (COLOR *)emalloc(xmax*sizeof(COLOR));
253 greg 1.15 }
254    
255    
256 greg 1.18 init() /* perform final setup */
257 greg 1.15 {
258 greg 1.17 double l_colin(), l_expos();
259 greg 1.16 register int i;
260 greg 1.15 /* define constants */
261     varset(vnfiles, ':', (double)nfiles);
262 greg 1.18 varset(vxmax, ':', (double)xmax);
263     varset(vymax, ':', (double)ymax);
264 greg 1.15 /* set functions */
265     for (i = 0; i < 3; i++) {
266 greg 1.17 funset(vcolexp[i], 1, ':', l_expos);
267 greg 1.15 funset(vcolin[i], 1, '=', l_colin);
268     }
269 greg 1.17 funset(vbrtexp, 1, ':', l_expos);
270 greg 1.15 funset(vbrtin, 1, '=', l_colin);
271 greg 1.1 }
272    
273    
274     combine() /* combine pictures */
275     {
276 greg 1.8 EPNODE *coldef[3], *brtdef;
277 greg 1.1 COLOR *scanout;
278 greg 1.8 double d;
279 greg 1.1 register int i, j;
280     /* check defined variables */
281 greg 1.4 for (j = 0; j < 3; j++) {
282     if (vardefined(vcolout[j]))
283     coldef[j] = eparse(vcolout[j]);
284     else
285     coldef[j] = NULL;
286     }
287 greg 1.8 if (vardefined(vbrtout))
288     brtdef = eparse(vbrtout);
289     else
290     brtdef = NULL;
291 greg 1.1 /* allocate scanline */
292     scanout = (COLOR *)emalloc(xres*sizeof(COLOR));
293 greg 1.18 /* set input position */
294     yscan = ymax+MIDSCN;
295 greg 1.1 /* combine files */
296     for (ypos = yres-1; ypos >= 0; ypos--) {
297 greg 1.14 advance();
298 greg 1.11 varset(vypos, '=', (double)ypos);
299 greg 1.8 for (xpos = 0; xpos < xres; xpos++) {
300 greg 1.18 xscan = (long)xpos*xmax/xres;
301 greg 1.11 varset(vxpos, '=', (double)xpos);
302 greg 1.8 eclock++;
303     if (brtdef != NULL) {
304     d = evalue(brtdef);
305     if (d < 0.0)
306     d = 0.0;
307     setcolor(scanout[xpos], d, d, d);
308     } else {
309     for (j = 0; j < 3; j++) {
310     if (coldef[j] != NULL) {
311 greg 1.13 d = evalue(coldef[j]);
312 greg 1.8 } else {
313 greg 1.13 d = 0.0;
314 greg 1.8 for (i = 0; i < nfiles; i++)
315 greg 1.18 d += colval(input[i].scan[MIDSCN][xscan],j);
316 greg 1.1 }
317 greg 1.13 if (d < 0.0)
318     d = 0.0;
319     colval(scanout[xpos],j) = d;
320 greg 1.8 }
321 greg 1.1 }
322 greg 1.8 }
323     if (fwritescan(scanout, xres, stdout) < 0) {
324     perror("write error");
325     quit(1);
326     }
327 greg 1.1 }
328     efree(scanout);
329     }
330    
331    
332 greg 1.18 advance() /* read in data for next scanline */
333 greg 1.14 {
334 greg 1.18 int ytarget;
335 greg 1.14 register COLOR *st;
336     register int i, j;
337    
338 greg 1.18 for (ytarget = (long)ypos*ymax/yres; yscan > ytarget; yscan--)
339     for (i = 0; i < nfiles; i++) {
340     st = input[i].scan[WINSIZ-1];
341     for (j = WINSIZ-1; j > 0; j--) /* rotate window */
342     input[i].scan[j] = input[i].scan[j-1];
343     input[i].scan[0] = st;
344     if (yscan <= MIDSCN) /* hit bottom? */
345     continue;
346     if (freadscan(st, xmax, input[i].fp) < 0) { /* read */
347     eputs(input[i].name);
348     eputs(": read error\n");
349     quit(1);
350     }
351     for (j = 0; j < xmax; j++) /* adjust color */
352     multcolor(st[j], input[i].coef);
353 greg 1.14 }
354     }
355    
356    
357 greg 1.1 double
358 greg 1.17 l_expos(nam) /* return picture exposure */
359 greg 1.15 register char *nam;
360 greg 1.1 {
361 greg 1.15 register int fn, n;
362 greg 1.14
363 greg 1.18 fn = argument(1) - .5;
364     if (fn < 0 || fn >= nfiles)
365     return(1.0);
366 greg 1.17 if (nam == vbrtexp)
367     return(bright(input[fn].expos));
368 greg 1.15 n = 3;
369     while (n--)
370 greg 1.17 if (nam == vcolexp[n])
371     return(colval(input[fn].expos,n));
372     eputs("Bad call to l_expos()!\n");
373 greg 1.15 quit(1);
374     }
375    
376    
377     double
378     l_colin(nam) /* return color value for picture */
379     register char *nam;
380     {
381     int fn;
382     register int n, xoff, yoff;
383     double d;
384    
385 greg 1.16 d = argument(1);
386     if (d > -.5 && d < .5)
387     return((double)nfiles);
388     fn = d - .5;
389     if (fn < 0 || fn >= nfiles) {
390 greg 1.15 errno = EDOM;
391     return(0.0);
392     }
393 greg 1.14 xoff = yoff = 0;
394     n = nargum();
395     if (n >= 2) {
396     d = argument(2);
397     if (d < 0.0) {
398     xoff = d-.5;
399 greg 1.18 if (xscan+xoff < 0)
400     xoff = -xscan;
401 greg 1.14 } else {
402     xoff = d+.5;
403 greg 1.18 if (xscan+xoff >= xmax)
404     xoff = xmax-1-xscan;
405 greg 1.14 }
406     }
407     if (n >= 3) {
408     d = argument(3);
409     if (d < 0.0) {
410     yoff = d-.5;
411     if (yoff+MIDSCN < 0)
412     yoff = -MIDSCN;
413 greg 1.18 if (yscan+yoff < 0)
414     yoff = -yscan;
415 greg 1.14 } else {
416     yoff = d+.5;
417     if (yoff+MIDSCN >= WINSIZ)
418     yoff = WINSIZ-1-MIDSCN;
419 greg 1.18 if (yscan+yoff >= ymax)
420     yoff = ymax-1-yscan;
421 greg 1.14 }
422     }
423 greg 1.15 if (nam == vbrtin)
424 greg 1.18 return(bright(input[fn].scan[MIDSCN+yoff][xscan+xoff]));
425 greg 1.15 n = 3;
426     while (n--)
427     if (nam == vcolin[n])
428 greg 1.18 return(colval(input[fn].scan[MIDSCN+yoff][xscan+xoff],n));
429 greg 1.15 eputs("Bad call to l_colin()!\n");
430     quit(1);
431 greg 1.1 }
432    
433    
434     wputs(msg)
435     char *msg;
436     {
437     if (!nowarn)
438     eputs(msg);
439     }
440    
441    
442     eputs(msg)
443     char *msg;
444     {
445     fputs(msg, stderr);
446     }
447    
448    
449     quit(code)
450     int code;
451     {
452     exit(code);
453     }