ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/px/pcomb.c
Revision: 1.17
Committed: Fri May 24 17:14:49 1991 UTC (32 years, 11 months ago) by greg
Content type: text/plain
Branch: MAIN
Changes since 1.16: +36 -26 lines
Log Message:
changed operation of -o option

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