ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/px/pfilt.c
(Generate patch)

Comparing ray/src/px/pfilt.c (file contents):
Revision 2.31 by greg, Fri Sep 3 21:18:15 2010 UTC vs.
Revision 2.39 by greg, Fri Dec 15 01:57:45 2023 UTC

# Line 11 | Line 11 | static const char RCSid[] = "$Id$";
11   #include  "copyright.h"
12  
13   #include  <signal.h>
14 < #include  <string.h>
15 <
14 > #include  "pfilt.h"
15   #include  "platform.h"
17 #include  "standard.h"
18 #include  "rtio.h"
19 #include  "color.h"
20 #include  "view.h"
16   #include  "paths.h"
17 < #include  "pfilt.h"
17 > #include  "view.h"
18  
24
19   #define  FEQ(a,b)       ((a) >= .98*(b) && (a) <= 1.02*(b))
20  
21   double   CHECKRAD = 2.0;        /* radius to check for filtering */
# Line 75 | Line 69 | int  xbrad;                    /* x box size */
69   int  ybrad;                     /* y box size */
70  
71   int  barsize;                   /* size of input scan bar */
72 < COLOR  **scanin;                /* input scan bar */
73 < COLOR  *scanout;                /* output scan line */
74 < COLOR  **scoutbar;              /* output scan bar (if thresh > 0) */
72 > COLORV  **scanin;               /* input scan bar */
73 > COLORV  *scanout;               /* output scan line */
74 > COLORV  **scoutbar;             /* output scan bar (if thresh > 0) */
75   float  **greybar;               /* grey-averaged input values */
76   int  obarsize = 0;              /* size of output scan bar */
77   int  orad = 0;                  /* output window radius */
78  
79   char  *progname;
80  
81 < static gethfunc headline;
82 < static brightfunc_t rgb_bright;
83 < static brightfunc_t xyz_bright;
90 < static void copyfile(FILE  *in, FILE  *out);
91 < static void pass1(FILE  *in);
92 < static void pass2(FILE  *in);
81 > static void copyfile(FILE  *infp, FILE  *out);
82 > static void pass1(FILE  *infp);
83 > static void pass2(FILE  *infp);
84   static void scan2init(void);
85   static void scan2sync(int  r);
86   static void scan2flush(void);
87  
88  
89 + static double
90 + rgb_bright(
91 +        SCOLOR  clr
92 + )
93 + {
94 +        return(bright(clr));
95 + }
96 +
97 +
98 + static double
99 + xyz_bright(
100 +        SCOLOR  clr
101 + )
102 + {
103 +        return(colval(clr,CIEY));
104 + }
105 +
106 +
107 + static double
108 + spec_bright(
109 +        SCOLOR  clr
110 + )
111 + {
112 +        return(pbright(clr));
113 + }
114 +
115 +
116 + brightfunc_t    *ourbright = rgb_bright;
117 +
118 +
119 + static int
120 + headline(                               /* process line from header */
121 +        char    *s,
122 +        void    *p
123 + )
124 + {
125 +        char  fmt[MAXFMTLEN];
126 +
127 +        fputs(s, stdout);               /* copy to output */
128 +        if (isaspect(s))                /* get aspect ratio */
129 +                inpaspect *= aspectval(s);
130 +        else if (isexpos(s))            /* get exposure */
131 +                hotlvl *= exposval(s);
132 +        else if (isncomp(s))            /* get #components (spectral) */
133 +                NCSAMP = ncompval(s);
134 +        else if (iswlsplit(s))          /* get wavelength partitions */
135 +                wlsplitval(WLPART, s);
136 +        else if (formatval(fmt, s)) {   /* get format */
137 +                wrongformat = 0;
138 +                if (!strcmp(COLRFMT, fmt))
139 +                        ourbright = rgb_bright;
140 +                else if (!strcmp(CIEFMT, fmt))
141 +                        ourbright = xyz_bright;
142 +                else if (!strcmp(SPECFMT, fmt))
143 +                        ourbright = spec_bright;
144 +                else
145 +                        wrongformat = !globmatch(PICFMT, fmt);
146 +        } else if (isview(s) && sscanview(&ourview, s) > 0)
147 +                gotview++;
148 +        return(0);
149 + }
150 +
151 +
152   int
153   main(
154          int  argc,
# Line 246 | Line 300 | main(
300                          fin = stdin;
301                  else {
302                          tfname = mktemp(template);
303 <                        if ((fin = fopen(tfname, "w+")) == NULL) {
303 >                        if ((fin = fopen(tfname, "w+b")) == NULL) {
304                                  fprintf(stderr, "%s: can't create ", progname);
305                                  fprintf(stderr, "temp file \"%s\"\n", tfname);
306                                  quit(1);
# Line 258 | Line 312 | main(
312                          }
313                  }
314          } else if (i == argc-1) {
315 <                if ((fin = fopen(argv[i], "r")) == NULL) {
315 >                if ((fin = fopen(argv[i], "rb")) == NULL) {
316                          fprintf(stderr, "%s: can't open file \"%s\"\n",
317                                                  progname, argv[i]);
318                          quit(1);
# Line 274 | Line 328 | main(
328                                  progname);
329                  quit(1);
330          }
331 +        if ((ourbright == spec_bright) ^ (NCSAMP > 3) ||
332 +                        setspectrsamp(CNDX, WLPART) < 0) {
333 +                fprintf(stderr, "%s: bad number of components\n", progname);
334 +                quit(1);
335 +        }
336                                          /* add new header info. */
337          printargs(i, argv, stdout);
338                                          /* get picture size */
# Line 322 | Line 381 | main(
381   }
382  
383  
325 static double
326 rgb_bright(
327        COLOR  clr
328 )
329 {
330        return(bright(clr));
331 }
332
333
334 static double
335 xyz_bright(
336        COLOR  clr
337 )
338 {
339        return(clr[CIEY]);
340 }
341
342
343 brightfunc_t *ourbright = rgb_bright;
344
345 static int
346 headline(                               /* process line from header */
347        char    *s,
348        void    *p
349 )
350 {
351        char  fmt[32];
352
353        fputs(s, stdout);               /* copy to output */
354        if (isaspect(s))                /* get aspect ratio */
355                inpaspect *= aspectval(s);
356        else if (isexpos(s))            /* get exposure */
357                hotlvl *= exposval(s);
358        else if (formatval(fmt, s)) {   /* get format */
359                wrongformat = 0;
360                if (!strcmp(COLRFMT, fmt))
361                        ourbright = rgb_bright;
362                else if (!strcmp(CIEFMT, fmt))
363                        ourbright = xyz_bright;
364                else
365                        wrongformat = !globmatch(PICFMT, fmt);
366        } else if (isview(s) && sscanview(&ourview, s) > 0)
367                gotview++;
368        return(0);
369 }
370
371
384   static void
385   copyfile(                       /* copy a file */
386 <        register FILE  *in,
387 <        register FILE  *out
386 >        FILE  *infp,
387 >        FILE  *out
388   )
389   {
390 <        register int  c;
390 >        int  c;
391  
392 <        while ((c = getc(in)) != EOF)
392 >        while ((c = getc(infp)) != EOF)
393                  putc(c, out);
394  
395          if (ferror(out)) {
# Line 389 | Line 401 | copyfile(                      /* copy a file */
401  
402   static void
403   pass1(                          /* first pass of picture file */
404 <        FILE  *in
404 >        FILE  *infp
405   )
406   {
407          int  i;
408 <        COLOR  *scan;
408 >        COLORV  *scan;
409  
410          pass1init();
411  
412 <        scan = (COLOR *)malloc(xres*sizeof(COLOR));
412 >        scan = (COLORV *)malloc(xres*NCSAMP*sizeof(COLORV));
413          if (scan == NULL) {
414                  fprintf(stderr, "%s: out of memory\n", progname);
415                  quit(1);
416          }
417          for (i = 0; i < yres; i++) {
418 <                if (freadscan(scan, xres, in) < 0) {
418 >                if (freadsscan(scan, NCSAMP, xres, infp) < 0) {
419                          nrows = (long)nrows * i / yres; /* adjust frame */
420                          if (nrows <= 0) {
421                                  fprintf(stderr, "%s: empty frame\n", progname);
# Line 418 | Line 430 | pass1(                         /* first pass of picture file */
430                  }
431                  pass1scan(scan, i);
432          }
433 <        free((void *)scan);
433 >        free(scan);
434   }
435  
436  
437   static void
438   pass2(                  /* last pass on file, write to stdout */
439 <        FILE  *in
439 >        FILE  *infp
440   )
441   {
442          int  yread;
# Line 438 | Line 450 | pass2(                 /* last pass on file, write to stdout */
450                  ycent = (r+.5)*yres/nrows;
451                  while (yread <= ycent+yrad) {
452                          if (yread < yres) {
453 <                                if (freadscan(scanin[yread%barsize],
454 <                                                xres, in) < 0) {
453 >                                if (freadsscan(scanin[yread%barsize],
454 >                                                NCSAMP, xres, infp) < 0) {
455                                          fprintf(stderr,
456                                                  "%s: truncated input (y=%d)\n",
457                                                  progname, yres-1-yread);
# Line 449 | Line 461 | pass2(                 /* last pass on file, write to stdout */
461                          }
462                          yread++;
463                  }
464 <                if (obarsize > 0)
464 >                if (obarsize > 0)       /* => thresh > FTINY */
465                          scan2sync(r);
466                  for (c = 0; c < ncols; c++) {
467                          xcent = (c+.5)*xres/ncols;
468                          if (thresh > FTINY)
469                                  dothresh(xcent, ycent, c, r);
470                          else if (rad > FTINY)
471 <                                dogauss(scanout[c], xcent, ycent, c, r);
471 >                                dogauss(scanout+c*NCSAMP, xcent, ycent, c, r);
472                          else
473 <                                dobox(scanout[c], xcent, ycent, c, r);
473 >                                dobox(scanout+c*NCSAMP, xcent, ycent, c, r);
474                  }
475 <                if (scanout != NULL && fwritescan(scanout, ncols, stdout) < 0) {
475 >                if (scanout != NULL && fwritesscan(scanout, NCSAMP, ncols, stdout) < 0) {
476                          fprintf(stderr, "%s: write error in pass2\n", progname);
477                          quit(1);
478                  }
479          }
480                                          /* skip leftover input */
481          while (yread < yres) {
482 <                if (freadscan(scanin[0], xres, in) < 0)
482 >                if (freadscolrs((uby8 *)tempbuffer(xres*(NCSAMP+1)),
483 >                                        NCSAMP, xres, infp) < 0)
484                          break;
485                  yread++;
486          }
# Line 480 | Line 493 | scan2init(void)                        /* prepare scanline arrays */
493   {
494          COLOR   ctmp;
495          double  d;
496 <        register int  i;
496 >        int  i;
497  
498          xbrad = xres/ncols/2 + 1;
499          ybrad = yres/nrows/2 + 1;
# Line 503 | Line 516 | scan2init(void)                        /* prepare scanline arrays */
516                  yrad = ybrad;
517          }
518          barsize = 2*yrad + 1;
519 <        scanin = (COLOR **)malloc(barsize*sizeof(COLOR *));
519 >        scanin = (COLORV **)malloc(barsize*sizeof(COLORV *));
520          if (scanin == NULL)
521                  goto memerr;
522          for (i = 0; i < barsize; i++) {
523 <                scanin[i] = (COLOR *)malloc(xres*sizeof(COLOR));
523 >                scanin[i] = (COLORV *)malloc(xres*NCSAMP*sizeof(COLORV));
524                  if (scanin[i] == NULL)
525                          goto memerr;
526          }
527          if (obarsize > 0) {
528 <                scoutbar = (COLOR **)malloc(obarsize*sizeof(COLOR *));
528 >                scoutbar = (COLORV **)malloc(obarsize*sizeof(COLORV *));
529                  greybar = (float **)malloc(obarsize*sizeof(float *));
530                  if ((scoutbar == NULL) | (greybar == NULL))
531                          goto memerr;
532                  for (i = 0; i < obarsize; i++) {
533 <                        scoutbar[i] = (COLOR *)malloc(ncols*sizeof(COLOR));
533 >                        scoutbar[i] = (COLORV *)malloc(ncols*NCSAMP*sizeof(COLORV));
534                          greybar[i] = (float *)malloc(ncols*sizeof(float));
535                          if ((scoutbar[i] == NULL) | (greybar[i] == NULL))
536                                  goto memerr;
537                  }
538          } else {
539 <                scanout = (COLOR *)malloc(ncols*sizeof(COLOR));
539 >                scanout = (COLORV *)malloc(ncols*NCSAMP*sizeof(COLORV));
540                  if (scanout == NULL)
541                          goto memerr;
542          }
# Line 534 | Line 547 | scan2init(void)                        /* prepare scanline arrays */
547                          fputaspect(d, stdout);
548          }
549                                          /* record exposure */
550 <        d = (*ourbright)(exposure);
550 >        d = bright(exposure);
551          if (!FEQ(d,1.0))
552                  fputexpos(d, stdout);
553                                          /* record color correction */
# Line 559 | Line 572 | scan2sync(                     /* synchronize grey averages and output s
572   )
573   {
574          static int  nextrow = 0;
575 <        COLOR  ctmp;
575 >        SCOLOR  ctmp;
576          int  ybot;
577 <        register int  c;
577 >        int  c;
578                                          /* average input scanlines */
579          while (nextrow <= r+orad && nextrow < nrows) {
580                  ybot = (nextrow+.5)*yres/nrows;
# Line 570 | Line 583 | scan2sync(                     /* synchronize grey averages and output s
583                          greybar[nextrow%obarsize][c] = (*ourbright)(ctmp);
584                  }
585                                          /* and zero output scanline */
586 <                memset((char *)scoutbar[nextrow%obarsize], '\0', ncols*sizeof(COLOR));
586 >                memset(scoutbar[nextrow%obarsize], 0, ncols*NCSAMP*sizeof(COLORV));
587                  nextrow++;
588          }
589                                          /* point to top scanline for output */
# Line 584 | Line 597 | scan2sync(                     /* synchronize grey averages and output s
597   static void
598   scan2flush(void)                        /* flush output buffer */
599   {
600 <        register int  r;
600 >        int  r;
601  
602          for (r = nrows-orad; r < nrows; r++)
603 <                if (fwritescan(scoutbar[r%obarsize], ncols, stdout) < 0)
603 >                if (fwritesscan(scoutbar[r%obarsize], NCSAMP, ncols, stdout) < 0)
604                          break;
605          if (fflush(stdout) < 0) {
606                  fprintf(stderr, "%s: write error at end of pass2\n", progname);

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines