| 1 | < | /* Copyright (c) 1986 Regents of the University of California */ | 
| 1 | > | /* Copyright (c) 1991 Regents of the University of California */ | 
| 2 |  |  | 
| 3 |  | #ifndef lint | 
| 4 |  | static char SCCSid[] = "$SunId$ LBL"; | 
| 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 */ | 
| 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 |  |  | 
| 75 |  | extern char  *mktemp(); | 
| 76 |  | extern double  atof(), pow(); | 
| 77 |  | extern long  ftell(); | 
| 78 | < | extern int  quit(); | 
| 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; | 
| 85 | > | int  i, j; | 
| 86 |  |  | 
| 87 |  | if (signal(SIGINT, quit) == SIG_IGN) | 
| 88 |  | signal(SIGINT, SIG_IGN); | 
| 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); | 
| 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 'p': | 
| 167 | > | case 'n': | 
| 168 |  | npts = atoi(argv[++i]) / 2; | 
| 169 |  | break; | 
| 170 |  | case 's': | 
| 191 |  | } | 
| 192 |  | else | 
| 193 |  | break; | 
| 194 | < |  | 
| 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; | 
| 231 |  | fprintf(stderr, "%s: bad # file arguments\n", progname); | 
| 232 |  | quit(1); | 
| 233 |  | } | 
| 234 | < | /* copy header */ | 
| 235 | < | copyheader(fin, stdout); | 
| 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 (fgetresolu(&xres, &yres, fin) != (YMAJOR|YDECR)) { | 
| 244 | > | if ((order = fgetresolu(&xres, &yres, fin)) < 0) { | 
| 245 |  | fprintf(stderr, "%s: bad picture size\n", progname); | 
| 246 |  | quit(1); | 
| 247 |  | } | 
| 248 | < | if (ncols > 0) | 
| 249 | < | x_c = (double)ncols/xres; | 
| 250 | < | else | 
| 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) | 
| 202 | < | y_r = (double)nrows/yres; | 
| 203 | < | else | 
| 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) { | 
| 207 | < | /* skip exposure, etc. */ | 
| 265 | > | if (singlepass) {               /* skip exposure, etc. */ | 
| 266 |  | pass1default(); | 
| 267 |  | pass2(fin); | 
| 268 |  | 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 |  | { | 
| 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); | 
| 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 | < | double  e; | 
| 395 | > | COLOR   ctmp; | 
| 396 | > | double  d; | 
| 397 |  | register int  i; | 
| 398 |  |  | 
| 399 |  | if (rad <= 0.0) { | 
| 408 |  |  | 
| 409 |  | initmask();             /* initialize filter table */ | 
| 410 |  | } | 
| 411 | < | barsize = 2 * yrad; | 
| 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)); | 
| 422 |  | fprintf(stderr, "%s: out of memory\n", progname); | 
| 423 |  | quit(1); | 
| 424 |  | } | 
| 425 | < | e = bright(exposure); | 
| 426 | < | if (e < 1-1e-7 || e > 1+1e-7)           /* record exposure */ | 
| 427 | < | fputexpos(e, stdout); | 
| 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 | < | fputresolu(YMAJOR|YDECR, ncols, nrows, stdout); /* resolution */ | 
| 442 | > | /* write out resolution */ | 
| 443 | > | fputresolu(order, ncols, nrows, stdout); | 
| 444 |  | } | 
| 445 |  |  | 
| 446 |  |  |