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 1.11 by greg, Tue Oct 16 13:52:44 1990 UTC vs.
Revision 2.34 by greg, Sat Dec 28 18:05:14 2019 UTC

# Line 1 | Line 1
1 /* Copyright (c) 1986 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  
15 + #include  "platform.h"
16 + #include  "standard.h"
17 + #include  "rtio.h"
18   #include  "color.h"
19 + #include  "view.h"
20 + #include  "paths.h"
21 + #include  "pfilt.h"
22  
19 extern char  *malloc();
23  
24 < #define  CHECKRAD       1.5     /* radius to check for filtering */
24 > #define  FEQ(a,b)       ((a) >= .98*(b) && (a) <= 1.02*(b))
25  
26 + double   CHECKRAD = 2.0;        /* radius to check for filtering */
27 +
28 + #define  THRESHRAD      5.0     /* maximum sample spread in output */
29 +
30   COLOR  exposure = WHTCOLOR;     /* exposure for the frame */
31  
32 < double  rad = 0.0;              /* output pixel radius for filtering */
32 > double  rad = 0.0;              /* output pixel radius for filtering */
33  
34 + double  thresh = 0.0;           /* maximum contribution for subpixel */
35 +
36   int  nrows = 0;                 /* number of rows for output */
37   int  ncols = 0;                 /* number of columns for output */
38  
39 < double  x_c = 1.0;              /* ratio of output x size to input */
40 < double  y_r = 1.0;              /* ratio of output y size to input */
39 > double  x_c = 1.0;              /* ratio of output x size to input */
40 > double  y_r = 1.0;              /* ratio of output y size to input */
41  
42   int  singlepass = 0;            /* true means skip first pass */
43  
44   int  avghot = 0;                /* true means average in bright spots */
45  
46 < double  hotlvl = 1000.0;        /* level considered "hot" */
46 > double  hotlvl = 100.0;         /* level considered "hot" */
47  
48   int  npts = 0;                  /* (half) number of points for stars */
49  
50 < double  spread = 1e-4;          /* spread for star points */
50 > double  spread = 1e-4;          /* spread for star points */
51  
43 #define  TEMPLATE       "/usr/tmp/pfXXXXXX"
44
52   char  *tfname = NULL;
53  
54 + char  template[] = TEMPLATE;
55 +
56 + char  *lampdat = "lamp.tab";    /* lamp data file */
57 +
58 + int  order;                     /* scanline ordering of input */
59   int  xres, yres;                /* resolution of input */
60 < double  inpaspect = 1.0;        /* pixel aspect ratio of input */
60 > double  inpaspect = 1.0;        /* pixel aspect ratio of input */
61 > int  correctaspect = 0;         /* aspect ratio correction? */
62  
63 < int  xrad;                      /* x window size */
51 < int  yrad;                      /* y window size */
63 > int  wrongformat = 0;
64  
65 + VIEW  ourview = STDVIEW;
66 + int  gotview = 0;
67 + int  wrapfilt = 0;              /* wrap filter horizontally? */
68 +
69 + int  estatus = 0;               /* exit status (for non-fatal errors) */
70 +
71 + int  xrad;                      /* x search radius */
72 + int  yrad;                      /* y search radius */
73 + int  xbrad;                     /* x box size */
74 + int  ybrad;                     /* y box size */
75 +
76   int  barsize;                   /* size of input scan bar */
77   COLOR  **scanin;                /* input scan bar */
78   COLOR  *scanout;                /* output scan line */
79 + COLOR  **scoutbar;              /* output scan bar (if thresh > 0) */
80 + float  **greybar;               /* grey-averaged input values */
81 + int  obarsize = 0;              /* size of output scan bar */
82 + int  orad = 0;                  /* output window radius */
83  
84   char  *progname;
85  
86 + static gethfunc headline;
87 + static brightfunc_t rgb_bright;
88 + static brightfunc_t xyz_bright;
89 + static void copyfile(FILE  *in, FILE  *out);
90 + static void pass1(FILE  *in);
91 + static void pass2(FILE  *in);
92 + static void scan2init(void);
93 + static void scan2sync(int  r);
94 + static void scan2flush(void);
95  
96 < main(argc, argv)
97 < int  argc;
98 < char  **argv;
96 >
97 > int
98 > main(
99 >        int  argc,
100 >        char  **argv
101 > )
102   {
64        extern char  *mktemp();
65        extern double  atof(), pow();
66        extern long  ftell();
67        extern int  quit(), headline();
103          FILE  *fin;
104 +        float  *lampcolor;
105 +        char  *lamptype = NULL;
106          long  fpos;
107 <        double  outaspect = 0.0;
108 <        double  d;
109 <        int  i;
110 <
107 >        double  outaspect = 0.0;
108 >        double  d;
109 >        int  i, j;
110 >        SET_DEFAULT_BINARY();
111 >        SET_FILE_BINARY(stdin);
112 >        SET_FILE_BINARY(stdout);
113          if (signal(SIGINT, quit) == SIG_IGN)
114                  signal(SIGINT, SIG_IGN);
115 + #ifdef SIGHUP
116          if (signal(SIGHUP, quit) == SIG_IGN)
117 <                signal(SIGINT, SIG_IGN);
117 >                signal(SIGHUP, SIG_IGN);
118 > #endif
119          signal(SIGTERM, quit);
120 + #ifdef SIGPIPE
121          signal(SIGPIPE, quit);
122 < #ifdef  SIGXCPU
122 > #endif
123 > #ifdef  SIGXCPU
124          signal(SIGXCPU, quit);
125          signal(SIGXFSZ, quit);
126   #endif
127  
128 <        progname = argv[0];
128 >        progname = argv[0] = fixargv0(argv[0]);
129  
130          for (i = 1; i < argc; i++)
131                  if (argv[i][0] == '-')
# Line 103 | Line 146 | char  **argv;
146                                  } else
147                                          nrows = atoi(argv[i]);
148                                  break;
149 +                        case 'c':
150 +                                correctaspect = !correctaspect;
151 +                                break;
152                          case 'p':
153                                  i++;
154                                  outaspect = atof(argv[i]);
# Line 112 | Line 158 | char  **argv;
158                                          d = pow(2.0, atof(argv[i+1]));
159                                  else
160                                          d = atof(argv[i+1]);
161 +                                if (d < 1e-20 || d > 1e20) {
162 +                                        fprintf(stderr,
163 +                                                "%s: exposure out of range\n",
164 +                                                        argv[0]);
165 +                                        quit(1);
166 +                                }
167                                  switch (argv[i][2]) {
168                                  case '\0':
169                                          scalecolor(exposure, d);
# Line 130 | Line 182 | char  **argv;
182                                  }
183                                  i++;
184                                  break;
185 +                        case 'f':
186 +                                lampdat = argv[++i];
187 +                                break;
188 +                        case 't':
189 +                                lamptype = argv[++i];
190 +                                break;
191                          case '1':
192                                  singlepass = 1;
193                                  break;
# Line 151 | Line 209 | char  **argv;
209                          case 'r':
210                                  rad = atof(argv[++i]);
211                                  break;
212 +                        case 'm':
213 +                                thresh = atof(argv[++i]);
214 +                                if (rad <= FTINY)
215 +                                        rad = 0.6;
216 +                                break;
217                          case 'b':
218 <                                rad = 0.0;
218 >                                rad = thresh = 0.0;
219                                  break;
220                          default:;
221                          badopt:
# Line 163 | Line 226 | char  **argv;
226                          }
227                  else
228                          break;
229 <                        
229 >                                        /* get lamp data (if necessary) */
230 >        if (lamptype != NULL) {
231 >                if (loadlamps(lampdat) < 0)
232 >                        quit(1);
233 >                if ((lampcolor = matchlamp(lamptype)) == NULL) {
234 >                        fprintf(stderr, "%s: unknown lamp type\n", lamptype);
235 >                        quit(1);
236 >                }
237 >                for (j = 0; j < 3; j++)
238 >                        if (lampcolor[j] > 1e-4)
239 >                                colval(exposure,j) /= lampcolor[j];
240 >                freelamps();
241 >        }
242 >                                        /* open input file */
243          if (i == argc) {
244                  if (singlepass)
245                          fin = stdin;
246                  else {
247 <                        tfname = mktemp(TEMPLATE);
247 >                        tfname = mktemp(template);
248                          if ((fin = fopen(tfname, "w+")) == NULL) {
249                                  fprintf(stderr, "%s: can't create ", progname);
250                                  fprintf(stderr, "temp file \"%s\"\n", tfname);
# Line 191 | Line 267 | char  **argv;
267                  quit(1);
268          }
269                                          /* get header */
270 <        getheader(fin, headline);
270 >        getheader(fin, headline, NULL);
271 >        if (wrongformat) {
272 >                fprintf(stderr, "%s: input must be a Radiance picture\n",
273 >                                progname);
274 >                quit(1);
275 >        }
276                                          /* add new header info. */
277          printargs(i, argv, stdout);
278                                          /* get picture size */
279 <        if (fgetresolu(&xres, &yres, fin) != (YMAJOR|YDECR)) {
279 >        if ((order = fgetresolu(&xres, &yres, fin)) < 0) {
280                  fprintf(stderr, "%s: bad picture size\n", progname);
281                  quit(1);
282          }
283 +        if (!(order & YMAJOR))
284 +                inpaspect = 1.0/inpaspect;
285 +                                        /* wrap around for cylindrical view? */
286 +        wrapfilt = gotview && ourview.type == VT_CYL &&
287 +                        ourview.horiz >= 360.-FTINY && order & YMAJOR;
288                                          /* compute output resolution */
289          if (ncols <= 0)
290                  ncols = x_c*xres + .5;
# Line 230 | Line 316 | char  **argv;
316          }
317          pass2(fin);
318  
319 <        quit(0);
319 >        quit(estatus);
320 >        return estatus; /* pro forma return */
321   }
322  
323  
324 < headline(s)                             /* process line from header */
325 < char  *s;
324 > static double
325 > rgb_bright(
326 >        COLOR  clr
327 > )
328   {
329 +        return(bright(clr));
330 + }
331 +
332 +
333 + static double
334 + xyz_bright(
335 +        COLOR  clr
336 + )
337 + {
338 +        return(clr[CIEY]);
339 + }
340 +
341 +
342 + brightfunc_t *ourbright = rgb_bright;
343 +
344 + static int
345 + headline(                               /* process line from header */
346 +        char    *s,
347 +        void    *p
348 + )
349 + {
350 +        char  fmt[MAXFMTLEN];
351 +
352          fputs(s, stdout);               /* copy to output */
353          if (isaspect(s))                /* get aspect ratio */
354                  inpaspect *= aspectval(s);
355 +        else if (isexpos(s))            /* get exposure */
356 +                hotlvl *= exposval(s);
357 +        else if (formatval(fmt, s)) {   /* get format */
358 +                wrongformat = 0;
359 +                if (!strcmp(COLRFMT, fmt))
360 +                        ourbright = rgb_bright;
361 +                else if (!strcmp(CIEFMT, fmt))
362 +                        ourbright = xyz_bright;
363 +                else
364 +                        wrongformat = !globmatch(PICFMT, fmt);
365 +        } else if (isview(s) && sscanview(&ourview, s) > 0)
366 +                gotview++;
367 +        return(0);
368   }
369  
370  
371 < copyfile(in, out)                       /* copy a file */
372 < register FILE  *in, *out;
371 > static void
372 > copyfile(                       /* copy a file */
373 >        FILE  *in,
374 >        FILE  *out
375 > )
376   {
377 <        register int  c;
377 >        int  c;
378  
379          while ((c = getc(in)) != EOF)
380                  putc(c, out);
# Line 258 | Line 386 | register FILE  *in, *out;
386   }
387  
388  
389 < pass1(in)                               /* first pass of picture file */
390 < FILE  *in;
389 > static void
390 > pass1(                          /* first pass of picture file */
391 >        FILE  *in
392 > )
393   {
394          int  i;
395          COLOR  *scan;
# Line 273 | Line 403 | FILE  *in;
403          }
404          for (i = 0; i < yres; i++) {
405                  if (freadscan(scan, xres, in) < 0) {
406 <                        nrows = nrows * i / yres;       /* adjust frame */
406 >                        nrows = (long)nrows * i / yres; /* adjust frame */
407                          if (nrows <= 0) {
408                                  fprintf(stderr, "%s: empty frame\n", progname);
409                                  quit(1);
410                          }
411                          fprintf(stderr, "%s: warning - partial frame (%d%%)\n",
412 <                                        progname, 100*i/yres);
412 >                                        progname, (int)(100L*i/yres));
413                          yres = i;
414                          y_r = (double)nrows/yres;
415 +                        estatus++;
416                          break;
417                  }
418                  pass1scan(scan, i);
419          }
420 <        free((char *)scan);
420 >        free((void *)scan);
421   }
422  
423  
424 < pass2(in)                       /* last pass on file, write to stdout */
425 < FILE  *in;
424 > static void
425 > pass2(                  /* last pass on file, write to stdout */
426 >        FILE  *in
427 > )
428   {
429          int  yread;
430          int  ycent, xcent;
431          int  r, c;
432 <        
432 >        
433          pass2init();
434          scan2init();
435          yread = 0;
436          for (r = 0; r < nrows; r++) {
437 <                ycent = (long)r*yres/nrows;
437 >                ycent = (r+.5)*yres/nrows;
438                  while (yread <= ycent+yrad) {
439                          if (yread < yres) {
440                                  if (freadscan(scanin[yread%barsize],
441                                                  xres, in) < 0) {
442                                          fprintf(stderr,
443 <                                                "%s: bad read (y=%d)\n",
443 >                                                "%s: truncated input (y=%d)\n",
444                                                  progname, yres-1-yread);
445                                          quit(1);
446                                  }
# Line 315 | Line 448 | FILE  *in;
448                          }
449                          yread++;
450                  }
451 +                if (obarsize > 0)
452 +                        scan2sync(r);
453                  for (c = 0; c < ncols; c++) {
454 <                        xcent = (long)c*xres/ncols;
455 <                        if (rad <= 0.0)
456 <                                dobox(scanout[c], xcent, ycent, c, r);
457 <                        else
454 >                        xcent = (c+.5)*xres/ncols;
455 >                        if (thresh > FTINY)
456 >                                dothresh(xcent, ycent, c, r);
457 >                        else if (rad > FTINY)
458                                  dogauss(scanout[c], xcent, ycent, c, r);
459 +                        else
460 +                                dobox(scanout[c], xcent, ycent, c, r);
461                  }
462 <                if (fwritescan(scanout, ncols, stdout) < 0) {
462 >                if (scanout != NULL && fwritescan(scanout, ncols, stdout) < 0) {
463                          fprintf(stderr, "%s: write error in pass2\n", progname);
464                          quit(1);
465                  }
466          }
467 <                                        /* skip leftovers */
467 >                                        /* skip leftover input */
468          while (yread < yres) {
469                  if (freadscan(scanin[0], xres, in) < 0)
470                          break;
471                  yread++;
472          }
473 +        scan2flush();                   /* flush output */
474   }
475  
476  
477 < scan2init()                     /* prepare scanline arrays */
477 > static void
478 > scan2init(void)                 /* prepare scanline arrays */
479   {
480 <        double  d;
481 <        register int  i;
480 >        COLOR   ctmp;
481 >        double  d;
482 >        int  i;
483  
484 <        if (rad <= 0.0) {
485 <                xrad = xres/ncols/2 + 1;
486 <                yrad = yres/nrows/2 + 1;
347 <        } else {
484 >        xbrad = xres/ncols/2 + 1;
485 >        ybrad = yres/nrows/2 + 1;
486 >        if (rad > FTINY) {
487                  if (nrows >= yres && ncols >= xres)
488                          rad *= (y_r + x_c)/2.0;
489  
490 <                xrad = CHECKRAD*rad/x_c + 1;
491 <                yrad = CHECKRAD*rad/y_r + 1;
492 <
490 >                if (thresh > FTINY) {
491 >                        orad = CHECKRAD*THRESHRAD*rad + 1;
492 >                        xrad = orad/x_c + xbrad;
493 >                        yrad = orad/y_r + ybrad;
494 >                        obarsize = 2*orad + 1;
495 >                } else {
496 >                        xrad = CHECKRAD*rad/x_c + 1;
497 >                        yrad = CHECKRAD*rad/y_r + 1;
498 >                }
499                  initmask();             /* initialize filter table */
500 +        } else {
501 +                xrad = xbrad;
502 +                yrad = ybrad;
503          }
504 <        barsize = 2 * yrad;
504 >        barsize = 2*yrad + 1;
505          scanin = (COLOR **)malloc(barsize*sizeof(COLOR *));
506 +        if (scanin == NULL)
507 +                goto memerr;
508          for (i = 0; i < barsize; i++) {
509                  scanin[i] = (COLOR *)malloc(xres*sizeof(COLOR));
510 <                if (scanin[i] == NULL) {
511 <                        fprintf(stderr, "%s: out of memory\n", progname);
512 <                        quit(1);
510 >                if (scanin[i] == NULL)
511 >                        goto memerr;
512 >        }
513 >        if (obarsize > 0) {
514 >                scoutbar = (COLOR **)malloc(obarsize*sizeof(COLOR *));
515 >                greybar = (float **)malloc(obarsize*sizeof(float *));
516 >                if ((scoutbar == NULL) | (greybar == NULL))
517 >                        goto memerr;
518 >                for (i = 0; i < obarsize; i++) {
519 >                        scoutbar[i] = (COLOR *)malloc(ncols*sizeof(COLOR));
520 >                        greybar[i] = (float *)malloc(ncols*sizeof(float));
521 >                        if ((scoutbar[i] == NULL) | (greybar[i] == NULL))
522 >                                goto memerr;
523                  }
524 +        } else {
525 +                scanout = (COLOR *)malloc(ncols*sizeof(COLOR));
526 +                if (scanout == NULL)
527 +                        goto memerr;
528          }
529 <        scanout = (COLOR *)malloc(ncols*sizeof(COLOR));
530 <        if (scanout == NULL) {
531 <                fprintf(stderr, "%s: out of memory\n", progname);
532 <                quit(1);
529 >                                        /* record pixel aspect ratio */
530 >        if (!correctaspect) {
531 >                d = order & YMAJOR ? x_c/y_r : y_r/x_c ;
532 >                if (!FEQ(d,1.0))
533 >                        fputaspect(d, stdout);
534          }
535 <                                        /* record pixel aspect and exposure */
536 <        d = x_c / y_r;
537 <        if (d < .99 || d > 1.01)
373 <                fputaspect(d, stdout);
374 <        d = bright(exposure);
375 <        if (d < .995 || d > 1.005)
535 >                                        /* record exposure */
536 >        d = (*ourbright)(exposure);
537 >        if (!FEQ(d,1.0))
538                  fputexpos(d, stdout);
539 +                                        /* record color correction */
540 +        copycolor(ctmp, exposure);
541 +        scalecolor(ctmp, 1.0/d);
542 +        if (!FEQ(colval(ctmp,RED),colval(ctmp,GRN)) ||
543 +                        !FEQ(colval(ctmp,GRN),colval(ctmp,BLU)))
544 +                fputcolcor(ctmp, stdout);
545          printf("\n");
546 <        fputresolu(YMAJOR|YDECR, ncols, nrows, stdout); /* resolution */
546 >                                        /* write out resolution */
547 >        fputresolu(order, ncols, nrows, stdout);
548 >        return;
549 > memerr:
550 >        fprintf(stderr, "%s: out of memory\n", progname);
551 >        quit(1);
552   }
553  
554  
555 + static void
556 + scan2sync(                      /* synchronize grey averages and output scan */
557 +        int  r
558 + )
559 + {
560 +        static int  nextrow = 0;
561 +        COLOR  ctmp;
562 +        int  ybot;
563 +        int  c;
564 +                                        /* average input scanlines */
565 +        while (nextrow <= r+orad && nextrow < nrows) {
566 +                ybot = (nextrow+.5)*yres/nrows;
567 +                for (c = 0; c < ncols; c++) {
568 +                        dobox(ctmp, (int)((c+.5)*xres/ncols),ybot, c,nextrow);
569 +                        greybar[nextrow%obarsize][c] = (*ourbright)(ctmp);
570 +                }
571 +                                        /* and zero output scanline */
572 +                memset((char *)scoutbar[nextrow%obarsize], '\0', ncols*sizeof(COLOR));
573 +                nextrow++;
574 +        }
575 +                                        /* point to top scanline for output */
576 +        if (r-orad >= 0)
577 +                scanout = scoutbar[(r-orad)%obarsize];
578 +        else
579 +                scanout = NULL;
580 + }
581 +
582 +
583 + static void
584 + scan2flush(void)                        /* flush output buffer */
585 + {
586 +        int  r;
587 +
588 +        for (r = nrows-orad; r < nrows; r++)
589 +                if (fwritescan(scoutbar[r%obarsize], ncols, stdout) < 0)
590 +                        break;
591 +        if (fflush(stdout) < 0) {
592 +                fprintf(stderr, "%s: write error at end of pass2\n", progname);
593 +                quit(1);
594 +        }
595 + }
596 +
597 +
598 + void
599   quit(code)              /* remove temporary file and exit */
600   int  code;
601   {

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines