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

# Content
1 /* Copyright (c) 1992 Regents of the University of California */
2
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 "resolu.h"
20
21 #include "calcomp.h"
22
23 #define MAXINP 32 /* maximum number of input files */
24 #define WINSIZ 9 /* scanline window size */
25 #define MIDSCN 4 /* current scan position */
26
27 struct {
28 char *name; /* file or command name */
29 FILE *fp; /* stream pointer */
30 COLOR *scan[WINSIZ]; /* input scanline window */
31 COLOR coef; /* coefficient */
32 COLOR expos; /* recorded exposure */
33 } input[MAXINP]; /* input pictures */
34
35 int nfiles; /* number of input files */
36
37 char Command[] = "<Command>";
38 char vcolin[3][4] = {"ri", "gi", "bi"};
39 char vcolout[3][4] = {"ro", "go", "bo"};
40 char vbrtin[] = "li";
41 char vbrtout[] = "lo";
42 char vcolexp[3][4] = {"re", "ge", "be"};
43 char vbrtexp[] = "le";
44
45 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
53 int nowarn = 0; /* no warning messages? */
54
55 int xmax = 0, ymax = 0; /* input resolution */
56
57 int xscan, yscan; /* input position */
58
59 int xres, yres; /* output resolution */
60
61 int xpos, ypos; /* output position */
62
63 int wrongformat = 0;
64
65 FILE *popen();
66
67
68 main(argc, argv)
69 int argc;
70 char *argv[];
71 {
72 int original;
73 double f;
74 int a, i;
75 /* scan options */
76 for (a = 1; a < argc; a++) {
77 if (argv[a][0] == '-')
78 switch (argv[a][1]) {
79 case 'x':
80 case 'y':
81 a++;
82 continue;
83 case 'w':
84 nowarn = !nowarn;
85 continue;
86 case 'f':
87 case 'e':
88 a++;
89 continue;
90 }
91 break;
92 }
93 /* process files */
94 for (nfiles = 0; nfiles < MAXINP; nfiles++) {
95 setcolor(input[nfiles].coef, 1.0, 1.0, 1.0);
96 setcolor(input[nfiles].expos, 1.0, 1.0, 1.0);
97 }
98 nfiles = 0;
99 original = 0;
100 for ( ; a < argc; a++) {
101 if (nfiles >= MAXINP) {
102 eputs(argv[0]);
103 eputs(": too many picture files\n");
104 quit(1);
105 }
106 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 case 'o':
113 original++;
114 continue;
115 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 if (argv[a][0] == '!') {
129 input[nfiles].name = Command;
130 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 if (input[nfiles].fp == NULL) {
136 perror(argv[a]);
137 quit(1);
138 }
139 }
140 checkfile();
141 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 original = 0;
151 }
152 init(); /* set constants */
153 /* 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 varset(vxres, ':', eval(argv[++a]));
159 continue;
160 case 'y':
161 varset(vyres, ':', eval(argv[++a]));
162 continue;
163 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 /* 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 /* complete header */
187 printargs(argc, argv, stdout);
188 fputformat(COLRFMT, stdout);
189 putchar('\n');
190 fprtresolu(xres, yres, stdout);
191 /* combine pictures */
192 combine();
193 quit(0);
194 usage:
195 eputs("Usage: ");
196 eputs(argv[0]);
197 eputs(
198 " [-w][-x xr][-y yr][-e expr][-f file] [ [-s f][-c r g b] pic ..]\n");
199 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 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 colcorval(ctmp, s);
220 multcolor(input[nfiles].expos, ctmp);
221 }
222 /* echo line */
223 putchar('\t');
224 fputs(s, stdout);
225 }
226
227
228 checkfile() /* ready a file */
229 {
230 int xinp, yinp;
231 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 if (fgetresolu(&xinp, &yinp, input[nfiles].fp) < 0) {
242 eputs(input[nfiles].name);
243 eputs(": bad picture size\n");
244 quit(1);
245 }
246 if (xmax == 0 && ymax == 0) {
247 xmax = xinp;
248 ymax = yinp;
249 } else if (xinp != xmax || yinp != ymax) {
250 eputs(input[nfiles].name);
251 eputs(": resolution mismatch\n");
252 quit(1);
253 }
254 /* allocate scanlines */
255 for (i = 0; i < WINSIZ; i++)
256 input[nfiles].scan[i] = (COLOR *)emalloc(xmax*sizeof(COLOR));
257 }
258
259
260 init() /* perform final setup */
261 {
262 double l_colin(), l_expos();
263 register int i;
264 /* define constants */
265 varset(vnfiles, ':', (double)nfiles);
266 varset(vxmax, ':', (double)xmax);
267 varset(vymax, ':', (double)ymax);
268 /* set functions */
269 for (i = 0; i < 3; i++) {
270 funset(vcolexp[i], 1, ':', l_expos);
271 funset(vcolin[i], 1, '=', l_colin);
272 }
273 funset(vbrtexp, 1, ':', l_expos);
274 funset(vbrtin, 1, '=', l_colin);
275 }
276
277
278 combine() /* combine pictures */
279 {
280 EPNODE *coldef[3], *brtdef;
281 COLOR *scanout;
282 double d;
283 register int i, j;
284 /* check defined variables */
285 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 if (vardefined(vbrtout))
292 brtdef = eparse(vbrtout);
293 else
294 brtdef = NULL;
295 /* allocate scanline */
296 scanout = (COLOR *)emalloc(xres*sizeof(COLOR));
297 /* set input position */
298 yscan = ymax+MIDSCN;
299 /* combine files */
300 for (ypos = yres-1; ypos >= 0; ypos--) {
301 advance();
302 varset(vypos, '=', (double)ypos);
303 for (xpos = 0; xpos < xres; xpos++) {
304 xscan = (long)xpos*xmax/xres;
305 varset(vxpos, '=', (double)xpos);
306 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 d = evalue(coldef[j]);
316 } else {
317 d = 0.0;
318 for (i = 0; i < nfiles; i++)
319 d += colval(input[i].scan[MIDSCN][xscan],j);
320 }
321 if (d < 0.0)
322 d = 0.0;
323 colval(scanout[xpos],j) = d;
324 }
325 }
326 }
327 if (fwritescan(scanout, xres, stdout) < 0) {
328 perror("write error");
329 quit(1);
330 }
331 }
332 efree(scanout);
333 }
334
335
336 advance() /* read in data for next scanline */
337 {
338 extern double fabs();
339 int ytarget;
340 register COLOR *st;
341 register int i, j;
342
343 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 if (freadscan(st, xmax, input[i].fp) < 0) { /* read */
352 eputs(input[i].name);
353 eputs(": read error\n");
354 quit(1);
355 }
356 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 }
362 }
363
364
365 double
366 l_expos(nam) /* return picture exposure */
367 register char *nam;
368 {
369 register int fn, n;
370
371 fn = argument(1) - .5;
372 if (fn < 0 || fn >= nfiles)
373 return(1.0);
374 if (nam == vbrtexp)
375 return(bright(input[fn].expos));
376 n = 3;
377 while (n--)
378 if (nam == vcolexp[n])
379 return(colval(input[fn].expos,n));
380 eputs("Bad call to l_expos()!\n");
381 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 d = argument(1);
394 if (d > -.5 && d < .5)
395 return((double)nfiles);
396 fn = d - .5;
397 if (fn < 0 || fn >= nfiles) {
398 errno = EDOM;
399 return(0.0);
400 }
401 xoff = yoff = 0;
402 n = nargum();
403 if (n >= 2) {
404 d = argument(2);
405 if (d < 0.0) {
406 xoff = d-.5;
407 if (xscan+xoff < 0)
408 xoff = -xscan;
409 } else {
410 xoff = d+.5;
411 if (xscan+xoff >= xmax)
412 xoff = xmax-1-xscan;
413 }
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 if (yscan+yoff < 0)
422 yoff = -yscan;
423 } else {
424 yoff = d+.5;
425 if (yoff+MIDSCN >= WINSIZ)
426 yoff = WINSIZ-1-MIDSCN;
427 if (yscan+yoff >= ymax)
428 yoff = ymax-1-yscan;
429 }
430 }
431 if (nam == vbrtin)
432 return(bright(input[fn].scan[MIDSCN+yoff][xscan+xoff]));
433 n = 3;
434 while (n--)
435 if (nam == vcolin[n])
436 return(colval(input[fn].scan[MIDSCN+yoff][xscan+xoff],n));
437 eputs("Bad call to l_colin()!\n");
438 quit(1);
439 }
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 quit(code) /* exit gracefully */
458 int code;
459 {
460 register int i;
461 /* close input files */
462 for (i = 0; i < nfiles; i++)
463 if (input[i].name == Command)
464 pclose(input[i].fp);
465 else
466 fclose(input[i].fp);
467 exit(code);
468 }