| 16 |
|
|
| 17 |
|
#include "color.h" |
| 18 |
|
|
| 19 |
+ |
#include "resolu.h" |
| 20 |
+ |
|
| 21 |
|
#include "calcomp.h" |
| 22 |
|
|
| 23 |
|
#define MAXINP 32 /* maximum number of input files */ |
| 24 |
|
#define WINSIZ 9 /* scanline window size */ |
| 25 |
|
#define MIDSCN 4 /* current scan position */ |
| 26 |
|
|
| 25 |
– |
#define BRT (-1) /* special index for brightness */ |
| 26 |
– |
|
| 27 |
|
struct { |
| 28 |
< |
char *name; /* file name */ |
| 28 |
> |
char *name; /* file or command name */ |
| 29 |
|
FILE *fp; /* stream pointer */ |
| 30 |
|
COLOR *scan[WINSIZ]; /* input scanline window */ |
| 31 |
|
COLOR coef; /* coefficient */ |
| 34 |
|
|
| 35 |
|
int nfiles; /* number of input files */ |
| 36 |
|
|
| 37 |
< |
char *vcolin[3] = {"ri", "gi", "bi"}; |
| 38 |
< |
char *vcolout[3] = {"ro", "go", "bo"}; |
| 37 |
> |
char vcolin[3][4] = {"ri", "gi", "bi"}; |
| 38 |
> |
char vcolout[3][4] = {"ro", "go", "bo"}; |
| 39 |
|
char vbrtin[] = "li"; |
| 40 |
|
char vbrtout[] = "lo"; |
| 41 |
< |
char *vcolexp[3] = {"re", "ge", "be"}; |
| 41 |
> |
char vcolexp[3][4] = {"re", "ge", "be"}; |
| 42 |
|
char vbrtexp[] = "le"; |
| 43 |
|
|
| 44 |
< |
#define vnfiles "nfiles" |
| 45 |
< |
#define vxres "xres" |
| 46 |
< |
#define vyres "yres" |
| 47 |
< |
#define vxpos "x" |
| 48 |
< |
#define vypos "y" |
| 44 |
> |
char vnfiles[] = "nfiles"; |
| 45 |
> |
char vxmax[] = "xmax"; |
| 46 |
> |
char vymax[] = "ymax"; |
| 47 |
> |
char vxres[] = "xres"; |
| 48 |
> |
char vyres[] = "yres"; |
| 49 |
> |
char vxpos[] = "x"; |
| 50 |
> |
char vypos[] = "y"; |
| 51 |
|
|
| 52 |
|
int nowarn = 0; /* no warning messages? */ |
| 53 |
|
|
| 54 |
< |
int original = 0; /* origninal values? */ |
| 54 |
> |
int xmax = 0, ymax = 0; /* input resolution */ |
| 55 |
|
|
| 56 |
< |
int xres=0, yres=0; /* picture resolution */ |
| 56 |
> |
int xscan, yscan; /* input position */ |
| 57 |
|
|
| 58 |
< |
int xpos, ypos; /* picture position */ |
| 58 |
> |
int xres, yres; /* output resolution */ |
| 59 |
|
|
| 60 |
+ |
int xpos, ypos; /* output position */ |
| 61 |
+ |
|
| 62 |
|
int wrongformat = 0; |
| 63 |
|
|
| 64 |
+ |
FILE *popen(); |
| 65 |
|
|
| 66 |
+ |
|
| 67 |
|
main(argc, argv) |
| 68 |
|
int argc; |
| 69 |
|
char *argv[]; |
| 70 |
|
{ |
| 71 |
+ |
int original; |
| 72 |
|
double f; |
| 73 |
|
int a, i; |
| 74 |
|
/* scan options */ |
| 76 |
|
if (argv[a][0] == '-') |
| 77 |
|
switch (argv[a][1]) { |
| 78 |
|
case 'x': |
| 72 |
– |
xres = atoi(argv[++a]); |
| 73 |
– |
continue; |
| 79 |
|
case 'y': |
| 80 |
< |
yres = atoi(argv[++a]); |
| 80 |
> |
a++; |
| 81 |
|
continue; |
| 82 |
|
case 'w': |
| 83 |
|
nowarn = !nowarn; |
| 95 |
|
setcolor(input[nfiles].expos, 1.0, 1.0, 1.0); |
| 96 |
|
} |
| 97 |
|
nfiles = 0; |
| 98 |
+ |
original = 0; |
| 99 |
|
for ( ; a < argc; a++) { |
| 100 |
|
if (nfiles >= MAXINP) { |
| 101 |
|
eputs(argv[0]); |
| 110 |
|
break; |
| 111 |
|
case 'o': |
| 112 |
|
original++; |
| 113 |
< |
break; |
| 113 |
> |
continue; |
| 114 |
|
case 's': |
| 115 |
|
f = atof(argv[++a]); |
| 116 |
|
scalecolor(input[nfiles].coef, f); |
| 125 |
|
} |
| 126 |
|
else { |
| 127 |
|
input[nfiles].name = argv[a]; |
| 128 |
< |
input[nfiles].fp = fopen(argv[a], "r"); |
| 128 |
> |
input[nfiles].fp = argv[a][0]=='!' ? |
| 129 |
> |
popen(argv[a]+1, "r") : |
| 130 |
> |
fopen(argv[a], "r"); |
| 131 |
|
if (input[nfiles].fp == NULL) { |
| 132 |
|
perror(argv[a]); |
| 133 |
|
quit(1); |
| 134 |
|
} |
| 135 |
|
} |
| 136 |
|
checkfile(); |
| 137 |
< |
original = 0; |
| 137 |
> |
if (original) { |
| 138 |
> |
colval(input[nfiles].coef,RED) /= |
| 139 |
> |
colval(input[nfiles].expos,RED); |
| 140 |
> |
colval(input[nfiles].coef,GRN) /= |
| 141 |
> |
colval(input[nfiles].expos,GRN); |
| 142 |
> |
colval(input[nfiles].coef,BLU) /= |
| 143 |
> |
colval(input[nfiles].expos,BLU); |
| 144 |
> |
} |
| 145 |
|
nfiles++; |
| 146 |
+ |
original = 0; |
| 147 |
|
} |
| 148 |
< |
init(); /* set constant expressions */ |
| 148 |
> |
init(); /* set constants */ |
| 149 |
|
/* go back and get expressions */ |
| 150 |
|
for (a = 1; a < argc; a++) { |
| 151 |
|
if (argv[a][0] == '-') |
| 152 |
|
switch (argv[a][1]) { |
| 153 |
|
case 'x': |
| 154 |
+ |
varset(vxres, ':', eval(argv[++a])); |
| 155 |
+ |
continue; |
| 156 |
|
case 'y': |
| 157 |
< |
a++; |
| 157 |
> |
varset(vyres, ':', eval(argv[++a])); |
| 158 |
|
continue; |
| 159 |
|
case 'w': |
| 160 |
|
continue; |
| 167 |
|
} |
| 168 |
|
break; |
| 169 |
|
} |
| 170 |
+ |
/* set/get output resolution */ |
| 171 |
+ |
if (!vardefined(vxres)) |
| 172 |
+ |
varset(vxres, ':', (double)xmax); |
| 173 |
+ |
if (!vardefined(vyres)) |
| 174 |
+ |
varset(vyres, ':', (double)ymax); |
| 175 |
+ |
xres = varvalue(vxres) + .5; |
| 176 |
+ |
yres = varvalue(vyres) + .5; |
| 177 |
+ |
if (xres <= 0 || yres <= 0) { |
| 178 |
+ |
eputs(argv[0]); |
| 179 |
+ |
eputs(": illegal output resolution\n"); |
| 180 |
+ |
quit(1); |
| 181 |
+ |
} |
| 182 |
|
/* complete header */ |
| 183 |
|
printargs(argc, argv, stdout); |
| 184 |
|
fputformat(COLRFMT, stdout); |
| 185 |
|
putchar('\n'); |
| 186 |
< |
fputresolu(YMAJOR|YDECR, xres, yres, stdout); |
| 186 |
> |
fprtresolu(xres, yres, stdout); |
| 187 |
|
/* combine pictures */ |
| 188 |
|
combine(); |
| 189 |
|
quit(0); |
| 191 |
|
eputs("Usage: "); |
| 192 |
|
eputs(argv[0]); |
| 193 |
|
eputs( |
| 194 |
< |
" [-w][-h][-x xr][-y yr][-e expr][-f file] [ [-s f][-c r g b] pic ..]\n"); |
| 194 |
> |
" [-w][-x xr][-y yr][-e expr][-f file] [ [-s f][-c r g b] pic ..]\n"); |
| 195 |
|
quit(1); |
| 196 |
|
} |
| 197 |
|
|
| 211 |
|
if (isexpos(s)) { /* exposure */ |
| 212 |
|
d = exposval(s); |
| 213 |
|
scalecolor(input[nfiles].expos, d); |
| 184 |
– |
if (original) |
| 185 |
– |
scalecolor(input[nfiles].coef, 1.0/d); |
| 214 |
|
} else if (iscolcor(s)) { /* color correction */ |
| 215 |
|
colcorval(ctmp, s); |
| 216 |
|
multcolor(input[nfiles].expos, ctmp); |
| 189 |
– |
if (original) { |
| 190 |
– |
colval(input[nfiles].coef,RED) /= colval(ctmp,RED); |
| 191 |
– |
colval(input[nfiles].coef,GRN) /= colval(ctmp,GRN); |
| 192 |
– |
colval(input[nfiles].coef,BLU) /= colval(ctmp,BLU); |
| 193 |
– |
} |
| 217 |
|
} |
| 218 |
|
/* echo line */ |
| 219 |
|
putchar('\t'); |
| 223 |
|
|
| 224 |
|
checkfile() /* ready a file */ |
| 225 |
|
{ |
| 226 |
+ |
int xinp, yinp; |
| 227 |
|
register int i; |
| 228 |
|
/* process header */ |
| 229 |
|
fputs(input[nfiles].name, stdout); |
| 234 |
|
eputs(": not in Radiance picture format\n"); |
| 235 |
|
quit(1); |
| 236 |
|
} |
| 237 |
< |
if (fgetresolu(&xpos, &ypos, input[nfiles].fp) != (YMAJOR|YDECR)) { |
| 237 |
> |
if (fgetresolu(&xinp, &yinp, input[nfiles].fp) < 0) { |
| 238 |
|
eputs(input[nfiles].name); |
| 239 |
|
eputs(": bad picture size\n"); |
| 240 |
|
quit(1); |
| 241 |
|
} |
| 242 |
< |
if (xres == 0 && yres == 0) { |
| 243 |
< |
xres = xpos; |
| 244 |
< |
yres = ypos; |
| 245 |
< |
} else if (xpos != xres || ypos != yres) { |
| 242 |
> |
if (xmax == 0 && ymax == 0) { |
| 243 |
> |
xmax = xinp; |
| 244 |
> |
ymax = yinp; |
| 245 |
> |
} else if (xinp != xmax || yinp != ymax) { |
| 246 |
|
eputs(input[nfiles].name); |
| 247 |
|
eputs(": resolution mismatch\n"); |
| 248 |
|
quit(1); |
| 249 |
|
} |
| 250 |
|
/* allocate scanlines */ |
| 251 |
|
for (i = 0; i < WINSIZ; i++) |
| 252 |
< |
input[nfiles].scan[i] = (COLOR *)emalloc(xres*sizeof(COLOR)); |
| 252 |
> |
input[nfiles].scan[i] = (COLOR *)emalloc(xmax*sizeof(COLOR)); |
| 253 |
|
} |
| 254 |
|
|
| 255 |
|
|
| 256 |
< |
init() /* perform final setup */ |
| 256 |
> |
init() /* perform final setup */ |
| 257 |
|
{ |
| 258 |
|
double l_colin(), l_expos(); |
| 259 |
|
register int i; |
| 236 |
– |
/* prime input */ |
| 237 |
– |
for (ypos = yres+(MIDSCN-1); ypos >= yres; ypos--) |
| 238 |
– |
advance(); |
| 260 |
|
/* define constants */ |
| 261 |
|
varset(vnfiles, ':', (double)nfiles); |
| 262 |
< |
varset(vxres, ':', (double)xres); |
| 263 |
< |
varset(vyres, ':', (double)yres); |
| 262 |
> |
varset(vxmax, ':', (double)xmax); |
| 263 |
> |
varset(vymax, ':', (double)ymax); |
| 264 |
|
/* set functions */ |
| 265 |
|
for (i = 0; i < 3; i++) { |
| 266 |
|
funset(vcolexp[i], 1, ':', l_expos); |
| 290 |
|
brtdef = NULL; |
| 291 |
|
/* allocate scanline */ |
| 292 |
|
scanout = (COLOR *)emalloc(xres*sizeof(COLOR)); |
| 293 |
+ |
/* set input position */ |
| 294 |
+ |
yscan = ymax+MIDSCN; |
| 295 |
|
/* combine files */ |
| 296 |
|
for (ypos = yres-1; ypos >= 0; ypos--) { |
| 297 |
|
advance(); |
| 298 |
|
varset(vypos, '=', (double)ypos); |
| 299 |
|
for (xpos = 0; xpos < xres; xpos++) { |
| 300 |
+ |
xscan = (long)xpos*xmax/xres; |
| 301 |
|
varset(vxpos, '=', (double)xpos); |
| 302 |
|
eclock++; |
| 303 |
|
if (brtdef != NULL) { |
| 312 |
|
} else { |
| 313 |
|
d = 0.0; |
| 314 |
|
for (i = 0; i < nfiles; i++) |
| 315 |
< |
d += colval(input[i].scan[MIDSCN][xpos],j); |
| 315 |
> |
d += colval(input[i].scan[MIDSCN][xscan],j); |
| 316 |
|
} |
| 317 |
|
if (d < 0.0) |
| 318 |
|
d = 0.0; |
| 329 |
|
} |
| 330 |
|
|
| 331 |
|
|
| 332 |
< |
advance() /* read in next scanline */ |
| 332 |
> |
advance() /* read in data for next scanline */ |
| 333 |
|
{ |
| 334 |
+ |
int ytarget; |
| 335 |
|
register COLOR *st; |
| 336 |
|
register int i, j; |
| 337 |
|
|
| 338 |
< |
for (i = 0; i < nfiles; i++) { |
| 339 |
< |
st = input[i].scan[WINSIZ-1]; |
| 340 |
< |
for (j = WINSIZ-1; j > 0; j--) /* rotate window */ |
| 341 |
< |
input[i].scan[j] = input[i].scan[j-1]; |
| 342 |
< |
input[i].scan[0] = st; |
| 343 |
< |
if (ypos < MIDSCN) /* hit bottom */ |
| 344 |
< |
continue; |
| 345 |
< |
if (freadscan(st, xres, input[i].fp) < 0) { |
| 346 |
< |
eputs(input[i].name); |
| 347 |
< |
eputs(": read error\n"); |
| 348 |
< |
quit(1); |
| 338 |
> |
for (ytarget = (long)ypos*ymax/yres; yscan > ytarget; yscan--) |
| 339 |
> |
for (i = 0; i < nfiles; i++) { |
| 340 |
> |
st = input[i].scan[WINSIZ-1]; |
| 341 |
> |
for (j = WINSIZ-1; j > 0; j--) /* rotate window */ |
| 342 |
> |
input[i].scan[j] = input[i].scan[j-1]; |
| 343 |
> |
input[i].scan[0] = st; |
| 344 |
> |
if (yscan <= MIDSCN) /* hit bottom? */ |
| 345 |
> |
continue; |
| 346 |
> |
if (freadscan(st, xmax, input[i].fp) < 0) { /* read */ |
| 347 |
> |
eputs(input[i].name); |
| 348 |
> |
eputs(": read error\n"); |
| 349 |
> |
quit(1); |
| 350 |
> |
} |
| 351 |
> |
for (j = 0; j < xmax; j++) /* adjust color */ |
| 352 |
> |
multcolor(st[j], input[i].coef); |
| 353 |
|
} |
| 325 |
– |
for (j = 0; j < xres; j++) /* adjust color */ |
| 326 |
– |
multcolor(st[j], input[i].coef); |
| 327 |
– |
} |
| 354 |
|
} |
| 355 |
|
|
| 356 |
|
|
| 359 |
|
register char *nam; |
| 360 |
|
{ |
| 361 |
|
register int fn, n; |
| 336 |
– |
double d; |
| 362 |
|
|
| 363 |
< |
d = argument(1); |
| 364 |
< |
if (d > -.5 && d < .5) |
| 365 |
< |
return((double)nfiles); |
| 341 |
< |
fn = d - .5; |
| 342 |
< |
if (fn < 0 || fn >= nfiles) { |
| 343 |
< |
errno = EDOM; |
| 344 |
< |
return(0.0); |
| 345 |
< |
} |
| 363 |
> |
fn = argument(1) - .5; |
| 364 |
> |
if (fn < 0 || fn >= nfiles) |
| 365 |
> |
return(1.0); |
| 366 |
|
if (nam == vbrtexp) |
| 367 |
|
return(bright(input[fn].expos)); |
| 368 |
|
n = 3; |
| 396 |
|
d = argument(2); |
| 397 |
|
if (d < 0.0) { |
| 398 |
|
xoff = d-.5; |
| 399 |
< |
if (xpos+xoff < 0) |
| 400 |
< |
xoff = -xpos; |
| 399 |
> |
if (xscan+xoff < 0) |
| 400 |
> |
xoff = -xscan; |
| 401 |
|
} else { |
| 402 |
|
xoff = d+.5; |
| 403 |
< |
if (xpos+xoff >= xres) |
| 404 |
< |
xoff = xres-1-xpos; |
| 403 |
> |
if (xscan+xoff >= xmax) |
| 404 |
> |
xoff = xmax-1-xscan; |
| 405 |
|
} |
| 406 |
|
} |
| 407 |
|
if (n >= 3) { |
| 410 |
|
yoff = d-.5; |
| 411 |
|
if (yoff+MIDSCN < 0) |
| 412 |
|
yoff = -MIDSCN; |
| 413 |
< |
if (ypos+yoff < 0) |
| 414 |
< |
yoff = -ypos; |
| 413 |
> |
if (yscan+yoff < 0) |
| 414 |
> |
yoff = -yscan; |
| 415 |
|
} else { |
| 416 |
|
yoff = d+.5; |
| 417 |
|
if (yoff+MIDSCN >= WINSIZ) |
| 418 |
|
yoff = WINSIZ-1-MIDSCN; |
| 419 |
< |
if (ypos+yoff >= yres) |
| 420 |
< |
yoff = yres-1-ypos; |
| 419 |
> |
if (yscan+yoff >= ymax) |
| 420 |
> |
yoff = ymax-1-yscan; |
| 421 |
|
} |
| 422 |
|
} |
| 423 |
|
if (nam == vbrtin) |
| 424 |
< |
return(bright(input[fn].scan[MIDSCN+yoff][xpos+xoff])); |
| 424 |
> |
return(bright(input[fn].scan[MIDSCN+yoff][xscan+xoff])); |
| 425 |
|
n = 3; |
| 426 |
|
while (n--) |
| 427 |
|
if (nam == vcolin[n]) |
| 428 |
< |
return(colval(input[fn].scan[MIDSCN+yoff][xpos+xoff],n)); |
| 428 |
> |
return(colval(input[fn].scan[MIDSCN+yoff][xscan+xoff],n)); |
| 429 |
|
eputs("Bad call to l_colin()!\n"); |
| 430 |
|
quit(1); |
| 431 |
|
} |