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.17 by greg, Wed Jun 5 12:15:30 1991 UTC vs.
Revision 2.36 by greg, Fri Dec 8 17:56:26 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();
20 < extern float  *matchlamp();
21 > double   CHECKRAD = 2.0;        /* radius to check for filtering */
22  
23 < #define  FEQ(a,b)       ((a) >= .98*(b) && (a) <= 1.02*(b))
23 > #define  THRESHRAD      5.0     /* maximum sample spread in output */
24  
24 #define  CHECKRAD       1.5     /* radius to check for filtering */
25
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  
46 #define  TEMPLATE       "/usr/tmp/pfXXXXXX"
47
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  wrongformat = 0;
59  
60 < int  xrad;                      /* x window size */
61 < int  yrad;                      /* y window size */
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 */
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 void copyfile(FILE  *in, FILE  *out);
83 + static void pass1(FILE  *in);
84 + static void pass2(FILE  *in);
85 + static void scan2init(void);
86 + static void scan2sync(int  r);
87 + static void scan2flush(void);
88  
89 < main(argc, argv)
90 < int  argc;
91 < char  **argv;
89 >
90 > int
91 > main(
92 >        int  argc,
93 >        char  **argv
94 > )
95   {
72        extern char  *mktemp();
73        extern double  atof(), pow();
74        extern long  ftell();
75        extern int  quit(), headline();
96          FILE  *fin;
97          float  *lampcolor;
98          char  *lamptype = NULL;
99          long  fpos;
100 <        double  outaspect = 0.0;
101 <        double  d;
102 <        int  i;
103 <
100 >        double  outaspect = 0.0;
101 >        double  d;
102 >        int  i, j;
103 >        SET_DEFAULT_BINARY();
104 >        SET_FILE_BINARY(stdin);
105 >        SET_FILE_BINARY(stdout);
106          if (signal(SIGINT, quit) == SIG_IGN)
107                  signal(SIGINT, SIG_IGN);
108 + #ifdef SIGHUP
109          if (signal(SIGHUP, quit) == SIG_IGN)
110 <                signal(SIGINT, SIG_IGN);
110 >                signal(SIGHUP, SIG_IGN);
111 > #endif
112          signal(SIGTERM, quit);
113 + #ifdef SIGPIPE
114          signal(SIGPIPE, quit);
115 < #ifdef  SIGXCPU
115 > #endif
116 > #ifdef  SIGXCPU
117          signal(SIGXCPU, quit);
118          signal(SIGXFSZ, quit);
119   #endif
120  
121 <        progname = argv[0];
121 >        progname = argv[0] = fixargv0(argv[0]);
122  
123          for (i = 1; i < argc; i++)
124                  if (argv[i][0] == '-')
# Line 129 | Line 155 | char  **argv;
155                                          fprintf(stderr,
156                                                  "%s: exposure out of range\n",
157                                                          argv[0]);
158 <                                        exit(1);
158 >                                        quit(1);
159                                  }
160                                  switch (argv[i][2]) {
161                                  case '\0':
# Line 176 | Line 202 | char  **argv;
202                          case 'r':
203                                  rad = atof(argv[++i]);
204                                  break;
205 +                        case 'm':
206 +                                thresh = atof(argv[++i]);
207 +                                if (rad <= FTINY)
208 +                                        rad = 0.6;
209 +                                break;
210                          case 'b':
211 <                                rad = 0.0;
211 >                                rad = thresh = 0.0;
212                                  break;
213                          default:;
214                          badopt:
# Line 196 | Line 227 | char  **argv;
227                          fprintf(stderr, "%s: unknown lamp type\n", lamptype);
228                          quit(1);
229                  }
230 <                colval(exposure,RED) /= lampcolor[0];
231 <                colval(exposure,GRN) /= lampcolor[1];
232 <                colval(exposure,BLU) /= lampcolor[2];
230 >                for (j = 0; j < 3; j++)
231 >                        if (lampcolor[j] > 1e-4)
232 >                                colval(exposure,j) /= lampcolor[j];
233                  freelamps();
234          }
235                                          /* open input file */
# Line 206 | Line 237 | char  **argv;
237                  if (singlepass)
238                          fin = stdin;
239                  else {
240 <                        tfname = mktemp(TEMPLATE);
240 >                        tfname = mktemp(template);
241                          if ((fin = fopen(tfname, "w+")) == NULL) {
242                                  fprintf(stderr, "%s: can't create ", progname);
243                                  fprintf(stderr, "temp file \"%s\"\n", tfname);
# Line 235 | Line 266 | char  **argv;
266                                  progname);
267                  quit(1);
268          }
269 +        if (NCSAMP < 3) {
270 +                fprintf(stderr, "%s: bad number of components\n", progname);
271 +                quit(1);
272 +        }
273                                          /* add new header info. */
274          printargs(i, argv, stdout);
275                                          /* get picture size */
276 <        if (fgetresolu(&xres, &yres, fin) != (YMAJOR|YDECR)) {
276 >        if ((order = fgetresolu(&xres, &yres, fin)) < 0) {
277                  fprintf(stderr, "%s: bad picture size\n", progname);
278                  quit(1);
279          }
280 +        if (!(order & YMAJOR))
281 +                inpaspect = 1.0/inpaspect;
282 +                                        /* wrap around for cylindrical view? */
283 +        wrapfilt = gotview && ourview.type == VT_CYL &&
284 +                        ourview.horiz >= 360.-FTINY && order & YMAJOR;
285                                          /* compute output resolution */
286          if (ncols <= 0)
287                  ncols = x_c*xres + .5;
# Line 273 | Line 313 | char  **argv;
313          }
314          pass2(fin);
315  
316 <        quit(0);
316 >        quit(estatus);
317 >        return estatus; /* pro forma return */
318   }
319  
320  
321 < headline(s)                             /* process line from header */
322 < char  *s;
321 > static double
322 > rgb_bright(
323 >        SCOLOR  clr
324 > )
325   {
326 <        char  fmt[32];
326 >        return(bright(clr));
327 > }
328  
329 +
330 + static double
331 + xyz_bright(
332 +        SCOLOR  clr
333 + )
334 + {
335 +        return(colval(clr,CIEY));
336 + }
337 +
338 +
339 + static double
340 + spec_bright(
341 +        SCOLOR  clr
342 + )
343 + {
344 +        return(pbright(clr));
345 + }
346 +
347 +
348 + brightfunc_t *ourbright = rgb_bright;
349 +
350 + static int
351 + headline(                               /* process line from header */
352 +        char    *s,
353 +        void    *p
354 + )
355 + {
356 +        char  fmt[MAXFMTLEN];
357 +
358          fputs(s, stdout);               /* copy to output */
359          if (isaspect(s))                /* get aspect ratio */
360                  inpaspect *= aspectval(s);
361 <        else if (isformat(s)) {
362 <                formatval(fmt, s);
363 <                wrongformat = strcmp(fmt, COLRFMT);
364 <        }
361 >        else if (isexpos(s))            /* get exposure */
362 >                hotlvl *= exposval(s);
363 >        else if (isncomp(s))            /* get #components (spectral) */
364 >                NCSAMP = ncompval(s);
365 >        else if (iswlsplit(s))          /* get wavelength partitions */
366 >                wlsplitval(WLPART, s);
367 >        else if (formatval(fmt, s)) {   /* get format */
368 >                wrongformat = 0;
369 >                if (!strcmp(COLRFMT, fmt))
370 >                        ourbright = rgb_bright;
371 >                else if (!strcmp(CIEFMT, fmt))
372 >                        ourbright = xyz_bright;
373 >                else if (!strcmp(SPECFMT, fmt))
374 >                        ourbright = spec_bright;
375 >                else
376 >                        wrongformat = !globmatch(PICFMT, fmt);
377 >        } else if (isview(s) && sscanview(&ourview, s) > 0)
378 >                gotview++;
379 >        return(0);
380   }
381  
382  
383 < copyfile(in, out)                       /* copy a file */
384 < register FILE  *in, *out;
383 > static void
384 > copyfile(                       /* copy a file */
385 >        FILE  *in,
386 >        FILE  *out
387 > )
388   {
389 <        register int  c;
389 >        int  c;
390  
391          while ((c = getc(in)) != EOF)
392                  putc(c, out);
# Line 307 | Line 398 | register FILE  *in, *out;
398   }
399  
400  
401 < pass1(in)                               /* first pass of picture file */
402 < FILE  *in;
401 > static void
402 > pass1(                          /* first pass of picture file */
403 >        FILE  *in
404 > )
405   {
406          int  i;
407 <        COLOR  *scan;
407 >        COLORV  *scan;
408  
409          pass1init();
410  
411 <        scan = (COLOR *)malloc(xres*sizeof(COLOR));
411 >        scan = (COLORV *)malloc(xres*NCSAMP*sizeof(COLORV));
412          if (scan == NULL) {
413                  fprintf(stderr, "%s: out of memory\n", progname);
414                  quit(1);
415          }
416          for (i = 0; i < yres; i++) {
417 <                if (freadscan(scan, xres, in) < 0) {
418 <                        nrows = nrows * i / yres;       /* adjust frame */
417 >                if (freadsscan(scan, NCSAMP, xres, in) < 0) {
418 >                        nrows = (long)nrows * i / yres; /* adjust frame */
419                          if (nrows <= 0) {
420                                  fprintf(stderr, "%s: empty frame\n", progname);
421                                  quit(1);
422                          }
423                          fprintf(stderr, "%s: warning - partial frame (%d%%)\n",
424 <                                        progname, 100*i/yres);
424 >                                        progname, (int)(100L*i/yres));
425                          yres = i;
426                          y_r = (double)nrows/yres;
427 +                        estatus++;
428                          break;
429                  }
430                  pass1scan(scan, i);
431          }
432 <        free((char *)scan);
432 >        free(scan);
433   }
434  
435  
436 < pass2(in)                       /* last pass on file, write to stdout */
437 < FILE  *in;
436 > static void
437 > pass2(                  /* last pass on file, write to stdout */
438 >        FILE  *in
439 > )
440   {
441          int  yread;
442          int  ycent, xcent;
443          int  r, c;
444 <        
444 >        
445          pass2init();
446          scan2init();
447          yread = 0;
448          for (r = 0; r < nrows; r++) {
449 <                ycent = (long)r*yres/nrows;
449 >                ycent = (r+.5)*yres/nrows;
450                  while (yread <= ycent+yrad) {
451                          if (yread < yres) {
452 <                                if (freadscan(scanin[yread%barsize],
453 <                                                xres, in) < 0) {
452 >                                if (freadsscan(scanin[yread%barsize],
453 >                                                NCSAMP, xres, in) < 0) {
454                                          fprintf(stderr,
455 <                                                "%s: bad read (y=%d)\n",
455 >                                                "%s: truncated input (y=%d)\n",
456                                                  progname, yres-1-yread);
457                                          quit(1);
458                                  }
# Line 364 | Line 460 | FILE  *in;
460                          }
461                          yread++;
462                  }
463 +                if (obarsize > 0)
464 +                        scan2sync(r);
465                  for (c = 0; c < ncols; c++) {
466 <                        xcent = (long)c*xres/ncols;
467 <                        if (rad <= 0.0)
468 <                                dobox(scanout[c], xcent, ycent, c, r);
466 >                        xcent = (c+.5)*xres/ncols;
467 >                        if (thresh > FTINY)
468 >                                dothresh(xcent, ycent, c, r);
469 >                        else if (rad > FTINY)
470 >                                dogauss(scanout+c*NCSAMP, xcent, ycent, c, r);
471                          else
472 <                                dogauss(scanout[c], xcent, ycent, c, r);
472 >                                dobox(scanout+c*NCSAMP, xcent, ycent, c, r);
473                  }
474 <                if (fwritescan(scanout, ncols, stdout) < 0) {
474 >                if (scanout != NULL && fwritesscan(scanout, NCSAMP, ncols, stdout) < 0) {
475                          fprintf(stderr, "%s: write error in pass2\n", progname);
476                          quit(1);
477                  }
478          }
479 <                                        /* skip leftovers */
479 >                                        /* skip leftover input */
480          while (yread < yres) {
481 <                if (freadscan(scanin[0], xres, in) < 0)
481 >                if (freadsscan(scanin[0], NCSAMP, xres, in) < 0)
482                          break;
483                  yread++;
484          }
485 +        scan2flush();                   /* flush output */
486   }
487  
488  
489 < scan2init()                     /* prepare scanline arrays */
489 > static void
490 > scan2init(void)                 /* prepare scanline arrays */
491   {
492          COLOR   ctmp;
493 <        double  d;
494 <        register int  i;
493 >        double  d;
494 >        int  i;
495  
496 <        if (rad <= 0.0) {
497 <                xrad = xres/ncols/2 + 1;
498 <                yrad = yres/nrows/2 + 1;
397 <        } else {
496 >        xbrad = xres/ncols/2 + 1;
497 >        ybrad = yres/nrows/2 + 1;
498 >        if (rad > FTINY) {
499                  if (nrows >= yres && ncols >= xres)
500                          rad *= (y_r + x_c)/2.0;
501  
502 <                xrad = CHECKRAD*rad/x_c + 1;
503 <                yrad = CHECKRAD*rad/y_r + 1;
504 <
502 >                if (thresh > FTINY) {
503 >                        orad = CHECKRAD*THRESHRAD*rad + 1;
504 >                        xrad = orad/x_c + xbrad;
505 >                        yrad = orad/y_r + ybrad;
506 >                        obarsize = 2*orad + 1;
507 >                } else {
508 >                        xrad = CHECKRAD*rad/x_c + 1;
509 >                        yrad = CHECKRAD*rad/y_r + 1;
510 >                }
511                  initmask();             /* initialize filter table */
512 +        } else {
513 +                xrad = xbrad;
514 +                yrad = ybrad;
515          }
516          barsize = 2*yrad + 1;
517 <        scanin = (COLOR **)malloc(barsize*sizeof(COLOR *));
517 >        scanin = (COLORV **)malloc(barsize*sizeof(COLORV *));
518 >        if (scanin == NULL)
519 >                goto memerr;
520          for (i = 0; i < barsize; i++) {
521 <                scanin[i] = (COLOR *)malloc(xres*sizeof(COLOR));
522 <                if (scanin[i] == NULL) {
523 <                        fprintf(stderr, "%s: out of memory\n", progname);
524 <                        quit(1);
521 >                scanin[i] = (COLORV *)malloc(xres*NCSAMP*sizeof(COLORV));
522 >                if (scanin[i] == NULL)
523 >                        goto memerr;
524 >        }
525 >        if (obarsize > 0) {
526 >                scoutbar = (COLORV **)malloc(obarsize*sizeof(COLORV *));
527 >                greybar = (float **)malloc(obarsize*sizeof(float *));
528 >                if ((scoutbar == NULL) | (greybar == NULL))
529 >                        goto memerr;
530 >                for (i = 0; i < obarsize; i++) {
531 >                        scoutbar[i] = (COLORV *)malloc(ncols*NCSAMP*sizeof(COLORV));
532 >                        greybar[i] = (float *)malloc(ncols*sizeof(float));
533 >                        if ((scoutbar[i] == NULL) | (greybar[i] == NULL))
534 >                                goto memerr;
535                  }
536 +        } else {
537 +                scanout = (COLORV *)malloc(ncols*NCSAMP*sizeof(COLORV));
538 +                if (scanout == NULL)
539 +                        goto memerr;
540          }
415        scanout = (COLOR *)malloc(ncols*sizeof(COLOR));
416        if (scanout == NULL) {
417                fprintf(stderr, "%s: out of memory\n", progname);
418                quit(1);
419        }
541                                          /* record pixel aspect ratio */
542          if (!correctaspect) {
543 <                d = x_c / y_r;
543 >                d = order & YMAJOR ? x_c/y_r : y_r/x_c ;
544                  if (!FEQ(d,1.0))
545                          fputaspect(d, stdout);
546          }
# Line 435 | Line 556 | scan2init()                    /* prepare scanline arrays */
556                  fputcolcor(ctmp, stdout);
557          printf("\n");
558                                          /* write out resolution */
559 <        fputresolu(YMAJOR|YDECR, ncols, nrows, stdout);
559 >        fputresolu(order, ncols, nrows, stdout);
560 >        return;
561 > memerr:
562 >        fprintf(stderr, "%s: out of memory\n", progname);
563 >        quit(1);
564   }
565  
566  
567 + static void
568 + scan2sync(                      /* synchronize grey averages and output scan */
569 +        int  r
570 + )
571 + {
572 +        static int  nextrow = 0;
573 +        SCOLOR  ctmp;
574 +        int  ybot;
575 +        int  c;
576 +                                        /* average input scanlines */
577 +        while (nextrow <= r+orad && nextrow < nrows) {
578 +                ybot = (nextrow+.5)*yres/nrows;
579 +                for (c = 0; c < ncols; c++) {
580 +                        dobox(ctmp, (int)((c+.5)*xres/ncols),ybot, c,nextrow);
581 +                        greybar[nextrow%obarsize][c] = (*ourbright)(ctmp);
582 +                }
583 +                                        /* and zero output scanline */
584 +                memset(scoutbar[nextrow%obarsize], 0, ncols*NCSAMP*sizeof(COLORV));
585 +                nextrow++;
586 +        }
587 +                                        /* point to top scanline for output */
588 +        if (r-orad >= 0)
589 +                scanout = scoutbar[(r-orad)%obarsize];
590 +        else
591 +                scanout = NULL;
592 + }
593 +
594 +
595 + static void
596 + scan2flush(void)                        /* flush output buffer */
597 + {
598 +        int  r;
599 +
600 +        for (r = nrows-orad; r < nrows; r++)
601 +                if (fwritesscan(scoutbar[r%obarsize], NCSAMP, ncols, stdout) < 0)
602 +                        break;
603 +        if (fflush(stdout) < 0) {
604 +                fprintf(stderr, "%s: write error at end of pass2\n", progname);
605 +                quit(1);
606 +        }
607 + }
608 +
609 +
610 + void
611   quit(code)              /* remove temporary file and exit */
612   int  code;
613   {

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines