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.20 by gwlarson, Tue Oct 27 09:08:26 1998 UTC

# Line 1 | Line 1
1 < /* Copyright (c) 1986 Regents of the University of California */
1 > /* Copyright (c) 1996 Regents of the University of California */
2  
3   #ifndef lint
4   static char SCCSid[] = "$SunId$ LBL";
# Line 8 | Line 8 | static char SCCSid[] = "$SunId$ LBL";
8   *  pfilt.c - program to post-process picture file.
9   *
10   *     9/26/85
11 + *     6/23/93  Added additional buffers for value spreading
12   */
13  
14 < #include  <stdio.h>
14 > #include  "standard.h"
15  
16   #include  <signal.h>
17  
18   #include  "color.h"
19  
20 < extern char  *malloc();
20 > #include  "view.h"
21  
22 < #define  CHECKRAD       1.5     /* radius to check for filtering */
22 > #include  "resolu.h"
23  
24 + #include  "paths.h"
25 +
26 + extern float  *matchlamp();
27 +
28 + #define  FEQ(a,b)       ((a) >= .98*(b) && (a) <= 1.02*(b))
29 +
30 + double   CHECKRAD = 1.5;        /* radius to check for filtering */
31 +
32 + #define  THRESHRAD      5.0     /* maximum sample spread in output */
33 +
34   COLOR  exposure = WHTCOLOR;     /* exposure for the frame */
35  
36 < double  rad = 0.0;              /* output pixel radius for filtering */
36 > double  rad = 0.0;              /* output pixel radius for filtering */
37  
38 + double  thresh = 0.0;           /* maximum contribution for subpixel */
39 +
40   int  nrows = 0;                 /* number of rows for output */
41   int  ncols = 0;                 /* number of columns for output */
42  
43 < double  x_c = 1.0;              /* ratio of output x size to input */
44 < double  y_r = 1.0;              /* ratio of output y size to input */
43 > double  x_c = 1.0;              /* ratio of output x size to input */
44 > double  y_r = 1.0;              /* ratio of output y size to input */
45  
46   int  singlepass = 0;            /* true means skip first pass */
47  
48   int  avghot = 0;                /* true means average in bright spots */
49  
50 < double  hotlvl = 1000.0;        /* level considered "hot" */
50 > double  hotlvl = 100.0;         /* level considered "hot" */
51  
52   int  npts = 0;                  /* (half) number of points for stars */
53  
54 < double  spread = 1e-4;          /* spread for star points */
54 > double  spread = 1e-4;          /* spread for star points */
55  
43 #define  TEMPLATE       "/usr/tmp/pfXXXXXX"
44
56   char  *tfname = NULL;
57  
58 + char  template[] = TEMPLATE;
59 +
60 + char  *lampdat = "lamp.tab";    /* lamp data file */
61 +
62 + int  order;                     /* scanline ordering of input */
63   int  xres, yres;                /* resolution of input */
64 < double  inpaspect = 1.0;        /* pixel aspect ratio of input */
64 > double  inpaspect = 1.0;        /* pixel aspect ratio of input */
65 > int  correctaspect = 0;         /* aspect ratio correction? */
66  
67 < int  xrad;                      /* x window size */
51 < int  yrad;                      /* y window size */
67 > int  wrongformat = 0;
68  
69 + VIEW  ourview = STDVIEW;
70 + int  gotview = 0;
71 + int  wrapfilt = 0;              /* wrap filter horizontally? */
72 +
73 + int  estatus = 0;               /* exit status (for non-fatal errors) */
74 +
75 + int  xrad;                      /* x search radius */
76 + int  yrad;                      /* y search radius */
77 + int  xbrad;                     /* x box size */
78 + int  ybrad;                     /* y box size */
79 +
80   int  barsize;                   /* size of input scan bar */
81   COLOR  **scanin;                /* input scan bar */
82   COLOR  *scanout;                /* output scan line */
83 + COLOR  **scoutbar;              /* output scan bar (if thresh > 0) */
84 + float  **greybar;               /* grey-averaged input values */
85 + int  obarsize = 0;              /* size of output scan bar */
86 + int  orad = 0;                  /* output window radius */
87  
88   char  *progname;
89  
# Line 61 | Line 92 | main(argc, argv)
92   int  argc;
93   char  **argv;
94   {
64        extern char  *mktemp();
65        extern double  atof(), pow();
95          extern long  ftell();
96          extern int  quit(), headline();
97          FILE  *fin;
98 +        float  *lampcolor;
99 +        char  *lamptype = NULL;
100          long  fpos;
101 <        double  outaspect = 0.0;
102 <        double  d;
103 <        int  i;
104 <
101 >        double  outaspect = 0.0;
102 >        double  d;
103 >        int  i, j;
104 > #ifdef MSDOS
105 >        extern int  _fmode;
106 >        _fmode = O_BINARY;
107 >        setmode(fileno(stdin), O_BINARY);
108 >        setmode(fileno(stdout), O_BINARY);
109 > #endif
110          if (signal(SIGINT, quit) == SIG_IGN)
111                  signal(SIGINT, SIG_IGN);
112          if (signal(SIGHUP, quit) == SIG_IGN)
113 <                signal(SIGINT, SIG_IGN);
113 >                signal(SIGHUP, SIG_IGN);
114          signal(SIGTERM, quit);
115          signal(SIGPIPE, quit);
116 < #ifdef  SIGXCPU
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 103 | Line 139 | char  **argv;
139                                  } else
140                                          nrows = atoi(argv[i]);
141                                  break;
142 +                        case 'c':
143 +                                correctaspect = !correctaspect;
144 +                                break;
145                          case 'p':
146                                  i++;
147                                  outaspect = atof(argv[i]);
# Line 112 | Line 151 | char  **argv;
151                                          d = pow(2.0, atof(argv[i+1]));
152                                  else
153                                          d = atof(argv[i+1]);
154 +                                if (d < 1e-20 || d > 1e20) {
155 +                                        fprintf(stderr,
156 +                                                "%s: exposure out of range\n",
157 +                                                        argv[0]);
158 +                                        quit(1);
159 +                                }
160                                  switch (argv[i][2]) {
161                                  case '\0':
162                                          scalecolor(exposure, d);
# Line 130 | Line 175 | char  **argv;
175                                  }
176                                  i++;
177                                  break;
178 +                        case 'f':
179 +                                lampdat = argv[++i];
180 +                                break;
181 +                        case 't':
182 +                                lamptype = argv[++i];
183 +                                break;
184                          case '1':
185                                  singlepass = 1;
186                                  break;
# Line 151 | 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 = 1.0;
209 +                                break;
210                          case 'b':
211 <                                rad = 0.0;
211 >                                rad = thresh = 0.0;
212                                  break;
213                          default:;
214                          badopt:
# Line 163 | Line 219 | char  **argv;
219                          }
220                  else
221                          break;
222 <                        
222 >                                        /* get lamp data (if necessary) */
223 >        if (lamptype != NULL) {
224 >                if (loadlamps(lampdat) < 0)
225 >                        quit(1);
226 >                if ((lampcolor = matchlamp(lamptype)) == NULL) {
227 >                        fprintf(stderr, "%s: unknown lamp type\n", lamptype);
228 >                        quit(1);
229 >                }
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 */
236          if (i == argc) {
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 191 | Line 260 | char  **argv;
260                  quit(1);
261          }
262                                          /* get header */
263 <        getheader(fin, headline);
263 >        getheader(fin, headline, NULL);
264 >        if (wrongformat) {
265 >                fprintf(stderr, "%s: input must be a Radiance picture\n",
266 >                                progname);
267 >                quit(1);
268 >        }
269                                          /* add new header info. */
270          printargs(i, argv, stdout);
271                                          /* get picture size */
272 <        if (fgetresolu(&xres, &yres, fin) != (YMAJOR|YDECR)) {
272 >        if ((order = fgetresolu(&xres, &yres, fin)) < 0) {
273                  fprintf(stderr, "%s: bad picture size\n", progname);
274                  quit(1);
275          }
276 +        if (!(order & YMAJOR))
277 +                inpaspect = 1.0/inpaspect;
278 +                                        /* wrap around for cylindrical view? */
279 +        wrapfilt = gotview && ourview.type == VT_CYL &&
280 +                        ourview.horiz >= 360.-FTINY && order & YMAJOR;
281                                          /* compute output resolution */
282          if (ncols <= 0)
283                  ncols = x_c*xres + .5;
# Line 230 | Line 309 | char  **argv;
309          }
310          pass2(fin);
311  
312 <        quit(0);
312 >        quit(estatus);
313   }
314  
315  
316 + double
317 + rgb_bright(clr)
318 + COLOR  clr;
319 + {
320 +        return(bright(clr));
321 + }
322 +
323 +
324 + double
325 + xyz_bright(clr)
326 + COLOR  clr;
327 + {
328 +        return(clr[CIEY]);
329 + }
330 +
331 +
332 + double  (*ourbright)() = rgb_bright;
333 +
334 +
335 + int
336   headline(s)                             /* process line from header */
337   char  *s;
338   {
339 +        char  fmt[32];
340 +
341          fputs(s, stdout);               /* copy to output */
342          if (isaspect(s))                /* get aspect ratio */
343                  inpaspect *= aspectval(s);
344 +        else if (isexpos(s))            /* get exposure */
345 +                hotlvl *= exposval(s);
346 +        else if (formatval(fmt, s)) {   /* get format */
347 +                wrongformat = 0;
348 +                if (!strcmp(COLRFMT, fmt))
349 +                        ourbright = rgb_bright;
350 +                else if (!strcmp(CIEFMT, fmt))
351 +                        ourbright = xyz_bright;
352 +                else
353 +                        wrongformat = !globmatch(PICFMT, fmt);
354 +        } else if (isview(s) && sscanview(&ourview, s) > 0)
355 +                gotview++;
356 +        return(0);
357   }
358  
359  
# Line 273 | Line 387 | FILE  *in;
387          }
388          for (i = 0; i < yres; i++) {
389                  if (freadscan(scan, xres, in) < 0) {
390 <                        nrows = nrows * i / yres;       /* adjust frame */
390 >                        nrows = (long)nrows * i / yres; /* adjust frame */
391                          if (nrows <= 0) {
392                                  fprintf(stderr, "%s: empty frame\n", progname);
393                                  quit(1);
394                          }
395                          fprintf(stderr, "%s: warning - partial frame (%d%%)\n",
396 <                                        progname, 100*i/yres);
396 >                                        progname, (int)(100L*i/yres));
397                          yres = i;
398 +                        y_r = (double)nrows/yres;
399 +                        estatus++;
400                          break;
401                  }
402                  pass1scan(scan, i);
# Line 295 | Line 411 | FILE  *in;
411          int  yread;
412          int  ycent, xcent;
413          int  r, c;
414 <        
414 >        
415          pass2init();
416          scan2init();
417          yread = 0;
418          for (r = 0; r < nrows; r++) {
419 <                ycent = (long)r*yres/nrows;
419 >                ycent = (r+.5)*yres/nrows;
420                  while (yread <= ycent+yrad) {
421                          if (yread < yres) {
422                                  if (freadscan(scanin[yread%barsize],
423                                                  xres, in) < 0) {
424                                          fprintf(stderr,
425 <                                                "%s: bad read (y=%d)\n",
425 >                                                "%s: truncated input (y=%d)\n",
426                                                  progname, yres-1-yread);
427                                          quit(1);
428                                  }
# Line 314 | Line 430 | FILE  *in;
430                          }
431                          yread++;
432                  }
433 +                if (obarsize > 0)
434 +                        scan2sync(r);
435                  for (c = 0; c < ncols; c++) {
436 <                        xcent = (long)c*xres/ncols;
437 <                        if (rad <= 0.0)
438 <                                dobox(scanout[c], xcent, ycent, c, r);
439 <                        else
436 >                        xcent = (c+.5)*xres/ncols;
437 >                        if (thresh > FTINY)
438 >                                dothresh(xcent, ycent, c, r);
439 >                        else if (rad > FTINY)
440                                  dogauss(scanout[c], xcent, ycent, c, r);
441 +                        else
442 +                                dobox(scanout[c], xcent, ycent, c, r);
443                  }
444 <                if (fwritescan(scanout, ncols, stdout) < 0) {
444 >                if (scanout != NULL && fwritescan(scanout, ncols, stdout) < 0) {
445                          fprintf(stderr, "%s: write error in pass2\n", progname);
446                          quit(1);
447                  }
448          }
449 <                                        /* skip leftovers */
449 >                                        /* skip leftover input */
450          while (yread < yres) {
451                  if (freadscan(scanin[0], xres, in) < 0)
452                          break;
453                  yread++;
454          }
455 +        scan2flush();                   /* flush output */
456   }
457  
458  
459   scan2init()                     /* prepare scanline arrays */
460   {
461 <        double  d;
461 >        COLOR   ctmp;
462 >        double  d;
463          register int  i;
464  
465 <        if (rad <= 0.0) {
466 <                xrad = xres/ncols/2 + 1;
467 <                yrad = yres/nrows/2 + 1;
346 <        } else {
465 >        xbrad = xres/ncols/2 + 1;
466 >        ybrad = yres/nrows/2 + 1;
467 >        if (rad > FTINY) {
468                  if (nrows >= yres && ncols >= xres)
469                          rad *= (y_r + x_c)/2.0;
470  
471 <                xrad = CHECKRAD*rad/x_c + 1;
472 <                yrad = CHECKRAD*rad/y_r + 1;
473 <
471 >                if (thresh > FTINY) {
472 >                        orad = CHECKRAD*THRESHRAD*rad + 1;
473 >                        xrad = orad/x_c + xbrad;
474 >                        yrad = orad/y_r + ybrad;
475 >                        obarsize = 2*orad + 1;
476 >                } else {
477 >                        xrad = CHECKRAD*rad/x_c + 1;
478 >                        yrad = CHECKRAD*rad/y_r + 1;
479 >                }
480                  initmask();             /* initialize filter table */
481 +        } else {
482 +                xrad = xbrad;
483 +                yrad = ybrad;
484          }
485 <        barsize = 2 * yrad;
485 >        barsize = 2*yrad + 1;
486          scanin = (COLOR **)malloc(barsize*sizeof(COLOR *));
487 +        if (scanin == NULL)
488 +                goto memerr;
489          for (i = 0; i < barsize; i++) {
490                  scanin[i] = (COLOR *)malloc(xres*sizeof(COLOR));
491 <                if (scanin[i] == NULL) {
492 <                        fprintf(stderr, "%s: out of memory\n", progname);
493 <                        quit(1);
491 >                if (scanin[i] == NULL)
492 >                        goto memerr;
493 >        }
494 >        if (obarsize > 0) {
495 >                scoutbar = (COLOR **)malloc(obarsize*sizeof(COLOR *));
496 >                greybar = (float **)malloc(obarsize*sizeof(float *));
497 >                if (scoutbar == NULL | greybar == NULL)
498 >                        goto memerr;
499 >                for (i = 0; i < obarsize; i++) {
500 >                        scoutbar[i] = (COLOR *)malloc(ncols*sizeof(COLOR));
501 >                        greybar[i] = (float *)malloc(ncols*sizeof(float));
502 >                        if (scoutbar[i] == NULL | greybar[i] == NULL)
503 >                                goto memerr;
504                  }
505 +        } else {
506 +                scanout = (COLOR *)malloc(ncols*sizeof(COLOR));
507 +                if (scanout == NULL)
508 +                        goto memerr;
509          }
510 <        scanout = (COLOR *)malloc(ncols*sizeof(COLOR));
511 <        if (scanout == NULL) {
512 <                fprintf(stderr, "%s: out of memory\n", progname);
513 <                quit(1);
510 >                                        /* record pixel aspect ratio */
511 >        if (!correctaspect) {
512 >                d = order & YMAJOR ? x_c/y_r : y_r/x_c ;
513 >                if (!FEQ(d,1.0))
514 >                        fputaspect(d, stdout);
515          }
516 <                                        /* record pixel aspect and exposure */
517 <        d = x_c / y_r;
518 <        if (d < .99 || d > 1.01)
372 <                fputaspect(d, stdout);
373 <        d = bright(exposure);
374 <        if (d < .995 || d > 1.005)
516 >                                        /* record exposure */
517 >        d = (*ourbright)(exposure);
518 >        if (!FEQ(d,1.0))
519                  fputexpos(d, stdout);
520 +                                        /* record color correction */
521 +        copycolor(ctmp, exposure);
522 +        scalecolor(ctmp, 1.0/d);
523 +        if (!FEQ(colval(ctmp,RED),colval(ctmp,GRN)) ||
524 +                        !FEQ(colval(ctmp,GRN),colval(ctmp,BLU)))
525 +                fputcolcor(ctmp, stdout);
526          printf("\n");
527 <        fputresolu(YMAJOR|YDECR, ncols, nrows, stdout); /* resolution */
527 >                                        /* write out resolution */
528 >        fputresolu(order, ncols, nrows, stdout);
529 >        return;
530 > memerr:
531 >        fprintf(stderr, "%s: out of memory\n", progname);
532 >        quit(1);
533 > }
534 >
535 >
536 > scan2sync(r)                    /* synchronize grey averages and output scan */
537 > int  r;
538 > {
539 >        static int  nextrow = 0;
540 >        COLOR  ctmp;
541 >        int  ybot;
542 >        register int  c;
543 >                                        /* average input scanlines */
544 >        while (nextrow <= r+orad && nextrow < nrows) {
545 >                ybot = (nextrow+.5)*yres/nrows;
546 >                for (c = 0; c < ncols; c++) {
547 >                        dobox(ctmp, (int)((c+.5)*xres/ncols),ybot, c,nextrow);
548 >                        greybar[nextrow%obarsize][c] = (*ourbright)(ctmp);
549 >                }
550 >                                        /* and zero output scanline */
551 >                bzero((char *)scoutbar[nextrow%obarsize], ncols*sizeof(COLOR));
552 >                nextrow++;
553 >        }
554 >                                        /* point to top scanline for output */
555 >        if (r-orad >= 0)
556 >                scanout = scoutbar[(r-orad)%obarsize];
557 >        else
558 >                scanout = NULL;
559 > }
560 >
561 >
562 > scan2flush()                    /* flush output buffer */
563 > {
564 >        register int  r;
565 >
566 >        for (r = nrows-orad; r < nrows; r++)
567 >                if (fwritescan(scoutbar[r%obarsize], ncols, stdout) < 0)
568 >                        break;
569 >        if (fflush(stdout) < 0) {
570 >                fprintf(stderr, "%s: write error at end of pass2\n", progname);
571 >                quit(1);
572 >        }
573   }
574  
575  

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines