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.15 by greg, Mon Oct 16 12:04:36 1995 UTC vs.
Revision 2.39 by greg, Fri Dec 15 01:57:45 2023 UTC

# Line 1 | Line 1
1 /* Copyright (c) 1992 Regents of the University of California */
2
1   #ifndef lint
2 < static char SCCSid[] = "$SunId$ LBL";
2 > static const char RCSid[] = "$Id$";
3   #endif
6
4   /*
5   *  pfilt.c - program to post-process picture file.
6   *
# Line 11 | Line 8 | static char SCCSid[] = "$SunId$ LBL";
8   *     6/23/93  Added additional buffers for value spreading
9   */
10  
11 < #include  "standard.h"
11 > #include  "copyright.h"
12  
13   #include  <signal.h>
14 <
15 < #include  "color.h"
19 <
20 < #include  "resolu.h"
21 <
14 > #include  "pfilt.h"
15 > #include  "platform.h"
16   #include  "paths.h"
17 + #include  "view.h"
18  
24 extern float  *matchlamp();
25
19   #define  FEQ(a,b)       ((a) >= .98*(b) && (a) <= 1.02*(b))
20  
21 < double   CHECKRAD = 1.5;        /* radius to check for filtering */
21 > double   CHECKRAD = 2.0;        /* radius to check for filtering */
22  
23   #define  THRESHRAD      5.0     /* maximum sample spread in output */
24  
# Line 64 | Line 57 | int  correctaspect = 0;                /* aspect ratio correction? *
57  
58   int  wrongformat = 0;
59  
60 + VIEW  ourview = STDVIEW;
61 + int  gotview = 0;
62 + int  wrapfilt = 0;              /* wrap filter horizontally? */
63 +
64 + int  estatus = 0;               /* exit status (for non-fatal errors) */
65 +
66   int  xrad;                      /* x search radius */
67   int  yrad;                      /* y search radius */
68   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 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 < main(argc, argv)
89 < int  argc;
90 < char  **argv;
88 >
89 > static double
90 > rgb_bright(
91 >        SCOLOR  clr
92 > )
93   {
94 <        extern long  ftell();
95 <        extern int  quit(), headline();
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,
155 >        char  **argv
156 > )
157 > {
158          FILE  *fin;
159          float  *lampcolor;
160          char  *lamptype = NULL;
# Line 93 | Line 162 | char  **argv;
162          double  outaspect = 0.0;
163          double  d;
164          int  i, j;
165 < #ifdef MSDOS
166 <        extern int  _fmode;
167 <        _fmode = O_BINARY;
99 <        setmode(fileno(stdin), O_BINARY);
100 <        setmode(fileno(stdout), O_BINARY);
101 < #endif
165 >        SET_DEFAULT_BINARY();
166 >        SET_FILE_BINARY(stdin);
167 >        SET_FILE_BINARY(stdout);
168          if (signal(SIGINT, quit) == SIG_IGN)
169                  signal(SIGINT, SIG_IGN);
170 + #ifdef SIGHUP
171          if (signal(SIGHUP, quit) == SIG_IGN)
172                  signal(SIGHUP, SIG_IGN);
173 + #endif
174          signal(SIGTERM, quit);
175 + #ifdef SIGPIPE
176          signal(SIGPIPE, quit);
177 + #endif
178   #ifdef  SIGXCPU
179          signal(SIGXCPU, quit);
180          signal(SIGXFSZ, quit);
# Line 147 | Line 217 | char  **argv;
217                                          fprintf(stderr,
218                                                  "%s: exposure out of range\n",
219                                                          argv[0]);
220 <                                        exit(1);
220 >                                        quit(1);
221                                  }
222                                  switch (argv[i][2]) {
223                                  case '\0':
# Line 197 | Line 267 | char  **argv;
267                          case 'm':
268                                  thresh = atof(argv[++i]);
269                                  if (rad <= FTINY)
270 <                                        rad = 1.0;
270 >                                        rad = 0.6;
271                                  break;
272                          case 'b':
273                                  rad = thresh = 0.0;
# Line 230 | Line 300 | char  **argv;
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 242 | Line 312 | char  **argv;
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 258 | Line 328 | char  **argv;
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 267 | Line 342 | char  **argv;
342          }
343          if (!(order & YMAJOR))
344                  inpaspect = 1.0/inpaspect;
345 +                                        /* wrap around for cylindrical view? */
346 +        wrapfilt = gotview && ourview.type == VT_CYL &&
347 +                        ourview.horiz >= 360.-FTINY && order & YMAJOR;
348                                          /* compute output resolution */
349          if (ncols <= 0)
350                  ncols = x_c*xres + .5;
# Line 298 | Line 376 | char  **argv;
376          }
377          pass2(fin);
378  
379 <        quit(0);
379 >        quit(estatus);
380 >        return estatus; /* pro forma return */
381   }
382  
383  
384 < double
385 < rgb_bright(clr)
386 < COLOR  clr;
384 > static void
385 > copyfile(                       /* copy a file */
386 >        FILE  *infp,
387 >        FILE  *out
388 > )
389   {
390 <        return(bright(clr));
310 < }
390 >        int  c;
391  
392 <
313 < double
314 < xyz_bright(clr)
315 < COLOR  clr;
316 < {
317 <        return(clr[CIEY]);
318 < }
319 <
320 <
321 < double  (*ourbright)() = rgb_bright;
322 <
323 <
324 < headline(s)                             /* process line from header */
325 < char  *s;
326 < {
327 <        char  fmt[32];
328 <
329 <        fputs(s, stdout);               /* copy to output */
330 <        if (isaspect(s))                /* get aspect ratio */
331 <                inpaspect *= aspectval(s);
332 <        else if (isexpos(s))            /* get exposure */
333 <                hotlvl *= exposval(s);
334 <        else if (formatval(fmt, s)) {   /* get format */
335 <                wrongformat = 0;
336 <                if (!strcmp(COLRFMT, fmt))
337 <                        ourbright = rgb_bright;
338 <                else if (!strcmp(CIEFMT, fmt))
339 <                        ourbright = xyz_bright;
340 <                else
341 <                        wrongformat = !globmatch(PICFMT, fmt);
342 <        }
343 < }
344 <
345 <
346 < copyfile(in, out)                       /* copy a file */
347 < register FILE  *in, *out;
348 < {
349 <        register int  c;
350 <
351 <        while ((c = getc(in)) != EOF)
392 >        while ((c = getc(infp)) != EOF)
393                  putc(c, out);
394  
395          if (ferror(out)) {
# Line 358 | Line 399 | register FILE  *in, *out;
399   }
400  
401  
402 < pass1(in)                               /* first pass of picture file */
403 < FILE  *in;
402 > static void
403 > pass1(                          /* first pass of picture file */
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 382 | Line 425 | FILE  *in;
425                                          progname, (int)(100L*i/yres));
426                          yres = i;
427                          y_r = (double)nrows/yres;
428 +                        estatus++;
429                          break;
430                  }
431                  pass1scan(scan, i);
432          }
433 <        free((char *)scan);
433 >        free(scan);
434   }
435  
436  
437 < pass2(in)                       /* last pass on file, write to stdout */
438 < FILE  *in;
437 > static void
438 > pass2(                  /* last pass on file, write to stdout */
439 >        FILE  *infp
440 > )
441   {
442          int  yread;
443          int  ycent, xcent;
# Line 401 | Line 447 | FILE  *in;
447          scan2init();
448          yread = 0;
449          for (r = 0; r < nrows; r++) {
450 <                ycent = (long)r*yres/nrows;
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 415 | Line 461 | FILE  *in;
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 = (long)c*xres/ncols;
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 441 | Line 488 | FILE  *in;
488   }
489  
490  
491 < scan2init()                     /* prepare scanline arrays */
491 > static void
492 > 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 454 | Line 502 | scan2init()                    /* prepare scanline arrays */
502                          rad *= (y_r + x_c)/2.0;
503  
504                  if (thresh > FTINY) {
457                        xrad = CHECKRAD*THRESHRAD*rad/x_c + xbrad;
458                        yrad = CHECKRAD*THRESHRAD*rad/y_r + ybrad;
505                          orad = CHECKRAD*THRESHRAD*rad + 1;
506 +                        xrad = orad/x_c + xbrad;
507 +                        yrad = orad/y_r + ybrad;
508                          obarsize = 2*orad + 1;
509                  } else {
510                          xrad = CHECKRAD*rad/x_c + 1;
# Line 468 | Line 516 | scan2init()                    /* 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)
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)
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 499 | Line 547 | scan2init()                    /* 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 518 | Line 566 | memerr:
566   }
567  
568  
569 < scan2sync(r)                    /* synchronize grey averages and output scan */
570 < int  r;
569 > static void
570 > scan2sync(                      /* synchronize grey averages and output scan */
571 >        int  r
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 = (long)nextrow*yres/nrows;
579 >        while (nextrow <= r+orad && nextrow < nrows) {
580 >                ybot = (nextrow+.5)*yres/nrows;
581                  for (c = 0; c < ncols; c++) {
582 <                        dobox(ctmp, (int)((long)c*xres/ncols),ybot, c,nextrow);
582 >                        dobox(ctmp, (int)((c+.5)*xres/ncols),ybot, c,nextrow);
583                          greybar[nextrow%obarsize][c] = (*ourbright)(ctmp);
584                  }
585                                          /* and zero output scanline */
586 <                bzero((char *)scoutbar[nextrow%obarsize], ncols*sizeof(COLOR));
586 >                memset(scoutbar[nextrow%obarsize], 0, ncols*NCSAMP*sizeof(COLORV));
587                  nextrow++;
588          }
589                                          /* point to top scanline for output */
# Line 544 | Line 594 | int  r;
594   }
595  
596  
597 < scan2flush()                    /* flush output buffer */
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);
607 <                exit(1);
607 >                quit(1);
608          }
609   }
610  
611  
612 + void
613   quit(code)              /* remove temporary file and exit */
614   int  code;
615   {

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines