ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/px/pcomb.c
Revision: 1.5
Committed: Tue Sep 12 13:04:22 1989 UTC (34 years, 7 months ago) by greg
Content type: text/plain
Branch: MAIN
Changes since 1.4: +3 -2 lines
Log Message:
added calls to get/put picture resolution (bug fix)

File Contents

# Content
1 /* Copyright (c) 1989 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 16 /* 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
35 #define vxpos "x"
36 #define vypos "y"
37
38 int nowarn = 0; /* no warning messages? */
39
40 int xres=0, yres=0; /* picture resolution */
41
42 int xpos, ypos; /* picture position */
43
44
45 tputs(s) /* put out string preceded by a tab */
46 char *s;
47 {
48 putchar('\t');
49 fputs(s, stdout);
50 }
51
52
53 main(argc, argv)
54 int argc;
55 char *argv[];
56 {
57 extern double l_redin(), l_grnin(), l_bluin(), atof();
58 double f;
59 int a;
60
61 funset(vcolin[RED], 1, l_redin);
62 funset(vcolin[GRN], 1, l_grnin);
63 funset(vcolin[BLU], 1, l_bluin);
64
65 for (a = 1; a < argc; a++)
66 if (argv[a][0] == '-')
67 switch (argv[a][1]) {
68 case '\0':
69 case 's':
70 case 'c':
71 goto getfiles;
72 case 'x':
73 xres = atoi(argv[++a]);
74 break;
75 case 'y':
76 yres = atoi(argv[++a]);
77 break;
78 case 'w':
79 nowarn = !nowarn;
80 break;
81 case 'f':
82 fcompile(argv[++a]);
83 break;
84 case 'e':
85 scompile(NULL, argv[++a]);
86 break;
87 default:
88 goto usage;
89 }
90 else
91 break;
92 getfiles:
93 for (nfiles = 0; nfiles < MAXINP; nfiles++)
94 setcolor(input[nfiles].coef, 1.0, 1.0, 1.0);
95 nfiles = 0;
96 for ( ; a < argc; a++) {
97 if (nfiles >= MAXINP) {
98 eputs(argv[0]);
99 eputs(": too many picture files\n");
100 quit(1);
101 }
102 if (argv[a][0] == '-')
103 switch (argv[a][1]) {
104 case '\0':
105 input[nfiles].name = "<stdin>";
106 input[nfiles].fp = stdin;
107 break;
108 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 input[nfiles].name = argv[a];
122 input[nfiles].fp = fopen(argv[a], "r");
123 if (input[nfiles].fp == NULL) {
124 eputs(argv[a]);
125 eputs(": cannot open\n");
126 quit(1);
127 }
128 }
129 fputs(input[nfiles].name, stdout);
130 fputs(":\n", stdout);
131 getheader(input[nfiles].fp, tputs);
132 if (fgetresolu(&xpos, &ypos, input[nfiles].fp) !=
133 (YMAJOR|YDECR)) {
134 eputs(input[nfiles].name);
135 eputs(": bad picture size\n");
136 quit(1);
137 }
138 if (xres == 0 && yres == 0) {
139 xres = xpos;
140 yres = ypos;
141 } else if (xpos != xres || ypos != yres) {
142 eputs(argv[0]);
143 eputs(": resolution mismatch\n");
144 quit(1);
145 }
146 input[nfiles].scan = (COLOR *)emalloc(xres*sizeof(COLOR));
147 nfiles++;
148 }
149 printargs(argc, argv, stdout);
150 putchar('\n');
151 fputresolu(YMAJOR|YDECR, xres, yres, stdout);
152 combine();
153 quit(0);
154 usage:
155 eputs("Usage: ");
156 eputs(argv[0]);
157 eputs(" [-w][-x xr][-y yr][-e expr][-f file] [ [-s f][-c r g b] picture ..]\n");
158 quit(1);
159 }
160
161
162 combine() /* combine pictures */
163 {
164 EPNODE *coldef[3];
165 COLOR *scanout;
166 register int i, j;
167 /* check defined variables */
168 for (j = 0; j < 3; j++) {
169 if (vardefined(vcolout[j]))
170 coldef[j] = eparse(vcolout[j]);
171 else
172 coldef[j] = NULL;
173 }
174 /* allocate scanline */
175 scanout = (COLOR *)emalloc(xres*sizeof(COLOR));
176 /* combine files */
177 for (ypos = yres-1; ypos >= 0; ypos--) {
178 for (i = 0; i < nfiles; i++)
179 if (freadscan(input[i].scan, xres, input[i].fp) < 0) {
180 eputs(input[i].name);
181 eputs(": read error\n");
182 quit(1);
183 }
184 varset(vypos, (double)ypos);
185 for (xpos = 0; xpos < xres; xpos++) {
186 varset(vxpos, (double)xpos);
187 eclock++;
188 for (j = 0; j < 3; j++) {
189 if (coldef[j] != NULL) {
190 colval(scanout[xpos],j) =
191 evalue(coldef[j]);
192 } else {
193 colval(scanout[xpos],j) = 0.0;
194 for (i = 0; i < nfiles; i++)
195 colval(scanout[xpos],j) +=
196 colval(input[i].coef,j) *
197 colval(input[i].scan[xpos],j);
198 }
199 if (colval(scanout[xpos],j) < 0.0)
200 colval(scanout[xpos],j) = 0.0;
201 }
202 }
203 if (fwritescan(scanout, xres, stdout) < 0) {
204 eputs("write error\n");
205 quit(1);
206 }
207 }
208 efree(scanout);
209 }
210
211
212 double
213 colin(fn, ci) /* return color value for picture */
214 register int fn, ci;
215 {
216 if (fn == 0)
217 return((double)nfiles);
218 if (fn < 1 || fn > nfiles) {
219 errno = EDOM;
220 return(0.0);
221 }
222 return(colval(input[fn-1].scan[xpos],ci));
223 }
224
225
226 double
227 l_redin() /* get red color */
228 {
229 return(colin((int)(argument(1)+.5), RED));
230 }
231
232
233 double
234 l_grnin() /* get green color */
235 {
236 return(colin((int)(argument(1)+.5), GRN));
237 }
238
239
240 double
241 l_bluin() /* get blue color */
242 {
243 return(colin((int)(argument(1)+.5), BLU));
244 }
245
246
247 wputs(msg)
248 char *msg;
249 {
250 if (!nowarn)
251 eputs(msg);
252 }
253
254
255 eputs(msg)
256 char *msg;
257 {
258 fputs(msg, stderr);
259 }
260
261
262 quit(code)
263 int code;
264 {
265 exit(code);
266 }