--- ray/src/px/pfilt.c 1989/04/11 21:39:00 1.3 +++ ray/src/px/pfilt.c 1992/09/21 12:14:17 2.4 @@ -1,4 +1,4 @@ -/* Copyright (c) 1986 Regents of the University of California */ +/* Copyright (c) 1992 Regents of the University of California */ #ifndef lint static char SCCSid[] = "$SunId$ LBL"; @@ -12,42 +12,55 @@ static char SCCSid[] = "$SunId$ LBL"; #include +#ifdef MSDOS +#include +#endif + #include #include "color.h" +#include "resolu.h" +#include "paths.h" + extern char *malloc(); +extern float *matchlamp(); -#define CHECKRAD 1.5 /* radius to check for filtering */ +#define FEQ(a,b) ((a) >= .98*(b) && (a) <= 1.02*(b)) +#define CHECKRAD 1.5 /* radius to check for filtering */ + COLOR exposure = WHTCOLOR; /* exposure for the frame */ -double rad = 0.0; /* output pixel radius for filtering */ +double rad = 0.0; /* output pixel radius for filtering */ int nrows = 0; /* number of rows for output */ int ncols = 0; /* number of columns for output */ -double xrat = 1.0; /* ratio of input x size to output */ -double yrat = 1.0; /* ratio of input y size to output */ +double x_c = 1.0; /* ratio of output x size to input */ +double y_r = 1.0; /* ratio of output y size to input */ int singlepass = 0; /* true means skip first pass */ int avghot = 0; /* true means average in bright spots */ -double hotlvl = 1000.0; /* level considered "hot" */ +double hotlvl = 1000.0; /* level considered "hot" */ int npts = 0; /* (half) number of points for stars */ -double spread = 1e-4; /* spread for star points */ +double spread = 1e-4; /* spread for star points */ -#define TEMPLATE "/usr/tmp/pfXXXXXX" - char *tfname = NULL; +char *lampdat = "lamp.tab"; /* lamp data file */ + +int order; /* scanline ordering of input */ int xres, yres; /* resolution of input */ +double inpaspect = 1.0; /* pixel aspect ratio of input */ +int correctaspect = 0; /* aspect ratio correction? */ -double x_c, y_r; /* conversion factors */ +int wrongformat = 0; int xrad; /* x window size */ int yrad; /* y window size */ @@ -63,22 +76,29 @@ main(argc, argv) int argc; char **argv; { - extern char *mktemp(); - extern double atof(), pow(); + extern double pow(); extern long ftell(); - extern int quit(); + extern int quit(), headline(); FILE *fin; + float *lampcolor; + char *lamptype = NULL; long fpos; - double d; - int i; - + double outaspect = 0.0; + double d; + int i, j; +#ifdef MSDOS + extern int _fmode; + _fmode = O_BINARY; + setmode(fileno(stdin), O_BINARY); + setmode(fileno(stdout), O_BINARY); +#endif if (signal(SIGINT, quit) == SIG_IGN) signal(SIGINT, SIG_IGN); if (signal(SIGHUP, quit) == SIG_IGN) signal(SIGINT, SIG_IGN); signal(SIGTERM, quit); signal(SIGPIPE, quit); -#ifdef SIGXCPU +#ifdef SIGXCPU signal(SIGXCPU, quit); signal(SIGXFSZ, quit); #endif @@ -90,23 +110,38 @@ char **argv; switch (argv[i][1]) { case 'x': i++; - if (argv[i][0] == '/') - xrat = atof(argv[i]+1); - else + if (argv[i][0] == '/') { + x_c = 1.0/atof(argv[i]+1); + ncols = 0; + } else ncols = atoi(argv[i]); break; case 'y': i++; - if (argv[i][0] == '/') - yrat = atof(argv[i]+1); - else + if (argv[i][0] == '/') { + y_r = 1.0/atof(argv[i]+1); + nrows = 0; + } else nrows = atoi(argv[i]); break; + case 'c': + correctaspect = !correctaspect; + break; + case 'p': + i++; + outaspect = atof(argv[i]); + break; case 'e': if (argv[i+1][0] == '+' || argv[i+1][0] == '-') d = pow(2.0, atof(argv[i+1])); else d = atof(argv[i+1]); + if (d < 1e-20 || d > 1e20) { + fprintf(stderr, + "%s: exposure out of range\n", + argv[0]); + exit(1); + } switch (argv[i][2]) { case '\0': scalecolor(exposure, d); @@ -125,10 +160,19 @@ char **argv; } i++; break; + case 'f': + lampdat = argv[++i]; + break; + case 't': + lamptype = argv[++i]; + break; case '1': singlepass = 1; break; - case 'p': + case '2': + singlepass = 0; + break; + case 'n': npts = atoi(argv[++i]) / 2; break; case 's': @@ -155,7 +199,20 @@ char **argv; } else break; - + /* get lamp data (if necessary) */ + if (lamptype != NULL) { + if (loadlamps(lampdat) < 0) + quit(1); + if ((lampcolor = matchlamp(lamptype)) == NULL) { + fprintf(stderr, "%s: unknown lamp type\n", lamptype); + quit(1); + } + for (i = 0; i < 3; i++) + if (lampcolor[i] > 1e-4) + colval(exposure,i) /= lampcolor[i]; + freelamps(); + } + /* open input file */ if (i == argc) { if (singlepass) fin = stdin; @@ -182,26 +239,38 @@ char **argv; fprintf(stderr, "%s: bad # file arguments\n", progname); quit(1); } - /* copy header */ - copyheader(fin, stdout); + /* get header */ + getheader(fin, headline, NULL); + if (wrongformat) { + fprintf(stderr, "%s: input must be a Radiance picture\n", + progname); + quit(1); + } /* add new header info. */ printargs(i, argv, stdout); /* get picture size */ - if (fscanf(fin, "-Y %d +X %d\n", &yres, &xres) != 2) { + if ((order = fgetresolu(&xres, &yres, fin)) < 0) { fprintf(stderr, "%s: bad picture size\n", progname); quit(1); } - if (ncols > 0) - xrat = (double)xres/ncols; - else - ncols = xres/xrat + .5; - if (nrows > 0) - yrat = (double)yres/nrows; - else - nrows = yres/yrat + .5; + if (!(order & YMAJOR)) + inpaspect = 1.0/inpaspect; + /* compute output resolution */ + if (ncols <= 0) + ncols = x_c*xres + .5; + if (nrows <= 0) + nrows = y_r*yres + .5; + if (outaspect > .01) { + d = inpaspect * yres/xres / outaspect; + if (d * ncols > nrows) + ncols = nrows / d; + else + nrows = ncols * d; + } + x_c = (double)ncols/xres; + y_r = (double)nrows/yres; - if (singlepass) { - /* skip exposure, etc. */ + if (singlepass) { /* skip exposure, etc. */ pass1default(); pass2(fin); quit(0); @@ -221,6 +290,21 @@ char **argv; } +headline(s) /* process line from header */ +char *s; +{ + char fmt[32]; + + fputs(s, stdout); /* copy to output */ + if (isaspect(s)) /* get aspect ratio */ + inpaspect *= aspectval(s); + else if (isformat(s)) { + formatval(fmt, s); + wrongformat = strcmp(fmt, COLRFMT); + } +} + + copyfile(in, out) /* copy a file */ register FILE *in, *out; { @@ -259,6 +343,7 @@ FILE *in; fprintf(stderr, "%s: warning - partial frame (%d%%)\n", progname, 100*i/yres); yres = i; + y_r = (double)nrows/yres; break; } pass1scan(scan, i); @@ -273,7 +358,7 @@ FILE *in; int yread; int ycent, xcent; int r, c; - + pass2init(); scan2init(); yread = 0; @@ -304,17 +389,21 @@ FILE *in; quit(1); } } + /* skip leftovers */ + while (yread < yres) { + if (freadscan(scanin[0], xres, in) < 0) + break; + yread++; + } } scan2init() /* prepare scanline arrays */ { - double e; + COLOR ctmp; + double d; register int i; - x_c = (double)ncols/xres; - y_r = (double)nrows/yres; - if (rad <= 0.0) { xrad = xres/ncols/2 + 1; yrad = yres/nrows/2 + 1; @@ -327,7 +416,7 @@ scan2init() /* prepare scanline arrays */ initmask(); /* initialize filter table */ } - barsize = 2 * yrad; + barsize = 2*yrad + 1; scanin = (COLOR **)malloc(barsize*sizeof(COLOR *)); for (i = 0; i < barsize; i++) { scanin[i] = (COLOR *)malloc(xres*sizeof(COLOR)); @@ -341,11 +430,25 @@ scan2init() /* prepare scanline arrays */ fprintf(stderr, "%s: out of memory\n", progname); quit(1); } - e = bright(exposure); - if (e < 1-1e-7 || e > 1+1e-7) /* record exposure */ - printf("EXPOSURE=%e\n", e); + /* record pixel aspect ratio */ + if (!correctaspect) { + d = order & YMAJOR ? x_c/y_r : y_r/x_c ; + if (!FEQ(d,1.0)) + fputaspect(d, stdout); + } + /* record exposure */ + d = bright(exposure); + if (!FEQ(d,1.0)) + fputexpos(d, stdout); + /* record color correction */ + copycolor(ctmp, exposure); + scalecolor(ctmp, 1.0/d); + if (!FEQ(colval(ctmp,RED),colval(ctmp,GRN)) || + !FEQ(colval(ctmp,GRN),colval(ctmp,BLU))) + fputcolcor(ctmp, stdout); printf("\n"); - printf("-Y %d +X %d\n", nrows, ncols); /* write picture size */ + /* write out resolution */ + fputresolu(order, ncols, nrows, stdout); }