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