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.10 by greg, Thu Sep 13 09:46:00 1990 UTC vs.
Revision 2.26 by schorsch, Sun Jul 27 22:12:03 2003 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  <string.h>
15  
16 + #include  "standard.h"
17 + #include  "platform.h"
18   #include  "color.h"
19 + #include  "view.h"
20 + #include  "paths.h"
21  
22 < extern char  *malloc();
22 > extern float  *matchlamp();
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  
# Line 61 | Line 88 | main(argc, argv)
88   int  argc;
89   char  **argv;
90   {
91 <        extern char  *mktemp();
65 <        extern double  atof(), pow();
66 <        extern long  ftell();
67 <        extern int  quit(), headline();
91 >        extern int  headline();
92          FILE  *fin;
93 +        float  *lampcolor;
94 +        char  *lamptype = NULL;
95          long  fpos;
96 <        double  outaspect = 0.0;
97 <        double  d;
98 <        int  i;
99 <
96 >        double  outaspect = 0.0;
97 >        double  d;
98 >        int  i, j;
99 >        SET_DEFAULT_BINARY();
100 >        SET_FILE_BINARY(stdin);
101 >        SET_FILE_BINARY(stdout);
102          if (signal(SIGINT, quit) == SIG_IGN)
103                  signal(SIGINT, SIG_IGN);
104 + #ifdef SIGHUP
105          if (signal(SIGHUP, quit) == SIG_IGN)
106 <                signal(SIGINT, SIG_IGN);
106 >                signal(SIGHUP, SIG_IGN);
107 > #endif
108          signal(SIGTERM, quit);
109 + #ifdef SIGPIPE
110          signal(SIGPIPE, quit);
111 < #ifdef  SIGXCPU
111 > #endif
112 > #ifdef  SIGXCPU
113          signal(SIGXCPU, quit);
114          signal(SIGXFSZ, quit);
115   #endif
116  
117 <        progname = argv[0];
117 >        progname = argv[0] = fixargv0(argv[0]);
118  
119          for (i = 1; i < argc; i++)
120                  if (argv[i][0] == '-')
# Line 103 | Line 135 | char  **argv;
135                                  } else
136                                          nrows = atoi(argv[i]);
137                                  break;
138 +                        case 'c':
139 +                                correctaspect = !correctaspect;
140 +                                break;
141                          case 'p':
142                                  i++;
143                                  outaspect = atof(argv[i]);
# Line 112 | Line 147 | char  **argv;
147                                          d = pow(2.0, atof(argv[i+1]));
148                                  else
149                                          d = atof(argv[i+1]);
150 +                                if (d < 1e-20 || d > 1e20) {
151 +                                        fprintf(stderr,
152 +                                                "%s: exposure out of range\n",
153 +                                                        argv[0]);
154 +                                        quit(1);
155 +                                }
156                                  switch (argv[i][2]) {
157                                  case '\0':
158                                          scalecolor(exposure, d);
# Line 130 | Line 171 | char  **argv;
171                                  }
172                                  i++;
173                                  break;
174 +                        case 'f':
175 +                                lampdat = argv[++i];
176 +                                break;
177 +                        case 't':
178 +                                lamptype = argv[++i];
179 +                                break;
180                          case '1':
181                                  singlepass = 1;
182                                  break;
# Line 151 | Line 198 | char  **argv;
198                          case 'r':
199                                  rad = atof(argv[++i]);
200                                  break;
201 +                        case 'm':
202 +                                thresh = atof(argv[++i]);
203 +                                if (rad <= FTINY)
204 +                                        rad = 0.6;
205 +                                break;
206                          case 'b':
207 <                                rad = 0.0;
207 >                                rad = thresh = 0.0;
208                                  break;
209                          default:;
210                          badopt:
# Line 163 | Line 215 | char  **argv;
215                          }
216                  else
217                          break;
218 <                        
218 >                                        /* get lamp data (if necessary) */
219 >        if (lamptype != NULL) {
220 >                if (loadlamps(lampdat) < 0)
221 >                        quit(1);
222 >                if ((lampcolor = matchlamp(lamptype)) == NULL) {
223 >                        fprintf(stderr, "%s: unknown lamp type\n", lamptype);
224 >                        quit(1);
225 >                }
226 >                for (j = 0; j < 3; j++)
227 >                        if (lampcolor[j] > 1e-4)
228 >                                colval(exposure,j) /= lampcolor[j];
229 >                freelamps();
230 >        }
231 >                                        /* open input file */
232          if (i == argc) {
233                  if (singlepass)
234                          fin = stdin;
235                  else {
236 <                        tfname = mktemp(TEMPLATE);
236 >                        tfname = mktemp(template);
237                          if ((fin = fopen(tfname, "w+")) == NULL) {
238                                  fprintf(stderr, "%s: can't create ", progname);
239                                  fprintf(stderr, "temp file \"%s\"\n", tfname);
# Line 191 | Line 256 | char  **argv;
256                  quit(1);
257          }
258                                          /* get header */
259 <        getheader(fin, headline);
259 >        getheader(fin, headline, NULL);
260 >        if (wrongformat) {
261 >                fprintf(stderr, "%s: input must be a Radiance picture\n",
262 >                                progname);
263 >                quit(1);
264 >        }
265                                          /* add new header info. */
266          printargs(i, argv, stdout);
267                                          /* get picture size */
268 <        if (fgetresolu(&xres, &yres, fin) != (YMAJOR|YDECR)) {
268 >        if ((order = fgetresolu(&xres, &yres, fin)) < 0) {
269                  fprintf(stderr, "%s: bad picture size\n", progname);
270                  quit(1);
271          }
272 +        if (!(order & YMAJOR))
273 +                inpaspect = 1.0/inpaspect;
274 +                                        /* wrap around for cylindrical view? */
275 +        wrapfilt = gotview && ourview.type == VT_CYL &&
276 +                        ourview.horiz >= 360.-FTINY && order & YMAJOR;
277                                          /* compute output resolution */
278          if (ncols <= 0)
279                  ncols = x_c*xres + .5;
# Line 230 | Line 305 | char  **argv;
305          }
306          pass2(fin);
307  
308 <        quit(0);
308 >        quit(estatus);
309   }
310  
311  
312 + double
313 + rgb_bright(clr)
314 + COLOR  clr;
315 + {
316 +        return(bright(clr));
317 + }
318 +
319 +
320 + double
321 + xyz_bright(clr)
322 + COLOR  clr;
323 + {
324 +        return(clr[CIEY]);
325 + }
326 +
327 +
328 + double  (*ourbright)() = rgb_bright;
329 +
330 +
331 + int
332   headline(s)                             /* process line from header */
333   char  *s;
334   {
335 +        char  fmt[32];
336 +
337          fputs(s, stdout);               /* copy to output */
338          if (isaspect(s))                /* get aspect ratio */
339                  inpaspect *= aspectval(s);
340 +        else if (isexpos(s))            /* get exposure */
341 +                hotlvl *= exposval(s);
342 +        else if (formatval(fmt, s)) {   /* get format */
343 +                wrongformat = 0;
344 +                if (!strcmp(COLRFMT, fmt))
345 +                        ourbright = rgb_bright;
346 +                else if (!strcmp(CIEFMT, fmt))
347 +                        ourbright = xyz_bright;
348 +                else
349 +                        wrongformat = !globmatch(PICFMT, fmt);
350 +        } else if (isview(s) && sscanview(&ourview, s) > 0)
351 +                gotview++;
352 +        return(0);
353   }
354  
355  
# Line 273 | Line 383 | FILE  *in;
383          }
384          for (i = 0; i < yres; i++) {
385                  if (freadscan(scan, xres, in) < 0) {
386 <                        nrows = nrows * i / yres;       /* adjust frame */
386 >                        nrows = (long)nrows * i / yres; /* adjust frame */
387                          if (nrows <= 0) {
388                                  fprintf(stderr, "%s: empty frame\n", progname);
389                                  quit(1);
390                          }
391                          fprintf(stderr, "%s: warning - partial frame (%d%%)\n",
392 <                                        progname, 100*i/yres);
392 >                                        progname, (int)(100L*i/yres));
393                          yres = i;
394 +                        y_r = (double)nrows/yres;
395 +                        estatus++;
396                          break;
397                  }
398                  pass1scan(scan, i);
399          }
400 <        free((char *)scan);
400 >        free((void *)scan);
401   }
402  
403  
# Line 295 | Line 407 | FILE  *in;
407          int  yread;
408          int  ycent, xcent;
409          int  r, c;
410 <        
410 >        
411          pass2init();
412          scan2init();
413          yread = 0;
414          for (r = 0; r < nrows; r++) {
415 <                ycent = (long)r*yres/nrows;
415 >                ycent = (r+.5)*yres/nrows;
416                  while (yread <= ycent+yrad) {
417                          if (yread < yres) {
418                                  if (freadscan(scanin[yread%barsize],
419                                                  xres, in) < 0) {
420                                          fprintf(stderr,
421 <                                                "%s: bad read (y=%d)\n",
421 >                                                "%s: truncated input (y=%d)\n",
422                                                  progname, yres-1-yread);
423                                          quit(1);
424                                  }
# Line 314 | Line 426 | FILE  *in;
426                          }
427                          yread++;
428                  }
429 +                if (obarsize > 0)
430 +                        scan2sync(r);
431                  for (c = 0; c < ncols; c++) {
432 <                        xcent = (long)c*xres/ncols;
433 <                        if (rad <= 0.0)
434 <                                dobox(scanout[c], xcent, ycent, c, r);
435 <                        else
432 >                        xcent = (c+.5)*xres/ncols;
433 >                        if (thresh > FTINY)
434 >                                dothresh(xcent, ycent, c, r);
435 >                        else if (rad > FTINY)
436                                  dogauss(scanout[c], xcent, ycent, c, r);
437 +                        else
438 +                                dobox(scanout[c], xcent, ycent, c, r);
439                  }
440 <                if (fwritescan(scanout, ncols, stdout) < 0) {
440 >                if (scanout != NULL && fwritescan(scanout, ncols, stdout) < 0) {
441                          fprintf(stderr, "%s: write error in pass2\n", progname);
442                          quit(1);
443                  }
444          }
445 <                                        /* skip leftovers */
445 >                                        /* skip leftover input */
446          while (yread < yres) {
447                  if (freadscan(scanin[0], xres, in) < 0)
448                          break;
449                  yread++;
450          }
451 +        scan2flush();                   /* flush output */
452   }
453  
454  
455   scan2init()                     /* prepare scanline arrays */
456   {
457 <        double  d;
457 >        COLOR   ctmp;
458 >        double  d;
459          register int  i;
460  
461 <        if (rad <= 0.0) {
462 <                xrad = xres/ncols/2 + 1;
463 <                yrad = yres/nrows/2 + 1;
346 <        } else {
461 >        xbrad = xres/ncols/2 + 1;
462 >        ybrad = yres/nrows/2 + 1;
463 >        if (rad > FTINY) {
464                  if (nrows >= yres && ncols >= xres)
465                          rad *= (y_r + x_c)/2.0;
466  
467 <                xrad = CHECKRAD*rad/x_c + 1;
468 <                yrad = CHECKRAD*rad/y_r + 1;
469 <
467 >                if (thresh > FTINY) {
468 >                        orad = CHECKRAD*THRESHRAD*rad + 1;
469 >                        xrad = orad/x_c + xbrad;
470 >                        yrad = orad/y_r + ybrad;
471 >                        obarsize = 2*orad + 1;
472 >                } else {
473 >                        xrad = CHECKRAD*rad/x_c + 1;
474 >                        yrad = CHECKRAD*rad/y_r + 1;
475 >                }
476                  initmask();             /* initialize filter table */
477 +        } else {
478 +                xrad = xbrad;
479 +                yrad = ybrad;
480          }
481 <        barsize = 2 * yrad;
481 >        barsize = 2*yrad + 1;
482          scanin = (COLOR **)malloc(barsize*sizeof(COLOR *));
483 +        if (scanin == NULL)
484 +                goto memerr;
485          for (i = 0; i < barsize; i++) {
486                  scanin[i] = (COLOR *)malloc(xres*sizeof(COLOR));
487 <                if (scanin[i] == NULL) {
488 <                        fprintf(stderr, "%s: out of memory\n", progname);
489 <                        quit(1);
487 >                if (scanin[i] == NULL)
488 >                        goto memerr;
489 >        }
490 >        if (obarsize > 0) {
491 >                scoutbar = (COLOR **)malloc(obarsize*sizeof(COLOR *));
492 >                greybar = (float **)malloc(obarsize*sizeof(float *));
493 >                if ((scoutbar == NULL) | (greybar == NULL))
494 >                        goto memerr;
495 >                for (i = 0; i < obarsize; i++) {
496 >                        scoutbar[i] = (COLOR *)malloc(ncols*sizeof(COLOR));
497 >                        greybar[i] = (float *)malloc(ncols*sizeof(float));
498 >                        if ((scoutbar[i] == NULL) | (greybar[i] == NULL))
499 >                                goto memerr;
500                  }
501 +        } else {
502 +                scanout = (COLOR *)malloc(ncols*sizeof(COLOR));
503 +                if (scanout == NULL)
504 +                        goto memerr;
505          }
506 <        scanout = (COLOR *)malloc(ncols*sizeof(COLOR));
507 <        if (scanout == NULL) {
508 <                fprintf(stderr, "%s: out of memory\n", progname);
509 <                quit(1);
506 >                                        /* record pixel aspect ratio */
507 >        if (!correctaspect) {
508 >                d = order & YMAJOR ? x_c/y_r : y_r/x_c ;
509 >                if (!FEQ(d,1.0))
510 >                        fputaspect(d, stdout);
511          }
512 <                                        /* record pixel aspect and exposure */
513 <        d = x_c / y_r;
514 <        if (d < .99 || d > 1.01)
372 <                fputaspect(d, stdout);
373 <        d = bright(exposure);
374 <        if (d < .995 || d > 1.005)
512 >                                        /* record exposure */
513 >        d = (*ourbright)(exposure);
514 >        if (!FEQ(d,1.0))
515                  fputexpos(d, stdout);
516 +                                        /* record color correction */
517 +        copycolor(ctmp, exposure);
518 +        scalecolor(ctmp, 1.0/d);
519 +        if (!FEQ(colval(ctmp,RED),colval(ctmp,GRN)) ||
520 +                        !FEQ(colval(ctmp,GRN),colval(ctmp,BLU)))
521 +                fputcolcor(ctmp, stdout);
522          printf("\n");
523 <        fputresolu(YMAJOR|YDECR, ncols, nrows, stdout); /* resolution */
523 >                                        /* write out resolution */
524 >        fputresolu(order, ncols, nrows, stdout);
525 >        return;
526 > memerr:
527 >        fprintf(stderr, "%s: out of memory\n", progname);
528 >        quit(1);
529   }
530  
531  
532 + scan2sync(r)                    /* synchronize grey averages and output scan */
533 + int  r;
534 + {
535 +        static int  nextrow = 0;
536 +        COLOR  ctmp;
537 +        int  ybot;
538 +        register int  c;
539 +                                        /* average input scanlines */
540 +        while (nextrow <= r+orad && nextrow < nrows) {
541 +                ybot = (nextrow+.5)*yres/nrows;
542 +                for (c = 0; c < ncols; c++) {
543 +                        dobox(ctmp, (int)((c+.5)*xres/ncols),ybot, c,nextrow);
544 +                        greybar[nextrow%obarsize][c] = (*ourbright)(ctmp);
545 +                }
546 +                                        /* and zero output scanline */
547 +                memset((char *)scoutbar[nextrow%obarsize], '\0', ncols*sizeof(COLOR));
548 +                nextrow++;
549 +        }
550 +                                        /* point to top scanline for output */
551 +        if (r-orad >= 0)
552 +                scanout = scoutbar[(r-orad)%obarsize];
553 +        else
554 +                scanout = NULL;
555 + }
556 +
557 +
558 + scan2flush()                    /* flush output buffer */
559 + {
560 +        register int  r;
561 +
562 +        for (r = nrows-orad; r < nrows; r++)
563 +                if (fwritescan(scoutbar[r%obarsize], ncols, stdout) < 0)
564 +                        break;
565 +        if (fflush(stdout) < 0) {
566 +                fprintf(stderr, "%s: write error at end of pass2\n", progname);
567 +                quit(1);
568 +        }
569 + }
570 +
571 +
572 + void
573   quit(code)              /* remove temporary file and exit */
574   int  code;
575   {

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines