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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines