| 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 |
* pfilt.c - program to post-process picture file. |
| 9 |
* |
| 10 |
* 9/26/85 |
| 11 |
*/ |
| 12 |
|
| 13 |
#include <stdio.h> |
| 14 |
|
| 15 |
#include <signal.h> |
| 16 |
|
| 17 |
#include "color.h" |
| 18 |
|
| 19 |
#include "resolu.h" |
| 20 |
|
| 21 |
extern char *malloc(); |
| 22 |
extern float *matchlamp(); |
| 23 |
|
| 24 |
#define FEQ(a,b) ((a) >= .98*(b) && (a) <= 1.02*(b)) |
| 25 |
|
| 26 |
#define CHECKRAD 1.5 /* radius to check for filtering */ |
| 27 |
|
| 28 |
COLOR exposure = WHTCOLOR; /* exposure for the frame */ |
| 29 |
|
| 30 |
double rad = 0.0; /* output pixel radius for filtering */ |
| 31 |
|
| 32 |
int nrows = 0; /* number of rows for output */ |
| 33 |
int ncols = 0; /* number of columns for output */ |
| 34 |
|
| 35 |
double x_c = 1.0; /* ratio of output x size to input */ |
| 36 |
double y_r = 1.0; /* ratio of output y size to input */ |
| 37 |
|
| 38 |
int singlepass = 0; /* true means skip first pass */ |
| 39 |
|
| 40 |
int avghot = 0; /* true means average in bright spots */ |
| 41 |
|
| 42 |
double hotlvl = 1000.0; /* level considered "hot" */ |
| 43 |
|
| 44 |
int npts = 0; /* (half) number of points for stars */ |
| 45 |
|
| 46 |
double spread = 1e-4; /* spread for star points */ |
| 47 |
|
| 48 |
#define TEMPLATE "/usr/tmp/pfXXXXXX" |
| 49 |
|
| 50 |
char *tfname = NULL; |
| 51 |
|
| 52 |
char *lampdat = "lamp.tab"; /* lamp data file */ |
| 53 |
|
| 54 |
int order; /* scanline ordering of input */ |
| 55 |
int xres, yres; /* resolution of input */ |
| 56 |
double inpaspect = 1.0; /* pixel aspect ratio of input */ |
| 57 |
int correctaspect = 0; /* aspect ratio correction? */ |
| 58 |
|
| 59 |
int wrongformat = 0; |
| 60 |
|
| 61 |
int xrad; /* x window size */ |
| 62 |
int yrad; /* y window size */ |
| 63 |
|
| 64 |
int barsize; /* size of input scan bar */ |
| 65 |
COLOR **scanin; /* input scan bar */ |
| 66 |
COLOR *scanout; /* output scan line */ |
| 67 |
|
| 68 |
char *progname; |
| 69 |
|
| 70 |
|
| 71 |
main(argc, argv) |
| 72 |
int argc; |
| 73 |
char **argv; |
| 74 |
{ |
| 75 |
extern char *mktemp(); |
| 76 |
extern double atof(), pow(); |
| 77 |
extern long ftell(); |
| 78 |
extern int quit(), headline(); |
| 79 |
FILE *fin; |
| 80 |
float *lampcolor; |
| 81 |
char *lamptype = NULL; |
| 82 |
long fpos; |
| 83 |
double outaspect = 0.0; |
| 84 |
double d; |
| 85 |
int i, j; |
| 86 |
|
| 87 |
if (signal(SIGINT, quit) == SIG_IGN) |
| 88 |
signal(SIGINT, SIG_IGN); |
| 89 |
if (signal(SIGHUP, quit) == SIG_IGN) |
| 90 |
signal(SIGINT, SIG_IGN); |
| 91 |
signal(SIGTERM, quit); |
| 92 |
signal(SIGPIPE, quit); |
| 93 |
#ifdef SIGXCPU |
| 94 |
signal(SIGXCPU, quit); |
| 95 |
signal(SIGXFSZ, quit); |
| 96 |
#endif |
| 97 |
|
| 98 |
progname = argv[0]; |
| 99 |
|
| 100 |
for (i = 1; i < argc; i++) |
| 101 |
if (argv[i][0] == '-') |
| 102 |
switch (argv[i][1]) { |
| 103 |
case 'x': |
| 104 |
i++; |
| 105 |
if (argv[i][0] == '/') { |
| 106 |
x_c = 1.0/atof(argv[i]+1); |
| 107 |
ncols = 0; |
| 108 |
} else |
| 109 |
ncols = atoi(argv[i]); |
| 110 |
break; |
| 111 |
case 'y': |
| 112 |
i++; |
| 113 |
if (argv[i][0] == '/') { |
| 114 |
y_r = 1.0/atof(argv[i]+1); |
| 115 |
nrows = 0; |
| 116 |
} else |
| 117 |
nrows = atoi(argv[i]); |
| 118 |
break; |
| 119 |
case 'c': |
| 120 |
correctaspect = !correctaspect; |
| 121 |
break; |
| 122 |
case 'p': |
| 123 |
i++; |
| 124 |
outaspect = atof(argv[i]); |
| 125 |
break; |
| 126 |
case 'e': |
| 127 |
if (argv[i+1][0] == '+' || argv[i+1][0] == '-') |
| 128 |
d = pow(2.0, atof(argv[i+1])); |
| 129 |
else |
| 130 |
d = atof(argv[i+1]); |
| 131 |
if (d < 1e-20 || d > 1e20) { |
| 132 |
fprintf(stderr, |
| 133 |
"%s: exposure out of range\n", |
| 134 |
argv[0]); |
| 135 |
exit(1); |
| 136 |
} |
| 137 |
switch (argv[i][2]) { |
| 138 |
case '\0': |
| 139 |
scalecolor(exposure, d); |
| 140 |
break; |
| 141 |
case 'r': |
| 142 |
colval(exposure,RED) *= d; |
| 143 |
break; |
| 144 |
case 'g': |
| 145 |
colval(exposure,GRN) *= d; |
| 146 |
break; |
| 147 |
case 'b': |
| 148 |
colval(exposure,BLU) *= d; |
| 149 |
break; |
| 150 |
default: |
| 151 |
goto badopt; |
| 152 |
} |
| 153 |
i++; |
| 154 |
break; |
| 155 |
case 'f': |
| 156 |
lampdat = argv[++i]; |
| 157 |
break; |
| 158 |
case 't': |
| 159 |
lamptype = argv[++i]; |
| 160 |
break; |
| 161 |
case '1': |
| 162 |
singlepass = 1; |
| 163 |
break; |
| 164 |
case '2': |
| 165 |
singlepass = 0; |
| 166 |
break; |
| 167 |
case 'n': |
| 168 |
npts = atoi(argv[++i]) / 2; |
| 169 |
break; |
| 170 |
case 's': |
| 171 |
spread = atof(argv[++i]); |
| 172 |
break; |
| 173 |
case 'a': |
| 174 |
avghot = !avghot; |
| 175 |
break; |
| 176 |
case 'h': |
| 177 |
hotlvl = atof(argv[++i]); |
| 178 |
break; |
| 179 |
case 'r': |
| 180 |
rad = atof(argv[++i]); |
| 181 |
break; |
| 182 |
case 'b': |
| 183 |
rad = 0.0; |
| 184 |
break; |
| 185 |
default:; |
| 186 |
badopt: |
| 187 |
fprintf(stderr, "%s: unknown option: %s\n", |
| 188 |
progname, argv[i]); |
| 189 |
quit(1); |
| 190 |
break; |
| 191 |
} |
| 192 |
else |
| 193 |
break; |
| 194 |
/* get lamp data (if necessary) */ |
| 195 |
if (lamptype != NULL) { |
| 196 |
if (loadlamps(lampdat) < 0) |
| 197 |
quit(1); |
| 198 |
if ((lampcolor = matchlamp(lamptype)) == NULL) { |
| 199 |
fprintf(stderr, "%s: unknown lamp type\n", lamptype); |
| 200 |
quit(1); |
| 201 |
} |
| 202 |
for (i = 0; i < 3; i++) |
| 203 |
if (lampcolor[i] > 1e-4) |
| 204 |
colval(exposure,i) /= lampcolor[i]; |
| 205 |
freelamps(); |
| 206 |
} |
| 207 |
/* open input file */ |
| 208 |
if (i == argc) { |
| 209 |
if (singlepass) |
| 210 |
fin = stdin; |
| 211 |
else { |
| 212 |
tfname = mktemp(TEMPLATE); |
| 213 |
if ((fin = fopen(tfname, "w+")) == NULL) { |
| 214 |
fprintf(stderr, "%s: can't create ", progname); |
| 215 |
fprintf(stderr, "temp file \"%s\"\n", tfname); |
| 216 |
quit(1); |
| 217 |
} |
| 218 |
copyfile(stdin, fin); |
| 219 |
if (fseek(fin, 0L, 0) == -1) { |
| 220 |
fprintf(stderr, "%s: seek fail\n", progname); |
| 221 |
quit(1); |
| 222 |
} |
| 223 |
} |
| 224 |
} else if (i == argc-1) { |
| 225 |
if ((fin = fopen(argv[i], "r")) == NULL) { |
| 226 |
fprintf(stderr, "%s: can't open file \"%s\"\n", |
| 227 |
progname, argv[i]); |
| 228 |
quit(1); |
| 229 |
} |
| 230 |
} else { |
| 231 |
fprintf(stderr, "%s: bad # file arguments\n", progname); |
| 232 |
quit(1); |
| 233 |
} |
| 234 |
/* get header */ |
| 235 |
getheader(fin, headline, NULL); |
| 236 |
if (wrongformat) { |
| 237 |
fprintf(stderr, "%s: input must be a Radiance picture\n", |
| 238 |
progname); |
| 239 |
quit(1); |
| 240 |
} |
| 241 |
/* add new header info. */ |
| 242 |
printargs(i, argv, stdout); |
| 243 |
/* get picture size */ |
| 244 |
if ((order = fgetresolu(&xres, &yres, fin)) < 0) { |
| 245 |
fprintf(stderr, "%s: bad picture size\n", progname); |
| 246 |
quit(1); |
| 247 |
} |
| 248 |
if (!(order & YMAJOR)) |
| 249 |
inpaspect = 1.0/inpaspect; |
| 250 |
/* compute output resolution */ |
| 251 |
if (ncols <= 0) |
| 252 |
ncols = x_c*xres + .5; |
| 253 |
if (nrows <= 0) |
| 254 |
nrows = y_r*yres + .5; |
| 255 |
if (outaspect > .01) { |
| 256 |
d = inpaspect * yres/xres / outaspect; |
| 257 |
if (d * ncols > nrows) |
| 258 |
ncols = nrows / d; |
| 259 |
else |
| 260 |
nrows = ncols * d; |
| 261 |
} |
| 262 |
x_c = (double)ncols/xres; |
| 263 |
y_r = (double)nrows/yres; |
| 264 |
|
| 265 |
if (singlepass) { /* skip exposure, etc. */ |
| 266 |
pass1default(); |
| 267 |
pass2(fin); |
| 268 |
quit(0); |
| 269 |
} |
| 270 |
|
| 271 |
fpos = ftell(fin); /* save input file position */ |
| 272 |
|
| 273 |
pass1(fin); |
| 274 |
|
| 275 |
if (fseek(fin, fpos, 0) == -1) { |
| 276 |
fprintf(stderr, "%s: seek fail\n", progname); |
| 277 |
quit(1); |
| 278 |
} |
| 279 |
pass2(fin); |
| 280 |
|
| 281 |
quit(0); |
| 282 |
} |
| 283 |
|
| 284 |
|
| 285 |
headline(s) /* process line from header */ |
| 286 |
char *s; |
| 287 |
{ |
| 288 |
char fmt[32]; |
| 289 |
|
| 290 |
fputs(s, stdout); /* copy to output */ |
| 291 |
if (isaspect(s)) /* get aspect ratio */ |
| 292 |
inpaspect *= aspectval(s); |
| 293 |
else if (isformat(s)) { |
| 294 |
formatval(fmt, s); |
| 295 |
wrongformat = strcmp(fmt, COLRFMT); |
| 296 |
} |
| 297 |
} |
| 298 |
|
| 299 |
|
| 300 |
copyfile(in, out) /* copy a file */ |
| 301 |
register FILE *in, *out; |
| 302 |
{ |
| 303 |
register int c; |
| 304 |
|
| 305 |
while ((c = getc(in)) != EOF) |
| 306 |
putc(c, out); |
| 307 |
|
| 308 |
if (ferror(out)) { |
| 309 |
fprintf(stderr, "%s: write error in copyfile\n", progname); |
| 310 |
quit(1); |
| 311 |
} |
| 312 |
} |
| 313 |
|
| 314 |
|
| 315 |
pass1(in) /* first pass of picture file */ |
| 316 |
FILE *in; |
| 317 |
{ |
| 318 |
int i; |
| 319 |
COLOR *scan; |
| 320 |
|
| 321 |
pass1init(); |
| 322 |
|
| 323 |
scan = (COLOR *)malloc(xres*sizeof(COLOR)); |
| 324 |
if (scan == NULL) { |
| 325 |
fprintf(stderr, "%s: out of memory\n", progname); |
| 326 |
quit(1); |
| 327 |
} |
| 328 |
for (i = 0; i < yres; i++) { |
| 329 |
if (freadscan(scan, xres, in) < 0) { |
| 330 |
nrows = nrows * i / yres; /* adjust frame */ |
| 331 |
if (nrows <= 0) { |
| 332 |
fprintf(stderr, "%s: empty frame\n", progname); |
| 333 |
quit(1); |
| 334 |
} |
| 335 |
fprintf(stderr, "%s: warning - partial frame (%d%%)\n", |
| 336 |
progname, 100*i/yres); |
| 337 |
yres = i; |
| 338 |
y_r = (double)nrows/yres; |
| 339 |
break; |
| 340 |
} |
| 341 |
pass1scan(scan, i); |
| 342 |
} |
| 343 |
free((char *)scan); |
| 344 |
} |
| 345 |
|
| 346 |
|
| 347 |
pass2(in) /* last pass on file, write to stdout */ |
| 348 |
FILE *in; |
| 349 |
{ |
| 350 |
int yread; |
| 351 |
int ycent, xcent; |
| 352 |
int r, c; |
| 353 |
|
| 354 |
pass2init(); |
| 355 |
scan2init(); |
| 356 |
yread = 0; |
| 357 |
for (r = 0; r < nrows; r++) { |
| 358 |
ycent = (long)r*yres/nrows; |
| 359 |
while (yread <= ycent+yrad) { |
| 360 |
if (yread < yres) { |
| 361 |
if (freadscan(scanin[yread%barsize], |
| 362 |
xres, in) < 0) { |
| 363 |
fprintf(stderr, |
| 364 |
"%s: bad read (y=%d)\n", |
| 365 |
progname, yres-1-yread); |
| 366 |
quit(1); |
| 367 |
} |
| 368 |
pass2scan(scanin[yread%barsize], yread); |
| 369 |
} |
| 370 |
yread++; |
| 371 |
} |
| 372 |
for (c = 0; c < ncols; c++) { |
| 373 |
xcent = (long)c*xres/ncols; |
| 374 |
if (rad <= 0.0) |
| 375 |
dobox(scanout[c], xcent, ycent, c, r); |
| 376 |
else |
| 377 |
dogauss(scanout[c], xcent, ycent, c, r); |
| 378 |
} |
| 379 |
if (fwritescan(scanout, ncols, stdout) < 0) { |
| 380 |
fprintf(stderr, "%s: write error in pass2\n", progname); |
| 381 |
quit(1); |
| 382 |
} |
| 383 |
} |
| 384 |
/* skip leftovers */ |
| 385 |
while (yread < yres) { |
| 386 |
if (freadscan(scanin[0], xres, in) < 0) |
| 387 |
break; |
| 388 |
yread++; |
| 389 |
} |
| 390 |
} |
| 391 |
|
| 392 |
|
| 393 |
scan2init() /* prepare scanline arrays */ |
| 394 |
{ |
| 395 |
COLOR ctmp; |
| 396 |
double d; |
| 397 |
register int i; |
| 398 |
|
| 399 |
if (rad <= 0.0) { |
| 400 |
xrad = xres/ncols/2 + 1; |
| 401 |
yrad = yres/nrows/2 + 1; |
| 402 |
} else { |
| 403 |
if (nrows >= yres && ncols >= xres) |
| 404 |
rad *= (y_r + x_c)/2.0; |
| 405 |
|
| 406 |
xrad = CHECKRAD*rad/x_c + 1; |
| 407 |
yrad = CHECKRAD*rad/y_r + 1; |
| 408 |
|
| 409 |
initmask(); /* initialize filter table */ |
| 410 |
} |
| 411 |
barsize = 2*yrad + 1; |
| 412 |
scanin = (COLOR **)malloc(barsize*sizeof(COLOR *)); |
| 413 |
for (i = 0; i < barsize; i++) { |
| 414 |
scanin[i] = (COLOR *)malloc(xres*sizeof(COLOR)); |
| 415 |
if (scanin[i] == NULL) { |
| 416 |
fprintf(stderr, "%s: out of memory\n", progname); |
| 417 |
quit(1); |
| 418 |
} |
| 419 |
} |
| 420 |
scanout = (COLOR *)malloc(ncols*sizeof(COLOR)); |
| 421 |
if (scanout == NULL) { |
| 422 |
fprintf(stderr, "%s: out of memory\n", progname); |
| 423 |
quit(1); |
| 424 |
} |
| 425 |
/* record pixel aspect ratio */ |
| 426 |
if (!correctaspect) { |
| 427 |
d = order & YMAJOR ? x_c/y_r : y_r/x_c ; |
| 428 |
if (!FEQ(d,1.0)) |
| 429 |
fputaspect(d, stdout); |
| 430 |
} |
| 431 |
/* record exposure */ |
| 432 |
d = bright(exposure); |
| 433 |
if (!FEQ(d,1.0)) |
| 434 |
fputexpos(d, stdout); |
| 435 |
/* record color correction */ |
| 436 |
copycolor(ctmp, exposure); |
| 437 |
scalecolor(ctmp, 1.0/d); |
| 438 |
if (!FEQ(colval(ctmp,RED),colval(ctmp,GRN)) || |
| 439 |
!FEQ(colval(ctmp,GRN),colval(ctmp,BLU))) |
| 440 |
fputcolcor(ctmp, stdout); |
| 441 |
printf("\n"); |
| 442 |
/* write out resolution */ |
| 443 |
fputresolu(order, ncols, nrows, stdout); |
| 444 |
} |
| 445 |
|
| 446 |
|
| 447 |
quit(code) /* remove temporary file and exit */ |
| 448 |
int code; |
| 449 |
{ |
| 450 |
if (tfname != NULL) |
| 451 |
unlink(tfname); |
| 452 |
exit(code); |
| 453 |
} |