ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/px/pcomb.c
Revision: 1.9
Committed: Thu Apr 18 10:19:48 1991 UTC (33 years ago) by greg
Content type: text/plain
Branch: MAIN
Changes since 1.8: +24 -2 lines
Log Message:
added -o option for original values

File Contents

# Content
1 /* Copyright (c) 1991 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 "calcomp.h"
20
21 #define MAXINP 32 /* maximum number of input files */
22
23 struct {
24 char *name; /* file name */
25 FILE *fp; /* stream pointer */
26 COLOR *scan; /* input scanline */
27 COLOR coef; /* coefficient */
28 } input[MAXINP]; /* input pictures */
29
30 int nfiles; /* number of input files */
31
32 char *vcolin[3] = {"ri", "gi", "bi"};
33 char *vcolout[3] = {"ro", "go", "bo"};
34 #define vbrtin "li"
35 #define vbrtout "lo"
36
37 #define vnfiles "nfiles"
38 #define vxres "xres"
39 #define vyres "yres"
40 #define vxpos "x"
41 #define vypos "y"
42
43 int nowarn = 0; /* no warning messages? */
44
45 int original = 0; /* origninal values? */
46
47 int xres=0, yres=0; /* picture resolution */
48
49 int xpos, ypos; /* picture position */
50
51
52 tputs(s) /* put out string preceded by a tab */
53 char *s;
54 {
55 double d;
56 COLOR ctmp;
57 /* echo header line */
58 putchar('\t');
59 fputs(s, stdout);
60 if (!original)
61 return;
62 /* check for exposure */
63 if (isexpos(s)) {
64 d = 1.0/exposval(s);
65 scalecolor(input[nfiles].coef, d);
66 } else if (iscolcor(s)) {
67 colcorval(ctmp, s);
68 colval(input[nfiles].coef,RED) /= colval(ctmp,RED);
69 colval(input[nfiles].coef,GRN) /= colval(ctmp,GRN);
70 colval(input[nfiles].coef,BLU) /= colval(ctmp,BLU);
71 }
72 }
73
74
75 main(argc, argv)
76 int argc;
77 char *argv[];
78 {
79 extern double l_redin(), l_grnin(), l_bluin(), l_brtin(), atof();
80 double f;
81 int a;
82
83 funset(vcolin[RED], 1, l_redin);
84 funset(vcolin[GRN], 1, l_grnin);
85 funset(vcolin[BLU], 1, l_bluin);
86 funset(vbrtin, 1, l_brtin);
87
88 for (a = 1; a < argc; a++)
89 if (argv[a][0] == '-')
90 switch (argv[a][1]) {
91 case '\0':
92 case 's':
93 case 'c':
94 goto getfiles;
95 case 'x':
96 xres = atoi(argv[++a]);
97 break;
98 case 'y':
99 yres = atoi(argv[++a]);
100 break;
101 case 'w':
102 nowarn = !nowarn;
103 break;
104 case 'o':
105 original = !original;
106 break;
107 case 'f':
108 fcompile(argv[++a]);
109 break;
110 case 'e':
111 scompile(argv[++a], NULL, 0);
112 break;
113 default:
114 goto usage;
115 }
116 else
117 break;
118 getfiles:
119 for (nfiles = 0; nfiles < MAXINP; nfiles++)
120 setcolor(input[nfiles].coef, 1.0, 1.0, 1.0);
121 nfiles = 0;
122 for ( ; a < argc; a++) {
123 if (nfiles >= MAXINP) {
124 eputs(argv[0]);
125 eputs(": too many picture files\n");
126 quit(1);
127 }
128 if (argv[a][0] == '-')
129 switch (argv[a][1]) {
130 case '\0':
131 input[nfiles].name = "<stdin>";
132 input[nfiles].fp = stdin;
133 break;
134 case 's':
135 f = atof(argv[++a]);
136 scalecolor(input[nfiles].coef, f);
137 continue;
138 case 'c':
139 colval(input[nfiles].coef,RED)*=atof(argv[++a]);
140 colval(input[nfiles].coef,GRN)*=atof(argv[++a]);
141 colval(input[nfiles].coef,BLU)*=atof(argv[++a]);
142 continue;
143 default:
144 goto usage;
145 }
146 else {
147 input[nfiles].name = argv[a];
148 input[nfiles].fp = fopen(argv[a], "r");
149 if (input[nfiles].fp == NULL) {
150 perror(argv[a]);
151 quit(1);
152 }
153 }
154 fputs(input[nfiles].name, stdout);
155 fputs(":\n", stdout);
156 getheader(input[nfiles].fp, tputs);
157 if (fgetresolu(&xpos, &ypos, input[nfiles].fp) !=
158 (YMAJOR|YDECR)) {
159 eputs(input[nfiles].name);
160 eputs(": bad picture size\n");
161 quit(1);
162 }
163 if (xres == 0 && yres == 0) {
164 xres = xpos;
165 yres = ypos;
166 } else if (xpos != xres || ypos != yres) {
167 eputs(argv[0]);
168 eputs(": resolution mismatch\n");
169 quit(1);
170 }
171 input[nfiles].scan = (COLOR *)emalloc(xres*sizeof(COLOR));
172 nfiles++;
173 }
174 printargs(argc, argv, stdout);
175 putchar('\n');
176 fputresolu(YMAJOR|YDECR, xres, yres, stdout);
177 combine();
178 quit(0);
179 usage:
180 eputs("Usage: ");
181 eputs(argv[0]);
182 eputs(
183 " [-w][-h][-x xr][-y yr][-e expr][-f file] [ [-s f][-c r g b] pic ..]\n");
184 quit(1);
185 }
186
187
188 combine() /* combine pictures */
189 {
190 EPNODE *coldef[3], *brtdef;
191 COLOR *scanout;
192 double d;
193 register int i, j;
194 /* check defined variables */
195 for (j = 0; j < 3; j++) {
196 if (vardefined(vcolout[j]))
197 coldef[j] = eparse(vcolout[j]);
198 else
199 coldef[j] = NULL;
200 }
201 if (vardefined(vbrtout))
202 brtdef = eparse(vbrtout);
203 else
204 brtdef = NULL;
205 /* predefine variables */
206 varset(vnfiles, (double)nfiles);
207 varset(vxres, (double)xres);
208 varset(vyres, (double)yres);
209 /* allocate scanline */
210 scanout = (COLOR *)emalloc(xres*sizeof(COLOR));
211 /* combine files */
212 for (ypos = yres-1; ypos >= 0; ypos--) {
213 for (i = 0; i < nfiles; i++)
214 if (freadscan(input[i].scan, xres, input[i].fp) < 0) {
215 eputs(input[i].name);
216 eputs(": read error\n");
217 quit(1);
218 }
219 varset(vypos, (double)ypos);
220 for (xpos = 0; xpos < xres; xpos++) {
221 for (i = 0; i < nfiles; i++)
222 multcolor(input[i].scan[xpos],input[i].coef);
223 varset(vxpos, (double)xpos);
224 eclock++;
225 if (brtdef != NULL) {
226 d = evalue(brtdef);
227 if (d < 0.0)
228 d = 0.0;
229 setcolor(scanout[xpos], d, d, d);
230 } else {
231 for (j = 0; j < 3; j++) {
232 if (coldef[j] != NULL) {
233 colval(scanout[xpos],j) = evalue(coldef[j]);
234 } else {
235 colval(scanout[xpos],j) = 0.0;
236 for (i = 0; i < nfiles; i++)
237 colval(scanout[xpos],j) +=
238 colval(input[i].scan[xpos],j);
239 }
240 if (colval(scanout[xpos],j) < 0.0)
241 colval(scanout[xpos],j) = 0.0;
242 }
243 }
244 }
245 if (fwritescan(scanout, xres, stdout) < 0) {
246 perror("write error");
247 quit(1);
248 }
249 }
250 efree(scanout);
251 }
252
253
254 double
255 colin(fn, ci) /* return color value for picture */
256 register int fn, ci;
257 {
258 if (fn == 0)
259 return((double)nfiles);
260 if (fn < 1 || fn > nfiles) {
261 errno = EDOM;
262 return(0.0);
263 }
264 return(colval(input[fn-1].scan[xpos],ci));
265 }
266
267
268 double
269 l_redin() /* get red color */
270 {
271 return(colin((int)(argument(1)+.5), RED));
272 }
273
274
275 double
276 l_grnin() /* get green color */
277 {
278 return(colin((int)(argument(1)+.5), GRN));
279 }
280
281
282 double
283 l_bluin() /* get blue color */
284 {
285 return(colin((int)(argument(1)+.5), BLU));
286 }
287
288
289 double
290 l_brtin() /* get brightness value */
291 {
292 register int fn;
293
294 fn = argument(1)+.5;
295 if (fn == 0)
296 return((double)nfiles);
297 if (fn < 1 || fn > nfiles) {
298 errno = EDOM;
299 return(0.0);
300 }
301 return(bright(input[fn-1].scan[xpos]));
302 }
303
304
305 wputs(msg)
306 char *msg;
307 {
308 if (!nowarn)
309 eputs(msg);
310 }
311
312
313 eputs(msg)
314 char *msg;
315 {
316 fputs(msg, stderr);
317 }
318
319
320 quit(code)
321 int code;
322 {
323 exit(code);
324 }