ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/px/pcomb.c
Revision: 1.20
Committed: Thu May 30 10:14:39 1991 UTC (32 years, 11 months ago) by greg
Content type: text/plain
Branch: MAIN
Changes since 1.19: +1 -1 lines
Log Message:
fixed outdated usages message

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 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     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.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 char buf[128];
72     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     break;
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 1.1 input[nfiles].name = argv[a];
129 greg 1.19 input[nfiles].fp = argv[a][0]=='!' ?
130     popen(argv[a]+1, "r") :
131     fopen(argv[a], "r");
132 greg 1.1 if (input[nfiles].fp == NULL) {
133 greg 1.6 perror(argv[a]);
134 greg 1.1 quit(1);
135     }
136     }
137 greg 1.16 checkfile();
138 greg 1.18 if (original) {
139     colval(input[nfiles].coef,RED) /=
140     colval(input[nfiles].expos,RED);
141     colval(input[nfiles].coef,GRN) /=
142     colval(input[nfiles].expos,GRN);
143     colval(input[nfiles].coef,BLU) /=
144     colval(input[nfiles].expos,BLU);
145     }
146     nfiles++;
147 greg 1.17 original = 0;
148 greg 1.15 }
149 greg 1.18 init(); /* set constants */
150 greg 1.15 /* go back and get expressions */
151     for (a = 1; a < argc; a++) {
152     if (argv[a][0] == '-')
153     switch (argv[a][1]) {
154     case 'x':
155 greg 1.18 strcpy(buf, vxres);
156     strcat(buf, ":");
157     strcat(buf, argv[++a]);
158     scompile(buf, NULL, 0);
159     continue;
160 greg 1.15 case 'y':
161 greg 1.18 strcpy(buf, vyres);
162     strcat(buf, ":");
163     strcat(buf, argv[++a]);
164     scompile(buf, NULL, 0);
165 greg 1.16 continue;
166 greg 1.15 case 'w':
167     continue;
168     case 'f':
169     fcompile(argv[++a]);
170     continue;
171     case 'e':
172     scompile(argv[++a], NULL, 0);
173     continue;
174     }
175     break;
176     }
177 greg 1.18 /* set/get output resolution */
178     if (!vardefined(vxres))
179     varset(vxres, ':', (double)xmax);
180     if (!vardefined(vyres))
181     varset(vyres, ':', (double)ymax);
182     xres = varvalue(vxres) + .5;
183     yres = varvalue(vyres) + .5;
184     if (xres <= 0 || yres <= 0) {
185     eputs(argv[0]);
186     eputs(": illegal output resolution\n");
187     quit(1);
188     }
189 greg 1.15 /* complete header */
190     printargs(argc, argv, stdout);
191     fputformat(COLRFMT, stdout);
192     putchar('\n');
193     fputresolu(YMAJOR|YDECR, xres, yres, stdout);
194     /* combine pictures */
195     combine();
196     quit(0);
197     usage:
198     eputs("Usage: ");
199     eputs(argv[0]);
200     eputs(
201 greg 1.20 " [-w][-x xr][-y yr][-e expr][-f file] [ [-s f][-c r g b] pic ..]\n");
202 greg 1.15 quit(1);
203     }
204    
205    
206     tputs(s) /* put out string preceded by a tab */
207     char *s;
208     {
209     char fmt[32];
210     double d;
211     COLOR ctmp;
212    
213     if (isformat(s)) { /* check format */
214     formatval(fmt, s);
215     wrongformat = strcmp(fmt, COLRFMT);
216 greg 1.17 return; /* don't echo */
217     }
218     if (isexpos(s)) { /* exposure */
219     d = exposval(s);
220     scalecolor(input[nfiles].expos, d);
221     } else if (iscolcor(s)) { /* color correction */
222 greg 1.15 colcorval(ctmp, s);
223 greg 1.17 multcolor(input[nfiles].expos, ctmp);
224 greg 1.15 }
225 greg 1.17 /* echo line */
226     putchar('\t');
227     fputs(s, stdout);
228 greg 1.16 }
229 greg 1.15
230 greg 1.16
231     checkfile() /* ready a file */
232     {
233 greg 1.18 int xinp, yinp;
234 greg 1.16 register int i;
235     /* process header */
236     fputs(input[nfiles].name, stdout);
237     fputs(":\n", stdout);
238     getheader(input[nfiles].fp, tputs, NULL);
239     if (wrongformat) {
240     eputs(input[nfiles].name);
241     eputs(": not in Radiance picture format\n");
242     quit(1);
243     }
244 greg 1.18 if (fgetresolu(&xinp, &yinp, input[nfiles].fp) != (YMAJOR|YDECR)) {
245 greg 1.16 eputs(input[nfiles].name);
246     eputs(": bad picture size\n");
247     quit(1);
248     }
249 greg 1.18 if (xmax == 0 && ymax == 0) {
250     xmax = xinp;
251     ymax = yinp;
252     } else if (xinp != xmax || yinp != ymax) {
253 greg 1.16 eputs(input[nfiles].name);
254     eputs(": resolution mismatch\n");
255     quit(1);
256     }
257     /* allocate scanlines */
258     for (i = 0; i < WINSIZ; i++)
259 greg 1.18 input[nfiles].scan[i] = (COLOR *)emalloc(xmax*sizeof(COLOR));
260 greg 1.15 }
261    
262    
263 greg 1.18 init() /* perform final setup */
264 greg 1.15 {
265 greg 1.17 double l_colin(), l_expos();
266 greg 1.16 register int i;
267 greg 1.15 /* define constants */
268     varset(vnfiles, ':', (double)nfiles);
269 greg 1.18 varset(vxmax, ':', (double)xmax);
270     varset(vymax, ':', (double)ymax);
271 greg 1.15 /* set functions */
272     for (i = 0; i < 3; i++) {
273 greg 1.17 funset(vcolexp[i], 1, ':', l_expos);
274 greg 1.15 funset(vcolin[i], 1, '=', l_colin);
275     }
276 greg 1.17 funset(vbrtexp, 1, ':', l_expos);
277 greg 1.15 funset(vbrtin, 1, '=', l_colin);
278 greg 1.1 }
279    
280    
281     combine() /* combine pictures */
282     {
283 greg 1.8 EPNODE *coldef[3], *brtdef;
284 greg 1.1 COLOR *scanout;
285 greg 1.8 double d;
286 greg 1.1 register int i, j;
287     /* check defined variables */
288 greg 1.4 for (j = 0; j < 3; j++) {
289     if (vardefined(vcolout[j]))
290     coldef[j] = eparse(vcolout[j]);
291     else
292     coldef[j] = NULL;
293     }
294 greg 1.8 if (vardefined(vbrtout))
295     brtdef = eparse(vbrtout);
296     else
297     brtdef = NULL;
298 greg 1.1 /* allocate scanline */
299     scanout = (COLOR *)emalloc(xres*sizeof(COLOR));
300 greg 1.18 /* set input position */
301     yscan = ymax+MIDSCN;
302 greg 1.1 /* combine files */
303     for (ypos = yres-1; ypos >= 0; ypos--) {
304 greg 1.14 advance();
305 greg 1.11 varset(vypos, '=', (double)ypos);
306 greg 1.8 for (xpos = 0; xpos < xres; xpos++) {
307 greg 1.18 xscan = (long)xpos*xmax/xres;
308 greg 1.11 varset(vxpos, '=', (double)xpos);
309 greg 1.8 eclock++;
310     if (brtdef != NULL) {
311     d = evalue(brtdef);
312     if (d < 0.0)
313     d = 0.0;
314     setcolor(scanout[xpos], d, d, d);
315     } else {
316     for (j = 0; j < 3; j++) {
317     if (coldef[j] != NULL) {
318 greg 1.13 d = evalue(coldef[j]);
319 greg 1.8 } else {
320 greg 1.13 d = 0.0;
321 greg 1.8 for (i = 0; i < nfiles; i++)
322 greg 1.18 d += colval(input[i].scan[MIDSCN][xscan],j);
323 greg 1.1 }
324 greg 1.13 if (d < 0.0)
325     d = 0.0;
326     colval(scanout[xpos],j) = d;
327 greg 1.8 }
328 greg 1.1 }
329 greg 1.8 }
330     if (fwritescan(scanout, xres, stdout) < 0) {
331     perror("write error");
332     quit(1);
333     }
334 greg 1.1 }
335     efree(scanout);
336     }
337    
338    
339 greg 1.18 advance() /* read in data for next scanline */
340 greg 1.14 {
341 greg 1.18 int ytarget;
342 greg 1.14 register COLOR *st;
343     register int i, j;
344    
345 greg 1.18 for (ytarget = (long)ypos*ymax/yres; yscan > ytarget; yscan--)
346     for (i = 0; i < nfiles; i++) {
347     st = input[i].scan[WINSIZ-1];
348     for (j = WINSIZ-1; j > 0; j--) /* rotate window */
349     input[i].scan[j] = input[i].scan[j-1];
350     input[i].scan[0] = st;
351     if (yscan <= MIDSCN) /* hit bottom? */
352     continue;
353     if (freadscan(st, xmax, input[i].fp) < 0) { /* read */
354     eputs(input[i].name);
355     eputs(": read error\n");
356     quit(1);
357     }
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     quit(code)
457     int code;
458     {
459     exit(code);
460     }