--- ray/src/px/pfilt.c 2019/12/28 18:05:14 2.34 +++ ray/src/px/pfilt.c 2023/12/15 01:57:45 2.39 @@ -1,5 +1,5 @@ #ifndef lint -static const char RCSid[] = "$Id: pfilt.c,v 2.34 2019/12/28 18:05:14 greg Exp $"; +static const char RCSid[] = "$Id: pfilt.c,v 2.39 2023/12/15 01:57:45 greg Exp $"; #endif /* * pfilt.c - program to post-process picture file. @@ -11,16 +11,11 @@ static const char RCSid[] = "$Id: pfilt.c,v 2.34 2019/ #include "copyright.h" #include - +#include "pfilt.h" #include "platform.h" -#include "standard.h" -#include "rtio.h" -#include "color.h" -#include "view.h" #include "paths.h" -#include "pfilt.h" +#include "view.h" - #define FEQ(a,b) ((a) >= .98*(b) && (a) <= 1.02*(b)) double CHECKRAD = 2.0; /* radius to check for filtering */ @@ -74,26 +69,86 @@ int xbrad; /* x box size */ int ybrad; /* y box size */ int barsize; /* size of input scan bar */ -COLOR **scanin; /* input scan bar */ -COLOR *scanout; /* output scan line */ -COLOR **scoutbar; /* output scan bar (if thresh > 0) */ +COLORV **scanin; /* input scan bar */ +COLORV *scanout; /* output scan line */ +COLORV **scoutbar; /* output scan bar (if thresh > 0) */ float **greybar; /* grey-averaged input values */ int obarsize = 0; /* size of output scan bar */ int orad = 0; /* output window radius */ char *progname; -static gethfunc headline; -static brightfunc_t rgb_bright; -static brightfunc_t xyz_bright; -static void copyfile(FILE *in, FILE *out); -static void pass1(FILE *in); -static void pass2(FILE *in); +static void copyfile(FILE *infp, FILE *out); +static void pass1(FILE *infp); +static void pass2(FILE *infp); static void scan2init(void); static void scan2sync(int r); static void scan2flush(void); +static double +rgb_bright( + SCOLOR clr +) +{ + return(bright(clr)); +} + + +static double +xyz_bright( + SCOLOR clr +) +{ + return(colval(clr,CIEY)); +} + + +static double +spec_bright( + SCOLOR clr +) +{ + return(pbright(clr)); +} + + +brightfunc_t *ourbright = rgb_bright; + + +static int +headline( /* process line from header */ + char *s, + void *p +) +{ + char fmt[MAXFMTLEN]; + + fputs(s, stdout); /* copy to output */ + if (isaspect(s)) /* get aspect ratio */ + inpaspect *= aspectval(s); + else if (isexpos(s)) /* get exposure */ + hotlvl *= exposval(s); + else if (isncomp(s)) /* get #components (spectral) */ + NCSAMP = ncompval(s); + else if (iswlsplit(s)) /* get wavelength partitions */ + wlsplitval(WLPART, s); + else if (formatval(fmt, s)) { /* get format */ + wrongformat = 0; + if (!strcmp(COLRFMT, fmt)) + ourbright = rgb_bright; + else if (!strcmp(CIEFMT, fmt)) + ourbright = xyz_bright; + else if (!strcmp(SPECFMT, fmt)) + ourbright = spec_bright; + else + wrongformat = !globmatch(PICFMT, fmt); + } else if (isview(s) && sscanview(&ourview, s) > 0) + gotview++; + return(0); +} + + int main( int argc, @@ -245,7 +300,7 @@ main( fin = stdin; else { tfname = mktemp(template); - if ((fin = fopen(tfname, "w+")) == NULL) { + if ((fin = fopen(tfname, "w+b")) == NULL) { fprintf(stderr, "%s: can't create ", progname); fprintf(stderr, "temp file \"%s\"\n", tfname); quit(1); @@ -257,7 +312,7 @@ main( } } } else if (i == argc-1) { - if ((fin = fopen(argv[i], "r")) == NULL) { + if ((fin = fopen(argv[i], "rb")) == NULL) { fprintf(stderr, "%s: can't open file \"%s\"\n", progname, argv[i]); quit(1); @@ -273,6 +328,11 @@ main( progname); quit(1); } + if ((ourbright == spec_bright) ^ (NCSAMP > 3) || + setspectrsamp(CNDX, WLPART) < 0) { + fprintf(stderr, "%s: bad number of components\n", progname); + quit(1); + } /* add new header info. */ printargs(i, argv, stdout); /* get picture size */ @@ -321,62 +381,15 @@ main( } -static double -rgb_bright( - COLOR clr -) -{ - return(bright(clr)); -} - - -static double -xyz_bright( - COLOR clr -) -{ - return(clr[CIEY]); -} - - -brightfunc_t *ourbright = rgb_bright; - -static int -headline( /* process line from header */ - char *s, - void *p -) -{ - char fmt[MAXFMTLEN]; - - fputs(s, stdout); /* copy to output */ - if (isaspect(s)) /* get aspect ratio */ - inpaspect *= aspectval(s); - else if (isexpos(s)) /* get exposure */ - hotlvl *= exposval(s); - else if (formatval(fmt, s)) { /* get format */ - wrongformat = 0; - if (!strcmp(COLRFMT, fmt)) - ourbright = rgb_bright; - else if (!strcmp(CIEFMT, fmt)) - ourbright = xyz_bright; - else - wrongformat = !globmatch(PICFMT, fmt); - } else if (isview(s) && sscanview(&ourview, s) > 0) - gotview++; - return(0); -} - - static void copyfile( /* copy a file */ - FILE *in, + FILE *infp, FILE *out ) { int c; - while ((c = getc(in)) != EOF) + while ((c = getc(infp)) != EOF) putc(c, out); if (ferror(out)) { @@ -388,21 +401,21 @@ copyfile( /* copy a file */ static void pass1( /* first pass of picture file */ - FILE *in + FILE *infp ) { int i; - COLOR *scan; + COLORV *scan; pass1init(); - scan = (COLOR *)malloc(xres*sizeof(COLOR)); + scan = (COLORV *)malloc(xres*NCSAMP*sizeof(COLORV)); if (scan == NULL) { fprintf(stderr, "%s: out of memory\n", progname); quit(1); } for (i = 0; i < yres; i++) { - if (freadscan(scan, xres, in) < 0) { + if (freadsscan(scan, NCSAMP, xres, infp) < 0) { nrows = (long)nrows * i / yres; /* adjust frame */ if (nrows <= 0) { fprintf(stderr, "%s: empty frame\n", progname); @@ -417,13 +430,13 @@ pass1( /* first pass of picture file */ } pass1scan(scan, i); } - free((void *)scan); + free(scan); } static void pass2( /* last pass on file, write to stdout */ - FILE *in + FILE *infp ) { int yread; @@ -437,8 +450,8 @@ pass2( /* last pass on file, write to stdout */ ycent = (r+.5)*yres/nrows; while (yread <= ycent+yrad) { if (yread < yres) { - if (freadscan(scanin[yread%barsize], - xres, in) < 0) { + if (freadsscan(scanin[yread%barsize], + NCSAMP, xres, infp) < 0) { fprintf(stderr, "%s: truncated input (y=%d)\n", progname, yres-1-yread); @@ -448,25 +461,26 @@ pass2( /* last pass on file, write to stdout */ } yread++; } - if (obarsize > 0) + if (obarsize > 0) /* => thresh > FTINY */ scan2sync(r); for (c = 0; c < ncols; c++) { xcent = (c+.5)*xres/ncols; if (thresh > FTINY) dothresh(xcent, ycent, c, r); else if (rad > FTINY) - dogauss(scanout[c], xcent, ycent, c, r); + dogauss(scanout+c*NCSAMP, xcent, ycent, c, r); else - dobox(scanout[c], xcent, ycent, c, r); + dobox(scanout+c*NCSAMP, xcent, ycent, c, r); } - if (scanout != NULL && fwritescan(scanout, ncols, stdout) < 0) { + if (scanout != NULL && fwritesscan(scanout, NCSAMP, ncols, stdout) < 0) { fprintf(stderr, "%s: write error in pass2\n", progname); quit(1); } } /* skip leftover input */ while (yread < yres) { - if (freadscan(scanin[0], xres, in) < 0) + if (freadscolrs((uby8 *)tempbuffer(xres*(NCSAMP+1)), + NCSAMP, xres, infp) < 0) break; yread++; } @@ -502,27 +516,27 @@ scan2init(void) /* prepare scanline arrays */ yrad = ybrad; } barsize = 2*yrad + 1; - scanin = (COLOR **)malloc(barsize*sizeof(COLOR *)); + scanin = (COLORV **)malloc(barsize*sizeof(COLORV *)); if (scanin == NULL) goto memerr; for (i = 0; i < barsize; i++) { - scanin[i] = (COLOR *)malloc(xres*sizeof(COLOR)); + scanin[i] = (COLORV *)malloc(xres*NCSAMP*sizeof(COLORV)); if (scanin[i] == NULL) goto memerr; } if (obarsize > 0) { - scoutbar = (COLOR **)malloc(obarsize*sizeof(COLOR *)); + scoutbar = (COLORV **)malloc(obarsize*sizeof(COLORV *)); greybar = (float **)malloc(obarsize*sizeof(float *)); if ((scoutbar == NULL) | (greybar == NULL)) goto memerr; for (i = 0; i < obarsize; i++) { - scoutbar[i] = (COLOR *)malloc(ncols*sizeof(COLOR)); + scoutbar[i] = (COLORV *)malloc(ncols*NCSAMP*sizeof(COLORV)); greybar[i] = (float *)malloc(ncols*sizeof(float)); if ((scoutbar[i] == NULL) | (greybar[i] == NULL)) goto memerr; } } else { - scanout = (COLOR *)malloc(ncols*sizeof(COLOR)); + scanout = (COLORV *)malloc(ncols*NCSAMP*sizeof(COLORV)); if (scanout == NULL) goto memerr; } @@ -533,7 +547,7 @@ scan2init(void) /* prepare scanline arrays */ fputaspect(d, stdout); } /* record exposure */ - d = (*ourbright)(exposure); + d = bright(exposure); if (!FEQ(d,1.0)) fputexpos(d, stdout); /* record color correction */ @@ -558,7 +572,7 @@ scan2sync( /* synchronize grey averages and output s ) { static int nextrow = 0; - COLOR ctmp; + SCOLOR ctmp; int ybot; int c; /* average input scanlines */ @@ -569,7 +583,7 @@ scan2sync( /* synchronize grey averages and output s greybar[nextrow%obarsize][c] = (*ourbright)(ctmp); } /* and zero output scanline */ - memset((char *)scoutbar[nextrow%obarsize], '\0', ncols*sizeof(COLOR)); + memset(scoutbar[nextrow%obarsize], 0, ncols*NCSAMP*sizeof(COLORV)); nextrow++; } /* point to top scanline for output */ @@ -586,7 +600,7 @@ scan2flush(void) /* flush output buffer */ int r; for (r = nrows-orad; r < nrows; r++) - if (fwritescan(scoutbar[r%obarsize], ncols, stdout) < 0) + if (fwritesscan(scoutbar[r%obarsize], NCSAMP, ncols, stdout) < 0) break; if (fflush(stdout) < 0) { fprintf(stderr, "%s: write error at end of pass2\n", progname);