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; /* input scanline */ |
30 |
> |
COLOR *scan[WINSIZ]; /* input scanline window */ |
31 |
|
COLOR coef; /* coefficient */ |
32 |
|
} input[MAXINP]; /* input pictures */ |
33 |
|
|
35 |
|
|
36 |
|
char *vcolin[3] = {"ri", "gi", "bi"}; |
37 |
|
char *vcolout[3] = {"ro", "go", "bo"}; |
38 |
< |
#define vbrtin "li" |
39 |
< |
#define vbrtout "lo" |
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" |
54 |
|
|
55 |
|
int xpos, ypos; /* picture position */ |
56 |
|
|
57 |
+ |
int wrongformat = 0; |
58 |
|
|
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 |
– |
} |
59 |
|
|
74 |
– |
|
60 |
|
main(argc, argv) |
61 |
|
int argc; |
62 |
|
char *argv[]; |
63 |
|
{ |
79 |
– |
extern double l_redin(), l_grnin(), l_bluin(), l_brtin(), atof(); |
64 |
|
double f; |
65 |
< |
int a; |
66 |
< |
|
67 |
< |
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++) |
65 |
> |
int a, i; |
66 |
> |
/* scan options */ |
67 |
> |
for (a = 1; a < argc; a++) { |
68 |
|
if (argv[a][0] == '-') |
69 |
|
switch (argv[a][1]) { |
91 |
– |
case '\0': |
92 |
– |
case 's': |
93 |
– |
case 'c': |
94 |
– |
goto getfiles; |
70 |
|
case 'x': |
71 |
|
xres = atoi(argv[++a]); |
72 |
< |
break; |
72 |
> |
continue; |
73 |
|
case 'y': |
74 |
|
yres = atoi(argv[++a]); |
75 |
< |
break; |
75 |
> |
continue; |
76 |
|
case 'w': |
77 |
|
nowarn = !nowarn; |
78 |
< |
break; |
78 |
> |
continue; |
79 |
|
case 'o': |
80 |
|
original = !original; |
81 |
< |
break; |
81 |
> |
continue; |
82 |
|
case 'f': |
108 |
– |
fcompile(argv[++a]); |
109 |
– |
break; |
83 |
|
case 'e': |
84 |
< |
scompile(argv[++a], NULL, 0); |
112 |
< |
break; |
113 |
< |
default: |
114 |
< |
goto usage; |
84 |
> |
continue; |
85 |
|
} |
86 |
< |
else |
87 |
< |
break; |
88 |
< |
getfiles: |
86 |
> |
break; |
87 |
> |
} |
88 |
> |
/* process files */ |
89 |
|
for (nfiles = 0; nfiles < MAXINP; nfiles++) |
90 |
|
setcolor(input[nfiles].coef, 1.0, 1.0, 1.0); |
91 |
|
nfiles = 0; |
121 |
|
quit(1); |
122 |
|
} |
123 |
|
} |
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)); |
124 |
|
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: |
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 |
+ |
fputs(":\n", stdout); |
196 |
+ |
getheader(input[n].fp, tputs, NULL); |
197 |
+ |
if (wrongformat) { |
198 |
+ |
eputs(input[n].name); |
199 |
+ |
eputs(": not in Radiance picture format\n"); |
200 |
+ |
quit(1); |
201 |
+ |
} |
202 |
+ |
if (fgetresolu(&xpos, &ypos, input[n].fp) != (YMAJOR|YDECR)) { |
203 |
+ |
eputs(input[n].name); |
204 |
+ |
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 |
+ |
eputs(input[n].name); |
212 |
+ |
eputs(": resolution mismatch\n"); |
213 |
+ |
quit(1); |
214 |
+ |
} |
215 |
+ |
for (i = 0; i < WINSIZ; i++) |
216 |
+ |
input[n].scan[i] = (COLOR *)emalloc(xres*sizeof(COLOR)); |
217 |
+ |
} |
218 |
+ |
/* 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 |
+ |
} |
233 |
+ |
|
234 |
+ |
|
235 |
|
combine() /* combine pictures */ |
236 |
|
{ |
237 |
|
EPNODE *coldef[3], *brtdef; |
249 |
|
brtdef = eparse(vbrtout); |
250 |
|
else |
251 |
|
brtdef = NULL; |
205 |
– |
/* predefine variables */ |
206 |
– |
varset(vnfiles, (double)nfiles); |
207 |
– |
varset(vxres, (double)xres); |
208 |
– |
varset(vyres, (double)yres); |
252 |
|
/* allocate scanline */ |
253 |
|
scanout = (COLOR *)emalloc(xres*sizeof(COLOR)); |
254 |
|
/* combine files */ |
255 |
|
for (ypos = yres-1; ypos >= 0; ypos--) { |
256 |
< |
for (i = 0; i < nfiles; i++) |
257 |
< |
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); |
256 |
> |
advance(); |
257 |
> |
varset(vypos, '=', (double)ypos); |
258 |
|
for (xpos = 0; xpos < xres; xpos++) { |
259 |
< |
for (i = 0; i < nfiles; i++) |
222 |
< |
multcolor(input[i].scan[xpos],input[i].coef); |
223 |
< |
varset(vxpos, (double)xpos); |
259 |
> |
varset(vxpos, '=', (double)xpos); |
260 |
|
eclock++; |
261 |
|
if (brtdef != NULL) { |
262 |
|
d = evalue(brtdef); |
266 |
|
} else { |
267 |
|
for (j = 0; j < 3; j++) { |
268 |
|
if (coldef[j] != NULL) { |
269 |
< |
colval(scanout[xpos],j) = evalue(coldef[j]); |
269 |
> |
d = evalue(coldef[j]); |
270 |
|
} else { |
271 |
< |
colval(scanout[xpos],j) = 0.0; |
271 |
> |
d = 0.0; |
272 |
|
for (i = 0; i < nfiles; i++) |
273 |
< |
colval(scanout[xpos],j) += |
238 |
< |
colval(input[i].scan[xpos],j); |
273 |
> |
d += colval(input[i].scan[MIDSCN][xpos],j); |
274 |
|
} |
275 |
< |
if (colval(scanout[xpos],j) < 0.0) |
276 |
< |
colval(scanout[xpos],j) = 0.0; |
275 |
> |
if (d < 0.0) |
276 |
> |
d = 0.0; |
277 |
> |
colval(scanout[xpos],j) = d; |
278 |
|
} |
279 |
|
} |
280 |
|
} |
287 |
|
} |
288 |
|
|
289 |
|
|
290 |
< |
double |
255 |
< |
colin(fn, ci) /* return color value for picture */ |
256 |
< |
register int fn, ci; |
290 |
> |
advance() /* read in next scanline */ |
291 |
|
{ |
292 |
< |
if (fn == 0) |
293 |
< |
return((double)nfiles); |
294 |
< |
if (fn < 1 || fn > nfiles) { |
295 |
< |
errno = EDOM; |
296 |
< |
return(0.0); |
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 |
|
} |
264 |
– |
return(colval(input[fn-1].scan[xpos],ci)); |
310 |
|
} |
311 |
|
|
312 |
|
|
313 |
|
double |
314 |
< |
l_redin() /* get red color */ |
314 |
> |
l_coef(nam) /* return picture coefficients */ |
315 |
> |
register char *nam; |
316 |
|
{ |
317 |
< |
return(colin((int)(argument(1)+.5), RED)); |
318 |
< |
} |
317 |
> |
register int fn, n; |
318 |
> |
double d; |
319 |
|
|
320 |
< |
|
321 |
< |
double |
322 |
< |
l_grnin() /* get green color */ |
323 |
< |
{ |
324 |
< |
return(colin((int)(argument(1)+.5), GRN)); |
320 |
> |
fn = (d = argument(1)) - .5; |
321 |
> |
if (d <= -.5 || fn >= nfiles) { |
322 |
> |
errno = EDOM; |
323 |
> |
return(0.0); |
324 |
> |
} |
325 |
> |
if (d < .5) |
326 |
> |
return((double)nfiles); |
327 |
> |
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_bluin() /* get blue color */ |
339 |
> |
l_colin(nam) /* return color value for picture */ |
340 |
> |
register char *nam; |
341 |
|
{ |
342 |
< |
return(colin((int)(argument(1)+.5), BLU)); |
343 |
< |
} |
342 |
> |
int fn; |
343 |
> |
register int n, xoff, yoff; |
344 |
> |
double d; |
345 |
|
|
346 |
< |
|
347 |
< |
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) { |
346 |
> |
fn = (d = argument(1)) - .5; |
347 |
> |
if (d <= -.5 || fn >= nfiles) { |
348 |
|
errno = EDOM; |
349 |
|
return(0.0); |
350 |
|
} |
351 |
< |
return(bright(input[fn-1].scan[xpos])); |
351 |
> |
if (d < .5) |
352 |
> |
return((double)nfiles); |
353 |
> |
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 |
> |
if (nam == vbrtin) |
384 |
> |
return(bright(input[fn].scan[MIDSCN+yoff][xpos+xoff])); |
385 |
> |
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 |
|
} |
392 |
|
|
393 |
|
|