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