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.2 by greg, Thu Dec 19 14:51:52 1991 UTC vs.
Revision 2.41 by greg, Sat Jun 7 05:09:46 2025 UTC

# Line 1 | Line 1
1 /* Copyright (c) 1991 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   *
7   *     9/26/85
8 + *     6/23/93  Added additional buffers for value spreading
9   */
10  
11 < #include  <stdio.h>
11 > #include  "copyright.h"
12  
13   #include  <signal.h>
14 + #include  "pfilt.h"
15 + #include  "platform.h"
16 + #include  "view.h"
17  
18 < #include  "color.h"
18 > #define  FEQ(a,b)       ((a) >= .98*(b) && (a) <= 1.02*(b))
19  
20 < #include  "resolu.h"
20 > double   CHECKRAD = 2.0;        /* radius to check for filtering */
21  
22 < extern char  *malloc();
22 < extern float  *matchlamp();
22 > #define  THRESHRAD      5.0     /* maximum sample spread in output */
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
24   COLOR  exposure = WHTCOLOR;     /* exposure for the frame */
25  
26 < double  rad = 0.0;              /* output pixel radius for filtering */
26 > double  rad = 0.0;              /* output pixel radius for filtering */
27  
28 + double  thresh = 0.0;           /* maximum contribution for subpixel */
29 +
30   int  nrows = 0;                 /* number of rows for output */
31   int  ncols = 0;                 /* number of columns for output */
32  
33 < double  x_c = 1.0;              /* ratio of output x size to input */
34 < double  y_r = 1.0;              /* ratio of output y size to input */
33 > double  x_c = 1.0;              /* ratio of output x size to input */
34 > double  y_r = 1.0;              /* ratio of output y size to input */
35  
36   int  singlepass = 0;            /* true means skip first pass */
37  
38   int  avghot = 0;                /* true means average in bright spots */
39  
40 < double  hotlvl = 1000.0;        /* level considered "hot" */
40 > double  hotlvl = 100.0;         /* level considered "hot" */
41  
42   int  npts = 0;                  /* (half) number of points for stars */
43  
44 < double  spread = 1e-4;          /* spread for star points */
44 > double  spread = 1e-4;          /* spread for star points */
45  
48 #define  TEMPLATE       "/usr/tmp/pfXXXXXX"
49
46   char  *tfname = NULL;
47  
48 + char  template[] = TEMPLATE;
49 +
50   char  *lampdat = "lamp.tab";    /* lamp data file */
51  
52   int  order;                     /* scanline ordering of input */
53   int  xres, yres;                /* resolution of input */
54 < double  inpaspect = 1.0;        /* pixel aspect ratio of input */
54 > double  inpaspect = 1.0;        /* pixel aspect ratio of input */
55   int  correctaspect = 0;         /* aspect ratio correction? */
56  
57   int  wrongformat = 0;
58  
59 < int  xrad;                      /* x window size */
60 < int  yrad;                      /* y window size */
59 > VIEW  ourview = STDVIEW;
60 > int  gotview = 0;
61 > int  wrapfilt = 0;              /* wrap filter horizontally? */
62  
63 + int  estatus = 0;               /* exit status (for non-fatal errors) */
64 +
65 + int  xrad;                      /* x search radius */
66 + int  yrad;                      /* y search radius */
67 + int  xbrad;                     /* x box size */
68 + int  ybrad;                     /* y box size */
69 +
70   int  barsize;                   /* size of input scan bar */
71 < COLOR  **scanin;                /* input scan bar */
72 < COLOR  *scanout;                /* output scan line */
71 > COLORV  **scanin;               /* input scan bar */
72 > COLORV  *scanout;               /* output scan line */
73 > COLORV  **scoutbar;             /* output scan bar (if thresh > 0) */
74 > float  **greybar;               /* grey-averaged input values */
75 > int  obarsize = 0;              /* size of output scan bar */
76 > int  orad = 0;                  /* output window radius */
77  
78 < char  *progname;
78 > static void copyfile(FILE  *infp, FILE  *out);
79 > static void pass1(FILE  *infp);
80 > static void pass2(FILE  *infp);
81 > static void scan2init(void);
82 > static void scan2sync(int  r);
83 > static void scan2flush(void);
84  
85  
86 < main(argc, argv)
87 < int  argc;
88 < char  **argv;
86 > static double
87 > rgb_bright(
88 >        SCOLOR  clr
89 > )
90   {
91 <        extern char  *mktemp();
92 <        extern double  pow();
93 <        extern long  ftell();
94 <        extern int  quit(), headline();
91 >        return(bright(clr));
92 > }
93 >
94 >
95 > static double
96 > xyz_bright(
97 >        SCOLOR  clr
98 > )
99 > {
100 >        return(colval(clr,CIEY));
101 > }
102 >
103 >
104 > static double
105 > spec_bright(
106 >        SCOLOR  clr
107 > )
108 > {
109 >        return(pbright(clr));
110 > }
111 >
112 >
113 > brightfunc_t    *ourbright = rgb_bright;
114 >
115 >
116 > static int
117 > headline(                               /* process line from header */
118 >        char    *s,
119 >        void    *p
120 > )
121 > {
122 >        char  fmt[MAXFMTLEN];
123 >
124 >        fputs(s, stdout);               /* copy to output */
125 >        if (isaspect(s))                /* get aspect ratio */
126 >                inpaspect *= aspectval(s);
127 >        else if (isexpos(s))            /* get exposure */
128 >                hotlvl *= exposval(s);
129 >        else if (isncomp(s))            /* get #components (spectral) */
130 >                NCSAMP = ncompval(s);
131 >        else if (iswlsplit(s))          /* get wavelength partitions */
132 >                wlsplitval(WLPART, s);
133 >        else if (formatval(fmt, s)) {   /* get format */
134 >                wrongformat = 0;
135 >                if (!strcmp(COLRFMT, fmt))
136 >                        ourbright = rgb_bright;
137 >                else if (!strcmp(CIEFMT, fmt))
138 >                        ourbright = xyz_bright;
139 >                else if (!strcmp(SPECFMT, fmt))
140 >                        ourbright = spec_bright;
141 >                else
142 >                        wrongformat = !globmatch(PICFMT, fmt);
143 >        } else if (isview(s) && sscanview(&ourview, s) > 0)
144 >                gotview++;
145 >        return(0);
146 > }
147 >
148 >
149 > int
150 > main(
151 >        int  argc,
152 >        char  **argv
153 > )
154 > {
155          FILE  *fin;
156          float  *lampcolor;
157          char  *lamptype = NULL;
158          long  fpos;
159 <        double  outaspect = 0.0;
160 <        double  d;
159 >        double  outaspect = 0.0;
160 >        double  d;
161          int  i, j;
162  
163 +        fixargv0(argv[0]);              /* sets global progname */
164 +        SET_DEFAULT_BINARY();
165 +        SET_FILE_BINARY(stdin);
166 +        SET_FILE_BINARY(stdout);
167          if (signal(SIGINT, quit) == SIG_IGN)
168                  signal(SIGINT, SIG_IGN);
169 + #ifdef SIGHUP
170          if (signal(SIGHUP, quit) == SIG_IGN)
171 <                signal(SIGINT, SIG_IGN);
171 >                signal(SIGHUP, SIG_IGN);
172 > #endif
173          signal(SIGTERM, quit);
174 + #ifdef SIGPIPE
175          signal(SIGPIPE, quit);
176 < #ifdef  SIGXCPU
176 > #endif
177 > #ifdef  SIGXCPU
178          signal(SIGXCPU, quit);
179          signal(SIGXFSZ, quit);
180   #endif
97
98        progname = argv[0];
99
181          for (i = 1; i < argc; i++)
182                  if (argv[i][0] == '-')
183                          switch (argv[i][1]) {
# Line 131 | Line 212 | char  **argv;
212                                  if (d < 1e-20 || d > 1e20) {
213                                          fprintf(stderr,
214                                                  "%s: exposure out of range\n",
215 <                                                        argv[0]);
216 <                                        exit(1);
215 >                                                        progname);
216 >                                        quit(1);
217                                  }
218                                  switch (argv[i][2]) {
219                                  case '\0':
# Line 179 | Line 260 | char  **argv;
260                          case 'r':
261                                  rad = atof(argv[++i]);
262                                  break;
263 +                        case 'm':
264 +                                thresh = atof(argv[++i]);
265 +                                if (rad <= FTINY)
266 +                                        rad = 0.6;
267 +                                break;
268                          case 'b':
269 <                                rad = 0.0;
269 >                                rad = thresh = 0.0;
270                                  break;
271                          default:;
272                          badopt:
# Line 199 | Line 285 | char  **argv;
285                          fprintf(stderr, "%s: unknown lamp type\n", lamptype);
286                          quit(1);
287                  }
288 <                for (i = 0; i < 3; i++)
289 <                        if (lampcolor[i] > 1e-4)
290 <                                colval(exposure,i) /= lampcolor[i];
288 >                for (j = 0; j < 3; j++)
289 >                        if (lampcolor[j] > 1e-4)
290 >                                colval(exposure,j) /= lampcolor[j];
291                  freelamps();
292          }
293                                          /* open input file */
# Line 209 | Line 295 | char  **argv;
295                  if (singlepass)
296                          fin = stdin;
297                  else {
298 <                        tfname = mktemp(TEMPLATE);
299 <                        if ((fin = fopen(tfname, "w+")) == NULL) {
298 >                        tfname = mktemp(template);
299 >                        if ((fin = fopen(tfname, "w+b")) == NULL) {
300                                  fprintf(stderr, "%s: can't create ", progname);
301                                  fprintf(stderr, "temp file \"%s\"\n", tfname);
302                                  quit(1);
# Line 222 | Line 308 | char  **argv;
308                          }
309                  }
310          } else if (i == argc-1) {
311 <                if ((fin = fopen(argv[i], "r")) == NULL) {
311 >                if ((fin = fopen(argv[i], "rb")) == NULL) {
312                          fprintf(stderr, "%s: can't open file \"%s\"\n",
313                                                  progname, argv[i]);
314                          quit(1);
# Line 238 | Line 324 | char  **argv;
324                                  progname);
325                  quit(1);
326          }
327 +        if ((ourbright == spec_bright) ^ (NCSAMP > 3) ||
328 +                        setspectrsamp(CNDX, WLPART) < 0) {
329 +                fprintf(stderr, "%s: bad number of components\n", progname);
330 +                quit(1);
331 +        }
332                                          /* add new header info. */
333          printargs(i, argv, stdout);
334                                          /* get picture size */
# Line 247 | Line 338 | char  **argv;
338          }
339          if (!(order & YMAJOR))
340                  inpaspect = 1.0/inpaspect;
341 +                                        /* wrap around for cylindrical view? */
342 +        wrapfilt = gotview && ourview.type == VT_CYL &&
343 +                        ourview.horiz >= 360.-FTINY && order & YMAJOR;
344                                          /* compute output resolution */
345          if (ncols <= 0)
346                  ncols = x_c*xres + .5;
# Line 278 | Line 372 | char  **argv;
372          }
373          pass2(fin);
374  
375 <        quit(0);
375 >        quit(estatus);
376 >        return estatus; /* pro forma return */
377   }
378  
379  
380 < headline(s)                             /* process line from header */
381 < char  *s;
380 > static void
381 > copyfile(                       /* copy a file */
382 >        FILE  *infp,
383 >        FILE  *out
384 > )
385   {
386 <        char  fmt[32];
386 >        int  c;
387  
388 <        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 < {
303 <        register int  c;
304 <
305 <        while ((c = getc(in)) != EOF)
388 >        while ((c = getc(infp)) != EOF)
389                  putc(c, out);
390  
391          if (ferror(out)) {
# Line 312 | Line 395 | register FILE  *in, *out;
395   }
396  
397  
398 < pass1(in)                               /* first pass of picture file */
399 < FILE  *in;
398 > static void
399 > pass1(                          /* first pass of picture file */
400 >        FILE  *infp
401 > )
402   {
403          int  i;
404 <        COLOR  *scan;
404 >        COLORV  *scan;
405  
406          pass1init();
407  
408 <        scan = (COLOR *)malloc(xres*sizeof(COLOR));
408 >        scan = (COLORV *)malloc(xres*NCSAMP*sizeof(COLORV));
409          if (scan == NULL) {
410                  fprintf(stderr, "%s: out of memory\n", progname);
411                  quit(1);
412          }
413          for (i = 0; i < yres; i++) {
414 <                if (freadscan(scan, xres, in) < 0) {
415 <                        nrows = nrows * i / yres;       /* adjust frame */
414 >                if (freadsscan(scan, NCSAMP, xres, infp) < 0) {
415 >                        nrows = (long)nrows * i / yres; /* adjust frame */
416                          if (nrows <= 0) {
417                                  fprintf(stderr, "%s: empty frame\n", progname);
418                                  quit(1);
419                          }
420                          fprintf(stderr, "%s: warning - partial frame (%d%%)\n",
421 <                                        progname, 100*i/yres);
421 >                                        progname, (int)(100L*i/yres));
422                          yres = i;
423                          y_r = (double)nrows/yres;
424 +                        estatus++;
425                          break;
426                  }
427                  pass1scan(scan, i);
428          }
429 <        free((char *)scan);
429 >        free(scan);
430   }
431  
432  
433 < pass2(in)                       /* last pass on file, write to stdout */
434 < FILE  *in;
433 > static void
434 > pass2(                  /* last pass on file, write to stdout */
435 >        FILE  *infp
436 > )
437   {
438          int  yread;
439          int  ycent, xcent;
440          int  r, c;
441 <        
441 >        
442          pass2init();
443          scan2init();
444          yread = 0;
445          for (r = 0; r < nrows; r++) {
446 <                ycent = (long)r*yres/nrows;
446 >                ycent = (r+.5)*yres/nrows;
447                  while (yread <= ycent+yrad) {
448                          if (yread < yres) {
449 <                                if (freadscan(scanin[yread%barsize],
450 <                                                xres, in) < 0) {
449 >                                if (freadsscan(scanin[yread%barsize],
450 >                                                NCSAMP, xres, infp) < 0) {
451                                          fprintf(stderr,
452 <                                                "%s: bad read (y=%d)\n",
452 >                                                "%s: truncated input (y=%d)\n",
453                                                  progname, yres-1-yread);
454                                          quit(1);
455                                  }
# Line 369 | Line 457 | FILE  *in;
457                          }
458                          yread++;
459                  }
460 +                if (obarsize > 0)       /* => thresh > FTINY */
461 +                        scan2sync(r);
462                  for (c = 0; c < ncols; c++) {
463 <                        xcent = (long)c*xres/ncols;
464 <                        if (rad <= 0.0)
465 <                                dobox(scanout[c], xcent, ycent, c, r);
463 >                        xcent = (c+.5)*xres/ncols;
464 >                        if (thresh > FTINY)
465 >                                dothresh(xcent, ycent, c, r);
466 >                        else if (rad > FTINY)
467 >                                dogauss(scanout+c*NCSAMP, xcent, ycent, c, r);
468                          else
469 <                                dogauss(scanout[c], xcent, ycent, c, r);
469 >                                dobox(scanout+c*NCSAMP, xcent, ycent, c, r);
470                  }
471 <                if (fwritescan(scanout, ncols, stdout) < 0) {
471 >                if (scanout != NULL && fwritesscan(scanout, NCSAMP, ncols, stdout) < 0) {
472                          fprintf(stderr, "%s: write error in pass2\n", progname);
473                          quit(1);
474                  }
475          }
476 <                                        /* skip leftovers */
476 >                                        /* skip leftover input */
477          while (yread < yres) {
478 <                if (freadscan(scanin[0], xres, in) < 0)
478 >                if (freadscolrs((uby8 *)tempbuffer(xres*(NCSAMP+1)),
479 >                                        NCSAMP, xres, infp) < 0)
480                          break;
481                  yread++;
482          }
483 +        scan2flush();                   /* flush output */
484   }
485  
486  
487 < scan2init()                     /* prepare scanline arrays */
487 > static void
488 > scan2init(void)                 /* prepare scanline arrays */
489   {
490          COLOR   ctmp;
491 <        double  d;
492 <        register int  i;
491 >        double  d;
492 >        int  i;
493  
494 <        if (rad <= 0.0) {
495 <                xrad = xres/ncols/2 + 1;
496 <                yrad = yres/nrows/2 + 1;
402 <        } else {
494 >        xbrad = xres/ncols/2 + 1;
495 >        ybrad = yres/nrows/2 + 1;
496 >        if (rad > FTINY) {
497                  if (nrows >= yres && ncols >= xres)
498                          rad *= (y_r + x_c)/2.0;
499  
500 <                xrad = CHECKRAD*rad/x_c + 1;
501 <                yrad = CHECKRAD*rad/y_r + 1;
502 <
500 >                if (thresh > FTINY) {
501 >                        orad = CHECKRAD*THRESHRAD*rad + 1;
502 >                        xrad = orad/x_c + xbrad;
503 >                        yrad = orad/y_r + ybrad;
504 >                        obarsize = 2*orad + 1;
505 >                } else {
506 >                        xrad = CHECKRAD*rad/x_c + 1;
507 >                        yrad = CHECKRAD*rad/y_r + 1;
508 >                }
509                  initmask();             /* initialize filter table */
510 +        } else {
511 +                xrad = xbrad;
512 +                yrad = ybrad;
513          }
514          barsize = 2*yrad + 1;
515 <        scanin = (COLOR **)malloc(barsize*sizeof(COLOR *));
515 >        scanin = (COLORV **)malloc(barsize*sizeof(COLORV *));
516 >        if (scanin == NULL)
517 >                goto memerr;
518          for (i = 0; i < barsize; i++) {
519 <                scanin[i] = (COLOR *)malloc(xres*sizeof(COLOR));
520 <                if (scanin[i] == NULL) {
521 <                        fprintf(stderr, "%s: out of memory\n", progname);
522 <                        quit(1);
519 >                scanin[i] = (COLORV *)malloc(xres*NCSAMP*sizeof(COLORV));
520 >                if (scanin[i] == NULL)
521 >                        goto memerr;
522 >        }
523 >        if (obarsize > 0) {
524 >                scoutbar = (COLORV **)malloc(obarsize*sizeof(COLORV *));
525 >                greybar = (float **)malloc(obarsize*sizeof(float *));
526 >                if ((scoutbar == NULL) | (greybar == NULL))
527 >                        goto memerr;
528 >                for (i = 0; i < obarsize; i++) {
529 >                        scoutbar[i] = (COLORV *)malloc(ncols*NCSAMP*sizeof(COLORV));
530 >                        greybar[i] = (float *)malloc(ncols*sizeof(float));
531 >                        if ((scoutbar[i] == NULL) | (greybar[i] == NULL))
532 >                                goto memerr;
533                  }
534 +        } else {
535 +                scanout = (COLORV *)malloc(ncols*NCSAMP*sizeof(COLORV));
536 +                if (scanout == NULL)
537 +                        goto memerr;
538          }
420        scanout = (COLOR *)malloc(ncols*sizeof(COLOR));
421        if (scanout == NULL) {
422                fprintf(stderr, "%s: out of memory\n", progname);
423                quit(1);
424        }
539                                          /* record pixel aspect ratio */
540          if (!correctaspect) {
541                  d = order & YMAJOR ? x_c/y_r : y_r/x_c ;
# Line 441 | Line 555 | scan2init()                    /* prepare scanline arrays */
555          printf("\n");
556                                          /* write out resolution */
557          fputresolu(order, ncols, nrows, stdout);
558 +        return;
559 + memerr:
560 +        fprintf(stderr, "%s: out of memory\n", progname);
561 +        quit(1);
562   }
563  
564  
565 + static void
566 + scan2sync(                      /* synchronize grey averages and output scan */
567 +        int  r
568 + )
569 + {
570 +        static int  nextrow = 0;
571 +        SCOLOR  ctmp;
572 +        int  ybot;
573 +        int  c;
574 +                                        /* average input scanlines */
575 +        while (nextrow <= r+orad && nextrow < nrows) {
576 +                ybot = (nextrow+.5)*yres/nrows;
577 +                for (c = 0; c < ncols; c++) {
578 +                        dobox(ctmp, (int)((c+.5)*xres/ncols),ybot, c,nextrow);
579 +                        greybar[nextrow%obarsize][c] = (*ourbright)(ctmp);
580 +                }
581 +                                        /* and zero output scanline */
582 +                memset(scoutbar[nextrow%obarsize], 0, ncols*NCSAMP*sizeof(COLORV));
583 +                nextrow++;
584 +        }
585 +                                        /* point to top scanline for output */
586 +        if (r-orad >= 0)
587 +                scanout = scoutbar[(r-orad)%obarsize];
588 +        else
589 +                scanout = NULL;
590 + }
591 +
592 +
593 + static void
594 + scan2flush(void)                        /* flush output buffer */
595 + {
596 +        int  r;
597 +
598 +        for (r = nrows-orad; r < nrows; r++)
599 +                if (fwritesscan(scoutbar[r%obarsize], NCSAMP, ncols, stdout) < 0)
600 +                        break;
601 +        if (fflush(stdout) < 0) {
602 +                fprintf(stderr, "%s: write error at end of pass2\n", progname);
603 +                quit(1);
604 +        }
605 + }
606 +
607 +
608 + void
609   quit(code)              /* remove temporary file and exit */
610   int  code;
611   {

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines